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.
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.
$ 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.
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):
$ 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 (make sure to select the C++ build tools during installation) before proceeding to install Spotiflow.
Finally, install spotiflow
using pip
:
$ 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:
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:
# 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
$ 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 plugin. See Predicting spots using the napari plugin for more information.
Contents¶
- Predicting spots using the napari plugin
- Inference via CLI
- Available pre-trained models
- Training a Spotiflow model on a custom dataset
- Fine-tuning a Spotiflow model on a custom dataset
- API Reference
- spotiflow.model.spotiflow.Spotiflow
- spotiflow.model.config.SpotiflowModelConfig
- spotiflow.model.config.SpotiflowTrainingConfig
- spotiflow.data.spots.SpotsDataset
- spotiflow.utils.get_data
- spotiflow.utils.normalize
- spotiflow.utils.read_coords_csv
- spotiflow.utils.write_coords_csv
- spotiflow.sample_data.test_image_hybiss_2d
- spotiflow.sample_data.test_image_synth_3d
- spotiflow.sample_data.test_image_terra_2d