Porting to pydicom 1.0
Here you'll find relevant info about porting your code to pydicom v1.0. Because of the changes to the code base, usage in your project may need tweaking.
In pydicom v1.0, the module name changed from dicom
to pydicom
. Thus, imports should be changed from:
import dicom
to:
import pydicom
As a shortcut, you can change the local name like so: import pydicom as dicom
, but the new import pydicom
is encouraged.
In the most common scenario, a DICOM file is opened by using: pydicom.dcmread()
and saved by pydicom.dcmwrite()
. The old names pydicom.read_file()
and pydicom.write_file()
will still work, but may be deprecated in future. A common file-reading script may look like this:
import pydicom
ds = pydicom.dcmread("my_file.dcm")
# work with the data
pydicom.dcmwrite("altered_file.dcm", ds)
The dataset method save_as()
can also be used instread of dcmwrite()
:
ds.save_as("altered_again.dcm")
pydicom 1.0 has a few new features you may find useful.
v1.0 has an is_dicom()
function, for detecting if a given file is a valid DICOM file:
from pydicom.misc import is_dicom
if is_dicom("questionable_file_with_dcm_ext.dcm"):
print("It is indeed DICOM!")
else:
print("It's probably not DICOM")
DICOM datasets can also now be used with context management. This may be useful, e.g., when iterating over numerous files or when you only want a small piece of information from the DICOM file without hogging memory:
import pydicom
# get small pieces of information from several DICOM files
dcm_1 = "CT_1.dcm"
with pydicom.dcmread(dcm_1) as ds:
print("Patient Name: {}".format(ds.PatientName))
print("CT KVP: {}".format(ds.KVP))
# the CT dataset is destroyed, freeing memory
dcm_2 = "RTPLAN.dcm"
with pydicom.dcmread(dcm_1) as ds:
print("Number of Beams: {}".format(len(ds.FractionGroupSequence[0].ReferencedBeamSequence)))
dcm_3 = "RTSTRUCT.dcm"
with pydicom.dcmread(dcm_1) as ds:
print("Name of first ROI: {}".format(ds.StructureSetROISequence[0].ROIName))
TODO: VR items??
pydicom 1.0 supports python 2.7, and 3.x. Python 2.6 is also supported, although to run pydicom tests you'll need pytest.
Documentation is now standardized, using Numpy style docstrings.
Is a docstring not in Numpy format? Fix it and send a pull request!