question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NIfTI writer does not store cal_min and cal_max

See original GitHub issue

Describe the bug

I’m storing NIfTI images with SaveImaged but when viewed in MRIcroGL I’m seeing this:

image

This is due to wrong intensity values: image

These are read from the NIfTI attributes cal_min and cal_max.

I did add a transform to make sure these are set:

class FixMinMax(MapTransform):

    def __init__(
        self,
        keys: KeysCollection,
        allow_missing_keys: bool = False,
    ) -> None:
        super().__init__(keys, allow_missing_keys)

    def __call__(self, data: Mapping[Hashable, NdarrayTensor]) -> Dict[Hashable, NdarrayTensor]:
        d = dict(data)
        for key in self.key_iterator(d):
            img: torch.Tensor
            img, *_ = convert_data_type(d[key], torch.Tensor)  # type: ignore

            meta_key = f"{key}_meta_dict"
            print(d[meta_key])
            d[meta_key]['cal_min'] = torch.min(img).item()
            d[meta_key]['cal_max'] = torch.max(img).item()
            print(d[meta_key])

        return d

Nonetheless, it still does not work.

I did output the NIfTI header:

nifti = nib.load(path / 'hippo.nii.gz')
header = {a: nifti.header[a] for a in nifti.header}

And it contains:

'cal_max': array(0., dtype=float32),
'cal_min': array(0., dtype=float32)

In fact, the monai.data.nifti_writer.write_nifti function does not expect nor set these two attributes.

Environment

Click to view the environment

Printing MONAI config…

MONAI version: 0.7.0 Numpy version: 1.19.2 Pytorch version: 1.8.1+cu111 MONAI flags: HAS_EXT = False, USE_COMPILED = False MONAI rev id: bfa054b9c3064628a21f4c35bbe3132964e91f43

Optional dependencies: Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION. Nibabel version: 3.1.1 scikit-image version: 0.18.0 Pillow version: 8.3.2 Tensorboard version: 2.7.0 gdown version: NOT INSTALLED or UNKNOWN VERSION. TorchVision version: NOT INSTALLED or UNKNOWN VERSION. tqdm version: 4.62.3 lmdb version: NOT INSTALLED or UNKNOWN VERSION. psutil version: NOT INSTALLED or UNKNOWN VERSION. pandas version: 1.2.4 einops version: NOT INSTALLED or UNKNOWN VERSION. transformers 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: Windows Win32 version: (‘10’, ‘10.0.19041’, ‘SP0’, ‘’) Platform: Windows-10-10.0.19041-SP0 Processor: Intel64 Family 6 Model 63 Stepping 2, GenuineIntel Machine: AMD64 Python version: 3.7.7 Process name: python.exe Command: [‘C:\Users\SebastianPenhouet\AppData\Local\Programs\Python\Python37\python.exe’, ‘-c’, ‘import monai; monai.config.print_debug_info()’] Open files: [popenfile(path=‘C:\Windows\System32\de-DE\KernelBase.dll.mui’, fd=-1), popenfile(path=‘C:\Windows\System32\de-DE\kernel32.dll.mui’, fd=-1)] Num physical CPUs: 6 Num logical CPUs: 12 Num usable CPUs: 12 CPU usage (%): [16.3, 11.8, 26.1, 20.3, 12.4, 7.2, 17.6, 16.3, 15.0, 9.2, 12.4, 58.2] CPU freq. (MHz): 3501 Load avg. in last 1, 5, 15 mins (%): [0.0, 0.0, 0.0] Disk usage (%): 97.8 Avg. sensor temp. (Celsius): UNKNOWN for given OS Total physical memory (GB): 31.9 Available memory (GB): 18.6 Used memory (GB): 13.3

Printing GPU config…

Num GPUs: 1 Has CUDA: True CUDA version: 11.1 cuDNN enabled: True cuDNN version: 8005 Current device: 0 Library compiled for CUDA architectures: [‘sm_37’, ‘sm_50’, ‘sm_60’, ‘sm_61’, ‘sm_70’, ‘sm_75’, ‘sm_80’, ‘sm_86’, ‘compute_37’] GPU 0 Name: Quadro K2200 GPU 0 Is integrated: False GPU 0 Is multi GPU board: False GPU 0 Multi processor count: 5 GPU 0 Total memory (GB): 4.0 GPU 0 CUDA capability (maj.min): 5.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
Spenhouetcommented, Oct 28, 2021
1reaction
rijobrocommented, Oct 26, 2021

Or perhaps you’re saying that in the general case, cal_min and cal_max might not be zero, in which case we need to take it into account?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with NIfTI images in R | A HopStat and Jump Away
It has functions to read/write images, introduces the S4 nifti class, and has useful plotting functions. There are some limitations and some ...
Read more >
oro.nifti: Rigorous - 'NIfTI' + 'ANALYZE' + 'AFNI' : Input / Output
This is so that when images are rendered/written, the values correspond to those in the array (stored in .Data slot) are plotted on...
Read more >
Working with the NIfTI Data Standard in \proglang{R},Working ...
Abstract. The package facilitates the interaction with and manipulation of medical imaging data that conform to the ANALYZE, NIfTI and AFNI formats.
Read more >
Working with the NIfTI Data Standard in R
The oro.nifti package should appeal not only to R package developers, ... the min/max values of the imaging data using the cal.min and...
Read more >
Time Stamp information missing in NifTI header data - Support
I am working with Philips cardiac ultrasound scans and would like to gain information about interesting slices from using their .nii header and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found