Trouble creating DICOMDIR

241 views
Skip to first unread message

Peter Brunnengräber

unread,
Feb 7, 2020, 5:52:12 PM2/7/20
to pydicom
Hello all,
I'm looking for some assistance as I've hit a wall here and I'm not sure how best to proceed. I am attempting to write a DICOMDIR file from scratch, but I've run into some troubles.

First, I am able to load a DICOMDIR and modify it successfully, but the output file it is non-compliant because of invalid or missing 0004 group tags.  Two of these tags (OffsetOfTheNextDirectoryRecord and OffsetOfReferencedLowerLevelDirectoryEntity) should be the byte offsets in the file, but I'm not sure how best to do this as sequences may be added to the file out of order and I'm not sure I can properly calculate the offset.  I am writing the file with:
ds = DicomDir(filename, dicom_dir, is_implicit_VR=False, is_little_endian=True, file_meta=file_meta, preamble=preamble) 
ds.save_as(filename, write_like_original=False)
I was expecting the DicomDir class would have a method that handles these offsets for you as 'write_like_original=False' does for you with the offsets in the 0002 group.  Otherwise is there a difference in using the DicomDir vs the FileDataset class?


Also, when I try to create a Patient record sequence, I'm adding it to the dataset to the sequence without an error, but when I go to pass this to the DicomDir I get the error:

Traceback (most recent call last):

  File "make_dicomdir.py", line 88, in <module>

    ds = DicomDir(filename, dicom_dir, is_implicit_VR=False, is_little_endian=True, file_meta=file_meta, preamble=preamble) 

  File "/usr/local/lib/python3.5/dist-packages/pydicom/dicomdir.py", line 86, in __init__

    self.parse_records()

  File "/usr/local/lib/python3.5/dist-packages/pydicom/dicomdir.py", line 118, in parse_records

    offset = record.seq_item_tell

  File "/usr/local/lib/python3.5/dist-packages/pydicom/dataset.py", line 783, in __getattr__

    return object.__getattribute__(self, name)

AttributeError: 'Dataset' object has no attribute 'seq_item_tell'


My code for the patient sequence is:
DirectoryRecordSequence = Sequence([])

Patient = Dataset()
Patient.DirectoryRecordType = 'PATIENT'
Patient.PatientName = 'Unnamed'
Patient.PatientID = '0001'
Patient.PatientComments = 'nothing here'
Patient.RecordInUseFlag = 65535

DirectoryRecordSequence.append( Patient )
...
dicom_dir.DirectoryRecordSequence = DirectoryRecordSequence
But if I try to put anything else other than a Dataset into a Sequence, it doesn't like that either.

Any help would be appreciated.  -Cheers, Peter.

Peter Brunnengräber

unread,
Mar 2, 2020, 4:39:49 PM3/2/20
to pydicom
Just wanted to bump this up.  Anyone have any thoughts?

Thank you.

Darcy Mason

unread,
Mar 3, 2020, 10:56:16 AM3/3/20
to pydicom
Hi Peter,

There is no write support in pydicom for DicomDir - better DicomDir support has been asked for from time to time, but we have not had time to get to that.

As you have said, it comes down to computing the offsets, otherwise it is just writing a standard dicom file.  Without a lot of thought about it, I have two very loose ideas on how to deal with it.  
1. Pre-compute the offsets -- as per DICOM standard for DICOMDIR, it must be little endian explicit VR. There is an explanation of the lengths of data elements in filereader.py, here, that shows how one could compute the length of a data element (there are "extra length VRs" that need to be considered).  So one could Dataset.walk() through the dataset and keep a running sum of that length based on VR, plus the data element value length.
2. Another way might be to actually write the Dataset (not DicomDir) in memory, and read it back in, then use the `file_tell` and `seq_tell` values to determine the correct offsets, set those, and write to file.

I think you want to create just a Dataset() to start with - the error in using DicomDir is that it is expecting those "tell" attributes which are created on reading.  And your comment about adding to a sequence - yes, anything appended to a Sequence must be a Dataset.

If you wanted to attempt one of these ideas (or something else), and if you could get something close and create a pull request, we could help debug it and get through any problems.

Hope that helps,
Darcy

Peter Brunnengräber

unread,
Mar 3, 2020, 2:20:25 PM3/3/20
to pydicom
Hello Darcy,
  Ahh! Ok... So that makes sense then.  I thought I was just structuring or initializing something wrong... I've been trying to understand what is wrong with an old workstation application that is having difficulty reading dcmtk generated dicomdir files.  Appending the sequence for a normal dataset object works fine, but I thought I was doing something wrong with DicomDir because if I read a dicomdir file I can then add/make changes without any problems.  So, that makes sense as to what is happening.

  Ok, I think proposal two seems to be the least likely to break down the road.  I'll have to take a look into that further.

Thank you ever so much. That was very helpful!
Reply all
Reply to author
Forward
0 new messages