diff --git a/python/damask/asciitable.py b/python/damask/asciitable.py index d81c22530..bed9a3395 100644 --- a/python/damask/asciitable.py +++ b/python/damask/asciitable.py @@ -14,11 +14,9 @@ class ASCIItable(): # ------------------------------------------------------------------ def __init__(self, - name = None, - outname = None, - buffered = False, # is ignored, only exists for compatibility reasons - labeled = True, # assume table has labels - readonly = False, # no reading from file + name, + labeled = True, # assume table has labels + readonly = False, # no reading from file ): """Read and write to ASCII tables.""" self.__IO__ = {'output': [], @@ -27,8 +25,9 @@ class ASCIItable(): 'dataStart': 0, } - self.__IO__['inPlace'] = not outname and name and not readonly - if self.__IO__['inPlace']: outname = name + self.tmpext # transparently create tmp file + self.__IO__['inPlace'] = name and not readonly + outname = name + self.tmpext if self.__IO__['inPlace'] else None # transparently create tmp file + try: self.__IO__['in'] = (open( name,'r') if os.access( name, os.R_OK) else None) if name else sys.stdin except TypeError: @@ -424,29 +423,26 @@ class ASCIItable(): return labels_missing # ------------------------------------------------------------------ - def data_write(self, - delimiter = '\t'): + def data_write(self): """Write current data array and report alive output back.""" if len(self.data) == 0: return True if isinstance(self.data[0],list): - return self.output_write([delimiter.join(map(self._quote,items)) for items in self.data]) + return self.output_write(['\t'.join(map(self._quote,items)) for items in self.data]) else: - return self.output_write( delimiter.join(map(self._quote,self.data))) + return self.output_write( '\t'.join(map(self._quote,self.data))) # ------------------------------------------------------------------ - def data_writeArray(self, - fmt = None, - delimiter = '\t'): + def data_writeArray(self): """Write whole numpy array data.""" for row in self.data: try: - output = [fmt % value for value in row] if fmt else list(map(repr,row)) + output = list(map(repr,row)) except Exception: - output = [fmt % row] if fmt else [repr(row)] + output = [repr(row)] try: - self.__IO__['out'].write(delimiter.join(output) + '\n') + self.__IO__['out'].write('\t'.join(output) + '\n') except Exception: pass diff --git a/python/damask/test.py b/python/damask/test.py index b1729548d..e88da7fb0 100644 --- a/python/damask/test.py +++ b/python/damask/test.py @@ -282,12 +282,10 @@ class Test(): import numpy as np logging.info('\n '.join(['comparing',File1,File2])) - table1 = damask.ASCIItable(name=File1,readonly=True) - table1.head_read() - len1=len(table1.info)+2 - table2 = damask.ASCIItable(name=File2,readonly=True) - table2.head_read() - len2=len(table2.info)+2 + table = damask.Table.from_ASCII(File1) + len1=len(table.comments)+2 + table = damask.Table.from_ASCII(File2) + len2=len(table.comments)+2 refArray = np.nan_to_num(np.genfromtxt(File1,missing_values='n/a',skip_header = len1,autostrip=True)) curArray = np.nan_to_num(np.genfromtxt(File2,missing_values='n/a',skip_header = len2,autostrip=True)) @@ -445,15 +443,15 @@ class Test(): if not (isinstance(files, Iterable) and not isinstance(files, str)): # check whether list of files is requested files = [str(files)] - tables = [damask.ASCIItable(name = filename,readonly = True) for filename in files] + tables = [damask.Table.from_ASCII(filename) for filename in files] for table in tables: - table.head_read() + table._label_flat() columns += [columns[0]]*(len(files)-len(columns)) # extend to same length as files columns = columns[:len(files)] # truncate to same length as files for i,column in enumerate(columns): - if column is None: columns[i] = tables[i].labels(raw = True) # if no column is given, read all + if column is None: columns[i] = list(tables[i].data.columns) # if no column is given, read all logging.info('comparing ASCIItables statistically') for i in range(len(columns)): @@ -467,9 +465,8 @@ class Test(): data = [] for table,labels in zip(tables,columns): - table.data_readArray(labels) - data.append(table.data) - table.close() + table._label_condensed() + data.append(np.hstack(list(table.get(label) for label in labels))) for i in range(1,len(data)):