Skip to content

Commit 5d54d40

Browse files
committed
Merge branch 'main' of github.com:SpikeInterface/spikeinterface into zarr-dep
2 parents 919a65e + cd63bf0 commit 5d54d40

105 files changed

Lines changed: 3679 additions & 534 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

doc/api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ spikeinterface.curation
346346
.. autofunction:: remove_redundant_units
347347
.. autofunction:: remove_duplicated_spikes
348348
.. autofunction:: remove_excess_spikes
349+
.. autofunction:: load_model
350+
.. autofunction:: auto_label_units
351+
.. autofunction:: train_model
349352

350353
Deprecated
351354
~~~~~~~~~~

doc/conf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,15 @@
119119

120120
# for sphinx gallery plugin
121121
sphinx_gallery_conf = {
122-
'only_warn_on_example_error': True,
122+
# This is the default but including here explicitly. Should build all docs and fail on gallery failures only.
123+
# other option would be abort_on_example_error, but this fails on first failure. So we decided against this.
124+
'only_warn_on_example_error': False,
123125
'examples_dirs': ['../examples/tutorials'],
124126
'gallery_dirs': ['tutorials' ], # path where to save gallery generated examples
125127
'subsection_order': ExplicitOrder([
126128
'../examples/tutorials/core',
127129
'../examples/tutorials/extractors',
130+
'../examples/tutorials/curation',
128131
'../examples/tutorials/qualitymetrics',
129132
'../examples/tutorials/comparison',
130133
'../examples/tutorials/widgets',
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
How to use a trained model to predict the curation labels
2+
=========================================================
3+
4+
For a more detailed guide to using trained models, `read our tutorial here
5+
<https://spikeinterface.readthedocs.io/en/latest/tutorials/curation/plot_1_automated_curation.html>`_).
6+
7+
There is a Collection of models for automated curation available on the
8+
`SpikeInterface HuggingFace page <https://huggingface.co/SpikeInterface>`_.
9+
10+
We'll apply the model ``toy_tetrode_model`` from ``SpikeInterface`` on a SortingAnalyzer
11+
called ``sorting_analyzer``. We assume that the quality and template metrics have
12+
already been computed.
13+
14+
We need to pass the ``sorting_analyzer``, the ``repo_id`` (which is just the part of the
15+
repo's URL after huggingface.co/) and that we trust the model.
16+
17+
.. code::
18+
19+
from spikeinterface.curation import auto_label_units
20+
21+
labels_and_probabilities = auto_label_units(
22+
sorting_analyzer = sorting_analyzer,
23+
repo_id = "SpikeInterface/toy_tetrode_model",
24+
trust_model = True
25+
)
26+
27+
If you have a local directory containing the model in a ``skops`` file you can use this to
28+
create the labels:
29+
30+
.. code::
31+
32+
labels_and_probabilities = si.auto_label_units(
33+
sorting_analyzer = sorting_analyzer,
34+
model_folder = "my_folder_with_a_model_in_it",
35+
)
36+
37+
The returned labels are a dictionary of model's predictions and it's confidence. These
38+
are also saved as a property of your ``sorting_analyzer`` and can be accessed like so:
39+
40+
.. code::
41+
42+
labels = sorting_analyzer.sorting.get_property("classifier_label")
43+
probabilities = sorting_analyzer.sorting.get_property("classifier_probability")
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
How to train a model to predict curation labels
2+
===============================================
3+
4+
A full tutorial for model-based curation can be found `here <https://spikeinterface.readthedocs.io/en/latest/tutorials/curation/plot_2_train_a_model.html>`_.
5+
6+
Here, we assume that you have:
7+
8+
* Two SortingAnalyzers called ``analyzer_1`` and
9+
``analyzer_2``, and have calculated some template and quality metrics for both
10+
* Manually curated labels for the units in each analyzer, in lists called
11+
``analyzer_1_labels`` and ``analyzer_2_labels``. If you have used phy, the lists can
12+
be accessed using ``curated_labels = analyzer.sorting.get_property("quality")``.
13+
14+
With these objects calculated, you can train a model as follows
15+
16+
.. code::
17+
18+
from spikeinterface.curation import train_model
19+
20+
analyzer_list = [analyzer_1, analyzer_2]
21+
labels_list = [analyzer_1_labels, analyzer_2_labels]
22+
output_folder = "/path/to/output_folder"
23+
24+
trainer = train_model(
25+
mode="analyzers",
26+
labels=labels_list,
27+
analyzers=analyzer_list,
28+
output_folder=output_folder,
29+
metric_names=None, # Set if you want to use a subset of metrics, defaults to all calculated quality and template metrics
30+
imputation_strategies=None, # Default is all available imputation strategies
31+
scaling_techniques=None, # Default is all available scaling techniques
32+
classifiers=None, # Defaults to Random Forest classifier only - we usually find this gives the best results, but a range of classifiers is available
33+
seed=None, # Set a seed for reproducibility
34+
)
35+
36+
37+
The trainer tries several models and chooses the most accurate one. This model and
38+
some metadata are stored in the ``output_folder``, which can later be loaded using the
39+
``load_model`` function (`more details <https://spikeinterface.readthedocs.io/en/latest/tutorials/curation/plot_1_automated_curation.html#download-a-pretrained-model>`_).
40+
We can also access the model, which is an sklearn ``Pipeline``, from the trainer object
41+
42+
.. code::
43+
44+
best_model = trainer.best_pipeline
45+
46+
47+
The training function can also be run in “csv” mode, if you prefer to
48+
store metrics in as .csv files. If the target labels are stored as a column in
49+
the file, you can point to these with the ``target_label`` parameter
50+
51+
.. code::
52+
53+
trainer = train_model(
54+
mode="csv",
55+
metrics_paths = ["/path/to/csv_file_1", "/path/to/csv_file_2"],
56+
target_label = "my_label",
57+
output_folder=output_folder,
58+
)

doc/how_to/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ Guides on how to solve specific, short problems in SpikeInterface. Learn how to.
1515
load_your_data_into_sorting
1616
benchmark_with_hybrid_recordings
1717
drift_with_lfp
18+
auto_curation_training
19+
auto_curation_prediction

doc/images/files_screen.png

96.9 KB
Loading

doc/images/hf-logo.svg

Lines changed: 8 additions & 0 deletions
Loading
33.8 KB
Loading

doc/tutorials_custom_index.rst

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ The :code:`spikeinterface.qualitymetrics` module allows users to compute various
119119

120120
.. grid-item-card:: Quality Metrics
121121
:link-type: ref
122-
:link: sphx_glr_tutorials_qualitymetrics_plot_3_quality_mertics.py
123-
:img-top: /tutorials/qualitymetrics/images/thumb/sphx_glr_plot_3_quality_mertics_thumb.png
122+
:link: sphx_glr_tutorials_qualitymetrics_plot_3_quality_metrics.py
123+
:img-top: /tutorials/qualitymetrics/images/thumb/sphx_glr_plot_3_quality_metrics_thumb.png
124124
:img-alt: Quality Metrics
125125
:class-card: gallery-card
126126
:text-align: center
@@ -133,6 +133,39 @@ The :code:`spikeinterface.qualitymetrics` module allows users to compute various
133133
:class-card: gallery-card
134134
:text-align: center
135135

136+
Automated curation tutorials
137+
----------------------------
138+
139+
Learn how to curate your units using a trained machine learning model. Or how to create
140+
and share your own model.
141+
142+
.. grid:: 1 2 2 3
143+
:gutter: 2
144+
145+
.. grid-item-card:: Model-based curation
146+
:link-type: ref
147+
:link: sphx_glr_tutorials_curation_plot_1_automated_curation.py
148+
:img-top: /tutorials/curation/images/sphx_glr_plot_1_automated_curation_002.png
149+
:img-alt: Model-based curation
150+
:class-card: gallery-card
151+
:text-align: center
152+
153+
.. grid-item-card:: Train your own model
154+
:link-type: ref
155+
:link: sphx_glr_tutorials_curation_plot_2_train_a_model.py
156+
:img-top: /tutorials/curation/images/thumb/sphx_glr_plot_2_train_a_model_thumb.png
157+
:img-alt: Train your own model
158+
:class-card: gallery-card
159+
:text-align: center
160+
161+
.. grid-item-card:: Upload your model to HuggingFaceHub
162+
:link-type: ref
163+
:link: sphx_glr_tutorials_curation_plot_3_upload_a_model.py
164+
:img-top: /images/hf-logo.svg
165+
:img-alt: Upload your model
166+
:class-card: gallery-card
167+
:text-align: center
168+
136169
Comparison tutorial
137170
-------------------
138171

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Curation tutorials
2+
------------------
3+
4+
Learn how to use models to automatically curated your sorted data, or generate models
5+
based on your own curation.

0 commit comments

Comments
 (0)