pbugnion / gmaps

Google maps for Jupyter notebooks

Home Page:https://jupyter-gmaps.readthedocs.io/en/stable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TraitError when creating a WeightedHeatmap with sample code

gerzhoy opened this issue · comments

Hi,

I'm trying to create a weighted heatmap, and Python is throwing an error. I've actually replicated the error using the sample code here. (I have a valid API key that otherwise works for pulling data from Google). I'm running the code in Jupyter notebook (v. 6.0.3) using Python 3.7.6

import gmaps
import gmaps.datasets

gmaps.configure(api_key="AI...") # I've filled this with my own key

earthquake_data = gmaps.datasets.load_dataset("earthquakes")

print(earthquake_data[:4]) # first four rows

m = gmaps.Map()
m.add_layer(gmaps.WeightedHeatmap(data=earthquake_data))
m

This results in the following output:

[(65.1933, -149.0725, 1.7), (38.791832, -122.7808304, 2.1), (38.8180008, -122.79216770000001, 0.48), (33.6016667, -116.72766670000001, 0.78)]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/traitlets/traitlets.py in get(self, obj, cls)
    527         try:
--> 528             value = obj._trait_values[self.name]
    529         except KeyError:

KeyError: 'locations'

During handling of the above exception, another exception occurred:

TraitError                                Traceback (most recent call last)
<ipython-input-23-46911e438658> in <module>
      9 
     10 m = gmaps.Map()
---> 11 m.add_layer(gmaps.WeightedHeatmap(data=earthquake_data))
     12 m

~/opt/anaconda3/lib/python3.7/site-packages/ipywidgets/widgets/widget.py in __init__(self, **kwargs)
    413 
    414         Widget._call_widget_constructed(self)
--> 415         self.open()
    416 
    417     def __del__(self):

~/opt/anaconda3/lib/python3.7/site-packages/ipywidgets/widgets/widget.py in open(self)
    426         """Open a comm to the frontend if one isn't already open."""
    427         if self.comm is None:
--> 428             state, buffer_paths, buffers = _remove_buffers(self.get_state())
    429 
    430             args = dict(target_name='jupyter.widget',

~/opt/anaconda3/lib/python3.7/site-packages/ipywidgets/widgets/widget.py in get_state(self, key, drop_defaults)
    516         for k in keys:
    517             to_json = self.trait_metadata(k, 'to_json', self._trait_to_json)
--> 518             value = to_json(getattr(self, k), self)
    519             if not PY3 and isinstance(traits[k], Bytes) and isinstance(value, bytes):
    520                 value = memoryview(value)

~/opt/anaconda3/lib/python3.7/site-packages/traitlets/traitlets.py in __get__(self, obj, cls)
    554             return self
    555         else:
--> 556             return self.get(obj, cls)
    557 
    558     def set(self, obj, value):

~/opt/anaconda3/lib/python3.7/site-packages/traitlets/traitlets.py in get(self, obj, cls)
    533                 raise TraitError("No default value found for %s trait of %r"
    534                                  % (self.name, obj))
--> 535             value = self._validate(obj, dynamic_default())
    536             obj._trait_values[self.name] = value
    537             return value

~/opt/anaconda3/lib/python3.7/site-packages/traitlets/traitlets.py in _validate(self, obj, value)
    589             return value
    590         if hasattr(self, 'validate'):
--> 591             value = self.validate(obj, value)
    592         if obj._cross_validation_lock is False:
    593             value = self._cross_validate(obj, value)

~/opt/anaconda3/lib/python3.7/site-packages/gmaps/geotraitlets.py in validate(self, obj, value)
     27             _validate_latitude(latitude)
     28             _validate_longitude(longitude)
---> 29         return super(LocationArray, self).validate(obj, locations_as_list)
     30 
     31 

~/opt/anaconda3/lib/python3.7/site-packages/traitlets/traitlets.py in validate(self, obj, value)
   2322 
   2323     def validate(self, obj, value):
-> 2324         value = super(List, self).validate(obj, value)
   2325         value = self.validate_elements(obj, value)
   2326         return value

~/opt/anaconda3/lib/python3.7/site-packages/traitlets/traitlets.py in validate(self, obj, value)
   2240             return value
   2241 
-> 2242         value = self.validate_elements(obj, value)
   2243 
   2244         return value

~/opt/anaconda3/lib/python3.7/site-packages/traitlets/traitlets.py in validate_elements(self, obj, value)
   2317         length = len(value)
   2318         if length < self._minlen or length > self._maxlen:
-> 2319             self.length_error(obj, value)
   2320 
   2321         return super(List, self).validate_elements(obj, value)

~/opt/anaconda3/lib/python3.7/site-packages/traitlets/traitlets.py in length_error(self, obj, value)
   2312         e = "The '%s' trait of %s instance must be of length %i <= L <= %i, but a value of %s was specified." \
   2313             % (self.name, class_of(obj), self._minlen, self._maxlen, value)
-> 2314         raise TraitError(e)
   2315 
   2316     def validate_elements(self, obj, value):

TraitError: The 'locations' trait of a WeightedHeatmap instance must be of length 1 <= L <= 9223372036854775807, but a value of [] was specified.

Welcome any thoughts and happy to provide additional detail.

Hey there, try this out, hope it helps

locations = earthquake_data[['latitude','longitude']]
weights = earthquake_data['whatever you are taking as a weight']
fig = gmaps.figure
fig.add_layer(gmaps.heatmap_layer(locations, weights=weights))
fig

I'm having a similar issue. I'm not using the default earthquake data but a different set of data from a csv file my workplace handed me.