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.4.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

ct_counterfactuals-0.0.4-py3-none-any.whl (1.3 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ct-counterfactuals-0.0.4.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • 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.4.tar.gz
Algorithm Hash digest
SHA256 e701a3223aca6ccbe89066cf130c15e55a9e0ab3d5c59ccc853349f287678308
MD5 2f1eed7cf6e0a08d72c761c94cb60e67
BLAKE2b-256 d757fbd3f63a5c2325797bd3de1dd950c85b3c74041c58515e6c561b4edb0e58

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ct_counterfactuals-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 cf9b418fffaa20bebf51c2d8cc2fbc1b519113266afa73425b503a75ef57ac43
MD5 9c665b3e71b0cfba2b0da7783707a99d
BLAKE2b-256 e0ec28eff5ba8b057326593167d2d91c73de0af05f0feb29021afb93515c1a28

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