This commit is contained in:
parent
adc3bf6bb1
commit
2f2d54e5c8
|
@ -6,8 +6,10 @@ from itertools import izip
|
||||||
|
|
||||||
|
|
||||||
class Dataset:
|
class Dataset:
|
||||||
"""
|
"""Dataset base class.
|
||||||
Dataset base class
|
|
||||||
|
A Dataset is an n-way array with defined string identifiers across
|
||||||
|
all dimensions.
|
||||||
"""
|
"""
|
||||||
def __init__(self,input_array,def_list,parents=None):
|
def __init__(self,input_array,def_list,parents=None):
|
||||||
self._data = asarray(input_array)
|
self._data = asarray(input_array)
|
||||||
|
@ -18,6 +20,7 @@ class Dataset:
|
||||||
self.ids={}
|
self.ids={}
|
||||||
self.children=[]
|
self.children=[]
|
||||||
self._dim_num = {}
|
self._dim_num = {}
|
||||||
|
self._dim_names = []
|
||||||
if parents!=None:
|
if parents!=None:
|
||||||
for parent in self.parents:
|
for parent in self.parents:
|
||||||
parent.children.append(self)
|
parent.children.append(self)
|
||||||
|
@ -34,13 +37,23 @@ class Dataset:
|
||||||
self.ids[dim_name] = enum_ids
|
self.ids[dim_name] = enum_ids
|
||||||
self._ids_set = self._ids_set.union(set(ids))
|
self._ids_set = self._ids_set.union(set(ids))
|
||||||
self._dim_num[dim_name] = axis
|
self._dim_num[dim_name] = axis
|
||||||
|
self._dim_names.append(dim_name)
|
||||||
for df,d in izip(def_list,self.dims):
|
for df,d in izip(def_list,self.dims):
|
||||||
df=df[1]
|
df=df[1]
|
||||||
if len(df)!=d and df:
|
if len(df)!=d and df:
|
||||||
raise ValueError,"dim size and identifyer mismatch"
|
raise ValueError,"dim size and identifyer mismatch"
|
||||||
|
|
||||||
|
def names(self,axis=0):
|
||||||
|
"""Returns identifier names of a dimension. NB: not in any order! """
|
||||||
|
|
||||||
|
if type(axis)==int:
|
||||||
|
dim_name = self._dim_names[axis]
|
||||||
|
elif type(axis)==str:
|
||||||
|
dim_name = axis
|
||||||
|
return self.ids[dim_name].keys()
|
||||||
|
|
||||||
def extract_data(self,ids,dim_name):
|
def extract_data(self,ids,dim_name):
|
||||||
|
"""Extracts data along a dimension by identifiers"""
|
||||||
new_def_list = self.def_list[:]
|
new_def_list = self.def_list[:]
|
||||||
ids_index = [self.ids[dim_name][id_name] for id_name in ids]
|
ids_index = [self.ids[dim_name][id_name] for id_name in ids]
|
||||||
dim_number = self._dim_num[dim_name]
|
dim_number = self._dim_num[dim_name]
|
||||||
|
@ -49,13 +62,15 @@ class Dataset:
|
||||||
except:
|
except:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
new_def_list[dim_number][1] = ids
|
new_def_list[dim_number][1] = ids
|
||||||
D = Dataset(out_data,def_list=new_def_list,parents=self.parents)
|
extracted_data = Dataset(out_data,def_list=new_def_list,parents=self.parents)
|
||||||
return D
|
return extracted_data
|
||||||
|
|
||||||
def _create_identifiers(self,axis):
|
def _create_identifiers(self,axis):
|
||||||
|
"""Creates identifiers along an axis"""
|
||||||
n_dim = self.dims[axis]
|
n_dim = self.dims[axis]
|
||||||
return [str(axis) + '_' + str(i) for i in range(n_dim)]
|
return [str(axis) + '_' + str(i) for i in range(n_dim)]
|
||||||
|
|
||||||
class Selection:
|
class Selection:
|
||||||
|
"""Handles selected identifiers along each dimension of a dataset"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.current_selection={}
|
self.current_selection={}
|
||||||
|
|
Reference in New Issue