Releases: QIB-Sheffield/dbdicom
v0.2.5
v0.2.4
v0.2.3
Summary
Collecting a series up updates that were made during application in iBEAt and other projects
What's Changed
- test synch small changes by @plaresmedima in #3
- Update README by @plaresmedima in #4
- bug fixes in set attributes and db.copy by @plaresmedima in #5
- improved package management by @plaresmedima in #8
- update for distribution v 0.0.9 by @plaresmedima in #9
- moved to src structure by @plaresmedima in #10
- Restructure by @plaresmedima in #14
- Update README.md by @plaresmedima in #16
- Full code restructure, extended functionality and small tweaks in syntax by @plaresmedima in #17
- Sphinx docs by @EbonyGunwhy in #18
- Docs test by @EbonyGunwhy in #20
- Test in wezel by @plaresmedima in #22
- New version 0.1.2 by @plaresmedima in #23
- split out optional dependencies by @plaresmedima in #25
- v0.1.3 by @plaresmedima in #26
- Update to docs by @plaresmedima in #29
- Dev pytest - Github actions configuration with CodeCov by @EveLMedPhys in #27
- Bug fix multiframe v0.1.4 by @plaresmedima in #30
- Adding gh-action for continuous integration of Sphinx docs by @EbonyGunwhy in #31
- Development - testing documentation by @plaresmedima in #33
- Development by @EbonyGunwhy in #34
- Development include continuous integration of testing and documentation by @plaresmedima in #35
- Merge pull request #35 from QIB-Sheffield/development by @plaresmedima in #36
- Merge pull request #36 from QIB-Sheffield/main by @plaresmedima in #37
- Development by @plaresmedima in #38
- Update main for new version by @plaresmedima in #39
- added dcm4che lib by @plaresmedima in #40
- test CI by @plaresmedima in #41
- text CI 2 by @plaresmedima in #42
- test CI 3 by @plaresmedima in #43
- debug CI 4 by @plaresmedima in #44
- debug CI 5 by @plaresmedima in #45
- debug CI 6 by @plaresmedima in #46
- Dev pytest - added Java to pytest github actions by @EveLMedPhys in #47
- update to test java integration in actions by @plaresmedima in #48
- moved data to test folder & fixed dcm4che issues by @plaresmedima in #49
- moved docs requirements back into requirements.txt by @plaresmedima in #50
- Development version 0.1.8 by @plaresmedima in #52
- Development v 0.1.8 by @plaresmedima in #53
- Separate main and docs requirements to work with GH action by @EbonyGunwhy in #51
- bug correct in scipy module by @plaresmedima in #54
- Development by @plaresmedima in #55
- added wrappers by @plaresmedima in #56
- bug correct in affine_matrix by @plaresmedima in #58
- added Leeds test data file by @plaresmedima in #59
- Development by @KanishkaS in #60
- improved export dcm, png, csv, nii by @plaresmedima in #61
- nifti / dicom import export added by @plaresmedima in #74
- fix: install wrapper dependencies with test automation by @EbonyGunwhy in #73
- import/export + k-means wrapper by @plaresmedima in #75
- disabled tests of new classes by @plaresmedima in #76
- removed new classes in development by @plaresmedima in #77
- fixed image calculator bugs by @plaresmedima in #78
- Fixed bug in map mask & added skimage wrappers by @plaresmedima in #79
- added mdreg wrapper + skimage morphology by @plaresmedima in #80
- Dev pytest - updated github actions to run tests on macOS and ubuntu linux by @EveLMedPhys in #81
- changed emf2sf file with mac permissions set properly by @plaresmedima in #82
- added sequential k-means + bug fix in image calculator by @plaresmedima in #83
- added cval to map_to by @plaresmedima in #84
- mdreg wrapper added, cval added to elastix by @KanishkaS in #85
- remove mdreg wrappers, added skimage and numpy wrappers by @plaresmedima in #86
- fix: correct typo in numpy.maximum_intensity_projection by @EbonyGunwhy in #87
- additional radiomics-type wrappers for scipy and skimage: mask statis… by @plaresmedima in #88
- Added split_by and move to status update by @plaresmedima in #89
- import gif, date and time support, slice spacing bug fixed, ROI curve… by @plaresmedima in #90
- removed restrictions on skimage and opencv in requirements by @plaresmedima in #92
- Fix: zero-fill TM strings to avoid error by @EbonyGunwhy in #91
- Extended wrappers with new functionality by @plaresmedima in #94
- update naming dicom export by @plaresmedima in #95
- Fixed bug in read array + added plugin functionality by @plaresmedima in #96
- bug fix write pixel array by @plaresmedima in #97
- coregistration functionality WIP by @plaresmedima in #98
- bug fix by @plaresmedima in #99
- Added derived SliceLocation option by @plaresmedima in #100
- reverse dataset changes by @plaresmedima in #101
- extracted helper function for shape features by @plaresmedima in #103
- Bug fixes in dicom reading of non-existing tags by @plaresmedima in #104
- rename align to vreg by @plaresmedima in #106
- docs: switch ReadTheDocs to PyData theme by @EbonyGunwhy in #102
- created wrapper for vreg by @plaresmedima in #107
- Test - added summary in getting started by @plaresmedima in #108
- populated user guide by @plaresmedima in #109
- index fix by @plaresmedima in #110
- test title by @plaresmedima in #111
- deleted redundant headings by @plaresmedima in #112
- index files by @plaresmedima in #113
- test API reference by @plaresmedima in #114
- core API by @plaresmedima in #115
- simplify API structure by @plaresmedima in #116
- Update core.rst by @plaresmedima in #117
- testing API by @plaresmedima in #118
- testing by @plaresmedima in #119
- setup of API structure by @plaresmedima in #120
- Update creation.rst by @plaresmedima in #121
- restructure source by @plaresmedima in #122
- Update requirements.txt by @plaresmedima in https://github.com/QIB-Sheffield/d...
v0.0.5
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, ...