From 3b66a68f198b0774dc4d3b3baa5be36552928471 Mon Sep 17 00:00:00 2001 From: Loic Huder Date: Fri, 30 Apr 2021 11:50:11 +0200 Subject: [PATCH] Jsonize NaN and Infinity as strings --- jupyterlab_hdf/tests/test_data.py | 8 ++++++++ jupyterlab_hdf/util.py | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/jupyterlab_hdf/tests/test_data.py b/jupyterlab_hdf/tests/test_data.py index 146503d..6c63d89 100644 --- a/jupyterlab_hdf/tests/test_data.py +++ b/jupyterlab_hdf/tests/test_data.py @@ -22,6 +22,7 @@ def setUp(self): h5file["complex"] = COMPLEX h5file["scalar"] = SCALAR h5file["empty"] = h5py.Empty(">f8") + h5file["NaNs"] = [np.NaN, np.inf, np.NINF, 0] def test_oneD_dataset(self): response = self.tester.get(["data", "test_file.h5"], params={"uri": "/oneD_dataset"}) @@ -88,3 +89,10 @@ def test_empty_dataset(self): assert response.status_code == 200 payload = response.json() assert payload is None + + def test_dataset_with_NaNs(self): + response = self.tester.get(["data", "test_file.h5"], params={"uri": "/NaNs"}) + + assert response.status_code == 200 + payload = response.json() + assert payload == ["NaN", "Infinity", "-Infinity", 0] diff --git a/jupyterlab_hdf/util.py b/jupyterlab_hdf/util.py index df2272e..01c6c6e 100644 --- a/jupyterlab_hdf/util.py +++ b/jupyterlab_hdf/util.py @@ -329,6 +329,12 @@ def jsonize(v): return [v.real, v.imag] if isinstance(v, h5py.Empty): return None + if isinstance(v, float) and np.isnan(v): + return "NaN" + if isinstance(v, float) and np.isposinf(v): + return "Infinity" + if isinstance(v, float) and np.isneginf(v): + return "-Infinity" return v