def test_from_npy_a(self) -> None: a1 = np.arange(20) with temp_file('.npy') as fp: with open(fp, 'wb') as f: NPYConverter.to_npy(f, a1) with open(fp, 'rb') as f: a2, _ = NPYConverter.from_npy(f, {}) self.assertTrue((a1 == a2).all())
def test_from_npy_c(self) -> None: with temp_file('.npy') as fp: with open(fp, 'wb') as f: f.write(b'foo') with open(fp, 'rb') as f: # invaliud header raises with self.assertRaises(ErrorNPYDecode): a2, _ = NPYConverter.from_npy(f, {})
def test_from_npy_g(self) -> None: a1 = np.array([2, 3, 4]) with temp_file('.npy') as fp: with open(fp, 'wb') as f: NPYConverter.to_npy(f, a1) with open(fp, 'rb') as f: a2, _ = NPYConverter.from_npy(f, {}, memory_map=True) self.assertEqual(a2.tolist(), [2, 3, 4])
def test_from_npy_f(self) -> None: a1 = np.array([None, 'foo', 3], dtype=object) with temp_file('.npy') as fp: with open(fp, 'wb') as f: write_array(f, a1, version=(1, 0)) with open(fp, 'rb') as f: # invalid object dtype with self.assertRaises(ErrorNPYDecode): a2, _ = NPYConverter.from_npy(f, {})
def test_from_npy_e(self) -> None: a1 = np.array([2, 3, 4]) with temp_file('.npy') as fp: with open(fp, 'wb') as f: write_array(f, a1, version=(3, 0)) with open(fp, 'rb') as f: # invlid header; only version 1,0 is supported with self.assertRaises(ErrorNPYDecode): a2, _ = NPYConverter.from_npy(f, {})
def test_from_npy_d(self) -> None: a1 = np.arange(12).reshape(2, 3, 2) with temp_file('.npy') as fp: with open(fp, 'wb') as f: write_array(f, a1, version=(1, 0)) with open(fp, 'rb') as f: # invalid shape with self.assertRaises(ErrorNPYDecode): a2, _ = NPYConverter.from_npy(f, {})
def test_frame_to_npy_b(self, a1: Frame) -> None: header_decode_cache: HeaderDecodeCacheType = {} with temp_file('.npy') as fp: with open(fp, 'wb') as f: NPYConverter.to_npy(f, a1) with open(fp, 'rb') as f: a2, mm = NPYConverter.from_npy(f, header_decode_cache, memory_map=True, ) if a2.dtype.kind in DTYPE_INEXACT_KINDS: self.assertAlmostEqualArray(a1, a2) else: self.assertTrue((a1 == a2).all()) self.assertTrue(a1.shape == a2.shape)
def test_frame_to_npy_a(self, a1: Frame) -> None: header_decode_cache: HeaderDecodeCacheType = {} with temp_file('.npy') as fp: with open(fp, 'wb') as f: NPYConverter.to_npy(f, a1) # check compatibility with built-in NPY reading a2 = np.load(fp) if a2.dtype.kind in DTYPE_INEXACT_KINDS: self.assertAlmostEqualArray(a1, a2) else: self.assertTrue((a1 == a2).all()) self.assertTrue(a1.shape == a2.shape) with open(fp, 'rb') as f: a3, _ = NPYConverter.from_npy(f, header_decode_cache) if a3.dtype.kind in DTYPE_INEXACT_KINDS: self.assertAlmostEqualArray(a1, a3) else: self.assertTrue((a1 == a3).all()) self.assertTrue(a1.shape == a3.shape)