From 6fabd004ddbe6a03df5897de4cb2a8860487de36 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Wed, 6 Dec 2023 14:54:54 +0100 Subject: [PATCH 1/4] improve docstring --- src/spikeinterface/core/base.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/spikeinterface/core/base.py b/src/spikeinterface/core/base.py index ee7671c6ed..3b7fa66471 100644 --- a/src/spikeinterface/core/base.py +++ b/src/spikeinterface/core/base.py @@ -90,17 +90,27 @@ def _check_segment_index(self, segment_index: Optional[int] = None) -> int: else: return segment_index - def ids_to_indices(self, ids: Iterable, prefer_slice: bool = False) -> Union[np.ndarray, slice]: + def ids_to_indices(self, ids: list[str, int], prefer_slice: bool = False) -> Union[np.ndarray, slice]: """ - Transform a ids list (aka channel_ids or unit_ids) - into a indices array. - Useful to manipulate: - * data - * properties + Convert a list of IDs into indices, either as an array or a slice. - "prefer_slice" is an efficient option that tries to make a slice object - when indices are consecutive. + This function is designed to transform a list of IDs (such as channel or unit IDs) into an array of indices. + These indices are useful for data manipulation and accessing properties. When `prefer_slice` is set to `True`, + the function tries to return a slice object if the indices are consecutive, which can be more efficient + (e.g. with hdf5 files and to avoid copying data in numpy). + Parameters + ---------- + ids : list of str or int + The list of IDs to be converted into indices. If `None`, it generates indices based on the length of `_main_ids`. + prefer_slice : bool, optional + If `True`, the function will return a slice object when the indices are consecutive. Default is `False`. + + Returns + ------- + Union[np.ndarray, slice] + An array of indices corresponding to the input IDs. If `prefer_slice` is `True` and the indices are consecutive, + a slice object is returned instead. """ if ids is None: if prefer_slice: From 97e0ae222311bce6cc7e52f850ba6fecb3fd45a3 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Wed, 6 Dec 2023 15:23:23 +0100 Subject: [PATCH 2/4] Update src/spikeinterface/core/base.py --- src/spikeinterface/core/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spikeinterface/core/base.py b/src/spikeinterface/core/base.py index 3b7fa66471..cabf0c9d4a 100644 --- a/src/spikeinterface/core/base.py +++ b/src/spikeinterface/core/base.py @@ -103,7 +103,7 @@ def ids_to_indices(self, ids: list[str, int], prefer_slice: bool = False) -> Uni ---------- ids : list of str or int The list of IDs to be converted into indices. If `None`, it generates indices based on the length of `_main_ids`. - prefer_slice : bool, optional + prefer_slice : bool, default: False If `True`, the function will return a slice object when the indices are consecutive. Default is `False`. Returns From bc4333425c6a6cc9a346b4956ce7affc86bc523e Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Wed, 6 Dec 2023 16:51:53 +0100 Subject: [PATCH 3/4] Update src/spikeinterface/core/base.py --- src/spikeinterface/core/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spikeinterface/core/base.py b/src/spikeinterface/core/base.py index cabf0c9d4a..0158e3e615 100644 --- a/src/spikeinterface/core/base.py +++ b/src/spikeinterface/core/base.py @@ -95,7 +95,7 @@ def ids_to_indices(self, ids: list[str, int], prefer_slice: bool = False) -> Uni Convert a list of IDs into indices, either as an array or a slice. This function is designed to transform a list of IDs (such as channel or unit IDs) into an array of indices. - These indices are useful for data manipulation and accessing properties. When `prefer_slice` is set to `True`, + These indices are useful for interacting with data and accessing properties. When `prefer_slice` is set to `True`, the function tries to return a slice object if the indices are consecutive, which can be more efficient (e.g. with hdf5 files and to avoid copying data in numpy). From 889de76965ba9bcbe8bc314c068c0c9497ad5a24 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Thu, 7 Dec 2023 10:14:09 +0100 Subject: [PATCH 4/4] add check --- src/spikeinterface/core/base.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/spikeinterface/core/base.py b/src/spikeinterface/core/base.py index 0158e3e615..1373b25d5c 100644 --- a/src/spikeinterface/core/base.py +++ b/src/spikeinterface/core/base.py @@ -90,7 +90,7 @@ def _check_segment_index(self, segment_index: Optional[int] = None) -> int: else: return segment_index - def ids_to_indices(self, ids: list[str, int], prefer_slice: bool = False) -> Union[np.ndarray, slice]: + def ids_to_indices(self, ids: list | np.ndarray | None = None, prefer_slice: bool = False) -> np.ndarray | slice: """ Convert a list of IDs into indices, either as an array or a slice. @@ -101,23 +101,25 @@ def ids_to_indices(self, ids: list[str, int], prefer_slice: bool = False) -> Uni Parameters ---------- - ids : list of str or int - The list of IDs to be converted into indices. If `None`, it generates indices based on the length of `_main_ids`. + ids : list or np.ndarray + The array of IDs to be converted into indices. If `None`, it generates indices based on the length of `_main_ids`. prefer_slice : bool, default: False If `True`, the function will return a slice object when the indices are consecutive. Default is `False`. Returns ------- - Union[np.ndarray, slice] + np.ndarray or slice An array of indices corresponding to the input IDs. If `prefer_slice` is `True` and the indices are consecutive, a slice object is returned instead. """ + if ids is None: if prefer_slice: indices = slice(None) else: indices = np.arange(len(self._main_ids)) else: + assert isinstance(ids, (list, np.ndarray)), "'ids' must be a list, np.ndarray" _main_ids = self._main_ids.tolist() indices = np.array([_main_ids.index(id) for id in ids], dtype=int) if prefer_slice: