def test_read_ang(self, ref_path, mode): if mode == 'path': new = Table.load_ang(ref_path / 'simple.ang') elif mode == 'str': new = Table.load_ang(str(ref_path / 'simple.ang')) assert new.data.shape == (4,10) and \ new.labels == ['eu', 'pos', 'IQ', 'CI', 'ID', 'intensity', 'fit']
def test_sort(self): t = Table(np.array([[0,1,],[2,1,]]), {'v':(2,)}, ['test data'])\ .add('s',np.array(['b','a']))\ .sort_by('s') assert np.all(t.get('v')[:, 0] == np.array([2, 0]))
def test_write_read(self, default, tmp_path, mode): default.save(tmp_path / 'default.txt') if mode == 'path': new = Table.load(tmp_path / 'default.txt') elif mode == 'str': new = Table.load(str(tmp_path / 'default.txt')) assert all(default.data == new.data) and default.shapes == new.shapes
def test_join_invalid(self): x = np.random.random((5, 13)) a = Table(x, { 'F': (3, 3), 'v': (3, ), 's': (1, ) }, ['random test data']) with pytest.raises(KeyError): a.join(a)
def test_append(self): x = np.random.random((5, 13)) a = Table(x, { 'F': (3, 3), 'v': (3, ), 's': (1, ) }, ['random test data']) b = a.append(a) assert np.array_equal(b.data[:5].to_numpy(), b.data[5:].to_numpy())
def test_relationship_reference(self, update, ref_path, model, lattice): reference = ref_path / f'{lattice}_{model}.txt' o = Orientation(lattice=lattice) eu = o.related(model).as_Euler_angles(degrees=True) if update: coords = np.array([(1, i + 1) for i, x in enumerate(eu)]) Table(eu,{'Eulers':(3,)})\ .add('pos',coords)\ .save(reference) assert np.allclose(eu, Table.load(reference).get('Eulers'))
def test_from_table(self): cells = np.random.randint(60,100,3) size = np.ones(3)+np.random.rand(3) coords = grid_filters.coordinates0_point(cells,size).reshape(-1,3,order='F') z=np.ones(cells.prod()) z[cells[:2].prod()*int(cells[2]/2):]=0 t = Table(np.column_stack((coords,z)),{'coords':3,'z':1}) t = t.add('indicator',t.get('coords')[:,0]) g = Grid.from_table(t,'coords',['indicator','z']) assert g.N_materials == g.cells[0]*2 and (g.material[:,:,-1]-g.material[:,:,0] == cells[0]).all()
def test_sort_scalar(self): x = np.random.random((5, 13)) t = Table(x, { 'F': (3, 3), 'v': (3, ), 's': (1, ) }, ['random test data']) unsort = t.get('s') sort = t.sort_by('s').get('s') assert np.all(np.sort(unsort, 0) == sort)
def test_rename_equivalent(self): x = np.random.random((5, 13)) t = Table(x, { 'F': (3, 3), 'v': (3, ), 's': (1, ) }, ['random test data']) s = t.get('s') u = t.rename('s', 'u').get('u') assert np.all(s == u)
def test_Schmid(self, update, ref_path, lattice, mode): L = Orientation(lattice=lattice) reference = ref_path / f'{lattice}_{mode}.txt' P = L.Schmid(mode) if update: table = Table(P.reshape(-1, 9), {'Schmid': ( 3, 3, )}) table.save(reference) assert np.allclose(P, Table.load(reference).get('Schmid'))
def test_Schmid(self, update, ref_path, lattice): O = Orientation(lattice=lattice) # noqa for mode in ['slip', 'twin']: reference = ref_path / f'{lattice}_{mode}.txt' P = O.Schmid(N_slip='*') if mode == 'slip' else O.Schmid( N_twin='*') if update: table = Table(P.reshape(-1, 9), {'Schmid': ( 3, 3, )}) table.save(reference) assert np.allclose(P, Table.load(reference).get('Schmid'))
def test_append_invalid(self): x = np.random.random((5, 13)) a = Table(x, { 'F': (3, 3), 'v': (3, ), 's': (1, ) }, ['random test data']) b = Table(x, { 'F': (3, 3), 'u': (3, ), 's': (1, ) }, ['random test data']) with pytest.raises(KeyError): a.append(b)
def default(): """Simple Table.""" x = np.ones((5, 13), dtype=float) return Table(x, { 'F': (3, 3), 'v': (3, ), 's': (1, ) }, ['test data', 'contains five rows of only ones'])
def test_from_table_recover(self,tmp_path): cells = np.random.randint(60,100,3) size = np.ones(3)+np.random.rand(3) s = seeds.from_random(size,np.random.randint(60,100)) grid = Grid.from_Voronoi_tessellation(cells,size,s) coords = grid_filters.coordinates0_point(cells,size) t = Table(np.column_stack((coords.reshape(-1,3,order='F'),grid.material.flatten(order='F'))),{'c':3,'m':1}) assert grid_equal(grid.sort().renumber(),Grid.from_table(t,'c',['m']))
def test_from_table(self): grid = np.random.randint(60,100,3) size = np.ones(3)+np.random.rand(3) coords = grid_filters.cell_coord0(grid,size).reshape(-1,3,order='F') z=np.ones(grid.prod()) z[grid[:2].prod()*int(grid[2]/2):]=0 t = Table(np.column_stack((coords,z)),{'coords':3,'z':1}) g = Geom.from_table(t,'coords',['1_coords','z']) assert g.N_materials == g.grid[0]*2 and (g.material[:,:,-1]-g.material[:,:,0] == grid[0]).all()
def test_join(self): x = np.random.random((5, 13)) a = Table(x, { 'F': (3, 3), 'v': (3, ), 's': (1, ) }, ['random test data']) y = np.random.random((5, 3)) b = Table(y, {'u': (3, )}, ['random test data']) a.join(b) assert np.array_equal(a.get('u'), b.get('u'))
def test_from_table(self): N = np.random.randint(3,10) a = np.vstack((np.hstack((np.arange(N),np.arange(N)[::-1])), np.ones(N*2),np.zeros(N*2),np.ones(N*2),np.ones(N*2), np.ones(N*2), )).T t = Table(a,{'varying':1,'constant':4,'ones':1}) c = ConfigMaterial.from_table(t,**{'phase':'varying','O':'constant','homogenization':'ones'}) assert len(c['material']) == N for i,m in enumerate(c['material']): assert m['homogenization'] == 1 and (m['constituents'][0]['O'] == [1,0,1,1]).all()
def test_sort_component(self): x = np.random.random((5, 12)) t = Table(x, {'F': (3, 3), 'v': (3, )}, ['random test data']) unsort = t.get('4_F') t.sort_by('4_F') sort = t.get('4_F') assert np.all(np.sort(unsort, 0) == sort)
def test_sort(self): t = Table(np.array([[ 0, 1, ], [ 2, 1, ]]), {'v': (2, )}, ['test data']) t.add('s', np.array(['b', 'a'])) t.sort_by('s') assert np.all(t.get('1_v') == np.array([2, 0]).reshape(2, 1))
def test_from_table(self): N = np.random.randint(3, 10) a = np.vstack((np.hstack((np.arange(N), np.arange(N)[::-1])), np.ones(N * 2), np.zeros(N * 2), np.ones(N * 2))).T t = Table(a, {'varying': 2, 'constant': 2}) c = ConfigMaterial.from_table(t, constituents={ 'a': 'varying', 'b': '1_constant' }, c='2_constant') assert len(c['material']) == N for i, m in enumerate(c['material']): c = m['constituents'][0] assert m['c'] == 1 and c['b'] == 0 and c['a'] == [i, 1]
def test_ODF_cell(self, reference_dir, fractions, degrees, N): steps = np.array([144, 36, 36]) limits = np.array([360., 90., 90.]) rng = tuple(zip(np.zeros(3), limits)) weights = Table.load( reference_dir / 'ODF_experimental_cell.txt').get('intensity').flatten() Eulers = grid_filters.cell_coord0(steps, limits) Eulers = np.radians(Eulers) if not degrees else Eulers Eulers_r = Rotation.from_ODF(weights, Eulers.reshape(-1, 3, order='F'), N, degrees, fractions).as_Eulers(True) weights_r = np.histogramdd( Eulers_r, steps, rng)[0].flatten(order='F') / N * np.sum(weights) if fractions: assert np.sqrt(((weights_r - weights)**2).mean()) < 4
def test_relationship_reference(self, update, reference_dir, model, lattice): reference = os.path.join(reference_dir, f'{lattice}_{model}.txt') ori = Orientation(Rotation(), lattice) eu = np.array( [o.rotation.as_Eulers(degrees=True) for o in ori.related(model)]) if update: coords = np.array([(1, i + 1) for i, x in enumerate(eu)]) table = Table(eu, {'Eulers': (3, )}) table = table.add('pos', coords) table.save(reference) assert np.allclose(eu, Table.load(reference).get('Eulers'))
def test_ODF_node(self, reference_dir, degrees, N): steps = np.array([144, 36, 36]) limits = np.array([360., 90., 90.]) rng = tuple(zip(-limits / steps * .5, limits - limits / steps * .5)) weights = Table.load(reference_dir / 'ODF_experimental.txt').get('intensity') weights = weights.reshape(steps + 1, order='F')[:-1, :-1, :-1].reshape(-1, order='F') Eulers = grid_filters.node_coord0(steps, limits)[:-1, :-1, :-1] Eulers = np.radians(Eulers) if not degrees else Eulers Eulers_r = Rotation.from_ODF(weights, Eulers.reshape(-1, 3, order='F'), N, degrees).as_Eulers(True) weights_r = np.histogramdd( Eulers_r, steps, rng)[0].flatten(order='F') / N * np.sum(weights) assert np.sqrt(((weights_r - weights)**2).mean()) < 5
def test_read_ang_file(self, ref_path): f = open(ref_path / 'simple.ang') new = Table.load_ang(f) assert new.data.shape == (4,10) and \ new.labels == ['eu', 'pos', 'IQ', 'CI', 'ID', 'intensity', 'fit']
def test_read_strange(self, reference_dir, fname): with open(os.path.join(reference_dir, fname)) as f: Table.from_ASCII(f)
def test_read_strange(self, ref_path, fname): with open(ref_path / fname) as f: Table.load(f)
def test_invalid_initialization(self): x = np.random.random((5, 10)) with pytest.raises(ValueError): Table(x, {'F': (3, 3)})
def test_sort_revert(self): x = np.random.random((5, 12)) t = Table(x, {'F': (3, 3), 'v': (3, )}, ['random test data']) sort = t.sort_by('F[1,0]', ascending=False).get('F')[:, 1, 0] assert np.all(np.sort(sort, 0) == sort[::-1])
def test_write_read_file(self, default, tmp_path): with open(tmp_path / 'default.txt', 'w') as f: default.save(f) with open(tmp_path / 'default.txt') as f: new = Table.load(f) assert all(default.data == new.data) and default.shapes == new.shapes
def test_len(self, N): assert len(Table(np.random.rand(N, 3), {'X': 3})) == N