A package for quickly identifying part of the torso imaged an CT scan throught visible vertebrae
Project description
torso mapper
A Python package for quickly identifying parts of the torso imaged in CT scans through visible vertebrae detection.
Description
torso-mapper is a powerful tool designed to analyze CT scan volumes and identify visible vertebrae. It uses deep learning to process 3D medical imaging data. This package is particularly useful for medical professionals and researchers working with CT scans of the torso region.
Features
- Fast and accurate vertebrae detection in CT scans
- Support for various CT scan file formats (.nii, .nii.gz)
- Efficient data loading and preprocessing
- 3D convolutional neural network for vertebrae classification
- Automatic trimming of CT scans to focus on the region of interest
- Visualization tools for CT scan cross-sections
Installation
You can install torso-mapper using pip:
pip install torso-mapper
CLI Usage
The torso-mapper package provides a command-line interface for analyzing CT scans. Here's how to use it:
torso-mapper --input-dir <path_to_scans> --vertebrae <vertebra1> --vertebrae <vertebra2> [OPTIONS]
Arguments:
--input-dir: Directory containing the CT scan files (.nii or .nii.gz)--vertebrae: Target vertebrae to look for (can be specified multiple times)
Options:
--model: Trained model name or file (default: "marius-urbonas/torso-mapper")--batch-size: Batch size for processing (default: 4)--num-workers: Number of worker threads for data loading (default: 4)--device: Device to run the model on ('cuda' or 'cpu', default: 'cpu')--output: Output file to save the analysis results (default: 'analysis_results.txt')
Example:
torso-mapper --input-dir ./ct_scans --vertebrae T11 --vertebrae T12 --vertebrae L1 --output results.json
This command will analyze the CT scans in the ./ct_scans directory, looking for vertebrae T1, T2, and L1, and save the results to results.json.
Specifiable Vertebrae
The torso-mapper can detect the following vertebrae:
- Cervical (C1-C7): Detected as a group due to their proximity
- Thoracic (T1-T12): Individually detectable
- Lumbar (L1-L5): Individually detectable
When specifying vertebrae for detection, you can use the following labels:
C: Represents any cervical vertebra (C1-C7)T1toT12: Individual thoracic vertebraeL1toL5: Individual lumbar vertebrae
Note: For cervical vertebrae (C1-C7), which are located in the neck region, the model detects the general existence of any cervical vertebrae rather than identifying them individually. This is due to their close proximity to each other, which makes individual detection challenging.
Example usage:
torso-mapper --input-dir ./ct_scans --vertebrae T11 --vertebrae T12 --vertebrae L1 --vertebrae L2
This command will look for the presence of any cervical vertebrae, as well as the specific thoracic vertebrae T1 and T12, and the lumbar vertebra L5.
In your Python code, you can specify vertebrae in the same way:
target_vertebrae = ['T11', 'T12', 'L1']
results = analyze_vertebrae(dataloader, model, target_vertebrae, device='cpu')
Remember that when 'C' is specified, the results will indicate the presence or absence of any cervical vertebrae, not individual C1-C7 vertebrae.
API Usage
Here's a detailed example of how to use the torso-mapper API:
from torso_mapper.data import create_folder_ct_dataloader
from torso_mapper.model import TorsoNet
from torso_mapper.analyzer import analyze_vertebrae
# Create a dataloader for your CT scans
dataloader = create_folder_ct_dataloader("path/to/ct_scans", batch_size=4)
# Load the pre-trained model
model = TorsoNet.from_pretrained("marius-urbonas/torso-mapper")
# Specify the vertebrae you want to detect
target_vertebrae = ['T11', 'T12', 'L1']
# Analyze the CT scans
results = analyze_vertebrae(dataloader, model, target_vertebrae, device='cpu')
# Process the results
for scan_id, scan_results in results.items():
print(f"Scan: {scan_id}")
print(f"Contains target vertebrae: {scan_results['contains_target_vertebrae']}")
print("Vertebrae details:")
for vertebra, details in scan_results['vertebrae_details'].items():
print(f" {vertebra}: Present: {details['present']}, Probability: {details['probability']:.2f}")
print()
This script will:
- Load CT scans from a specified directory
- Use the pre-trained TorsoNet model
- Analyze the scans for the presence of T11, T12, and L1 vertebrae
- Print out detailed results for each scan
You can adjust the target_vertebrae list to look for different vertebrae, and modify the processing of results as needed for your specific use case.
API Usage for getting clasification labels
Here's a basic example of how to use torso-mapper:
from torso_mapper.data import create_folder_ct_dataloader
from torso_mapper.model import TorsoNet
from torso_mapper.results import ResultTracker
# Load CT scans from a folder
dataloader = create_folder_ct_dataloader("path/to/ct_scans", batch_size=4)
# Initialize the TorsoNet model
from torso_mapper.model import TorsoNet
model = TorsoNet.from_pretrained("marius-urbonas/torso-mapper")
_ = model.eval()
# Initialize the result tracker
tracker = ResultTracker()
# Process CT scans
for batch, ids in dataloader:
outputs, _ = model(batch)
tracker.update(outputs, ids)
# Get results for a specific scan
scan_result = tracker.get_scan_result_at(0)
labels = scan_result.get_scan_labels()
Dependencies
- PyTorch
- NumPy
- Nibabel
- Matplotlib
Contributing
Interested in contributing? We welcome contributions of all forms. Please check out our contributing guidelines for more information on how to get started.
License
torso-mapper is licensed under the MIT License. See the LICENSE file for more details.
Credits
- Created by Marius Urbonas
- Developed using
cookiecutterand thepy-pkgs-cookiecuttertemplate
Contact
For questions, issues, or suggestions, please open an issue on our GitHub repository or contact the maintainer directly.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torso_mapper-1.0.2.tar.gz.
File metadata
- Download URL: torso_mapper-1.0.2.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.10 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
beaf4648a0e90177d1dd0b4c880e5516396c72836067ae5044fe1f20d4b5965b
|
|
| MD5 |
d2d9e2aea74f71d9befd9b498a4b0a5d
|
|
| BLAKE2b-256 |
3a241df36325de7bb651cc253ba935f04bfacb2b9d1201d97eca7fa6ccb8942c
|
File details
Details for the file torso_mapper-1.0.2-py3-none-any.whl.
File metadata
- Download URL: torso_mapper-1.0.2-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.10 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1562ba970fb58ee4aa98e62c7cf148e6f8d9b5404581212dafd093d754743e83
|
|
| MD5 |
d904555d13acb325147429694033e173
|
|
| BLAKE2b-256 |
ec13db815529bda5eb5eb44a8a61b8be5c41655918652b285ec7542026c5fc8a
|