documented and tested handling of multi-dimensional data

more precise regex expressions. get_array can handle individual
components
This commit is contained in:
Martin Diehl 2019-11-27 09:58:58 +01:00
parent 9ad74745c1
commit 96714089b1
1 changed files with 23 additions and 11 deletions

View File

@ -40,6 +40,13 @@ class Table():
@staticmethod @staticmethod
def from_ASCII(fname): def from_ASCII(fname):
"""
Create table from ASCII file.
The first line needs to indicate the number of subsequent header lines as 'n header'.
Vector data labels are indicated by '1_x, 2_x, ..., n_x'.
Tensor data labels are indicated by '3x3:1_x, 3x3:2_x, ..., 3x3:9_x'.
"""
try: try:
f = open(fname) f = open(fname)
except TypeError: except TypeError:
@ -50,29 +57,34 @@ class Table():
header = int(header) header = int(header)
else: else:
raise Exception raise Exception
comments = [f.readline()[:-1] for i in range(header-1)] comments = [f.readline()[:-1] for i in range(header-1)]
labels_raw = f.readline().split() labels = f.readline().split()
labels = [l.split('_',1)[1] if '_' in l else l for l in labels_raw]
headings = {} headings = {}
for l in labels_raw: for label in labels:
tensor_column = re.search(':.*?_',l) tensor_column = re.search(r'[0-9,x]*?:[0-9]*?_',label)
if tensor_column: if tensor_column:
my_shape = tensor_column.group()[1:-1].split('x') my_shape = tensor_column.group().split(':',1)[0].split('x')
headings[l.split('_',1)[1]] = tuple([int(d) for d in my_shape]) headings[label.split('_',1)[1]] = tuple([int(d) for d in my_shape])
else: else:
vector_column = re.match('.*?_',l) vector_column = re.match(r'[0-9]*?_',label)
if vector_column: if vector_column:
headings[l.split('_',1)[1]] = (int(l.split('_',1)[0]),) headings[label.split('_',1)[1]] = (int(label.split('_',1)[0]),)
else: else:
headings[l]=(1,) headings[label]=(1,)
return Table(np.loadtxt(f),headings,comments) return Table(np.loadtxt(f),headings,comments)
def get_array(self,label): def get_array(self,label):
return self.data[label].to_numpy().reshape((-1,)+self.headings[label]) """Return data as array."""
if re.match(r'[0-9]*?_',label):
idx,key = label.split('_',1)
return self.data[key].to_numpy()[:,int(idx)-1]
else:
return self.data[label].to_numpy().reshape((-1,)+self.headings[label])
def get_labels(self): def get_labels(self):
"""Return the labels of all columns."""
return [label for label in self.headings] return [label for label in self.headings]
def add_array(self,label,array,info): def add_array(self,label,array,info):