MONAI icon indicating copy to clipboard operation
MONAI copied to clipboard

`PydicomReader` should not just silently make up an affine matrix

Open CPBridge opened this issue 7 months ago • 0 comments

Describe the bug

The PydicomReader sometimes just makes up an incorrect affine matrix! Based on this line, this is true for any DICOM file from which the orientation and position cannot be straightforwardly determined from a single ImagePositionPatient and ImageOrientationPatient attribute. There are various situations in DICOM where this might be true, but an important one is any file that has multiple frames. The file used in the example below, a "multiframe" Enhanced CT, demonstrates this. The spatial information attributes are recorded per-frame in the SharedFunctionalGroupsSequence and/or the PerFrameFunctionalGroupsSequence, nested sequences within the DICOM file. In such situations it seems that the reader will just assume an affine matrix that is a permuted identity matrix.

Deducing the affine matrix in these sorts of situations gets surprisingly complicated (I know, I have implemented it before...). There are multiple places where the relevant spatial metadata may be stored and there are usually no guarantees that frames have the same orientation, they are regularly spaced, the axes are orthogonal, etc, and all this geometry must be figured out. I therefore understand why the PydicomReader would not support all this, since pydicom itself does not really have these capabilities and it would have to be hand-coded in monai, which seems like unnecessary work when the ITKReader seems to work correctly in these situations (although I haven't systematically looked into this). However I do feel that the reader should not just silently pretend that the affine matrix is something other than it is as this leads to downstream strangeness that has created a lot of confusion in some of our projects. Personally, I would therefore not necessarily suggest that we try to implement the correct behaviour in PydicomReader, but perhaps either raise an exception or at least issue a warning if the PydicomReader is assuming an incorrect matrix, and maybe point users to using the ITKReader instead.

To Reproduce

import pydicom
import monai

# Use a multiframe DICOM file from the pydicom test data
testfile = pydicom.data.get_testdata_file('eCT_Supplemental.dcm')

# Create two loaders, one that uses ITK reader and another that uses Pydicom reader
load_itk = monai.transforms.LoadImage(
    reader=monai.data.ITKReader(),
    ensure_channel_first=True,
)
load_pydicom = monai.transforms.LoadImage(
    reader=monai.data.PydicomReader(),
    ensure_channel_first=True,
)

metatensor_itk = load_itk(testfile)
metatensor_pydicom = load_pydicom(testfile)

print(metatensor_itk.affine)
print(metatensor_pydicom.affine)

prints this:

tensor([[   0.3887,    0.0000,    0.0000,  -99.5000],
        [   0.0000,   -0.3887,    0.0000,  301.5000],
        [   0.0000,    0.0000,   10.0000, -159.0000],
        [   0.0000,    0.0000,    0.0000,    1.0000]], dtype=torch.float64)
tensor([[0., 1., 0., 0.],
        [1., 0., 0., 0.],
        [0., 0., 1., 0.],
        [0., 0., 0., 1.]], dtype=torch.float64)

Expected behavior

As above, at least a warning here would be very useful and save a lot of confusion.

Environment I'm fairly sure this isn't relevant, but

================================
Printing MONAI config...
================================
MONAI version: 1.4.0
Numpy version: 1.26.4
Pytorch version: 2.7.0
MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False
MONAI rev id: 46a5272196a6c2590ca2589029eed8e4d56ff008
MONAI __file__: /Users/<username>/.pyenv/versions/dev/lib/python3.13/site-packages/monai/__init__.py

Optional dependencies:
Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.
ITK version: 5.4.3
Nibabel version: 5.3.2
scikit-image version: NOT INSTALLED or UNKNOWN VERSION.
scipy version: 1.15.2
Pillow version: 11.1.0
Tensorboard version: NOT INSTALLED or UNKNOWN VERSION.
gdown version: NOT INSTALLED or UNKNOWN VERSION.
TorchVision version: NOT INSTALLED or UNKNOWN VERSION.
tqdm version: 4.67.1
lmdb version: NOT INSTALLED or UNKNOWN VERSION.
psutil version: 7.0.0
pandas version: 2.2.3
einops version: NOT INSTALLED or UNKNOWN VERSION.
transformers version: NOT INSTALLED or UNKNOWN VERSION.
mlflow version: NOT INSTALLED or UNKNOWN VERSION.
pynrrd version: 1.1.3
clearml version: NOT INSTALLED or UNKNOWN VERSION.

For details about installing the optional dependencies, please visit:
    https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies


================================
Printing system config...
================================
System: Darwin
Mac version: 15.5
Platform: macOS-15.5-arm64-arm-64bit-Mach-O
Processor: arm
Machine: arm64
Python version: 3.13.2
Process name: python3.13
Command: ['/Users/cpb28/.pyenv/versions/dev/bin/python', '-c', 'import monai; monai.config.print_debug_info()']
Open files: []
Num physical CPUs: 10
Num logical CPUs: 10
Num usable CPUs: UNKNOWN for given OS
CPU usage (%): [18.8, 19.5, 66.5, 37.1, 56.8, 32.6, 2.3, 1.2, 0.6, 1.2]
CPU freq. (MHz): 3228
Load avg. in last 1, 5, 15 mins (%): [22.2, 25.4, 35.1]
Disk usage (%): 35.9
Avg. sensor temp. (Celsius): UNKNOWN for given OS
Total physical memory (GB): 64.0
Available memory (GB): 25.9
Used memory (GB): 28.4

================================
Printing GPU config...
================================
Num GPUs: 0
Has CUDA: False
cuDNN enabled: False
NVIDIA_TF32_OVERRIDE: None
TORCH_ALLOW_TF32_CUBLAS_OVERRIDE: None

Additional context

I have been discussing this with @sudomakeinstall, @FJDorfner, and @mdorster, who have all been bitten by this unfortunate behaviour.

Once a path forward is determined, I am happy to help with a fix if that would save time.

As an aside that is not strictly related, I maintain the highdicom library which builds on top of pydicom to deal with things like spatial arrangements of frames. I am considering contributing a HighdicomReader to monai in the near future so that there is an alternative to ITK (plus some other advantages particularly regarding DICOM segmentations). Is this something that you may be interested in?

CPBridge avatar Jun 02 '25 15:06 CPBridge