home / github / issues

Menu
  • GraphQL API
  • Search all tables

issues: 373646673

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
373646673 MDU6SXNzdWUzNzM2NDY2NzM= 2507 Error when applying a function with apply_ufunc() when using a function that returns multiple arrays 1328158 closed 0     5 2018-10-24T19:42:49Z 2018-10-29T05:07:06Z 2018-10-29T05:07:06Z NONE      

Code Sample, a copy-pastable example if possible

You can reproduce this error I think by using the code found here: https://github.com/monocongo/climate_indices/tree/issue_191_groupby

To exercise the code where the error occurs run the following command:

$ python scripts/process_grid_ufunc.py --index palmers --periodicity monthly --netcdf_precip example_inputs/nclimgrid_lowres_prcp.nc --var_name_precip prcp --netcdf_pet example_inputs/nclimgrid_lowres_pet.nc --var_name_pet pet --netcdf_awc example_inputs/nclimgrid_lowres_soil.nc --var_name_awc awc --output_file_base /data/test/nclimgrid_lowres_ufunc --calibration_start_year 1951 --calibration_end_year 2010 Within scripts/process_grid_ufunc.py, in the compute_write_palmers() function:

```python # stack the lat and lon dimensions into a new dimension named point, so at each lat/lon # we'll have a time series for the geospatial point, and group by these points da_precip_groupby = da_precip.stack(point=('lat', 'lon')).groupby('point') da_pet_groupby = da_pet.stack(point=('lat', 'lon')).groupby('point') da_awc_groupby = da_awc.stack(point=('lat', 'lon')).groupby('point')

# keyword arguments used for the function we'll apply to the data array
args_dict = {'data_start_year': data_start_year,
             'calibration_start_year': kwrgs['calibration_start_year'],
             'calibration_end_year': kwrgs['calibration_end_year']}

# apply the self-calibrated Palmers function to the data arrays
da_scpdsi, da_pdsi, da_phdi, da_pmdi, da_zindex = xr.apply_ufunc(indices.scpdsi,
                                                                 da_precip_groupby,
                                                                 da_pet_groupby,
                                                                 da_awc_groupby,
                                                                 output_core_dims=[[], [], [], [], []],
                                                                 kwargs=args_dict)

```

Problem description

I have a function that I apply to xarray.DataArray objects using xarray.apply_ufunc().

The function I'm using returns five arrays. The signature is this:

def scpdsi(precip_inches: np.ndarray,
           pet_inches: np.ndarray,
           awc: np.ndarray,
           data_start_year,
           calibration_start_year,
           calibration_end_year):
...

   return scpdsi, pdsi, phdi, pmdi, zindex

I'm applying the function over three xarray.DataArray objects like so:

# keyword arguments used for the function we'll apply to the data arrays
args_dict = {'data_start_year': data_start_year,
             'calibration_start_year': kwrgs['calibration_start_year'],
             'calibration_end_year': kwrgs['calibration_end_year']}

# apply the self-calibrated Palmers function to the data arrays
da_scpdsi, da_pdsi, da_phdi, da_pmdi, da_zindex = xr.apply_ufunc(scpdsi,
                                                                 da_precip_groupby,
                                                                 da_pet_groupby,
                                                                 da_awc_groupby,
                                                                 output_core_dims=[[], [], [], [], []],
                                                                 kwargs=args_dict)

When I run the code I get the following error:

ValueError: applied function does not have the number of outputs specified in the ufunc signature. Result is not a tuple of 5 elements: [array([nan, nan, nan, ..., nan, nan, nan], dtype=float32), array([nan, nan, nan, ..., nan, nan, nan], dtype=float32), array([nan, nan, nan, ..., nan, nan, nan], dtype=float32), array([nan, nan, nan, ..., nan, nan, nan], dtype=float32), array([nan, nan, nan, ..., nan, nan, nan], dtype=float32)]

When I step through the xarray computation.py code I see that the result data is returned as a list of arrays rather than as a tuple, and this is what raises the error (line 565 in computation.py). I've tried modifying the function to return the arrays as a tuple, along with marking the function's return type as a tuple in the signature but none of this has helped, i.e. the result data always comes through to xarray as a list of arrays rather than as a tuple.

I am using xarray version 0.10.9 with Python 3.6.

Expected Output

I was expecting to get the five arrays returned after applying the function. I may be doing something else wrong, as this is my first time using appy_ufunc() in this way, and if so please advise.

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.5.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None xarray: 0.10.9 pandas: 0.23.4 numpy: 1.15.2 scipy: 1.1.0 netCDF4: 1.4.1 h5netcdf: None h5py: 2.8.0 Nio: None zarr: None cftime: 1.0.0b1 PseudonetCDF: None rasterio: None iris: None bottleneck: None cyordereddict: None dask: 0.19.4 distributed: 1.23.3 matplotlib: None cartopy: None seaborn: None setuptools: 39.2.0 pip: 10.0.1 conda: 4.5.11 pytest: None IPython: 7.0.1 sphinx: None
{
    "url": "https://api.github.com/repos/pydata/xarray/issues/2507/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  completed 13221727 issue

Links from other tables

  • 0 rows from issues_id in issues_labels
  • 5 rows from issue in issue_comments
Powered by Datasette · Queries took 0.594ms · About: xarray-datasette