Skip to content

fix(nifti): quantize float NIfTI volumes with fractional values instead of truncating them - #554

Open
darkclad wants to merge 1 commit into
PolusAI:mainfrom
darkclad:main-nifti-float
Open

darkclad wants to merge 1 commit into
PolusAI:mainfrom
darkclad:main-nifti-float

Conversation

@darkclad

Copy link
Copy Markdown

Problem

Reported on an MRI dataset: a NIfTI intensity volume stored as FLOAT32 or FLOAT64 lost the fractional part of every voxel. Each value was truncated to a whole grey level, and the intensity features were reported from those integers. The docstrings say a floating-point image is rescaled to dynamic_range grey levels using min_intensity and max_intensity, but that never happened for NIfTI. A volume normalised to [0, 1] came out as all zeros, and a [0, 1000] volume reported its mean about 0.5 low.

Cause: RawNiftiLoader never set fp_pixels_ for the float datatypes, which TIFF and OME-Zarr do. The scan therefore recorded the offset map (1 grey level = 1 unit) and NiftiLoader truncated each voxel. NiftiLoader also had no quantized branch at all.

Change

  • RawNiftiLoader reports a FLOAT32/FLOAT64 volume as real-valued when any finite voxel's header-rescaled value carries a fraction. The scan then records the quantized map, the same map a floating-point TIFF or OME-Zarr takes.
  • NiftiLoader takes the quantized map from ImageLoader::open, as the TIFF and Zarr loaders do. It applies scl_slope/scl_inter first and then clamps to [fpmin, fpmax] spread over dynamic_range levels.
  • A float volume holding whole values only keeps one grey level per unit, as before. That covers a CT saved as float32 and label-like phantoms. Its texture features stay binned in its own units, so absolute bin widths (e.g. 25 HU) keep their meaning.
  • preserve_hu=True, integer NIfTI (stored map) and mask volumes are unchanged.

Fixture and vetting

tests/data/nifti/phantoms/ut_inten.nii held continuous fractions that no test ever saw, because the loader truncated them. Every golden on it, and every MATLAB, MIRP and PyRadiomics oracle, was computed on the truncated volume; the generators floor it explicitly as fixture setup. The fixture is now floor(ut_inten) (still float32), so all of those values hold unchanged under the new rule. Two follow-on changes:

  • 3COVERED_IMAGE_INTENSITY_RANGE on it is exactly 1. The old pin, 1.0002, exceeded the feature's bound (not_covered.md §F, flagged bound-violation). The flag and the §F entry are removed.
  • The ngldm3d.mirp_fbn64 and gldzm3d.mirp_fbn64 comparison tables are refreshed: these "measured only" tables fed MIRP the raw fractional values. gen_ngldm3d_mirp.py verifies 115/115 and gen_gldzm3d_mirp.py --check 96/96.

Tests

These fail without the fix:

  • TEST_3D_NIFTI_FLOAT_REPORTS_REAL_VALUED_VOXELS_MECHANICS: float with fractions reports real-valued; whole-valued float32/float64, int16 and uint8 do not.
  • TEST_3D_NIFTI_FLOAT_UNIT_RANGE_QUANTIZED_MECHANICS: a [0, 1] volume lands on 512 distinct levels over 0..10000, each reading back to within 1e-4.
  • TEST_3D_NIFTI_FLOAT_SLOPE_QUANTIZED_MECHANICS: scl_slope 2 is applied before quantizing over the physical window.
  • TEST_3D_NIFTI_FLOAT_FPIMAGE_WINDOW_MECHANICS: --fpimgmin/--fpimgmax/--fpimgdr set the window.
  • test_3d_nifti_float_keeps_fraction_mechanics: the reporter's script as a test, with random float32/float64 volumes over [0, 1) and [0, 1000), checking 3MIN/3MAX/3MEAN against numpy to within one quantization step.

Negative test: TEST_3D_NIFTI_FLOAT_NOT_QUANTIZED_MECHANICS covers what must not quantize: preserve_hu, a whole-valued float volume, an integer volume with a rescale, and a float volume read as a mask.

New fixtures: ct3d_unit.nii and ct3d_float_slope.nii, documented in tests/data/hounsfield/README.md.

Docs: supported_formats.rst (intensity units) and f_intensity_3d.rst now describe the rule.

Closes PN-302. Settles question 1 of PN-262 (truncating float NIfTI was a defect).

@vjaganat90 vjaganat90 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I built the head and its base and read 8 x 8 x 8 NIfTI volumes made from ct3d_int16.nii's header:

  • float32 in [0, 1]: 3MIN, 3MAX, 3MEAN are 0, 0, 0 on the base and 0.0011, 0.9998, 0.4966 on the head (numpy: 0.0011, 0.9998, 0.4967).
  • float64 in [0, 1000]: the head's 3MEAN is 493.084 against numpy's 493.135, within one quantization step (0.1); the base gave 492.637.
  • Whole-valued float32 and int16 volumes give the same 52 intensity and GLSZM values as the base.
  • The floored ut_inten.nii on the head against the old file on the base: of 52 values only 3COVERED_IMAGE_INTENSITY_RANGE moves.

…ad of truncating them

A FLOAT32/FLOAT64 NIfTI volume was never reported as real-valued, so the
scan put it on the offset map, where every voxel was truncated to a whole
unit. A volume normalised to [0, 1] read back as all zeros and a [0, 1000]
volume reported its mean about half a unit low. min_intensity,
max_intensity and dynamic_range did nothing for NIfTI.

RawNiftiLoader now reports a float volume as real-valued when a finite
voxel's header-rescaled value carries a fraction. The scan then records the
quantized map a float TIFF or OME-Zarr takes, and NiftiLoader applies it
after the scl_slope/scl_inter rescale. A float volume holding whole values
only (a CT saved as float, a label-like phantom) keeps one grey level per
unit, so its texture features stay binned in its own units.

ut_inten.nii is replaced by the floor of its values: every golden and every
MATLAB, MIRP and PyRadiomics oracle on it was computed on the truncated
volume, so they all hold unchanged. 3COVERED_IMAGE_INTENSITY_RANGE on it is
now exactly 1, no longer above its bound. The two MIRP fixed_bin_number
comparison tables, which fed MIRP the fractional values, are refreshed.

Tests that fail without the fix: TEST_3D_NIFTI_FLOAT_REPORTS_REAL_VALUED_
VOXELS, _UNIT_RANGE_QUANTIZED, _SLOPE_QUANTIZED, _FPIMAGE_WINDOW (gtest) and
test_3d_nifti_float_keeps_fraction_mechanics (float32/float64, [0, 1) and
[0, 1000)). TEST_3D_NIFTI_FLOAT_NOT_QUANTIZED pins the paths that must not
quantize: preserve_hu, a whole-valued float volume, an integer volume and a
float mask.

Closes PN-302; settles question 1 of PN-262.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants