Replies: 1 comment 5 replies
-
You might have a look at https://github.com/xarray-contrib/xarray-schema which provides Dataset/DataArray validation. |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to create several templates that define
xr.DataArray
objects constrained to have specificdims
that are defined at the class level.For example, I might want to define an
ImageDataArray
that always has dims (x
,y
) or aTimeSeriesDataArray
with only dims ('t'). These templates can then be used as type information in other models and if one tries to construct one with the wrong dims, they should fail gracefully.What is the recommended way to handle this sort of behavior in xarray? There are two approaches I've tried, but each has some drawbacks.
A naive approach is to subclass
xr.DataArray
and provide some logic to check the dims.However, subclassing
xr.DataArray
is not recommended, as explained here. And in my experience, it is indeed difficult to maintain.Another option is to define classes that contain the dim information as well as a regular xr.DataArray attributes, and do a bit of checking. For example:
This works, but then introduces the need for many intermediate calls to
.data_array
as theImageDataArray
class itself is not very useful in my application.Any thoughts or discussion appreciated, thanks!
Beta Was this translation helpful? Give feedback.
All reactions