introduced two more helper functions:

- labels_index
- data_asArray
This commit is contained in:
Philip Eisenlohr 2012-12-06 21:46:19 +00:00
parent 149c747688
commit dc746119e3
1 changed files with 43 additions and 2 deletions

View File

@ -4,7 +4,7 @@ class ASCIItable():
'''
There should be a doc string here :)
'''
import sys
import sys,numpy
__slots__ = ['__IO__',
'info',
'labels',
@ -86,17 +86,44 @@ class ASCIItable():
# ------------------------------------------------------------------
def head_write(self):
'''
write current header information (info + labels)
'''
return self.output_write (['%i\theader'%(len(self.info)+1),
self.info,
'\t'.join(self.labels)])
# ------------------------------------------------------------------
def labels_append(self,
what):
what):
'''
add item or list to existing set of labels
'''
if isinstance(what,list):
for item in what: self.labels_append(item)
else: self.labels += [str(what)]
# ------------------------------------------------------------------
def labels_index(self,
labels):
'''
tell index of column label(s)
'''
if isinstance(labels,list):
idx = []
for label in labels:
try:
idx.append(self.labels.index(label))
except ValueError:
idx.append(-1)
else:
try:
idx = self.labels.index(labels)
except ValueError:
idx = -1
return idx
# ------------------------------------------------------------------
def info_append(self,
what):
@ -169,3 +196,17 @@ class ASCIItable():
def data_asFloat(self):
return map(self._transliterateToFloat,self.data)
# ------------------------------------------------------------------
def data_asArray(self,
labels = []):
import numpy
'''
read whole data of all (given) labels as numpy array
'''
if labels == []: indices = range(self.__IO__['validReadSize']) # use all columns
else: indices = self.labels_index(labels) # use specified columns
self.data_rewind()
return numpy.loadtxt(self.__IO__['in'], usecols=indices)