Skip to content

Commit

Permalink
Merge branch 'main' into remove-redundant
Browse files Browse the repository at this point in the history
  • Loading branch information
zm711 authored May 3, 2024
2 parents 84995a4 + 42e1f02 commit fa0177b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/spikeinterface/core/numpyextractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ def from_sorting(source_sorting: BaseSorting, with_metadata=False, copy_spike_ve
spike_vector = source_sorting.to_spike_vector()
if copy_spike_vector:
spike_vector = spike_vector.copy()
sorting = NumpySorting(spike_vector, source_sorting.get_sampling_frequency(), source_sorting.unit_ids)
sorting = NumpySorting(spike_vector, source_sorting.get_sampling_frequency(), source_sorting.unit_ids.copy())
if with_metadata:
sorting.copy_metadata(source_sorting)
source_sorting.copy_metadata(sorting)
return sorting

@staticmethod
Expand Down
35 changes: 25 additions & 10 deletions src/spikeinterface/core/sortinganalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from .recording_tools import check_probe_do_not_overlap, get_rec_attributes
from .core_tools import check_json, retrieve_importing_provenance
from .job_tools import split_job_kwargs
from .numpyextractors import SharedMemorySorting
from .numpyextractors import NumpySorting
from .sparsity import ChannelSparsity, estimate_sparsity
from .sortingfolder import NumpyFolderSorting
from .zarrextractors import get_default_zarr_compressor, ZarrSortingExtractor
Expand Down Expand Up @@ -296,8 +296,9 @@ def create_memory(cls, sorting, recording, sparsity, return_scaled, rec_attribut
# a copy is done to avoid shared dict between instances (which can block garbage collector)
rec_attributes = rec_attributes.copy()

# a copy of sorting is created directly in shared memory format to avoid further duplication of spikes.
sorting_copy = SharedMemorySorting.from_sorting(sorting, with_metadata=True)
# a copy of sorting is copied in memory for fast access
sorting_copy = NumpySorting.from_sorting(sorting, with_metadata=True, copy_spike_vector=True)

sorting_analyzer = SortingAnalyzer(
sorting=sorting_copy,
recording=recording,
Expand Down Expand Up @@ -375,8 +376,10 @@ def load_from_binary_folder(cls, folder, recording=None):
folder = Path(folder)
assert folder.is_dir(), f"This folder does not exists {folder}"

# load internal sorting copy and make it sharedmem
sorting = SharedMemorySorting.from_sorting(NumpyFolderSorting(folder / "sorting"), with_metadata=True)
# load internal sorting copy in memory
sorting = NumpySorting.from_sorting(
NumpyFolderSorting(folder / "sorting"), with_metadata=True, copy_spike_vector=True
)

# load recording if possible
if recording is None:
Expand Down Expand Up @@ -416,9 +419,21 @@ def load_from_binary_folder(cls, folder, recording=None):
else:
sparsity = None

# PATCH: Because SortingAnalyzer added this json during the development of 0.101.0 we need to save
# this as a bridge for early adopters. The else branch can be removed in version 0.102.0/0.103.0
# so that this can be simplified in the future
# See https://github.com/SpikeInterface/spikeinterface/issues/2788

settings_file = folder / f"settings.json"
with open(settings_file, "r") as f:
settings = json.load(f)
if settings_file.exists():
with open(settings_file, "r") as f:
settings = json.load(f)
else:
warnings.warn("settings.json not found for this folder writing one with return_scaled=True")
settings = dict(return_scaled=True)
with open(settings_file, "w") as f:
json.dump(check_json(settings), f, indent=4)

return_scaled = settings["return_scaled"]

sorting_analyzer = SortingAnalyzer(
Expand Down Expand Up @@ -525,10 +540,10 @@ def load_from_zarr(cls, folder, recording=None):

zarr_root = zarr.open(folder, mode="r")

# load internal sorting and make it sharedmem
# load internal sorting in memory
# TODO propagate storage_options
sorting = SharedMemorySorting.from_sorting(
ZarrSortingExtractor(folder, zarr_group="sorting"), with_metadata=True
sorting = NumpySorting.from_sorting(
ZarrSortingExtractor(folder, zarr_group="sorting"), with_metadata=True, copy_spike_vector=True
)

# load recording if possible
Expand Down
8 changes: 4 additions & 4 deletions src/spikeinterface/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,25 @@ def do_plot(self):

@classmethod
def ensure_sorting_analyzer(cls, input):
# internal help to accept both SortingAnalyzer or MockWaveformExtractor for a ploter
# internal help to accept both SortingAnalyzer or MockWaveformExtractor for a plotter
if isinstance(input, SortingAnalyzer):
return input
elif isinstance(input, MockWaveformExtractor):
return input.sorting_analyzer
else:
return input
raise TypeError("input must be a SortingAnalyzer or MockWaveformExtractor")

@classmethod
def ensure_sorting(cls, input):
# internal help to accept both Sorting or SortingAnalyzer or MockWaveformExtractor for a ploter
# internal help to accept both Sorting or SortingAnalyzer or MockWaveformExtractor for a plotter
if isinstance(input, BaseSorting):
return input
elif isinstance(input, SortingAnalyzer):
return input.sorting
elif isinstance(input, MockWaveformExtractor):
return input.sorting_analyzer.sorting
else:
return input
raise TypeError("input must be a SortingAnalyzer, MockWaveformExtractor, or of type BaseSorting")

@staticmethod
def check_extensions(sorting_analyzer, extensions):
Expand Down
4 changes: 3 additions & 1 deletion src/spikeinterface/widgets/crosscorrelograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def __init__(
backend=None,
**backend_kwargs,
):
sorting_analyzer_or_sorting = self.ensure_sorting_analyzer(sorting_analyzer_or_sorting)

if not isinstance(sorting_analyzer_or_sorting, BaseSorting):
sorting_analyzer_or_sorting = self.ensure_sorting_analyzer(sorting_analyzer_or_sorting)

if min_similarity_for_correlograms is None:
min_similarity_for_correlograms = 0
Expand Down

0 comments on commit fa0177b

Please sign in to comment.