From dcfd76fd42c34e3458a10b174cb33fdc0899bd8d Mon Sep 17 00:00:00 2001 From: Jayaram Kancherla Date: Fri, 2 Jan 2026 17:52:49 -0800 Subject: [PATCH] Fix the base extract_subset --- CHANGELOG.md | 2 +- src/compressed_lists/base.py | 34 +++++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b75685b..17c9ac5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## Version 0.4.0 - 0.4.2 +## Version 0.4.0 - 0.4.3 - Classes extend `BiocObject` from biocutils. `metadata` is a named list. - Update actions to run from 3.10-3.14 diff --git a/src/compressed_lists/base.py b/src/compressed_lists/base.py index 9211115..f9983c5 100644 --- a/src/compressed_lists/base.py +++ b/src/compressed_lists/base.py @@ -378,7 +378,10 @@ def __getitem__(self, key: Union[int, str, slice]) -> Any: return self.extract_range(start, end) # slices - elif isinstance(key, slice): + elif isinstance(key, (range, slice)): + if isinstance(key, range): + key = slice(key.start, key.stop, key.step) + indices = range(*key.indices(len(self))) result = [] for i in indices: @@ -531,6 +534,10 @@ def extract_subset(self, indices: Sequence[int]) -> CompressedList: Returns: A new CompressedList with only the selected elements. """ + print("here", indices, type(indices)) + if isinstance(indices, np.ndarray): + indices = indices.tolist() + # Validate indices for i in indices: if i < 0 or i >= len(self): @@ -544,16 +551,25 @@ def extract_subset(self, indices: Sequence[int]) -> CompressedList: new_partitioning = Partitioning.from_lengths(new_lengths, new_names) # Extract data - new_data = [] + _new_data = [] for i in indices: start, end = self._partitioning.get_partition_range(i) - if isinstance(self._unlist_data, np.ndarray): - new_data.append(self._unlist_data[start:end]) - else: - new_data.extend(self._unlist_data[start:end]) - - if isinstance(self._unlist_data, np.ndarray): - new_data = np.concatenate(new_data) + _subset = ut.subset_sequence(self._unlist_data, [j for j in range(start, end)]) + _new_data.append(_subset) + # if isinstance(self._unlist_data, np.ndarray): + # new_data.append(self._unlist_data[start:end]) + # else: + # new_data.extend(self._unlist_data[start:end]) + + # if isinstance(self._unlist_data, np.ndarray): + # new_data = np.concatenate(new_data) + + if len(_new_data) == 1: + new_data = _new_data[0] + elif len(_new_data) > 0: + new_data = ut.combine_sequences(*_new_data) + else: + new_data = [] current_class_const = type(self) return current_class_const(