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.3.tar.gz (26.7 kB view details)

Uploaded Source

Built Distribution

ct_counterfactuals-0.0.3-py3-none-any.whl (28.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ct-counterfactuals-0.0.3.tar.gz
  • Upload date:
  • Size: 26.7 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.3.tar.gz
Algorithm Hash digest
SHA256 24fb06a4d2a0b338d48386dd3055f151b88bf73d5d2e19a09a8933c0a8d9eeb4
MD5 35178355092263295f7c4f37a25f79f7
BLAKE2b-256 5478c68182340436fae2ec4431453fc460fa78e27cfa5cba9d69adcb2b07a114

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ct_counterfactuals-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f004083cbb9b719f19be271650e51d574414637cff0a1bb6feff45d594277801
MD5 88f7eccf76b92c7394f6f8e92de6b156
BLAKE2b-256 54bb392909f7af44cbe30cbd0807699fa95f58afd6e7f18784999da79e6f7ee6

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