diff --git a/docs/source/Math/f_2dmoments.rst b/docs/source/Math/f_2dmoments.rst index 57e62ae1..58906196 100644 --- a/docs/source/Math/f_2dmoments.rst +++ b/docs/source/Math/f_2dmoments.rst @@ -32,11 +32,8 @@ set again with every pixel also weighted by its distance to the ROI contour. with anisotropy the run can crash, a known defect (`#521 `__). The GPU's distance search computes the step with a single-precision logarithm. * - **Out-of-core** - - Yes, all 180 moments, with or without anisotropy, with the same values as in memory on - tiled TIFF files and on striped files of up to 1024 × 1024 pixels: the weighted moments are - measured against the same contour on both paths. On a larger striped file out-of-core - processing of this family raises an error or returns wrong values, depending on the ROI - (see :ref:`large-images-tiles`). + - Yes, all 180 moments, with or without anisotropy, with the same values as in memory: the + weighted moments are measured against the same contour on both paths. * - **IBSI** - None. IBSI defines no geometric moment features. @@ -222,8 +219,7 @@ Settings * - Memory limit - ``--ramLimit`` - ``Nyxus(..., ram_limit=...)`` - - ROIs whose footprint reaches the limit are computed out of core, with the same values; on - a striped file larger than 1024 × 1024 pixels it raises an error or changes the moments. + - ROIs whose footprint reaches the limit are computed out of core, with the same values. * - GPU - ``--useGpu=true`` - ``Nyxus(..., use_gpu_device=0)`` diff --git a/docs/source/Math/f_erosion.rst b/docs/source/Math/f_erosion.rst index 8e67f0e3..88495391 100644 --- a/docs/source/Math/f_erosion.rst +++ b/docs/source/Math/f_erosion.rst @@ -21,9 +21,7 @@ thin or ragged ones, so the count is a rough measure of the ROI's thickness in p :doc:`/gpu`), a known defect (`#520 `__). The GPU uses the same structuring element, the same pixel range and the same cap. * - **Out-of-core** - - Yes, with the same values as in memory on tiled TIFF files and on striped files of up to - 1024 × 1024 pixels. On a larger striped file out-of-core erosion raises an error or returns a - wrong value, depending on the ROI; see :ref:`large-images-tiles`. + - Yes, with the same values as in memory. * - **IBSI** - Neither feature has an IBSI identifier; IBSI defines no erosion count. diff --git a/docs/source/Math/f_fractal_dimension.rst b/docs/source/Math/f_fractal_dimension.rst index 10aec1cd..c12df5f2 100644 --- a/docs/source/Math/f_fractal_dimension.rst +++ b/docs/source/Math/f_fractal_dimension.rst @@ -21,9 +21,7 @@ rough or ragged shapes give larger values. * - **Out-of-core** - Yes. Out of core both features have their in-memory values: box counting follows the same rule for the padded side :math:`S`, and the divider walks the same borders, traced the same - way (the worked example gives 1.3732 and 1.1311 on both paths). On a striped image larger - than 1024 pixels in either direction out-of-core processing raises an error or returns wrong - values, depending on the ROI (see :ref:`large-images-tiles`). + way (the worked example gives 1.3732 and 1.1311 on both paths). * - **IBSI** - Neither feature has an IBSI identifier; IBSI defines no fractal dimension. diff --git a/docs/source/Math/f_gabor.rst b/docs/source/Math/f_gabor.rst index 192a4eda..85204026 100644 --- a/docs/source/Math/f_gabor.rst +++ b/docs/source/Math/f_gabor.rst @@ -25,9 +25,7 @@ orientation. from Python. The GPU values are not checked against the CPU values (see :ref:`gabor-possible-issues`). * - **Out-of-core** - - Yes, with the same values as in memory on tiled TIFF files and on striped files of up to - 1024 × 1024 pixels. On a larger striped file out-of-core processing of this family raises - an error or returns wrong values, depending on the ROI (see :ref:`large-images-tiles`). + - Yes, with the same values as in memory. * - **IBSI** - No identifier. IBSI defines no Gabor feature; its image-feature chapter names Gabor filters only as an image transformation. diff --git a/docs/source/Math/f_glcm.rst b/docs/source/Math/f_glcm.rst index e433b56f..f1c936f9 100644 --- a/docs/source/Math/f_glcm.rst +++ b/docs/source/Math/f_glcm.rst @@ -18,9 +18,7 @@ regularity that first-order intensity statistics cannot see. * - **GPU** - No; computed on the CPU in every build. * - **Out-of-core** - - Yes, with the same values as in memory on tiled TIFF files and on striped files of up to - 1024 × 1024 pixels. On a larger striped file out-of-core processing of this family raises - an error or returns wrong values, depending on the ROI (see :ref:`large-images-tiles`). + - Yes, with the same values as in memory. * - **IBSI** - 25 of the 30 features have an IBSI identifier. Four of the other 5 return the same value as one that does; ``GLCM_VARIANCE`` equals IBSI joint variance, which ``GLCM_JVAR`` matches only diff --git a/docs/source/Math/f_gldm.rst b/docs/source/Math/f_gldm.rst index 4bd915b9..40e8fe25 100644 --- a/docs/source/Math/f_gldm.rst +++ b/docs/source/Math/f_gldm.rst @@ -17,9 +17,7 @@ pixels with many same-level neighbours) or heterogeneous (few). * - **GPU** - No; computed on the CPU in every build. * - **Out-of-core** - - Yes, with the same values as in memory on tiled TIFF files and on striped files of up to - 1024 × 1024 pixels. On a larger striped file out-of-core processing of this family raises - an error or returns wrong values, depending on the ROI (see :ref:`large-images-tiles`). + - Yes, with the same values as in memory. * - **IBSI** - None. IBSI defines no GLDM; the family follows PyRadiomics. IBSI's neighbouring grey level dependence matrix with :math:`\alpha = 0` and :math:`\delta = 1` counts the same diff --git a/docs/source/Math/f_gldzm.rst b/docs/source/Math/f_gldzm.rst index da7f2b65..b370d88b 100644 --- a/docs/source/Math/f_gldzm.rst +++ b/docs/source/Math/f_gldzm.rst @@ -18,9 +18,7 @@ areas deep inside the ROI give large ones. * - **GPU** - No; computed on the CPU in every build. * - **Out-of-core** - - Yes, with the same values as in memory on tiled TIFF files and on striped files of up to - 1024 × 1024 pixels. On a larger striped file out-of-core processing of this family raises - an error or returns wrong values, depending on the ROI (see :ref:`large-images-tiles`). + - Yes, with the same values as in memory. * - **IBSI** - 16 of the 18 features have an IBSI identifier. Grey level mean and zone distance mean have none. diff --git a/docs/source/Math/f_glrlm.rst b/docs/source/Math/f_glrlm.rst index c75fbc2d..f0b770cd 100644 --- a/docs/source/Math/f_glrlm.rst +++ b/docs/source/Math/f_glrlm.rst @@ -18,9 +18,7 @@ texture is fine (many short runs) or coarse (few long runs). * - **GPU** - No; computed on the CPU in every build. * - **Out-of-core** - - Yes, with the same values as in memory on tiled TIFF files and on striped files of up to - 1024 × 1024 pixels. On a larger striped file out-of-core processing of this family raises - an error or returns wrong values, depending on the ROI (see :ref:`large-images-tiles`). + - Yes, with the same values as in memory. * - **IBSI** - All 16 features have an IBSI identifier. diff --git a/docs/source/Math/f_glszm.rst b/docs/source/Math/f_glszm.rst index 3cca2508..acd547c1 100644 --- a/docs/source/Math/f_glszm.rst +++ b/docs/source/Math/f_glszm.rst @@ -21,9 +21,7 @@ small zones; coarse or homogeneous textures give few large ones. level non-uniformity, grey level variance and zone variance do not follow the formulas on this page. * - **Out-of-core** - - Yes, with the same values as in memory on tiled TIFF files and on striped files of up to - 1024 × 1024 pixels. On a larger striped file out-of-core processing of this family raises - an error or returns wrong values, depending on the ROI (see :ref:`large-images-tiles`). + - Yes, with the same values as in memory. * - **IBSI** - All 16 features have an IBSI identifier. diff --git a/docs/source/Math/f_image_quality.rst b/docs/source/Math/f_image_quality.rst index 74d1e85c..a3dd9fa7 100644 --- a/docs/source/Math/f_image_quality.rst +++ b/docs/source/Math/f_image_quality.rst @@ -19,10 +19,8 @@ computed on the ROI's bounding box as a rectangular image. * - **GPU** - No; computed on the CPU in every build. * - **Out-of-core** - - Partly, with a label image only. On tiled TIFF files and on striped files of up to - 1024 × 1024 pixels (see :ref:`large-images-tiles`), ``FOCUS_SCORE`` and - ``LOCAL_FOCUS_SCORE`` have the same values as in memory, and so do the saturations unless - the ROI is constant. + - Partly, with a label image only. ``FOCUS_SCORE`` and ``LOCAL_FOCUS_SCORE`` have the same + values as in memory, and so do the saturations unless the ROI is constant. ``POWER_SPECTRUM_SLOPE`` and ``SHARPNESS`` are not computed out of core, a known defect (`#508 `__; see :ref:`imq-possible-issues`). In whole-image mode an image too large for memory is refused. * - **IBSI** diff --git a/docs/source/Math/f_intensity_histogram.rst b/docs/source/Math/f_intensity_histogram.rst index 49b2e490..5edb59ba 100644 --- a/docs/source/Math/f_intensity_histogram.rst +++ b/docs/source/Math/f_intensity_histogram.rst @@ -20,9 +20,7 @@ indices, as IBSI defines it, and over the bin-centre intensities, in the image's * - **GPU** - No; computed on the CPU in every build. * - **Out-of-core** - - Yes, with the same values as in memory on tiled TIFF files and on striped files of up to - 1024 × 1024 pixels; see :ref:`intensity-histogram-possible-issues` for larger striped files - and :ref:`large-images-tiles`. + - Yes, with the same values as in memory. * - **IBSI** - 23 features have an identifier from the IBSI intensity histogram features, which list 23 features: the 19 ``_IDX`` statistics other than ``IH_ROBUST_MEAN_IDX`` and the 4 gradient @@ -745,16 +743,6 @@ In-memory arrays are not in the input's units 1.84. Read from a file, the same image gives :math:`-9` and :math:`-7.16`. The ``_IDX`` features do not change. -Out-of-core ROIs of large striped TIFF files - A ROI processed out of core is read from the file again. For a striped (not tiled) TIFF more - than 1024 pixels high or wide, the pixels read back do not match the image, and most ``IH_*`` - features differ from the in-memory values; the minimum, maximum, range, bin size and number of - bins come from the first pass over the image and do not change. On a 1200 × 1200 image of four - quadrants of 100, 1100, 2100 and 3100, ``IH_MAX_GRADIENT`` was 1123200 in place of 360000 and - ``IH_MEAN_VAL`` 1635.0 in place of 1600. The intensity features are affected too; see - :ref:`intensity-possible-issues`. Tiled TIFF files of the same size, and striped files of - 1024 × 1024 pixels, give the in-memory values. - Validation ---------- diff --git a/docs/source/Math/f_morphology.rst b/docs/source/Math/f_morphology.rst index 26afc5cd..e2402944 100644 --- a/docs/source/Math/f_morphology.rst +++ b/docs/source/Math/f_morphology.rst @@ -26,9 +26,8 @@ intensity-weighted features, they use only the ROI's mask. - Partly. Out of core the contour is traced as in memory, every border of the ROI in image coordinates, so every contour-based feature, with or without anisotropy, has its in-memory value. The neighbour features are not computed: they are 0, and ``POLYGONALITY_AVE`` and - ``HEXAGONALITY_*`` are −1. On a striped file larger than 1024 × 1024 pixels out-of-core - processing raises an error or returns wrong values, depending on the ROI. See - :ref:`morphology-possible-issues`, :doc:`/large_images` and :ref:`large-images-tiles`. + ``HEXAGONALITY_*`` are −1. See :ref:`morphology-possible-issues` and + :doc:`/large_images`. * - **IBSI** - None. IBSI defines morphological features in 3D only (DHQ4), so no 2D code on this page has an identifier. @@ -1313,8 +1312,7 @@ Out of core 0, and an out-of-core ROI is also not seen as a neighbour by the ROIs computed in memory, so their neighbour features leave it out. ``POLYGONALITY_AVE`` and ``HEXAGONALITY_*``, which need the neighbour count, are −1 out of core. On the worked example every other code gives its - in-memory value; on striped files larger than 1024 × 1024 pixels see - :ref:`large-images-tiles`, and for the families that differ see :doc:`/large_images`. + in-memory value; for the families that differ see :doc:`/large_images`. Codes that need companions Several codes read values that another family computes, and are wrong when no code of that diff --git a/docs/source/Math/f_ngldm.rst b/docs/source/Math/f_ngldm.rst index cf043613..d9c0a35a 100644 --- a/docs/source/Math/f_ngldm.rst +++ b/docs/source/Math/f_ngldm.rst @@ -18,9 +18,7 @@ counts; fine textures give low ones. * - **GPU** - No; computed on the CPU in every build. * - **Out-of-core** - - Yes, with the same values as in memory on tiled TIFF files and on striped files of up to - 1024 × 1024 pixels (see :ref:`large-images-tiles` for larger striped files), except on a - constant ROI (see + - Yes, with the same values as in memory, except on a constant ROI (see :ref:`ngldm-possible-issues`). * - **IBSI** - 17 of the 19 features have an IBSI identifier. Grey level mean and dependence count mean diff --git a/docs/source/Math/f_ngtdm.rst b/docs/source/Math/f_ngtdm.rst index 89586506..397d8eae 100644 --- a/docs/source/Math/f_ngtdm.rst +++ b/docs/source/Math/f_ngtdm.rst @@ -17,13 +17,10 @@ neighbourhood averages. The features describe how coarse, busy or contrasted a t * - **GPU** - No; computed on the CPU in every build. * - **Out-of-core** - - Yes, with the same values as in memory on tiled TIFF files and on striped files of up to - 1024 × 1024 pixels, except in + - Yes, with the same values as in memory, except in IBSI mode: there only the first ROI that a ``Nyxus`` object or a command-line run computes out of core matches, and the later ones are wrong, a known defect (`#512 `__): the list - of grey levels is not cleared between out-of-core ROIs in IBSI mode. On a larger striped - file out-of-core processing of this family raises an error or returns wrong - values, depending on the ROI (see :ref:`large-images-tiles`). + of grey levels is not cleared between out-of-core ROIs in IBSI mode. * - **IBSI** - All 5 features have an IBSI identifier. diff --git a/docs/source/Math/f_pixel_intensity.rst b/docs/source/Math/f_pixel_intensity.rst index 75a2274b..6baaa76b 100644 --- a/docs/source/Math/f_pixel_intensity.rst +++ b/docs/source/Math/f_pixel_intensity.rst @@ -19,9 +19,7 @@ lies. * - **GPU** - No; computed on the CPU in every build. * - **Out-of-core** - - Yes, with the same values as in memory on tiled TIFF files and on striped files of up to - 1024 × 1024 pixels; see :ref:`intensity-possible-issues` for larger striped files and - :ref:`large-images-tiles`. + - Yes, with the same values as in memory. * - **IBSI** - 18 features have an identifier from the IBSI intensity-based statistics, which list 18 features. ``COV`` follows a different convention from IBSI; its entry says how. @@ -850,15 +848,6 @@ Covered range of in-memory images ``COVERED_IMAGE_INTENSITY_RANGE`` is not computed for arrays passed to ``Nyxus.featurize``: it is 0 in pandas and ``-0.0`` in Arrow and Parquet files. Write the image to a file to get it. -Out-of-core ROIs of large striped TIFF files - A ROI processed out of core is read from the file again. For a striped (not tiled) TIFF more - than 1024 pixels high or wide, the pixels read back do not match the image, and the features - computed from them differ from the in-memory values (``MIN``, ``MAX``, ``RANGE`` and - ``UNIFORMITY_PIU`` come from the first pass over the image and stay correct): on a - 1200 × 1200 image of four quadrants of 100, 1100, 2100 and 3100, ``MEAN`` was 4764.0 in place - of 1600. Tiled TIFF files of the same size, and striped files of 1024 × 1024 pixels, give the - in-memory values. - Grey levels above 65535 ``ENERGY`` and ``ROOT_MEAN_SQUARED`` square each pixel as a 32-bit unsigned integer, a known defect (`#490 `__), which diff --git a/docs/source/Math/f_radial_distribution.rst b/docs/source/Math/f_radial_distribution.rst index bd5326e8..fa72a830 100644 --- a/docs/source/Math/f_radial_distribution.rst +++ b/docs/source/Math/f_radial_distribution.rst @@ -19,10 +19,8 @@ unevenly the intensity is spread over the wedges. * - **GPU** - No; computed on the CPU in every build. * - **Out-of-core** - - Yes, with or without anisotropy, with the same values as in memory on tiled TIFF files and - on striped files of up to 1024 × 1024 pixels: the contour is the same on both paths. On a - larger striped file out-of-core processing raises an error or returns wrong values, - depending on the ROI (see :ref:`large-images-tiles`). + - Yes, with or without anisotropy, with the same values as in memory: the contour is the + same on both paths. * - **IBSI** - None. IBSI defines no radial distribution features. diff --git a/docs/source/Math/f_zernike.rst b/docs/source/Math/f_zernike.rst index baedd4b3..fbca30eb 100644 --- a/docs/source/Math/f_zernike.rst +++ b/docs/source/Math/f_zernike.rst @@ -18,9 +18,7 @@ patterns and high orders fine radial and angular detail. * - **GPU** - No; computed on the CPU in every build. * - **Out-of-core** - - Yes, with the same values as in memory on tiled TIFF files and on striped files of up to - 1024 × 1024 pixels. On a larger striped file out-of-core processing of this family raises - an error or returns wrong values, depending on the ROI (see :ref:`large-images-tiles`). + - Yes, with the same values as in memory. * - **IBSI** - None. IBSI defines no Zernike features. diff --git a/docs/source/large_images.rst b/docs/source/large_images.rst index 55e1d7e0..3ee3a6fd 100644 --- a/docs/source/large_images.rst +++ b/docs/source/large_images.rst @@ -181,9 +181,8 @@ the last digits (see :doc:`Math/f_morphology_3d`). A test volume with two ROIs, volume. This holds for inputs Nyxus can stream; see the end of :ref:`large-images-tiles` for those it refuses. -In 2D, every family matches the in-memory values on tiled TIFF files and on striped files of up to -1024 x 1024 pixels (see :ref:`large-images-tiles` for larger striped files), with these -exceptions: +In 2D, every family matches the in-memory values, on tiled and striped TIFF files of any size, with +these exceptions: * In IBSI mode 2D NGTDM matches only on the first ROI computed out of core, a known defect (`#512 `__; see :doc:`Math/f_ngtdm`). @@ -207,9 +206,7 @@ neighbor features for large ROIs, raise the RAM limit so that the ROIs are compu When a feature fails on an out-of-core 2D ROI, Python raises ``RuntimeError`` with the message ``Error while computing feature ... over oversized ROI ...``. The command line prints the same message and continues: the ROI's row is written with 0 in that feature's columns, and the run -exits with status 0. For example, on a 300 x 300 ROI of a striped 1200 x 1200 image, the contour -failed out of core with ``pixel ... lies outside the bounding box ... that sizes its buffer`` (see -:ref:`large-images-tiles`). After a command-line run with large ROIs, search the console output for +exits with status 0. After a command-line run with large ROIs, search the console output for ``Error while computing feature``. .. _large-images-tiles: @@ -226,30 +223,15 @@ same tile size; a pair with different tile sizes is refused with an error that s In memory and in whole-slide mode, tiling does not change the results: a 256 x 256 image with 66 ROIs, 30 of them crossing 64 x 64 tile borders, gave the same features tiled and striped. -Out of core, tiling does not change the results either: a tiled TIFF of several tiles gives the -values of a striped copy of the same image, and the in-memory values, for ROIs inside one tile and -for ROIs that span tiles. - -.. warning:: - - Out-of-core processing of a 2D striped TIFF larger than 1024 pixels in either direction, which - Nyxus reads as several 1024 x 1024 tiles, places the pixels of later tiles at wrong positions - and can read ROI pixels more than once, so the results depend on the family. Measured on a - 300 x 300 square and a 300 x 240 elliptical ROI inside the first 1024 x 1024 region of a - striped 1200 x 1200 image: - - * Every family that builds the ROI's pixel matrix or contour (the texture families, the - moments, morphology, the radial distribution, the fractal dimension, Zernike and Gabor) stops - when a misplaced pixel lies outside the ROI's bounding box: from Python the call raises - ``RuntimeError`` ("pixel ... lies outside the bounding box ... that sizes its buffer"); the - command line prints that error and leaves those features uncomputed. - * When every misplaced pixel stays inside the bounding box, the same families return wrong - values without an error. - * ROI pixels were read more than once: the intensity features built from sums, such as - ``MEAN``, differed from the in-memory values, while ``MIN`` and ``MAX`` kept them. - - Keep large 2D ROIs of such images in memory by raising the RAM limit, store the images as tiled - TIFF files, or split them so that each is at most 1024 x 1024 pixels. +Out of core, tiling does not change the results either: a tiled TIFF of several tiles, and a +striped TIFF larger than 1024 pixels in either direction, give the values of a copy of the same +image in the other layout, and the in-memory values, for ROIs inside one tile, for ROIs that span +tiles, and for ROIs in the partial tiles at the right and bottom edges of the image. The test suite +checks this on a striped 1120 x 1080 image with a ROI in the first tile, one across all four tiles +and one inside the partial corner tile: out of core, on the striped image and on a tiled copy, every +column of the intensity, intensity histogram, morphology, texture, moments, Gabor and Zernike groups +other than the neighbor features matches the in-memory run, and so do the intensity and morphology +columns with ``anisotropy_x`` set to 2. Out-of-core processing of a volume streams it one read at a time. When a single read of the intensity or label file returns the whole volume, as with a NIfTI file or an OME-Zarr array diff --git a/docs/source/troubleshooting.rst b/docs/source/troubleshooting.rst index 7cf4bc22..bfe183a5 100644 --- a/docs/source/troubleshooting.rst +++ b/docs/source/troubleshooting.rst @@ -204,10 +204,7 @@ physical memory available when Nyxus starts. A ROI whose estimated footprint rea is processed out of core, and the other ROIs are processed in batches that fit within it. In Python, ``get_params("ram_limit")`` reports the budget in megabytes. ``Nyxus3D`` accepts ``ram_limit`` in its constructor only, not in ``set_params``. :doc:`large_images` says which -features differ when a ROI is processed out of core. On a 2D striped TIFF larger than -1024 x 1024 pixels, out-of-core processing raises an error for most 2D families or gives wrong -values, depending on the family and the ROI; tiled TIFF files are processed correctly. See -:ref:`large-images-tiles`. +features differ when a ROI is processed out of core. Cost of the feature families ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/nyx/grayscale_tiff.h b/src/nyx/grayscale_tiff.h index 7e06af9d..831b9e83 100644 --- a/src/nyx/grayscale_tiff.h +++ b/src/nyx/grayscale_tiff.h @@ -509,6 +509,11 @@ class NyxusGrayscaleTiffStripLoader : public AbstractTileLoader startCol = indexColGlobalTile * tileWidth_, endCol = std::min((indexColGlobalTile + 1) * tileWidth_, fullWidth_); + // The buffer is always a full tile. A partial edge tile fills only its part inside the + // image, and the rest is set to 0 so that it holds no pixels or labels of an earlier tile. + if (endLayer - startLayer < tileDepth_ || endRow - startRow < tileHeight_ || endCol - startCol < tileWidth_) + std::fill (tileDataVec.begin(), tileDataVec.end(), (DataType) 0); + for (layer = startLayer; layer < endLayer; ++layer) { // OME-TIFF: the plane's directory per DimensionOrder; plain multi-page TIFF: directory = Z @@ -623,45 +628,35 @@ class NyxusGrayscaleTiffStripLoader : public AbstractTileLoader } #endif - /// @brief Private function to copy and cast the values + /// @brief Private function to copy and cast the values of one scanline's part inside the tile /// @tparam FileType Type inside the file /// @param src Piece of memory coming from libtiff /// @param dest_as_vector Feature extraction facing buffer to fill /// @param layer Destination layer /// @param row Destination row - /// @param startCol Starting column tile to copy - /// @param endCol End column tile to copy + /// @param start_col First image column to copy + /// @param end_col Image column past the last one to copy, at most the image's width template void copyRow(const void* src, std::vector& dest_as_vector, size_t layer, size_t row, size_t start_col, - size_t end_col) + size_t end_col) { // Get ahold of the raw pointer DataType* dest = dest_as_vector.data(); for (size_t col = start_col; col < end_col; col++) { - // Logic to prevent "noise" in images whose dimensions are smaller than the default tile buffer size 1024x1024 - DataType dataItem = (DataType) 0; // Zero-fill gaps - - // - Informative zone of the strip - if (layer < fullDepth_ && row < fullHeight_ && col < fullWidth_) - { - // The same load-time map the tile loader applies: an offset by the slide's floored - // minimum, which is what keeps a signed int16 CT pixel from wrapping to ~4.29e9 and - // blowing up the max-intensity-sized grey-bin/histogram allocation (#373), or the - // min-max rescale when the slide is real-valued. - dataItem = map_intensity ((double)((const FileType*)(src))[col]); - } - - // - Save the informative or zero-filled value + // The same load-time map the tile loader applies: an offset by the slide's floored + // minimum, which is what keeps a signed int16 CT pixel from wrapping to ~4.29e9 and + // blowing up the max-intensity-sized grey-bin/histogram allocation (#373), or the + // min-max rescale when the slide is real-valued. dest[ tileWidth_ * tileHeight_ * layer + tileWidth_ * row - + col - start_col] = dataItem; + + col - start_col] = map_intensity ((double)((const FileType*)(src))[col]); } } diff --git a/src/nyx/image_loader.cpp b/src/nyx/image_loader.cpp index fab2980c..533fcd9e 100644 --- a/src/nyx/image_loader.cpp +++ b/src/nyx/image_loader.cpp @@ -10,7 +10,6 @@ #include "raw_nifti.h" #include "ome/format_detect.h" // container-family classification for loader dispatch #include "mask_plane.h" -#include "volume_walk.h" ImageLoader::ImageLoader() {} diff --git a/src/nyx/image_loader.h b/src/nyx/image_loader.h index a8a41284..c12ff27b 100644 --- a/src/nyx/image_loader.h +++ b/src/nyx/image_loader.h @@ -9,6 +9,7 @@ #include "abs_tile_loader.h" #include "cli_fpimage_options.h" #include "slideprops.h" +#include "volume_walk.h" /// @brief Incapsulates access to an intensity and mask image file pair class ImageLoader diff --git a/src/nyx/image_loader1x.cpp b/src/nyx/image_loader1x.cpp index 9161160e..aa86d482 100644 --- a/src/nyx/image_loader1x.cpp +++ b/src/nyx/image_loader1x.cpp @@ -163,4 +163,14 @@ size_t ImageLoader1x::get_tile_height() size_t ImageLoader1x::get_tile_width() { return tw; +} + +size_t ImageLoader1x::get_full_width() +{ + return fw; +} + +size_t ImageLoader1x::get_full_height() +{ + return fh; } \ No newline at end of file diff --git a/src/nyx/image_loader1x.h b/src/nyx/image_loader1x.h index 0fca8d7f..e6a62670 100644 --- a/src/nyx/image_loader1x.h +++ b/src/nyx/image_loader1x.h @@ -28,6 +28,8 @@ class ImageLoader1x size_t get_tile_x(size_t pixel_col); size_t get_tile_y(size_t pixel_row); size_t get_within_tile_idx(size_t pixel_row, size_t pixel_col); + size_t get_full_width(); + size_t get_full_height(); private: std::unique_ptr> FL = nullptr; std::shared_ptr> ptr = nullptr; diff --git a/src/nyx/phase1.cpp b/src/nyx/phase1.cpp index 92959583..ce13fed4 100644 --- a/src/nyx/phase1.cpp +++ b/src/nyx/phase1.cpp @@ -32,10 +32,8 @@ namespace Nyxus // Read the tiff. The image loader is put in the open state in processDataset() size_t ntHor = L.get_num_tiles_hor(), ntVert = L.get_num_tiles_vert(), - fw = L.get_tile_width(), th = L.get_tile_height(), tw = L.get_tile_width(), - tileSize = L.get_tile_size(), fullwidth = L.get_full_width(), fullheight = L.get_full_height(); @@ -60,8 +58,8 @@ namespace Nyxus const std::shared_ptr>& spL = L.get_seg_tile_sptr(); bool wholeslide = env.singleROI; - // Iterate pixels - for (size_t i = 0; i < tileSize; i++) + // Iterate the tile's pixels inside the image + walk_tile_pixels (row, col, tw, th, fullwidth, fullheight, [&](size_t i, size_t x, size_t y) { // mask label if not in the wholeslide mode PixIntens label = 1; @@ -70,22 +68,15 @@ namespace Nyxus // Skip non-mask pixels if (! label) - continue; + return; // Merge all foreground labels into a single ROI if requested if (env.mergeLabels) label = 1; - int y = row * th + i / tw, - x = col * tw + i % tw; - - // Skip tile buffer pixels beyond the image's bounds - if (x >= fullwidth || y >= fullheight) - continue; - // Update pixel's ROI metrics - feed_pixel_2_metrics (env.uniqueLabels, env.roiData, x, y, dataI[i], label, sidx); - } + feed_pixel_2_metrics (env.uniqueLabels, env.roiData, (int) x, (int) y, dataI[i], label, sidx); + }); #ifdef WITH_PYTHON_H if (PyErr_CheckSignals() != 0) @@ -146,10 +137,8 @@ namespace Nyxus // Read the tiff. The image loader is put in the open state in processDataset() size_t ntHor = env.theImLoader.get_num_tiles_hor(), // tiles across a row ntVert = env.theImLoader.get_num_tiles_vert(), // tiles down a column - fw = env.theImLoader.get_tile_width(), th = env.theImLoader.get_tile_height(), tw = env.theImLoader.get_tile_width(), - tileSize = env.theImLoader.get_tile_size(), fullwidth = env.theImLoader.get_full_width(), fullheight = env.theImLoader.get_full_height(); @@ -174,28 +163,21 @@ namespace Nyxus auto dataI = env.theImLoader.get_int_tile_buffer(), dataL = env.theImLoader.get_seg_tile_buffer(); - // Iterate pixels - for (size_t i = 0; i < tileSize; i++) + // Iterate the tile's pixels inside the image + walk_tile_pixels (row, col, tw, th, fullwidth, fullheight, [&](size_t i, size_t x, size_t y) { // Skip non-mask pixels auto label = dataL[i]; if (!label) - continue; - - int y = row * th + i / tw, - x = col * tw + i % tw; - - // Skip tile buffer pixels beyond the image's bounds - if (x >= fullwidth || y >= fullheight) - continue; + return; // Collapse all the labels to one if single-ROI mde is requested if (env.singleROI) label = 1; // Update pixel's ROI metrics - feed_pixel_2_metrics_3D (env.uniqueLabels, env.roiData, x, y, z, dataI[i], label, sidx); // Updates 'uniqueLabels' and 'roiData' - } + feed_pixel_2_metrics_3D (env.uniqueLabels, env.roiData, (int) x, (int) y, z, dataI[i], label, sidx); // Updates 'uniqueLabels' and 'roiData' + }); #ifdef WITH_PYTHON_H if (PyErr_CheckSignals() != 0) diff --git a/src/nyx/phase2_25d.cpp b/src/nyx/phase2_25d.cpp index 6f21c0f7..92606aca 100644 --- a/src/nyx/phase2_25d.cpp +++ b/src/nyx/phase2_25d.cpp @@ -55,10 +55,8 @@ namespace Nyxus size_t ntHor = env.theImLoader.get_num_tiles_hor(), // tiles across a row ntVert = env.theImLoader.get_num_tiles_vert(), // tiles down a column - fw = env.theImLoader.get_tile_width(), th = env.theImLoader.get_tile_height(), tw = env.theImLoader.get_tile_width(), - tileSize = env.theImLoader.get_tile_size(), fullwidth = env.theImLoader.get_full_width(), fullheight = env.theImLoader.get_full_height(); @@ -85,34 +83,26 @@ namespace Nyxus auto dataI = env.theImLoader.get_int_tile_buffer(), dataL = env.theImLoader.get_seg_tile_buffer(); - // Iterate pixels - for (unsigned long i = 0; i < tileSize; i++) + // Iterate the tile's pixels inside the image + walk_tile_pixels (row, col, tw, th, fullwidth, fullheight, [&](size_t i, size_t x, size_t y) { // Skip non-mask pixels auto label = dataL[i]; if (!label) - continue; + return; // Skip this ROI if the label isn't in the pending set of a multi-ROI mode if (!env.singleROI && !std::binary_search(whiteList.begin(), whiteList.end(), label)) - continue; - - auto inten = dataI[i]; - int y = row * th + i / tw, - x = col * tw + i % tw; - - // Skip tile buffer pixels beyond the image's bounds - if (x >= fullwidth || y >= fullheight) - continue; + return; // Collapse all the labels to one if single-ROI mde is requested if (env.singleROI) label = 1; - // Cache this pixel + // Cache this pixel LR& r = env.roiData[label]; - feed_pixel_2_cache_3D_LR (x, y, z, dataI[i], r); - } + feed_pixel_2_cache_3D_LR ((int) x, (int) y, z, dataI[i], r); + }); VERBOSLVL2(env.get_verbosity_level(), // Show stayalive progress info diff --git a/src/nyx/phase2_2d.cpp b/src/nyx/phase2_2d.cpp index 1eed0266..cb4eaae2 100644 --- a/src/nyx/phase2_2d.cpp +++ b/src/nyx/phase2_2d.cpp @@ -123,10 +123,8 @@ namespace Nyxus // Read the tiffs size_t ntHor = ldr.get_num_tiles_hor(), ntVert = ldr.get_num_tiles_vert(), - fw = ldr.get_tile_width(), th = ldr.get_tile_height(), tw = ldr.get_tile_width(), - tileSize = ldr.get_tile_size(), fullwidth = ldr.get_full_width(), fullheight = ldr.get_full_height(); @@ -152,8 +150,8 @@ namespace Nyxus const std::shared_ptr>& spL = ldr.get_seg_tile_sptr(); bool wholeslide = env.singleROI; - // Iterate pixels - for (unsigned long i = 0; i < tileSize; i++) + // Iterate the tile's pixels inside the image + walk_tile_pixels (row, col, tw, th, fullwidth, fullheight, [&](size_t i, size_t x, size_t y) { // mask label if not in the wholeslide mode PixIntens label = 1; @@ -167,20 +165,12 @@ namespace Nyxus // Skip this ROI if the label isn't in the pending set of a multi-ROI mode if (! env.singleROI && ! std::binary_search(whiteList.begin(), whiteList.end(), label)) - continue; - - auto inten = dataI[i]; - int y = row * th + i / tw, - x = col * tw + i % tw; + return; - // Skip tile buffer pixels beyond the image's bounds - if (x >= fullwidth || y >= fullheight) - continue; - - // Cache this pixel + // Cache this pixel LR& r = env.roiData [label]; - feed_pixel_2_cache_LR (x, y, dataI[i], r); - } + feed_pixel_2_cache_LR ((int) x, (int) y, dataI[i], r); + }); VERBOSLVL2 (env.get_verbosity_level(), // Show stayalive progress info @@ -319,10 +309,8 @@ namespace Nyxus // Read the tiffs size_t ntHor = ldr.get_num_tiles_hor(), // tiles across a row ntVert = ldr.get_num_tiles_vert(), // tiles down a column - fw = ldr.get_tile_width(), th = ldr.get_tile_height(), tw = ldr.get_tile_width(), - tileSize = ldr.get_tile_size(), fullwidth = ldr.get_full_width(), fullheight = ldr.get_full_height(); @@ -346,20 +334,11 @@ namespace Nyxus // Get ahold of tile's pixel buffer const std::vector& dataI = ldr.get_int_tile_buffer(); - // Iterate pixels - for (unsigned long i = 0; i < tileSize; i++) + // Cache the tile's pixels inside the image + walk_tile_pixels (row, col, tw, th, fullwidth, fullheight, [&](size_t i, size_t x, size_t y) { - auto inten = dataI[i]; - int y = row * th + i / tw, - x = col * tw + i % tw; - - // Skip tile buffer pixels beyond the image's bounds - if (x >= fullwidth || y >= fullheight) - continue; - - // Cache this pixel - feed_pixel_2_cache_LR (x, y, dataI[i], vroi); - } + feed_pixel_2_cache_LR ((int) x, (int) y, dataI[i], vroi); + }); } return true; diff --git a/src/nyx/phase3.cpp b/src/nyx/phase3.cpp index 6e2fc5d5..0ec22281 100644 --- a/src/nyx/phase3.cpp +++ b/src/nyx/phase3.cpp @@ -147,7 +147,9 @@ namespace Nyxus size_t ntHor = env.theImLoader.get_num_tiles_hor(), // tiles across a row ntVert = env.theImLoader.get_num_tiles_vert(), // tiles down a column th = env.theImLoader.get_tile_height(), - tw = env.theImLoader.get_tile_width(); + tw = env.theImLoader.get_tile_width(), + fullwidth = env.theImLoader.get_full_width(), + fullheight = env.theImLoader.get_full_height(); for (size_t row = 0; row < ntVert; row++) for (size_t col = 0; col < ntHor; col++) { @@ -168,26 +170,22 @@ namespace Nyxus } auto& dataI = env.theImLoader.get_int_tile_buffer(); auto& dataL = env.theImLoader.get_seg_tile_buffer(); - for (unsigned long i = 0; i < env.theImLoader.get_tile_size(); i++) + walk_tile_pixels (row, col, tw, th, fullwidth, fullheight, [&](size_t i, size_t x, size_t y) { auto pixLabel = dataL[i]; // Skip blanks and other ROI's pixel if (pixLabel == 0 || pixLabel != r.label) - continue; - - // Pixel intensity and global position - auto intens = dataI[i]; - StatsInt y = (StatsInt) (row * th + i / tw), - x = (StatsInt) (col * tw + i % tw); + return; // Track the streamed extrema (see the aux_min/aux_max reset above) + auto intens = dataI[i]; r.aux_min = (std::min)(r.aux_min, (PixIntens)intens); r.aux_max = (std::max)(r.aux_max, (PixIntens)intens); // Feed the pixel to online features and helper objects - r.raw_pixels_NT.add_pixel(Pixel2(x, y, intens)); - } + r.raw_pixels_NT.add_pixel(Pixel2((StatsInt)x, (StatsInt)y, intens)); + }); } //=== Features requiring non-raster access to pixels diff --git a/src/nyx/python/nested_roi_py.cpp b/src/nyx/python/nested_roi_py.cpp index 4c28033c..6b0cd200 100644 --- a/src/nyx/python/nested_roi_py.cpp +++ b/src/nyx/python/nested_roi_py.cpp @@ -16,6 +16,7 @@ #include "../dirs_and_files.h" #include "../image_loader1x.h" #include "../results_cache.h" +#include "../volume_walk.h" #include "../helpers/fsystem.h" #include "../helpers/helpers.h" @@ -96,10 +97,10 @@ bool gatherRoisMetrics_H(const std::string& fpath, std::unordered_set & uni // Read the tiff size_t ntHor = imlo.get_num_tiles_hor(), // tiles across a row ntVert = imlo.get_num_tiles_vert(), // tiles down a column - fw = imlo.get_tile_width(), th = imlo.get_tile_height(), tw = imlo.get_tile_width(), - tileSize = imlo.get_tile_size(); + fullwidth = imlo.get_full_width(), + fullheight = imlo.get_full_height(); int cnt = 1; for (unsigned int row = 0; row < ntVert; row++) @@ -122,20 +123,17 @@ bool gatherRoisMetrics_H(const std::string& fpath, std::unordered_set & uni auto tileIdx = row * ntHor + col; auto data = imlo.get_tile_buffer(); - // 1st image -> uniqueLabels1, roiData1 - for (size_t i = 0; i < tileSize; i++) + // 1st image -> uniqueLabels1, roiData1, from the tile's pixels inside the image + Nyxus::walk_tile_pixels (row, col, tw, th, fullwidth, fullheight, [&](size_t i, size_t x, size_t y) { // Skip non-mask pixels auto label = data[i]; if (label != 0) { - int y = row * th + i / tw, - x = col * tw + i % tw; - // Update pixel's ROI metrics - Nyxus::feed_pixel_2_metrics_H(uniqueLabels, roiData, x, y, label, tileIdx); // Updates 'uniqueLabels' and 'roiData' + Nyxus::feed_pixel_2_metrics_H(uniqueLabels, roiData, (int) x, (int) y, label, tileIdx); // Updates 'uniqueLabels' and 'roiData' } - } + }); #ifdef WITH_PYTHON_H if (PyErr_CheckSignals() != 0) diff --git a/src/nyx/slideprops.cpp b/src/nyx/slideprops.cpp index 8a6e21d0..670e73a9 100644 --- a/src/nyx/slideprops.cpp +++ b/src/nyx/slideprops.cpp @@ -152,10 +152,8 @@ namespace Nyxus // Read the image/volume. The image loader is put in the open state in processDataset_XX_YY () size_t ntHor = ilo.get_num_tiles_hor(), // tiles across a row ntVert = ilo.get_num_tiles_vert(), // tiles down a column - fw = ilo.get_tile_width(), th = ilo.get_tile_height(), tw = ilo.get_tile_width(), - tileSize = ilo.get_tile_size(), fullwidth = ilo.get_full_width(), fullheight = ilo.get_full_height(); @@ -175,17 +173,10 @@ namespace Nyxus return false; } - // Iterate pixels + // Iterate the tile's pixels inside the image uint32_t prevMsk = 0; // the label of the pixel to the left in the tile's row - for (size_t i = 0; i < tileSize; i++) + walk_tile_pixels (row, col, tw, th, fullwidth, fullheight, [&](size_t i, size_t x, size_t y) { - int y = row * th + i / tw, - x = col * tw + i % tw; - - // Skip tile buffer pixels beyond the image's bounds - if (x >= fullwidth || y >= fullheight) - continue; - // the offset the loader will apply is driven by every pixel, so this runs // before the mask filter below double dxequiv_I = ilo.get_cur_tile_dpequiv_pixel(i); @@ -213,7 +204,7 @@ namespace Nyxus // Skip non-mask pixels if (!msk) - continue; + return; // dynamic range within- and off-ROI if (finite_I) @@ -239,7 +230,7 @@ namespace Nyxus // Initialize basic counters r.aux_area = 1; r.aux_min = r.aux_max = 0; //we don't have uint-cast intensities at this moment - r.init_aabb(x, y); + r.init_aabb((int) x, (int) y); // - not storing file names (r.segFname = segFile, r.intFname = intFile) but will do so in the future @@ -257,9 +248,9 @@ namespace Nyxus r.aux_area++; // save - r.update_aabb(x, y); + r.update_aabb((int) x, (int) y); } - } // scan tile + }); // scan tile // free tile buffers ilo.free_tile_buffers(); diff --git a/src/nyx/volume_walk.h b/src/nyx/volume_walk.h index 50f410e0..7dfa0b3a 100644 --- a/src/nyx/volume_walk.h +++ b/src/nyx/volume_walk.h @@ -70,6 +70,27 @@ namespace Nyxus } } + /// @brief Visit the pixels of tile (tile_row, tile_col) of a 2D image cut into tile_w x tile_h + /// tiles, row by row: pixel(i, x, y) receives each pixel of the tile that lies inside the + /// full_w x full_h image, at offset i of the tile buffer and position (x, y) in the image. A tile + /// buffer is always a full tile, and the part of an edge tile past the image's right or bottom + /// edge holds none of its pixels, so it is not visited. + template + void walk_tile_pixels (std::size_t tile_row, std::size_t tile_col, std::size_t tile_w, std::size_t tile_h, + std::size_t full_w, std::size_t full_h, Pixel&& pixel) + { + const std::size_t row0 = tile_row * tile_h, + col0 = tile_col * tile_w; + if (row0 >= full_h || col0 >= full_w) + return; + + const std::size_t valid_h = (std::min) (tile_h, full_h - row0), + valid_w = (std::min) (tile_w, full_w - col0); + for (std::size_t r = 0; r < valid_h; r++) + for (std::size_t c = 0; c < valid_w; c++) + pixel (r * tile_w + c, col0 + c, row0 + r); + } + /// @brief Walk a whole X*Y*Z volume, tile layer by tile layer (see walk_tile_layer). /// Featurization (ImageLoader::stream_volume_planes, tile layer by tile layer) and the prescan /// (RawImageLoader::for_each_voxel) all walk through here, so they see the same voxels. diff --git a/tests/python/test_2d_ooc_invariant.py b/tests/python/test_2d_ooc_invariant.py index e1fa3441..30d7dcf3 100644 --- a/tests/python/test_2d_ooc_invariant.py +++ b/tests/python/test_2d_ooc_invariant.py @@ -706,3 +706,98 @@ def test_2d_ooc_2d_tiled_matches_untiled_invariant(tmp_path): if np.isnan(p) != np.isnan(q) or abs(p - q) > 1e-6 * max(abs(p), abs(q), 1.0) + 1e-9: bad.append((lab, c, p, q)) assert not bad, "out-of-core features of a tiled TIFF diverge from its untiled copy: %r" % (bad[:8],) + + +# A striped TIFF is read in 1024 x 1024 tiles, so a 1120 x 1080 striped image is 2 tiles across and +# 2 down, and the last tile column (96 wide) and the last tile row (56 high) are partial. The image +# is not square, so swapping the two partial extents also moves pixels. +STRIPED_W, STRIPED_H, STRIPED_TILE = 1120, 1080, 1024 + + +def _make_striped_tiled_pairs(tmp_path): + """The same intensity and mask arrays written as striped TIFFs and as 64 x 64 tiled TIFFs. + + Label 1 lies inside the first tile. Label 2 spans all four tiles, so it has pixels in the + partial last column, the partial last row and the partial corner tile. Label 3 lies inside the + partial corner tile.""" + yy, xx = np.mgrid[0:STRIPED_H, 0:STRIPED_W] + inten = (1 + xx % 500 + 2 * (yy % 300)).astype(np.uint32) + mask = np.zeros((STRIPED_H, STRIPED_W), np.uint32) + mask[100:140, 200:250] = 1 + mask[((xx - 1024) / 30.0) ** 2 + ((yy - 1024) / 25.0) ** 2 <= 1.0] = 2 + mask[1040:1070, 1050:1100] = 3 + T = STRIPED_TILE + assert (mask[:T, :T] == 1).sum() == 40 * 50 + for part in (mask[:T, :T], mask[:T, T:], mask[T:, :T], mask[T:, T:]): + assert (part == 2).any() + assert (mask[T:, T:] == 3).sum() == 30 * 50 + + dirs = {} + for kind, kw in (("striped", {}), ("tiled", {"tile": (64, 64)})): + intdir = tmp_path / kind / "int" + segdir = tmp_path / kind / "seg" + intdir.mkdir(parents=True) + segdir.mkdir(parents=True) + tifffile.imwrite(str(intdir / "img.tif"), inten, **kw) + tifffile.imwrite(str(segdir / "img.tif"), mask, **kw) + with tifffile.TiffFile(str(intdir / "img.tif")) as tf: + assert tf.pages[0].is_tiled == (kind == "tiled") + dirs[kind] = (str(intdir) + os.sep, str(segdir) + os.sep) + return dirs + + +STRIPED_CASES = { + "intensity": (["*ALL_INTENSITY*"], 1.0), + "all_groups": (ALL_2D_FEATURE_GROUPS, 1.0), + "anisotropic": (["*ALL_INTENSITY*", "*ALL_MORPHOLOGY*"], 2.0), +} + + +@pytest.mark.parametrize("case", list(STRIPED_CASES)) +def test_2d_ooc_2d_striped_over_one_tile_matches_in_ram_invariant(tmp_path, case): + """Out-of-core featurization of a striped TIFF read as several 1024 x 1024 tiles equals the + in-memory one and the out-of-core featurization of a tiled copy, every column of every ROI. + + A tile buffer is always a full tile, and only the part inside the image holds this tile's + pixels, so the ROIs are placed to have pixels in the first tile and in each partial edge tile. + The intensity run compares the features built from sums over the ROI's pixels; the run of every + group adds the families that size a buffer from the ROI's box; the anisotropic run adds the scan + of the ROI resampled twice along x. + + The out-of-core runs are checked to have taken the out-of-core path: ROIs 2 and 3 lie a few + pixels apart, so in memory each has a neighbor, while out of core the neighbor features are not + computed and read 0.""" + feats, aniso = STRIPED_CASES[case] + dirs = _make_striped_tiled_pairs(tmp_path) + + frames = {} + for name, kind, ram_mb in (("ram", "striped", RAM_LIMIT_LARGE_MB), ("ooc", "striped", 0), + ("ooc_tiled", "tiled", 0)): + kw = {"anisotropy_x": aniso} if aniso != 1.0 else {} + nyx = nyxus.Nyxus(list(feats) + ["NUM_NEIGHBORS"], **kw) + _set_ram_limit_mb(nyx, ram_mb) + frames[name] = nyx.featurize_directory(*dirs[kind]).sort_values("ROI_label").reset_index(drop=True) + + df_ram = frames["ram"] + assert list(df_ram["ROI_label"]) == [1, 2, 3] + assert list(df_ram["NUM_NEIGHBORS"])[1:] == [1, 1] + for other in ("ooc", "ooc_tiled"): + assert list(frames[other]["NUM_NEIGHBORS"]) == [0, 0, 0], "%s did not run out of core" % other + cols = [c for c in _feature_cols(df_ram)[0] if not c.startswith(NEIGHBOR_FEATURE_PREFIXES)] + assert "MEAN" in cols + + # Tolerance: same build, same pixels, same formulas; only the summation order differs. + bad = [] + for other in ("ooc", "ooc_tiled"): + df = frames[other] + assert list(df["ROI_label"]) == [1, 2, 3] + for row in range(df_ram.shape[0]): + lab = int(df_ram["ROI_label"].iloc[row]) + for c in cols: + p = float(df_ram[c].iloc[row]) + q = float(df[c].iloc[row]) + if np.isnan(p) and np.isnan(q): + continue + if np.isnan(p) != np.isnan(q) or abs(p - q) > 1e-6 * max(abs(p), abs(q), 1.0) + 1e-9: + bad.append((other, lab, c, p, q)) + assert not bad, "out-of-core features of a striped TIFF over 1024 x 1024 diverge from in-RAM: %r" % (bad[:10],) diff --git a/tests/test_2d_tiff_strip_loader_mechanics.h b/tests/test_2d_tiff_strip_loader_mechanics.h index cb5f516b..1128b6be 100644 --- a/tests/test_2d_tiff_strip_loader_mechanics.h +++ b/tests/test_2d_tiff_strip_loader_mechanics.h @@ -46,6 +46,8 @@ // columns, whatever the strip height -- including a strip height that does not divide 1024, a // single strip spanning the whole image, and a grid that is past 1024 in height only; // - the same holds when the tiles are read out of order; +// - a partial edge tile zero-fills the part of the buffer the image does not reach, rather than +// keep the tile read before it there; // - a strip that does not decode makes the tile read throw, naming the row, rather than hand // back the previous contents of the scanline buffer as pixels. // @@ -335,9 +337,10 @@ void test_2d_raw_tiff_strip_loader_tile_out_of_grid_refused_mechanics() ldr.free_tile(); } -// Read tile (tr, tc) through the featurize-side loader and check every in-image pixel against -// nyxus_ut_strip_enc. Only the in-image part is checked: this loader writes the image's own rows -// and columns and leaves the rest of the buffer to the caller, which skips pixels past the image. +// Read tile (tr, tc) through the featurize-side loader and check the whole buffer: every in-image +// pixel against nyxus_ut_strip_enc, and the part of a partial edge tile past the image against 0. +// The buffer is reused from tile to tile, so a read that leaves that part alone hands the earlier +// tile's pixels to every caller that walks the buffer. static inline void nyxus_ut_assert_grayscale_strip_tile (NyxusGrayscaleTiffStripLoader& ldr, std::shared_ptr>& tile, const char* stem, size_t W, size_t H, size_t tr, size_t tc) { @@ -350,10 +353,13 @@ static inline void nyxus_ut_assert_grayscale_strip_tile (NyxusGrayscaleTiffStrip validH = (std::min) (th, H - row0), validW = (std::min) (tw, W - col0); - for (size_t r = 0; r < validH; r++) - for (size_t c = 0; c < validW; c++) - ASSERT_EQ ((*tile)[r * tw + c], nyxus_ut_strip_enc (col0 + c, row0 + r)) + for (size_t r = 0; r < th; r++) + for (size_t c = 0; c < tw; c++) + { + const uint32_t expected = (r < validH && c < validW) ? nyxus_ut_strip_enc (col0 + c, row0 + r) : 0u; + ASSERT_EQ ((*tile)[r * tw + c], expected) << stem << " tile (" << tr << "," << tc << ") at in-tile (" << c << "," << r << ")"; + } } // Walk the whole tile grid of a strip TIFF of the given geometry, in row-major order -- the order diff --git a/tests/vetting/not_covered.md b/tests/vetting/not_covered.md index bb7a27a7..f9bcb3cb 100644 --- a/tests/vetting/not_covered.md +++ b/tests/vetting/not_covered.md @@ -17,7 +17,7 @@ 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 — 34 files, 132 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. @@ -46,7 +46,7 @@ These assert no feature value, so they have no `(feature × config × oracle)` r | `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 | | `test_2d_tiff_loader_mechanics.h` | 1 | uint32 strip loader | -| `test_2d_ooc_invariant.py` | 9 | out-of-core == in-RAM equality; spans all features, per-feature rows would be meaningless | +| `test_2d_ooc_invariant.py` | 17 | out-of-core == in-RAM equality; spans all features, per-feature rows would be meaningless | | `test_2d_ooc_mechanics.py` | 1 | oversized-montage failure path | | `test_vetting_mechanics.py` | 5 | self-test of `check_coverage.py` | | `test_3d_coverage_common.h`, `test_3d_morphology_common.h`, `test_2d_moments_common.h`, `test_2d_morphology_common.h`, `test_2d_radial_common.h`, `test_2d_zernike_common.h` | 0 | shared fixtures; the kind belongs to the files that include them |