diff --git a/docs/source/Math/f_intensity_3d.rst b/docs/source/Math/f_intensity_3d.rst index 18c5c8268..e6b97eb9b 100644 --- a/docs/source/Math/f_intensity_3d.rst +++ b/docs/source/Math/f_intensity_3d.rst @@ -55,8 +55,9 @@ bounding box outside the mask. ``3COVERED_IMAGE_INTENSITY_RANGE`` through its slope, variances through the square of its slope, and the ratios ``3COV``, ``3QCOD`` and ``3UNIFORMITY_PIU`` are recomputed from the mapped parts. A signed integer volume is therefore reported in its own signed values. A floating-point volume - is reported in its own range, to the resolution of its grey levels (``--fpimgdr``, 10000 steps - over the volume's range by default). + with fractional values is reported in its own range, to the resolution of its grey levels + (``--fpimgdr``, 10000 steps over the volume's range by default); a floating-point NIfTI volume + that holds only whole values is reported exactly, as an integer volume is. - **Files only:** ``Nyxus3D`` reads volumes from files (``featurize_files`` or ``featurize_directory``); there is no in-memory path. - **IBSI mode:** ``--ibsi=true`` does not change any 3D intensity feature. diff --git a/docs/source/supported_formats.rst b/docs/source/supported_formats.rst index 2f7858dd0..afc6549ba 100644 --- a/docs/source/supported_formats.rst +++ b/docs/source/supported_formats.rst @@ -152,8 +152,11 @@ they are reported in the image's own units: ``0 .. --fpimgdr`` grey levels (10000 by default). ``--fpimgmin`` and ``--fpimgmax`` replace the image's own range with a fixed one; giving any of the three options switches every floating-point image to that fixed range, which is 0 to 1 unless set. -* A floating-point NIfTI volume is not rescaled: its values are truncated to whole units (after - the shift, when it has negative values), so fractional values are lost. +* A floating-point NIfTI volume with fractional values (after its ``scl_slope`` and ``scl_inter`` + rescale) is rescaled the same way, so a volume normalised to 0 .. 1 keeps its fraction. One that + holds only whole values, such as a CT saved as float, is not rescaled: it is shifted like an + integer image, so one grey level is one intensity unit and texture features are binned in its own + units. * A DICOM image, or an integer NIfTI volume, keeps its stored integers as grey levels and applies its rescale slope and intercept when converting back, so a fractional rescaled value is reported exactly. diff --git a/src/nyx/image_loader.cpp b/src/nyx/image_loader.cpp index fab2980cd..686012889 100644 --- a/src/nyx/image_loader.cpp +++ b/src/nyx/image_loader.cpp @@ -93,8 +93,11 @@ bool ImageLoader::open (SlideProps & p, const FpImageOptions & fpopts) if (fmt == Nyxus::ContainerKind::Nifti) { // Same as DICOM for an integer volume. A real-valued one never takes the stored - // map, so it rescales and shifts by the offset the scan recorded. - intFL = new NiftiLoader (int_fpath, medical_shift, medical_rescale, round_offset); + // map: one holding fractional values takes the quantization a real-valued TIFF + // or Zarr takes, and one of whole values the offset map, as does either under + // preserve_hu -- every map after the header rescale. On the quantized map + // medical_shift is fpmin. + intFL = new NiftiLoader (int_fpath, medical_shift, medical_rescale, round_offset, quantize, fpmax, dr); } else { diff --git a/src/nyx/raw_nifti.h b/src/nyx/raw_nifti.h index bb26b72d2..c6e63cdb8 100644 --- a/src/nyx/raw_nifti.h +++ b/src/nyx/raw_nifti.h @@ -69,10 +69,12 @@ class RawNiftiLoader : public RawFormatLoader case 16: // NIFTI_TYPE_FLOAT32 get_uint32_pixel_typeresolved = get_uint32_pixel_imp ; get_dpequiv_pixel_typeresolved = get_dp_pixel_imp ; + fp_pixels_ = holds_fraction(); break; case 64: // NIFTI_TYPE_FLOAT64 get_uint32_pixel_typeresolved = get_uint32_pixel_imp ; get_dpequiv_pixel_typeresolved = get_dp_pixel_imp ; + fp_pixels_ = holds_fraction(); break; default: std::string erm = "error: unrecognized NIFTI data type " + std::to_string(nii_->datatype) + " in " + slide_path_; @@ -193,6 +195,24 @@ class RawNiftiLoader : public RawFormatLoader int bitsPerSample_ = 1; std::string slide_path_; + + // Whether a real-valued volume carries a fraction anywhere: a finite voxel whose header-rescaled + // value is not a whole number. Such a volume reports itself as real-valued, so the scan records + // the quantized map a real-valued TIFF or OME-Zarr takes and a volume normalised to [0, 1] + // keeps its fraction. A FLOAT32/FLOAT64 volume holding whole values only -- a CT stored as + // float, a label-like phantom -- reports itself as integer and keeps 1 grey level == 1 + // intensity unit, so its texture features are binned in its own units. Non-finite voxels say + // nothing about the volume's values and are skipped, as the scan skips them. + bool holds_fraction() const + { + for (size_t i = 0; i < nii_->nvox; i++) + { + double v = get_dpequiv_pixel (i); + if (std::isfinite (v) && v != std::floor (v)) + return true; + } + return false; + } }; template @@ -204,9 +224,13 @@ class NiftiLoader : public AbstractTileLoader std::string const& slide_path, double inten_offset = 0.0, // the shift the scan recorded: physical units, or stored units with the rescale off bool rescale_to_physical = true, // off for a mask volume, whose voxels are labels, and on the stored map - bool round_offset = false) // offset map: round to nearest instead of truncating + bool round_offset = false, // offset map: round to nearest instead of truncating + bool quantize = false, // min-max rescale a real-valued volume, instead of the offset map + double inten_max = 1.0, // upper end of the clamp, used only when quantize is set + double target_dyn_range = 1e4) // grey levels the rescale spans, used only when quantize is set : AbstractTileLoader("NiftiLoader", 1/*numberThreads*/, slide_path), - inten_offset_(inten_offset), rescale_(rescale_to_physical), round_offset_(round_offset) + inten_offset_(inten_offset), rescale_(rescale_to_physical), round_offset_(round_offset), + quantize_(quantize), inten_max_(inten_max), target_dyn_range_(target_dyn_range) { slide_path_ = slide_path; @@ -341,36 +365,67 @@ class NiftiLoader : public AbstractTileLoader // The shift this volume's grey levels carry. On the offset map voxels are rescaled to physical // units and shifted by SlideProps::inten_offset (0 unless the volume's own minimum is - // negative). On the stored map rescale_ is off, so the stored integers are shifted by - // SlideProps::inten_stored_shift and the rescale lives in the recorded inverse instead. The - // intensity families apply that inverse, so reported statistics are in the volume's own domain - // either way. cur_scl_* hold the current read's header rescale, shared with rescale_offset(). + // negative). On the quantized map, which a volume holding fractional values takes when read + // without --preserve-hu, inten_offset_ is the low end of the [fpmin, fpmax] window the physical values are + // clamped to and rescaled from. On the stored map rescale_ is off, so the stored integers are + // shifted by SlideProps::inten_stored_shift and the rescale lives in the recorded inverse + // instead. The intensity families apply that inverse, so reported statistics are in the + // volume's own domain on every map. cur_scl_* hold the current read's header rescale, shared + // with rescale_offset(). double inten_offset_ = 0.0; bool rescale_ = true; bool round_offset_ = false; // offset map: round to nearest (--preserve-hu) instead of truncating double cur_scl_slope_ = 1.0, cur_scl_inter_ = 0.0; + // Whether this volume is min-max rescaled into [0, target_dyn_range_] (a real-valued volume + // left in its default mode) or carried on the offset map. SlideProps::inten_map is where the + // choice is made and recorded; ImageLoader::open passes it here, exactly as it does for TIFF + // and OME-Zarr. + bool quantize_ = false; + double inten_max_ = 1.0, + target_dyn_range_ = 1e4; + template void rescale_offset (std::vector& nyxbuf, const fra* houbuf, size_t n) { - // Rescale stored -> physical (slope*stored + intercept) unless the rescale is off, then shift - // by the recorded offset so sub-minimum voxels (negative CT values among them) clamp to 0 - // instead of wrapping on the unsigned cast. With the rescale off the stored integers are - // shifted as they are, and the narrowing below is exact. Reading the volume's own minimum - // here instead would produce a shift nothing downstream could undo. - // - // Nyxus::grey_level() carries the narrowing every load-time map shares. A non-finite voxel - // takes grey level 0 -- a float volume is free to hold NaN, a masked-out background most - // often -- and one above the grey type's maximum saturates, which a 64-bit volume or a - // large scl_slope can reach. round_offset_ rounds to nearest under --preserve-hu, where - // scl_slope is routinely fractional on a PET-derived volume and the scale-1 inverse could - // not recover a truncated fraction; a volume read without the flag truncates, which is what - // a real-valued NIfTI has always done. An integer volume read without the flag takes the - // stored map, where there is no fraction to narrow. + // Rescale stored -> physical (slope*stored + intercept) unless the rescale is off, then map + // the physical value the way the scan recorded. The scan measured its range on the same + // rescaled values (RawNiftiLoader::get_dpequiv_pixel), so the quantized window and the + // offset are both in physical units. With the rescale off the stored integers are shifted + // as they are, and the narrowing is exact. for (size_t i = 0; i < n; ++i) { double v = cur_scl_slope_ * (double)houbuf[i] + cur_scl_inter_; - nyxbuf[i] = Nyxus::grey_level (v - inten_offset_, round_offset_); + nyxbuf[i] = map_intensity (v); } } + + // One physical value -> the grey level stored for it. Both branches narrow through the + // Nyxus::grey_level* calls the TIFF and OME-Zarr loaders use, so no backend can drift from + // another. A non-finite voxel takes grey level 0 on either -- a float volume is free to hold + // NaN, a masked-out background most often -- and one above the grey type's maximum saturates, + // which a 64-bit volume or a large scl_slope can reach on the offset map. + // + // The offset map shifts by the recorded offset so sub-minimum voxels (negative CT values among + // them) clamp to 0 instead of wrapping on the unsigned cast. round_offset_ rounds to nearest + // under --preserve-hu, where scl_slope is routinely fractional on a PET-derived volume and the + // scale-1 inverse could not recover a truncated fraction; otherwise it truncates. Without the + // flag the volumes that reach it hold no fraction to lose: a FLOAT32/FLOAT64 volume of whole + // values (RawNiftiLoader::holds_fraction), an integer one whose header rescale the stored map + // cannot carry (a zero, negative or non-finite slope), or a constant real-valued volume, which + // has no window to quantize into. + // + // The quantized map hard-clamps to [inten_offset_, inten_max_] and spreads that window over + // target_dyn_range_ grey levels, so a volume normalised to [0, 1] keeps its fraction. + template + til map_intensity (double v) const + { + if (! quantize_) + return Nyxus::grey_level (v - inten_offset_, round_offset_); + // Ahead of the clamp, as in the TIFF loaders: the clamp would store +Inf as the top level. + if (! std::isfinite (v)) return (til) 0; + double t = v < inten_offset_ ? inten_offset_ : v; + t = t > inten_max_ ? inten_max_ : t; + return Nyxus::grey_level_truncated (target_dyn_range_ * (t - inten_offset_) / (inten_max_ - inten_offset_)); + } }; diff --git a/tests/data/hounsfield/README.md b/tests/data/hounsfield/README.md index d099360e1..b61f8acb1 100644 --- a/tests/data/hounsfield/README.md +++ b/tests/data/hounsfield/README.md @@ -65,7 +65,9 @@ loader path (`RawNiftiLoader`/`NiftiLoader`). `ct3d_int16.nii` is also the overs | `ct3d_int16.nii` | 8×8×8 signed int16, `scl_slope=2`, `scl_inter=-1024` | stored `idx-200` (−200..311, crosses 0); true HU = 2·stored−1024 | | `ct3d_frac.nii` | 8×8×8 signed int16, `scl_slope=0.5`, `scl_inter=-1024` | stored `idx` (0..511); physical = 0.5·idx−1024 over −1024..−768.5, half the voxels fractional | | `ct3d_nan.nii` | 8×8×8 float32, no rescale | `v(idx) = idx`, with NaN at 3, +Inf at 5, −Inf at 7 | -| `mask3d.nii` | 8×8×8 uint8, all ones | single ROI over the whole volume, shared by all three | +| `ct3d_unit.nii` | 8×8×8 float32, no rescale (`scl_slope=0`) | `v(idx) = idx/511` over 0..1, every voxel but the two ends fractional | +| `ct3d_float_slope.nii` | 8×8×8 float32, `scl_slope=2`, `scl_inter=-1024` | stored `idx + 0.25`; physical = 2·idx − 1023.5 over −1023.5..−1.5 | +| `mask3d.nii` | 8×8×8 uint8, all ones | single ROI over the whole volume, shared by all five | The non-unit `scl_slope` makes the HU rescale observable: in HU mode the loader maps each voxel to `u = round((2·stored − 1024) − floor(HU min)) = 2·stored + 400` (offset domain 0..1022, mean 511), @@ -83,11 +85,25 @@ maps that `ct3d_int16.nii` cannot show: level is `round(0.5·idx)` (MAX −768, MEAN −896): `inten_scale` is 1 on the offset map, so a fraction dropped there is unrecoverable. Without the flag the stored map keeps `idx` as the grey level and reports the fractions exactly (MAX −768.5, MEAN −896.25). Both are pinned. -- **Non-finite voxels.** `ct3d_nan.nii` is float32, so it takes the offset map either way. They are - left out of the scanned extrema and stored as grey level 0, so the finite range stays `[0, 511]` - and the mean is `(130816 − 3 − 5 − 7)/512 = 255.470703125`. - -Both were produced by patching `ct3d_int16.nii`'s own 352-byte header block — its `datatype` +- **Non-finite voxels.** `ct3d_nan.nii` is float32 holding whole values only, so it takes the offset + map either way. They are left out of the scanned extrema and stored as grey level 0, so the finite + range stays `[0, 511]` and the mean is `(130816 − 3 − 5 − 7)/512 = 255.470703125`. + +`ct3d_unit.nii` and `ct3d_float_slope.nii` are consumed by `tests/test_3d_nifti_mechanics.h` +(`TEST_3D_NIFTI_FLOAT_*`). A float NIfTI whose header-rescaled values carry a fraction takes the +quantized map a float TIFF takes, and these pin it: + +- **A volume normalised to 0..1.** `ct3d_unit.nii` spreads over grey levels 0..10000, its 512 values + on 512 distinct levels, each reading back to within 1e-4. Narrowed to whole units it would read + back as 0 everywhere but its last voxel. +- **The rescale comes first.** `ct3d_float_slope.nii` is quantized over the physical window + `[−1023.5, −1.5]` the scan measures on rescaled values, so each voxel reads back to within + 1022/10000. Narrowed to whole units, every voxel lands half a unit off. +- **Not quantized.** Under `--preserve-hu`, `ct3d_unit.nii` takes the offset map and rounds (0.499 → + 0, 0.501 → 1). `ct3d_nan.nii`, float32 of whole values, keeps one grey level per unit. + `ct3d_frac.nii` keeps the stored map. + +All four were produced by patching `ct3d_int16.nii`'s own 352-byte header block — its `datatype` (offset 70), `bitpix` (72), `scl_slope` (112), `scl_inter` (116), `cal_max` (124) and `cal_min` (128) — and appending new little-endian payload, so every field the loader does not read stays byte-identical to the fixture already in the tree: @@ -109,8 +125,16 @@ write("ct3d_frac.nii", 4, 16, 0.5, -1024.0, v = [float(i) for i in range(512)] v[3], v[5], v[7] = math.nan, math.inf, -math.inf write("ct3d_nan.nii", 16, 32, 1.0, 0.0, struct.pack("<512f", *v), 0.0, 511.0) + +write("ct3d_unit.nii", 16, 32, 0.0, 0.0, + struct.pack("<512f", *[i / 511.0 for i in range(512)]), 0.0, 1.0) +write("ct3d_float_slope.nii", 16, 32, 2.0, -1024.0, + struct.pack("<512f", *[i + 0.25 for i in range(512)]), -1023.5, -1.5) ``` +`tests/python/test_3d_nifti_float_mechanics.py` writes its own random float32 and float64 volumes +in the same way at test time. + ## Real-scanner fixture `ct_small.dcm` is pydicom's `CT_small.dcm` test file (128x128 signed int16, diff --git a/tests/data/hounsfield/ct3d_float_slope.nii b/tests/data/hounsfield/ct3d_float_slope.nii new file mode 100644 index 000000000..2fa17a0be Binary files /dev/null and b/tests/data/hounsfield/ct3d_float_slope.nii differ diff --git a/tests/data/hounsfield/ct3d_unit.nii b/tests/data/hounsfield/ct3d_unit.nii new file mode 100644 index 000000000..8732d7d70 Binary files /dev/null and b/tests/data/hounsfield/ct3d_unit.nii differ diff --git a/tests/data/nifti/phantoms/ut_inten.nii b/tests/data/nifti/phantoms/ut_inten.nii index 0a1ad9cc6..dfefb1d2d 100644 Binary files a/tests/data/nifti/phantoms/ut_inten.nii and b/tests/data/nifti/phantoms/ut_inten.nii differ diff --git a/tests/python/test_3d_nifti_float_mechanics.py b/tests/python/test_3d_nifti_float_mechanics.py new file mode 100644 index 000000000..682a68ffc --- /dev/null +++ b/tests/python/test_3d_nifti_float_mechanics.py @@ -0,0 +1,73 @@ +"""Mechanics tests for real-valued NIfTI volumes (FLOAT32 / FLOAT64) read end to end. + +A real-valued NIfTI takes the quantization a real-valued TIFF or OME-Zarr takes: the volume's range +(or the ``min_intensity`` / ``max_intensity`` window) is spread over ``dynamic_range`` grey levels, +and the intensity family applies the recorded inverse, so MIN / MAX / MEAN come back in the volume's +own units to within one quantization step, ``(max - min) / dynamic_range``. A volume normalised to +[0, 1] keeps its fraction; narrowed to whole units instead it would read back as all zeros, and a +[0, 1000] volume would report its MEAN about half a unit low. + +The volumes are random, written at test time with the header of ``ct3d_int16.nii`` (8x8x8) patched +to the float datatype, as tests/data/hounsfield/README.md describes for the committed fixtures, and +paired with ``mask3d.nii`` (all ones). The reference is numpy over the same values. + +Kind: *mechanics* per tests/vetting/SPEC.md 2 -- these pin the loader's load-time map rather than +feature values against an independent reference, and establish no vetting. +""" +import os +import pathlib +import struct + +import numpy as np +import pytest +import nyxus + +DATA = pathlib.Path(__file__).resolve().parent.parent / "data" / "hounsfield" +HEADER_SOURCE = DATA / "ct3d_int16.nii" +MASK = str(DATA / "mask3d.nii") + +pytestmark = pytest.mark.skipif( + not (HEADER_SOURCE.exists() and os.path.exists(MASK)), + reason="NIfTI fixtures not present in tests/data/hounsfield", +) + +FEATS = ["3MIN", "3MAX", "3MEAN"] +DYNAMIC_RANGE = 10000.0 # the Nyxus3D default + +# NIfTI-1 datatype codes and the numpy dtype each stores +FLOAT_TYPES = {16: np.float32, 64: np.float64} + + +def _write_float_nifti(path, values, datatype): + """Writes an 8x8x8 real-valued NIfTI with no header rescale.""" + h = bytearray(HEADER_SOURCE.read_bytes()[:352]) # 348 header + 4 extender + dtype = FLOAT_TYPES[datatype] + struct.pack_into(" f["3MIN"], "the volume collapsed onto one grey level" diff --git a/tests/python/test_3d_nifti_offset_map_mechanics.py b/tests/python/test_3d_nifti_offset_map_mechanics.py index 0c5069299..5032a4cf7 100644 --- a/tests/python/test_3d_nifti_offset_map_mechanics.py +++ b/tests/python/test_3d_nifti_offset_map_mechanics.py @@ -14,8 +14,9 @@ *Non-finite voxels.* A float volume is free to hold NaN -- a masked-out background, most often -- and converting one to an unsigned grey level is undefined. The loader stores grey level 0 for it, the -convention every load-time map uses. ``ct3d_nan.nii`` is float32, which has no stored integers to -keep, so it takes the offset map with or without the flag. +convention every load-time map uses. ``ct3d_nan.nii`` is float32 holding only whole values, so it +is not quantized as a volume with fractional values would be: it takes the offset map with or +without the flag. Kind: *mechanics* per tests/vetting/SPEC.md 2 -- these pin loader plumbing rather than feature values against a reference, and establish no vetting. diff --git a/tests/test_3d_firstorder_regression.h b/tests/test_3d_firstorder_regression.h index b16eec207..55dd51346 100644 --- a/tests/test_3d_firstorder_regression.h +++ b/tests/test_3d_firstorder_regression.h @@ -7,10 +7,9 @@ // Nyxus snapshots only: these values guard current behavior and establish no oracle vetting. static const ref_vals_map firstorder_3d_regression_ref_vals { - // Both ends are now in the volume's own domain, but the ROI range is a difference of integer - // grey levels while the volume range keeps its fraction, so this ratio still exceeds its - // implied upper bound of one by that truncation. - { "3COVERED_IMAGE_INTENSITY_RANGE", 1.0002043207290587 }, + // The ROI spans the same range the scan measures, [-1024, 2000], and ut_inten.nii holds whole + // values only, so numerator and denominator are the same difference and the ratio is one. + { "3COVERED_IMAGE_INTENSITY_RANGE", 1.0 }, // MATLAB mad(x,1) takes the median absolute deviation; Nyxus takes the mean absolute // deviation about its median. Keep regression-only until the intended definition is resolved. { "3MEDIAN_ABSOLUTE_DEVIATION", 507.12380480410445 }, diff --git a/tests/test_3d_nifti_mechanics.h b/tests/test_3d_nifti_mechanics.h index 86ee72247..704e52d5f 100644 --- a/tests/test_3d_nifti_mechanics.h +++ b/tests/test_3d_nifti_mechanics.h @@ -1,4 +1,5 @@ #include +#include #include "../src/nyx/raw_nifti.h" #include "../src/nyx/image_loader.h" #include "../src/nyx/helpers/fsystem.h" @@ -118,3 +119,190 @@ void test_3d_nifti_facade_stream_equivalence_mechanics() } il.close(); } + +// --------------------------------------------------------------------------- +// Real-valued NIfTI volumes (FLOAT32 / FLOAT64) and the load-time map. +// +// A volume holding fractional values takes the quantization a real-valued TIFF or OME-Zarr takes: +// the header rescale first, then a hard clamp to [fpmin, fpmax] spread over the target dynamic +// range, with the inverse recorded so the intensity families report the volume's own values. A +// float volume of whole values keeps the offset map instead, 1 grey level per unit. Every grey level +// reads back to within one quantization step, (fpmax - fpmin) / DR, below the value stored. The +// fixtures are 8x8x8 with mask3d.nii (all ones) as the ROI; see tests/data/hounsfield/README.md. +// +// ct3d_unit.nii : float32, no rescale, v(idx) = idx/511 over [0, 1] +// ct3d_float_slope.nii : float32, scl_slope 2, scl_inter -1024, stored idx + 0.25, so the +// physical value is 2*idx - 1023.5 over [-1023.5, -1.5] +// +// Narrowed to whole units instead, ct3d_unit.nii reads back as 0 everywhere but its last voxel, +// and every voxel of ct3d_float_slope.nii lands half a unit off. +// --------------------------------------------------------------------------- + +static fs::path nifti_hu_path (const char* name) +{ + fs::path p(__FILE__); + return p.parent_path() / "data" / "hounsfield" / name; +} + +// Scans one fixture volume against mask3d.nii, then reads it on the map the scan recorded. +static std::vector nifti_scan_and_load (SlideProps & p, const FpImageOptions & fpo) +{ + AnisotropyOptions aniso; + EXPECT_TRUE(Nyxus::scan_slide_props (p, 3, aniso, false, fpo, false)); + ImageLoader ilo; + EXPECT_TRUE(ilo.open (p, fpo)); + EXPECT_TRUE(ilo.load_tile ((size_t)0, (size_t)0)); + std::vector buf = ilo.get_int_tile_buffer(); + ilo.close(); + return buf; +} + +// The raw loader is what tells the scan a volume is real-valued, and a FLOAT32/FLOAT64 volume says +// so only when a finite voxel's header-rescaled value carries a fraction. One of whole values keeps +// 1 grey level == 1 intensity unit, and an integer datatype never says so, whatever its rescale. +void test_3d_nifti_float_reports_real_valued_voxels_mechanics() +{ + EXPECT_TRUE(RawNiftiLoader (nifti_hu_path("ct3d_unit.nii").string()).get_fp_pixels()); // float32 over [0, 1] + EXPECT_TRUE(RawNiftiLoader (nifti_hu_path("ct3d_float_slope.nii").string()).get_fp_pixels()); // float32, x.5 after the rescale + + // float32 of whole values, NaN and both infinities among them + EXPECT_FALSE(RawNiftiLoader (nifti_hu_path("ct3d_nan.nii").string()).get_fp_pixels()); + + fs::path p(__FILE__); + fs::path f64 = p.parent_path() / "data" / "nifti" / "signal" / "signal1.nii"; + ASSERT_TRUE(fs::exists(f64)) << f64.string(); + EXPECT_FALSE(RawNiftiLoader (f64.string()).get_fp_pixels()); // float64 of whole values + + EXPECT_FALSE(RawNiftiLoader (nifti_hu_path("ct3d_frac.nii").string()).get_fp_pixels()); // int16, scl_slope 0.5 + EXPECT_FALSE(RawNiftiLoader (nifti_hu_path("mask3d.nii").string()).get_fp_pixels()); // uint8 +} + +// A volume normalised to [0, 1] keeps its fraction: 512 distinct values become 512 distinct grey +// levels over [0, 10000], and each reads back to within 1e-4 of the value stored. +void test_3d_nifti_float_unit_range_quantized_mechanics() +{ + SlideProps p (nifti_hu_path("ct3d_unit.nii").string(), nifti_hu_path("mask3d.nii").string()); + FpImageOptions fpo; + std::vector buf = nifti_scan_and_load (p, fpo); + ASSERT_GE(buf.size(), 512u); + + EXPECT_TRUE(p.fp_phys_pivoxels); + ASSERT_EQ((int)p.inten_map, (int)IntenMap::quantized); + EXPECT_DOUBLE_EQ(p.inten_offset, 0.0); + EXPECT_DOUBLE_EQ(p.inten_scale, 1.0 / 10000.0); + + const double step = p.inten_scale; + for (size_t idx = 0; idx < 512; idx++) + { + double v = (double)(float)((double)idx / 511.0); // the float32 the file stores + EXPECT_NEAR(p.to_source_intensity (buf[idx]), v, step) << "idx " << idx; + } + EXPECT_EQ(buf[0], 0u); + EXPECT_EQ(buf[511], 10000u); + EXPECT_EQ(std::set(buf.begin(), buf.begin() + 512).size(), 512u); + + // the forward map lands on the grey level the loader stored + EXPECT_EQ(p.to_grey_level (p.max_preroi_inten), buf[511]); +} + +// The header rescale applies before the quantization, so the window is in physical units: the +// scan measures [-1023.5, -1.5] on the rescaled values, and the loader has to rescale each voxel +// the same way before clamping it into that window. +void test_3d_nifti_float_slope_quantized_mechanics() +{ + SlideProps p (nifti_hu_path("ct3d_float_slope.nii").string(), nifti_hu_path("mask3d.nii").string()); + FpImageOptions fpo; + std::vector buf = nifti_scan_and_load (p, fpo); + ASSERT_GE(buf.size(), 512u); + + ASSERT_EQ((int)p.inten_map, (int)IntenMap::quantized); + EXPECT_DOUBLE_EQ(p.inten_offset, -1023.5); + EXPECT_DOUBLE_EQ(p.inten_scale, 1022.0 / 10000.0); + + const double step = p.inten_scale; + for (size_t idx = 0; idx < 512; idx++) + { + double physical = 2.0 * (double) idx - 1023.5; + EXPECT_NEAR(p.to_source_intensity (buf[idx]), physical, step) << "idx " << idx; + } + EXPECT_EQ(buf[0], 0u); + EXPECT_EQ(buf[511], 10000u); +} + +// --fpimgmin / --fpimgmax / --fpimgdr (min_intensity / max_intensity / dynamic_range in Python) +// name the window instead of the scanned range. [0, 2] over 100 grey levels puts the volume's +// [0, 1] on levels 0..50, each 0.02 wide. +void test_3d_nifti_float_fpimage_window_mechanics() +{ + SlideProps p (nifti_hu_path("ct3d_unit.nii").string(), nifti_hu_path("mask3d.nii").string()); + FpImageOptions fpo; + fpo.raw_min_intensity = "0"; + fpo.raw_max_intensity = "2"; + fpo.raw_target_dyn_range = "100"; + ASSERT_TRUE(fpo.parse_input()); + std::vector buf = nifti_scan_and_load (p, fpo); + ASSERT_GE(buf.size(), 512u); + + ASSERT_EQ((int)p.inten_map, (int)IntenMap::quantized); + EXPECT_DOUBLE_EQ(p.inten_offset, 0.0); + EXPECT_DOUBLE_EQ(p.inten_scale, 2.0 / 100.0); + + EXPECT_EQ(buf[0], 0u); + EXPECT_EQ(buf[255], 24u); // 100 * (255/511) / 2 = 24.95 + EXPECT_EQ(buf[511], 50u); + for (size_t idx = 0; idx < 512; idx++) + { + double v = (double)(float)((double)idx / 511.0); + EXPECT_NEAR(p.to_source_intensity (buf[idx]), v, 0.02) << "idx " << idx; + } +} + +// The readings of a NIfTI that must not take the quantized map. Under --preserve-hu a volume with +// fractional values takes the offset map, rounding to whole units; a float volume of whole values +// takes the offset map and reads back exactly; an integer volume with a header rescale takes the +// stored map; and a mask volume, read with the rescale off and no quantization, keeps its voxels +// as labels even when they are stored as floats. +void test_3d_nifti_float_not_quantized_mechanics() +{ + { + SlideProps p (nifti_hu_path("ct3d_nan.nii").string(), nifti_hu_path("mask3d.nii").string()); + FpImageOptions fpo; + std::vector buf = nifti_scan_and_load (p, fpo); + ASSERT_GE(buf.size(), 512u); + EXPECT_FALSE(p.fp_phys_pivoxels); + ASSERT_EQ((int)p.inten_map, (int)IntenMap::offset); + EXPECT_DOUBLE_EQ(p.inten_scale, 1.0); + EXPECT_DOUBLE_EQ(p.inten_offset, 0.0); + EXPECT_EQ(buf[100], 100u); // v(idx) = idx, stored as it is + EXPECT_EQ(buf[511], 511u); + EXPECT_EQ(buf[3], 0u); // NaN + } + { + SlideProps p (nifti_hu_path("ct3d_unit.nii").string(), nifti_hu_path("mask3d.nii").string()); + p.preserve_hu = true; + FpImageOptions fpo; + std::vector buf = nifti_scan_and_load (p, fpo); + ASSERT_GE(buf.size(), 512u); + ASSERT_EQ((int)p.inten_map, (int)IntenMap::offset); + EXPECT_DOUBLE_EQ(p.inten_scale, 1.0); + EXPECT_EQ(buf[255], 0u); // 0.499 rounds down + EXPECT_EQ(buf[256], 1u); // 0.501 rounds up + EXPECT_EQ(buf[511], 1u); + } + { + SlideProps p (nifti_hu_path("ct3d_frac.nii").string(), nifti_hu_path("mask3d.nii").string()); + FpImageOptions fpo; + std::vector buf = nifti_scan_and_load (p, fpo); + ASSERT_GE(buf.size(), 512u); + EXPECT_FALSE(p.fp_phys_pivoxels); + ASSERT_EQ((int)p.inten_map, (int)IntenMap::stored); + EXPECT_EQ(buf[511], 511u); // the stored integer itself + } + { + NiftiLoader ldr (nifti_hu_path("ct3d_unit.nii").string(), 0.0, false); // as ImageLoader opens a mask + auto t = std::make_shared> (512); + ASSERT_NO_THROW(ldr.loadTileFromFile (t, 0, 0, 0, 0/*channel*/, 0/*timeframe*/, 0)); + EXPECT_EQ((*t)[510], 0u); + EXPECT_EQ((*t)[511], 1u); + } +} diff --git a/tests/test_all.cc b/tests/test_all.cc index 4f9f4b37d..f7fa47e4f 100644 --- a/tests/test_all.cc +++ b/tests/test_all.cc @@ -4706,6 +4706,26 @@ TEST(TEST_NYXUS, TEST_3D_NIFTI_FACADE_STREAM_EQUIVALENCE_MECHANICS) { ASSERT_NO_THROW (test_3d_nifti_facade_stream_equivalence_mechanics()); } +TEST(TEST_NYXUS, TEST_3D_NIFTI_FLOAT_REPORTS_REAL_VALUED_VOXELS_MECHANICS) { + ASSERT_NO_THROW (test_3d_nifti_float_reports_real_valued_voxels_mechanics()); +} + +TEST(TEST_NYXUS, TEST_3D_NIFTI_FLOAT_UNIT_RANGE_QUANTIZED_MECHANICS) { + ASSERT_NO_THROW (test_3d_nifti_float_unit_range_quantized_mechanics()); +} + +TEST(TEST_NYXUS, TEST_3D_NIFTI_FLOAT_SLOPE_QUANTIZED_MECHANICS) { + ASSERT_NO_THROW (test_3d_nifti_float_slope_quantized_mechanics()); +} + +TEST(TEST_NYXUS, TEST_3D_NIFTI_FLOAT_FPIMAGE_WINDOW_MECHANICS) { + ASSERT_NO_THROW (test_3d_nifti_float_fpimage_window_mechanics()); +} + +TEST(TEST_NYXUS, TEST_3D_NIFTI_FLOAT_NOT_QUANTIZED_MECHANICS) { + ASSERT_NO_THROW (test_3d_nifti_float_not_quantized_mechanics()); +} + //***** Nested-ROI table ***** TEST(TEST_NYXUS, TEST_2D_NESTED_ROI_TABLE_CARRIES_LEADING_COLUMNS_MECHANICS) { diff --git a/tests/vetting/audit/gldzm_3d_mirp_vetting_report.md b/tests/vetting/audit/gldzm_3d_mirp_vetting_report.md index 1e0e50a2d..0f7cd8bcd 100644 --- a/tests/vetting/audit/gldzm_3d_mirp_vetting_report.md +++ b/tests/vetting/audit/gldzm_3d_mirp_vetting_report.md @@ -189,26 +189,26 @@ fixture, and it is why the vetted cell uses a phantom on which neither tool disc | feature | Nyxus | MIRP | Nyxus / MIRP | |---|---:|---:|---:| -| `3GLDZM_SDE` | 0.470065379495797 | 0.3819882926480209 | 1.23 | -| `3GLDZM_LDE` | 7.433785474257402 | 11.231508735923956 | 0.662 | -| `3GLDZM_LGLZE` | 0.0004349083674222441 | 0.0014952243584138115 | 0.291 | -| `3GLDZM_HGLZE` | 2685.0693909588167 | 1920.8249709702682 | 1.4 | -| `3GLDZM_SDLGLE` | 0.00015407786939386234 | 0.0002162322321202084 | 0.713 | -| `3GLDZM_SDHGLE` | 1540.784139550179 | 1098.1125072487994 | 1.4 | -| `3GLDZM_LDLGLE` | 0.004580296296000341 | 0.03479098207358527 | 0.132 | -| `3GLDZM_LDHGLE` | 14203.744217420099 | 10881.709594663931 | 1.31 | -| `3GLDZM_GLNU` | 1349.3969192278446 | 1433.445626637142 | 0.941 | -| `3GLDZM_GLNUN` | 0.03309860235050761 | 0.01935467077093708 | 1.71 | -| `3GLDZM_ZDNU` | 10424.1403272094 | 14488.2767411088 | 0.719 | -| `3GLDZM_ZDNUN` | 0.25568790814612574 | 0.19562362265546165 | 1.31 | -| `3GLDZM_ZP` | 0.14855774836753732 | 0.2698737756529851 | 0.55 | -| `3GLDZM_GLV` | 84.66230769118728 | 227.68948600566614 | 0.372 | -| `3GLDZM_ZDV` | 2.094102783766484 | 3.2456304818289197 | 0.645 | -| `3GLDZM_ZDE` | 6.469785865699117 | 7.504620664827702 | 0.862 | +| `3GLDZM_SDE` | 0.470065379495797 | 0.38250154002645964 | 1.23 | +| `3GLDZM_LDE` | 7.433785474257402 | 11.278169871014846 | 0.659 | +| `3GLDZM_LGLZE` | 0.0004349083674222441 | 0.0014998057095406794 | 0.29 | +| `3GLDZM_HGLZE` | 2685.0693909588167 | 1922.2813742191936 | 1.4 | +| `3GLDZM_SDLGLE` | 0.00015407786939386234 | 0.0002164330280224201 | 0.712 | +| `3GLDZM_SDHGLE` | 1540.784139550179 | 1100.0882764114576 | 1.4 | +| `3GLDZM_LDLGLE` | 0.004580296296000341 | 0.0351714725853311 | 0.13 | +| `3GLDZM_LDHGLE` | 14203.744217420099 | 10895.842513723263 | 1.3 | +| `3GLDZM_GLNU` | 1349.3969192278446 | 1432.2585381682486 | 0.942 | +| `3GLDZM_GLNUN` | 0.03309860235050761 | 0.01936478919131782 | 1.71 | +| `3GLDZM_ZDNU` | 10424.1403272094 | 14472.563857115816 | 0.72 | +| `3GLDZM_ZDNUN` | 0.25568790814612574 | 0.1956756693588034 | 1.31 | +| `3GLDZM_ZP` | 0.14855774836753732 | 0.26950938666044777 | 0.551 | +| `3GLDZM_GLV` | 84.66230769118728 | 227.8830238502643 | 0.372 | +| `3GLDZM_ZDV` | 2.094102783766484 | 3.2813915565713567 | 0.638 | +| `3GLDZM_ZDE` | 6.469785865699117 | 7.50723228567559 | 0.862 | The residual has the shape a discretisation gap has: the grey-level-weighted features are the ones -far from 1 (`LDLGLE` 0.132x, `LGLZE` 0.291x, `GLV` 0.372x) and `GLNU`, which sums a marginal over -grey levels but weights nothing by them, is 0.941x. **`INVALID` as a vetting cell**, and +far from 1 (`LDLGLE` 0.13x, `LGLZE` 0.29x, `GLV` 0.372x) and `GLNU`, which sums a marginal over +grey levels but weights nothing by them, is 0.942x. **`INVALID` as a vetting cell**, and `matrix/gldzm3d.md` records it as such with this measurement beside it. ## Result at `gldzm3d.mirp_samelevels` -- the same grey levels diff --git a/tests/vetting/audit/ngldm_3d_mirp_vetting_report.md b/tests/vetting/audit/ngldm_3d_mirp_vetting_report.md index cbede1239..a582370c6 100644 --- a/tests/vetting/audit/ngldm_3d_mirp_vetting_report.md +++ b/tests/vetting/audit/ngldm_3d_mirp_vetting_report.md @@ -48,22 +48,22 @@ discretisation gap above, not the NGLDM. | feature | Nyxus | MIRP | Nyxus/MIRP | |---|---|---|---| | `3NGLDM_DCP` | 1 | 1 | 1 | -| `3NGLDM_LDE` | 0.15365 | 0.25594 | 0.600338 | -| `3NGLDM_HDE` | 40.6394 | 28.0738 | 1.44759 | -| `3NGLDM_LGLCE` | 0.000783908 | 0.0321849 | 0.0243564 | -| `3NGLDM_HGLCE` | 1873.25 | 1323.96 | 1.41488 | -| `3NGLDM_LDLGLE` | 7.80276e-05 | 0.000684901 | 0.113925 | -| `3NGLDM_LDHGLE` | 375.708 | 474.82 | 0.791263 | -| `3NGLDM_HDLGLE` | 0.056243 | 8.71408 | 0.00645427 | -| `3NGLDM_HDHGLE` | 44248.7 | 14942.8 | 2.96119 | -| `3NGLDM_GLNU` | 6480.48 | 4350.27 | 1.48967 | -| `3NGLDM_GLNUN` | 0.0236142 | 0.0158519 | 1.48967 | -| `3NGLDM_DCNU` | 32085.4 | 40745 | 0.787469 | -| `3NGLDM_DCNUN` | 0.116916 | 0.14847 | 0.787469 | -| `3NGLDM_GLV` | 153.096 | 350.171 | 0.437203 | -| `3NGLDM_DCV` | 14.6164 | 11.9476 | 1.22338 | -| `3NGLDM_DCENT` | 8.40569 | 8.67597 | 0.968847 | -| `3NGLDM_DCENE` | 0.00347501 | 0.00287482 | 1.20878 | +| `3NGLDM_LDE` | 0.15365 | 0.2554 | 0.601605 | +| `3NGLDM_HDE` | 40.6394 | 28.1829 | 1.44199 | +| `3NGLDM_LGLCE` | 0.000783908 | 0.0323217 | 0.0242533 | +| `3NGLDM_HGLCE` | 1873.25 | 1323.6 | 1.41527 | +| `3NGLDM_LDLGLE` | 7.80276e-05 | 0.000685271 | 0.113864 | +| `3NGLDM_LDHGLE` | 375.708 | 474.024 | 0.792593 | +| `3NGLDM_HDLGLE` | 0.056243 | 8.78468 | 0.00640239 | +| `3NGLDM_HDHGLE` | 44248.7 | 14987.7 | 2.95234 | +| `3NGLDM_GLNU` | 6480.48 | 4354.1 | 1.48836 | +| `3NGLDM_GLNUN` | 0.0236142 | 0.0158659 | 1.48836 | +| `3NGLDM_DCNU` | 32085.4 | 40667.7 | 0.788965 | +| `3NGLDM_DCNUN` | 0.116916 | 0.148189 | 0.788968 | +| `3NGLDM_GLV` | 153.096 | 350.048 | 0.437357 | +| `3NGLDM_DCV` | 14.6164 | 11.9967 | 1.21837 | +| `3NGLDM_DCENT` | 8.40569 | 8.67611 | 0.968832 | +| `3NGLDM_DCENE` | 0.00347501 | 0.00287305 | 1.20952 | The features weighted by the grey level rather than by the dependence count carry the largest ratios, which is the signature of a level-ladder difference: `LGLCE` sums the reciprocal square of the level diff --git a/tests/vetting/benchmarks.md b/tests/vetting/benchmarks.md index 7f1ab03f2..4504cac8b 100644 --- a/tests/vetting/benchmarks.md +++ b/tests/vetting/benchmarks.md @@ -36,6 +36,7 @@ Tests: `test_2d_firstorder_{matlab,pyradiomics,regression}.h`. | Files | `tests/data/nifti/phantoms/ut_inten.nii` + `tests/data/nifti/phantoms/ut_mask57.nii` | | ROI | label **57** | | Shape | 100×100×100 voxels at 1×1×1 spacing; the ROI is **274 432** voxels, ~27% of the volume | +| Values | float32 holding whole values only, `[-1024, 2000]`, so Nyxus loads it at one grey level per unit (a float NIfTI with fractional values would be quantized instead). The values are the floor of the phantom's original fractional ones, which is what every value pinned on it was computed from | | Why it exists | a solid single-label 3D ROI, large enough that a 13-direction texture matrix is well populated in every direction and small enough to featurise inside a unit test | Recipes: `firstorder3d.matlab_native`, `firstorder3d.regression_ut_phantom`, diff --git a/tests/vetting/not_covered.md b/tests/vetting/not_covered.md index bb7a27a7d..afc581b36 100644 --- a/tests/vetting/not_covered.md +++ b/tests/vetting/not_covered.md @@ -17,19 +17,19 @@ Regenerate by re-deriving both sets; keep this file in step with each reorg wave --- -## A. Test files no registry row references — 34 files, 124 test functions +## A. Test files no registry row references — 35 files, 131 test functions Both totals are the sum of the two tables below, counting each distinct file once and skipping any row struck through as closed, so they can be recomputed from the tables. -### A.1 Correctly absent — plumbing, fixtures and framework self-tests (27 files) +### A.1 Correctly absent — plumbing, fixtures and framework self-tests (28 files) These assert no feature value, so they have no `(feature × config × oracle)` row by construction (SPEC §1). Recording them here so their absence is a documented decision, not an oversight. | file | functions | why it has no row | |---|---:|---| -| `test_3d_nifti_mechanics.h` | 2 | NIfTI loader geometry / data access | +| `test_3d_nifti_mechanics.h` | 8 | NIfTI loader geometry / data access, and the load-time map of a float volume: quantized when its values carry a fraction, one grey level per unit when they are whole, the offset map under `--preserve-hu` | | `test_arrow_mechanics.h` | 4 | Arrow + Parquet writer plumbing, including an output path that cannot be opened | | `test_arrow_file_name_mechanics.h` | 1 | output-file naming rules | | `test_2d_glcm_mechanics.h` | 4 | guards the `GLCM_OFFSET` default and own grey depth (settings, not values) and the substitute for a ROI whose matrix cannot be held | @@ -42,6 +42,7 @@ These assert no feature value, so they have no `(feature × config × oracle)` r | `test_2d_nonfinite_pixels_mechanics.py` | 6 | a real-valued slide holding NaN or an infinity, segmented and whole-slide; spans the scan and every load-time map, so per-feature rows would be meaningless | | `test_2d_intensity_constant_roi_mechanics.py` | 4 | an ROI of one intensity end to end, on a mapped and on an identity-map slide, through the in-RAM and the out-of-core paths; pins the reported values, not an oracle | | `test_3d_nifti_offset_map_mechanics.py` | 4 | the NIfTI load-time maps on a fractional `scl_slope` — rounded on the offset map under `--preserve-hu`, exact on the stored map without it — and a non-finite voxel taking grey level 0 either way; loader plumbing, no oracle | +| `test_3d_nifti_float_mechanics.py` | 1 | random float32 and float64 NIfTI volumes over [0, 1) and [0, 1000) read back to within one quantization step of numpy; loader plumbing, no oracle | | `test_roi_blacklist_mechanics.h` | 1 | ROI blacklisting | | `test_2d_moments_mechanics.h` | 1 | the whole-slide weighting on a slide one pixel wide or high whose box ends on its last pixel, so its contour has fewer than 4 distinct corners; a degenerate-input guard that a file run, whose whole-slide box ends one past the last pixel, never reaches | | `test_2d_contour_mechanics.h` | 1 | `BorderedContour` refuses to bind a temporary pixel vector; a compile-time property of the type | @@ -516,26 +517,20 @@ A drift guard pins what the code produces. When that value is outside the range definition allows, the pin is still the right thing to assert — it detects change — but the row is not merely "unvetted", it is known-wrong, and the registry says so in `flag`. -| feature | pinned | bound | why | -|---|---|---|---| -| 3D `3COVERED_IMAGE_INTENSITY_RANGE` | 1.0002043207290587 | ≤ 1 | the ratio compares two different intensity domains | - -`3d_intensity.cpp` computes it as `double(r.aux_max - r.aux_min) / (p.max_preroi_inten - -p.min_preroi_inten)`, then multiplies the numerator by the slide's `inten_scale` so both ends are -in the slide's own domain. That closes the half of the mismatch that came from a quantized -floating-point slide, but not the **truncation**: the numerator is still a difference of integer -grey levels. `ut_inten.nii` stores `[-1024, 2000]`, so the ROI's grey levels span `[1024, 3024]` -and the numerator is exactly `2000`, while the denominator comes from `SlideProps` filled by -`RawNiftiLoader::get_dpequiv_pixel` with the unrounded values, giving `2000 - 0.40855798 = -1999.5914`. - -An ROI cannot span more than the slide containing it, so the bound is definitional and the excess -is the truncation, not a rounding artifact. Asserting the bound today would still fail; a bound is -an invariant rather than a snapshot and belongs in an `_invariant` file once both ends are measured -the same way. Until then `test_3d_firstorder_regression.h` pins the current value and states why. - -This supersedes the earlier registry note that the segmented workflow never sets `roi.slide_idx`. -It is set on this path; the value is not the fallback `1`. +No feature is in this section today. + +3D `3COVERED_IMAGE_INTENSITY_RANGE` was, at 1.0002043207290587 against a bound of 1. It is pinned at +exactly 1 now. `3d_intensity.cpp` computes it as `double(r.aux_max - r.aux_min) / +(p.max_preroi_inten - p.min_preroi_inten)`, with the numerator multiplied by the slide's +`inten_scale` so both ends are in the slide's own domain. Both ends are now also measured at the +same resolution. A floating-point NIfTI volume with fractional values is quantized, so the ROI's +grey-level range maps back onto the scanned range. A floating-point NIfTI of whole values keeps one +grey level per unit, with nothing dropped. `ut_inten.nii` holds whole values over `[-1024, 2000]`, +and the ROI spans all of it. The bound is an invariant rather than a snapshot, and it belongs in an +`_invariant` file; until one exists, `test_3d_firstorder_regression.h` pins the value. + +The segmented workflow sets `roi.slide_idx` on this path, so the denominator is the slide's own +range, not the fallback `1`. --- diff --git a/tests/vetting/oracle_coverage.csv b/tests/vetting/oracle_coverage.csv index b40ea72f9..771b3d478 100644 --- a/tests/vetting/oracle_coverage.csv +++ b/tests/vetting/oracle_coverage.csv @@ -654,7 +654,7 @@ dim,feature,family,status,oracle,agreement,config_recipe,tolerance,current_test, 2D,IH_NUM_BINS,intensity_histogram,vetted,analytic,agreed,ih.ibsi_fbn,rel=1e-9,test_2d_intensity_histogram_regression.h;test_2d_intensity_histogram_analytic.py;test_2d_intensity_histogram_analytic.h,,,,in-tree,"No tool reports this quantity - MIRP and PyRadiomics both stay in the discretised (_IDX) domain - so the oracle is the closed form, evaluated by tests/vetting/oracles/gen_intensity_histogram_analytic.py from the published definitions rather than from intensity_histogram.cpp, and asserted at rel<=1e-9 in test_2d_intensity_histogram_analytic.h (worst measured 7.5e-15). Evidence: tests/vetting/audit/intensity_histogram_2d_*_vetting_report.md.",, 2D,IH_BIN_SIZE,intensity_histogram,vetted,analytic,agreed,ih.ibsi_fbn,rel=1e-9,test_2d_intensity_histogram_regression.h;test_2d_intensity_histogram_analytic.py;test_2d_intensity_histogram_analytic.h,,,,in-tree,"No tool reports this quantity - MIRP and PyRadiomics both stay in the discretised (_IDX) domain - so the oracle is the closed form, evaluated by tests/vetting/oracles/gen_intensity_histogram_analytic.py from the published definitions rather than from intensity_histogram.cpp, and asserted at rel<=1e-9 in test_2d_intensity_histogram_analytic.h (worst measured 7.5e-15). Evidence: tests/vetting/audit/intensity_histogram_2d_*_vetting_report.md.",, 3D,3COV,firstorder,vetted,matlab,agreed,firstorder3d.matlab_native,rel=1e-3,test_3d_firstorder_matlab.h,test_3d_firstorder_matlab.h,mirp:stat_cov (IBSI 7TET; no pyradiomics equiv),,in-tree,"MATLAB R2026a std(x,0)/mean(x)=0.29486207043457396; direct definition from MATLAB built-ins.",TEST_NYXUS.TEST_3D_FIRSTORDER_MATLAB,bench_ut57_3d -3D,3COVERED_IMAGE_INTENSITY_RANGE,firstorder,regression,,,firstorder3d.regression_ut_phantom,rel=1e-6,test_3d_firstorder_regression.h,test_3d_firstorder_regression.h,none (Nyxus-specific: (roi_max-roi_min)/(slide_max-slide_min)),bound-violation,in-tree,"Regression only: the ROI range is measured in the loader's truncated integer domain while the slide range is measured in the stored float domain, producing 1.0002043207290587 and exceeding the definition's implied upper bound of one.",TEST_NYXUS.TEST_3D_FIRSTORDER_COVERED_IMAGE_INTENSITY_RANGE_REGRESSION,bench_ut57_3d +3D,3COVERED_IMAGE_INTENSITY_RANGE,firstorder,regression,,,firstorder3d.regression_ut_phantom,rel=1e-6,test_3d_firstorder_regression.h,test_3d_firstorder_regression.h,none (Nyxus-specific: (roi_max-roi_min)/(slide_max-slide_min)),,in-tree,"Regression only: Nyxus-specific ratio with no external oracle. On the whole-valued ut_inten.nii the ROI spans the scanned range and the pinned value is 1.",TEST_NYXUS.TEST_3D_FIRSTORDER_COVERED_IMAGE_INTENSITY_RANGE_REGRESSION,bench_ut57_3d 3D,3ENERGY,firstorder,vetted,pyradiomics,agreed,firstorder3d.pyradiomics_bincount20,rel=1e-9,test_3d_firstorder_pyradiomics.h,test_3d_firstorder_pyradiomics.h,,,in-tree,PyRadiomics 3.0.1 pin reproduced by tests/vetting/oracles/gen_firstorder3d_pyradiomics.py; Nyxus agrees within rel=1e-9 (measured below 1e-12). Evidence: tests/vetting/audit/firstorder_3d_pyradiomics_vetting_report.md.,TEST_NYXUS.TEST_3D_FIRSTORDER_ENERGY_PYRADIOMICS,bench_compat_liver_3d 3D,3ENTROPY,firstorder,vetted,pyradiomics,agreed,firstorder3d.pyradiomics_bincount20,rel=1e-9,test_3d_firstorder_pyradiomics.h,test_3d_firstorder_pyradiomics.h,,,in-tree,PyRadiomics 3.0.1 pin reproduced by tests/vetting/oracles/gen_firstorder3d_pyradiomics.py; Nyxus agrees within rel=1e-9 (measured below 1e-12). Evidence: tests/vetting/audit/firstorder_3d_pyradiomics_vetting_report.md.,TEST_NYXUS.TEST_3D_FIRSTORDER_ENTROPY_PYRADIOMICS,bench_compat_liver_3d 3D,3EXCESS_KURTOSIS,firstorder,vetted,matlab,agreed,firstorder3d.matlab_native,rel=1e-3,test_3d_firstorder_matlab.h,test_3d_firstorder_matlab.h,pyradiomics/mirp (native 3D first-order; kurtosis -3),,in-tree,"MATLAB R2026a kurtosis(x,1)-3=-1.2127631603215849; direct convention conversion from the MATLAB built-in.",TEST_NYXUS.TEST_3D_FIRSTORDER_MATLAB,bench_ut57_3d diff --git a/tests/vetting/oracles/gen_firstorder3d_matlab.m b/tests/vetting/oracles/gen_firstorder3d_matlab.m index ab5572605..96ad1955f 100644 --- a/tests/vetting/oracles/gen_firstorder3d_matlab.m +++ b/tests/vetting/oracles/gen_firstorder3d_matlab.m @@ -21,10 +21,11 @@ mask = niftiread(fullfile(phantoms, 'ut_mask57.nii')); header_text = fileread(fullfile(repo_root, 'tests', 'test_3d_firstorder_matlab.h')); -% Match what Nyxus reports: the loader offsets a volume holding negative values by its floored -% minimum and truncates to integer grey levels, and the intensity family adds that offset back, -% so the reported domain is the volume's own with the truncation kept. Fixture setup, not a -% feature formula. +% Match what Nyxus reports for a volume of whole values: the loader offsets a volume holding +% negative values by its floored minimum and keeps one grey level per unit, and the intensity +% family adds that offset back, so the reported domain is the volume's own. ut_inten.nii holds +% whole values only, so the fix() below changes nothing on it; it is kept so the setup states the +% offset map exactly. Fixture setup, not a feature formula. intensity = double(intensity); offset = floor(min(intensity, [], 'all')); if offset < 0 diff --git a/tests/vetting/report_output.csv b/tests/vetting/report_output.csv index 213b9c4d9..fe8a765b7 100644 --- a/tests/vetting/report_output.csv +++ b/tests/vetting/report_output.csv @@ -654,7 +654,7 @@ dim,family,feature,claim_status,claim_oracle,claim_recipe,claim_tolerance,claim_ 2D,intensity_histogram,IH_NUM_BINS,vetted,analytic,ih.ibsi_fbn,rel=1e-9,agreed,,,,,in-tree,analytic,test_2d_intensity_histogram_phantom_analytic;test_2d_intensity_histogram_values_integer_domain_analytic,test_2d_intensity_histogram_float_domain_negative_min_regression;test_2d_intensity_histogram_float_domain_preserve_hu_regression;test_2d_intensity_histogram_float_domain_regression;test_2d_intensity_histogram_integer_domain_values_regression,,2,4,,,agree,feature,audit/intensity_histogram_2d_analytic_vetting_report.md,audit/intensity_histogram_2d_golden_regen.md,,"No tool reports this quantity - MIRP and PyRadiomics both stay in the discretised (_IDX) domain - so the oracle is the closed form, evaluated by tests/vetting/oracles/gen_intensity_histogram_analytic.py from the published definitions rather than from intensity_histogram.cpp, and asserted at rel<=1e-9 in test_2d_intensity_histogram_analytic.h (worst measured 7.5e-15). Evidence: tests/vetting/audit/intensity_histogram_2d_*_vetting_report.md." 2D,intensity_histogram,IH_BIN_SIZE,vetted,analytic,ih.ibsi_fbn,rel=1e-9,agreed,,,,,in-tree,analytic,test_2d_intensity_histogram_phantom_analytic;test_2d_intensity_histogram_values_integer_domain_analytic,test_2d_intensity_histogram_float_domain_negative_min_regression;test_2d_intensity_histogram_float_domain_preserve_hu_fpactive_regression;test_2d_intensity_histogram_float_domain_preserve_hu_regression;test_2d_intensity_histogram_float_domain_regression;test_2d_intensity_histogram_integer_domain_values_regression,,2,5,,,agree,feature,audit/intensity_histogram_2d_analytic_vetting_report.md,audit/intensity_histogram_2d_golden_regen.md,,"No tool reports this quantity - MIRP and PyRadiomics both stay in the discretised (_IDX) domain - so the oracle is the closed form, evaluated by tests/vetting/oracles/gen_intensity_histogram_analytic.py from the published definitions rather than from intensity_histogram.cpp, and asserted at rel<=1e-9 in test_2d_intensity_histogram_analytic.h (worst measured 7.5e-15). Evidence: tests/vetting/audit/intensity_histogram_2d_*_vetting_report.md." 3D,firstorder,3COV,vetted,matlab,firstorder3d.matlab_native,rel=1e-3,agreed,bench_ut57_3d,TEST_NYXUS.TEST_3D_FIRSTORDER_MATLAB,,mirp:stat_cov (IBSI 7TET; no pyradiomics equiv),in-tree,matlab,test_3d_firstorder_matlab,,,1,0,MATLAB R2026a std/mean,test_3d_firstorder_matlab,agree,row,audit/firstorder_3d_matlab_vetting_report.md,,matrix/firstorder3d.md,"MATLAB R2026a std(x,0)/mean(x)=0.29486207043457396; direct definition from MATLAB built-ins." -3D,firstorder,3COVERED_IMAGE_INTENSITY_RANGE,regression,,firstorder3d.regression_ut_phantom,rel=1e-6,,bench_ut57_3d,TEST_NYXUS.TEST_3D_FIRSTORDER_COVERED_IMAGE_INTENSITY_RANGE_REGRESSION,bound-violation,none (Nyxus-specific: (roi_max-roi_min)/(slide_max-slide_min)),in-tree,,,test_3d_firstorder_covered_image_intensity_range_regression,,0,1,Nyxus-specific slide/ROI ratio; snapshot only,test_3d_firstorder_covered_image_intensity_range_regression,agree,row,,,matrix/firstorder3d.md,"Regression only: the ROI range is measured in the loader's truncated integer domain while the slide range is measured in the stored float domain, producing 1.0002043207290587 and exceeding the definition's implied upper bound of one." +3D,firstorder,3COVERED_IMAGE_INTENSITY_RANGE,regression,,firstorder3d.regression_ut_phantom,rel=1e-6,,bench_ut57_3d,TEST_NYXUS.TEST_3D_FIRSTORDER_COVERED_IMAGE_INTENSITY_RANGE_REGRESSION,,none (Nyxus-specific: (roi_max-roi_min)/(slide_max-slide_min)),in-tree,,,test_3d_firstorder_covered_image_intensity_range_regression,,0,1,Nyxus-specific slide/ROI ratio; snapshot only,test_3d_firstorder_covered_image_intensity_range_regression,agree,row,,,matrix/firstorder3d.md,Regression only: Nyxus-specific ratio with no external oracle. On the whole-valued ut_inten.nii the ROI spans the scanned range and the pinned value is 1. 3D,firstorder,3ENERGY,vetted,pyradiomics,firstorder3d.pyradiomics_bincount20,rel=1e-9,agreed,bench_compat_liver_3d,TEST_NYXUS.TEST_3D_FIRSTORDER_ENERGY_PYRADIOMICS,,,in-tree,pyradiomics,test_3d_firstorder_energy_pyradiomics,,,1,0,native PyRadiomics Energy,test_3d_firstorder_energy_pyradiomics,agree,row,audit/firstorder_3d_pyradiomics_vetting_report.md,,matrix/firstorder3d.md,PyRadiomics 3.0.1 pin reproduced by tests/vetting/oracles/gen_firstorder3d_pyradiomics.py; Nyxus agrees within rel=1e-9 (measured below 1e-12). Evidence: tests/vetting/audit/firstorder_3d_pyradiomics_vetting_report.md. 3D,firstorder,3ENTROPY,vetted,pyradiomics,firstorder3d.pyradiomics_bincount20,rel=1e-9,agreed,bench_compat_liver_3d,TEST_NYXUS.TEST_3D_FIRSTORDER_ENTROPY_PYRADIOMICS,,,in-tree,pyradiomics,test_3d_firstorder_entropy_pyradiomics,,,1,0,native PyRadiomics Entropy at binCount=20,test_3d_firstorder_entropy_pyradiomics,agree,row,audit/firstorder_3d_pyradiomics_vetting_report.md,,matrix/firstorder3d.md,PyRadiomics 3.0.1 pin reproduced by tests/vetting/oracles/gen_firstorder3d_pyradiomics.py; Nyxus agrees within rel=1e-9 (measured below 1e-12). Evidence: tests/vetting/audit/firstorder_3d_pyradiomics_vetting_report.md. 3D,firstorder,3EXCESS_KURTOSIS,vetted,matlab,firstorder3d.matlab_native,rel=1e-3,agreed,bench_ut57_3d,TEST_NYXUS.TEST_3D_FIRSTORDER_MATLAB,,pyradiomics/mirp (native 3D first-order; kurtosis -3),in-tree,matlab,test_3d_firstorder_matlab,,,1,0,MATLAB R2026a kurtosis-3,test_3d_firstorder_matlab,agree,row,audit/firstorder_3d_matlab_vetting_report.md,,matrix/firstorder3d.md,"MATLAB R2026a kurtosis(x,1)-3=-1.2127631603215849; direct convention conversion from the MATLAB built-in."