flexibility for non-standard ANG headers.

This commit is contained in:
Philip Eisenlohr 2023-02-28 15:35:31 +00:00 committed by Martin Diehl
parent 12f7d055f6
commit b15096c2f6
2 changed files with 33 additions and 23 deletions

View File

@ -299,11 +299,18 @@ class Table:
@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'.
- Spatial position in meters, 2 floats, label 'pos'.
@ -316,7 +323,10 @@ class Table:
Parameters
----------
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
-------
@ -338,7 +348,6 @@ class Table:
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:
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,['v','s']].data.equals(default['v','s'][:N].data)
@pytest.mark.parametrize('mode',['str','path'])
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'))
@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)
assert all(default.data == new.data) and default.shapes == new.shapes
def test_write_read_file(self,default,tmp_path):
@ -101,20 +103,19 @@ class TestTable:
with pytest.raises(TypeError):
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'))
@pytest.mark.parametrize('mode',['str','path','file'])
def test_read_ang_mode(self,ref_path,mode):
where = ref_path/'simple.ang'
fname = {'path': where,
'str': str(where),
'file': open(where)}[mode]
new = Table.load_ang(fname)
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']
def test_read_ang_shapes(self,ref_path):
new = Table.load_ang(str(ref_path/'simple.ang'),shapes={})
assert new.data.shape == (4,10) and new.labels == ['unknown']
def test_save_ang(self,ref_path,tmp_path):
orig = Table.load_ang(ref_path/'simple.ang')