Adding a Block Sequence

740 views
Skip to first unread message

Robert Ross

unread,
Oct 18, 2011, 8:14:59 AM10/18/11
to pydicom
Dear List

I'm trying to add a block sequence to RT Plan objects, using Python2.6
and Pydicom. The objective is to add a reticle to the plans, which
must be done as a block sequence. I can add the individual data
elements to the plan, but I can't add them to the block sequence.
When I try to add a block sequence using:

plan.Beams[i].AddNew(0x30a00f4, 'SQ','')

the Block is added, but its type is String. I have tried creating a
sequence of all the dataelements I require and tried making the Block
equal to the sequence. I've also tried with datasets. These appear
to work when calling one of the dataelements in the block sequence,
eg:

>>> plan.Beams[0].Blocks[0]
(300a, 00e1) Material ID SH: 'BL1'

but I can't print the whole plan information (AttributeError:
'DataElement' object has no attribute '_PrettyStr') or save the plan
object (AttributeError: 'DataElement' object has no attribute 'keys')
by doing this.

I've seen what I want to achieve done in Visual Basic, using:

beamSequences(I).Attributes.Add &H300A, &HF4,
New DicomDataSets
Set BlockSequences =
beamSequences(I).Attributes.Item(&H300A, &HF4).Value
BlockSequences.AddNew

but I don't know how to implement this in Python. Can anyone point me
in the correct direction?

Kind regards
Robert

Full Code:

import dicom
from dicom.sequence import Sequence
from dicom.dataelem import DataElement
from dicom.dataset import Dataset

plan = dicom.ReadFile("C:\\dcm\\dstest.dcm")

i=0
plan.Beams[i].NumberofBlocks = '1'
plan.Beams[i].TotalBlockTrayFactor = '1.000'
plan.Beams[i]

ds = Dataset()
sq = Sequence()
sq.append(DataElement(0x300A00F8,'CS','APERTURE'))
sq.append(DataElement(0x300A00FE,'LO','RETIC'))

#add block sequence to plan
plan.Beams[i].AddNew(0x30a00f4, "SQ", ds)
ds=sq
plan.Beams[i].Blocks = ds
print plan.Beams[i]

Darcy Mason

unread,
Oct 18, 2011, 11:09:51 PM10/18/11
to pydicom
On Oct 18, 8:14 am, Robert Ross <robert.2.r...@gmail.com> wrote:
> Dear List
>
> I'm trying to add a block sequence to RT Plan objects, using Python2.6
> and Pydicom.  The objective is to add a reticle to the plans, which
> must be done as a block sequence.  I can add the individual data
> elements to the plan, but I can't add them to the block sequence.

Hi Robert,

Yeah, adding sequences can get quite confusing. The key thing is that
a sequence is a list of datasets. So I try to build these up from the
bottom, doing the datasets first, then create the sequence and set the
'SQ' data element in the parent.

I think this should do what you want:

>>> ds1 = Dataset()
>>> ds1.BlockType = "APERTURE"
>>> ds1.BlockName = "RETIC"
>>> ds.Beams[i].Blocks = Sequence([ds1])


Regards,
Darcy

Robert Ross

unread,
Oct 19, 2011, 3:57:49 AM10/19/11
to pydicom
Excellent, thank you very much Darcy! This works perfectly.

Let me just check I'm understanding what's going on:
>>> ds1 = Dataset()
make a new dataset

>>>ds1.BlockType = "APERTURE"
the dataset already has access to the dataelements, so we can add them
by specifying the name and value

>>> ds.Beams[i].Blocks = Sequence([ds1])
adds a Block Sequence and sets it equal to the dataset which has been
cast to a sequence

Kind regards
Robert

Darcy Mason

unread,
Oct 19, 2011, 9:08:06 AM10/19/11
to pydicom
On Oct 19, 3:57 am, Robert Ross <robert.2.r...@gmail.com> wrote:
> Excellent, thank you very much Darcy!  This works perfectly.
>
> Let me just check I'm understanding what's going on:
> ... ...
> >>> ds.Beams[i].Blocks = Sequence([ds1])
>
> adds a Block Sequence and sets it equal to the dataset which has been
> cast to a sequence

What you said is all correct, except the "dataset cast to a sequence"
is a strange way to say it, so just to make sure it is clear: the last
line creates a python list, with one item in it (a dataset ds1), and
then converts that to a Sequence (which is really just a python list
with a little bit of functionality added).

If there had been two items in the sequence, then there would have
been something like this in addition to the previous lines:
>>> ds2 = Dataset()
>>> ds2.BlockType = '...' etc
>>> ds.Beams[i].Blocks = Sequence([ds1, ds2])

In fact, since this is a nice tidy example, I think I'll add that to
the wiki to show how to do this kind of operation.

By the way, the Sequence above is just an object, and the Blocks data
element is set to it by reference, so any further appending to the
list etc will be reflected in the contents of that sequence. In other
words, you don't have to assign the SQ item fully formed as shown
here; it could be initialized and then items appended as you go.

Cheers,
Darcy

Edoardo Pasca

unread,
Nov 1, 2011, 6:37:45 AM11/1/11
to pyd...@googlegroups.com
Hi there,

what if I want to add a sequence that is not there yet? I want to add
the sequence
(0x0008,0x1250) RelatedSeriesSequence to the dicom file.

Edo

> --
> You received this message because you are subscribed to the Google Groups "pydicom" group.
> To post to this group, send email to pyd...@googlegroups.com.
> To unsubscribe from this group, send email to pydicom+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pydicom?hl=en.
>
>

--
Edo
weblog  http://edo.imanetti.net/wordpress
podcast http://sieegiueeaccaso.blogspot.com
cooking http://edo.imanetti.net/cucina
:wq

Darcy Mason

unread,
Nov 1, 2011, 8:00:42 AM11/1/11
to pydicom

On Nov 1, 6:37 am, Edoardo Pasca <edo.pask...@gmail.com> wrote:
> Hi there,
>
> what if I want to add a sequence that is not there yet? I want to add
> the sequence
> (0x0008,0x1250) RelatedSeriesSequence to the dicom file.
>

Hi Edo,

The previous examples should work whether or not the sequence already
exists. With the tag you want to use, since it is group 8, I guess it
must belong to the root dataset. So, something like this should work:

>>> ds= dicom.read_file(...)
>>> ds.RelatedSeries = Sequence([ds1, ds2, ds3])

-Darcy


Edo

unread,
Nov 1, 2011, 9:33:32 AM11/1/11
to pydicom
Darcy, thanks for the prompt response. I could achieve what I wanted
by

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

ds1 = Dataset()
ds1.StudyInstanceUID = "Study UID of the first related series"
ds1.SeriesInstanceUID = "Series UID of the first related series"

ds2 = Dataset()
ds2.StudyInstanceUID = "Study UID of the second related series"
ds2.SeriesInstanceUID = "Series UID of the second related series"


ds = ReadFile (somefile)
ds.AddNew((0x0008,0x1250), 'SQ', Sequence([ds1,ds2]))

I wonder whether the AddNew is really needed. In my case I did not
have that as a known tag.

Edo
Reply all
Reply to author
Forward
0 new messages