Skip to content

Propagate input dims/coords through true_color() (#3429)#3434

Merged
brendancol merged 3 commits into
mainfrom
deep-sweep-metadata-multispectral-2026-06-20
Jun 20, 2026
Merged

Propagate input dims/coords through true_color() (#3429)#3434
brendancol merged 3 commits into
mainfrom
deep-sweep-metadata-multispectral-2026-06-20

Conversation

@brendancol

Copy link
Copy Markdown
Contributor

Closes #3429.

true_color() hardcoded its output dims to ['y','x','band'] and rebuilt coords via r['y']/r['x']. That raised KeyError: 'y' on rasters with lat/lon (or row/col) dims and silently dropped non-spatial coords like spatial_ref. The other indices in the module already pass dims/coords/attrs straight through; this brings true_color() in line.

  • Derive the two spatial dims and their coords from the input DataArray, then append the band dim.
  • Pass input attrs and any extra coords through unchanged.

Backend coverage: numpy, cupy, dask+numpy, dask+cupy (all verified locally on a GPU host).

Test plan:

  • New test_true_color_preserves_non_yx_dims runs on all four backends with lat/lon input.
  • New test_true_color_preserves_extra_coords checks a spatial_ref coord survives.
  • Full test_multispectral.py passes (159 tests).

true_color() hardcoded output dims to ['y','x','band'] and rebuilt coords
via r['y']/r['x'], so it raised KeyError on lat/lon (or row/col) rasters
and dropped non-spatial coords like spatial_ref. Derive the two spatial
dims and coords from the input instead, then append the band dim, matching
how the other indices in the module preserve metadata.

Verified on numpy, cupy, dask+numpy, dask+cupy.

@brendancol brendancol left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

PR Review: Propagate input dims/coords through true_color() (#3429)

Blockers (must fix before merge)

None.

Suggestions (should fix, not blocking)

None.

Nits (optional improvements)

  • multispectral.py:1828 — if an input raster carried a coord literally named band, the 'band': [0, 1, 2, 3] entry would overwrite it. Far-fetched (a 2D raster with a non-dim coord called band), and the old code had the same collision, so it's not a regression. Not worth guarding against unless it ever comes up.

What looks good

  • The fix mirrors how every other index in the module preserves metadata, so true_color is no longer the odd one out.
  • Metadata is built in the public function after dispatch, so all four backends share one code path. The parametrized test runs numpy, cupy, dask+numpy, and dask+cupy with lat/lon input.
  • [*r.dims, 'band'] lines up with the (H, W, 4) output cube: r is validated 2D and the stack axis is appended last on every backend.
  • Tests assert on dims, coord values, and attrs, not just that the call returns. The spatial_ref test covers the dropped-coord half of the bug.

Checklist

  • Algorithm matches reference/paper (N/A, metadata-only change, pixel math untouched)
  • All implemented backends produce consistent results
  • NaN handling is correct (unchanged)
  • Edge cases are covered by tests (lat/lon dims, extra coord)
  • Dask chunk boundaries handled correctly (metadata built post-dispatch, no chunk interaction)
  • No premature materialization or unnecessary copies
  • Benchmark exists or is not needed (not needed, no perf change)
  • README feature matrix updated (N/A, no new function or backend change)
  • Docstrings present and accurate

@brendancol brendancol merged commit 667cdc8 into main Jun 20, 2026
10 checks passed
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.

true_color() hardcodes y/x dims and drops extra coords

1 participant