2019-10-31 15:15:34 +05:30
|
|
|
import re
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
class Table():
|
2019-11-27 03:23:46 +05:30
|
|
|
"""Store spreadsheet-like data."""
|
|
|
|
|
2019-12-05 09:30:26 +05:30
|
|
|
def __init__(self,data,shapes,comments=None):
|
2019-11-27 03:23:46 +05:30
|
|
|
"""
|
2019-12-05 09:30:26 +05:30
|
|
|
New spreadsheet.
|
2019-11-27 03:23:46 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2019-12-05 09:30:26 +05:30
|
|
|
data : numpy.ndarray
|
2019-11-27 03:23:46 +05:30
|
|
|
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.
|
2019-11-27 03:23:46 +05:30
|
|
|
comments : iterable of str, optional
|
2019-12-05 09:30:26 +05:30
|
|
|
Additional, human-readable information.
|
2019-11-27 03:23:46 +05:30
|
|
|
|
|
|
|
"""
|
2019-12-05 09:30:26 +05:30
|
|
|
self.data = pd.DataFrame(data=data)
|
2019-11-27 03:23:46 +05:30
|
|
|
|
2019-12-05 09:30:26 +05:30
|
|
|
labels = {}
|
2019-11-27 03:23:46 +05:30
|
|
|
i = 0
|
2019-12-05 09:30:26 +05:30
|
|
|
for label in shapes.keys():
|
|
|
|
for components in range(np.prod(shapes[label])):
|
|
|
|
labels[i] = label
|
2019-11-27 03:23:46 +05:30
|
|
|
i+=1
|
|
|
|
|
2019-11-29 00:48:54 +05:30
|
|
|
if i != self.data.shape[1]:
|
2019-12-05 09:30:26 +05:30
|
|
|
raise IndexError('Shape mismatch between shapes and data')
|
2019-11-29 00:48:54 +05:30
|
|
|
|
2019-12-05 09:30:26 +05:30
|
|
|
self.data.rename(columns=labels,inplace=True)
|
2019-11-27 03:23:46 +05:30
|
|
|
|
|
|
|
if comments is None:
|
|
|
|
self.comments = []
|
|
|
|
else:
|
|
|
|
self.comments = [c for c in comments]
|
|
|
|
|
2019-12-05 09:30:26 +05:30
|
|
|
self.shapes = shapes
|
2019-11-27 03:23:46 +05:30
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_ASCII(fname):
|
2019-11-27 14:28:58 +05:30
|
|
|
"""
|
|
|
|
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'.
|
2019-11-28 10:22:23 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
fname : file, str, or pathlib.Path
|
|
|
|
Filename or file for reading.
|
|
|
|
|
2019-11-27 14:28:58 +05:30
|
|
|
"""
|
2019-11-26 18:56:25 +05:30
|
|
|
try:
|
|
|
|
f = open(fname)
|
|
|
|
except TypeError:
|
|
|
|
f = fname
|
|
|
|
|
|
|
|
header,keyword = f.readline().split()
|
|
|
|
if keyword == 'header':
|
|
|
|
header = int(header)
|
|
|
|
else:
|
|
|
|
raise Exception
|
2019-11-27 14:28:58 +05:30
|
|
|
comments = [f.readline()[:-1] for i in range(header-1)]
|
|
|
|
labels = f.readline().split()
|
2019-11-27 03:23:46 +05:30
|
|
|
|
2019-12-05 09:30:26 +05:30
|
|
|
shapes = {}
|
2019-11-27 14:28:58 +05:30
|
|
|
for label in labels:
|
|
|
|
tensor_column = re.search(r'[0-9,x]*?:[0-9]*?_',label)
|
2019-11-26 18:56:25 +05:30
|
|
|
if tensor_column:
|
2019-11-27 14:28:58 +05:30
|
|
|
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:
|
2019-11-27 14:28:58 +05:30
|
|
|
vector_column = re.match(r'[0-9]*?_',label)
|
2019-11-26 18:56:25 +05:30
|
|
|
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 09:30:26 +05:30
|
|
|
shapes[label]=(1,)
|
2019-11-26 18:56:25 +05:30
|
|
|
|
2019-12-05 10:15:27 +05:30
|
|
|
data = pd.read_csv(f,names=[i for i in range(len(labels))],sep='\s+').to_numpy()
|
|
|
|
|
|
|
|
return Table(data,shapes,comments)
|
|
|
|
|
2019-10-31 15:15:34 +05:30
|
|
|
|
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-04 09:38:52 +05:30
|
|
|
"""
|
2019-12-05 10:40:27 +05:30
|
|
|
Get column data.
|
2019-12-04 09:38:52 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
label : str
|
2019-12-05 10:40:27 +05:30
|
|
|
Column label.
|
2019-12-04 09:38:52 +05:30
|
|
|
|
|
|
|
"""
|
2019-11-27 14:28:58 +05:30
|
|
|
if re.match(r'[0-9]*?_',label):
|
|
|
|
idx,key = label.split('_',1)
|
|
|
|
return self.data[key].to_numpy()[:,int(idx)-1]
|
|
|
|
else:
|
2019-12-05 09:30:26 +05:30
|
|
|
return self.data[label].to_numpy().reshape((-1,)+self.shapes[label])
|
2019-10-31 15:15:34 +05:30
|
|
|
|
2019-12-05 10:40:27 +05:30
|
|
|
def set(self,label,data,info=None):
|
2019-11-28 10:22:23 +05:30
|
|
|
"""
|
2019-12-05 10:40:27 +05:30
|
|
|
Set column data.
|
2019-11-28 10:22:23 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
label : str
|
2019-12-05 10:40:27 +05:30
|
|
|
Column label.
|
|
|
|
data : np.ndarray
|
2019-12-04 09:38:52 +05:30
|
|
|
New data.
|
2019-12-05 10:40:27 +05:30
|
|
|
info : str, optional
|
2019-12-04 09:38:52 +05:30
|
|
|
Human-readable information about the new data.
|
2019-11-28 10:22:23 +05:30
|
|
|
|
|
|
|
"""
|
2019-12-05 10:40:27 +05:30
|
|
|
if info is not None:
|
|
|
|
if np.prod(data.shape[1:],dtype=int) == 1:
|
|
|
|
self.comments.append('{}: {}'.format(label,info))
|
|
|
|
else:
|
|
|
|
self.comments.append('{} {}: {}'.format(label,data.shape[1:],info))
|
2019-11-27 16:38:23 +05:30
|
|
|
|
2019-11-27 15:26:29 +05:30
|
|
|
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
|
2019-11-27 15:26:29 +05:30
|
|
|
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 10:40:27 +05:30
|
|
|
def add(self,label,data,info=None):
|
2019-11-28 10:22:23 +05:30
|
|
|
"""
|
2019-12-05 10:40:27 +05:30
|
|
|
Add column data.
|
2019-12-04 09:38:52 +05:30
|
|
|
|
2019-11-28 10:22:23 +05:30
|
|
|
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-11-28 10:22:23 +05:30
|
|
|
|
|
|
|
"""
|
2019-12-05 10:40:27 +05:30
|
|
|
if info is not None:
|
|
|
|
if np.prod(data.shape[1:],dtype=int) == 1:
|
|
|
|
self.comments.append('{}: {}'.format(label,info))
|
|
|
|
else:
|
|
|
|
self.comments.append('{} {}: {}'.format(label,data.shape[1:],info))
|
2019-10-31 15:15:34 +05:30
|
|
|
|
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_data = pd.DataFrame(data=data.reshape(-1,size),
|
2019-10-31 15:15:34 +05:30
|
|
|
columns=[label for l in range(size)])
|
|
|
|
self.data = pd.concat([self.data,new_data],axis=1)
|
2019-12-05 10:40:27 +05:30
|
|
|
|
|
|
|
|
2019-11-26 18:56:25 +05:30
|
|
|
def to_ASCII(self,fname):
|
2019-11-28 10:22:23 +05:30
|
|
|
"""
|
|
|
|
Store as plain text file.
|
2019-12-04 09:38:52 +05:30
|
|
|
|
2019-11-28 10:22:23 +05:30
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
fname : file, str, or pathlib.Path
|
|
|
|
Filename or file for reading.
|
|
|
|
|
|
|
|
"""
|
2019-10-31 15:15:34 +05:30
|
|
|
labels = []
|
2019-12-05 09:30:26 +05:30
|
|
|
for l in self.shapes:
|
|
|
|
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-10-31 15:15:34 +05:30
|
|
|
labels+=['{}_{}'.format(i+1,l)\
|
2019-12-05 09:30:26 +05:30
|
|
|
for i in range(self.shapes[l][0])]
|
2019-10-31 15:15:34 +05:30
|
|
|
else:
|
2019-12-05 09:30:26 +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
|
|
|
|
|
|
|
header = ['{} header'.format(len(self.comments)+1)]\
|
|
|
|
+ self.comments\
|
|
|
|
+ [' '.join(labels)]
|
|
|
|
|
2019-11-26 18:56:25 +05:30
|
|
|
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)
|