Skip to content

Commit

Permalink
deploy: 806ce89
Browse files Browse the repository at this point in the history
  • Loading branch information
maweigert committed Oct 16, 2024
0 parents commit 4754aac
Show file tree
Hide file tree
Showing 233 changed files with 9,025 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .buildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Sphinx build info version 1
# This file records the configuration used when building these files. When it is not found, a full rebuild will be done.
config: 12299cc8f79cdf6739ffbaf4bd988240
tags: 645f666f9bcd5a90fca523b33c5a78b7
Binary file added .doctrees/api.doctree
Binary file not shown.
Binary file added .doctrees/cli.doctree
Binary file not shown.
Binary file added .doctrees/environment.pickle
Binary file not shown.
Binary file added .doctrees/finetune.doctree
Binary file not shown.
Binary file added .doctrees/index.doctree
Binary file not shown.
Binary file added .doctrees/napari.doctree
Binary file not shown.
Binary file added .doctrees/pretrained.doctree
Binary file not shown.
Binary file added .doctrees/train.doctree
Binary file not shown.
Empty file added .nojekyll
Empty file.
Binary file added _images/spotiflow_napari_gui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added _images/spotiflow_napari_preds.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions _sources/api.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
API Reference
-------------

.. autoclass:: spotiflow.model.spotiflow.Spotiflow
:members: from_pretrained, from_folder, predict, fit, save, load, optimize_threshold

.. autoclass:: spotiflow.model.config.SpotiflowModelConfig
:members:

.. autoclass:: spotiflow.model.config.SpotiflowTrainingConfig
:members:

.. autoclass:: spotiflow.data.spots.SpotsDataset
:members:

.. automethod:: __init__

.. automodule:: spotiflow.utils
:members: get_data, read_coords_csv, write_coords_csv, normalize

.. automodule:: spotiflow.sample_data
:members:
12 changes: 12 additions & 0 deletions _sources/cli.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Inference via CLI
-----------------

Command Line Interface (CLI)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can use the CLI to run inference on an image or folder containing several images. To do that, you can use the following command

.. code-block:: console
$ spotiflow predict --input PATH
where ``PATH`` can be either an image or a folder. By default, the command will use the ``general`` pretrained model. You can specify a different model by using the ``--pretrained-model`` flag. Moreover, spots are saved to a subfolder ``spotiflow_results`` created inside the input folder (this can be changed with the ``--out-dir`` flag). For more information, please refer to the help message of the CLI (``spotiflow-predict -h``).
50 changes: 50 additions & 0 deletions _sources/finetune.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Fine-tuning a Spotiflow model on a custom dataset
-------------------------------------------------

Data format
^^^^^^^^^^^

See :ref:`train:Data format`.

Fine-tuning (CLI)
^^^^^^^^^^^^^^^^^

You can fine-tune from an existing model by simply adding an argument to the CLI call. See :ref:`train:Basic training (CLI)` for more information.

.. code-block:: console
spotiflow-train /path/to/spots_data --save-dir /my/trained/model --pretrained-model general
where `/path/to/pretrained/model` is the path to the directory containing the model you want to fine-tune. You can also pass other parameters to the training, such as the number of epochs, the learning rate, etc. For more information on the arguments allowed, see the documentation of the CLI command:


Fine-tuning (API)
^^^^^^^^^^^^^^^^^

Finetuning a pre-trained model on a custom dataset is very easy. You can load the model very similarly to what you would normally do to predict on new images (you only need to add one extra parameter!):

.. code-block:: python
from spotiflow.model import Spotiflow
from spotiflow.utils import get_data
# Get the data
train_imgs, train_spots, val_imgs, val_spots = get_data("/path/to/spots_data")
# Initialize the model
model = Spotiflow.from_pretrained(
"general",
inference_mode=False,
)
# Train and save the model
model.fit(
train_imgs,
train_spots,
val_imgs,
val_spots,
save_dir="/my/trained/model",
)
Of course, you can also fine-tune from a model you have trained before. In that case, use the ``from_folder()`` method instead of ``from_pretrained()`` (see :ref:`index:Predicting spots in an image`).
All the information about training customization from :ref:`train:Customizing the training` applies here as well. However, note that you cannot change the model architecture when fine-tuning!
102 changes: 102 additions & 0 deletions _sources/index.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
:hero: Spotiflow: accurate and robust spot detection for fluorescence microscopy

=========
Spotiflow
=========

Spotiflow is a learning-based subpixel-accurate spot detection method for 2D and 3D fluorescence microscopy. It is primarily developed for spatial transcriptomics workflows that require transcript detection in large, multiplexed FISH-images, although it can also be used to detect spot-like structures in general fluorescence microscopy images and volumes. For more information, please refer to our `paper <https://doi.org/10.1101/2024.02.01.578426/>`__.

Getting Started
---------------

Installation
~~~~~~~~~~~~


First, create and activate a fresh ``conda`` environment (we currently support Python 3.9 to 3.12). If you don't have ``conda`` installed, we recommend using `miniforge <https://github.com/conda-forge/miniforge>`__.

.. code-block:: console
$ conda create -n spotiflow python=3.12
$ conda activate spotiflow
**Note (for MacOS users):** if using MacOS, there is a known bug causing the installation of PyTorch with conda to sometimes break OpenMP. You can avoid installing PyTorch with ``conda`` and let install it automatically via pip instead.
Then, install Pytorch using ``conda``/ ``mamba``. Please follow the `official instructions for your system <https://pytorch.org/get-started/locally>`__.

As an example, for a Linux system with CUDA (note that you should change the CUDA version to match the one installed on your system):

.. code-block:: console
$ conda install pytorch torchvision pytorch-cuda=12.1 -c pytorch -c nvidia
**Note (for Windows users):** if using Windows, if using Windows, please install the latest `Build Tools for Visual Studio <https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2022>`__ (make sure to select the C++ build tools during installation) before proceeding to install Spotiflow.

Finally, install ``spotiflow`` using ``pip``:

.. code-block:: console
$ pip install spotiflow
Predicting spots in an image
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Python API
^^^^^^^^^^

The snippet below shows how to retrieve the spots from an image using one of the pretrained models:

.. code-block:: python
from skimage.io import imread
from spotiflow.model import Spotiflow
from spotiflow.utils import write_coords_csv
# Load the desired image
img = imread("/path/to/your/image")
# Load a pretrained model
model = Spotiflow.from_pretrained("general")
# Predict spots
spots, details = model.predict(img) # predict expects a numpy array
# spots is a numpy array with shape (n_spots, 2)
# details contains additional information about the prediction, like the predicted heatmap, the probability per spot, the flow field, etc.
# Save the results to a CSV file
write_coords_csv(spots, "/path/to/save/spots.csv")
If a custom model is used, simply change the model loadings step to:

.. code-block:: python
# Load a custom model
model = Spotiflow.from_folder("/path/to/model")
Command Line Interface (CLI)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can use the CLI to run inference on an image or folder containing several images. To do that, you can use the following command

.. code-block:: console
$ spotiflow predict --input PATH
where ``PATH`` can be either an image or a folder. By default, the command will use the ``general`` pretrained model. You can specify a different model by using the ``--pretrained-model`` flag. Moreover, spots are saved to a subfolder ``spotiflow_results`` created inside the input folder (this can be changed with the ``--out-dir`` flag). For more information, please refer to the help message of the CLI (``spotiflow-predict -h``).

Napari plugin
^^^^^^^^^^^^^
Spotiflow also can be run easily in a graphical user interface as a `napari <https://napari.org/>`__ plugin. See :ref:`napari:Predicting spots using the napari plugin` for more information.

Contents
--------

.. toctree::
:maxdepth: 2

napari
cli
pretrained
train
finetune
api
26 changes: 26 additions & 0 deletions _sources/napari.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Predicting spots using the napari plugin
----------------------------------------

The napari plugin can be used to predict spots in a napari viewer. First, you must install it in the environment containing Spotiflow:

.. code-block:: console
(spotiflow) $ pip install napari-spotiflow
The plugin will then be available in the napari GUI under the name "Spotiflow widget". This is how the GUI looks like:

.. image:: ./_static/spotiflow_napari_gui.png
:width: 700
:align: center

The plugin allows running on two modes: for images (``2D``) and volumes (``3D``), which can be toggled using the corresponding buttons in the GUI. You can also run on movies by setting the appropriate axis order (should be leading with a `T`).

Upon pressing the button ``Run``, The plugin will create a ``Points`` layer containing the predicted spots:

.. image:: ./_static/spotiflow_napari_preds.png
:width: 700
:align: center

If the option ``Show CNN output`` is checked, the plugin will also create two ``Image`` layers containing the heatmap output of the CNN as well as the stereographic flow.

Finally, the plugin includes two sample 2D images (HybISS and Terra) as well as a synthetic 3D volume. These samples can be loaded from the ``File`` menu (``File -> Open sample -> napari-spotiflow``). You can try the plugin with these samples to get a better idea of how it works!
21 changes: 21 additions & 0 deletions _sources/pretrained.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Available pre-trained models
----------------------------

The following pre-trained models are available (for a more detailed description, please refer to the *Methods* section of the paper as well as *Supplementary Table 2*):

- ``general``: trained on a diverse dataset of spots of different modalities acquired in different microscopes with different settings. This model is the default one used in the CLI (pixel sizes: 0.04µm, 0.1µm, 0.11µm, 0.15µm, 0.32µm, 0.34µm).
- ``hybiss``: trained on HybISS data acquired in 3 different microscopes (pixel sizes: 0.15µm, 0.32µm, 0.34µm).
- ``synth_complex``: trained on synthetic data, which includes simulations of aberrated spots and fluorescence background (pixel size: 0.1µm).
- ``synth_3d``: trained on synthetic 3D data, which includes simulations of aberrated spots and Z-related artifacts (voxel size: 0.2µm).
- ``smfish_3d``: fine-tuned from the ``synth_3d`` model on smFISH 3D data of *Platynereis dumerilii* (voxel size: 0.13µm (YX), 0.48µm (Z)).

You can use these models to predict spots in images or to fine-tune them on a few annotations of your own data. The models can be loaded via the API as follows:

.. code-block:: python
from spotiflow.model import Spotiflow
pretrained_model_name = "general"
model = Spotiflow.from_pretrained(pretrained_model_name)
You can also load them from the napari plugin or from the CLI by specifying the name of the model. See :ref:`napari:Predicting spots using the napari plugin` and :ref:`cli:Inference via CLI` for more information respectively.
Loading

0 comments on commit 4754aac

Please sign in to comment.