mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-01 11:58:11 +08:00
X float16 support (#2406)
* float16 support * fix type checks * PR review comments * add tests for custom json encoder; rename and comment for posterity * lint * typos
This commit is contained in:
committed by
Colin Megill
parent
eaae6df5e3
commit
154d099fef
@@ -0,0 +1,58 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
|
||||
from backend.common.utils.utils import (
|
||||
jsonify_strict,
|
||||
)
|
||||
|
||||
|
||||
class TestJsonifyStrict(unittest.TestCase):
|
||||
def test_jsonify_numpy_general_cases(self):
|
||||
self.assertEqual(jsonify_strict({}), "{}")
|
||||
self.assertEqual(jsonify_strict({"a": [], "b": "hello", "c": True}), '{"a": [], "b": "hello", "c": true}')
|
||||
|
||||
def test_jsonify_numpy_float_edges(self):
|
||||
with self.assertRaises(ValueError):
|
||||
jsonify_strict({"nan": [np.nan]})
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
jsonify_strict({"pinf": [np.PINF]})
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
jsonify_strict({"ninf": [np.NINF]})
|
||||
|
||||
def test_jsonify_numpy_ndarray(self):
|
||||
values = {
|
||||
"integer": [
|
||||
np.int8(0),
|
||||
np.int16(1),
|
||||
np.int32(2),
|
||||
np.int64(3),
|
||||
np.uint8(4),
|
||||
np.uint16(5),
|
||||
np.uint32(6),
|
||||
np.uint64(7),
|
||||
],
|
||||
"floating": [
|
||||
np.float16(100.0),
|
||||
np.float32(101.0),
|
||||
np.float64(102.0),
|
||||
],
|
||||
}
|
||||
# these just confirm our test assumptions
|
||||
self.assertTrue(isinstance(values["floating"][0], np.float16))
|
||||
self.assertTrue(isinstance(values["floating"][1], np.float32))
|
||||
self.assertTrue(isinstance(values["floating"][2], np.float64))
|
||||
self.assertTrue(isinstance(values["integer"][0], np.int8))
|
||||
self.assertTrue(isinstance(values["integer"][1], np.int16))
|
||||
self.assertTrue(isinstance(values["integer"][2], np.int32))
|
||||
self.assertTrue(isinstance(values["integer"][3], np.int64))
|
||||
self.assertTrue(isinstance(values["integer"][4], np.uint8))
|
||||
self.assertTrue(isinstance(values["integer"][5], np.uint16))
|
||||
self.assertTrue(isinstance(values["integer"][6], np.uint32))
|
||||
self.assertTrue(isinstance(values["integer"][7], np.uint64))
|
||||
# the actual test!
|
||||
self.assertEqual(
|
||||
jsonify_strict(values),
|
||||
'{"floating": [100.0, 101.0, 102.0], "integer": [0, 1, 2, 3, 4, 5, 6, 7]}',
|
||||
)
|
||||
Reference in New Issue
Block a user