django_dicom.data_import package

Module contents

Provides classes that supervise data import. These classes manage the creation and association of DICOM entities (see here and here for more information).

Submodules

django_dicom.data_import.import_image module

class django_dicom.data_import.import_image.ImportImage(dcm: _io.BufferedReader)

Bases: object

A class to handle importing new DICOM files to the database. Takes care of maintaining the Series, Study, and Patient relations with the created Image.

create_image() → django_dicom.models.image.Image

Stores the DICOM file locally and creates an Image instance from it without saving it (allowing for the fields to be updated from the header beforehand).

Returns

The created Image for the given file.

Return type

Image

generate_entities_and_relationships() → None

Execute the generation and association of the new image’s DICOM entities.

get_entity_uid_from_header(Entity: django_dicom.models.dicom_entity.DicomEntity) → str

Returns the UID of the given entity from the DICOM header information.

Parameters

Entity (DicomEntity) – One of the DICOM entities (Image, Series, Study, and Patient).

Returns

The UID value for the given entity.

Return type

str

get_image_destination() → str

Returns the default relative path for this image under MEDIA_ROOT. TODO: Add a way for the user to configure this.

Returns

Default relative path for this image.

Return type

str

get_or_create_entity(Entity: django_dicom.models.dicom_entity.DicomEntity, save: bool = True) → tuple

Gets or creates an instance of the given django_dicom.DicomEntity using its UID. The save parameter is mostly meant to help with testing.

Parameters
  • Entity (DicomEntity) – One of the DICOM entities (Image, Series, Study, and Patient).

  • save (bool) – Whether to save the instance to the database if it is created (default to True, which will call the save() method).

Returns

(dicom_entity, created)

Return type

tuple

handle_integrity_error() → tuple

If an IntegrityError is raised during generation of the DICOM entities, delete the temporary file. An InegrityError should indicate the image already exists in the database, so the method also tries to return the existing Image instance.

Returns

( existing_image , created )

Return type

tuple

move_image_to_destination() → str

Moves the created image to its default location under MEDIA_ROOT.

Returns

Full path of the new location.

Return type

str

run() → tuple

Adds the image to the database and generates its associated entities as an atomic transaction. If the transaction fails, calls handle_integrity_error() This assumes an existing image and tries to return it.

Returns

( image_instance, created )

Return type

tuple

store_file() → str

Stores the DICOM file in a temporary location under MEDIA_ROOT using Django’s default_storage.

Returns

The name of the temporary file created.

Return type

str

django_dicom.data_import.local_import module

class django_dicom.data_import.local_import.LocalImport(path: str)

Bases: object

This class handles importing data from a local directory. Any .dcm files under the directory tree will be imported using ImportImage.

TODO: This should be made into a custom django-admin command: https://docs.djangoproject.com/en/2.2/howto/custom-management-commands/

import_dcm_files(verbose: bool = True) → None

Creates Image instances for each “.dcm” file under the given directory tree. Prints an iterations counter and reports the number of instances added in the end.

Parameters

verbose (bool, optional) – Shows an iterations/second meter (default to True).

classmethod import_local_dcm(path: str) → django_dicom.models.image.Image

Reads the local DICOM image into an io.BufferedReader and uses ImportImage to create an Image from it.

Parameters

path (str) – The local path of the DICOM image.

Returns

The resulting Image instance.

Return type

Image

classmethod import_local_zip_archive(path: str, verbose: bool = True) → None

Iterates over the files within a ZIP archive and imports any “.dcm” files.

Parameters
  • path (str) – Local ZIP archive path.

  • verbose (bool, optional) – Show a progressbar (default to True).

import_zip_archives(verbose: bool = True) → None

Finds ZIP archives under the current directory tree and imports any DICOM data (.dcm files) found within them.

Parameters

verbose (bool, optional) – Shows a progressbar (default to True).

path_generator(extension: str = '') → str

Generates paths from the given directory tree.

Parameters

extension (str) – A file extension to filter the generated files with.

Returns

File path.

Return type

str

run(import_zip: bool = True, verbose: bool = True) → None

Imports any DICOM data (.dcm files) found under the given path.

Parameters
  • import_zip (bool, optional) – Find and import data from ZIP archives (the default is True, which will import any ZIP archived DICOM images).

  • verbose (bool, optional) – Shows a progressbar (default to True).