Merge branch '276-ang-file-inflexibility' into 'development'

Resolve "ANG file inflexibility"

Closes #276

See merge request damask/DAMASK!739
This commit is contained in:
Martin Diehl 2023-02-28 15:35:31 +00:00
commit ae8e1c1791
2 changed files with 33 additions and 23 deletions

View File

@ -299,11 +299,18 @@ class Table:
@staticmethod @staticmethod
def load_ang(fname: FileHandle) -> 'Table': def load_ang(fname: FileHandle,
shapes = {'eu':3,
'pos':2,
'IQ':1,
'CI':1,
'ID':1,
'intensity':1,
'fit':1}) -> 'Table':
""" """
Load from ang file. Load from ANG file.
A valid TSL ang file has to have the following columns: Regular ANG files feature the following columns:
- Euler angles (Bunge notation) in radians, 3 floats, label 'eu'. - Euler angles (Bunge notation) in radians, 3 floats, label 'eu'.
- Spatial position in meters, 2 floats, label 'pos'. - Spatial position in meters, 2 floats, label 'pos'.
@ -316,7 +323,10 @@ class Table:
Parameters Parameters
---------- ----------
fname : file, str, or pathlib.Path fname : file, str, or pathlib.Path
Filename or file for reading. Filename or file to read.
shapes : dict with str:int pairs, optional
Column labels and their width.
Defaults to standard TSL ANG format.
Returns Returns
------- -------
@ -338,7 +348,6 @@ class Table:
data = np.loadtxt(content) data = np.loadtxt(content)
shapes = {'eu':3, 'pos':2, 'IQ':1, 'CI':1, 'ID':1, 'intensity':1, 'fit':1}
if (remainder := data.shape[1]-sum(shapes.values())) > 0: if (remainder := data.shape[1]-sum(shapes.values())) > 0:
shapes['unknown'] = remainder shapes['unknown'] = remainder

View File

@ -81,13 +81,15 @@ class TestTable:
assert default[N:].get('F').shape == (len(default)-N,3,3) assert default[N:].get('F').shape == (len(default)-N,3,3)
assert default[:N,['v','s']].data.equals(default['v','s'][:N].data) assert default[:N,['v','s']].data.equals(default['v','s'][:N].data)
@pytest.mark.parametrize('mode',['str','path']) @pytest.mark.parametrize('mode',['str','path','file'])
def test_write_read(self,default,tmp_path,mode): def test_write_read_mode(self,default,tmp_path,mode):
default.save(tmp_path/'default.txt') path = tmp_path/'default.txt'
if mode == 'path': if mode == 'file':
new = Table.load(tmp_path/'default.txt') default.save(open(path,'w'))
elif mode == 'str': new = Table.load(open(path))
new = Table.load(str(tmp_path/'default.txt')) else:
default.save(str(path) if mode == 'str' else path)
new = Table.load(str(path) if mode == 'str' else path)
assert all(default.data == new.data) and default.shapes == new.shapes assert all(default.data == new.data) and default.shapes == new.shapes
def test_write_read_file(self,default,tmp_path): def test_write_read_file(self,default,tmp_path):
@ -101,20 +103,19 @@ class TestTable:
with pytest.raises(TypeError): with pytest.raises(TypeError):
default.save(tmp_path/'shouldnotbethere.txt',format='invalid') default.save(tmp_path/'shouldnotbethere.txt',format='invalid')
@pytest.mark.parametrize('mode',['str','path']) @pytest.mark.parametrize('mode',['str','path','file'])
def test_read_ang(self,ref_path,mode): def test_read_ang_mode(self,ref_path,mode):
if mode == 'path': where = ref_path/'simple.ang'
new = Table.load_ang(ref_path/'simple.ang') fname = {'path': where,
elif mode == 'str': 'str': str(where),
new = Table.load_ang(str(ref_path/'simple.ang')) 'file': open(where)}[mode]
new = Table.load_ang(fname)
assert new.data.shape == (4,10) and \ assert new.data.shape == (4,10) and \
new.labels == ['eu', 'pos', 'IQ', 'CI', 'ID', 'intensity', 'fit'] new.labels == ['eu', 'pos', 'IQ', 'CI', 'ID', 'intensity', 'fit']
def test_read_ang_file(self,ref_path): def test_read_ang_shapes(self,ref_path):
f = open(ref_path/'simple.ang') new = Table.load_ang(str(ref_path/'simple.ang'),shapes={})
new = Table.load_ang(f) assert new.data.shape == (4,10) and new.labels == ['unknown']
assert new.data.shape == (4,10) and \
new.labels == ['eu', 'pos', 'IQ', 'CI', 'ID', 'intensity', 'fit']
def test_save_ang(self,ref_path,tmp_path): def test_save_ang(self,ref_path,tmp_path):
orig = Table.load_ang(ref_path/'simple.ang') orig = Table.load_ang(ref_path/'simple.ang')