2006-04-18 16:25:46 +02:00
|
|
|
import logger
|
2006-04-19 12:37:44 +02:00
|
|
|
from scipy import array,take,asarray,shape,nonzero
|
2006-04-17 00:57:50 +02:00
|
|
|
import project
|
|
|
|
from itertools import izip
|
|
|
|
|
|
|
|
|
|
|
|
class Dataset:
|
2006-04-17 11:08:40 +02:00
|
|
|
"""Dataset base class.
|
|
|
|
|
|
|
|
A Dataset is an n-way array with defined string identifiers across
|
|
|
|
all dimensions.
|
2006-04-17 00:57:50 +02:00
|
|
|
"""
|
2006-04-19 12:37:44 +02:00
|
|
|
def __init__(self,input_array,def_list):
|
2006-04-17 00:57:50 +02:00
|
|
|
self._data = asarray(input_array)
|
|
|
|
self.dims = shape(self._data)
|
|
|
|
self.def_list = def_list
|
|
|
|
self._ids_set = set()
|
|
|
|
self.ids={}
|
|
|
|
self._dim_num = {}
|
2006-04-17 11:08:40 +02:00
|
|
|
self._dim_names = []
|
2006-04-17 00:57:50 +02:00
|
|
|
if len(def_list)!=len(self.dims):
|
|
|
|
raise ValueError,"array dims and identifyer mismatch"
|
|
|
|
for axis,(dim_name,ids) in enumerate(def_list):
|
|
|
|
enum_ids = {}
|
2006-04-20 12:27:58 +02:00
|
|
|
#if dim_name not in project.c_p.dim_names:
|
|
|
|
# dim_name = project.c_p.suggest_dim_name(dim_name)
|
|
|
|
if not ids:
|
|
|
|
logger.log('debug','Creating identifiers along: '+dim_name)
|
|
|
|
ids = self._create_identifiers(axis)
|
2006-04-17 00:57:50 +02:00
|
|
|
for num,name in enumerate(ids):
|
|
|
|
enum_ids[name] = num
|
|
|
|
self.ids[dim_name] = enum_ids
|
|
|
|
self._ids_set = self._ids_set.union(set(ids))
|
|
|
|
self._dim_num[dim_name] = axis
|
2006-04-17 11:08:40 +02:00
|
|
|
self._dim_names.append(dim_name)
|
2006-04-18 16:25:46 +02:00
|
|
|
|
|
|
|
for df,d in izip(def_list,self.dims): #check that data and labels match
|
2006-04-17 00:57:50 +02:00
|
|
|
df=df[1]
|
|
|
|
if len(df)!=d and df:
|
|
|
|
raise ValueError,"dim size and identifyer mismatch"
|
2006-04-18 16:25:46 +02:00
|
|
|
|
2006-04-17 11:08:40 +02:00
|
|
|
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()
|
|
|
|
|
2006-04-17 00:57:50 +02:00
|
|
|
def extract_data(self,ids,dim_name):
|
2006-04-17 11:08:40 +02:00
|
|
|
"""Extracts data along a dimension by identifiers"""
|
2006-04-17 00:57:50 +02:00
|
|
|
new_def_list = self.def_list[:]
|
|
|
|
ids_index = [self.ids[dim_name][id_name] for id_name in ids]
|
|
|
|
dim_number = self._dim_num[dim_name]
|
|
|
|
try:
|
|
|
|
out_data = take(self._data,ids_index,axis=dim_number)
|
|
|
|
except:
|
|
|
|
raise ValueError
|
|
|
|
new_def_list[dim_number][1] = ids
|
2006-04-17 11:08:40 +02:00
|
|
|
extracted_data = Dataset(out_data,def_list=new_def_list,parents=self.parents)
|
|
|
|
return extracted_data
|
2006-04-17 00:57:50 +02:00
|
|
|
|
|
|
|
def _create_identifiers(self,axis):
|
2006-04-17 11:08:40 +02:00
|
|
|
"""Creates identifiers along an axis"""
|
2006-04-17 00:57:50 +02:00
|
|
|
n_dim = self.dims[axis]
|
|
|
|
return [str(axis) + '_' + str(i) for i in range(n_dim)]
|
2006-04-18 16:25:46 +02:00
|
|
|
|
2006-04-19 12:37:44 +02:00
|
|
|
def extract_id_from_index(self,dim_name,index):
|
|
|
|
"""Returns a set of ids from array/list of indexes."""
|
2006-04-18 16:25:46 +02:00
|
|
|
dim_ids = self.ids[dim_name]
|
2006-04-19 20:50:10 +02:00
|
|
|
if type(index)==int:
|
|
|
|
index = [index]
|
2006-04-20 12:27:58 +02:00
|
|
|
|
2006-04-19 12:37:44 +02:00
|
|
|
return set([id for id,ind in dim_ids.items() if ind in index])
|
|
|
|
|
|
|
|
def extract_index_from_id(self,dim_name,id):
|
|
|
|
"""Returns an array of indexes from a set/list of identifiers
|
|
|
|
(or a single id)"""
|
|
|
|
dim_ids = self.ids[dim_name]
|
|
|
|
return array([ind for name,ind in dim_ids.items() if name in id])
|
|
|
|
|
|
|
|
|
|
|
|
class CategoryDataset(Dataset):
|
|
|
|
def __init__(self,array,def_list):
|
|
|
|
Dataset.__init__(self,array,def_list)
|
|
|
|
|
|
|
|
def get_elements_by_category(self,dim,category):
|
|
|
|
"""Returns all elements along input dim belonging to category.
|
|
|
|
Assumes a two-dim category data only!
|
|
|
|
"""
|
|
|
|
if type(category)!=list:
|
|
|
|
raise ValueError, "category must be list"
|
|
|
|
gene_ids = []
|
|
|
|
axis_dim = self._dim_num[dim]
|
|
|
|
cat_index = self.extract_index_from_id(category)
|
|
|
|
for ind in cat_index:
|
|
|
|
if axis_dim==0:
|
|
|
|
gene_indx = nonzero(self._data[:,ind])
|
|
|
|
elif axis_dim==1:
|
|
|
|
gene_indx = nonzero(self._data[ind,:])
|
|
|
|
else:
|
|
|
|
ValueError, "Only support for 2-dim data"
|
|
|
|
gene_ids.append(self.extract_id_from_index(dim,gene_index))
|
|
|
|
return gene_ids
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-04-18 16:25:46 +02:00
|
|
|
|
2006-04-19 12:37:44 +02:00
|
|
|
|
2006-04-17 00:57:50 +02:00
|
|
|
class Selection:
|
2006-04-17 11:08:40 +02:00
|
|
|
"""Handles selected identifiers along each dimension of a dataset"""
|
2006-04-17 00:57:50 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.current_selection={}
|