Replies: 5 comments 14 replies
-
It doesn't answer the question on how to best represent vector quantities, but:
I think this might be possible by choosing a appropriate attrs handler for |
Beta Was this translation helpful? Give feedback.
-
Just saw this issue. I wanted to note that it also comes up in xgcm, where we have support for finite-volume calculus. Where we need to keep track of vectors, we put each component in a dictionary. https://xgcm.readthedocs.io/en/latest/api.html#xgcm.Grid.diff_2d_vector This is not very satisfactory. |
Beta Was this translation helpful? Give feedback.
-
What about using complex data types? |
Beta Was this translation helpful? Give feedback.
-
@jthielen Late to the party, and not having fully understood all the requirements, but isn't this something that could be handled using a duck-array? Say we need an array of 3-D vectors, one could imagine something like: class Components:
def __init__(self, obj):
self._obj = obj
def __getitem__(self, key):
return self._obj[self._components.index(key)]
class VectorArray:
def __init__(self, values: np.ndarray, components: List[str]):
# Assuming a structure-of-array implementation
assert len(values) == len(components)
self._values = values
self._components = components
@property
def shape(self):
return self._values.shape[1:]
def __getitem__(self, index):
return VectorArray(self._values[:, index], self._components)
@property
def components(self):
return Components(self)
def __array_ufunc__(self):
"""TODO"""
def __array_function__(self):
"""TODO""" What is missing above is an interface ensures that gives us components with named dimensions. This can be solved using a custom This is a problem I am experiencing in other places as well --- duck arrays and the Array API specification lack named dimensions, but this would be extremely valuable when writing a duck-array. One could use |
Beta Was this translation helpful? Give feedback.
-
I tried this too by wrapping a pandas |
Beta Was this translation helpful? Give feedback.
-
In both following along with the development of
xr.cross
(#5365) and having some recent discussions downstream in MetPy (Unidata/MetPy#2079), I wanted to raise (or re-raise) the idea/suggestion of having some explicit form of vector data object in xarray (or a suitable xarray-contrib extension package).I would imagine it behaving like a Dataset, but with additional constraints (e.g., identical (or at least coupled) dimensions and chunking across contained variables) and additional operations (e.g., tuple unpacking, presently DataArray-only things like
xr.dot
andxr.cross
working as expected). In this way, it could fit naturally as a subclass of/wrapper around theDataNode
proposed in #4118 (comment), as this would ideally both:Dataset
as it currently exists)DataArray
with a stack dimension)This would allow for some rather streamlined code when doing vector analysis. For example, with a dataset/datatree like
horizontal advection can be expressed as
where
gradient
is like that of MetPy or a simple vector-based wrap ofda.differentiate()
.What are everyone's thoughts on this?
xref Unidata/MetPy#537
Beta Was this translation helpful? Give feedback.
All reactions