Skip to content

Commit 38dff14

Browse files
authored
Propagate lazy arg to from si.load() (#4709)
1 parent 9ca603e commit 38dff14

5 files changed

Lines changed: 39 additions & 12 deletions

File tree

src/spikeinterface/core/core_tools.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from probeinterface import ProbeGroup
1515
import numpy as np
16+
import zarr
1617

1718

1819
def define_function_handling_dict_from_class(source_class, name):
@@ -779,3 +780,26 @@ def is_path_remote(path: str | Path) -> bool:
779780
def ms_to_samples(ms: float, sampling_frequency: float) -> int:
780781
"""Convert a duration in milliseconds to the nearest number of samples."""
781782
return round(ms * sampling_frequency / 1000.0)
783+
784+
785+
def slice_rows(array: np.ndarray | zarr.Array, row_indices: np.ndarray | list) -> np.ndarray:
786+
"""
787+
Slice a 2D array to select specific rows based on provided indices.
788+
789+
Parameters
790+
----------
791+
array : np.ndarray | zarr.Array
792+
A numpy or zarr array or boolean mask from which rows will be selected.
793+
row_indices : np.ndarray | list
794+
A list or array of row indices to select from the array.
795+
796+
Returns
797+
-------
798+
np.ndarray
799+
A new 2D numpy array containing only the selected rows.
800+
"""
801+
if isinstance(array, zarr.Array):
802+
# For zarr arrays, we need to convert the list of indices to a numpy array for advanced indexing
803+
return array.oindex[row_indices]
804+
else:
805+
return array[row_indices, ...]

src/spikeinterface/core/loading.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,7 @@ def _load_object_from_zarr(folder_or_url, object_type, **kwargs):
282282
if object_type == "SortingAnalyzer":
283283
from .sortinganalyzer import load_sorting_analyzer
284284

285-
backend_options = kwargs.get("backend_options", None)
286-
load_extensions = kwargs.get("load_extensions", True)
287-
analyzer = load_sorting_analyzer(
288-
folder_or_url, backend_options=backend_options, load_extensions=load_extensions
289-
)
285+
analyzer = load_sorting_analyzer(folder_or_url, **kwargs)
290286
return analyzer
291287
elif object_type == "Templates":
292288
from .template import Templates

src/spikeinterface/core/sortinganalyzer.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,21 @@ def __repr__(self) -> str:
466466
nchan = self.get_num_channels()
467467
nunits = self.get_num_units()
468468
txt = f"{clsname}: {nchan} channels - {nunits} units - {nseg} segments - {self.format}"
469-
if self.format != "memory" and is_path_remote(self.folder):
470-
txt += " (remote)"
469+
if self.format != "memory":
470+
if is_path_remote(self.folder):
471+
if self._lazy:
472+
txt += " (remote + lazy)"
473+
else:
474+
txt += " (remote)"
475+
elif self._lazy:
476+
txt += " (lazy)"
471477
if self.is_sparse():
472478
txt += " - sparse"
473479
if self.has_recording():
474480
txt += " - has recording"
475481
if self.has_temporary_recording():
476482
txt += " - has temporary recording"
483+
477484
ext_txt = f"Loaded {len(self.extensions)} extensions"
478485
if len(self.extensions) > 0:
479486
ext_txt += f": {', '.join(self.extensions.keys())}"

src/spikeinterface/postprocessing/principal_component.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
import numpy as np
1111

1212
from spikeinterface.core.sortinganalyzer import register_result_extension, AnalyzerExtension
13-
13+
from spikeinterface.core.core_tools import slice_rows
1414
from spikeinterface.core.job_tools import TimeSeriesChunkExecutor, _shared_job_kwargs_doc, fix_job_kwargs
15-
1615
from spikeinterface.core.analyzer_extension_core import _inplace_sparse_realign_waveforms
1716

1817
_possible_modes = ["by_channel_local", "by_channel_global", "concatenated"]
@@ -197,7 +196,7 @@ def get_projections_one_unit(self, unit_id, sparse=False):
197196

198197
unit_index = sorting.id_to_index(unit_id)
199198
spike_mask = some_spikes["unit_index"] == unit_index
200-
projections = self.data["pca_projection"][spike_mask]
199+
projections = slice_rows(self.data["pca_projection"], spike_mask)
201200

202201
if sparsity is None:
203202
return projections

src/spikeinterface/postprocessing/valid_unit_periods.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from tqdm.auto import tqdm
1010

1111
from spikeinterface.core.base import unit_period_dtype
12+
from spikeinterface.core.core_tools import slice_rows
1213
from spikeinterface.core.job_tools import fix_job_kwargs
1314
from spikeinterface.core.sorting_tools import cast_periods_to_unit_period_dtype, remap_unit_indices_in_vector
1415
from spikeinterface.core.sortinganalyzer import register_result_extension, AnalyzerExtension
@@ -539,7 +540,7 @@ def _get_data(self, outputs: str = "by_unit"):
539540
(start_sample_index, end_sample_index) tuples.
540541
"""
541542
if outputs == "numpy":
542-
good_periods = self.data["valid_unit_periods"].copy()
543+
good_periods = np.asarray(self.data["valid_unit_periods"]).copy()
543544
else:
544545
# by_unit
545546
unit_ids = self.sorting_analyzer.unit_ids
@@ -551,7 +552,7 @@ def _get_data(self, outputs: str = "by_unit"):
551552
for unit_index, unit_id in enumerate(unit_ids):
552553
periods_dict[unit_id] = []
553554
unit_mask = good_periods_array["unit_index"] == unit_index
554-
good_periods_unit_segment = good_periods_array[segment_mask & unit_mask]
555+
good_periods_unit_segment = slice_rows(good_periods_array, segment_mask & unit_mask)
555556
for start, end in good_periods_unit_segment[["start_sample_index", "end_sample_index"]]:
556557
periods_dict[unit_id].append((start, end))
557558
good_periods.append(periods_dict)

0 commit comments

Comments
 (0)