Merge branch 'table-save-ang' into 'development'

Table.save(with_labels=False)

See merge request damask/DAMASK!547
This commit is contained in:
Franz Roters 2022-03-18 12:55:04 +00:00
commit ecb33641da
2 changed files with 23 additions and 13 deletions

View File

@ -569,7 +569,8 @@ class Table:
def save(self,
fname: FileHandle):
fname: FileHandle,
with_labels: bool = True):
"""
Save as plain text file.
@ -577,9 +578,12 @@ class Table:
----------
fname : file, str, or pathlib.Path
Filename or file for writing.
with_labels : bool, optional
Write column labels. Defaults to True.
"""
labels = []
if with_labels:
for l in list(dict.fromkeys(self.data.columns)):
if self.shapes[l] == (1,):
labels.append(f'{l}')
@ -592,5 +596,5 @@ class Table:
f = open(Path(fname).expanduser(),'w',newline='\n') if isinstance(fname, (str, Path)) else fname
f.write('\n'.join([f'# {c}' for c in self.comments] + [' '.join(labels)])+'\n')
f.write('\n'.join([f'# {c}' for c in self.comments] + [' '.join(labels)])+('\n' if labels else ''))
self.data.to_csv(f,sep=' ',na_rep='nan',index=False,header=False)

View File

@ -105,6 +105,12 @@ class TestTable:
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):
orig = Table.load_ang(ref_path/'simple.ang')
orig.save(tmp_path/'simple.ang',with_labels=False)
saved = Table.load_ang(tmp_path/'simple.ang')
assert saved == orig
@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: