Added reversedict and updated get_dentifiers and get_indices
This commit is contained in:
parent
677d368b60
commit
bbd525bed0
|
@ -91,7 +91,7 @@ class Dataset:
|
|||
def _set_identifiers(self,identifiers,all_dims):
|
||||
"""Creates internal mapping of identifiers structure."""
|
||||
for dim,ids in identifiers:
|
||||
pos_map={}
|
||||
pos_map = ReverseDict()
|
||||
if dim not in self._dims:
|
||||
self._dims.append(dim)
|
||||
all_dims.add(dim)
|
||||
|
@ -139,10 +139,10 @@ class Dataset:
|
|||
else:
|
||||
return [dim for dim in self]
|
||||
|
||||
def get_identifiers(self, dim, indices=None,sorted=True):
|
||||
def get_identifiers(self, dim, indices=None,sorted=False):
|
||||
"""Returns identifiers along dim, sorted by position (index) is optional.
|
||||
|
||||
You can optionally provide a list of indices to get only the
|
||||
You can optionally provide a list/ndarray of indices to get only the
|
||||
identifiers of a given position.
|
||||
|
||||
Identifiers are the unique names (strings) for a variable in a given dim.
|
||||
|
@ -154,21 +154,16 @@ class Dataset:
|
|||
except:
|
||||
pass
|
||||
|
||||
if sorted==True:
|
||||
items = self._map[dim].items()
|
||||
backitems = [ [v[1],v[0]] for v in items]
|
||||
backitems.sort()
|
||||
ids = [ backitems[i][1] for i in range(0,len(backitems))]
|
||||
|
||||
else:
|
||||
ids = self._map[dim].keys()
|
||||
|
||||
if indices != None:
|
||||
ids = [ids[index] for index in indices]
|
||||
ids = [self._map[dim].reverse[i] for i in indices]
|
||||
else:
|
||||
if sorted==True:
|
||||
ids = [self._map[dim].reverse[i] for i in array_sort(self._map[dim].values())]
|
||||
else:
|
||||
ids = self._map[dim].keys()
|
||||
|
||||
return ids
|
||||
|
||||
|
||||
def get_indices(self, dim, idents=None):
|
||||
"""Returns indices for identifiers along dimension.
|
||||
|
||||
|
@ -183,6 +178,7 @@ class Dataset:
|
|||
index = [self._map[dim][key] for key in idents]
|
||||
return asarray(index)
|
||||
|
||||
|
||||
class CategoryDataset(Dataset):
|
||||
"""The category dataset class.
|
||||
|
||||
|
@ -204,7 +200,7 @@ class CategoryDataset(Dataset):
|
|||
.
|
||||
"""
|
||||
|
||||
def __init__(self,array,identifiers=None,name='A'):
|
||||
def __init__(self,array,identifiers=None,name='C'):
|
||||
Dataset.__init__(self,array,identifiers=identifiers,name=name)
|
||||
self.has_dictlists = False
|
||||
|
||||
|
@ -217,6 +213,7 @@ class CategoryDataset(Dataset):
|
|||
self.has_dictlists=True
|
||||
return data
|
||||
|
||||
|
||||
class GraphDataset(Dataset):
|
||||
"""The graph dataset class.
|
||||
|
||||
|
@ -273,3 +270,19 @@ class Selection:
|
|||
def __init__(self):
|
||||
self.current_selection={}
|
||||
|
||||
class ReverseDict(dict):
|
||||
"""
|
||||
A dictionary which can lookup values by key, and keys by value.
|
||||
All values and keys must be hashable, and unique.
|
||||
|
||||
d = ReverseDict((['a',1],['b',2]))
|
||||
print d['a']
|
||||
print d.reverse[1]
|
||||
"""
|
||||
def __init__(self, *args, **kw):
|
||||
dict.__init__(self, *args, **kw)
|
||||
self.reverse = dict([[v,k] for k,v in self.items()])
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
dict.__setitem__(self, key, value)
|
||||
self.reverse[value] = key
|
||||
|
|
Reference in New Issue