Skip to content

Commit

Permalink
recycle indices vector. fix slicing with empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche committed Jan 28, 2025
1 parent 0e10bdd commit 6dc81fc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
9 changes: 2 additions & 7 deletions src/genomicranges/GenomicRangesList.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,12 +847,6 @@ def __getitem__(self, args: Union[str, int, tuple, list, slice]) -> Union[Genomi
else:
idx, _ = ut.normalize_subscript(args, len(self), self._names)

if ut.is_list_of_type(idx, bool):
if len(idx) != len(self):
raise ValueError("`indices` is a boolean vector, length should match the size of the data.")

idx = [i for i in range(len(idx)) if idx[i] is True]

new_ranges = ut.subset_sequence(self._ranges, idx)
new_range_lengths = ut.subset_sequence(self._range_lengths, idx)

Expand All @@ -864,7 +858,8 @@ def __getitem__(self, args: Union[str, int, tuple, list, slice]) -> Union[Genomi
if self.mcols is not None:
new_mcols = ut.subset(self.mcols, idx)

return GenomicRangesList(new_ranges, new_range_lengths, new_names, new_mcols, self._metadata)
current_class_const = type(self)
return current_class_const(new_ranges, new_range_lengths, new_names, new_mcols, self._metadata)

#######################################
######>> class initializers <<#########
Expand Down
5 changes: 3 additions & 2 deletions tests/test_grl_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ def test_slice_by_bool():
assert isinstance(sgrl, GenomicRangesList)
assert len(sgrl) == 1

with pytest.raises(Exception):
grl[[False]]
empty = grl[[False]]
assert isinstance(empty, GenomicRangesList)
assert len(empty) == 0


def test_is_empty_True():
Expand Down

0 comments on commit 6dc81fc

Please sign in to comment.