DAMASK_EICMD/python/damask/table.py

259 lines
7.5 KiB
Python
Raw Normal View History

2019-10-31 15:15:34 +05:30
import re
import pandas as pd
import numpy as np
class Table():
"""Store spreadsheet-like data."""
2019-12-05 09:30:26 +05:30
def __init__(self,data,shapes,comments=None):
"""
2019-12-05 09:30:26 +05:30
New spreadsheet.
Parameters
----------
2019-12-05 09:30:26 +05:30
data : numpy.ndarray
Data.
2019-12-05 09:30:26 +05:30
shapes : dict with str:tuple pairs
Shapes of the columns. Example 'F':(3,3) for a deformation gradient.
comments : iterable of str, optional
2019-12-05 09:30:26 +05:30
Additional, human-readable information.
"""
2019-12-05 19:35:50 +05:30
self.comments = [] if comments is None else [c for c in comments]
2019-12-05 09:30:26 +05:30
self.data = pd.DataFrame(data=data)
2019-12-05 19:35:50 +05:30
self.shapes = shapes
self.__label_condensed()
def __label_flat(self):
"""Label data individually, e.g. v v v ==> 1_v 2_v 3_v."""
labels = []
for label,shape in self.shapes.items():
size = np.prod(shape)
labels += ['{}{}'.format('' if size == 1 else '{}_'.format(i+1),label) for i in range(size)]
self.data.columns = labels
def __label_condensed(self):
"""Label data condensed, e.g. 1_v 2_v 3_v ==> v v v."""
2019-12-05 19:35:50 +05:30
labels = []
for label,shape in self.shapes.items():
labels += [label] * np.prod(shape)
self.data.columns = labels
2019-12-05 19:35:50 +05:30
def __add_comment(self,label,shape,info):
if info is not None:
self.comments.append('{}{}: {}'.format(label,
' '+str(shape) if np.prod(shape,dtype=int) > 1 else '',
info))
2019-12-05 19:35:50 +05:30
@staticmethod
def from_ASCII(fname):
"""
Create table from ASCII file.
The first line needs to indicate the number of subsequent header lines as 'n header'.
2019-12-05 09:30:26 +05:30
Vector data column labels are indicated by '1_v, 2_v, ..., n_v'.
Tensor data column labels are indicated by '3x3:1_T, 3x3:2_T, ..., 3x3:9_T'.
Parameters
----------
fname : file, str, or pathlib.Path
Filename or file for reading.
"""
try:
f = open(fname)
except TypeError:
f = fname
header,keyword = f.readline().split()
if keyword == 'header':
header = int(header)
else:
raise Exception
2019-12-05 19:35:50 +05:30
comments = [f.readline()[:-1] for i in range(1,header)]
labels = f.readline().split()
2019-12-05 09:30:26 +05:30
shapes = {}
for label in labels:
tensor_column = re.search(r'[0-9,x]*?:[0-9]*?_',label)
if tensor_column:
my_shape = tensor_column.group().split(':',1)[0].split('x')
2019-12-05 09:30:26 +05:30
shapes[label.split('_',1)[1]] = tuple([int(d) for d in my_shape])
2019-10-31 15:15:34 +05:30
else:
vector_column = re.match(r'[0-9]*?_',label)
if vector_column:
2019-12-05 09:30:26 +05:30
shapes[label.split('_',1)[1]] = (int(label.split('_',1)[0]),)
2019-10-31 15:15:34 +05:30
else:
2019-12-05 19:35:50 +05:30
shapes[label] = (1,)
2019-12-05 10:15:27 +05:30
2019-12-05 19:35:50 +05:30
data = pd.read_csv(f,names=list(range(len(labels))),sep=r'\s+').to_numpy()
2019-12-05 10:15:27 +05:30
return Table(data,shapes,comments)
@property
2019-12-05 10:40:27 +05:30
def labels(self):
"""Return the labels of all columns."""
return list(self.shapes.keys())
def get(self,label):
"""
2019-12-05 10:40:27 +05:30
Get column data.
Parameters
----------
label : str
2019-12-05 10:40:27 +05:30
Column label.
"""
if re.match(r'[0-9]*?_',label):
idx,key = label.split('_',1)
data = self.data[key].to_numpy()[:,int(idx)-1].reshape((-1,1))
else:
data = self.data[label].to_numpy().reshape((-1,)+self.shapes[label])
return data.astype(type(data.flatten()[0]))
2019-12-05 19:35:50 +05:30
2019-10-31 15:15:34 +05:30
2019-12-05 10:40:27 +05:30
def set(self,label,data,info=None):
"""
2019-12-05 10:40:27 +05:30
Set column data.
Parameters
----------
label : str
2019-12-05 10:40:27 +05:30
Column label.
data : np.ndarray
New data.
2019-12-05 10:40:27 +05:30
info : str, optional
Human-readable information about the new data.
"""
2019-12-05 19:35:50 +05:30
self.__add_comment(label,data.shape[1:],info)
if re.match(r'[0-9]*?_',label):
idx,key = label.split('_',1)
iloc = self.data.columns.get_loc(key).tolist().index(True) + int(idx) -1
2019-12-05 10:40:27 +05:30
self.data.iloc[:,iloc] = data
else:
2019-12-05 10:40:27 +05:30
self.data[label] = data.reshape(self.data[label].shape)
2019-10-31 15:15:34 +05:30
2019-12-05 19:35:50 +05:30
2019-12-05 10:40:27 +05:30
def add(self,label,data,info=None):
"""
2019-12-05 10:40:27 +05:30
Add column data.
Parameters
----------
label : str
2019-12-05 10:40:27 +05:30
Column label.
data : np.ndarray
Modified data.
info : str, optional
Human-readable information about the modified data.
"""
2019-12-05 19:35:50 +05:30
self.__add_comment(label,data.shape[1:],info)
2019-12-05 10:40:27 +05:30
self.shapes[label] = data.shape[1:] if len(data.shape) > 1 else (1,)
size = np.prod(data.shape[1:],dtype=int)
new = pd.DataFrame(data=data.reshape(-1,size),
columns=[label]*size,
)
new.index = self.data.index
self.data = pd.concat([self.data,new],axis=1)
2019-12-05 19:35:50 +05:30
2019-12-05 10:40:27 +05:30
2019-12-05 11:20:06 +05:30
def delete(self,label):
"""
Delete column data.
Parameters
----------
label : str
Column label.
"""
self.data.drop(columns=label,inplace=True)
del self.shapes[label]
2019-12-05 19:35:50 +05:30
2019-12-05 11:20:06 +05:30
def rename(self,label_old,label_new,info=None):
"""
Rename column data.
Parameters
----------
label_old : str
Old column label.
label_new : str
New column label.
"""
self.data.rename(columns={label_old:label_new},inplace=True)
2019-12-05 19:35:50 +05:30
self.comments.append('{} => {}{}'.format(label_old,
label_new,
'' if info is None else ': {}'.format(info),
))
2019-12-05 11:20:06 +05:30
self.shapes = {(label if label is not label_old else label_new):self.shapes[label] for label in self.shapes}
2019-12-05 11:20:06 +05:30
2019-12-05 10:40:27 +05:30
2019-12-05 15:17:36 +05:30
def sort_by(self,labels,ascending=True):
"""
Get column data.
Parameters
----------
label : str or list
2019-12-05 15:17:36 +05:30
Column labels.
ascending : bool or list, optional
2019-12-05 15:17:36 +05:30
Set sort order.
"""
self.__label_flat()
self.data.sort_values(labels,axis=0,inplace=True,ascending=ascending)
self.__label_condensed()
2019-12-05 15:17:36 +05:30
self.comments.append('sorted by [{}]'.format(', '.join(labels)))
2019-12-05 19:35:50 +05:30
def to_ASCII(self,fname):
"""
Store as plain text file.
Parameters
----------
fname : file, str, or pathlib.Path
Filename or file for reading.
"""
seen = set()
2019-10-31 15:15:34 +05:30
labels = []
for l in [x for x in self.data.columns if not (x in seen or seen.add(x))]:
2019-12-05 09:30:26 +05:30
if(self.shapes[l] == (1,)):
2019-10-31 15:15:34 +05:30
labels.append('{}'.format(l))
2019-12-05 09:30:26 +05:30
elif(len(self.shapes[l]) == 1):
2019-12-05 19:35:50 +05:30
labels += ['{}_{}'.format(i+1,l) \
for i in range(self.shapes[l][0])]
2019-10-31 15:15:34 +05:30
else:
2019-12-05 19:35:50 +05:30
labels += ['{}:{}_{}'.format('x'.join([str(d) for d in self.shapes[l]]),i+1,l) \
for i in range(np.prod(self.shapes[l],dtype=int))]
2019-10-31 15:15:34 +05:30
2019-12-05 19:35:50 +05:30
header = ['{} header'.format(len(self.comments)+1)] \
+ self.comments \
2019-10-31 15:15:34 +05:30
+ [' '.join(labels)]
try:
f = open(fname,'w')
except TypeError:
f = fname
for line in header: f.write(line+'\n')
self.data.to_csv(f,sep=' ',index=False,header=False)