Skip to content

openeo.testing.results: compare large rasters block by block - #993

Open
dsamaey wants to merge 1 commit into
masterfrom
compare-large-rasters
Open

dsamaey wants to merge 1 commit into
masterfrom
compare-large-rasters

Conversation

@dsamaey

@dsamaey dsamaey commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #871

Problem

assert_job_results_allclose loads both rasters fully into memory and builds several full-size float64 intermediates (threshold, diff, masks, ...). In the APEx benchmarks (openeo-apex-benchmarks-cdse #217) this caused:

  • ramona_benin: MemoryError: Unable to allocate 17.3 GiB for an array with shape (1, 68355, 33904) in _compare_xarray_dataarray
  • sentinel1_sar_slc_preprocessing: process killed by the OOM killer (exit 137)

Approach

No new dependencies (e.g. dask/cubed): compare block by block with plain xarray/numpy, accumulating statistics incrementally.

  • _iter_blocks: yields aligned blocks of at most _COMPARE_BLOCK_SIZE (2**20) elements, each loaded once. Non-spatial dims (time, bands) are split first, then y, x is never split. Splitting on y only was ~4x slower on netCDF files that are chunked per band/time (as written by the openEO back-end), because of partial HDF5 chunk reads.
  • _compare_xarray_dataarray: nodata mismatch counts, bad pixel counts and the xarray.testing.assert_allclose check are done per block.
  • _compare_xarray_dataarray_xy: per (x, y) slice statistics are accumulated in _XYDiffStats (counts, min/max, mean/variance via Chan's parallel algorithm, diff bbox and a coarsened grid for the ascii art).
  • netCDF results are now opened lazily (xarray.open_dataset instead of load_dataset). Opened files are closed after comparison (_opened_xarray context manager) and re-opened per variable, to release the HDF5 chunk cache (otherwise RSS grew ~134 MB per variable).

Output for small arrays (single block) is unchanged. I verified this with a snapshot of all comparison messages for a set of mismatch scenarios, identical for block sizes 1, 7, 50, 100, 1000 and the default. With multiple blocks, the only difference is that the xarray "Left and right DataArray objects are not close" message shows the first differing block, rather than the full array.

Measurements

Case Before After
GeoTIFF 64M pixels (tracemalloc peak) not measured (each float64 intermediate is 512 MB) ~64 MB, ~3 s
netCDF 3 × 8000² int16, 2 vars (max RSS) 1836 MB ~530 MB
netCDF 10 bands × 6000² (max RSS / time) 2902 MB / 93 s 396 MB / 24 s

Limitations

  • Incompatible shapes or non-numeric data still fall back to a full xarray.testing.assert_allclose (existing behavior).
  • APEx needs a client release (or an install from master) to pick this up.

Tests

New tests in tests/testing/test_results.py: block-wise vs single-block equivalence (several block sizes, pixel tolerances, dimension orders), tracemalloc peak memory bounds for in-memory, GeoTIFF and netCDF inputs (including a check that the files get closed), and _iter_blocks split order.


This PR was drafted with the help of GitHub Copilot CLI (Claude).

Compare large (GeoTIFF/netCDF) job result rasters block by block,
reading files lazily, to bound memory usage and avoid out-of-memory
failures, as encountered in the APEx benchmarks (#871).

- Load netCDF lazily (`xarray.open_dataset`) and close opened files
  after comparison (re-open per variable to release HDF5 chunk caches).
- Iterate over blocks of at most `_COMPARE_BLOCK_SIZE` elements,
  splitting non-spatial dimensions (time, bands) first, then y.
- Accumulate per (x, y) slice difference statistics (counts, min/max,
  mean/var, bbox, coarsened ascii art) incrementally.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dsamaey
dsamaey requested a review from soxofaan December 3, 2026 10:24

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

testing: support large arrays

1 participant