def test_identity_dimension_when_existing_dim_split_with_wildcard(self): arr = ArrayWithUnits(np.zeros((12, )), [ContrivedDimension(10)]) rs = arr.reshape((2, -1, 3)) self.assertIsInstance(rs, ArrayWithUnits) self.assertIsInstance(rs.dimensions[0], IdentityDimension) self.assertIsInstance(rs.dimensions[1], IdentityDimension) self.assertIsInstance(rs.dimensions[2], IdentityDimension)
def test_can_create_new_array_from_example(self): arr = ArrayWithUnits(np.zeros((100, 10)), [ContrivedDimension(10), ContrivedDimension2(10)]) arr2 = ArrayWithUnits.from_example(np.zeros((90, 5)), arr) self.assertSequenceEqual(arr.dimensions, arr2.dimensions) self.assertEqual((90, 5), arr2.shape)
def test_can_add_dimension_of_size_one_to_1d_array(self): arr = ArrayWithUnits(np.zeros((3, )), [ContrivedDimension(10)]) rs = arr.reshape((1, 1, arr.size)) self.assertIsInstance(rs, ArrayWithUnits) self.assertIsInstance(rs.dimensions[0], IdentityDimension) self.assertIsInstance(rs.dimensions[1], IdentityDimension) self.assertIsInstance(rs.dimensions[2], ContrivedDimension)
def test_zeros(self): arr = ArrayWithUnits(np.zeros((100, 10)), [ContrivedDimension(10), ContrivedDimension2(10)]) arr2 = ArrayWithUnits.zeros(arr) self.assertEqual(arr.shape, arr2.shape) self.assertSequenceEqual(arr.dimensions, arr2.dimensions) np.testing.assert_allclose(arr2, 0)
def test_can_maintain_array_with_units_when_squeezing_2d(self): arr = ArrayWithUnits(np.zeros((1, 10)), [ContrivedDimension(10), ContrivedDimension2(10)]) squeezed = arr.squeeze() self.assertEqual((10, ), squeezed.shape) self.assertIsInstance(squeezed, ArrayWithUnits) self.assertIsInstance(squeezed.dimensions[0], ContrivedDimension2)
def test_can_reshape_and_downgrade_to_identity_dimension(self): arr = ArrayWithUnits(np.zeros((100, 10)), [ContrivedDimension(10), ContrivedDimension2(10)]) flattened = arr.reshape((-1, )) self.assertEqual((1000, ), flattened.shape) self.assertIsInstance(flattened, ArrayWithUnits) self.assertIsInstance(flattened.dimensions[0], IdentityDimension)
def test_size_is_not_modified_on_example_dimensions(self): arr = ArrayWithUnits(np.zeros((100, 10)), [ContrivedDimension(10), ContrivedDimension2(10)]) arr2 = ArrayWithUnits.from_example(np.zeros((90, 5)), arr) self.assertEqual(100, arr.dimensions[0].size) self.assertEqual(10, arr.dimensions[1].size) self.assertEqual(90, arr2.dimensions[0].size) self.assertEqual(5, arr2.dimensions[1].size)
def test_zeros_like(self): arr = ArrayWithUnits(np.zeros((3, 2, 10)), [ ContrivedDimension(10), IdentityDimension(), ContrivedDimension2(10) ]) zeros = arr.zeros_like() self.assertEqual(zeros.shape, arr.shape) self.assertEqual(zeros.dimensions, arr.dimensions) np.testing.assert_allclose(0, zeros)
def test_can_maintain_array_with_units_when_reshaping_3d(self): arr = ArrayWithUnits(np.zeros((3, 1, 10)), [ ContrivedDimension(10), IdentityDimension(), ContrivedDimension2(10) ]) squeezed = arr.reshape((3, 10)) self.assertEqual((3, 10), squeezed.shape) self.assertIsInstance(squeezed, ArrayWithUnits) self.assertIsInstance(squeezed.dimensions[0], ContrivedDimension) self.assertIsInstance(squeezed.dimensions[1], ContrivedDimension2)
def test_squeeze_with_no_single_dimension_is_unchanged(self): arr = ArrayWithUnits(np.zeros((3, 2, 10)), [ ContrivedDimension(10), IdentityDimension(), ContrivedDimension2(10) ]) squeezed = arr.squeeze() self.assertEqual((3, 2, 10), squeezed.shape) self.assertIsInstance(squeezed, ArrayWithUnits) self.assertIsInstance(squeezed.dimensions[0], ContrivedDimension) self.assertIsInstance(squeezed.dimensions[1], IdentityDimension) self.assertIsInstance(squeezed.dimensions[2], ContrivedDimension2)
def test_max_supports_multiple_axes(self): raw = np.random.random_sample((10, 9, 8, 7)) arr = ArrayWithUnits(raw, dimensions=[ ContrivedDimension(10), IdentityDimension(), IdentityDimension(), ContrivedDimension2(10) ]) result = arr.max(axis=(1, 2)) self.assertEqual((10, 7), result.shape) self.assertIsInstance(result, ArrayWithUnits) self.assertEqual(2, len(result.dimensions)) self.assertIsInstance(result.dimensions[0], ContrivedDimension) self.assertIsInstance(result.dimensions[1], ContrivedDimension2)
def test_maintain_array_with_units_with_boolean_condition(self): arr = ArrayWithUnits( np.random.random_sample((10, 100)) - 0.5, [ContrivedDimension(10), ContrivedDimension2(10)]) binary = arr >= 0 self.assertIsInstance(binary, ArrayWithUnits) self.assertIsInstance(arr.dimensions[0], ContrivedDimension) self.assertIsInstance(arr.dimensions[1], ContrivedDimension2)
def test_max_supports_keepdims(self): raw = np.random.random_sample((10, 9, 8, 7)) arr = ArrayWithUnits(raw, dimensions=[ ContrivedDimension(10), ContrivedDimension2(10), ContrivedDimension(10), ContrivedDimension2(10) ]) result = arr.max(axis=(1, 2), keepdims=True) self.assertEqual((10, 1, 1, 7), result.shape) self.assertIsInstance(result, ArrayWithUnits) self.assertEqual(4, len(result.dimensions)) print result.dimensions self.assertIsInstance(result.dimensions[0], ContrivedDimension) self.assertIsInstance(result.dimensions[1], IdentityDimension) self.assertIsInstance(result.dimensions[2], IdentityDimension) self.assertIsInstance(result.dimensions[3], ContrivedDimension2)
def __new__(cls, arr, dimensions): return ArrayWithUnits.__new__(cls, arr, dimensions)
def test_zeros_dtype(self): arr = ArrayWithUnits(np.zeros((100, 10), dtype=np.complex128), [ContrivedDimension(10), ContrivedDimension2(10)]) arr2 = ArrayWithUnits.zeros(arr) self.assertEqual(arr.dtype, arr2.dtype)
def test_assigns_size_where_appropriate(self): arr = ArrayWithUnits(np.zeros((100, 10)), [ContrivedDimension(10), ContrivedDimension2(10)]) self.assertEqual(10, arr.dimensions[1].size)