From 375620fa1589b8fdb46e7e9289909992ac5b0398 Mon Sep 17 00:00:00 2001 From: Samuel Garcia Date: Wed, 19 Jun 2024 19:01:08 +0200 Subject: [PATCH 1/6] fix spike_vector_to_indices --- src/spikeinterface/core/sorting_tools.py | 15 ++++++++++++++- .../postprocessing/spike_amplitudes.py | 2 +- .../postprocessing/spike_locations.py | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/spikeinterface/core/sorting_tools.py b/src/spikeinterface/core/sorting_tools.py index 2313e7d253..5e3af58198 100644 --- a/src/spikeinterface/core/sorting_tools.py +++ b/src/spikeinterface/core/sorting_tools.py @@ -47,7 +47,7 @@ def spike_vector_to_spike_trains(spike_vector: list[np.array], unit_ids: np.arra return spike_trains -def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array): +def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array, absolut_index=False): """ Similar to spike_vector_to_spike_trains but instead having the spike_trains (aka spike times) return spike indices by segment and units. @@ -61,6 +61,12 @@ def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array): List of spike vectors optained with sorting.to_spike_vector(concatenated=False) unit_ids: np.array Unit ids + absolut_index: bool, default False + Give spike indices absolut usefull when having a unique spike vector + or relative to segment usefull with a list of spike vectors + When a unique spike vectors (or amplitudes) is used then absolut_index should be True. + When a list of spikes (or amplitudes) is used then absolut_index should be False. + Returns ------- spike_indices: dict[dict]: @@ -82,12 +88,19 @@ def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array): num_units = unit_ids.size spike_indices = {} + + total_spikes = 0 for segment_index, spikes in enumerate(spike_vector): indices = np.arange(spikes.size, dtype=np.int64) + if absolut_index: + indices += total_spikes + total_spikes += spikes.size unit_indices = np.array(spikes["unit_index"]).astype(np.int64, copy=False) list_of_spike_indices = vector_to_list_of_spiketrain(indices, unit_indices, num_units) + spike_indices[segment_index] = dict(zip(unit_ids, list_of_spike_indices)) + return spike_indices diff --git a/src/spikeinterface/postprocessing/spike_amplitudes.py b/src/spikeinterface/postprocessing/spike_amplitudes.py index 09b46362e5..2a9edf7e73 100644 --- a/src/spikeinterface/postprocessing/spike_amplitudes.py +++ b/src/spikeinterface/postprocessing/spike_amplitudes.py @@ -127,7 +127,7 @@ def _get_data(self, outputs="numpy"): elif outputs == "by_unit": unit_ids = self.sorting_analyzer.unit_ids spike_vector = self.sorting_analyzer.sorting.to_spike_vector(concatenated=False) - spike_indices = spike_vector_to_indices(spike_vector, unit_ids) + spike_indices = spike_vector_to_indices(spike_vector, unit_ids, absolut_index=True) amplitudes_by_units = {} for segment_index in range(self.sorting_analyzer.sorting.get_num_segments()): amplitudes_by_units[segment_index] = {} diff --git a/src/spikeinterface/postprocessing/spike_locations.py b/src/spikeinterface/postprocessing/spike_locations.py index d468bd90ab..e7a9d7a992 100644 --- a/src/spikeinterface/postprocessing/spike_locations.py +++ b/src/spikeinterface/postprocessing/spike_locations.py @@ -140,7 +140,7 @@ def _get_data(self, outputs="numpy"): elif outputs == "by_unit": unit_ids = self.sorting_analyzer.unit_ids spike_vector = self.sorting_analyzer.sorting.to_spike_vector(concatenated=False) - spike_indices = spike_vector_to_indices(spike_vector, unit_ids) + spike_indices = spike_vector_to_indices(spike_vector, unit_ids, absolut_index=True) spike_locations_by_units = {} for segment_index in range(self.sorting_analyzer.sorting.get_num_segments()): spike_locations_by_units[segment_index] = {} From 0507d59b811682f6e2fb1132099dd6575b43362b Mon Sep 17 00:00:00 2001 From: Garcia Samuel Date: Thu, 20 Jun 2024 12:59:08 +0200 Subject: [PATCH 2/6] Update src/spikeinterface/core/sorting_tools.py Co-authored-by: Alessio Buccino --- src/spikeinterface/core/sorting_tools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spikeinterface/core/sorting_tools.py b/src/spikeinterface/core/sorting_tools.py index 5e3af58198..13f6b28f3c 100644 --- a/src/spikeinterface/core/sorting_tools.py +++ b/src/spikeinterface/core/sorting_tools.py @@ -47,7 +47,7 @@ def spike_vector_to_spike_trains(spike_vector: list[np.array], unit_ids: np.arra return spike_trains -def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array, absolut_index=False): +def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array, absolute_index : bool = False): """ Similar to spike_vector_to_spike_trains but instead having the spike_trains (aka spike times) return spike indices by segment and units. From 8a2c56fa6ca401fca56aa8f41d432c06eb0c62b2 Mon Sep 17 00:00:00 2001 From: Garcia Samuel Date: Thu, 20 Jun 2024 15:12:14 +0200 Subject: [PATCH 3/6] Merci Zach Co-authored-by: Zach McKenzie <92116279+zm711@users.noreply.github.com> --- src/spikeinterface/core/sorting_tools.py | 8 ++++---- src/spikeinterface/postprocessing/spike_amplitudes.py | 2 +- src/spikeinterface/postprocessing/spike_locations.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/spikeinterface/core/sorting_tools.py b/src/spikeinterface/core/sorting_tools.py index 13f6b28f3c..5ac3fcc822 100644 --- a/src/spikeinterface/core/sorting_tools.py +++ b/src/spikeinterface/core/sorting_tools.py @@ -61,11 +61,11 @@ def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array, ab List of spike vectors optained with sorting.to_spike_vector(concatenated=False) unit_ids: np.array Unit ids - absolut_index: bool, default False + absolute_index: bool, default False Give spike indices absolut usefull when having a unique spike vector or relative to segment usefull with a list of spike vectors - When a unique spike vectors (or amplitudes) is used then absolut_index should be True. - When a list of spikes (or amplitudes) is used then absolut_index should be False. + When a unique spike vectors (or amplitudes) is used then absolute_index should be True. + When a list of spikes (or amplitudes) is used then absolute_index should be False. Returns ------- @@ -92,7 +92,7 @@ def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array, ab total_spikes = 0 for segment_index, spikes in enumerate(spike_vector): indices = np.arange(spikes.size, dtype=np.int64) - if absolut_index: + if absolute_index: indices += total_spikes total_spikes += spikes.size unit_indices = np.array(spikes["unit_index"]).astype(np.int64, copy=False) diff --git a/src/spikeinterface/postprocessing/spike_amplitudes.py b/src/spikeinterface/postprocessing/spike_amplitudes.py index 2a9edf7e73..aebfd1fd78 100644 --- a/src/spikeinterface/postprocessing/spike_amplitudes.py +++ b/src/spikeinterface/postprocessing/spike_amplitudes.py @@ -127,7 +127,7 @@ def _get_data(self, outputs="numpy"): elif outputs == "by_unit": unit_ids = self.sorting_analyzer.unit_ids spike_vector = self.sorting_analyzer.sorting.to_spike_vector(concatenated=False) - spike_indices = spike_vector_to_indices(spike_vector, unit_ids, absolut_index=True) + spike_indices = spike_vector_to_indices(spike_vector, unit_ids, absolute_index=True) amplitudes_by_units = {} for segment_index in range(self.sorting_analyzer.sorting.get_num_segments()): amplitudes_by_units[segment_index] = {} diff --git a/src/spikeinterface/postprocessing/spike_locations.py b/src/spikeinterface/postprocessing/spike_locations.py index e7a9d7a992..a2dcd4a68a 100644 --- a/src/spikeinterface/postprocessing/spike_locations.py +++ b/src/spikeinterface/postprocessing/spike_locations.py @@ -140,7 +140,7 @@ def _get_data(self, outputs="numpy"): elif outputs == "by_unit": unit_ids = self.sorting_analyzer.unit_ids spike_vector = self.sorting_analyzer.sorting.to_spike_vector(concatenated=False) - spike_indices = spike_vector_to_indices(spike_vector, unit_ids, absolut_index=True) + spike_indices = spike_vector_to_indices(spike_vector, unit_ids, absolute_index=True) spike_locations_by_units = {} for segment_index in range(self.sorting_analyzer.sorting.get_num_segments()): spike_locations_by_units[segment_index] = {} From 6abb74b84bc766c801ac366a8678f6cdafb2a06c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 21 Jun 2024 09:37:35 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/spikeinterface/core/sorting_tools.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/spikeinterface/core/sorting_tools.py b/src/spikeinterface/core/sorting_tools.py index 5ac3fcc822..65a65875e1 100644 --- a/src/spikeinterface/core/sorting_tools.py +++ b/src/spikeinterface/core/sorting_tools.py @@ -47,7 +47,7 @@ def spike_vector_to_spike_trains(spike_vector: list[np.array], unit_ids: np.arra return spike_trains -def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array, absolute_index : bool = False): +def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array, absolute_index: bool = False): """ Similar to spike_vector_to_spike_trains but instead having the spike_trains (aka spike times) return spike indices by segment and units. @@ -66,7 +66,7 @@ def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array, ab or relative to segment usefull with a list of spike vectors When a unique spike vectors (or amplitudes) is used then absolute_index should be True. When a list of spikes (or amplitudes) is used then absolute_index should be False. - + Returns ------- spike_indices: dict[dict]: @@ -88,7 +88,7 @@ def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array, ab num_units = unit_ids.size spike_indices = {} - + total_spikes = 0 for segment_index, spikes in enumerate(spike_vector): indices = np.arange(spikes.size, dtype=np.int64) @@ -100,7 +100,6 @@ def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array, ab spike_indices[segment_index] = dict(zip(unit_ids, list_of_spike_indices)) - return spike_indices From c1c9f1f1d0d9967e3c08cf3e92aa520d9e9d289b Mon Sep 17 00:00:00 2001 From: Alessio Buccino Date: Fri, 21 Jun 2024 12:03:42 +0200 Subject: [PATCH 5/6] Update src/spikeinterface/core/sorting_tools.py --- src/spikeinterface/core/sorting_tools.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/spikeinterface/core/sorting_tools.py b/src/spikeinterface/core/sorting_tools.py index 65a65875e1..6045442466 100644 --- a/src/spikeinterface/core/sorting_tools.py +++ b/src/spikeinterface/core/sorting_tools.py @@ -62,10 +62,9 @@ def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array, ab unit_ids: np.array Unit ids absolute_index: bool, default False - Give spike indices absolut usefull when having a unique spike vector - or relative to segment usefull with a list of spike vectors - When a unique spike vectors (or amplitudes) is used then absolute_index should be True. - When a list of spikes (or amplitudes) is used then absolute_index should be False. + It True, return absolute spike indices, else spike indices are relative to the segment. + When a unique spike vector is used, then absolute_index should be True. + When a list of spikes per segment is used, then absolute_index should be False. Returns ------- From 391db33a9aad3c49718a415da6208c6d92add7d4 Mon Sep 17 00:00:00 2001 From: Alessio Buccino Date: Fri, 21 Jun 2024 12:04:13 +0200 Subject: [PATCH 6/6] Update src/spikeinterface/core/sorting_tools.py --- src/spikeinterface/core/sorting_tools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spikeinterface/core/sorting_tools.py b/src/spikeinterface/core/sorting_tools.py index 6045442466..02f4529a98 100644 --- a/src/spikeinterface/core/sorting_tools.py +++ b/src/spikeinterface/core/sorting_tools.py @@ -62,7 +62,7 @@ def spike_vector_to_indices(spike_vector: list[np.array], unit_ids: np.array, ab unit_ids: np.array Unit ids absolute_index: bool, default False - It True, return absolute spike indices, else spike indices are relative to the segment. + It True, return absolute spike indices. If False, spike indices are relative to the segment. When a unique spike vector is used, then absolute_index should be True. When a list of spikes per segment is used, then absolute_index should be False.