|
| 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 | + ) |
0 commit comments