def test_numpy_type_smaller_than(self): eight = t.NumpyType(t.NumpyKinds.UNSIGNED, t.NumpySizes.BYTE) also_eight = t.NumpyType(t.NumpyKinds.UNSIGNED, t.NumpySizes.BYTE) sixteen = t.NumpyType(t.NumpyKinds.UNSIGNED, t.NumpySizes.SHORT) self.assertTrue(eight.is_smaller_than(sixteen)) self.assertFalse(eight.is_smaller_than(also_eight)) self.assertFalse(sixteen.is_smaller_than(eight)) return
def test_numpy_type_equality(self): t1 = t.NumpyType(t.NumpyKinds.INTEGER, t.NumpySizes.BYTE) t2 = t.NumpyType(t.NumpyKinds.INTEGER, t.NumpySizes.BYTE) t3 = t.NumpyType(t.NumpyKinds.UNSIGNED, t.NumpySizes.BYTE) t4 = t.NumpyType(t.NumpyKinds.INTEGER, t.NumpySizes.DOUBLE) self.assertTrue(t1 == t2) self.assertFalse(t1 == t3) self.assertFalse(t1 == t4) self.assertFalse(t1 == 1) return
def test_integer_hash_transform(self): t = c.IntegerHashTransformation( np.array([1, 2, 3], dtype=np.uint8), fbt.NumpyType(fbt.NumpyKinds.UNSIGNED, fbt.NumpySizes.BYTE)) self.assertEqual(c.IntegerTransformTypes.HASH, t.transform_type) self.assertEqual(3, len(t.key_values)) self.assertEqual(1, t.key_values[0]) self.assertEqual( t.key_values_type, fbt.NumpyType(fbt.NumpyKinds.UNSIGNED, fbt.NumpySizes.BYTE)) ts = '{}'.format(t) self.assertTrue('key_values_type=' in ts and 'key_values=' in ts) return
def test_hash_decompression(self): arr = id.integer_hash_decompression( np.array([1, 1, 0, 2], dtype=np.uint8), cd.IntegerHashTransformation( key_values=np.array([0, -1000, 1000], dtype=np.int16), key_value_type=t.NumpyType(t.NumpyKinds.INTEGER, t.NumpySizes.SHORT) ) ) self.validate_hash_decompression(arr) return
def integer_minimize_decompression(arr: np.array, transform: IntegerMinimizeTransformation) -> np.array: """ Decompresses a minimize transform :param arr: minimized array :param transform: transformation info :return: un-minimized array """ logging.debug('decompressing minimized array with info: {}'.format(transform)) ret_array_type = t.NumpyType( kind=t.NumpyKinds.from_dtype(arr.dtype), size=t.NumpySizes.DOUBLE # make it as big as possible then shrink after addition ) return downcast_integers(arr.astype(ret_array_type.to_dtype()) + transform.reference_value)[0]
def test_numpy_type_errors(self): nt = t.NumpyType(t.NumpyKinds.INTEGER, None) with self.assertRaises(x.NumpyDtypeSizeInvalidException): nt.to_dtype() nt.kind = t.NumpyKinds.UNSIGNED with self.assertRaises(x.NumpyDtypeSizeInvalidException): nt.to_dtype() nt.kind = t.NumpyKinds.FLOAT with self.assertRaises(x.NumpyDtypeSizeInvalidException): nt.to_dtype() nt.kind = None with self.assertRaises(x.NumpyDtypeKindInvalidException): nt.to_dtype() return
def integer_derivative_decompression(arr: np.array, transform: IntegerElementWiseTransformation) -> np.array: """ Decompresses an element-wise derivative transform :param arr: derivative array :param transform: transformation info :return: decompressed array """ logging.debug('decompressing derivative array with info: {}'.format(transform)) ret_array_type = t.NumpyType( kind=t.NumpyKinds.from_dtype(arr.dtype), size=t.NumpySizes.DOUBLE ) ret_array = np.cumsum(arr.astype(ret_array_type.to_dtype())) + transform.reference_value ret_array = np.insert(ret_array, 0, transform.reference_value) return downcast_integers(ret_array)[0]
def test_integer_catch_all(self): arr = id.integer_decompression_from_transform( np.array([1, 2, 3, 4, 5], dtype=np.uint8), cd.IntegerMinimizeTransformation(1000) ) self.validate_minimize_decompression(arr) arr = id.integer_decompression_from_transform( np.array([1, 0, 1, 0], dtype=np.uint8), cd.IntegerElementWiseTransformation(1000) ) self.validate_elem_decompression(arr) arr = id.integer_decompression_from_transform( np.array([1, 1, 0, 2], dtype=np.uint8), cd.IntegerHashTransformation( key_values=np.array([0, -1000, 1000], dtype=np.int16), key_value_type=t.NumpyType(t.NumpyKinds.INTEGER, t.NumpySizes.SHORT) ) ) self.validate_hash_decompression(arr) return
def test_numpy_type_to_type(self): self.assertEqual( np.int8, t.NumpyType(t.NumpyKinds.INTEGER, t.NumpySizes.BYTE).to_dtype()) self.assertEqual( np.int16, t.NumpyType(t.NumpyKinds.INTEGER, t.NumpySizes.SHORT).to_dtype()) self.assertEqual( np.int32, t.NumpyType(t.NumpyKinds.INTEGER, t.NumpySizes.SINGLE).to_dtype()) self.assertEqual( np.int64, t.NumpyType(t.NumpyKinds.INTEGER, t.NumpySizes.DOUBLE).to_dtype()) self.assertEqual( np.uint8, t.NumpyType(t.NumpyKinds.UNSIGNED, t.NumpySizes.BYTE).to_dtype()) self.assertEqual( np.uint16, t.NumpyType(t.NumpyKinds.UNSIGNED, t.NumpySizes.SHORT).to_dtype()) self.assertEqual( np.uint32, t.NumpyType(t.NumpyKinds.UNSIGNED, t.NumpySizes.SINGLE).to_dtype()) self.assertEqual( np.uint64, t.NumpyType(t.NumpyKinds.UNSIGNED, t.NumpySizes.DOUBLE).to_dtype()) self.assertEqual( np.float16, t.NumpyType(t.NumpyKinds.FLOAT, t.NumpySizes.SHORT).to_dtype()) self.assertEqual( np.float32, t.NumpyType(t.NumpyKinds.FLOAT, t.NumpySizes.SINGLE).to_dtype()) self.assertEqual( np.float64, t.NumpyType(t.NumpyKinds.FLOAT, t.NumpySizes.DOUBLE).to_dtype()) return
def test_numpy_type_init(self): d = t.NumpyType(t.NumpyKinds.FLOAT, t.NumpySizes.DOUBLE) self.assertEqual(t.NumpyKinds.FLOAT, d.kind) self.assertEqual(t.NumpySizes.DOUBLE, d.size) return