Skip to content

Commit

Permalink
Fix slicing when subset args are numpy vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche committed Jan 6, 2025
1 parent ef95a64 commit 0826671
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Changelog

## Version 0.5.1 - 0.5.2
## Version 0.5.1 - 0.5.3

- Add wrapper class methods to combine experiments by rows or columns.
- Expand function names for readability, still backwards compatible with the older function and method names.
- Add getters and setters to replace a specific alternative experiment or reduced dimension.
- Fixed an issue with numpy arrays as slice arguments. Code now uses Biocutils's subset functions to perform these operations.


## Version 0.5.0

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ python_requires = >=3.9
# For more information, check out https://semver.org/.
install_requires =
importlib-metadata; python_version<"3.8"
summarizedexperiment>=0.5.1
summarizedexperiment>=0.5.3

[options.packages.find]
where = src
Expand Down
17 changes: 8 additions & 9 deletions src/singlecellexperiment/SingleCellExperiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,41 +980,40 @@ def get_slice(
rows: Optional[Union[str, int, bool, Sequence]],
columns: Optional[Union[str, int, bool, Sequence]],
) -> "SingleCellExperiment":
"""Alias for :py:attr:`~__getitem__`, for back-compatibility."""
"""Alias for :py:attr:`~__getitem__`."""

slicer = self._generic_slice(rows=rows, columns=columns)
do_slice_rows = not (isinstance(slicer.row_indices, slice) and slicer.row_indices == slice(None))
do_slice_cols = not (isinstance(slicer.col_indices, slice) and slicer.col_indices == slice(None))

new_row_ranges = None
if slicer.row_indices != slice(None):
if do_slice_rows:
new_row_ranges = self._row_ranges[slicer.row_indices]

new_reduced_dims = {}
for rdim, rmat in self._reduced_dims.items():
if slicer.col_indices != slice(None):
if do_slice_cols:
rmat = rmat[slicer.col_indices, :]

new_reduced_dims[rdim] = rmat

new_alt_expts = {}
for altname, altexpt in self._alternative_experiments.items():
if slicer.row_indices != slice(None):
altexpt = altexpt[slicer.row_indices, :]

if slicer.col_indices != slice(None):
if do_slice_cols:
altexpt = altexpt[:, slicer.col_indices]

new_alt_expts[altname] = altexpt

new_row_pairs = {}
for rname, rpair in self._row_pairs.items():
if slicer.row_indices != slice(None):
if do_slice_rows:
rpair = rpair[slicer.row_indices, :]

new_row_pairs[rname] = rpair

new_col_pairs = {}
for cname, cpair in self._column_pairs.items():
if slicer.col_indices != slice(None):
if do_slice_cols:
cpair = cpair[:, slicer.col_indices]

new_col_pairs[cname] = cpair
Expand Down
27 changes: 26 additions & 1 deletion tests/test_sce_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ def test_SCE_slice():

assert tse_slice.assay("counts").shape == (10, 3)

def test_SCE_slice_with_numpy():
tse = SingleCellExperiment(
assays={"counts": counts},
row_data=row_data,
column_data=col_data,
reduced_dims={"random_embeds": np.random.rand(ncols, 5)},
)

tse_slice = tse[np.arange(10), 0:3]
assert tse_slice is not None
assert isinstance(tse_slice, sce)

assert len(tse_slice.row_data) == 10
assert len(tse_slice.col_data) == 3

assert tse_slice.assay("counts").shape == (10, 3)

tse_slice = tse[np.arange(10), np.arange(3)]
assert tse_slice is not None
assert isinstance(tse_slice, sce)

assert len(tse_slice.row_data) == 10
assert len(tse_slice.col_data) == 3

assert tse_slice.assay("counts").shape == (10, 3)

def test_SCE_creation_with_alts_slice():
trse = SummarizedExperiment(
Expand All @@ -91,4 +116,4 @@ def test_SCE_creation_with_alts_slice():

assert tsce_slice.assay("counts").shape == (10, 3)
alt_exp = tsce_slice.alternative_experiments["alt"]
assert alt_exp.shape == (10, 3)
assert alt_exp.shape == (200, 3)

0 comments on commit 0826671

Please sign in to comment.