Search code examples
pythonpydicom

Is there a way to add MultiValues and lists in pydicom


I am trying to add a MultiValue to a list. But I am getting an error stating that I can't. I have also tried .append and .extend the multivalues to the list but get the error that you cannot add NaN values to a list. Also, I am having trouble understanding what exactly is MultiValue.

The code that I am trying to run is:

original_z_spacing = np.abs(patient_data[0].ImagePositionPatient[2]
                            - patient_data[1].ImagePositionPatient[2])
# obtain rescaled HU array
hu_array = get_hounsfield_unit_array(patient_data)

original_spacings = np.array(patient_data[0].PixelSpacing + [original_z_spacing], dtype='float32')  # the error occurs here 

The patient data is a folder of 1595 folders containing dicom files. I am running this code on Python 3.7 and pydicom version 1.4.2. The error that I am getting is:

TypeError: unsupported operand type(s) for +: 'MultiValue' and 'list'

Solution

  • There are two problems here: a MultiValue can indeed not be added to a list, but you can cast it to list. That alone would not work in this case, because PixelSpacing has the VR "DS", e.g. is written as string values, so you have to convert it to float first.

    This should work:

    pixel_spacing = patient_data[0].PixelSpacing
    original_spacings = np.array([float(pixel_spacing[0]), float(pixel_spacing[1]), original_z_spacing], dtype='float32')
    

    MultiValue is a class used in pydicom to represent the value of a multi-valued tag. It mostly behaves like a list (it is derived from MutableSequence) and can be converted to a list if needed.