KeyError: 'projection' when saving plot

Hi,
The following code:

 df = pd.DataFrame(gdf)
    points = hv.Points(gv.Dataset(df, kdims=['LON', 'LAT']))
    race = dynspread(datashade(points, element_type=gv.Image, aggregator=ds.count()))
    lay = gts.EsriImagery() * race
   gv.save(lay, "dens.png")

produces

  File "/home/losafe/users/tcevaer/statPlots/statsplots/plots.py", line 72, in densityMap
    gv.save(lay, "dens.png")
...
  File "/home/losafe/users/tcevaer/cyclobs_conda/lib/python3.7/site-packages/geoviews/plotting/plot.py", line 36, in _get_projection
    from_overlay = not all(p is None for p in opts[True]['projection'])
KeyError: 'projection'

Any idea how to solve this ?

1 Like

Solved by doing

    proj = ccrs.PlateCarree()
    points = hv.Points(gv.Dataset(df, kdims=['LON', 'LAT']))
    race = dynspread(datashade(points, element_type=gv.Image, aggregator=ds.count()))
    lay = (gts.EsriImagery() * race).opts(projection=proj)
    gv.save(lay, "dens.png")

You must be using dev releases of HoloViews without also upgrading to the latest dev release of GeoViews.

That is exactly the case !

I’m not sure this is the right place to put my troubles, but at least it consolidates solutions to this error (as far as I can tell it’s identical). I get this same error when running Projections.ipynb with

  1. osx
  2. clean conda install
  3. follow the geoviews install instructions

The problem persists after

  1. conda update -all
  2. installing dev versions of both geoviews and holoviews

Specifically, the final line of the second code box:

(gv.tile_sources.Wikipedia * cities_latlon * cities_mercator).options('Points', **options)

produces the error

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

~/opt/anaconda3/lib/python3.7/site-packages/holoviews/core/dimension.py in _repr_mimebundle_(self, include, exclude)
   1302         combined and returned.
   1303         """
-> 1304         return Store.render(self)
   1305 
   1306 

~/opt/anaconda3/lib/python3.7/site-packages/holoviews/core/options.py in render(cls, obj)
   1393         data, metadata = {}, {}
   1394         for hook in hooks:
-> 1395             ret = hook(obj)
   1396             if ret is None:
   1397                 continue

~/opt/anaconda3/lib/python3.7/site-packages/holoviews/ipython/display_hooks.py in pprint_display(obj)
    280     if not ip.display_formatter.formatters['text/plain'].pprint:
    281         return None
--> 282     return display(obj, raw_output=True)
    283 
    284 

~/opt/anaconda3/lib/python3.7/site-packages/holoviews/ipython/display_hooks.py in display(obj, raw_output, **kwargs)
    250     elif isinstance(obj, (CompositeOverlay, ViewableElement)):
    251         with option_state(obj):
--> 252             output = element_display(obj)
    253     elif isinstance(obj, (Layout, NdLayout, AdjointLayout)):
    254         with option_state(obj):

~/opt/anaconda3/lib/python3.7/site-packages/holoviews/ipython/display_hooks.py in wrapped(element)
    144         try:
    145             max_frames = OutputSettings.options['max_frames']
--> 146             mimebundle = fn(element, max_frames=max_frames)
    147             if mimebundle is None:
    148                 return {}, {}

~/opt/anaconda3/lib/python3.7/site-packages/holoviews/ipython/display_hooks.py in element_display(element, max_frames)
    190         return None
    191 
--> 192     return render(element)
    193 
    194 

~/opt/anaconda3/lib/python3.7/site-packages/holoviews/ipython/display_hooks.py in render(obj, **kwargs)
     66         renderer = renderer.instance(fig='png')
     67 
---> 68     return renderer.components(obj, **kwargs)
     69 
     70 

~/opt/anaconda3/lib/python3.7/site-packages/holoviews/plotting/renderer.py in components(self, obj, fmt, comm, **kwargs)
    363             doc = Document()
    364             with config.set(embed=embed):
--> 365                 model = plot.layout._render_model(doc, comm)
    366             return render_model(model, comm) if embed else render_mimebundle(model, doc, comm)
    367         else:

~/opt/anaconda3/lib/python3.7/site-packages/panel/viewable.py in _render_model(self, doc, comm)
    363         if comm is None:
    364             comm = state._comm_manager.get_server_comm()
--> 365         model = self.get_root(doc, comm)
    366 
    367         if config.embed:

~/opt/anaconda3/lib/python3.7/site-packages/panel/viewable.py in get_root(self, doc, comm)
    534         """
    535         doc = doc or _curdoc()
--> 536         root = self._get_model(doc, comm=comm)
    537         self._preprocess(root)
    538         ref = root.ref['id']

~/opt/anaconda3/lib/python3.7/site-packages/panel/layout.py in _get_model(self, doc, root, parent, comm)
    134         if root is None:
    135             root = model
--> 136         objects = self._get_objects(model, [], doc, root, comm)
    137         props = dict(self._init_properties(), objects=objects)
    138         model.update(**self._process_param_change(props))

~/opt/anaconda3/lib/python3.7/site-packages/panel/layout.py in _get_objects(self, model, old_objects, doc, root, comm)
    124             else:
    125                 try:
--> 126                     child = pane._get_model(doc, root, model, comm)
    127                 except RerenderError:
    128                     return self._get_objects(model, current_objects[:i], doc, root, comm)

~/opt/anaconda3/lib/python3.7/site-packages/panel/pane/holoviews.py in _get_model(self, doc, root, parent, comm)
    225             plot = self.object
    226         else:
--> 227             plot = self._render(doc, comm, root)
    228 
    229         plot.pane = self

~/opt/anaconda3/lib/python3.7/site-packages/panel/pane/holoviews.py in _render(self, doc, comm, root)
    284             kwargs = {}
    285 
--> 286         return renderer.get_plot(self.object, **kwargs)
    287 
    288     def _cleanup(self, root):

~/opt/anaconda3/lib/python3.7/site-packages/holoviews/plotting/bokeh/renderer.py in get_plot(self_or_cls, obj, doc, renderer, **kwargs)
     71         combining the bokeh model with another plot.
     72         """
---> 73         plot = super(BokehRenderer, self_or_cls).get_plot(obj, doc, renderer, **kwargs)
     74         if plot.document is None:
     75             plot.document = Document() if self_or_cls.notebook_context else curdoc()

~/opt/anaconda3/lib/python3.7/site-packages/holoviews/plotting/renderer.py in get_plot(self_or_cls, obj, doc, renderer, comm, **kwargs)
    217                 obj = Layout(obj)
    218             plot = self_or_cls.plotting_class(obj)(obj, renderer=renderer,
--> 219                                                    **plot_opts)
    220             defaults = [kd.default for kd in plot.dimensions]
    221             init_key = tuple(v if d is None else d for v, d in

~/opt/anaconda3/lib/python3.7/site-packages/geoviews/plotting/bokeh/plot.py in __init__(self, element, **params)
    179 
    180     def __init__(self, element, **params):
--> 181         super(GeoOverlayPlot, self).__init__(element, **params)
    182         self.geographic = any(element.traverse(is_geographic, [_Element]))
    183         if self.geographic:

~/opt/anaconda3/lib/python3.7/site-packages/geoviews/plotting/bokeh/plot.py in __init__(self, element, **params)
     61 
     62     def __init__(self, element, **params):
---> 63         super(GeoPlot, self).__init__(element, **params)
     64         self.geographic = is_geographic(self.hmap.last)
     65         if self.geographic and not isinstance(self.projection, (PlateCarree, Mercator)):

~/opt/anaconda3/lib/python3.7/site-packages/holoviews/plotting/plot.py in __init__(self, overlay, ranges, batched, keys, group_counter, **params)
   1449     def __init__(self, overlay, ranges=None, batched=True, keys=None, group_counter=None, **params):
   1450         if 'projection' not in params:
-> 1451             params['projection'] = self._get_projection(overlay)
   1452 
   1453         super(GenericOverlayPlot, self).__init__(overlay, ranges=ranges, keys=keys,

~/opt/anaconda3/lib/python3.7/site-packages/geoviews/plotting/plot.py in _get_projection(self, obj)
     34                                       [CompositeOverlay, Element],
     35                                       keyfn=isoverlay, defaults=False)
---> 36         from_overlay = not all(p is None for p in opts[True]['projection'])
     37         projections = opts[from_overlay]['projection']
     38         infer = any(opts[from_overlay]['infer_projection']) or self.infer_projection

KeyError: 'projection'

Thanks in advance for your insights.

Hi,

I’m also still getting these issues too.
What versions of bokeh, holoviews and geoviews are you using ?
Using holoviews 1.13.0, geoviews 1.6.6 and bokeh 2.0.0, I managed to get around by using :

(gv.tile_sources.Wikipedia * cities_latlon * cities_mercator).opts(projection=proj)
where proj is a defined projection.

Maybe using geoviews=1.7.0 solves the issue.

hmmm. I’m not totally sure what to make of this.

(base) james@orographic-2[511]:~> conda list | grep oview
geoviews                  1.6.6                      py_0    pyviz/label/dev
geoviews-core             1.6.6                      py_1    conda-forge
holoviews                 1.13.1                     py_0    pyviz/label/dev

I’ll try the 1.7.0 upgrade. I assumed that dev would get me the latest version and beyond.

I discourage you from using development release because they can lead to this kind of errors, I think.

Normally dev release give you the latest releases, yes, but geoviews has been released very recently so it may have to do with this.

If I were you I would do

pip uninstall bokeh geoviews holoviews
conda remove bokeh geoviews holoviews
conda install -c conda-forge bokeh geoviews holoviews

I had a lot of trouble mixing pip install and conda install in the same conda
Then check the version of geoviews

Skealz,
Thanks SO MUCH for your help!
Indeed the upgrade to 1.7.0 has fixed the issue.

I hope this gets ironed out somehow because following the geoviews install procedures currently provides a broken geoviews. I’m not sure if that should be an issue on the repo or not. I’d be happy to report it but I dont have the first idea where to start fixing it.

And thanks for the further guidance, I’ll take it!

1 Like

Happy to hear that !

The “recommended” installation is using pip or conda like this :

pip install geoviews
OR
conda install geoviews

Is that what you did ?

And there are some things to know.
For example, doing

conda install holoviews #geoviews dependency
pip install geoviews

is a bad idea because you are mixing package sources, which can lead to bugs.
If you start installing with pip, stay with pip if you can.
Also conda uses several “channels” (for example, conda-forge is channel, more info on their doc).
You can configure conda to prefer a chosen channel among others, to avoid mixing packages sources.

I followed the installation instructions. I did not mix conda and pip (been bitten by that enough laready). This is why I’m saying “following the geoviews install procedures currently provides a broken geoviews”. I nuked my conda and started from scratch, meticulously following the install instructions.

To be more specific, I
conda install -c pyviz geoviews

This next line fails for me:
conda install proj4<6

The following is fine:

geoviews examples
cd geoviews-examples
jupyter notebook

I believe the second line of the following needs a variant of conda install -c conda-forge nodejs before success

conda install -c conda-forge jupyterlab
jupyter labextension install @pyviz/jupyterlab_pyviz

I believe I also followed the conda installation instructions for xarray as well.

Then when Projections.ipynb did not work, I went to the variants of
conda install -c pyviz/label/dev geoviews
for both geoviews and holoviews. Still no success.

Then I
conda install -c pyviz geoviews=1.7.0
and then the Projections.ipynb worked through the second code block (as much as I tried).

Ok, it seems that, yes, the documentation could be improved.
I can’t help you more if you wish to contribute, but I think there is some documentation about it somewhere. But reproduce the problem and demonstrate it into a github issue is a good start !

You’ve been super helpful! Thanks again.
I’d just hope others dont get discouraged by this problem. I may start an issue and solicit guidance on the best way to improve the install docs.

1 Like

I am getting the same
KeyError: ‘projection’
any time I try to use the “tiles” parameter. The output looks like this:

KeyError: ‘projection’

:Overlay
.Tiles.I :Tiles [x,y]
.Points.I :Points [Long_,Lat] (Confirmed)

I am using 1.6.6 geoviews. It is the most recent in Anaconda Navigator and running
conda install -c pyviz geoviews=1.7.0
just ran into a conda error.

Why not install the latest version of geoviews ?
conda install -c pyviz 'geoviews>1.7.0' for example