more descriptive names
This commit is contained in:
parent
07e9778798
commit
1c83572589
|
@ -6,40 +6,40 @@ import numpy as np
|
||||||
class Table():
|
class Table():
|
||||||
"""Store spreadsheet-like data."""
|
"""Store spreadsheet-like data."""
|
||||||
|
|
||||||
def __init__(self,array,headings,comments=None):
|
def __init__(self,data,shapes,comments=None):
|
||||||
"""
|
"""
|
||||||
New spreadsheet data.
|
New spreadsheet.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
array : numpy.ndarray
|
data : numpy.ndarray
|
||||||
Data.
|
Data.
|
||||||
headings : dict
|
shapes : dict with str:tuple pairs
|
||||||
Column headings. Labels as keys and shape as tuple. Example 'F':(3,3) for a deformation gradient.
|
Shapes of the columns. Example 'F':(3,3) for a deformation gradient.
|
||||||
comments : iterable of str, optional
|
comments : iterable of str, optional
|
||||||
Additional, human-readable information
|
Additional, human-readable information.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.data = pd.DataFrame(data=array)
|
self.data = pd.DataFrame(data=data)
|
||||||
|
|
||||||
d = {}
|
labels = {}
|
||||||
i = 0
|
i = 0
|
||||||
for label in headings:
|
for label in shapes.keys():
|
||||||
for components in range(np.prod(headings[label])):
|
for components in range(np.prod(shapes[label])):
|
||||||
d[i] = label
|
labels[i] = label
|
||||||
i+=1
|
i+=1
|
||||||
|
|
||||||
if i != self.data.shape[1]:
|
if i != self.data.shape[1]:
|
||||||
raise IndexError('Mismatch between array shape and headings')
|
raise IndexError('Shape mismatch between shapes and data')
|
||||||
|
|
||||||
self.data.rename(columns=d,inplace=True)
|
self.data.rename(columns=labels,inplace=True)
|
||||||
|
|
||||||
if comments is None:
|
if comments is None:
|
||||||
self.comments = []
|
self.comments = []
|
||||||
else:
|
else:
|
||||||
self.comments = [c for c in comments]
|
self.comments = [c for c in comments]
|
||||||
|
|
||||||
self.headings = headings
|
self.shapes = shapes
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_ASCII(fname):
|
def from_ASCII(fname):
|
||||||
|
@ -47,8 +47,8 @@ class Table():
|
||||||
Create table from ASCII file.
|
Create table from ASCII file.
|
||||||
|
|
||||||
The first line needs to indicate the number of subsequent header lines as 'n header'.
|
The first line needs to indicate the number of subsequent header lines as 'n header'.
|
||||||
Vector data labels are indicated by '1_v, 2_v, ..., n_v'.
|
Vector data column labels are indicated by '1_v, 2_v, ..., n_v'.
|
||||||
Tensor data labels are indicated by '3x3:1_T, 3x3:2_T, ..., 3x3:9_T'.
|
Tensor data column labels are indicated by '3x3:1_T, 3x3:2_T, ..., 3x3:9_T'.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
|
@ -69,20 +69,20 @@ class Table():
|
||||||
comments = [f.readline()[:-1] for i in range(header-1)]
|
comments = [f.readline()[:-1] for i in range(header-1)]
|
||||||
labels = f.readline().split()
|
labels = f.readline().split()
|
||||||
|
|
||||||
headings = {}
|
shapes = {}
|
||||||
for label in labels:
|
for label in labels:
|
||||||
tensor_column = re.search(r'[0-9,x]*?:[0-9]*?_',label)
|
tensor_column = re.search(r'[0-9,x]*?:[0-9]*?_',label)
|
||||||
if tensor_column:
|
if tensor_column:
|
||||||
my_shape = tensor_column.group().split(':',1)[0].split('x')
|
my_shape = tensor_column.group().split(':',1)[0].split('x')
|
||||||
headings[label.split('_',1)[1]] = tuple([int(d) for d in my_shape])
|
shapes[label.split('_',1)[1]] = tuple([int(d) for d in my_shape])
|
||||||
else:
|
else:
|
||||||
vector_column = re.match(r'[0-9]*?_',label)
|
vector_column = re.match(r'[0-9]*?_',label)
|
||||||
if vector_column:
|
if vector_column:
|
||||||
headings[label.split('_',1)[1]] = (int(label.split('_',1)[0]),)
|
shapes[label.split('_',1)[1]] = (int(label.split('_',1)[0]),)
|
||||||
else:
|
else:
|
||||||
headings[label]=(1,)
|
shapes[label]=(1,)
|
||||||
|
|
||||||
return Table(np.loadtxt(f),headings,comments)
|
return Table(np.loadtxt(f),shapes,comments)
|
||||||
|
|
||||||
def get_array(self,label):
|
def get_array(self,label):
|
||||||
"""
|
"""
|
||||||
|
@ -98,7 +98,7 @@ class Table():
|
||||||
idx,key = label.split('_',1)
|
idx,key = label.split('_',1)
|
||||||
return self.data[key].to_numpy()[:,int(idx)-1]
|
return self.data[key].to_numpy()[:,int(idx)-1]
|
||||||
else:
|
else:
|
||||||
return self.data[label].to_numpy().reshape((-1,)+self.headings[label])
|
return self.data[label].to_numpy().reshape((-1,)+self.shapes[label])
|
||||||
|
|
||||||
def set_array(self,label,array,info):
|
def set_array(self,label,array,info):
|
||||||
"""
|
"""
|
||||||
|
@ -129,7 +129,7 @@ class Table():
|
||||||
|
|
||||||
def get_labels(self):
|
def get_labels(self):
|
||||||
"""Return the labels of all columns."""
|
"""Return the labels of all columns."""
|
||||||
return [label for label in self.headings]
|
return list(self.shapes.keys())
|
||||||
|
|
||||||
def add_array(self,label,array,info):
|
def add_array(self,label,array,info):
|
||||||
"""
|
"""
|
||||||
|
@ -150,7 +150,7 @@ class Table():
|
||||||
else:
|
else:
|
||||||
self.comments.append('{} {}: {}'.format(label,array.shape[1:],info))
|
self.comments.append('{} {}: {}'.format(label,array.shape[1:],info))
|
||||||
|
|
||||||
self.headings[label] = array.shape[1:] if len(array.shape) > 1 else (1,)
|
self.shapes[label] = array.shape[1:] if len(array.shape) > 1 else (1,)
|
||||||
size = np.prod(array.shape[1:],dtype=int)
|
size = np.prod(array.shape[1:],dtype=int)
|
||||||
new_data = pd.DataFrame(data=array.reshape(-1,size),
|
new_data = pd.DataFrame(data=array.reshape(-1,size),
|
||||||
columns=[label for l in range(size)])
|
columns=[label for l in range(size)])
|
||||||
|
@ -167,15 +167,15 @@ class Table():
|
||||||
|
|
||||||
"""
|
"""
|
||||||
labels = []
|
labels = []
|
||||||
for l in self.headings:
|
for l in self.shapes:
|
||||||
if(self.headings[l] == (1,)):
|
if(self.shapes[l] == (1,)):
|
||||||
labels.append('{}'.format(l))
|
labels.append('{}'.format(l))
|
||||||
elif(len(self.headings[l]) == 1):
|
elif(len(self.shapes[l]) == 1):
|
||||||
labels+=['{}_{}'.format(i+1,l)\
|
labels+=['{}_{}'.format(i+1,l)\
|
||||||
for i in range(self.headings[l][0])]
|
for i in range(self.shapes[l][0])]
|
||||||
else:
|
else:
|
||||||
labels+=['{}:{}_{}'.format('x'.join([str(d) for d in self.headings[l]]),i+1,l)\
|
labels+=['{}:{}_{}'.format('x'.join([str(d) for d in self.shapes[l]]),i+1,l)\
|
||||||
for i in range(np.prod(self.headings[l],dtype=int))]
|
for i in range(np.prod(self.shapes[l],dtype=int))]
|
||||||
|
|
||||||
header = ['{} header'.format(len(self.comments)+1)]\
|
header = ['{} header'.format(len(self.comments)+1)]\
|
||||||
+ self.comments\
|
+ self.comments\
|
||||||
|
|
Loading…
Reference in New Issue