Pydicom: write_file meta_dataset missing (0002,0002) and (0002,0003)

Created on 19 Nov 2015  路  4Comments  路  Source: pydicom/pydicom

I am reading, editing, then writing temporary DICOM files in Siemens' syngo MI applications (broker activity). The temporary DICOMs are missing the several required tags to use dicom.write_file. I tried populating the tags before writing the new DICOM but an error was still raised.

meta_dataset contains the following tags:
(0002, 0000) File Meta Information Group Length
(0002, 0001) File Meta Information Version
(0002, 0010) Transfer Syntax UID
(0002, 0012) Implementation Class UID
(0002, 0013) Implementation Version Name

In filewriter.py _write_file_meta_info I get a ValueError raised with missing tags (0002,0002) and (0002,0003).

I resolved the issue by adding the following (line 255 filewriter.py)

if Tag((2, 2)) not in meta_dataset:
        meta_dataset.add_new((2, 2), 'UI', "")   # file meta information version
if Tag((2, 3)) not in meta_dataset:
        meta_dataset.add_new((2, 3), 'UI', "")   # file meta information version

but am not sure if this is the right approach.

Most helpful comment

Okay, okay,.. just when the frustration level is high enough to post something, I figure it out.

I didn't realize that there was actually a file_meta field in my FileDataSet object. But, low and behold, it's there if you ask for it.

Just like in the example that darcymason mentioned one can add the missing fields. In my case, relying on the said example, this fixed the problem:

im.file_meta.ImplementationClassUID = "1.2.3.4"
im.file_meta.MediaStorageSOPInstanceUID = "1.2.3"
im.file_meta.MediaStorageSOPClassUID = '1.2.840.10008.5.1.4.1.1.2'

Thanks!

Best

Matt

All 4 comments

Well, that is one way of dealing with it, but I wouldn't recommend making those changes in pydicom, but rather in your user code. If you haven't seen it, have a look at write_new.py, in the examples folder. It shows how to add these tags. To do it properly you should set the proper media storage SOP class UID (2, 2), and a "real" instance UID for (2, 3) (which can be now be generated from generate_uid() function in the uid module).

Hello @darcymason,
I have DICOM files of a CT scanner which also misses the 0002 tags.
You state that write_new.py or generate_uid() should be used but i don't understand how to apply this. I need to be able to read the DICOM header to analyse it.
My code below

import dicom
import os
import numpy
from matplotlib import pyplot, cm
from dicom.dataset import Dataset, FileDataset
from dicom.UID import generate_uid

"""i sort of understand what this does"""
PathDicom = "./Dicomfiles"
lstFilesDCM = [] #create an empty list
for dirName, subdirlist, filelist in os.walk(PathDicom):
    for filename in filelist:
        if ".dcm" in filename.lower(): #to check whether the file is DICOM
            lstFilesDCM.append(os.path.join(dirName,filename))

"""I use force=True here because it missed DICM marker probably due to not having the 0002"""
RefDs = dicom.read_file(lstFilesDCM[0], force=True)
print RefDs

How do i add the functions you are talking about into my code, I am to much off noob to Python to understand what is happening in the files when i personally look at them.

Hi,

First of all, I really love the pydicom package. I really love it. I'm trying to solve the same problem that stevenrbwh had without changing the source code. I already looked at the code in write_new.py

I get the following error when trying to write a file with the save_as method of my dataset im:

/home/divine/miniconda3/envs/dicom/lib/python3.6/site-packages/dicom/dataset.py in save_as(self, filename, write_like_original)
    489         :write_like_original: see dicom.filewriter.write_file for info on this parameter.
    490         """
--> 491         dicom.write_file(filename, self, write_like_original)
    492
    493     def __setattr__(self, name, value):


/home/divine/miniconda3/envs/dicom/lib/python3.6/site-packages/dicom/filewriter.py in write_file(filename, dataset, write_like_original)
    348         if preamble:
    349             fp.write(preamble)  # blank 128 byte preamble
--> 350             _write_file_meta_info(fp, file_meta)
    351
    352         # Set file VR, endian. MUST BE AFTER writing META INFO (which changes to Explicit LittleEndian)

/home/divine/miniconda3/envs/dicom/lib/python3.6/site-packages/dicom/filewriter.py in _write_file_meta_info(fp, meta_dataset)
    272             missing.append(Tag((2, element)))
    273     if missing:
--> 274         raise ValueError("Missing required tags {0} for file meta information".format(str(missing)))
    275
    276     # Put in temp number for required group length, save current location to come back

ValueError: Missing required tags [(0002, 0002), (0002, 0003), (0002, 0012)] for file meta information

I add something for the missing elements using the add_new() method as follows:

im.add_new((0x002, 0x0012),'UI','')
im.add_new((0x002, 0x0003),'UI','')
im.add_new((0x002,0x002),'UI','')

This I check with the following code to make sure they are in the dataset:

missing=[]
for element in [2, 3, 0x10, 0x12]:
    if Tag((2, element)) not in im:
        missing.append(Tag((2, element)))
print(missing)

This of course returns an empty list. However, I still get the same error.

Any help would be greatly appreciated.

Thanks

Matt

Okay, okay,.. just when the frustration level is high enough to post something, I figure it out.

I didn't realize that there was actually a file_meta field in my FileDataSet object. But, low and behold, it's there if you ask for it.

Just like in the example that darcymason mentioned one can add the missing fields. In my case, relying on the said example, this fixed the problem:

im.file_meta.ImplementationClassUID = "1.2.3.4"
im.file_meta.MediaStorageSOPInstanceUID = "1.2.3"
im.file_meta.MediaStorageSOPClassUID = '1.2.840.10008.5.1.4.1.1.2'

Thanks!

Best

Matt

Was this page helpful?
0 / 5 - 0 ratings