Skip to main content

3D shape analysis using deep learning

Project description

Project Status: Active – The project has reached a stable, usable state and is being actively developed. Python Version PyPI Downloads Wheel Development Status Tests Coverage Status Code style: black

Cellshape logo by Matt De Vries

3D single-cell shape analysis of cancer cells using geometric deep learning

This is a package for automatically learning and clustering cell shapes from 3D images. Please refer to our preprint on bioRxiv here

cellshape is available for everyone.

cellshape is the main package which is made up of sub-packages:

Installation and requirements

Dependencies

The software requires Python 3.7 or greater, PyTorch, torchvision, pyntcloud, numpy, scikit-learn, tensorboard, tqdm, datetime. This repo makes extensive use of cellshape-cloud, cellshape-cluster, cellshape-helper, and cellshape-voxel. to reproduce our results in our paper, only cellshape-cloud, cellshape-cluster are needed.

To install

  1. We recommend creating a new conda environment:
conda create --name cellshape-env python=3.8
conda activate cellshape-env
pip install --upgrade pip
  1. Install cellshape from pip
pip install cellshape

Hardware requirements

We have tested this software of an Ubuntu 20.04LTS with 128Gb RAM and NVIDIA Quadro RTX 6000 GPU.

Data structure

Our data is structured in the following way:

Data/
    all_data_stats.csv
    Plate1/
        stacked_pointcloud/
            Binimetinib/
                0010_0001_accelerator_20210315_bakal01_erk_main_21-03-15_12-37-27.ply
                ...
            Blebbistatin/
            ...
    Plate2/
        stacked_pointcloud/
    Plate3/
        stacked_pointcloud/

Data availability

Datasets to reproduce our results in our paper are available here.

Usage

The following steps assume that one already has point cloud representations of cells or nuclei. If you need to generate point clouds from 3D binary masks please go to cellshape-helper.

The training procedure follows two steps:

  1. Training the dynamic graph convolutional foldingnet (DFN) autoencoder to automatically learn shape features.
  2. Adding the clustering layer to refine shape features and learn shape classes simultaneous.

Inference can be done after each step.

1. Train DFN autoencoder

python 
import torch
from torch.utils.data import DataLoader
from datetime import datetime
import logging

import cellshape_cloud as cscloud
import cellshape_cluster as cscluster
from cellshape_cloud.vendor.chamfer_distance import ChamferLoss
from cellshape_cloud.helpers.reports import get_experiment_name


input_dir = "/home/mvries/Documents/CellShape/DatasetForTesting/"
batch_size = 20
learning_rate_autoencoder = 0.00001
learning_rate_clustering = 0.000001
num_features = 128
num_clusters = 3
num_epochs_autoencoder = 1
num_epochs_clustering = 3
k=20
encoder_type="dgcnn"
decoder_type = "foldingnetbasic"
output_dir = "/home/mvries/Documents/Testing_output/"
gamma = 1
alpha = 1.0
divergence_tolerance = 0.01
update_interval = 1


autoencoder = cscloud.CloudAutoEncoder(num_features=num_features, 
                         k=k,
                         encoder_type=encoder_type,
                         decoder_type=decoder_type)

dataset = cscloud.PointCloudDataset(input_dir)

dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)

criterion = ChamferLoss()

optimizer = torch.optim.Adam(
    autoencoder.parameters(),
    lr=learning_rate_autoencoder * 16 / batch_size,
    betas=(0.9, 0.999),
    weight_decay=1e-6,
)

name_logging, name_model, name_writer, name = get_experiment_name(
        model=autoencoder, output_dir=output_dir
    )

logging_info = name_logging, name_model, name_writer, name

now = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
logging.basicConfig(filename=name_logging, level=logging.INFO)
logging.info(f"Started training model {name} at {now}.")

output_cloud = cscloud.train(autoencoder, 
                             dataloader,
                             num_epochs_autoencoder, 
                             criterion, 
                             optimizer,
                             logging_info)

autoencoder = output_cloud[0]


model = cscluster.DeepEmbeddedClustering(autoencoder=autoencoder, 
                               num_clusters=num_clusters)

dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False) 
# it is very important that shuffle=False here!
dataloader_inf = DataLoader(dataset, batch_size=1, shuffle=False) 
# it is very important that batch_size=1 and shuffle=False here!

optimizer = torch.optim.Adam(
    model.parameters(),
    lr=learning_rate_clustering * 16 / batch_size,
    betas=(0.9, 0.999),
    weight_decay=1e-6,
)

reconstruction_criterion = ChamferLoss()
cluster_criterion = torch.nn.KLDivLoss(reduction="sum")

cscluster.train(
    model,
    dataloader,
    dataloader_inf,
    num_epochs_clustering,
    optimizer,
    reconstruction_criterion,
    cluster_criterion,
    update_interval,
    gamma,
    divergence_tolerance,
    logging_info
)

For developers

  • Fork the repository
  • Clone your fork
git clone https://github.com/USERNAME/cellshape
  • Install an editable version (-e) with the development requirements (dev)
cd cellshape
pip install -e .[dev] 
  • To install pre-commit hooks to ensure formatting is correct:
pre-commit install
  • To release a new version:

Firstly, update the version with bump2version (bump2version patch, bump2version minor or bump2version major). This will increment the package version (to a release candidate - e.g. 0.0.1rc0) and tag the commit. Push this tag to GitHub to run the deployment workflow:

git push --follow-tags

Once the release candidate has been tested, the release version can be created with:

bump2version release

References

[1] An Tao, 'Unsupervised Point Cloud Reconstruction for Classific Feature Learning', GitHub Repo, 2020

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

cellshape-0.0.15rc0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

cellshape-0.0.15rc0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file cellshape-0.0.15rc0.tar.gz.

File metadata

  • Download URL: cellshape-0.0.15rc0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for cellshape-0.0.15rc0.tar.gz
Algorithm Hash digest
SHA256 23b4b305f950253d71482e37fbfad6d40ce4facea2d7bc3815e7128642cb2530
MD5 188660253b46fb2a41a74445c69d8d3f
BLAKE2b-256 77f08f3a9a6d7a507b165c70c9fec1d5d6ff31a785e1f4ece8f46cc79be891e1

See more details on using hashes here.

File details

Details for the file cellshape-0.0.15rc0-py3-none-any.whl.

File metadata

File hashes

Hashes for cellshape-0.0.15rc0-py3-none-any.whl
Algorithm Hash digest
SHA256 98567f258f3ecc0e6d5fdbb63834fa7bf593ed0ca5d9229f9c2d0e6d16ac435b
MD5 d4c813d2de09c3a45be7d6acbcb5ba76
BLAKE2b-256 cd54f2e11af1bc55f6fc2d8ac6cfa64654898dc405c9924d7b5e884dce289488

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