Skip to content
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

DataArray is converted to ndarray when passed as input variable #143

Closed
ethho opened this issue Oct 19, 2020 · 2 comments
Closed

DataArray is converted to ndarray when passed as input variable #143

ethho opened this issue Oct 19, 2020 · 2 comments

Comments

@ethho
Copy link

ethho commented Oct 19, 2020

Given the process definitions below:

@xs.process
class ConsumeDA:
    da = xs.variable(dims='x', intent='inout')

    def initialize(self):
        print(f"Is a DataArray: {isinstance(self.da, xr.DataArray)}")
        print(type(self.da))
        
@xs.process
class InitDA:
    da = xs.foreign(ConsumeDA, 'da', intent='out')
    
    def initialize(self):
        self.da = xr.DataArray(data=range(5), dims='x')

We see expected behavior when instantiating an xr.DataArray within an upstream process:

model0 = xs.Model(dict(
    init=InitDA,
    use=ConsumeDA
))

xs.create_setup(
    model=model0,
    clocks=dict(step=range(2)),
    input_vars=dict(),
    output_vars=dict()
).xsimlab.run(model=model0)
# Is a DataArray: True
# <class 'xarray.core.dataarray.DataArray'>

However, when the same variable is passed as an input, it is converted to a numpy.ndarray:

model1 = xs.Model(dict(
    use=ConsumeDA
))

xs.create_setup(
    model=model1,
    clocks=dict(step=range(2)),
    input_vars=dict(
        use__da=xr.DataArray(data=range(5), dims='x')
    ),
    output_vars=dict()
).xsimlab.run(model=model1)
# Is a DataArray: False
# <class 'numpy.ndarray'>

Is this expected behavior? If so, what is best practice for passing DataArrays as model inputs?

Environment

  • Python 3.8.3
  • numpy==1.19.2
  • scipy==1.5.3
  • xarray==0.16.1
  • xarray-simlab==0.4.1
  • zarr==2.5.0
  • attrs==19.3.0
@benbovy
Copy link
Member

benbovy commented Oct 28, 2020

Yes it's expected behavior. Currently Xarray objects are used only for the model I/O interface.

I agree that using xarray DataArray or Variable inside process classes would be useful, though. I suggest that in #141.

@ethho
Copy link
Author

ethho commented Oct 28, 2020

Thanks for confirming, @benbovy.

For posterity: I worked around this by writing upstream "converter" processes that ingest np.ndarray or numbers.Number and output DataArrays.

@ethho ethho closed this as completed Oct 28, 2020
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

No branches or pull requests

2 participants