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

error with Canvas().raster on xarray 0.17 #990

Closed
darribas opened this issue Mar 2, 2021 · 10 comments
Closed

error with Canvas().raster on xarray 0.17 #990

darribas opened this issue Mar 2, 2021 · 10 comments

Comments

@darribas
Copy link

darribas commented Mar 2, 2021

ALL software version info

The full list is available here, the gist is:

  • Python 3.8
  • xarray 0.17.0
  • datashader 0.12.0

Description of expected behavior and the observed behavior

I expect Canvas().raster to return a DataArray object, instead it returns an error.

Complete, minimal, self-contained example code that reproduces the issue

Adapted from your documentation

from datashader import transfer_functions as tf, reductions as rd
import numpy as np, datashader as ds, xarray as xr

def f(x,y):
    return np.cos((x**2+y**2)**2)

def sample(fn, n=50, range_=(0.0,2.4)):
    xs = ys = np.linspace(range_[0], range_[1], n)
    x,y = np.meshgrid(xs, ys)
    z   = fn(x,y)
    return xr.DataArray(z, coords=[('y',ys), ('x',xs)])

da = sample(f)
ds.Canvas().raster(da, interpolate='linear')

Stack traceback and/or browser JavaScript console output

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-7e8b8b432665> in <module>
     12 
     13 da = sample(f)
---> 14 ds.Canvas().raster(da, interpolate='linear')

/opt/conda/lib/python3.8/site-packages/datashader/core.py in raster(self, source, layer, upsample_method, downsample_method, nan_value, agg, interpolate, chunksize, max_mem)
   1131         dims = [ydim, xdim]
   1132         attrs = dict(res=res[0])
-> 1133         if source._file_obj is not None and hasattr(source._file_obj, 'nodata'):
   1134             attrs['nodata'] = source._file_obj.nodata
   1135 

/opt/conda/lib/python3.8/site-packages/xarray/core/common.py in __getattr__(self, name)
    237                 with suppress(KeyError):
    238                     return source[name]
--> 239         raise AttributeError(
    240             "{!r} object has no attribute {!r}".format(type(self).__name__, name)
    241         )

AttributeError: 'DataArray' object has no attribute '_file_obj'

I think this is related to #4809.

@jbednar
Copy link
Member

jbednar commented Mar 3, 2021

Same issue as holoviz/hvplot#563 . Proposed fixes welcome; I don't know where to find nodata in an xarray object.

@michaelaye
Copy link

I'm afraid it depends on the data source.
In some of my data sources it looks like this:

image

This is the result of the 'open_rasterio" function, which uses gdal which is one of the most used geo-raster libs, so maybe that's quite reliable? However, that's only available because the array creator put it there. So, looking for it at obj.attrs['nodatavals'] would hit a lot of raster-data, but I'm not sure there's a standard attribute location for opening netCDF files for example?

@philippjfr
Copy link
Member

This was always just a heuristic. I think we should support the major readers and conventions including attrs['NODATA'] and attrs['nodatavals'].

@ghost
Copy link

ghost commented Mar 3, 2021

This was always just a heuristic. I think we should support the major readers and conventions including attrs['NODATA'] and attrs['nodatavals'].

attrs['nodata'] is another variation

odc

@michaelaye
Copy link

michaelaye commented Mar 4, 2021

So, lines 1133...:

if source._file_obj is not None and hasattr(source._file_obj, 'nodata'):
            attrs['nodata'] = source._file_obj.nodata

could maybe become something like:

nodata_keys = ['NODATA', 'nodatavals', 'nodata']
for key in nodata_keys:
    if hasattr(source.attrs, key):
        attrs['nodata'] = source.attrs[key]

?

@jbednar
Copy link
Member

jbednar commented Mar 4, 2021

Yes, that's basically what I'm testing now.

@scottyhq
Copy link

scottyhq commented Mar 4, 2021

FYI, this is how rioxarray currently does it for any given xarray object:

  1. Look in attributes (attrs) of your data array for the _FillValue then missing_value then fill_value and finally nodata.
  2. Look in the nodatavals attribute. This is for backwards compatibility with xarray.open_rasterio.

from:
https://corteva.github.io/rioxarray/stable/getting_started/nodata_management.html#Search-order-for-nodata-(DataArray-only):

@michaelaye
Copy link

Temporary workaround, if you just want the plot working without caring about nodata vals (maybe they can be set manually?):

arr['_file_obj'] = None

1 similar comment
@michaelaye
Copy link

Temporary workaround, if you just want the plot working without caring about nodata vals (maybe they can be set manually?):

arr['_file_obj'] = None

@ppwadhwa
Copy link
Collaborator

Screen Shot 2021-03-22 at 10 20 39 AM

This should be fixed in 0.12.1, which is available on the pyviz channel, conda-forge, and pip.

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

6 participants