-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modified subset to be a generic named subset_by_sequence.
This gives us some more flexibility for registering downstream classes.
- Loading branch information
Showing
5 changed files
with
52 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import Any, Sequence, Union | ||
from functools import singledispatch | ||
import numpy | ||
|
||
|
||
@singledispatch | ||
def subset_sequence(x: Any, indices: Sequence) -> Any: | ||
""" | ||
Subset ``x`` by ``indices`` to obtain a new object with the desired | ||
subset of elements. This attempts to use ``x``'s ``__getitem__`` method. | ||
Args: | ||
x: | ||
Any object that supports ``__getitem__`` with an integer sequence. | ||
indices: | ||
Sequence of non-negative integers specifying the integers of interest. | ||
Returns: | ||
The result of slicing ``x`` by ``indices``. The exact type | ||
depends on what ``x``'s ``__getitem__`` method returns. | ||
""" | ||
return x[indices] | ||
|
||
|
||
@subset_sequence.register | ||
def _subset_sequence_list(x: list, indices: Sequence) -> list: | ||
return [x[i] for i in indices] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from biocutils import subset_sequence | ||
import numpy as np | ||
|
||
|
||
def test_subset_list(): | ||
x = [1, 2, 3, 4, 5] | ||
assert subset_sequence(x, [0, 2, 4]) == [1, 3, 5] | ||
|
||
x = [1, 2, 3, 4, 5] | ||
assert subset_sequence(x, range(5)) == x | ||
|
||
x = [1, 2, 3, 4, 5] | ||
assert subset_sequence(x, range(4, -1, -1)) == [5, 4, 3, 2, 1] | ||
|
||
|
||
def test_subset_numpy(): | ||
y = np.random.rand(10) | ||
assert (subset_sequence(y, range(5)) == y[0:5]).all() | ||
|
||
y = np.random.rand(10, 20) | ||
assert (subset_sequence(y, range(5)) == y[0:5, :]).all() |