BAMWelDX / weldx

The welding data exchange format
https://www.bam.de/weldx
BSD 3-Clause "New" or "Revised" License
19 stars 9 forks source link

Serializing MathematicalExpression with not quantified xarray.DataArray fails #860

Closed marscher closed 1 year ago

marscher commented 1 year ago

IMO this snippet should work


import weldx
import xarray as xr

x = xr.DataArray([1,2,3])
m = weldx.MathematicalExpression("x**2 + 4*x + 7", {"x": x})
weldx.WeldxFile(tree={"m": m}, mode="rw")

but raises:

  File "/home/marscher/sources/weldx/weldx/asdf/types.py", line 36, in to_yaml_tree_wrapped
    tree = func(self, obj, tag, ctx)
  File "/home/marscher/sources/weldx/weldx/tags/core/mathematical_expression.py", line 29, in to_yaml_tree
    setattr(v, META_ATTR, dict(dims=dims))
AttributeError: 'numpy.ndarray' object has no attribute 'wx_metadata'
CagtayFabry commented 1 year ago

The problem here is that parameters must be quantities when provided as xarray objects (we only assume "dimensionless" by default for simple numpy arrays)

This is certainly inconsistent and lacking in the documentation. We should at least provide an error message to catch that case before we run into asdf troubles as seen here

this works

import weldx
from weldx import Q_
import xarray as xr

x = xr.DataArray(Q_([1,2,3],""))
m = weldx.MathematicalExpression("x**2 + 4*x + 7", {"x": x})
weldx.WeldxFile(tree={"m": m}, mode="rw")
marscher commented 1 year ago

at least it should raise, or even better enforce the input to be a (dimensionless) quantity, right?