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

fix: JSON dump fails on numpy int, float and array types #198

Merged
merged 7 commits into from
May 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions qcodes/data/gnuplot_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import math
import json

from qcodes.utils.helpers import deep_update
from qcodes.utils.helpers import deep_update, NumpyJSONEncoder
from .data_array import DataArray
from .format import Formatter

Expand Down Expand Up @@ -297,7 +297,7 @@ def write_metadata(self, data_set, read_first=True):
fn = io_manager.join(location, self.metadata_file)
with io_manager.open(fn, 'w', encoding='utf8') as snap_file:
json.dump(data_set.metadata, snap_file, sort_keys=True,
indent=4, ensure_ascii=False)
indent=4, ensure_ascii=False, cls=NumpyJSONEncoder)

def read_metadata(self, data_set):
io_manager = data_set.io
Expand Down
44 changes: 44 additions & 0 deletions qcodes/tests/test_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from unittest import TestCase
import numpy as np
import json

from qcodes.utils.helpers import NumpyJSONEncoder


class TestNumpyJson(TestCase):

def setUp(self):
self.metadata = {
'name': 'Rapunzel',
'age': np.int64(12),
'height': np.float64(112.234),
'scores': np.linspace(0, 42, num=3),
# include some regular values to ensure they work right
# with our encoder
'weight': 19,
'length': 45.23,
'points': [12, 24, 48]
}

def test_numpy_fail(self):
metadata = self.metadata
with self.assertRaises(TypeError):
json.dumps(metadata, sort_keys=True, indent=4, ensure_ascii=False)

def test_numpy_good(self):
metadata = self.metadata
data = json.dumps(metadata, sort_keys=True, indent=4,
ensure_ascii=False, cls=NumpyJSONEncoder)
data_dict = json.loads(data)

metadata = {
'name': 'Rapunzel',
'age': 12,
'height': 112.234,
'scores': [0, 21, 42],
'weight': 19,
'length': 45.23,
'points': [12, 24, 48]
}

self.assertEqual(metadata, data_dict)
15 changes: 15 additions & 0 deletions qcodes/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@
import sys
import io
import numpy as np
import json

_tprint_times = {}


class NumpyJSONEncoder(json.JSONEncoder):
"""Return numpy types as standard types."""
# http://stackoverflow.com/questions/27050108/convert-numpy-type-to-python
# http://stackoverflow.com/questions/9452775/converting-numpy-dtypes-to-native-python-types/11389998#11389998
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NumpyJSONEncoder, self).default(obj)

def tprint(string, dt=1, tag='default'):
""" Print progress of a loop every dt seconds """
ptime = _tprint_times.get(tag, 0)
Expand Down