My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
WorkingWithSequences  
How to read or add sequences
Updated Oct 19, 2011 by darcymason@gmail.com

Introduction

Sequences are part of the DICOM standard, but they do complicate the structure of the data. Pydicom tries to make it as easy as possible to work with sequences, but you will still need to have a good understanding of the DICOM structure to navigate them. The PydicomUserGuide wiki page has an object model at the top which outlines the structure of DICOM objects. This is the way DICOM itself lays them out; pydicom has just translated the DICOM model directly into objects in python.

Reading Sequence data

This was covered in the PydicomUserGuide. For now, please refer to that page ... more details to be added here at some point.

One note: the use of the "normal English plural" will be phased out, in favor of adopting the new official DICOM keywords. The old style will be deprecated in pydicom 0.9.6 or 0.9.7, and will be removed sometime after version 1.0.

Creating a New Sequence

When it comes to creating sequences, there is one key thing to remember:


A sequence is a list of datasets.

I find it easiest to start at the bottom, so to speak, and create the datasets first, then make a list (Sequence) out of them, then assign that Sequence to the parent dataset.

Here is some code to show a brief example, adding a block sequence (with two items) to the first beam of an rtplan dataset 'plan_ds':

>>> from dicom.sequence import Sequence 
>>> from dicom.dataset import Dataset 

>>> block_ds1 = Dataset() 
>>> block_ds1.BlockType = "APERTURE" 
>>> block_ds1.BlockName = "Block1" 

>>> block_ds2 = Dataset() 
>>> block_ds2.BlockType = "APERTURE" 
>>> block_ds2.BlockName = "Block2" 

>>> plan_ds.Beams[0].Blocks = Sequence([block_ds1, block_ds2]) 
>>> plan_ds.Beams[0].NumberofBlocks = 2 

(Note that pydicom does not check that the datasets have all the correct items in them -- that's up to you. Likewise for related data elements, like NumberofBlocks above).

In the above code, plan_ds.Beams is a Sequence itself. But getting item 0 returns a dataset (a sequence is a list of datasets!). So we can add a new data element to that dataset in the usual way, by referring to it by name ('Blocks'). In this case the data element happens to be a Sequence type.

Note the square brackets in the argument to Sequence in the last line, which creates the python list which is passed to the Sequence to initialize it. You could, of course, have created any python list with general python code, and then passed that list by name to Sequence() instead.

Once the Sequence is initialized, it can still be worked with, just like any python list:

>>> beam0 = plan_ds.Beams[0]   # just for less typing
>>> print len(beam0.Blocks)
2
>>> block_ds3 = ... # new Dataset with elements assigned as above
>>> beam0.Blocks.append(block_ds3) # don't forget to update Number of Blocks data element...

Deleting a sequence item

Deleting a sequence item works the same as with any python list, using python's built-in del statement:

>>> del plan_ds.Beams[0].Blocks[1]

Sign in to add a comment
Powered by Google Project Hosting