polishing

This commit is contained in:
Martin Diehl 2020-09-18 15:03:51 +02:00
parent 3f169d6e30
commit 1849ff0330
3 changed files with 17 additions and 23 deletions

View File

@ -453,8 +453,6 @@ class Geom:
Parameters Parameters
---------- ----------
geom : Geom object
Geometry to write.
fname : str or file handle fname : str or file handle
Geometry file to write with extension '.geom'. Geometry file to write with extension '.geom'.
pack : bool, optional pack : bool, optional
@ -526,8 +524,6 @@ class Geom:
Parameters Parameters
---------- ----------
geom : Geom object
Geometry to write with extension '.vtr'.
fname : str, optional fname : str, optional
Filename to write. If no file is given, a string is returned. Filename to write. If no file is given, a string is returned.
Valid extension is .vtr, it will be appended if not given. Valid extension is .vtr, it will be appended if not given.

View File

@ -66,7 +66,7 @@ class Table:
@staticmethod @staticmethod
def load_ASCII(fname): def load_ASCII(fname):
""" """
Create table from ASCII file. Load ASCII table file.
In legacy style, the first line indicates the number of In legacy style, the first line indicates the number of
subsequent header lines as "N header", with the last header line being subsequent header lines as "N header", with the last header line being
@ -124,7 +124,7 @@ class Table:
@staticmethod @staticmethod
def load_ang(fname): def load_ang(fname):
""" """
Create table from TSL ang file. Load ang file.
A valid TSL ang file needs to contains the following columns: A valid TSL ang file needs to contains the following columns:
* Euler angles (Bunge notation) in radians, 3 floats, label 'eu'. * Euler angles (Bunge notation) in radians, 3 floats, label 'eu'.
@ -341,14 +341,12 @@ class Table:
return dup return dup
def save_ASCII(table,fname,legacy=False): def save_ASCII(self,fname,legacy=False):
""" """
Store as plain text file. Save as plain text file.
Parameters Parameters
---------- ----------
table : Table object
Table to write.
fname : file, str, or pathlib.Path fname : file, str, or pathlib.Path
Filename or file for writing. Filename or file for writing.
legacy : Boolean, optional legacy : Boolean, optional
@ -358,23 +356,23 @@ class Table:
""" """
seen = set() seen = set()
labels = [] labels = []
for l in [x for x in table.data.columns if not (x in seen or seen.add(x))]: for l in [x for x in self.data.columns if not (x in seen or seen.add(x))]:
if table.shapes[l] == (1,): if self.shapes[l] == (1,):
labels.append(f'{l}') labels.append(f'{l}')
elif len(table.shapes[l]) == 1: elif len(self.shapes[l]) == 1:
labels += [f'{i+1}_{l}' \ labels += [f'{i+1}_{l}' \
for i in range(table.shapes[l][0])] for i in range(self.shapes[l][0])]
else: else:
labels += [f'{util.srepr(table.shapes[l],"x")}:{i+1}_{l}' \ labels += [f'{util.srepr(self.shapes[l],"x")}:{i+1}_{l}' \
for i in range(np.prod(table.shapes[l]))] for i in range(np.prod(self.shapes[l]))]
header = ([f'{len(table.comments)+1} header'] + table.comments) if legacy else \ header = ([f'{len(self.comments)+1} header'] + self.comments) if legacy else \
[f'# {comment}' for comment in table.comments] [f'# {comment}' for comment in self.comments]
try: try:
f = open(fname,'w') fhandle = open(fname,'w')
except TypeError: except TypeError:
f = fname fhandle = fname
for line in header + [' '.join(labels)]: f.write(line+'\n') for line in header + [' '.join(labels)]: fhandle.write(line+'\n')
table.data.to_csv(f,sep=' ',na_rep='nan',index=False,header=False) self.data.to_csv(fhandle,sep=' ',na_rep='nan',index=False,header=False)

View File

@ -106,7 +106,7 @@ class TestOrientation:
coords = np.array([(1,i+1) for i,x in enumerate(eu)]) coords = np.array([(1,i+1) for i,x in enumerate(eu)])
table = Table(eu,{'Eulers':(3,)}) table = Table(eu,{'Eulers':(3,)})
table = table.add('pos',coords) table = table.add('pos',coords)
table.write(reference) table.save_ASCII(reference)
assert np.allclose(eu,Table.load_ASCII(reference).get('Eulers')) assert np.allclose(eu,Table.load_ASCII(reference).get('Eulers'))
@pytest.mark.parametrize('lattice',Lattice.lattices) @pytest.mark.parametrize('lattice',Lattice.lattices)