Skip to main content

Automated 3D cell detection in large microscopy images

Project description

Python Version PyPI Downloads Wheel Development Status Tests Coverage Status Code style: black Contributions Twitter

cellfinder-core

Standalone cellfinder cell detection algorithm

This package implements the cell detection algorithm from Tyson, Rousseau & Niedworok et al. (2021) without any dependency on data type (i.e. it can be used outside of whole-brain microscopy).

cellfinder-core supports the cellfinder software for whole-brain microscopy analysis, and the algorithm can also be implemented in napari using the cellfinder napari plugin.


Instructions

Installation

cellfinder-core supports Python >=3.7, and works across Linux, Windows, and should work on most versions of macOS (although this is not tested).

Assuming you have a Python environment set up (e.g. using conda), you can install cellfinder-core with:

pip install cellfinder-core

Once you have installed napari. You can install napari either through the napari plugin installation tool, or directly from PyPI with:

pip install cellfinder-napari

N.B. To speed up cellfinder, you need CUDA & cuDNN installed. Instructions here.

Usage

Before using cellfinder-core, it may be useful to take a look at the preprint which outlines the algorithm.

The API is not yet fully documented. For an idea of what the parameters do, see the documentation for the cellfinder whole-brain microscopy image analysis command-line tool (cell candidate detection, cell candidate classification). It may also be useful to try the cellfinder napari plugin so you can adjust the parameters in a GUI.

To run the full pipeline (cell candidate detection and classification)

from cellfinder_core.main import main as cellfinder_run
import tifffile

signal_array = tifffile.imread("/path/to/signal_image.tif")
background_array = tifffile.imread("/path/to/background_image.tif")

voxel_sizes = [5, 2, 2] # in microns
detected_cells = cellfinder_run(signal_array,background_array,voxel_sizes)

The output is a list of imlib Cell objects. Each Cell has a centroid coordinate, and a type:

print(detected_cells[0])
# Cell: x: 132, y: 308, z: 10, type: 2

Cell type 2 is a "real" cell, and Cell type 1 is a "rejected" object (i.e. not classified as a cell):

from imlib.cells.cells import Cell
print(Cell.CELL)
# 2

print(Cell.NO_CELL)
# 1

Saving the results

If you want to save the detected cells for use in other BrainGlobe software (e.g. the cellfinder napari plugin), you can save in the cellfinder XML standard:

from imlib.IO.cells import save_cells
save_cells(detected_cells, "/path/to/cells.xml")

You can load these back with:

from imlib.IO.cells import get_cells
cells = get_cells("/path/to/cells.xml")

Using dask for lazy loading

cellfinder-core supports most array-like objects. Using Dask arrays allows for lazy loading of data, allowing large (e.g. TB) datasets to be processed. cellfinder-core comes with a function (based on napari-ndtiffs) to load a series of image files (e.g. a directory of 2D tiff files) as a Dask array. cellfinder-core can then be used in the same way as with a numpy array.

from cellfinder_core.main import main as cellfinder_run
from cellfinder_core.tools.IO import read_with_dask

signal_array = read_with_dask("/path/to/signal_image_directory")
background_array = read_with_dask("/path/to/background_image_directory")

voxel_sizes = [5, 2, 2] # in microns
detected_cells = cellfinder_run(signal_array,background_array,voxel_sizes)

Running the cell candidate detection and classification separately.

import tifffile
from pathlib import Path

from cellfinder_core.detect import detect
from cellfinder_core.classify import classify

signal_array = tifffile.imread("/path/to/signal_image.tif")
background_array = tifffile.imread("/path/to/background_image.tif")
voxel_sizes = [5, 2, 2] # in microns

home = Path.home()
install_path = home / ".cellfinder" # default

start_plane=0
end_plane=-1
trained_model=None
model_weights=None
model="resnet50_tv"
batch_size=32
n_free_cpus=2
network_voxel_sizes=[5, 1, 1]
soma_diameter=16
ball_xy_size=6
ball_z_size=15
ball_overlap_fraction=0.6
log_sigma_size=0.2
n_sds_above_mean_thresh=10
soma_spread_factor=1.4
max_cluster_size=100000
cube_width=50
cube_height=50
cube_depth=20
network_depth="50"

cell_candidates = detect.main(
    signal_array,
    start_plane,
    end_plane,
    voxel_sizes,
    soma_diameter,
    max_cluster_size,
    ball_xy_size,
    ball_z_size,
    ball_overlap_fraction,
    soma_spread_factor,
    n_free_cpus,
    log_sigma_size,
    n_sds_above_mean_thresh,
)

if len(cell_candidates) > 0: # Don't run if there's nothing to classify
    classified_cells = classify.main(
        cell_candidates,
        signal_array,
        background_array,
        n_free_cpus,
        voxel_sizes,
        network_voxel_sizes,
        batch_size,
        cube_height,
        cube_width,
        cube_depth,
        trained_model,
        model_weights,
        network_depth,
    )

Training the network

The training data needed are matched pairs (signal & background) of small (usually 50 x 50 x 100um) images centered on the coordinate of candidate cells. These can be generated however you like, but I recommend using the Napari plugin.

cellfinder-core comes with a 50-layer ResNet trained on ~100,000 data points from serial two-photon microscopy images of mouse brains (available here).

Training the network is likely simpler using the command-line interface or the Napari plugin, but it is possible through the Python API.

from pathlib import Path
from cellfinder_core.train.train_yml import run as run_training

# list of training yml files
yaml_files = [Path("/path/to/training_yml.yml)]

# where to save the output
output_directory = Path("/path/to/saved_training_data")

home = Path.home()
install_path = home / ".cellfinder"  # default

run_training(
    output_directory,
    yaml_files,
    install_path=install_path,
    learning_rate=0.0001,
    continue_training=True, # by default use supplied model
    test_fraction=0.1,
    batch_size=32,
    save_progress=True,
    epochs=10,
)

More info

More documentation about cellfinder and other BrainGlobe tools can be found here.

This software is at a very early stage, and was written with our data in mind. Over time we hope to support other data types/formats. If you have any questions or issues, please get in touch by email, on the forum or by raising an issue.


Illustration

Introduction

cellfinder takes a stitched, but otherwise raw dataset with at least two channels:

  • Background channel (i.e. autofluorescence)
  • Signal channel, the one with the cells to be detected:

raw Raw coronal serial two-photon mouse brain image showing labelled cells

Cell candidate detection

Classical image analysis (e.g. filters, thresholding) is used to find cell-like objects (with false positives):

raw Candidate cells (including many artefacts)

Cell candidate classification

A deep-learning network (ResNet) is used to classify cell candidates as true cells or artefacts:

raw Cassified cell candidates. Yellow - cells, Blue - artefacts


Citing cellfinder

If you find this plugin useful, and use it in your research, please cite the preprint outlining the cell detection algorithm:

Tyson, A. L., Rousseau, C. V., Niedworok, C. J., Keshavarzi, S., Tsitoura, C., Cossell, L., Strom, M. and Margrie, T. W. (2021) “A deep learning algorithm for 3D cell detection in whole mouse brain image datasets’ PLOS Computational Biology, 17(5), e1009074 https://doi.org/10.1371/journal.pcbi.1009074

If you use this, or any other tools in the brainglobe suite, please let us know, and we'd be happy to promote your paper/talk etc.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cellfinder-core-0.2.6.tar.gz (140.8 kB view details)

Uploaded Source

Built Distributions

cellfinder_core-0.2.6-cp39-cp39-win_amd64.whl (326.5 kB view details)

Uploaded CPython 3.9 Windows x86-64

cellfinder_core-0.2.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

cellfinder_core-0.2.6-cp39-cp39-macosx_10_14_x86_64.whl (343.0 kB view details)

Uploaded CPython 3.9 macOS 10.14+ x86-64

cellfinder_core-0.2.6-cp38-cp38-win_amd64.whl (327.7 kB view details)

Uploaded CPython 3.8 Windows x86-64

cellfinder_core-0.2.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

cellfinder_core-0.2.6-cp38-cp38-macosx_10_14_x86_64.whl (335.7 kB view details)

Uploaded CPython 3.8 macOS 10.14+ x86-64

cellfinder_core-0.2.6-cp37-cp37m-win_amd64.whl (320.9 kB view details)

Uploaded CPython 3.7m Windows x86-64

cellfinder_core-0.2.6-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

cellfinder_core-0.2.6-cp37-cp37m-macosx_10_14_x86_64.whl (335.3 kB view details)

Uploaded CPython 3.7m macOS 10.14+ x86-64

File details

Details for the file cellfinder-core-0.2.6.tar.gz.

File metadata

  • Download URL: cellfinder-core-0.2.6.tar.gz
  • Upload date:
  • Size: 140.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for cellfinder-core-0.2.6.tar.gz
Algorithm Hash digest
SHA256 695805c2f7258b4e46f6a737677e48d90e9e4ac392f4be4806ca87f6ab1ac312
MD5 b33aa5b03ef38841823635d1e215fcc2
BLAKE2b-256 0b9e0eaaf1745a55980c62e127bfd15d4242a4869db4eef56e09a60595a09e24

See more details on using hashes here.

File details

Details for the file cellfinder_core-0.2.6-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: cellfinder_core-0.2.6-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 326.5 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for cellfinder_core-0.2.6-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 0bbeb80ebbc7a4ec7e8f39888ebe68a7f15f21edbf9a9c48f8d39b897cd7b561
MD5 ce183aaf689211f975995609a80bd917
BLAKE2b-256 e0208e2793a5834d20ce0e9124bc4e740169ca92209960b49230a46962ec4119

See more details on using hashes here.

File details

Details for the file cellfinder_core-0.2.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cellfinder_core-0.2.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e3e12e624739391600ea841d96622b3d8d6143ad4cba731431130fb33f2e5bd7
MD5 0eebf9534aa744b6877ab4d0b635b765
BLAKE2b-256 fc35d23bd736025233ac163aa66045dea2fecf506aaa96df21f8f711ea02d51e

See more details on using hashes here.

File details

Details for the file cellfinder_core-0.2.6-cp39-cp39-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: cellfinder_core-0.2.6-cp39-cp39-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 343.0 kB
  • Tags: CPython 3.9, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for cellfinder_core-0.2.6-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 7cb6fee3e6765ea73aac111c54e82c1c687f6dec79bce70a7a63451448f00890
MD5 afb035c349dbf40c54b8f69a2a65f952
BLAKE2b-256 30c5b71af20cc36fcb6f750eb3e6bbf20084268608c3258f0b11dcc27288d0b4

See more details on using hashes here.

File details

Details for the file cellfinder_core-0.2.6-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: cellfinder_core-0.2.6-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 327.7 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10

File hashes

Hashes for cellfinder_core-0.2.6-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 dc9fb728c17b412cb9470a0c27331653f12ab537c35d124d2b378a3f6f2aa4e4
MD5 7ec3d61c1bdeadf08008c00037979ae2
BLAKE2b-256 551e938a45e33e2a5c4b9791aa0ce251ea2455f0225dbfc3494ac0d0497444df

See more details on using hashes here.

File details

Details for the file cellfinder_core-0.2.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cellfinder_core-0.2.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6ac3b15b821b6aeb1e14d8baaa582c49876fb700fa22ed382dcd7f713f1b1748
MD5 3886e6bf05f35998e2a3f69e7487864c
BLAKE2b-256 f7416755ea65a024beccf705f05e04cca16b1c95f912d1195d9e4ab5f2816cf8

See more details on using hashes here.

File details

Details for the file cellfinder_core-0.2.6-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: cellfinder_core-0.2.6-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 335.7 kB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10

File hashes

Hashes for cellfinder_core-0.2.6-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 790b684ec4e5fa3a89597b0e4193173886dff0ae65e5c9e691775c93afcbe8c9
MD5 e63d8fb1791842cc310f25bc19a723eb
BLAKE2b-256 0f3abc22e3f72fc9ec0cf83b36accb7ec6a89eb7ca5c7986d8ad923cf5fdd167

See more details on using hashes here.

File details

Details for the file cellfinder_core-0.2.6-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: cellfinder_core-0.2.6-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 320.9 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.7.9

File hashes

Hashes for cellfinder_core-0.2.6-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 1ad5f0241a662f9288085cb83d14fa976afda37cd35f8d8b8c89e701e94214ae
MD5 76879e83de6c5853f36e8d52c67ae4b7
BLAKE2b-256 87975d643e59deb8ab5a81e62f74bb489a948ac2a35d5db754aca1967802ecba

See more details on using hashes here.

File details

Details for the file cellfinder_core-0.2.6-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cellfinder_core-0.2.6-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 53cbc65521efce4f98715981605b5f115c95e8e72fb8c773155e43edec255b66
MD5 5de00af10815a0a955046e26cc358f86
BLAKE2b-256 7f5b288a4f5e04bf12150d5301b417c28a72565d3d13c9aa96c6a9d2b8a7ac8e

See more details on using hashes here.

File details

Details for the file cellfinder_core-0.2.6-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: cellfinder_core-0.2.6-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 335.3 kB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.7.10

File hashes

Hashes for cellfinder_core-0.2.6-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 88f43e6b9fc06d13740158c1732567d9427e31e9a7af1a8e887e9ecfdad614b7
MD5 f0ebecd32dbced110b7b0b5f8ef60111
BLAKE2b-256 99e5cad06fa90c73208a6854051ff58f1c492ee3368087dba0910d69325984af

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page