2019-12-03 21:09:54 +05:30
|
|
|
import pytest
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
from damask import Table
|
|
|
|
|
2019-12-05 10:27:47 +05:30
|
|
|
|
2019-12-03 21:09:54 +05:30
|
|
|
@pytest.fixture
|
|
|
|
def default():
|
|
|
|
"""Simple Table."""
|
2019-12-05 22:30:59 +05:30
|
|
|
x = np.ones((5,13),dtype=float)
|
2022-03-12 03:01:35 +05:30
|
|
|
return Table({'F':(3,3),'v':(3,),'s':(1,)},
|
|
|
|
x,
|
|
|
|
['test data','contains five rows of only ones'])
|
2019-12-03 21:09:54 +05:30
|
|
|
|
2019-12-05 10:27:47 +05:30
|
|
|
@pytest.fixture
|
2023-04-24 10:41:14 +05:30
|
|
|
def res_path(res_path_base):
|
|
|
|
"""Directory containing testing resources."""
|
|
|
|
return res_path_base/'Table'
|
2019-12-03 21:09:54 +05:30
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-12-03 05:55:54 +05:30
|
|
|
@pytest.mark.parametrize('N',[10,40])
|
|
|
|
def test_len(self,N):
|
2022-03-12 03:01:35 +05:30
|
|
|
assert len(Table({'X':3},np.random.rand(N,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,)
|
2019-12-03 21:09:54 +05:30
|
|
|
|
|
|
|
def test_get_vector(self,default):
|
2019-12-05 10:40:27 +05:30
|
|
|
d = default.get('v')
|
2019-12-03 21:09:54 +05:30
|
|
|
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
|
|
|
|
2022-07-08 21:31:36 +05:30
|
|
|
def test_empty_init(self):
|
|
|
|
N = 3
|
|
|
|
D = dict(
|
|
|
|
scal=np.arange(10),
|
|
|
|
vctr=np.arange(10*N).reshape((10,N)),
|
|
|
|
)
|
|
|
|
t = Table()
|
|
|
|
for label,data in D.items():
|
|
|
|
t = t.set(label,data)
|
|
|
|
assert np.allclose(t.get('scal').flatten()*3,t.get('vctr')[:,0])
|
|
|
|
|
2021-04-01 00:00:07 +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
|
|
|
|
2021-04-01 00:00:07 +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))
|
2022-05-11 18:49:48 +05:30
|
|
|
assert np.allclose(d,default.set('nine',d,'random data').get('nine'))
|
2021-04-01 00:00:07 +05:30
|
|
|
|
|
|
|
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):
|
2022-02-14 08:30:48 +05:30
|
|
|
mask = np.random.choice([True,False],len(default))
|
2021-04-01 00:00:07 +05:30
|
|
|
assert len(default[:N]) == 1+N
|
|
|
|
assert len(default[:N,['F','s']]) == 1+N
|
2022-02-14 08:30:48 +05:30
|
|
|
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']]
|
2021-04-01 00:00:07 +05:30
|
|
|
assert default[N:].get('F').shape == (len(default)-N,3,3)
|
2022-02-14 08:30:48 +05:30
|
|
|
assert default[:N,['v','s']].data.equals(default['v','s'][:N].data)
|
2020-12-03 05:55:54 +05:30
|
|
|
|
2023-02-28 21:05:31 +05:30
|
|
|
@pytest.mark.parametrize('mode',['str','path','file'])
|
|
|
|
def test_write_read_mode(self,default,tmp_path,mode):
|
|
|
|
path = tmp_path/'default.txt'
|
|
|
|
if mode == 'file':
|
|
|
|
default.save(open(path,'w'))
|
|
|
|
new = Table.load(open(path))
|
|
|
|
else:
|
|
|
|
default.save(str(path) if mode == 'str' else path)
|
|
|
|
new = Table.load(str(path) if mode == 'str' else path)
|
2022-03-18 06:53:57 +05:30
|
|
|
assert all(default.data == new.data) and default.shapes == new.shapes
|
2019-12-03 21:09:54 +05:30
|
|
|
|
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:
|
2020-09-21 00:15:06 +05:30
|
|
|
default.save(f)
|
2020-09-29 22:55:50 +05:30
|
|
|
with open(tmp_path/'default.txt') as f:
|
2020-09-21 00:15:06 +05:30
|
|
|
new = Table.load(f)
|
2022-03-18 06:53:57 +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):
|
2020-09-03 20:11:22 +05:30
|
|
|
with pytest.raises(TypeError):
|
2020-09-29 22:55:50 +05:30
|
|
|
default.save(tmp_path/'shouldnotbethere.txt',format='invalid')
|
2020-07-31 20:34:14 +05:30
|
|
|
|
2023-02-28 21:05:31 +05:30
|
|
|
@pytest.mark.parametrize('mode',['str','path','file'])
|
2023-04-24 10:41:14 +05:30
|
|
|
def test_read_ang_mode(self,res_path,mode):
|
|
|
|
where = res_path/'simple.ang'
|
2023-02-28 21:05:31 +05:30
|
|
|
fname = {'path': where,
|
|
|
|
'str': str(where),
|
|
|
|
'file': open(where)}[mode]
|
|
|
|
new = Table.load_ang(fname)
|
2019-12-22 13:34:50 +05:30
|
|
|
assert new.data.shape == (4,10) and \
|
|
|
|
new.labels == ['eu', 'pos', 'IQ', 'CI', 'ID', 'intensity', 'fit']
|
|
|
|
|
2023-04-24 10:41:14 +05:30
|
|
|
def test_read_ang_shapes(self,res_path):
|
|
|
|
new = Table.load_ang(str(res_path/'simple.ang'),shapes={})
|
2023-02-28 21:05:31 +05:30
|
|
|
assert new.data.shape == (4,10) and new.labels == ['unknown']
|
2019-12-22 13:34:50 +05:30
|
|
|
|
2023-04-24 10:41:14 +05:30
|
|
|
def test_save_ang(self,res_path,tmp_path):
|
|
|
|
orig = Table.load_ang(res_path/'simple.ang')
|
2022-03-18 06:53:57 +05:30
|
|
|
orig.save(tmp_path/'simple.ang',with_labels=False)
|
|
|
|
saved = Table.load_ang(tmp_path/'simple.ang')
|
|
|
|
assert saved == orig
|
|
|
|
|
2019-12-05 10:27:47 +05:30
|
|
|
@pytest.mark.parametrize('fname',['datatype-mix.txt','whitespace-mix.txt'])
|
2023-04-24 10:41:14 +05:30
|
|
|
def test_read_strange(self,res_path,fname):
|
|
|
|
with open(res_path/fname) as f:
|
2020-09-21 00:15:06 +05:30
|
|
|
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))
|
2022-03-12 03:01:35 +05:30
|
|
|
t = Table({'F':(3,3),'v':(3,),'s':(1,)},x,['random test data'])
|
2019-12-11 00:35:24 +05:30
|
|
|
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
|
|
|
|
2019-12-22 22:41:01 +05:30
|
|
|
def test_join(self):
|
|
|
|
x = np.random.random((5,13))
|
2022-03-12 03:01:35 +05:30
|
|
|
a = Table({'F':(3,3),'v':(3,),'s':(1,)},x,['random test data'])
|
2019-12-22 22:41:01 +05:30
|
|
|
y = np.random.random((5,3))
|
2022-03-12 03:01:35 +05:30
|
|
|
b = Table({'u':(3,)},y,['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'))
|
2019-12-22 22:41:01 +05:30
|
|
|
|
|
|
|
def test_join_invalid(self):
|
|
|
|
x = np.random.random((5,13))
|
2022-03-12 03:01:35 +05:30
|
|
|
a = Table({'F':(3,3),'v':(3,),'s':(1,)},x,['random test data'])
|
2019-12-22 22:41:01 +05:30
|
|
|
with pytest.raises(KeyError):
|
|
|
|
a.join(a)
|
|
|
|
|
|
|
|
def test_append(self):
|
|
|
|
x = np.random.random((5,13))
|
2022-03-12 03:01:35 +05:30
|
|
|
a = Table({'F':(3,3),'v':(3,),'s':(1,)},x,['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())
|
2019-12-22 22:41:01 +05:30
|
|
|
|
|
|
|
def test_append_invalid(self):
|
|
|
|
x = np.random.random((5,13))
|
2022-03-12 03:01:35 +05:30
|
|
|
a = Table({'F':(3,3),'v':(3,),'s':(1,)},x,['random test data'])
|
|
|
|
b = Table({'F':(3,3),'u':(3,),'s':(1,)},x,['random test data'])
|
2019-12-22 22:41:01 +05:30
|
|
|
with pytest.raises(KeyError):
|
|
|
|
a.append(b)
|
2019-12-03 21:33:03 +05:30
|
|
|
|
2019-12-05 22:30:59 +05:30
|
|
|
def test_invalid_initialization(self):
|
|
|
|
x = np.random.random((5,10))
|
|
|
|
with pytest.raises(ValueError):
|
2022-03-12 03:01:35 +05:30
|
|
|
Table({'F':(3,3)},x)
|
2019-12-03 21:33:03 +05:30
|
|
|
|
|
|
|
def test_invalid_set(self,default):
|
2019-12-05 10:40:27 +05:30
|
|
|
x = default.get('v')
|
2019-12-03 21:33:03 +05:30
|
|
|
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))
|
2022-03-12 03:01:35 +05:30
|
|
|
t = Table({'F':(3,3),'v':(3,),'s':(1,)},x,['random test data'])
|
2019-12-05 15:17:36 +05:30
|
|
|
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))
|
2022-03-12 03:01:35 +05:30
|
|
|
t = Table({'F':(3,3),'v':(3,)},x,['random test data'])
|
2021-04-01 00:00:07 +05:30
|
|
|
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))
|
2022-03-12 03:01:35 +05:30
|
|
|
t = Table({'F':(3,3),'v':(3,)},x,['random test data'])
|
2021-04-01 00:00:07 +05:30
|
|
|
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
|
|
|
|
2019-12-05 22:30:59 +05:30
|
|
|
def test_sort(self):
|
2022-03-12 03:01:35 +05:30
|
|
|
t = Table({'v':(2,)},
|
|
|
|
np.array([[0,1,],[2,1,]]),
|
2020-09-14 10:34:01 +05:30
|
|
|
['test data'])\
|
2022-05-11 18:49:48 +05:30
|
|
|
.set('s',np.array(['b','a']))\
|
2020-09-15 10:28:06 +05:30
|
|
|
.sort_by('s')
|
2021-04-01 00:00:07 +05:30
|
|
|
assert np.all(t.get('v')[:,0] == np.array([2,0]))
|