-
Notifications
You must be signed in to change notification settings - Fork 241
Refactor API for auto_label_units
#4338
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
6
commits into
SpikeInterface:main
Choose a base branch
from
alejoe91:unitrefine-actor
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.
+152
−28
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
42aa4ff
auto_label_units -> model_base_label_units + unitrefine function
alejoe91 bbd0201
Use lightweight model by default
alejoe91 03d7077
examples!
alejoe91 6bfdb8b
Update src/spikeinterface/curation/unitrefine_curation.py
alejoe91 fc0ed03
Merge branch 'main' into unitrefine-actor
alejoe91 48cd1c5
Deprecate auto_label_units and set default models to None
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| from pathlib import Path | ||
|
|
||
| from spikeinterface.core import SortingAnalyzer | ||
| from spikeinterface.curation.model_based_curation import model_based_label_units | ||
|
|
||
|
|
||
| def unitrefine_label_units( | ||
| sorting_analyzer: SortingAnalyzer, | ||
| noise_neural_classifier: str | Path | None = None, | ||
| sua_mua_classifier: str | Path | None = None, | ||
| ): | ||
| """Label units using a cascade of pre-trained classifiers for | ||
| noise/neural unit classification and SUA/MUA classification, | ||
| as shown in the UnitRefine paper (see References). | ||
| The noise/neural classifier is applied first to remove noise units, | ||
| then the SUA/MUA classifier is applied to the remaining units. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| sorting_analyzer : SortingAnalyzer | ||
| The sorting analyzer object containing the spike sorting results. | ||
| noise_neural_classifier : str or Path or None, default: None | ||
| The path to the folder containing the model or a string to a repo on HuggingFace. | ||
| If None, the noise/neural classification step is skipped. | ||
| Make sure to provide at least one of the two classifiers. | ||
| sua_mua_classifier : str or Path or None, default: None | ||
| The path to the folder containing the model or a string to a repo on HuggingFace. | ||
| If None, the SUA/MUA classification step is skipped. | ||
|
|
||
| Returns | ||
| ------- | ||
| labels : pd.DataFrame | ||
| A DataFrame with unit ids as index and "label"/"probability" as column. | ||
|
|
||
| References | ||
| ---------- | ||
| The approach is described in [Jain]_. | ||
| """ | ||
| import pandas as pd | ||
| import pandas as pd | ||
|
|
||
| if noise_neural_classifier is None and sua_mua_classifier is None: | ||
| raise ValueError( | ||
| "At least one of noise_neural_classifier or sua_mua_classifier must be provided. " | ||
| "Pre-trained models can be found at https://huggingface.co/collections/SpikeInterface/curation-models or " | ||
| "https://huggingface.co/AnoushkaJain3/models. You can also train models on your own data: " | ||
| "see https://github.com/anoushkajain/UnitRefine for more details." | ||
| ) | ||
|
|
||
| if noise_neural_classifier is not None: | ||
| # 1. apply the noise/neural classification and remove noise | ||
| noise_neuron_labels = model_based_label_units( | ||
| sorting_analyzer=sorting_analyzer, | ||
| repo_id=noise_neural_classifier, | ||
| trust_model=True, | ||
| ) | ||
| noise_units = noise_neuron_labels[noise_neuron_labels["prediction"] == "noise"] | ||
| sorting_analyzer_neural = sorting_analyzer.remove_units(noise_units.index) | ||
| else: | ||
| sorting_analyzer_neural = sorting_analyzer | ||
| noise_units = pd.DataFrame(columns=["prediction", "probability"]) | ||
|
|
||
| if sua_mua_classifier is not None: | ||
| # 2. apply the sua/mua classification and aggregate results | ||
| if len(sorting_analyzer.unit_ids) > len(noise_units): | ||
| sua_mua_labels = model_based_label_units( | ||
| sorting_analyzer=sorting_analyzer_neural, | ||
| repo_id=sua_mua_classifier, | ||
| trust_model=True, | ||
| ) | ||
| all_labels = pd.concat([sua_mua_labels, noise_units]).sort_index() | ||
| else: | ||
| all_labels = noise_units | ||
| else: | ||
| all_labels = noise_neuron_labels | ||
|
|
||
| # rename prediction column to label | ||
| all_labels = all_labels.rename(columns={"prediction": "label"}) | ||
| return all_labels |
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.
Can we deprecate
auto_label_units(slowly!)? I think a lot of people have copies of unitRefine notebooks using it!