Metadata-Version: 2.4
Name: napari-signal-classifier
Version: 0.0.1
Summary: A napari plugin that classifies annotated signals stored in a table in the .features of a Labels layer using scikit-learn RandomForest classifier.
Author-email: Marcelo Leomil Zoccoler <marzoccoler@gmail.com>
License: 
        Copyright (c) 2023, Marcelo Leomil Zoccoler
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        * Redistributions of source code must retain the above copyright notice, this
          list of conditions and the following disclaimer.
        
        * Redistributions in binary form must reproduce the above copyright notice,
          this list of conditions and the following disclaimer in the documentation
          and/or other materials provided with the distribution.
        
        * Neither the name of copyright holder nor the names of its
          contributors may be used to endorse or promote products derived from
          this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
Project-URL: Homepage, https://github.com/zoccoler/napari-signal-classifier
Project-URL: Bug Tracker, https://github.com/zoccoler/napari-signal-classifier/issues
Project-URL: Documentation, https://github.com/zoccoler/napari-signal-classifier#README.md
Project-URL: Source Code, https://github.com/zoccoler/napari-signal-classifier
Project-URL: User Support, https://github.com/zoccoler/napari-signal-classifier/issues
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Framework :: napari
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Image Processing
Classifier: Topic :: Scientific/Engineering :: Visualization
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: magicgui
Requires-Dist: qtpy
Requires-Dist: scikit-learn
Requires-Dist: nap-plot-tools>=0.1.2
Requires-Dist: scipy
Requires-Dist: pandas
Requires-Dist: dask
Requires-Dist: tsfresh
Requires-Dist: dtaidistance
Requires-Dist: napari-signal-selector>=0.0.6
Requires-Dist: cmap
Requires-Dist: packaging
Provides-Extra: testing
Requires-Dist: tox; extra == "testing"
Requires-Dist: pytest; extra == "testing"
Requires-Dist: pytest-cov; extra == "testing"
Requires-Dist: pytest-qt; extra == "testing"
Requires-Dist: napari; extra == "testing"
Requires-Dist: pyqt5; extra == "testing"
Dynamic: license-file

# napari-signal-classifier

[![License BSD-3](https://img.shields.io/pypi/l/napari-signal-classifier.svg?color=green)](https://github.com/zoccoler/napari-signal-classifier/raw/main/LICENSE)
[![PyPI](https://img.shields.io/pypi/v/napari-signal-classifier.svg?color=green)](https://pypi.org/project/napari-signal-classifier)
[![Python Version](https://img.shields.io/pypi/pyversions/napari-signal-classifier.svg?color=green)](https://python.org)
[![tests](https://github.com/zoccoler/napari-signal-classifier/workflows/tests/badge.svg)](https://github.com/zoccoler/napari-signal-classifier/actions)
[![codecov](https://codecov.io/gh/zoccoler/napari-signal-classifier/branch/main/graph/badge.svg)](https://codecov.io/gh/zoccoler/napari-signal-classifier)
[![napari hub](https://img.shields.io/endpoint?url=https://api.napari-hub.org/shields/napari-signal-classifier)](https://napari-hub.org/plugins/napari-signal-classifier)

A napari plugin that classifies annotated signals stored in a table in the .features of a Labels layer using scikit-learn RandomForest classifier.

It also provides a sub-signal classifier that can be used to classify sub-signals inside time-series. First it detects sub-sginals with a template matching algorithm and then classifies them also using scikit-learn RandomForest classifier.

This plugin employs and works in synergy with the [napari-signal-selector plugin](https://github.com/zoccoler/napari-signal-selector?tab=readme-ov-file#napari-signal-selector). Take a look at it to see how to annotate signals in a plotter linked to a napari Labels layer with the .features attribute.

# Usage

## Napari Signal Classifier

After having annotated signals in the .features of a Labels layer (check the [napari-signal-selector plugin](https://github.com/zoccoler/napari-signal-selector?tab=readme-ov-file#napari-signal-selector)), you can train a signal classifier and predict the labels of unannotated signals.

Open the "Signal Classifier Widget" from the napari in "Layers > Classify > Signal / Time-series > Train and Predict Signal Classifier". The widget will appear in the right panel of napari.

Choose the Labels layer containing the annotated signals to classify (the annotations should have been previsouly done with the napari-signal-selector plugin). Choose the classifier (currently only RandomForest is implemented). Optionally provide a path to a folder where to save the trained model, select the number of trees (estimators) of the RandomForest, the random state for reproducibility, and the training percentage (the percentage of annotated signals that will be used for training, the rest will be used for testing the model and showing the accuracy, intially 80%). Finally click on "Train and Predict".

Several signal features will be calculated automatically from the signals in the .features of the Labels layer, a RandomForest classifier will be trained on the annotated signals, and the labels of unannotated signals will be predicted. The predicted labels will be stored in a new column in the .features of the Labels layer called "Predictions". The accuracy of the model on the test set will be printed in the napari console.

![demo](https://github.com/zoccoler/napari-signal-classifier/raw/main/images/signal_classifier_demo.gif)

The resulting .features table can be viewed via the native napari features table widget ("Layers > Visualize > Features Table Widget") and exported to a CSV file from there for further analysis.

## Napari Sub-Signal Classifier

After having annotated signals in the .features of a Labels layer (check the [napari-signal-selector plugin](https://github.com/zoccoler/napari-signal-selector?tab=readme-ov-file#napari-signal-selector)), you can train a sub-signal classifier to classify sub-signals inside time-series.

Open the "Sub-Signal Classifier Widget" from the napari in "Layers > Classify > Signal / Time-series > Train and Predict Sub-Signal Classifier". The widget will appear in the right panel of napari.

Choose the same parameters as the Signal Classifier Widget, plus a few additional parameters related to sub-signal detection and merging: the detection threshold for the template matching algorithm (default 0.8), the detrend option (default False) and smooth factor (default 0) for the template generation and the merging overlap threshold (default 0.5) for merging overlapping detected sub-signals. Finally click on "Train and Predict".

A signal template for each class will be generated by the median sub-signal of annotated sub-signals and a cross-correlation based template matching algorithm will be used to detect sub-signals in unannotated time-series. Several signal features will be calculated automatically from the detected sub-signals, a RandomForest classifier will be trained on the annotated sub-signals, and the detected unannotated sub-signals will be predicted. The predicted labels will be stored in a new column in the .features of the Labels layer called "Predictions". The accuracy of the model on the test set will be printed in the napari console.

![demo](https://github.com/zoccoler/napari-signal-classifier/raw/main//images/sub_signal_classifier_demo.gif)

Again, the resulting .features table can be viewed via the native napari features table widget ("Layers > Visualize > Features Table Widget") and exported to a CSV file from there for further analysis.

----------------------------------

This [napari] plugin was generated with [Cookiecutter] using [@napari]'s [cookiecutter-napari-plugin] template.

<!--
Don't miss the full getting started guide to set up your new package:
https://github.com/napari/cookiecutter-napari-plugin#getting-started

and review the napari docs for plugin developers:
https://napari.org/stable/plugins/index.html
-->

## Installation

You can install `napari-signal-classifier` via [pip]:

    pip install napari-signal-classifier



To install latest development version :

    pip install git+https://github.com/zoccoler/napari-signal-classifier.git


## Contributing

Contributions are very welcome. Tests can be run with [tox], please ensure
the coverage at least stays the same before you submit a pull request.

## License

Distributed under the terms of the [BSD-3] license,
"napari-signal-classifier" is free and open source software

## Issues

If you encounter any problems, please [file an issue] along with a detailed description.

[napari]: https://github.com/napari/napari
[Cookiecutter]: https://github.com/audreyr/cookiecutter
[@napari]: https://github.com/napari
[MIT]: http://opensource.org/licenses/MIT
[BSD-3]: http://opensource.org/licenses/BSD-3-Clause
[GNU GPL v3.0]: http://www.gnu.org/licenses/gpl-3.0.txt
[GNU LGPL v3.0]: http://www.gnu.org/licenses/lgpl-3.0.txt
[Apache Software License 2.0]: http://www.apache.org/licenses/LICENSE-2.0
[Mozilla Public License 2.0]: https://www.mozilla.org/media/MPL/2.0/index.txt
[cookiecutter-napari-plugin]: https://github.com/napari/cookiecutter-napari-plugin

[file an issue]: https://github.com/zoccoler/napari-signal-classifier/issues

[napari]: https://github.com/napari/napari
[tox]: https://tox.readthedocs.io/en/latest/
[pip]: https://pypi.org/project/pip/
[PyPI]: https://pypi.org/
