def test_build_arrays_unstructured_ndim_c_order(self): # Passing an unstructured array to build_arrays, should result in the # appropriately shaped array, plus any trailing dimensions. grp = GroupStructure(6, {'a': None}, array_order='c') orig = np.array([1, 2, 3, 4, 5, 6]).reshape(2, 3) orig = np.dstack([orig, orig + 10]) r = grp.build_arrays((2, 3), {'a': orig.reshape((-1, 2), order='c')}) self.assert_built_array('a', r, (orig, (0, 1)))
def test_simple_3d_structure(self): # Construct a structure representing a (3, 2, 4) group and assert # that the result is of the expected form. array_structures = { 'a': ArrayStructure(1, [1, -1, 2]), 'b': ArrayStructure(3, [1, -1]), 'c': ArrayStructure(6, [1, -1, 2, 3]) } structure = GroupStructure(24, array_structures, array_order='f') expected = ([('a', array_structures['a']), ('b', array_structures['b']), ('c', array_structures['c'])], ) self.assertEqual(structure.possible_structures(), expected)
def test_simple_3d_structure(self): # Construct a structure representing a (3, 2, 4) group and assert # that the result is of the expected form. array_structures = { "a": ArrayStructure(1, [1, -1, 2]), "b": ArrayStructure(3, [1, -1]), "c": ArrayStructure(6, [1, -1, 2, 3]), } structure = GroupStructure(24, array_structures, array_order="f") expected = ([ ("a", array_structures["a"]), ("b", array_structures["b"]), ("c", array_structures["c"]), ], ) self.assertEqual(structure.possible_structures(), expected)
def test_build_arrays_regular_f_order(self): # Construct simple orthogonal 1d array structures, adding a trailing # dimension to the second, and assert the result of build_arrays # produces the required result. elements = regular_array_structures((2, 3)) a = elements['a'].construct_array(6) b = elements['b'].construct_array(6) # Make b 2 dimensional. b = np.vstack([b, b + 100]).T grp = GroupStructure(6, elements, array_order='f') result = grp.build_arrays((2, 3), {'a': a, 'b': b}) self.assert_built_array('a', result, ([0, 1], (0, ))) self.assert_built_array('b', result, ([[0, 100], [1, 101], [2, 102]], (1, )))
def test_structured_array_not_applicable(self): # Just because an array has a possible structure, does not mean it # gets used. Check that 'd' which would make a good 1D array, doesn't # get used in a specific shape. elements = regular_array_structures((2, 2, 3)) elements['d'] = ArrayStructure(3, np.arange(4)) grp = GroupStructure(12, elements, array_order='f') d = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3]).reshape((3, 4), order='f') expected = np.array([[[0, 1, 2], [0, 2, 3]], [[0, 1, 3], [1, 2, 3]]]) r = grp.build_arrays( (2, 2, 3), { 'a': np.arange(12), 'b': np.arange(12), 'c': np.arange(12), 'd': d.flatten(order='f') }) self.assert_built_array('d', r, (expected, (0, 1, 2)))
def assert_potentials(self, length, array_structures, expected): structure = GroupStructure(length, array_structures, array_order='f') allowed = structure.possible_structures() names = [[name for (name, _) in allowed_structure] for allowed_structure in allowed] self.assertEqual(names, expected)
def test_build_arrays_unstructured(self): # Check that an unstructured array gets reshaped appropriately. grp = GroupStructure(6, {'a': None}, array_order='c') orig = np.array([1, 2, 3, 4, 5, 6]).reshape(2, 3) r = grp.build_arrays((2, 3), {'a': orig.flatten(order='c')}) self.assert_built_array('a', r, (orig, (0, 1)))