DAMASK_EICMD/python/tests/test_Table.py

198 lines
7.0 KiB
Python
Raw Normal View History

import pytest
import numpy as np
from damask import Table
2019-12-05 10:27:47 +05:30
@pytest.fixture
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'])
2019-12-05 10:27:47 +05:30
@pytest.fixture
def ref_path(ref_path_base):
2019-12-05 10:27:47 +05:30
"""Directory containing reference results."""
return ref_path_base/'Table'
class TestTable:
2020-05-16 20:53:05 +05:30
2020-11-14 23:54:31 +05:30
def test_repr(self,default):
print(default)
@pytest.mark.parametrize('N',[10,40])
def test_len(self,N):
assert len(Table(np.random.rand(N,3),{'X':3})) == N
2020-11-14 23:54:31 +05:30
2019-12-05 15:17:36 +05:30
def test_get_scalar(self,default):
d = default.get('s')
assert np.allclose(d,1.0) and d.shape[1:] == (1,)
def test_get_vector(self,default):
2019-12-05 10:40:27 +05:30
d = default.get('v')
assert np.allclose(d,1.0) and d.shape[1:] == (3,)
2019-12-05 15:17:36 +05:30
def test_get_tensor(self,default):
d = default.get('F')
2020-05-16 20:53:05 +05:30
assert np.allclose(d,1.0) and d.shape[1:] == (3,3)
2019-12-05 15:17:36 +05:30
def test_set(self,default):
d = default.set('F',np.zeros((5,3,3)),'set to zero').get('F')
assert np.allclose(d,0.0) and d.shape[1:] == (3,3)
2020-05-16 20:53:05 +05:30
def test_set_component(self,default):
d = default.set('F[0,0]',np.zeros((5)),'set to zero').get('F')
assert np.allclose(d[...,0,0],0.0) and d.shape[1:] == (3,3)
def test_labels(self,default):
assert default.labels == ['F','v','s']
def test_add(self,default):
d = np.random.random((5,9))
assert np.allclose(d,default.add('nine',d,'random data').get('nine'))
def test_isclose(self,default):
assert default.isclose(default).all()
def test_allclose(self,default):
assert default.allclose(default)
@pytest.mark.parametrize('N',[1,3,4])
def test_slice(self,default,N):
mask = np.random.choice([True,False],len(default))
assert len(default[:N]) == 1+N
assert len(default[:N,['F','s']]) == 1+N
assert len(default[mask,['F','s']]) == np.count_nonzero(mask)
assert default[mask,['F','s']] == default[mask][['F','s']] == default[['F','s']][mask]
assert default[np.logical_not(mask),['F','s']] != default[mask][['F','s']]
assert default[N:].get('F').shape == (len(default)-N,3,3)
assert default[:N,['v','s']].data.equals(default['v','s'][:N].data)
@pytest.mark.parametrize('mode',['str','path'])
2020-09-29 22:55:50 +05:30
def test_write_read(self,default,tmp_path,mode):
default.save(tmp_path/'default.txt')
if mode == 'path':
2020-09-29 22:55:50 +05:30
new = Table.load(tmp_path/'default.txt')
elif mode == 'str':
2020-09-29 22:55:50 +05:30
new = Table.load(str(tmp_path/'default.txt'))
2020-01-26 14:31:00 +05:30
assert all(default.data==new.data) and default.shapes == new.shapes
2020-09-29 22:55:50 +05:30
def test_write_read_file(self,default,tmp_path):
with open(tmp_path/'default.txt','w') as f:
default.save(f)
2020-09-29 22:55:50 +05:30
with open(tmp_path/'default.txt') as f:
new = Table.load(f)
2020-01-26 14:31:00 +05:30
assert all(default.data==new.data) and default.shapes == new.shapes
2019-12-05 10:27:47 +05:30
2020-09-29 22:55:50 +05:30
def test_write_invalid_format(self,default,tmp_path):
with pytest.raises(TypeError):
2020-09-29 22:55:50 +05:30
default.save(tmp_path/'shouldnotbethere.txt',format='invalid')
@pytest.mark.parametrize('mode',['str','path'])
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_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']
2019-12-05 10:27:47 +05:30
@pytest.mark.parametrize('fname',['datatype-mix.txt','whitespace-mix.txt'])
def test_read_strange(self,ref_path,fname):
with open(ref_path/fname) as f:
Table.load(f)
2020-05-16 20:53:05 +05:30
2019-12-11 00:35:24 +05:30
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')
2020-09-14 10:34:01 +05:30
u = t.rename('s','u').get('u')
2019-12-11 00:35:24 +05:30
assert np.all(s == u)
2019-12-05 11:20:06 +05:30
def test_rename_gone(self,default):
2020-09-14 10:34:01 +05:30
gone = default.rename('v','V')
assert 'v' not in gone.shapes and 'v' not in gone.data.columns
2019-12-05 11:20:06 +05:30
with pytest.raises(KeyError):
2020-09-14 10:34:01 +05:30
gone.get('v')
2019-12-05 11:20:06 +05:30
def test_delete(self,default):
2020-09-14 10:34:01 +05:30
delete = default.delete('v')
assert 'v' not in delete.shapes and 'v' not in delete.data.columns
2019-12-05 11:20:06 +05:30
with pytest.raises(KeyError):
2020-09-14 10:34:01 +05:30
delete.get('v')
2019-12-05 11:20:06 +05:30
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'])
2020-09-14 10:34:01 +05:30
c = a.join(b)
assert np.array_equal(c.get('u'), b.get('u'))
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'])
2020-09-14 10:34:01 +05:30
b = a.append(a)
assert np.array_equal(b.data[:5].to_numpy(),b.data[5:].to_numpy())
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 test_invalid_initialization(self):
x = np.random.random((5,10))
with pytest.raises(ValueError):
Table(x,{'F':(3,3)})
def test_invalid_set(self,default):
2019-12-05 10:40:27 +05:30
x = default.get('v')
with pytest.raises(ValueError):
2019-12-05 10:40:27 +05:30
default.set('F',x,'does not work')
2019-12-04 14:50:03 +05:30
2019-12-05 10:40:27 +05:30
def test_invalid_get(self,default):
2019-12-04 14:50:03 +05:30
with pytest.raises(KeyError):
2019-12-05 10:40:27 +05:30
default.get('n')
2019-12-05 15:17:36 +05:30
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')
2020-09-14 10:34:01 +05:30
sort = t.sort_by('s').get('s')
2019-12-05 15:17:36 +05:30
assert np.all(np.sort(unsort,0)==sort)
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('F')[:,1,0]
sort = t.sort_by('F[1,0]').get('F')[:,1,0]
2019-12-05 15:17:36 +05:30
assert np.all(np.sort(unsort,0)==sort)
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])
2019-12-05 15:17:36 +05:30
def test_sort(self):
t = Table(np.array([[0,1,],[2,1,]]),
{'v':(2,)},
2020-09-14 10:34:01 +05:30
['test data'])\
.add('s',np.array(['b','a']))\
.sort_by('s')
assert np.all(t.get('v')[:,0] == np.array([2,0]))