Skip to main content

A package to predict DICOM image modalities

Project description

DCMP (DICOM Modality Predictor)

DCMP is a Python package that predicts the modality of DICOM (Digital Imaging and Communications in Medicine) images using a pre-trained scikit-learn model.

Features

  • Predict modality for individual DICOM files
  • Process lists of DICOM file paths
  • Analyze entire directories of DICOM files, including nested subdirectories
  • Option to sample a subset of files for faster processing of large datasets
  • Skip specific folders based on keywords
  • Multithreaded processing for improved performance
  • Command-line interface for easy use
  • Comprehensive error handling and reporting

Dependencies

DCMP relies on several Python libraries and system dependencies. Here's how to install them:

System Dependencies

On Debian-based systems (like Ubuntu), you should install the necessary system dependencies for OpenCV with the following commands:

sudo apt update
sudo apt install libopencv-dev python3-opencv

Python Dependencies

The main Python dependencies are:

  • numpy
  • pydicom
  • opencv-python
  • scikit-learn==1.4.0
  • joblib
pip install numpy pydicom opencv-python opencv-contrib-python scikit-learn==1.4.0 joblib

Installation

You can install the DCMP package using pip:

pip install dcmp

Usage

Python API

Predicting a Single File

from dcmp import DicomModalityPredictor

predictor = DicomModalityPredictor()
result = predictor.predict_single_file('/path/to/your/dicom/file.dcm')
print(result)

Predicting a List of Files

from dcmp import DicomModalityPredictor

predictor = DicomModalityPredictor()

dicom_files = [
    '/path/to/file1.dcm',
    '/path/to/file2.dcm',
    '/path/to/file3.dcm',
]

results = predictor.predict_file_list(dicom_files)

for result in results:
    if result['error'] is None:
        print(f"File: {result['filename']}")
        print(f"Predicted Modality: {result['predicted_modality']}")
        print(f"Probability: {result['probability']:.4f}")
    else:
        print(f"Error processing {result['filename']}: {result['error']}")
    print("---")

Processing a single dicom series Directory

from dcmp import DicomModalityPredictor

predictor = DicomModalityPredictor()

results = predictor.predict_folder('/path/to/your/dicom-folder')

for result in results:
    if result['error'] is None:
        print(f"File: {result['filename']}")
        print(f"Predicted Modality: {result['predicted_modality']}")
        print(f"Probability: {result['probability']:.4f}")
    else:
        print(f"Error processing {result['filename']}: {result['error']}")
    print("---")

Processing a BIDS anat Directory

from dcmp import DicomModalityPredictor
from dcmp.utils import process_anat_folders

predictor = DicomModalityPredictor()

base_path = '/path/to/BIDS-data-dcm/anat-folder'
skip_keywords = ['localizer', 'scout', 'symri', 'swi']  # It will skip the folders that include these keywords
sample_size = 20  # Process randomly up to 20 files per folder

results = process_anat_folders(base_path, predictor, sample_size=sample_size, skip_keywords=skip_keywords)

for folder, result in results.items():
    print(f"Folder: {folder}")
    print(f"Predicted Modality: {result['predicted_modality']}")
    print(f"Average Probability: {result['average_probability']:.4f}")
    print(f"Files Processed: {result['files_processed']} / {result['total_files']}")
    print("---")

Command-line Interface

The package also provides a command-line interface for easy use:

dcmp /path/to/dicom/folders --sample 20 --skip localizer scout symri swi --output results.json

Options:

  • --sample: Number of DICOM files to sample from each folder
  • --skip: Keywords to skip folders (can provide multiple)
  • --output: Path to save the results JSON file
  • --model: Path to a custom trained model file (optional)

Customization

Using a Custom Model

You can use your own trained scikit-learn model by providing the path to the model file:

predictor = DicomModalityPredictor(model_path='/path/to/your/custom_model.pkl')

Adjusting Thread Pool Size

The predict_file_list method uses a ThreadPoolExecutor for parallel processing. You can adjust the number of worker threads:

results = predictor.predict_file_list(dicom_files, max_workers=4)

Error Handling

The package includes comprehensive error handling. When processing multiple files, if an error occurs with one file, the package will continue processing the remaining files and include error information in the results.

Contributing

Contributions to the DCMP package are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • This package uses scikit-learn for the underlying machine learning model.
  • The model is trained on KI MRI scans.
  • DICOM file handling is done using the pydicom library.

Support

If you encounter any problems or have any questions, please open an issue on the GitHub repository.

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

dcmp-0.1.2.tar.gz (24.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dcmp-0.1.2-py3-none-any.whl (24.0 MB view details)

Uploaded Python 3

File details

Details for the file dcmp-0.1.2.tar.gz.

File metadata

  • Download URL: dcmp-0.1.2.tar.gz
  • Upload date:
  • Size: 24.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.7

File hashes

Hashes for dcmp-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a1af1d78e492cfaaf1c0fb4ae166bde8f444f17e9aca66f5dc2a22806b3fd736
MD5 c89eb2e222cfc28cf80c28791b3c59a6
BLAKE2b-256 fe2a64bd6a8f15dd929feba941a1883f3ddb5ca0c91f12fb0b411b2d570e8597

See more details on using hashes here.

File details

Details for the file dcmp-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: dcmp-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 24.0 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.7

File hashes

Hashes for dcmp-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7fd32da4ff41ea248dfbcaf822b7c4b69e8810e8a638185582f84dd88f972608
MD5 6af0fb85563da1c76cb6e288ee436d8a
BLAKE2b-256 7abd794def66c85b9ff0f7916929cde7c868dca0b37e52ae7f54efdca8ec66b7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page