Skip to content

Accept DataArray target grids - #75

Open
ZackaryLoevseth wants to merge 2 commits into
xarray-contrib:mainfrom
ZackaryLoevseth:fix/dataarray-target
Open

ZackaryLoevseth wants to merge 2 commits into
xarray-contrib:mainfrom
ZackaryLoevseth:fix/dataarray-target

Conversation

@ZackaryLoevseth

Copy link
Copy Markdown

Closes #64.

The regridding methods already work with most xr.DataArray targets, but an unnamed DataArray target with a time coordinate fails in validate_input: reset_coords() on an unnamed DataArray raises ValueError: cannot reset_coords with drop=False on an unnamed DataArray. This turns a DataArray target into its coordinates (target.coords.to_dataset()) before validation, so its values are never read, and widens the type hints and docstrings of the seven methods and validate_input to xr.DataArray | xr.Dataset.

As you suggested in the issue, it comes with tests: tests/test_dataarray_target.py checks every method with a DataArray target against the same grid as a Dataset (named and unnamed targets, with and without time, NumPy and Dask, DataArray and Dataset input) and checks that a lazy target's values are not computed. 24 of the 97 cases fail on main and all pass with the change. The two test_conservative_nan_thresholds_against_coarsen failures on main come from xarray's coarsen(skipna=...) and are unchanged.

@BSchilperoort

Copy link
Copy Markdown
Contributor

Thanks for submitting this PR. The tests are failing due to a problem with github's billing system on the organization.
Hopefully that will be resolved soon, but it's already been over a week...

@ZackaryLoevseth

Copy link
Copy Markdown
Author

The re-run's only failures are test_conservative_nan_thresholds_against_coarsen[0] and [1] in tests/test_regrid.py, which this PR doesn't change. They fail on main too with xarray 2026.9.0, where coarsen(...).mean(skipna=...) raises a TypeError (introduced in pydata/xarray#11556 and fixed in pydata/xarray#11686, which isn't released yet). The Linting job last ran during the billing problem; with ruff 0.17.5 and mypy 2.4.1 this branch gives the same findings as main.

@BSchilperoort BSchilperoort left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

#76 fixes the CI issues.

Two comments from me.

Comment thread tests/test_dataarray_target.py Outdated
Comment on lines +51 to +52
expected = regrid(target.coords.to_dataset(), **kwargs)
actual = regrid(target, **kwargs)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

regrid will already do .coords.to_dataset() so this expected test result should always be the same...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You're right, that comparison could only fail by raising. expected now comes from a Dataset built directly from the coordinate values, and there is a new test with hand-computed values (linear and conservative) for the unnamed-target-with-time case that fails on main.

if time_dim is not None and time_dim in ds_target_grid.coords:
ds_target_grid = ds_target_grid.isel({time_dim: 0}).reset_coords()
target_grid: xr.Dataset = (
ds_target_grid.coords.to_dataset()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

.coords.to_dataset() causes dims without coords to be dropped. This can cause users to get an incorrect error later. Maybe it would be good to check that ds_target_grid has no dims without coords 🤔

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch. A target with dims but no coordinates got "None of the target dims are in the data … Target dims: []" here, where main (and the same grid as a Dataset) reports the missing coords. The dims check now reads the dims from the target before the conversion, so those errors are the same as on main, with a test.

I didn't add a hard check, because DataArray targets with a dim without a coordinate (an extra bnds-like dim, or time without a coordinate) regrid fine on main today, the same as the Dataset holding them, and DataArray.interp_like skips such dims too. Happy to add the check if you prefer it.

@ZackaryLoevseth

Copy link
Copy Markdown
Author

Pushed one commit for both comments. This conflicts with #76 only in CHANGELOG.md; with both applied locally the tests, ruff and mypy pass. I'll rebase once #76 is in.

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.

Too narrow types; regridding functions can also take xr.DataArray types as argument

2 participants