label_index now transparently searches for “1_”+label if label itself not found.

added label_dimension method returning the dimension of the requested object, i.e. 1, 3, or 9 as typical outcomes.
This commit is contained in:
Philip Eisenlohr 2015-06-08 05:02:11 +00:00
parent d99e34d870
commit 48f31f13a6
1 changed files with 55 additions and 25 deletions

View File

@ -171,57 +171,87 @@ class ASCIItable():
return numpy array if asked for list of labels. return numpy array if asked for list of labels.
transparently deals with label positions implicitly given as numbers or their headings given as strings. transparently deals with label positions implicitly given as numbers or their headings given as strings.
''' '''
if isinstance(labels,list): # check whether list of labels is requested from collections import Iterable
if isinstance(labels, Iterable) and not isinstance(labels, str): # check whether list of labels is requested
idx = [] idx = []
for label in labels: for label in labels:
if label != None: if label != None:
try: try:
idx.append(int(label)) # column given as integer number? idx.append(int(label)) # column given as integer number?
except ValueError: except ValueError:
try: try:
idx.append(self.labels.index(label)) # locate string in label list idx.append(self.labels.index(label)) # locate string in label list
except ValueError: except ValueError:
idx.append(-1) # not found... try:
idx.append(self.labels.index('1_'+label)) # locate '1_'+string in label list
except ValueError:
idx.append(-1) # not found...
else: else:
try: try:
idx = int(labels) idx = int(labels)
except ValueError: except ValueError:
try: try:
idx = self.labels.index(labels) idx = self.labels.index(labels)
except(ValueError): except ValueError:
idx = None if labels == None else -1 try:
idx = self.labels.index('1_'+labels) # locate '1_'+string in label list
except ValueError:
idx = None if labels == None else -1
return np.array(idx) if isinstance(idx,list) else idx return np.array(idx) if isinstance(idx,list) else idx
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def labels_dimension(self, def label_dimension(self,
labels): labels):
''' '''
tell dimension (length) of column label(s). tell dimension (length) of column label(s).
return numpy array if asked for list of labels. return numpy array if asked for list of labels.
transparently deals with label positions implicitly given as numbers or their headings given as strings. transparently deals with label positions implicitly given as numbers or their headings given as strings.
''' '''
if isinstance(labels,list): # check whether list of labels is requested
from collections import Iterable
if isinstance(labels, Iterable) and not isinstance(labels, str): # check whether list of labels is requested
dim = [] dim = []
for label in labels: for label in labels:
if label != None: if label != None:
try: myDim = -1
idx.append(int(label)) # column given as integer number? try: # column given as number?
except ValueError: idx = int(label)
try: myDim = 1 # if found has at least dimension 1
idx.append(self.labels.index(label)) # locate string in label list if self.labels[idx][:2] == '1_': # column has multidim indicator?
except ValueError: while idx+myDim < len(self.labels) and self.labels[idx+myDim][:2] == "%i_"%(myDim+1):
idx.append(-1) # not found... myDim += 1 # add while found
else: except ValueError: # column has string label
try: if label in self.labels: # can be directly found?
idx = int(labels) myDim = 1 # scalar by definition
except ValueError: elif '1_'+label in self.labels: # look for first entry of possible multidim object
try: idx = self.labels.index('1_'+label) # get starting column
idx = self.labels.index(labels) myDim = 1 # (at least) one-dimensional
except(ValueError): while idx+myDim < len(self.labels) and self.labels[idx+myDim][:2] == "%i_"%(myDim+1):
idx = None if labels == None else -1 myDim += 1 # keep adding while going through object
return np.array(idx) if isinstance(idx,list) else idx dim.append(myDim)
else:
dim = -1 # assume invalid label
idx = -1
try: # column given as number?
idx = int(labels)
dim = 1 # if found has at least dimension 1
if self.labels[idx][:2] == '1_': # column has multidim indicator?
while idx+dim < len(self.labels) and self.labels[idx+dim][:2] == "%i_"%(dim+1):
dim += 1 # add as long as found
except ValueError: # column has string label
if labels in self.labels: # can be directly found?
dim = 1 # scalar by definition
elif '1_'+labels in self.labels: # look for first entry of possible multidim object
idx = self.labels.index('1_'+labels) # get starting column
dim = 1 # is (at least) one-dimensional
while idx+dim < len(self.labels) and self.labels[idx+dim][:2] == "%i_"%(dim+1):
dim += 1 # keep adding while going through object
return np.array(dim) if isinstance(dim,list) else dim
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def info_append(self, def info_append(self,