def test_scalar_non_len1_first_dim(self): struct = ArrayStructure(1, [1]) orig = np.array([1, 1, 1]) array, dims = struct.nd_array_and_dims(orig, (3, 1), order=self.order) self.assertArrayEqual(array, [1]) self.assertEqual(dims, ())
def test_array_bigger_than_expected(self): # An array structure which has a length which is a product # of potential dimensions should not result in an array struct = ArrayStructure(2, [1, 2, 3, 4, 5, 6]) orig = np.array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]) with self.assertRaises(_UnstructuredArrayException): struct.nd_array_and_dims(orig, (2, 3, 2), order=self.order)
def test_orig_array_and_target_shape_inconsistent(self): # An array structure which has a length which is a product # of potential dimensions should not result in an array struct = ArrayStructure(2, [1, 2, 3]) orig = np.array([1, 1, 2, 2, 3, 3]) msg = 'Original array and target shape do not match up.' with self.assertRaisesRegexp(ValueError, msg): struct.nd_array_and_dims(orig, (2, 3, 2), order=self.order)
def test_single_vector(self): orig = construct_nd(np.array([1, 2]), 0, (2, 1, 3)) flattened = orig.flatten(order=self.order) struct = ArrayStructure.from_array(flattened) array, dims = struct.nd_array_and_dims(flattened, (2, 1, 3), order=self.order) self.assertArrayEqual(array, [1, 2]) self.assertEqual(dims, (0,))
def test_structure_creation(self): # Test that the appropriate dictionary containing ArrayStructures is # computed when constructing a GroupStructure from_component_arrays. array = np.arange(6) expected_structure = {'a': ArrayStructure.from_array(array)} grp = GroupStructure.from_component_arrays({'a': array}) self.assertEqual(grp.length, 6) self.assertEqual(grp._cmpt_structure, expected_structure)
def test_single_vector_extra_dimension(self): orig = construct_nd(np.array([1, 2]), 1, (3, 2)) flattened = orig.flatten(order=self.order) struct = ArrayStructure.from_array(flattened) # Add another dimension on flattened, making it a (6, 2). input_array = np.vstack([flattened, flattened + 100]).T array, dims = struct.nd_array_and_dims(input_array, (3, 1, 2, 1), order=self.order) self.assertArrayEqual(array, [[1, 101], [2, 102]]) self.assertEqual(dims, (2,))
def test_1d_range(self): a = np.arange(6) self.assertEqual(self.struct_from_arr(a), ArrayStructure(1, list(range(6))))
def test_multi_dim_array(self): with self.assertRaises(ValueError): ArrayStructure.from_array(np.arange(12).reshape(3, 4))
def test_not_an_array(self): # Support lists as an argument. self.assertEqual(ArrayStructure.from_array([1, 2, 3]), ArrayStructure(1, [1, 2, 3]))
def test_shared_first_dimension(self): # One 2d potential as well as one 3d, using the same first dimension. array_structures = regular_array_structures((4, 2, 3)) array_structures['bc combined'] = ArrayStructure(4, np.arange(6)) self.assert_potentials(24, array_structures, [['a', 'bc combined'], ['a', 'b', 'c']])
def test_1d_len_1(self): a = np.arange(1) self.assertEqual(self.struct_from_arr(a), ArrayStructure(1, a))
def test_len_1_3d(self): # Setup a case which triggers an IndexError when identifying # the stride, but the result should still be correct. sub = np.arange(2) a = construct_nd(sub, 1, (1, 1, 1)) self.assertEqual(self.struct_from_arr(a), ArrayStructure(1, sub))
def struct_from_arr(self, nd_array): return ArrayStructure.from_array(nd_array.flatten())
def test_1d_over_3d_third_dim(self): sub = np.array([-1, 3, 1, 2]) a = construct_nd(sub, 2, (3, 2, 4)) self.assertEqual(self.struct_from_arr(a), ArrayStructure(1, sub))
def test_multiple_potentials(self): # More than one potential dimension for dim 1. array_structures = regular_array_structures((4, 2, 3)) array_structures['shared b'] = ArrayStructure(4, [-10, 4]) self.assert_potentials(24, array_structures, [['a', 'b', 'c'], ['a', 'shared b', 'c']])
def test_non_viable_element(self): # One 2d potential as well as one 3d, using the same first dimension. array_structures = regular_array_structures((4, 2, 3)) array_structures.pop('c') array_structures['strange_length'] = ArrayStructure(4, np.arange(5)) self.assert_potentials(24, array_structures, [])
def test_3d_ones(self): a = np.ones([10, 2, 1]) self.assertEqual(self.struct_from_arr(a), ArrayStructure(1, [1]))
def test_1d_over_2d_first_dim_manual(self): sub = np.array([10, 10, 20, 20]) self.assertEqual(self.struct_from_arr(sub), ArrayStructure(2, [10, 20]))
def test_3d_first_dimension(self): flattened = np.array([1, 1, 1, 2, 2, 2]) self.assertEqual(ArrayStructure.from_array(flattened), ArrayStructure(3, [1, 2]))
def test_1d_over_3d_first_dim(self): sub = np.array([-1, 3, 1, 2]) a = construct_nd(sub, 0, (4, 2, 3)) self.assertEqual(self.struct_from_arr(a), ArrayStructure(6, sub))
def test_1d_over_3d_second_dim(self): sub = np.array([-1, 3, 1, 2]) a = construct_nd(sub, 1, (2, 4, 3)) self.assertEqual(self.struct_from_arr(a), ArrayStructure(3, sub))
def test_1d(self): a = np.array([-1, 3, 1, 2]) self.assertEqual(self.struct_from_arr(a), ArrayStructure(1, a))
def test_multiple_potentials(self): # More than one potential dimension for dim 1. array_structures = regular_array_structures((4, 2, 3)) array_structures["shared b"] = ArrayStructure(4, [-10, 4]) self.assert_potentials(24, array_structures, [["a", "b", "c"], ["a", "shared b", "c"]])