Skip to main content

Generate pydicom datasets and data elements for use in testing

Project description

dicomgenerator

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

Generate pydicom datasets and data elements for use in testing.

  • Free software: MIT license
  • Status: Alpha. Tests run but there are loose ends

Features

  • Extends factory-boy factories to produce pydicom Datasets and DicomElements
  • Generate valid DICOM values for person name, time, date, and UID
  • Create json-based editable templates from any dicom file
  • quick_dataset(): single-line pydicom dataset init

Installation

Install with pip::

pip install dicomgenerator

Usage

Quick dataset

I have found this quite useful in testing:

    from dicomgenerator.generators import quick_dataset
    ds = quick_dataset(PatientName='Jane', StudyDescription='Test')

    # >>> ds.PatientName -> 'Jane'     
    # >>> ds.StudyDescription -> 'Test'

Generating a dataset

Generate a realistic CT dataset

    from dicomgenerator.templates import CTDatasetFactory

    # Generate from template
    >>> CTDatasetFactory().PatientName -> 'van Haarlem^Anouk'  #  generated random name
    >>> CTDatasetFactory().PatientName -> 'Loreal^Casper'      #  generated random name

    # Overwrite arbitrary DICOM elements
    ds.CTDatasetFactory(PatientSex='M', PatientName='Smith^Harry')
    >>> ds.PatientName -> 'Smith^Harry'
    >>> ds.PatientSex  -> 'M'

    # generated UIDs and dates are valid DICOM
    >>> CTDatasetFactory().StudyTime        -> '130624.929'
    >>> CTDatasetFactory().StudyDate        -> '20110508'
    >>> CTDatasetFactory().StudyInstanceUID -> '1.2.826.0.1.3680'

Generating a data element

    # import
    from dicomgenerator.templates import DataElementFactory

    # Creating a DICOM data element by name will give a realistic value and correct VR
    >>> DataElementFactory(tag='PatientName').value -> "van Ooyen^Fiene"
    >>> DataElementFactory(tag='PatientName').VR -> 'PN'

    # You can also give DICOM tags as hex
    >>> DataElementFactory(tag=0x00100010).value -> "Weil^Jack"

    # Dates, times and UIDs all work.
    >>> DataElementFactory(tag="AcquisitionTime").value   -> '184146.928'
    >>> DataElementFactory(tag="PatientBirthDate").value  -> '20120511'
    >>> DataElementFactory(tag="SeriesInstanceUID").value -> '1.2.826.0.1.3680'

In reproducible tests

You can set the random seed in factory-boy like this:

    from factory import random

    def test_one:
        """The random patient name in this test will always be the same"""
        random.reseed_random('any string you want')
        assert element = DataElementFactory(tag='PatientName').value == "van Ooyen^Fiene"

Command Line Interface

You can convert a DICOM file to AnnotatedDataset via the commandline. by default this will write an annotated dataset to the same folder, appending_template.json

$ dicomgen convert to-json /tmp/dicom_file
$ ls
dicom_file  dicom_file_template.json

For options, use

$ dicomgen convert to-json --help

Credits

This package was originally created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

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

dicomgenerator-0.10.1.tar.gz (98.4 kB view details)

Uploaded Source

Built Distribution

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

dicomgenerator-0.10.1-py3-none-any.whl (101.7 kB view details)

Uploaded Python 3

File details

Details for the file dicomgenerator-0.10.1.tar.gz.

File metadata

  • Download URL: dicomgenerator-0.10.1.tar.gz
  • Upload date:
  • Size: 98.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dicomgenerator-0.10.1.tar.gz
Algorithm Hash digest
SHA256 533b7ee2135442ce5b8d9e929e69304abdb6e23a50d201ed8862fb982d7e8bdc
MD5 26442ba8e9617598fab3566b5c329466
BLAKE2b-256 e9b13fd213d7998e88e2b729d5f39caade937ece1de7fd27543dd08154603b37

See more details on using hashes here.

File details

Details for the file dicomgenerator-0.10.1-py3-none-any.whl.

File metadata

  • Download URL: dicomgenerator-0.10.1-py3-none-any.whl
  • Upload date:
  • Size: 101.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dicomgenerator-0.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2517414a5eb1ea2c95371686ae3b3e169d7c429ce12b12415fead838202f66bb
MD5 f7a665ccd282869a1374567085bc3115
BLAKE2b-256 4fba5d6f2f3e57524e4f5483a4bf9817c395541ceb17aba9d7e819cac34ab67d

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