added possibility to

* read ASCIItable not having labels
* write numpy array as data
This commit is contained in:
Philip Eisenlohr 2013-06-30 00:21:51 +00:00
parent f408ea3381
commit 9abc9a99fc
1 changed files with 55 additions and 17 deletions

View File

@ -15,11 +15,13 @@ class ASCIItable():
def __init__(self, def __init__(self,
fileIn = sys.stdin, fileIn = sys.stdin,
fileOut = sys.stdout, fileOut = sys.stdout,
buffered = True): buffered = True,
labels = True):
self.__IO__ = {'in': fileIn, self.__IO__ = {'in': fileIn,
'out':fileOut, 'out':fileOut,
'output':[], 'output':[],
'buffered':buffered, 'buffered':buffered,
'labels':labels,
'validReadSize': 0, 'validReadSize': 0,
'dataStart': 0, 'dataStart': 0,
} }
@ -72,26 +74,43 @@ class ASCIItable():
pass pass
firstline = self.__IO__['in'].readline() firstline = self.__IO__['in'].readline()
m = re.search('(\d+)\s*head', firstline.lower()) m = re.search('(\d+)\s*head', firstline.lower())
if m: if self.__IO__['labels']: # table features labels
self.info = [self.__IO__['in'].readline().strip() for i in xrange(1,int(m.group(1)))] if m: # found header info
self.labels = self.__IO__['in'].readline().split() self.info = [self.__IO__['in'].readline().strip() for i in xrange(1,int(m.group(1)))]
else: self.labels = self.__IO__['in'].readline().split()
self.info = [] else: # no header info (but labels)
self.labels = firstline.split() self.labels = firstline.split()
self.__IO__['validReadSize'] = len(self.labels)
self.__IO__['validReadSize'] = len(self.labels)
else: # no labels present in table
if m: # found header info
self.info = [self.__IO__['in'].readline().strip() for i in xrange(0,int(m.group(1)))] # all header is info
# ... without any labels
try: try:
self.__IO__['dataStart'] = self.__IO__['in'].tell() self.__IO__['dataStart'] = self.__IO__['in'].tell() # current file position is at start of data
except IOError: except IOError:
pass pass
if self.__IO__['validReadSize'] == 0: # in case no valid data length is known
self.__IO__['validReadSize'] = len(self.__IO__['in'].readline().split()) # assume constant data width from first line
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def head_write(self): def head_write(self):
''' '''
write current header information (info + labels) write current header information (info + labels)
''' '''
return self.output_write (['%i\theader'%(len(self.info)+1), if self.__IO__['labels']:
self.info, return self.output_write ([
'\t'.join(self.labels)]) '%i\theader'%(len(self.info)+1),
self.info,
'\t'.join(self.labels),
])
else:
return self.output_write ([
'%i\theader'%(len(self.info)),
self.info,
])
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def labels_append(self, def labels_append(self,
@ -103,6 +122,10 @@ class ASCIItable():
for item in what: self.labels_append(item) for item in what: self.labels_append(item)
else: self.labels += [str(what)] else: self.labels += [str(what)]
# ------------------------------------------------------------------
def labels_clear(self):
self.labels = []
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def labels_index(self, def labels_index(self,
labels): labels):
@ -127,6 +150,9 @@ class ASCIItable():
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def info_append(self, def info_append(self,
what): what):
'''
add item or list to existing set of infos
'''
if isinstance(what,list): if isinstance(what,list):
for item in what: self.info_append(item) for item in what: self.info_append(item)
else: self.info += [str(what)] else: self.info += [str(what)]
@ -146,10 +172,14 @@ class ASCIItable():
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def data_read(self): def data_read(self):
line = self.__IO__['in'].readline() line = self.__IO__['in'].readline() # get next data row
items = line.split()[:self.__IO__['validReadSize']] # get next data row if self.__IO__['labels']:
self.data = {False: [], items = line.split()[:self.__IO__['validReadSize']] # use up to valid size (label count)
True: items}[len(items) == self.__IO__['validReadSize']] # take if correct number of entries self.data = {False: [],
True: items}[len(items) == self.__IO__['validReadSize']] # take if correct number of entries
else:
self.data = line.split() # take all
return self.data != [] return self.data != []
# ------------------------------------------------------------------ # ------------------------------------------------------------------
@ -167,6 +197,14 @@ class ASCIItable():
else: else:
return self.output_write ('\t'.join(map(str,self.data))) return self.output_write ('\t'.join(map(str,self.data)))
# ------------------------------------------------------------------
def data_writeArray(self,format):
import numpy
'''
write whole numpy array data
'''
return numpy.savetxt(self.__IO__['out'], self.data, fmt=format)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def data_append(self, def data_append(self,
what): what):
@ -204,7 +242,7 @@ class ASCIItable():
read whole data of all (given) labels as numpy array read whole data of all (given) labels as numpy array
''' '''
if labels == []: indices = range(self.__IO__['validReadSize']) # use all columns if labels == []: indices = range(self.__IO__['validReadSize']) # use all columns
else: indices = self.labels_index(labels) # use specified columns else: indices = self.labels_index(labels) # use specified columns
self.data_rewind() self.data_rewind()