Skip to content

Releases: QIB-Sheffield/dbdicom

v0.0.5

08 Apr 09:54
5821f91
Compare
Choose a tag to compare

Full Changelog: https://github.com/QIB-Sheffield/dbdicom/commits/v0.0.5

dbdicom

dbdicom is a Python interface for reading and writing DICOM databases.

Installation

Run pip install dbdicom.

Browsing a DICOM folder

Reading and opening a DICOM folder

Open a DICOM database in a given folder,
read it and print a summary of the content:

from dbdicom import Folder

folder = Folder('C:\\Users\\MyName\\MyData\\DICOMtestData')
folder.open()
folder.print()

The first time the folder is read this will be relatively slow.
This is because each individual DICOM file in the folder
is read and summarised in a table (csv file).
If the folder is reopened again later,
the table can be read directly and opening will be much faster.

Use scan() to force a rereading of the folder. This may
be of use when files have become corrupted,
or have been removed/modified by external applications:

folder.scan()

After making changes to the DICOM data, the folder should be closed
properly so any changes can be either saved or rolled back as needed:

folder.close()

If unsaved changes exist, close() will prompt the user to either save or restore to
the last saved state.

Retrieving objects from the folder

A DICOM database has a hierarchical structure.
The files are instances of a specific DICOM class and correspond to real-world
objects such as images or regions-of-interest. Instances are grouped into a series,
and multiple series are grouped into studies. Typically a study consist of all the data
derived in a single examination of a subject. Studies are grouped into patients,
which correspond to the subjects the study is performed upon.
A patient can be an actual patient, but can also be a healthy volunteer, an animal,
a physical reference object, or a digital reference object.

To return a list of all patients, studies, series or instances in the folder:

instances = folder.instances()
series = folder.series()
studies = folder.studies()
patients = folder.patients()

The same functions can be used to retrieve the children of
a certain parent object. For instance,
to get all studies of a patient:

studies = patient.studies()

Or all series under the first of those studies:

series = studies[0].series()

Or all instances of a study:

instances = study.instances()

And so on for all other levels in the hierarchy.
Individual objects can also be access directly using
indices. For instance to retrieve the first instance in the folder:

instance = folder.instances(0)

These can be chained together for convencience,
e.g. to get all instances instance of series 5 in study 1 of patient 2:

instance = folder.patients(2).studies(1).series(5).instances()

These functions also work to find objects higher up in the hierarchy.
For instance, to find the patient of a given series:

patient = series.patients()

In this case the function will return a single object rather than a list.

Finding DICOM objects in the folder

Each DICOM file has a number of attributes describing the properties
of the object. Examples are PatientName, StudyDate, etc.
A full list of attributes for specific objects can be found here:
https://dicom.innolitics.com/.

Each known attribute is identified most easily by a keyword,
which has a capitalised notation. Objects in the folder
can be can also be listed by searching on any DICOM tag:

instances = folder.instances(PatientName = 'John Dory')

This will only return the instances for patient John Dory.
Objects can also be searched on multiple DICOM tags:

series = folder.instances(
    PatientName = 'John Dory', 
    ReferringPhysicianName = 'Dr. No', 
)

In this case objects are only returned if both conditions are fullfilled.
Any arbitrary number of conditions can be entered, and
higher order objects can be found in the same way:

studies = folder.studies(
    PatientName = 'John Dory', 
    ReferringPhysicianName = 'Dr. No', 
)

TO DO In addition to filtering, the results can also be sorted by attribute:

studies = folder.studies(
    sortby = 'StudyDate', 
    PatientName = 'John Dory', 
)

In this case the resulting studies will appear in the list in order of Study Date.
Sorting can also be done based on two or more attributes:

studies = folder.studies(
    sortby = ['PatientName', 'StudyDate', 'StudyDescription']
)

In this case the result will be a 3-dimensional list.
For instance to access all studies of patient 3 do:

studies[3][:][:]

As an alternative to calling explicit object types,
you can call children() and parent to move through the hierarchy:

studies = patient.children()
patient = studies[0].parent

The same convenience functions are available,
such as using an index or searching by keywords:

studies = patient.children(ReferringPhysicianName = 'Dr. No')
study = patient.children(0)

Moving and removing objects

To remove an object from the folder, call remove() on the object:

study.remove()
instance.remove()

remove() can be called on Patient, Study, Series or Instances.

Moving an object to another parent can be done with move_to()
For instance to move a study from one patient to another:

study = folder.patients(0).studies(0)
new_parent = folder.patients(1)
study.move_to(new_parent)

Objects can also be moved to objects higher up in the hierarchy.
Any missing parents will be automatically created. For instance:

instance = folder.instances(0)
study = folder.studies(1)
instance.move_to(study)

This will move instance from its current parent series to study.
Since no new parent series under study has been provided,
a new series will be created under study and used as a parent for instance.

Copying and creating objects

A DICOM object can be copied by calling copy():

study = folder.patients(0).studies(0)
new_study = study.copy()

This will create a copy of the object in the same parent object,
i.e. study.copy() in the example above has created a new study in patient 0.
This can be used for instance to copy-paste a study from one patient to another:

study = folder.patients(0).studies(0)
new_parent = folder.patients(1)
study.copy().move_to(new_parent)

This is equivalent to using copy_to():

study.copy_to(new_parent)   

To create a new object, call new_child() on the parent:

series = study.new_child()

series will now be a new (empty) series under study.

Export and import

To export an object out of the folder to an external folder,
call export() on any dicom object with the export path as argument:

series.export(path)

If no path is given then the user will be asked to select one.

TO DO Equivalently to import DICOM files from an external folder,
call import() with a list of files:

folder.import(files)

Creating and modifying DICOM files

Reading DICOM attributes

An object's DICOM attributes can be read by using the DICOM keyword of the attribute:

dimensions = [instance.Rows, instance.Columns]

All attributes can also be accessed at series, study, patient or folder level.
In this case they will return a single value taken from their first instance.

rows = folder.patient(0).series(0).Rows

To print the Rows for all instances in the series, iterate over them:

for instance in series.instances():
    print(instance.Rows)

DICOM attributes can also be accessed using the list notation,
using either the keyword as a string or a (group, element) pair.

columns = instance['Columns']
columns = instance[(0x0028, 0x0010)]

The tags can also be accessed as a list, for instance:

dimensions = ['Rows', (0x0028, 0x0010)]
dimensions = instance[dimensions] 

This will return a list with two items. As shown in the example,
the items in the list can be either KeyWord strings or (group, element) pairs.
This also works on higher-level objects:

dimensions = ['Rows', (0x0028, 0x0010)]
dimensions = patient[dimensions] 

As for single KeyWord attributes this will return one list
taken from the first instance of the patient.

Editing attributes

DICOM tags can be modified using the same notations:

instance.EchoTime = 23.0

or also:

instance['EchoTime'] = 23.0

or also:

instance[(0x0018, 0x0081)] = 23.0

Multiple tags can be inserted in the same line:

shape = ['Rows', 'Columns']
instance[shape] = [128, 192]

When setting values in a series, study or patient,
all the instances in the object will be modified.
For instance, to set all the Rows in all instances of a series to 128:

series.Rows = 128

This is shorthand for:

for instance in series.instances():
     instance.Rows = 128

Read and write

By default all changes to a DICOM object are made on disk.
For instance if a DICOM attribute is changed

instance.Rows = 128

The data are read from disk, the change is made, the data are
written to disk again and memory is cleared.
Equally, if a series is copied to another study, all
its instances will be read, any necessary changes made,
and then written to disk and cleared from memory.

For many applications reading and writing from disk is too slow.
For faster access at the cost of some memory usage, ...

Read more