-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace time dim validator with more generic validator
- Loading branch information
Showing
3 changed files
with
59 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Validators for data arrays.""" | ||
|
||
import xarray as xr | ||
|
||
from movement.utils.logging import log_error | ||
|
||
|
||
def validate_dimension_coordinates( | ||
data: xr.DataArray, required_dim_coords: dict | ||
) -> None: | ||
"""Validate dimensions and coordinates in a data array. | ||
This function raises a ValueError if the specified dimensions and | ||
coordinates are not present in the input data array.. | ||
Parameters | ||
---------- | ||
data : xarray.DataArray | ||
The input data array to validate. | ||
required_dim_coords : dict | ||
A dictionary of required dimensions and their corresponding | ||
coordinate values. If you don't need to specify any | ||
coordinate values, you can pass an empty list. | ||
Raises | ||
------ | ||
ValueError | ||
If the input data does not contain the required dimension(s) | ||
and/or the required coordinate(s). | ||
""" | ||
missing_dims = [dim for dim in required_dim_coords if dim not in data.dims] | ||
error_message = "" | ||
if missing_dims: | ||
error_message += ( | ||
f"Input data must contain {missing_dims} as dimensions.\n" | ||
) | ||
missing_coords = [] | ||
for dim, coords in required_dim_coords.items(): | ||
missing_coords = [ | ||
coord for coord in coords if coord not in data.coords.get(dim, []) | ||
] | ||
if missing_coords: | ||
error_message += ( | ||
"Input data must contain " | ||
f"{missing_coords} in the '{dim}' coordinates." | ||
) | ||
if error_message: | ||
raise log_error(ValueError, error_message) |