Skip to main content

Retrieve medical images via WADO, MINT, RAD69 and DICOM-QR

Project description

dicomtrolley

CI PyPI PyPI - Python Version Code style: black Checked with mypy

Retrieve medical images via WADO, MINT, RAD69 and DICOM-QR

  • Requires python 3.7, 3.8 or 3.9
  • Uses pydicom and pynetdicom. Images and query results are pydicom.Dataset instances
  • Multi-threaded downloading using requests-futures

A trolley

Installation

pip install dicomtrolley

Usage

Basic example

# Create a logged-in http session
session = VitreaConnection(
    "https://server/login").log_in(user,password,realm)
                           
# Use this session to create a trolley using MINT and WADO
trolley = Trolley(searcher=Mint(session, "https://server/mint"),
                  wado=Wado(session, "https://server/wado"]))

# find some studies (using MINT)
studies = trolley.find_studies(MintQuery(patientName='B*'))  

# download the fist one (using WADO)
trolley.download(studies[0], output_dir='/tmp/trolley')

Finding studies

studies = trolley.find_studies(MintQuery(patientName='B*'))

Query parameters can be found in mint.Query. Valid include fields (which information gets sent back) can be found in fields.py:

studies = trolley.find_studies(
    MintQuery(modalitiesInStudy='CT*', 
              patientSex="F", 
              minStudyDate=datetime(year=2015, month=3, day=1),
              maxStudyDate=datetime(year=2020, month=3, day=1),
              includeFields=['PatientBirthDate', 'SOPClassesInStudy']))

Finding series and instance details

To include series and instance level information as well, use the queryLevel parameter

studies = trolley.find_studies(  # find studies series and instances
    MintQuery(studyInstanceID='B*', 
              queryLevel=QueryLevels.INSTANCE)

a_series = studies.series[0]  # studies now contain series    
an_instance = a_series.instances[0]  # and series contain instances

Downloading data

Any study, series or instance can be downloaded

studies = trolley.find_studies(MintQuery(patientName='B*',
                                         queryLevel=QueryLevels.INSTANCE))

path = '/tmp/trolley'
trolley.download(studies, path)                             # all studies
trolley.download(studies[0]), path                          # a single study
trolley.download(studies[0].series[0], path)                # a single series
trolley.download(studies[0].series[0].instances[:3], path)  # first 3 instances

More control over download: obtain pydicom.Dataset instances directly

studies = trolley.find_studies(              # find study including instances
    Query(PatientID='1234', 
          queryLevel=QueryLevels.INSTANCE)

for ds in trolley.get_dataset(studies):      # obtain Dataset for each instance
    ds.save_as(f'/tmp/{ds.SOPInstanceUID}.dcm')

Multi-threaded downloading

trolley.download(studies, path, 
                 use_async=True,  # enable multi-threaded downloading 
                 max_workers=4)   # optionally set number of concurrent workers
                                  # defaults to None which lets python decide

Using WADO only, without search

from dicomtrolley.wado import Wado
from dicomtrolley.core import InstanceReference

instance = InstanceReference(series_instance_uid='1.2.1', study_instance_uid='1.2.2', sop_instance_uid='1.2.3')

wado = Wado(session, wado_url)
for ds in wado.datasets([instance]):
  ds.save_as(f'/tmp/{ds.SOPInstanceUID}.dcm')

DICOM-QR

Trolley can use DICOM-QR instead of MINT as a search method. See dicom_qr.DICOMQuery for query details.

dicom_qr = DICOMQR(host,port,aet,aec)
trolley = Trolley(searcher=dicom_qr, downloader=wado)

# Finding is similar to MINT, but a DICOMQuery is used instead
trolley.find_studies(  
    query=DICOMQuery(PatientName="BAL*",   
                     minStudyDate=datetime(year=2015, month=3, day=1),
                     maxStudyDate=datetime(year=2015, month=4, day=1),
                     includeFields=["PatientBirthDate", "SOPClassesInStudy"],
                     QueryRetrieveLevel=QueryRetrieveLevels.STUDY)) 

RAD69

The RAD69 protocol is an alternative to wado for downloading DICOM images.

dicom_qr = DICOMQR(host,port,aet,aec)
trolley = Trolley(searcher=dicom_qr, 
                  downloader=Rad69(session=session,
                                   url="https://server/rad69"))

studies = trolley.find_studies(PatientName="AB*",)
trolley.download(studies[0], path)  # rad69 download works exactly like wado 
trolley.download(studies[1], path,  
                 use_async=True)    # multi-threaded download is supported

Download

By default, trolley writes downloads to disk as StudyID/SeriesID/InstanceID, sorting files into separate study and series folders. You can change this by passing a DICOMDiskStorage instance to trolley:

from dicomtrolley.storage import FlatStorageDir

#  Creates no sub-folders, just write to single flat file
storage = FlatStorageDir(path=tmpdir)
trolley = Trolley(searcher=mint, downloader=wado,
                  storage=storage)

You can create your own custom storage method by subclassing storage.DICOMDiskStorage:

from dicomtrolley.storage import DICOMDiskStorage

class MyStorage(DICOMDiskStorage):
  """Saves to unique uid filename"""
  def save(self, dataset, path):    
    dataset.save_as(Path(path) / uuid.uuid4())

trolley = Trolley(searcher=mint, downloader=wado,
                  storage=MyStorage())

Examples

Alternatives

  • dicomweb-client - Active library supporting QIDO-RS, WADO-RS and STOW-RS.
  • pynetdicom - dicomtrolley's DICOM-QR support is based on pynetdicom. Pynetdicom supports a broad range of DICOM networking interactions and can be used as a stand alone application.

Caveats

Dicomtrolley has been developed for and tested on a Vitrea Connection 8.2.0.1 system. This claims to be consistent with WADO and MINT 1.2 interfaces, but does not implement all parts of these standards.

Certain query parameter values and restraints might be specific to Vitrea Connection 8.2.0.1. For example, the exact list of DICOM elements that can be returned from a query might be different for different servers.

Contributing

You can contribute in different ways

Report bugs

Report bugs at https://github.com/sjoerdk/clockify_api_client/issues.

Contribute code

Get the code

Fork this repo, create a feature branch

Set up environment

dicomtrolley uses poetry for dependency and package management

  • Install poetry (see poetry docs)
  • Create a virtual env. Go to the folder where cloned dicomtrolley and use
    poetry install 
    
  • Install pre-commit hooks.
    pre-commit install
    

Add your code

Make your code contributions. Make sure document and add tests for new features. To automatically publish to pypi, increment the version number. See below.

Lint your code

Publish

Create a pull request

Incrementing the version number

A merged pull request will only be published to pypi if it has a new version number. To bump dicomtrolley's version, do the following.

  • dicomtrolley uses semantic versioning Check whether your addition is a PATCH, MINOR or MAJOR version.

  • Manually increment the version number:

    • pyproject.toml -> version = "0.1.2"
  • Add a brief description of your updates new version to HISTORY.md

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

dicomtrolley-1.2.0.tar.gz (32.8 kB view details)

Uploaded Source

Built Distribution

dicomtrolley-1.2.0-py3-none-any.whl (35.2 kB view details)

Uploaded Python 3

File details

Details for the file dicomtrolley-1.2.0.tar.gz.

File metadata

  • Download URL: dicomtrolley-1.2.0.tar.gz
  • Upload date:
  • Size: 32.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.7.13 Linux/5.13.0-1029-azure

File hashes

Hashes for dicomtrolley-1.2.0.tar.gz
Algorithm Hash digest
SHA256 a20201c396113efd4fd96ab91b6cc4ab9da02c9123b13ae80e050aeaad3a6592
MD5 3c8463d01bc962dbc13588f008ed345a
BLAKE2b-256 0475b72c4871afc80e15171d9fb3e64ac13cdc65c23d6c6fd0dbbd7c1ba87ca3

See more details on using hashes here.

File details

Details for the file dicomtrolley-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: dicomtrolley-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 35.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.7.13 Linux/5.13.0-1029-azure

File hashes

Hashes for dicomtrolley-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d6cc4c9454c84bf9e98bc3c253d7c90d112ed04a2afc21933fc22242b56504e
MD5 260841baf168b169de04b2d0f095c923
BLAKE2b-256 2979acda23057b54e360f1f4099fa10e1fc24283c86415dcc6c65b83e1f6b8df

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