Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/spikeinterface/core/analyzer_extension_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,52 @@ def get_computed_metric_names(self):
computed_metric_names.append(m.metric_name)
return computed_metric_names

def delete(self, deleted_parents=None) -> bool:
"""
Delete the extension, or only the metrics depending on ``deleted_parents`` if other metrics remain.

Parameters
----------
deleted_parents : set[str] | None, default: None
Names of the recomputed/deleted extensions that trigger this deletion.

Returns
-------
deleted : bool
True if the extension was deleted, False if some metrics were kept.
"""
if not deleted_parents or deleted_parents.intersection(self.get_required_dependencies(**self.params)):
return super().delete()

dropped = [
metric_name
for metric_name in self.params["metric_names"]
if any(
name in deleted_parents
for dep in self.get_metric_by_name(metric_name).depend_on
for name in dep.split("|")
)
]
kept = [m for m in self.params["metric_names"] if m not in dropped]
if len(kept) == 0:
return super().delete()
if len(dropped) == 0:
return False

self.params["metric_names"] = kept
self.params["metrics_to_compute"] = [m for m in self.params["metrics_to_compute"] if m not in dropped]
columns = [col for m in dropped for col in self.get_metric_by_name(m).metric_columns]
self.data["metrics"] = self.data["metrics"].drop(columns=columns, errors="ignore")
for m in dropped:
self.data.get("runtime_s", {}).pop(m, None)
if self.format != "memory" and not self.sorting_analyzer.is_read_only():
self.save()
warnings.warn(
f"Removed metrics {dropped} from {self.extension_name} because they depend on recomputed "
f"extensions. Recompute them with `compute('{self.extension_name}', metric_names={dropped})`."
)
return False

@classmethod
def get_metric_by_name(cls, metric_name):
"""
Expand Down
143 changes: 89 additions & 54 deletions src/spikeinterface/core/sortinganalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2389,11 +2389,7 @@ def compute_one_extension(self, extension_name, save=True, verbose=False, **kwar
save = False
extension_class = get_extension_class(extension_name)

for child in _get_children_dependencies(extension_name):
if self.has_extension(child):
if verbose:
print(f"Deleting extension: {child}")
self.delete_extension(child)
self._delete_children(extension_name, self._get_extensions_params(), verbose=verbose)

params, job_kwargs = split_job_kwargs(kwargs)

Expand Down Expand Up @@ -2487,11 +2483,9 @@ def compute_several_extensions(
# Sort extensions by dependency order
sorted_extensions = _sort_extensions_by_dependency(extensions)

all_extensions_params = {**self._get_extensions_params(), **extensions}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think there might be a problem here for re-computes.
E.g. what happens if quality_metrics is in self._get_extensions_params() and extensions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the new extensions will update the existing ones, which I think is the correct behavior?

for extension_name in sorted_extensions.keys():
for child in _get_children_dependencies(extension_name):
if verbose:
print(f"Deleting extension: {child}")
self.delete_extension(child)
self._delete_children(extension_name, all_extensions_params, verbose=verbose)

# Group extensions by pipeline usage, to run them together
# Extensions whose dependencies (required or optional) include pipeline extensions are run after the pipeline
Expand Down Expand Up @@ -2727,31 +2721,62 @@ def _delete_extension_storage(self, extension_name) -> None:
del root[extension_name]
zarr.consolidate_metadata(zarr_root.store)

def delete_extension(self, extension_name) -> None:
def delete_extension(self, extension_name, deleted_parents=None) -> None:
"""
Delete the extension from the dict and also in the persistent zarr or folder.
"""
# Delete from folder or zarr
if self.format != "memory" and not self._read_only:
in_mem_extension = self.extensions.get(extension_name)
if in_mem_extension is not None:
# Call delete() on the already-loaded instance so it closes any open memmap handles
# before removing the on-disk files. The original code reloaded the extension first
# (opening new memmaps) and then called delete() on the fresh copy — on Windows that
# left the new memmaps open long enough to cause a PermissionError during rmtree.
in_mem_extension.delete()
else:
self._delete_extension_storage(extension_name)

Parameters
----------
extension_name : str
The extension to delete.
deleted_parents : set[str] | None, default: None
Names of the recomputed/deleted extensions that trigger this deletion. The extension can use them
to keep the part that does not depend on them (see ``AnalyzerExtension.delete()``).
"""
extension = self.extensions.get(extension_name)
if extension is None and deleted_parents is not None:
# load it, so that it can check what depends on deleted_parents
extension = self.get_extension(extension_name)
if extension is not None:
# Call delete() on the loaded instance so it closes any open memmap handles before
# removing the on-disk files (otherwise rmtree can raise a PermissionError on Windows).
if not extension.delete(deleted_parents=deleted_parents):
return
elif self.format != "memory" and not self._read_only:
self._delete_extension_storage(extension_name)

# remove from dict
self.extensions.pop(extension_name, None)

def _delete_children(self, extension_name, all_extension_params, verbose=False) -> None:
"""
Delete the children of ``extension_name`` before it is recomputed.
"""
children = _get_children_dependencies(extension_name, all_extension_params)
deleted_parents = {extension_name, *children}
for child in children:
if self.has_extension(child):
if verbose:
print(f"Deleting extension: {child}")
self.delete_extension(child, deleted_parents=deleted_parents)

def get_loaded_extension_names(self):
"""
Return the loaded or already computed extensions names.
"""
return list(self.extensions.keys())

def _get_extensions_params(self) -> dict[str, dict]:
"""
Return the params of all computed extensions (loaded or saved), keyed by extension name.
"""
# saved-but-unloaded extensions fall back to default params rather than being read from disk
params: dict[str, dict] = {}
if self.format != "memory":
params = {name: {} for name in self.get_saved_extension_names()}
params.update({name: ext.params for name, ext in self.extensions.items()})
return params

def has_extension(self, extension_name: str) -> bool:
"""
Check if the extension exists in memory (dict) or in the folder or in zarr.
Expand Down Expand Up @@ -2850,16 +2875,6 @@ def _sort_extensions_by_dependency(extensions):

required_dependencies = get_extension_class(extension).get_required_dependencies(**extension_params)
optional_dependencies = get_extension_class(extension).get_optional_dependencies(**extension_params)
for dependency in required_dependencies:
if "|" in dependency:
for dep in dependency.split("|"):
if extension not in _extension_children[dep]:
print(f"Adding missing dependency child link: {dep} -> {extension}")
_extension_children[dep].append(extension)
else:
if extension not in _extension_children[dependency]:
print(f"Adding missing dependency child link: {dependency} -> {extension}")
_extension_children[dependency].append(extension)

# Split cases with an "or" in them, and flatten into a list
all_dependencies = required_dependencies + optional_dependencies
Expand Down Expand Up @@ -2890,11 +2905,8 @@ def _sort_extensions_by_dependency(extensions):
global _possible_extensions
_possible_extensions = []

global _extension_children
_extension_children = {}


def _get_children_dependencies(extension_name):
def _get_children_dependencies(extension_name, all_extension_params):
"""
Extension classes have a `depend_on` attribute to declare on which class they
depend on. For instance "templates" depends on "waveforms". "waveforms" depends on "random_spikes".
Expand All @@ -2908,15 +2920,34 @@ def _get_children_dependencies(extension_name):
For instance, recomputing the "waveforms" needs to delete the "templates", since the latter depends on the former.
For this particular example, if we change the "ms_before" parameter of the "waveforms", also the "templates" will
require recomputation as this parameter is inherited.

Extension classes have a `depend_on` attribute to declare which class they depend on. For instance "templates"
depends on "waveforms"; "waveforms" depends on "random_spikes". This function is going the opposite way: it finds
all children, grandchildren etc that depend on a particular extension.

Depending on their parameters, certain extensions depend on different combinations of extensions. E.g. if you're
computing snrs, then noise_levels has quality_metrics as a child. But if you're just computing firing_rate,
then it does not. Hence the dependency graph depends on what other extensions are being computed, or are saved
in the analyzer. We account for these through the `all_extension_params` argument.

Parameters
----------
extension_name : str
The parent extension name.
all_extension_params : dict
Mapping of computed + new extension name as keys and their params as values.

Returns
-------
names : list
The children, grand children, etc. of ``extension_name``.
"""
names = []
children = _extension_children[extension_name]
for child in children:
if child not in names:
for child, params in all_extension_params.items():
if child not in names and extension_name in get_extension_class(child).get_any_dependencies(**params):
names.append(child)
grand_children = _get_children_dependencies(child)
names.extend(grand_children)
return list(names)
names.extend(n for n in _get_children_dependencies(child, all_extension_params) if n not in names)
return names


def register_result_extension(extension_class):
Expand All @@ -2943,15 +2974,6 @@ def register_result_extension(extension_class):

_possible_extensions.append(extension_class)

# create the children dpendencies to be able to delete on re-compute
_extension_children[extension_class.extension_name] = []
for parent_name in extension_class.get_required_dependencies():
if "|" in parent_name:
for name in parent_name.split("|"):
_extension_children[name].append(extension_class.extension_name)
else:
_extension_children[parent_name].append(extension_class.extension_name)


def get_extension_class(extension_name: str, auto_import=True):
"""
Expand Down Expand Up @@ -3650,17 +3672,30 @@ def _delete_extension_folder(self):
del zarr_root["extensions"][self.extension_name]
zarr.consolidate_metadata(zarr_root.store)

def delete(self):
def delete(self, deleted_parents=None) -> bool:
"""
Delete the extension from the folder or zarr and from the dict.
Delete the extension from the folder or zarr and reset it.

Parameters
----------
deleted_parents : set[str] | None, default: None
Names of the recomputed/deleted extensions that trigger this deletion. Subclasses can override
this method to keep the part of the extension that does not depend on them.

Returns
-------
deleted : bool
True if the extension was deleted, False if (part of) it was kept.
"""
# close any memmap handles onto these files first (e.g. from a lazy load) so the
# folder/files can actually be removed
self._close_memmaps()
self._delete_extension_folder()
if not self.sorting_analyzer.is_read_only():
self._delete_extension_folder()
self.params = None
self.run_info = self._default_run_info_dict()
self.data = dict()
return True

def reset(self):
"""
Expand Down
48 changes: 43 additions & 5 deletions src/spikeinterface/core/tests/test_analyzer_extension_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from spikeinterface.core import create_sorting_analyzer
from spikeinterface.core import Templates

from spikeinterface.core.sortinganalyzer import _extension_children, _get_children_dependencies
from spikeinterface.core.sortinganalyzer import _get_children_dependencies

import numpy as np

Expand Down Expand Up @@ -277,14 +277,27 @@ def test_ComputeNoiseLevels(format, sparse, create_cache_folder):


def test_get_children_dependencies():
assert "waveforms" in _extension_children["random_spikes"]

rs_children = _get_children_dependencies("random_spikes")
extensions = {"random_spikes": {}, "waveforms": {}, "templates": {}}
rs_children = _get_children_dependencies("random_spikes", extensions)
assert "waveforms" in rs_children
assert "templates" in rs_children

assert rs_children.index("waveforms") < rs_children.index("templates")

# "templates" depends on "random_spikes|waveforms": both alternatives are parents
assert _get_children_dependencies("waveforms", extensions) == ["templates"]
# children not computed are not returned
assert _get_children_dependencies("random_spikes", {"random_spikes": {}}) == []


def test_get_children_dependencies_optional():
import spikeinterface.metrics # noqa: F401 registers quality_metrics

# quality_metrics only optionally depends on spike_amplitudes, through its metrics
extensions = {"spike_amplitudes": {}, "quality_metrics": {"metric_names": ["amplitude_cutoff"]}}
assert _get_children_dependencies("spike_amplitudes", extensions) == ["quality_metrics"]
extensions["quality_metrics"] = {"metric_names": ["firing_rate"]}
assert _get_children_dependencies("spike_amplitudes", extensions) == []


def test_delete_on_recompute(create_cache_folder):
cache_folder = create_cache_folder
Expand All @@ -298,6 +311,31 @@ def test_delete_on_recompute(create_cache_folder):
assert sorting_analyzer.get_extension("templates") is None
assert sorting_analyzer.get_extension("waveforms") is None

# metric extensions only lose the metrics depending on the recomputed extension
import spikeinterface.metrics # noqa: F401 registers quality_metrics
from spikeinterface.core import load_sorting_analyzer

sorting_analyzer = get_sorting_analyzer(cache_folder, format="binary_folder", sparse=False)
sorting_analyzer.compute(
{
"random_spikes": {},
"templates": {},
"noise_levels": {},
"quality_metrics": {"metric_names": ["snr", "firing_rate"]},
}
)
with pytest.warns(UserWarning, match="Removed metrics"):
sorting_analyzer.compute("noise_levels")
for analyzer in (sorting_analyzer, load_sorting_analyzer(sorting_analyzer.folder)):
qm = analyzer.get_extension("quality_metrics")
assert qm.params["metric_names"] == ["firing_rate"]
assert list(qm.get_data().columns) == ["firing_rate"]

# if all metrics depend on it, the extension is deleted
sorting_analyzer.compute("quality_metrics", metric_names=["snr"], delete_existing_metrics=True)
sorting_analyzer.compute("noise_levels")
assert sorting_analyzer.get_extension("quality_metrics") is None


def test_compute_several(create_cache_folder):
cache_folder = create_cache_folder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_exception_raised_when_metric_params_not_equal(sorting_analyzer_for_unit
be raised depending on the `enforce_metric_params` kwarg. This behaviour is tested here."""

sorting_analyzer_for_unitrefine_curation.compute(
"quality_metrics", metric_names=["snr"], metric_params={"snr": {"peak_mode": "peak_to_peak"}}
"quality_metrics", metric_names=["snr"], metric_params={"snr": {"method": "peak_to_peak"}}
)
sorting_analyzer_for_unitrefine_curation.compute(
"template_metrics", metric_names=["half_width", "peak_to_trough_duration", "number_of_peaks"]
Expand All @@ -209,7 +209,7 @@ def test_exception_raised_when_metric_params_not_equal(sorting_analyzer_for_unit
sorting_analyzer_for_unitrefine_curation.compute(
"quality_metrics",
metric_names=["snr"],
metric_params={"snr": {"peak_sign": "neg", "peak_mode": "extremum"}},
metric_params={"snr": {"method": "extremum"}},
)
sorting_analyzer_for_unitrefine_curation.compute(
"template_metrics", metric_names=["half_width", "peak_to_trough_duration"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def test_train_using_two_sorting_analyzers():
{
"quality_metrics": {
"metric_names": ["num_spikes", "snr"],
"metric_params": {"snr": {"peak_mode": "at_index"}},
"metric_params": {"snr": {"method": "at_index"}},
}
}
)
Expand Down
3 changes: 1 addition & 2 deletions src/spikeinterface/exporters/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ def make_sorting_analyzer(sparse=True, with_group=False):
sorting_analyzer.compute("noise_levels")
sorting_analyzer.compute("principal_components")
sorting_analyzer.compute("template_similarity")
sorting_analyzer.compute("spike_amplitudes")
sorting_analyzer.compute(["spike_amplitudes", "spike_locations"])
sorting_analyzer.compute(
"quality_metrics", metric_names=["snr", "amplitude_median", "isi_violation", "amplitude_cutoff"]
)
sorting_analyzer.compute(["spike_amplitudes", "spike_locations"])

return sorting_analyzer

Expand Down
Loading
Loading