Skip to content

Commit

Permalink
Fix subsetting when slice arguments are numpy vectors (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche authored Jan 6, 2025
1 parent a1339b8 commit e35209d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Changelog

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

- Add wrapper methods to combine Summarized and RangedSummarized by rows or columns.
- Implement getters and setters to access and modify an assay.
- 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
6 changes: 3 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ python_requires = >=3.9
# For more information, check out https://semver.org/.
install_requires =
importlib-metadata; python_version<"3.8"
genomicranges>=0.4.18
biocframe>=0.5.10
biocutils>=0.1.4
genomicranges>=0.5.1
biocframe>=0.6.2
biocutils>=0.2.1

[options.packages.find]
where = src
Expand Down
22 changes: 11 additions & 11 deletions src/summarizedexperiment/BaseSE.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ def set_assay(self, name: str, assay: Any, in_place: bool = False) -> "BaseSE":
or as a reference to the (in-place-modified) original.
"""
if assay.shape != self.shape:
raise ValueError("Porvided assay does not match the dimensions of the experiment.")
raise ValueError("Provided assay does not match the dimensions of the experiment.")

output = self._define_output(in_place)
if in_place is False:
Expand All @@ -1000,14 +1000,14 @@ def set_assay(self, name: str, assay: Any, in_place: bool = False) -> "BaseSE":

def _normalize_row_slice(self, rows: Union[str, int, bool, Sequence]):
_scalar = None
if rows != slice(None):
if not (isinstance(rows, slice) and rows == slice(None)):
rows, _scalar = ut.normalize_subscript(rows, len(self._rows), self._row_names)

return rows, _scalar

def _normalize_column_slice(self, columns: Union[str, int, bool, Sequence]):
_scalar = None
if columns != slice(None):
if not (isinstance(columns, slice) and columns == slice(None)):
columns, _scalar = ut.normalize_subscript(columns, len(self._cols), self._column_names)

return columns, _scalar
Expand Down Expand Up @@ -1056,10 +1056,10 @@ def subset_assays(

new_assays = OrderedDict()
for asy, mat in self.assays.items():
if rows != slice(None):
if not (isinstance(rows, slice) and rows == slice(None)):
mat = mat[rows, :]

if columns != slice(None):
if not (isinstance(columns, slice) and columns == slice(None)):
mat = mat[:, columns]

new_assays[asy] = mat
Expand Down Expand Up @@ -1105,19 +1105,19 @@ def _generic_slice(
if columns is None:
columns = slice(None)

if rows is not None:
if not (isinstance(rows, slice) and rows == slice(None)):
rows, _ = self._normalize_row_slice(rows=rows)
new_rows = new_rows[rows, :]
new_rows = ut.subset(new_rows, rows)

if new_row_names is not None:
new_row_names = new_row_names[rows]
new_row_names = ut.subset_sequence(new_row_names, rows)

if columns is not None and self.column_data is not None:
if not (isinstance(columns, slice) and columns == slice(None)) and self.column_data is not None:
columns, _ = self._normalize_column_slice(columns=columns)
new_cols = new_cols[columns, :]
new_cols = ut.subset(new_cols, columns)

if new_col_names is not None:
new_col_names = new_col_names[columns]
new_col_names = ut.subset_sequence(new_col_names, columns)

new_assays = self.subset_assays(rows=rows, columns=columns)

Expand Down
4 changes: 2 additions & 2 deletions src/summarizedexperiment/RangedSummarizedExperiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ def get_slice(
slicer = self._generic_slice(rows=rows, columns=columns)

new_row_ranges = None
if slicer.row_indices != slice(None):
new_row_ranges = self.row_ranges[slicer.row_indices]
if not (isinstance(slicer.row_indices, slice) and slicer.row_indices == slice(None)):
new_row_ranges = ut.subset_sequence(self.row_ranges, slicer.row_indices)

current_class_const = type(self)
return current_class_const(
Expand Down

0 comments on commit e35209d

Please sign in to comment.