ml4a / ml4a

A python library and collection of notebooks for making art with machine learning.

Home Page:https://ml4a.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Json not serializable.

puterd opened this issue · comments

I'm using the audio t-sne notebook and getting a "not json serializable" error when I try to make a json file (both from the vintage drum machine data and my own). Have only used python 2.7 as I can't change the python kernel in the notebook using docker. Output is:


TypeError Traceback (most recent call last)
in ()
6 data = [{"path":os.path.abspath(f), "point":[x, y]} for f, x, y in zip(sound_paths, x_norm, y_norm)]
7 with open(tsne_path, 'w') as outfile:
----> 8 json.dump(data, outfile)
9
10 print("saved %s to disk!" % tsne_path)

/root/miniconda2/lib/python2.7/json/init.pyc in dump(obj, fp, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, encoding, default, sort_keys, **kw)
187 # could accelerate with writelines in some versions of Python, at
188 # a debuggability cost
--> 189 for chunk in iterable:
190 fp.write(chunk)
191

/root/miniconda2/lib/python2.7/json/encoder.pyc in _iterencode(o, _current_indent_level)
429 yield _floatstr(o)
430 elif isinstance(o, (list, tuple)):
--> 431 for chunk in _iterencode_list(o, _current_indent_level):
432 yield chunk
433 elif isinstance(o, dict):

/root/miniconda2/lib/python2.7/json/encoder.pyc in _iterencode_list(lst, _current_indent_level)
330 else:
331 chunks = _iterencode(value, _current_indent_level)
--> 332 for chunk in chunks:
333 yield chunk
334 if newline_indent is not None:

/root/miniconda2/lib/python2.7/json/encoder.pyc in _iterencode_dict(dct, _current_indent_level)
406 else:
407 chunks = _iterencode(value, _current_indent_level)
--> 408 for chunk in chunks:
409 yield chunk
410 if newline_indent is not None:

/root/miniconda2/lib/python2.7/json/encoder.pyc in _iterencode_list(lst, _current_indent_level)
330 else:
331 chunks = _iterencode(value, _current_indent_level)
--> 332 for chunk in chunks:
333 yield chunk
334 if newline_indent is not None:

/root/miniconda2/lib/python2.7/json/encoder.pyc in _iterencode(o, _current_indent_level)
440 raise ValueError("Circular reference detected")
441 markers[markerid] = o
--> 442 o = _default(o)
443 for chunk in _iterencode(o, _current_indent_level):
444 yield chunk

/root/miniconda2/lib/python2.7/json/encoder.pyc in default(self, o)
182
183 """
--> 184 raise TypeError(repr(o) + " is not JSON serializable")
185
186 def encode(self, o):

TypeError: 0.69805652 is not JSON serializable

@Davincb i fixed it by casting the floats32s to floats:

data = [{"path":os.path.abspath(img), "point":[float(x), float(y)]} for img, x, y in zip(images, tx, ty)]