column label given as integer always returns dimension=1

used to return full dimension if first column of a multidim object was referenced by number
This commit is contained in:
Philip Eisenlohr 2017-08-28 19:32:13 -04:00
parent adc7c9d5b1
commit 4f5e96d366
1 changed files with 22 additions and 41 deletions

View File

@ -364,48 +364,29 @@ class ASCIItable():
""" """
from collections import Iterable from collections import Iterable
if isinstance(labels, Iterable) and not isinstance(labels, str): # check whether list of labels is requested listOfLabels = isinstance(labels, Iterable) and not isinstance(labels, str) # check whether list of labels is requested
dim = [] if not listOfLabels: labels = [labels]
for label in labels:
if label is not None:
myDim = -1
try: # column given as number?
idx = int(label)-1
myDim = 1 # if found has at least dimension 1
if self.tags[idx].startswith('1_'): # column has multidim indicator?
while idx+myDim < len(self.tags) and self.tags[idx+myDim].startswith("%i_"%(myDim+1)):
myDim += 1 # add while found
except ValueError: # column has string label
label = label[1:-1] if label[0] == label[-1] and label[0] in ('"',"'") else label # remove outermost quotations
if label in self.tags: # can be directly found?
myDim = 1 # scalar by definition
elif '1_'+label in self.tags: # look for first entry of possible multidim object
idx = self.tags.index('1_'+label) # get starting column
myDim = 1 # (at least) one-dimensional
while idx+myDim < len(self.tags) and self.tags[idx+myDim].startswith("%i_"%(myDim+1)):
myDim += 1 # keep adding while going through object
dim.append(myDim) dim = []
else: for label in labels:
dim = -1 # assume invalid label if label is not None:
idx = -1 myDim = -1
try: # column given as number? try: # column given as number?
idx = int(labels)-1 idx = int(label)-1
dim = 1 # if found has at least dimension 1 myDim = 1 # if found treat as single column of dimension 1
if self.tags[idx].startswith('1_'): # column has multidim indicator? except ValueError: # column has string label
while idx+dim < len(self.tags) and self.tags[idx+dim].startswith("%i_"%(dim+1)): label = label[1:-1] if label[0] == label[-1] and label[0] in ('"',"'") else label # remove outermost quotations
dim += 1 # add as long as found if label in self.tags: # can be directly found?
except ValueError: # column has string label myDim = 1 # scalar by definition
labels = labels[1:-1] if labels[0] == labels[-1] and labels[0] in ('"',"'") else labels # remove outermost quotations elif '1_'+label in self.tags: # look for first entry of possible multidim object
if labels in self.tags: # can be directly found? idx = self.tags.index('1_'+label) # get starting column
dim = 1 # scalar by definition myDim = 1 # (at least) one-dimensional
elif '1_'+labels in self.tags: # look for first entry of possible multidim object while idx+myDim < len(self.tags) and self.tags[idx+myDim].startswith("%i_"%(myDim+1)):
idx = self.tags.index('1_'+labels) # get starting column myDim += 1 # keep adding while going through object
dim = 1 # is (at least) one-dimensional
while idx+dim < len(self.tags) and self.tags[idx+dim].startswith("%i_"%(dim+1)):
dim += 1 # keep adding while going through object
return np.array(dim) if isinstance(dim,Iterable) else dim dim.append(myDim)
return np.array(dim) if listOfLabels else dim[0]
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def label_indexrange(self, def label_indexrange(self,
@ -511,7 +492,7 @@ class ASCIItable():
(d if str(c) != str(labels[present[i]]) else (d if str(c) != str(labels[present[i]]) else
1))) 1)))
use = np.array(columns) if len(columns) > 0 else None use = np.array(columns) if len(columns) > 0 else None
self.tags = list(np.array(self.tags)[use]) # update labels with valid subset self.tags = list(np.array(self.tags)[use]) # update labels with valid subset
self.data = np.loadtxt(self.__IO__['in'],usecols=use,ndmin=2) self.data = np.loadtxt(self.__IO__['in'],usecols=use,ndmin=2)