Skip to main content

Models and methods to generate counterfactuals for computed tomography scans

Project description

CT Counterfactuals

The code and models here were used in the Paper 📄 Merlin: A Vision Language Foundation Model for 3D Computed Tomography .

Open In Colab

Classifiers

A 1692 target classifier predicting phenotypes from CT scans

import ct_counterfactuals as ct_cf
model = ct_cf.classifiers.phecode.PheCodeClassifier()
x = torch.ones([1, 1, 224, 224, 174])
out = model(x)
out.shape # [1, 1692]

A lung segmentation model from CT slices

import ct_counterfactuals as ct_cf
model = ct_cf.classifiers.lungmask.LungMaskSegmenter()
x = torch.ones([1, 1, 224, 224, 174])
out = model(x)
out.shape # [1, 3, 224, 224, 1]

# Channels
# 0 = No lung
# 1 = Right lung
# 2 = Left lung

Autoencoders

A VQ-GAN autoencoder trained on CT slices

import ct_counterfactuals as ct_cf
ae = ct_cf.ae.VQGAN(weights='2023-12-25T10-26-40_ct2_vqgan256_sddd')
x = torch.ones([1, 1, 224, 224])
out = ae(x)
out.shape # [1, 1, 224, 224]

Utility code is provided to encode 3D volumes

import ct_counterfactuals as ct_cf
ae = ct_cf.ae.VQGAN(weights='2023-12-25T10-26-40_ct2_vqgan256_sddd')

slice_ae = SliceAEFull(ae, 45, 55) # range specified is where gradients can propigate
x = torch.ones([1, 1, 224, 224, 174])
out = ae(x)
out.shape # [1, 1, 224, 224, 174]

Example CF explainations of the classifier

Effusion (fluid in lungs) Splenomegaly (enlarged spleen)

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

ct-counterfactuals-0.0.1.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

ct_counterfactuals-0.0.1-py3-none-any.whl (27.2 kB view details)

Uploaded Python 3

File details

Details for the file ct-counterfactuals-0.0.1.tar.gz.

File metadata

  • Download URL: ct-counterfactuals-0.0.1.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.0

File hashes

Hashes for ct-counterfactuals-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a3423debaae7366197e8026721ce53451cd5afbd1f75eaaad6b351f49c5b9c06
MD5 41ed121549bbf72cc91214623b30527a
BLAKE2b-256 9a8a198ee6d3b8d51debf4c5369e512c37c0a5f4470e194747ed694a20ace262

See more details on using hashes here.

File details

Details for the file ct_counterfactuals-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ct_counterfactuals-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c5d314853ef3d5a3cab447a7cb84c27b8c8ed01bba21fdab9a768e456670b1bf
MD5 3d7776878f2d5d19d910c0077d6ad4a6
BLAKE2b-256 4161b48500456639d68d284d8c8d5a256a61dd4ee55c254fc490ddfa8e47f03a

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