Skip to main content

No project description provided

Project description

dicomselect: DICOM database and conversion software

dicomselect is a Python tool that simplifies the process of creating SQLite databases from directories containing .dcm files or from .zip archives containing .dcm files at the root of the zip file. Once the database is created, you can easily perform SQL-like queries on the data directly within Python. Additionally, dicomselect allows you to convert query results into various file formats supported by SimpleITK, providing flexibility in working with your DICOM data.

Installation

Python 3.10 or higher. You can install this project using pip. If you haven't already, it's recommended to create a virtual environment to isolate project dependencies.

pip install dicomselect

Example

Clone this repo, install dicomselect, then run this example in the repo.

from dicomselect import Database
from pathlib import Path

db_path = Path('tests/output/example.db')
db_path.parent.mkdir(exist_ok=True)

# initialize the Database object with a path to the to-be-created SQLite database file
db = Database(db_path)

# create the .db file, using test data as the input directory.
db.create('tests/input/ProstateX', max_workers=4)

with (db as query):
    # we only want to convert images with patient_id "ProstateX-0000" and image_direction "transverse"
    query_0000 = query.where('patient_id', '=', 'ProstateX-0000'
                             ).where('image_direction', '=', 'transverse')

    # print out a detailed extraction of our query
    print(query_0000.info())

# initialize the Plan object, with a template of DICOM headers for our conversion
# (note: dcm to dcm conversion is possible, if you only need restructuring of your data)
plan = db.plan('{patient_id}/prostateX_{series_description}_{instance_creation_time}', query_0000)

# ensure these properties are set
plan.target_dir = 'tests/output/example'
plan.extension = '.mha'
plan.max_workers = 4

# print out a detailed structure of our intended conversion
print(plan.to_string())

plan.execute()

Check out the results in tests/output/example

===========================================================================

Create a new database

from pathlib import Path
from dicomselect.database import Database

db_path = Path("/path/to/dicomselect_archive.db")
archive_path = Path("/path/to/archive")
db_path.parent.mkdir(parents=True, exist_ok=True)
db = Database(db_path)
db.create(archive_path, max_workers=4)

Select scans

  1. Simple matching of values
from dicomselect.database import Database

mapping = {
    "t2w": {
        "SeriesDescription": [
            "t2_tse_tra_snel_bij bewogen t2 tra",
            "t2_tse_tra",        
            "t2_tse_tra_prostate",
            "t2_tse_tra_snel",
            "t2_tse_tra_Grappa3"
        ]
    },
}

db_path = Path("/path/to/dicomselect_archive.db")
db = Database(db_path)
cursor = db.open()
query = cursor.where("series_description", "in", mapping["t2w"]["SeriesDescription"])
print(query)
db.close()
  1. Pattern matching and combining queries
from dicomselect.database import Database

mapping = {
    "hbv": {
        "SeriesDescription": [
            "ep2d_diff_tra%CALC_BVAL",
            "diffusie-3Scan-4bval_fsCALC_BVAL"
        ],
        "ImageType": [
            r"DERIVED\PRIMARY\DIFFUSION\CALC_BVALUE\TRACEW\DIS2D\DFC",
            r"DERIVED\PRIMARY\DIFFUSION\CALC_BVALUE\TRACEW\DIS2D",
            r"DERIVED\PRIMARY\DIFFUSION\CALC_BVALUE\TRACEW\ND\DFC",
            r"DERIVED\PRIMARY\DIFFUSION\CALC_BVALUE\TRACEW\NORM\DIS2D",
        ]
    }
}

db_path = Path("/path/to/dicomselect_archive.db")
db = Database(db_path)
cursor = db.open()
query1 = cursor.where("series_description", "LIKE", mapping["hbv"]["SeriesDescription"])
query2 = cursor.where("image_type", "LIKE", mapping["hbv"]["ImageType"])
query = query1.union(query2)
print(query)
db.close()

Show info

# print a default overview of the query result
print(query)

# for more fine-grained control of the reporting, use the Info object
# print a summary of series_description given the query
print(query.info().include("series_description"))

# print a summary of everything but series_description given the query
# note: some recommended columns are excluded, such as columns referring to some UID.
print(query.info().exclude("series_description", recommended=True))

Convert

from dicomselect.database import Database

db_path = Path("/path/to/dicomselect_archive.db")
db = Database(db_path)
plan = db.plan("{patient_id}/{series_description}_{patients_age}", query)
plan.target_dir = "/path/to/target_dir"
plan.extension = ".mha"
print(plan)
plan.execute(max_workers=4)

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

dicomselect-0.8.0.tar.gz (62.9 kB view details)

Uploaded Source

Built Distribution

dicomselect-0.8.0-py3-none-any.whl (61.4 kB view details)

Uploaded Python 3

File details

Details for the file dicomselect-0.8.0.tar.gz.

File metadata

  • Download URL: dicomselect-0.8.0.tar.gz
  • Upload date:
  • Size: 62.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for dicomselect-0.8.0.tar.gz
Algorithm Hash digest
SHA256 5c3cdbac2d3ae3637a062806673c790f243e5c5d420523571cf7f1f6de3e9cff
MD5 39c245a4661d12add2304f01c9ebf585
BLAKE2b-256 53b1efc189c62fe29e5e6d18c03b328f2c1a2146170c58d64af87541483f586d

See more details on using hashes here.

File details

Details for the file dicomselect-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: dicomselect-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 61.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for dicomselect-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2255f7b7e8736045124f63f5cddcc79f783a3e62c5bfcbc1c0cb4b8b8952e1d3
MD5 86e9fdbafb9487c3d083077b134571ea
BLAKE2b-256 6dd9f59ff60096509570487d7d7e7a11d8902c606b0d0928345b3c76639dc911

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