Repository navigation
Fix: compute extension children on-the-fly to catch optional dependencies #4913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alejoe91
wants to merge
10
commits into
SpikeInterface:main
Choose a base branch
from
alejoe91:get-extension-children-runtime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bbf8a28
fix: compute extension children on-the-fly to catch optional dependen…
alejoe91 e592522
Merge branch 'main' of github.com:SpikeInterface/spikeinterface into …
alejoe91 2667682
Merge branch 'main' of github.com:SpikeInterface/spikeinterface into …
alejoe91 0ded9cc
fix: tests and renaming (+ wrong snr param in curation)
alejoe91 1147003
feat: implement partial metric deletion based on optional dependencies
alejoe91 2187f44
Merge branch 'main' into get-extension-children-runtime
alejoe91 ed40335
Merge branch 'main' into get-extension-children-runtime
alejoe91 bfda106
refactor: separate _remove_dependent_data function and add warning on…
alejoe91 6d81f1e
fix: docstrings
alejoe91 3a5bae0
Merge branch 'main' into get-extension-children-runtime
alejoe91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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()) | ||
|
|
||
| params, job_kwargs = split_job_kwargs(kwargs) | ||
|
|
||
|
|
@@ -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} | ||
|
chrishalcrow marked this conversation as resolved.
Resolved
|
||
| 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) | ||
|
|
||
| # Group extensions by pipeline usage, to run them together | ||
| # Extensions whose dependencies (required or optional) include pipeline extensions are run after the pipeline | ||
|
|
@@ -2731,27 +2725,52 @@ def delete_extension(self, extension_name) -> 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) | ||
| extension = self.extensions.get(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). | ||
| extension.delete() | ||
| 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) -> 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} | ||
| deleted = [] | ||
| for child in children: | ||
| extension = self.get_extension(child) | ||
| if extension is None or extension._remove_dependent_data(deleted_parents): | ||
| continue | ||
| self.delete_extension(child) | ||
| deleted.append(child) | ||
| if len(deleted) > 0: | ||
| warnings.warn( | ||
| f"Deleted extensions {deleted} because they depend on '{extension_name}', which is being recomputed." | ||
| ) | ||
|
|
||
| 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. | ||
|
|
@@ -2850,16 +2869,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 | ||
|
|
@@ -2890,11 +2899,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". | ||
|
|
@@ -2908,15 +2914,30 @@ 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. | ||
|
|
||
| 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. | ||
|
Comment on lines
2905
to
+2921
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This docstring repeats itself
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done in 6d81f1e |
||
|
|
||
| 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): | ||
|
|
@@ -2943,15 +2964,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): | ||
| """ | ||
|
|
@@ -3639,25 +3651,34 @@ def _delete_extension_folder(self): | |
| """ | ||
| Delete the extension in a folder (binary or zarr). | ||
| """ | ||
| if self.format == "binary_folder": | ||
| extension_folder = self._get_binary_extension_folder() | ||
| if extension_folder.exists(): | ||
| shutil.rmtree(extension_folder) | ||
| self.sorting_analyzer._delete_extension_storage(self.extension_name) | ||
|
|
||
| elif self.format == "zarr": | ||
| zarr_root = self.sorting_analyzer._get_zarr_root(mode="r+") | ||
| if self.extension_name in zarr_root["extensions"]: | ||
| del zarr_root["extensions"][self.extension_name] | ||
| zarr.consolidate_metadata(zarr_root.store) | ||
| def _remove_dependent_data(self, deleted_parents) -> bool: | ||
| """ | ||
| Called when parent extensions are recomputed. Subclasses can override this to drop only the | ||
| data depending on ``deleted_parents`` and keep the rest. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| deleted_parents : set[str] | ||
| Names of the recomputed/deleted extensions. | ||
|
|
||
| Returns | ||
| ------- | ||
| kept : bool | ||
| True if the extension was kept (possibly pruned), False if it must be deleted. | ||
| """ | ||
| return False | ||
|
|
||
| def delete(self): | ||
| """ | ||
| Delete the extension from the folder or zarr and from the dict. | ||
| Delete the extension from the folder or zarr and reset it. | ||
| """ | ||
| # 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() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This warning is great! We should maybe add a warning when we delete any parent extension (e.g. if you do a
.compute('noise_levels')we should report which extensions we're deleting.