8000 fix check dims for ds with multiple vars by DirkEilander · Pull Request #761 · Deltares/hydromt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix check dims for ds with multiple vars #761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 31, 2024
Merged

fix check dims for ds with multiple vars #761

merged 3 commits into from
Jan 31, 2024

Conversation

DirkEilander
Copy link
Contributor
@DirkEilander DirkEilander commented Jan 31, 2024

Issue addressed

Fixes #760

Explanation

Relax assumption that all dataset variables must have the same dimensions.

Checklist

  • Updated tests or added new tests
  • Branch is up to date with main
  • Tests & pre-commit hooks pass
  • Updated documentation if needed
  • Updated changelog.rst if needed
  • For predefined catalogs: update the catalog version in the file itself, the references in data/predefined_catalogs.yml, and the changelog in data/changelog.rst

Additional Notes (optional)

Add any additional notes or information that may be helpful.

@DirkEilander DirkEilander mentioned this pull request Jan 31, 2024
1 task
Copy link
Contributor
@hboisgon hboisgon left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor
@savente93 savente93 left a comment

Choose a reason for hiding this comment

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

functionality seems okay from what I can tell but I'd like to see a few code changes.

Comment on lines +658 to +664
def _check_dims(da: xr.DataArray, dims=dims) -> bool:
extra_dims = [dim for dim in da.dims if dim not in dims]
if len(extra_dims) == 1:
dims = tuple(extra_dims) + dims
elif len(extra_dims) > 1:
raise ValueError("Only 2D and 3D data arrays supported.")
return da.dims == dims, extra_dims
Copy link
Contributor

Choose a reason for hiding this comment

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

Please don't use closures if they are not necessary, they can cause very unexpected behavour. Could you also give this function a more descriptive name that includes what it actually checks?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also your type hint is incorrect, it returns a bool and I think a list of dimentions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good suggestion


extra_dims = []
if isinstance(self._obj, xr.DataArray):
check, dim0 = _check_dims(self._obj)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this name is very deceptive, since it's not just one dimention but a list of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will change the name

extra_dims = []
if isinstance(self._obj, xr.DataArray):
check, dim0 = _check_dims(self._obj)
extra_dims.extend(dim0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to extend here even if we raise a value error on the next line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only when no error is raised we actually use extra_dims, so I don't think this matters

Comment on lines +677 to +678
if not all([dim in self._obj[var].dims for dim in dims]):
continue # only check data variables with x and y dims
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this check data vars that have x and y dims or that only have x and y dims?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That check happens when getting the x and y dims in the raster.x_dim and raster.y_dim properties.

@DirkEilander DirkEilander merged commit 35e32f1 into main Jan 31, 2024
@DirkEilander DirkEilander deleted the fix/check_dims branch January 31, 2024 08:51
DirkEilander added a commit that referenced this pull request Jan 31, 2024
@DirkEilander DirkEilander restored the fix/check_dims branch January 31, 2024 08:53
@DirkEilander DirkEilander mentioned this pull request Jan 31, 2024
6 tasks
@DirkEilander DirkEilander deleted the fix/check_dims branch January 31, 2024 09:10
DirkEilander added a commit that referenced this pull request Jan 31, 2024
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.

raster dimension check fails for datasets of multiple variables with varying dimension size
3 participants
0