Skip to content

Exception when RescaleSlope/Intercept are present but empty (null) #418

@realcundo

Description

@realcundo

I've run into a problem when a Dicom file contains both RescaleSlope and RescaleIntercept, but because they're empty, highdicom fails with an exception:

     if (
         'RescaleSlope' in sub_ds or
         'RescaleIntercept' in sub_ds
         ):
                 modality_slope_intercept = (
>                   float(sub_ds.get('RescaleSlope', 1.0)),
>                   float(sub_ds.get('RescaleIntercept', 0.0))
                  )
 TypeError: float() argument must be a string or a real number, not 'NoneType'

src/highdicom/image.py:631/632: TypeError

To reproduce (from repo project root):

cp data/test_files/ct_image.dcm data/test_files/ct_image_blank_rescale.dcm
dcmodify -m "(0028,1052)=" data/test_files/ct_image_blank_rescale.dcm

dcmdump data/test_files/ct_image_blank_rescale.dcm | grep Rescale
# (0028,1052) DS (no value available)                     #   0, 0 RescaleIntercept
# (0028,1053) DS [1]                                      #   2, 1 RescaleSlope

running pytests for tests/test_image.py will then fail with the exception.

The cause is that the tag is present and sub_ds.get(..., ...) will return None rather than the default value.

The quickest fix would be along the lines:

rescale_slope = sub_ds.get('RescaleSlope', None)
rescale_intercept  = sub_ds.get('RescaleIntercept', None)

if rescale_slope or rescale_intercept:
    modality_slope_intercept = (
        float(rescale_slope or 1.0),
        float(rescale_intercept or 0.0)
    )

but maybe at this point you'd want to guard against not just empty values but also non-parseable values.

Happy to submit a PR, just let me know whether something quick like above is fine, or you'd prefer something more "proper" (maybe a helper get_float_or_none() which deals with edge cases.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions