condensing code
This commit is contained in:
parent
90fdb24faa
commit
3caf2c1296
|
@ -21,27 +21,27 @@ class Table():
|
||||||
Additional, human-readable information.
|
Additional, human-readable information.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
self.comments = [] if comments is None else [c for c in comments]
|
||||||
self.data = pd.DataFrame(data=data)
|
self.data = pd.DataFrame(data=data)
|
||||||
|
|
||||||
labels = {}
|
|
||||||
i = 0
|
|
||||||
for label in shapes.keys():
|
|
||||||
for components in range(np.prod(shapes[label])):
|
|
||||||
labels[i] = label
|
|
||||||
i+=1
|
|
||||||
|
|
||||||
if i != self.data.shape[1]:
|
|
||||||
raise IndexError('Shape mismatch between shapes and data')
|
|
||||||
|
|
||||||
self.data.rename(columns=labels,inplace=True)
|
|
||||||
|
|
||||||
if comments is None:
|
|
||||||
self.comments = []
|
|
||||||
else:
|
|
||||||
self.comments = [c for c in comments]
|
|
||||||
|
|
||||||
self.shapes = shapes
|
self.shapes = shapes
|
||||||
|
|
||||||
|
labels = []
|
||||||
|
for label,shape in self.shapes.items():
|
||||||
|
labels += [label] * np.prod(shape)
|
||||||
|
|
||||||
|
if len(labels) != self.data.shape[1]:
|
||||||
|
raise IndexError('Mismatch between shapes and data')
|
||||||
|
|
||||||
|
self.data.rename(columns=dict(zip(range(len(labels)),labels)),inplace=True)
|
||||||
|
|
||||||
|
|
||||||
|
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))
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_ASCII(fname):
|
def from_ASCII(fname):
|
||||||
"""
|
"""
|
||||||
|
@ -67,7 +67,7 @@ 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(1,header)]
|
||||||
labels = f.readline().split()
|
labels = f.readline().split()
|
||||||
|
|
||||||
shapes = {}
|
shapes = {}
|
||||||
|
@ -81,9 +81,9 @@ class Table():
|
||||||
if vector_column:
|
if vector_column:
|
||||||
shapes[label.split('_',1)[1]] = (int(label.split('_',1)[0]),)
|
shapes[label.split('_',1)[1]] = (int(label.split('_',1)[0]),)
|
||||||
else:
|
else:
|
||||||
shapes[label]=(1,)
|
shapes[label] = (1,)
|
||||||
|
|
||||||
data = pd.read_csv(f,names=[i for i in range(len(labels))],sep=r'\s+').to_numpy()
|
data = pd.read_csv(f,names=list(range(len(labels))),sep=r'\s+').to_numpy()
|
||||||
|
|
||||||
return Table(data,shapes,comments)
|
return Table(data,shapes,comments)
|
||||||
|
|
||||||
|
@ -107,7 +107,8 @@ class Table():
|
||||||
idx,key = label.split('_',1)
|
idx,key = label.split('_',1)
|
||||||
return self.data[key].to_numpy()[:,int(idx)-1].reshape((-1,1))
|
return self.data[key].to_numpy()[:,int(idx)-1].reshape((-1,1))
|
||||||
else:
|
else:
|
||||||
return self.data[label].to_numpy().reshape((-1,)+self.shapes[label])
|
return self.data[label].to_numpy().reshape((-1,)+self.shapes[label]) # better return shape (N) instead of (N,1), i.e. no reshaping?
|
||||||
|
|
||||||
|
|
||||||
def set(self,label,data,info=None):
|
def set(self,label,data,info=None):
|
||||||
"""
|
"""
|
||||||
|
@ -123,11 +124,7 @@ class Table():
|
||||||
Human-readable information about the new data.
|
Human-readable information about the new data.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if info is not None:
|
self.__add_comment(label,data.shape[1:],info)
|
||||||
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))
|
|
||||||
|
|
||||||
if re.match(r'[0-9]*?_',label):
|
if re.match(r'[0-9]*?_',label):
|
||||||
idx,key = label.split('_',1)
|
idx,key = label.split('_',1)
|
||||||
|
@ -136,6 +133,7 @@ class Table():
|
||||||
else:
|
else:
|
||||||
self.data[label] = data.reshape(self.data[label].shape)
|
self.data[label] = data.reshape(self.data[label].shape)
|
||||||
|
|
||||||
|
|
||||||
def add(self,label,data,info=None):
|
def add(self,label,data,info=None):
|
||||||
"""
|
"""
|
||||||
Add column data.
|
Add column data.
|
||||||
|
@ -150,17 +148,15 @@ class Table():
|
||||||
Human-readable information about the modified data.
|
Human-readable information about the modified data.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if info is not None:
|
self.__add_comment(label,data.shape[1:],info)
|
||||||
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))
|
|
||||||
|
|
||||||
self.shapes[label] = data.shape[1:] if len(data.shape) > 1 else (1,)
|
self.shapes[label] = data.shape[1:] if len(data.shape) > 1 else (1,)
|
||||||
size = np.prod(data.shape[1:],dtype=int)
|
size = np.prod(data.shape[1:],dtype=int)
|
||||||
new_data = pd.DataFrame(data=data.reshape(-1,size),
|
self.data = pd.concat([self.data,
|
||||||
columns=[label for l in range(size)])
|
pd.DataFrame(data=data.reshape(-1,size),
|
||||||
self.data = pd.concat([self.data,new_data],axis=1)
|
columns=[label]*size)],
|
||||||
|
axis=1)
|
||||||
|
|
||||||
|
|
||||||
def delete(self,label):
|
def delete(self,label):
|
||||||
"""
|
"""
|
||||||
|
@ -176,6 +172,7 @@ class Table():
|
||||||
|
|
||||||
del self.shapes[label]
|
del self.shapes[label]
|
||||||
|
|
||||||
|
|
||||||
def rename(self,label_old,label_new,info=None):
|
def rename(self,label_old,label_new,info=None):
|
||||||
"""
|
"""
|
||||||
Rename column data.
|
Rename column data.
|
||||||
|
@ -190,9 +187,10 @@ class Table():
|
||||||
"""
|
"""
|
||||||
self.data.rename(columns={label_old:label_new},inplace=True)
|
self.data.rename(columns={label_old:label_new},inplace=True)
|
||||||
|
|
||||||
comments = '{} => {}'.format(label_old,label_new)
|
self.comments.append('{} => {}{}'.format(label_old,
|
||||||
comments += ': {}'.format(info) if info is not None else ''
|
label_new,
|
||||||
self.comments.append(comments)
|
'' if info is None else ': {}'.format(info),
|
||||||
|
))
|
||||||
|
|
||||||
self.shapes[label_new] = self.shapes.pop(label_old)
|
self.shapes[label_new] = self.shapes.pop(label_old)
|
||||||
|
|
||||||
|
@ -223,6 +221,7 @@ class Table():
|
||||||
for t in _temp: self.delete(t)
|
for t in _temp: self.delete(t)
|
||||||
self.comments.append('sorted by [{}]'.format(', '.join(labels)))
|
self.comments.append('sorted by [{}]'.format(', '.join(labels)))
|
||||||
|
|
||||||
|
|
||||||
def to_ASCII(self,fname):
|
def to_ASCII(self,fname):
|
||||||
"""
|
"""
|
||||||
Store as plain text file.
|
Store as plain text file.
|
||||||
|
@ -238,14 +237,14 @@ class Table():
|
||||||
if(self.shapes[l] == (1,)):
|
if(self.shapes[l] == (1,)):
|
||||||
labels.append('{}'.format(l))
|
labels.append('{}'.format(l))
|
||||||
elif(len(self.shapes[l]) == 1):
|
elif(len(self.shapes[l]) == 1):
|
||||||
labels+=['{}_{}'.format(i+1,l)\
|
labels += ['{}_{}'.format(i+1,l) \
|
||||||
for i in range(self.shapes[l][0])]
|
for i in range(self.shapes[l][0])]
|
||||||
else:
|
else:
|
||||||
labels+=['{}:{}_{}'.format('x'.join([str(d) for d in self.shapes[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.shapes[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 \
|
||||||
+ [' '.join(labels)]
|
+ [' '.join(labels)]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue