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

ValueError: Out of range float values are not JSON compliant version 0.12.5 #6222

Closed
DuCorey opened this issue May 1, 2017 · 6 comments
Closed

Comments

@DuCorey
Copy link
Contributor

DuCorey commented May 1, 2017

Summary

Issue on versions 0.12.5 when plotting data with patches.
Error code is ValueError: Out of range float values are not JSON compliant.

Expected behavior

When using version 0.12.4 of the package the plotting works without any issue. This has been previously brought up in #5439 and fixed with #5544. It seems that this has broken from version 0.12.4 to 0.12.5.

Software

OS

Windows 7 Enterprise

Browser
  • Chrome
    Version 58.0.3029.81
Python
  • bokeh=0.12.5=py35_0
  • jinja2=2.9.6=py35_0
  • markupsafe=0.23=py35_2
  • mkl=2017.0.1=0
  • numpy=1.12.1=py35_0
  • pandas=0.19.2=np112py35_1
  • pip=9.0.1=py35_1
  • python=3.5.3=0
  • python-dateutil=2.6.0=py35_0
  • pytz=2017.2=py35_0
  • pyyaml=3.12=py35_0
  • requests=2.13.0=py35_0
  • setuptools=27.2.0=py35_1
  • six=1.10.0=py35_0
  • tornado=4.4.2=py35_0
  • vs2015_runtime=14.0.25123=0
  • wheel=0.29.0=py35_0
  • pip:
    • pyshp==1.2.11

Code example

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import HoverTool, ColumnDataSource
import itertools
import shapefile
import pandas as pd
import datetime
import requests
import zipfile
try:
    from StringIO import StringIO
except ImportError:
    from io import BytesIO as StringIO
import os

# Get World Country Map Data
# http://www.naturalearthdata.com/features/
shp = None
dbf = None
url = 'http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/ne_110m_admin_0_countries.zip'
response = requests.get(url)
with zipfile.ZipFile(StringIO(response.content)) as z:
    for fname in z.namelist():
        name, ext = os.path.splitext(fname)
        if ext == '.shp':
            shp = StringIO(z.read(fname)) 
            #shp = z.open(fname)
        elif ext == '.dbf':
            #dbf = z.open(fname)
            dbf = StringIO(z.read(fname))             
        else:
            pass
sf = shapefile.Reader(shp=shp, dbf=dbf)

# Munge map data for bokeh 
lats = []
lons = []
country = []
for shprec in sf.shapeRecords():
    name_long = str(shprec.record[18])
    country.append(name_long)
    lat, lon = map(list, zip(*shprec.shape.points))
    indices = shprec.shape.parts.tolist()
    lat = [lat[i:j] + [float('NaN')] for i, j in zip(indices, indices[1:]+[None])]
    lon = [lon[i:j] + [float('NaN')] for i, j in zip(indices, indices[1:]+[None])]
    lat = list(itertools.chain.from_iterable(lat))
    lon = list(itertools.chain.from_iterable(lon))
    lats.append(lat)
    lons.append(lon)

df = pd.DataFrame({'x': lats, 'y': lons, 'country': country})
cds = ColumnDataSource(df)
p = figure(width=800)
country_patches = p.patches('x', 'y', source=cds, line_color='white')
hover = HoverTool(renderers=[country_patches], tooltips=[('Country', '@country')])
p.add_tools(hover)
show(p)

Taken from Sean law at https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/F-DD-wzOevY

Traceback

Traceback (most recent call last): File "P:\Work\Projects\Capacity Planning\Capacity Planning\test_bokeh\better_us_map_5.py", line 85, in <module> show(p) File "D:\Users\corey.ducharme\Anaconda\envs\bokeh\lib\site-packages\bokeh\io.py", line 284, in show return _show_with_state(obj, _state, browser, new, notebook_handle=notebook_handle) File "D:\Users\corey.ducharme\Anaconda\envs\bokeh\lib\site-packages\bokeh\io.py", line 314, in _show_with_state _show_file_with_state(obj, state, new, controller) File "D:\Users\corey.ducharme\Anaconda\envs\bokeh\lib\site-packages\bokeh\io.py", line 319, in _show_file_with_state filename = save(obj, state=state) File "D:\Users\corey.ducharme\Anaconda\envs\bokeh\lib\site-packages\bokeh\io.py", line 367, in save _save_helper(obj, filename, resources, title) File "D:\Users\corey.ducharme\Anaconda\envs\bokeh\lib\site-packages\bokeh\io.py", line 422, in _save_helper html = file_html(obj, resources, title=title) File "D:\Users\corey.ducharme\Anaconda\envs\bokeh\lib\site-packages\bokeh\embed.py", line 416, in file_html (docs_json, render_items) = _standalone_docs_json_and_render_items(models) File "D:\Users\corey.ducharme\Anaconda\envs\bokeh\lib\site-packages\bokeh\embed.py", line 740, in _standalone_docs_json_and_render_items docs_json[k] = v.to_json() File "D:\Users\corey.ducharme\Anaconda\envs\bokeh\lib\site-packages\bokeh\document.py", line 734, in to_json doc_json = self.to_json_string() File "D:\Users\corey.ducharme\Anaconda\envs\bokeh\lib\site-packages\bokeh\document.py", line 763, in to_json_string return serialize_json(json, indent=indent) File "D:\Users\corey.ducharme\Anaconda\envs\bokeh\lib\site-packages\bokeh\core\json_encoder.py", line 226, in serialize_json return json.dumps(obj, cls=BokehJSONEncoder, allow_nan=False, indent=indent, separators=separators, sort_keys=True, **kwargs) File "D:\Users\corey.ducharme\Anaconda\envs\bokeh\lib\json\__init__.py", line 237, in dumps **kw).encode(obj) File "D:\Users\corey.ducharme\Anaconda\envs\bokeh\lib\json\encoder.py", line 198, in encode chunks = self.iterencode(o, _one_shot=True) File "D:\Users\corey.ducharme\Anaconda\envs\bokeh\lib\json\encoder.py", line 256, in iterencode return _iterencode(o, 0) ValueError: Out of range float values are not JSON compliant

Attempts

I've tried debugging the issue further by looking at changes between version on Github, but my inexperience does not let allow me to research this issue in any depth. I hope I've been precise enough in my post, but please let me know if you need any further help or clarification : I'm still new to how Github functions. Best of luck on the project.

@bryevdv
Copy link
Member

bryevdv commented May 1, 2017

The sublists in lats and lons probably need to be numpy arrays. Lists get serialized as pure plain json, which does not support sending nan values (this is entirely outside our control). But numpy arrays are serialized to a binary format that can faithfully represent nan values.

@DuCorey
Copy link
Contributor Author

DuCorey commented May 1, 2017

Perhaps, but how could this behavior have changed between versions. For now, I'm stuck staying on version 0.12.4

@DuCorey
Copy link
Contributor Author

DuCorey commented May 2, 2017

Okay, thanks for the help bryevdv. You were correct. Using lats = [np.asarray(i) for i in lats] in this example fixes the issue.

@mikepurvis
Copy link

@bryevdv I'm actually finding the opposite to be true on Bokeh 0.12.14. That is, my data which is simply a list of lists (containing some NaN entries to produce intentional discontinuities in the plot) works fine, whereas similar data that is instead a numpy array triggers the Out of range float values are not JSON compliant error. Any thoughts?

@mattpap
Copy link
Contributor

mattpap commented Feb 23, 2018

It will be hard to say anything specific without the code. All it takes is one bad array type conversion to get this error, like in issue #7523.

@mikepurvis
Copy link

mikepurvis commented Feb 23, 2018

My data is a list of scatter point pairs where some are (nan, nan). I plot this like so:

print repr(data)
fig.line(*zip(*data))

For the working data, I see:

[(450.0512595176697, 0.8252045919523492), (450.09909653663635, 0.8252045919515891), ... ]

For the non-working data, it is instead:

array([[ 450.01842856,    0.73487775],
       [ 450.03851318,    0.73487775],
       [ 450.05862641,    0.74477408],
       ..., 
       [ 527.10611415,    0.        ],
       [ 527.12610149,    0.        ],
       [ 527.149261  ,    0.        ]])

If it would be helpful to do so, I could dump this data into a pastebin somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants