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

PatchCollection - TypeError: array([ 1.]) is not JSON serializable #434

Open
omn14 opened this issue Oct 13, 2017 · 17 comments
Open

PatchCollection - TypeError: array([ 1.]) is not JSON serializable #434

omn14 opened this issue Oct 13, 2017 · 17 comments

Comments

@omn14
Copy link

omn14 commented Oct 13, 2017

I want to make a .html with a plot of some patches in a patch collection.
Im running the example from "https://matplotlib.org/examples/api/patch_collection.html"

Could someone please help?

thanks,

i get this error:

Traceback (most recent call last):
File "ttt.py", line 55, in
htmlfil.write(mpld3.fig_to_html(fig))
File "/usr/local/lib/python2.7/dist-packages/mpld3/_display.py", line 251, in fig_to_html
figure_json=json.dumps(figure_json, cls=NumpyEncoder),
File "/usr/lib/python2.7/json/init.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/local/lib/python2.7/dist-packages/mpld3/_display.py", line 138, in default
return json.JSONEncoder.default(self, obj)
File "/usr/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: array([ 1.]) is not JSON serializable

@shouldsee
Copy link

shouldsee commented Oct 29, 2017

I was having a similar error. It seems matplotlib.collections.pathcollection did not sanitise its dictionary enough, leaving many parameters in the form of numpy instances. Currently mpld3 relies on a "NumpyEncoder" to serialise these numpy instances, and the encoder would complain if the instance array is not listed.

Fix:
Modifiy _display.py

class NumpyEncoder(json.JSONEncoder):
    """ Special json encoder for numpy types """
    def default(self, obj):
        if isinstance(obj, (numpy.int_, numpy.intc, numpy.intp, numpy.int8,
            numpy.int16, numpy.int32, numpy.int64, numpy.uint8,
            numpy.uint16,numpy.uint32, numpy.uint64)):
            return int(obj)
        elif isinstance(obj, (numpy.float_, numpy.float16, numpy.float32, 
            numpy.float64)):
            return float(obj)
        elif isinstance(obj,(numpy.ndarray,)): #### This is the fix
            return obj.tolist()
        return json.JSONEncoder.default(self, obj)

javadba pushed a commit to javadba/mpld3 that referenced this issue Nov 19, 2017
@ghost
Copy link

ghost commented Dec 20, 2017

Thanks shouldsee! Your fix solved my save_html problem. I was getting "array is not JSON serializable html" when trying to save a scatter plot.

@bobleujr
Copy link

bobleujr commented Mar 1, 2018

It seems to me that this issue hasn't been solved in the latest release? I've got the latest version and had to fix it manually too. Thanks anyway @shouldsee !

@aflaxman
Copy link
Collaborator

aflaxman commented Mar 1, 2018

I'll merge a pull request if someone creates one. Bonus points if the automatic tests all pass!

@TastyMuffins
Copy link

Suggested fix also resolved my issue.

@jason-neal
Copy link
Contributor

Since javadba did not make a PR out of their commit I duplicated it.

aflaxman added a commit that referenced this issue Mar 12, 2018
@beukueb
Copy link

beukueb commented Apr 17, 2018

This is still an issue with
matplotlib==2.2.2 mpld3==0.3

I fix it manually by overwriting mpld3._display.NumpyEncoder in my program

#mpld3 hack
class NumpyEncoder(json.JSONEncoder):
    def default(self, obj):
        import numpy as np
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        return json.JSONEncoder.default(self, obj)
from mpld3 import _display
_display.NumpyEncoder = NumpyEncoder

@addinall
Copy link

Is there a production fix for this as yet? I introduced mpld3 into a Python project and am getting the same errors. Cheers!

@jason-neal
Copy link
Contributor

jason-neal commented Jul 12, 2018

This fix should be resolved in master. There has not been a release of mpl3d in almost 2 years. i do not know when/if there will be another as it is not actively unmaintained...

To use the mpld3 master branch
You can do something like
pip install git+git://github.com/mpld3/mpld3@master#egg=mpld3

or add
git+git://github.com/mpld3/mpld3@master#egg=mpld3 to your requirements.txt file

@bryevdv
Copy link

bryevdv commented Jul 12, 2018

@aflaxman @jakevdp perhaps it would be worth considering archiving this repository to indicate more clearly that the project is unmaintained:

https://help.github.com/articles/archiving-a-github-repository/

I did this for bokeh/bkcharts

@aflaxman
Copy link
Collaborator

@cliffckerr and company are planning to revive this project! if that has changed, i think archiving is a great idea.

@shouldsee
Copy link

shouldsee commented Jul 13, 2018 via email

@addinall
Copy link

Thanks all. I did not realise the code was unmaintained. It sounded perfect for my needs.
Cheers, Mark.

@cliffckerr
Copy link
Member

cliffckerr commented Jul 23, 2018

@addinall @aflaxman @bryevdv We are indeed reviving it! We are currently working on our fork (https://github.com/optimamodel/mpld3) but once everything is stable we'll be merging back here.

@aflaxman
Copy link
Collaborator

👍

@jjaimon
Copy link

jjaimon commented Jan 3, 2019

Similar issue exists with fig_to_dict() as well. The returned dict cannot be serialized to json due to included ndarray unless right encoder is passed. One way to ovecome the issue is by using the internal NumpyEncoder class itself. For eg.

from mpld3._display import NumpyEncoder
json.dumps(mpld3.fig_to_dict(fig), cls=NumpyEncoder)

@carlosa2
Copy link

This is still an issue with
matplotlib==2.2.2 mpld3==0.3

I fix it manually by overwriting mpld3._display.NumpyEncoder in my program

#mpld3 hack
class NumpyEncoder(json.JSONEncoder):
    def default(self, obj):
        import numpy as np
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        return json.JSONEncoder.default(self, obj)
from mpld3 import _display
_display.NumpyEncoder = NumpyEncoder

This is still an issue with
matplotlib==2.2.2 mpld3==0.3

I fix it manually by overwriting mpld3._display.NumpyEncoder in my program

#mpld3 hack
class NumpyEncoder(json.JSONEncoder):
    def default(self, obj):
        import numpy as np
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        return json.JSONEncoder.default(self, obj)
from mpld3 import _display
_display.NumpyEncoder = NumpyEncoder

Thank you very much!
You´re a genius!
And I know that because I barely know anything of python and I was able to fix it myself.

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