holoviz / geoviews

Simple, concise geographical visualization in Python
http://geoviews.org
BSD 3-Clause "New" or "Revised" License
578 stars 75 forks source link

Dataset to.image "unexpected keyword argument 'gridded'" #126

Closed zephyrthenoble closed 6 years ago

zephyrthenoble commented 6 years ago

I have a Pandas DataFrame containing data with the columns latitude, longitude, time, and count. I turn this DataFrame into a Dataset using this command piq_ds = gv.Dataset(df, kdims=['longitude', 'latitude', 'time']).

When I try to visualize this Dataset, using piq_ds.to.image(['longitude', 'latitude'], ['count'], ['time']), I get a long error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-e144ea6cd854> in <module>()
----> 3 piq_ds.to.image(['longitude', 'latitude'], ['count'], ['time'])

/opt/conda/lib/python3.6/site-packages/geoviews/element/__init__.py in image(self, kdims, vdims, mdims, **kwargs)
     34 
     35     def image(self, kdims=None, vdims=None, mdims=None, **kwargs):
---> 36         return self(Image, kdims, vdims, mdims, **kwargs)
     37 
     38     def points(self, kdims=None, vdims=None, mdims=None, **kwargs):

/opt/conda/lib/python3.6/site-packages/geoviews/element/__init__.py in __call__(self, *args, **kwargs)
     25         if 'crs' not in kwargs and issubclass(group_type, _Element):
     26             kwargs['crs'] = self._element.crs
---> 27         return super(GeoConversion, self).__call__(*args, **kwargs)
     28 
     29     def linecontours(self, kdims=None, vdims=None, mdims=None, **kwargs):

/opt/conda/lib/python3.6/site-packages/holoviews/core/data/__init__.py in __call__(self, new_type, kdims, vdims, groupby, sort, **kwargs)
    141             return element.sort() if sort else element
    142         group = selected.groupby(groupby, container_type=HoloMap,
--> 143                                  group_type=new_type, **params)
    144         if sort:
    145             return group.map(lambda x: x.sort(), [new_type])

/opt/conda/lib/python3.6/site-packages/holoviews/core/data/__init__.py in groupby(self, dimensions, container_type, group_type, dynamic, **kwargs)
    534 
    535         return self.interface.groupby(self, dim_names, container_type,
--> 536                                       group_type, **kwargs)
    537 
    538     def __len__(self):

/opt/conda/lib/python3.6/site-packages/holoviews/core/data/pandas.py in groupby(cls, columns, dimensions, container_type, group_type, **kwargs)
    152         group_by = [d.name for d in index_dims]
    153         data = [(k, group_type(v, **group_kwargs)) for k, v in
--> 154                 columns.data.groupby(group_by, sort=False)]
    155         if issubclass(container_type, NdMapping):
    156             with item_check(False):

/opt/conda/lib/python3.6/site-packages/holoviews/core/data/pandas.py in <listcomp>(.0)
    151 
    152         group_by = [d.name for d in index_dims]
--> 153         data = [(k, group_type(v, **group_kwargs)) for k, v in
    154                 columns.data.groupby(group_by, sort=False)]
    155         if issubclass(container_type, NdMapping):

/opt/conda/lib/python3.6/site-packages/geoviews/element/geo.py in __init__(self, data, kdims, vdims, **kwargs)
     88         elif crs:
     89             kwargs['crs'] = crs
---> 90         super(_Element, self).__init__(data, kdims=kdims, vdims=vdims, **kwargs)
     91 
     92 

/opt/conda/lib/python3.6/site-packages/holoviews/element/raster.py in __init__(self, data, kdims, vdims, bounds, extents, xdensity, ydensity, **params)
    237         Dataset.__init__(self, data, kdims=kdims, vdims=vdims, extents=extents, **params)
    238 
--> 239         dim2, dim1 = self.interface.shape(self, gridded=True)[:2]
    240         if bounds is None:
    241             xvals = self.dimension_values(0, False)

TypeError: shape() got an unexpected keyword argument 'gridded'

I am a novice to geoviews, holoviews, bokeh, and visualizing geospatial data in general. Is this a bug in geoviews/holoviews, or is this caused by something I did?

Thanks!

philippjfr commented 6 years ago

@zephyrthenoble this is an issue with poor error messages in HoloViews, which we should really improve. The issue is that columnar data like your dataframe cannot be turned into an Image just like that, because it has to be binned first. Depending on the size of your data I'd either visualize it as a set of points or use the datashader library to aggregate it into an Image.

tiles = gv.WMTS('https://maps.wikimedia.org/osm-intl/{Z}/{X}/{Y}@2x.png')
points = piq_ds.to(hv.Points, ['longitude', 'latitude'], ['count'], ['time'], dynamic=True)

# If the number of points is small I'd just plot it like this
%%opts Points [width=600 height=400 color_index='count'] (cmap='viridis')
tiles * points

# If the number of points is large (>10k) use datashader
# Install it with: conda install -c bokeh datashader
%%opts RGB [width=600 height=400]
import datashader as ds
from holoviews.operation.datashader import datashade 
tiles * datashade(points, aggregator=ds.sum('count'))
zephyrthenoble commented 6 years ago

Ah, that makes sense. I was able to get the data to render. Thank you very much!