Dictionary in pydicom

86 views
Skip to first unread message

Dumitru Georgiana

unread,
May 2, 2020, 4:58:41 PM5/2/20
to pydicom
I would like to create a DICOM worklist with pydicom. The worklist contains a sequence called ScheduledProcedureStepSequence which again, contains one or multiple dictionaries (DICOM tag: fffe,e000 --> "Item"). While adding simple tags such as "PatientName" or "PatientID" is very easy to do, i can figure out how to add a dictionary (DICOM "Item") to the ScheduledProcedureStepSequence. See below text representation of a modality worklist which i would like to create with pydicom.


# Dicom-Meta-Information-Header
# Used TransferSyntax: Little Endian Explicit
(0002,0000) UL 200                                      #   4, 1 FileMetaInformationGroupLength
(0002,0001) OB 00\01                                    #   2, 1 FileMetaInformationVersion
(0002,0002) UI [1.2.276.0.7230010.3.1.0.1]              #  26, 1 MediaStorageSOPClassUID
(0002,0003) UI [1.2.276.0.7230010.3.1.4.8323328.131065.1587929664.399909] #  56, 1 MediaStorageSOPInstanceUID
(0002,0010) UI =LittleEndianExplicit                    #  20, 1 TransferSyntaxUID
(0002,0012) UI [1.2.276.0.7230010.3.0.3.6.4]            #  28, 1 ImplementationClassUID
(0002,0013) SH [OFFIS_DCMTK_364]                        #  16, 1 ImplementationVersionName

# Dicom-Data-Set
# Used TransferSyntax: Little Endian Explicit
(0008,0050) SH [80372376]                               #   8, 1 AccessionNumber
(0010,0010) PN [Patient B]                              #  10, 1 PatientName
(0010,0020) LO [11788770005213]                         #  14, 1 PatientID
(0020,000d) UI [1.2.276.0.7230010.3.1.2.8323328.135028.1588014697.167604] #  56, 1 StudyInstanceUID
(0040,0100) SQ (Sequence with explicit length #=1)      # 162, 1 ScheduledProcedureStepSequence
 
(fffe,e000) na (Item with explicit length #=7)          # 154, 1 Item
   
(0008,0060) CS [US]                                     #   2, 1 Modality
   
(0040,0001) AE [PhillipsUS01]                           #  12, 1 ScheduledStationAETitle
   
(0040,0002) DA [20200427]                               #   8, 1 ScheduledProcedureStepStartDate
   
(0040,0003) TM [100000.001]                             #  10, 1 ScheduledProcedureStepStartTime
   
(0040,0006) PN [Max Messermann]                         #  14, 1 ScheduledPerformingPhysicianName
   
(0040,0007) LO [try not to melt this patients brain for once] #  44, 1 ScheduledProcedureStepDescription
   
(0040,0009) SH [80871868]                               #   8, 1 ScheduledProcedureStepID
 
(fffe,e00d) na (ItemDelimitationItem for re-encoding)   #   0, 0 ItemDelimitationItem
(fffe,e0dd) na (SequenceDelimitationItem for re-encod.) #   0, 0 SequenceDelimitationItem
(0040,1001) SH [91249028]                               #   8, 1 RequestedProcedureID


Sample Python code:

import pydicom
from pydicom.dataset import Dataset, FileDataset, DataElement, datadict

filename_little_endian
= "firstDicomFile"
file_meta
= Dataset()
file_meta
.MediaStorageSOPClassUID = '1.2.276.0.7230010.3.1.0.1'
file_meta
.ImplementationClassUID = "1.2.276.0.7230010.3.0.3.6.4"

ds
= FileDataset(filename_little_endian, {}, file_meta=file_meta, preamble=b"\0" * 128)

ds
.PatientName = "Max"
ds
.PatientID = "98273459872354"
ds.ScheduledProcedureStepSequence = []

ds2 = Dataset()
ds2
.Item = {}
ds2
.Item.update({"Modality": "Philips"})
ds
.ScheduledProcedureStepSequence = [ds2]
print(ds)


The resulting representation does not seem to be correct. How do i correctly handle dictionaries in this case?



Darcy Mason

unread,
May 3, 2020, 11:03:11 AM5/3/20
to pydicom
If I understand correctly, you are just trying to add items (datasets) to a Sequence.  You do not need to work with dictionaries directly in that case, just add pydicom Datasets to a pydicom Sequence (which is basically a python `list`) and pydicom takes care of the rest.


Also, if you have an example file already that is similar to what you want to produce, use the codify script to produce python code that you can then modify to customize as needed:
https://pydicom.github.io/pydicom/stable/old/writing_files.html#using-the-codify-script. The codify script nicely takes care of creating sequence items and adding them, giving you another source of example code to follow.

-Darcy
Reply all
Reply to author
Forward
0 new messages