mainly play in plots
This commit is contained in:
parent
1d48bd85c5
commit
a8937ededb
|
@ -1,7 +1,6 @@
|
|||
#import logger
|
||||
import logger
|
||||
from scipy import array,take,asarray,shape
|
||||
import project
|
||||
#from sets import Set as set
|
||||
from itertools import izip
|
||||
|
||||
|
||||
|
@ -38,7 +37,13 @@ class Dataset:
|
|||
self._ids_set = self._ids_set.union(set(ids))
|
||||
self._dim_num[dim_name] = axis
|
||||
self._dim_names.append(dim_name)
|
||||
for df,d in izip(def_list,self.dims):
|
||||
#if dim_name in project.c_p.dim_names:
|
||||
# if ids:
|
||||
# # check that identifers are same as before
|
||||
# raise NotImplementedError
|
||||
# else:
|
||||
|
||||
for df,d in izip(def_list,self.dims): #check that data and labels match
|
||||
df=df[1]
|
||||
if len(df)!=d and df:
|
||||
raise ValueError,"dim size and identifyer mismatch"
|
||||
|
@ -70,6 +75,10 @@ class Dataset:
|
|||
n_dim = self.dims[axis]
|
||||
return [str(axis) + '_' + str(i) for i in range(n_dim)]
|
||||
|
||||
def index_to_id(self,dim_name,index):
|
||||
dim_ids = self.ids[dim_name]
|
||||
return [id for id,ind in dim_ids.items() if ind in index]
|
||||
|
||||
class Selection:
|
||||
"""Handles selected identifiers along each dimension of a dataset"""
|
||||
def __init__(self):
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
import pygtk
|
||||
import gtk
|
||||
import matplotlib
|
||||
import project
|
||||
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
|
||||
from matplotlib.axes import Subplot
|
||||
from matplotlib.figure import Figure
|
||||
|
@ -12,6 +13,7 @@ from matplotlib.widgets import RectangleSelector
|
|||
|
||||
import scipy
|
||||
import logger
|
||||
import project
|
||||
|
||||
class MainView (gtk.Notebook):
|
||||
def __init__(self):
|
||||
|
@ -167,6 +169,8 @@ class Plot (gtk.Frame):
|
|||
gtk.Frame.__init__(self)
|
||||
self.mark_active(False)
|
||||
self.connect('button_press_event', self.on_button_press)
|
||||
self.sel_obj = None
|
||||
project.c_p.attach(self)
|
||||
|
||||
def on_button_press(self, *rest):
|
||||
logger.log('debug', 'button pressed in plot')
|
||||
|
@ -181,6 +185,9 @@ class Plot (gtk.Frame):
|
|||
else:
|
||||
self.set_shadow_type(gtk.SHADOW_OUT)
|
||||
|
||||
def update_selection(self,project):
|
||||
self.selection_object = project.get_selection()
|
||||
|
||||
class EmptyView (Plot):
|
||||
def __init__(self):
|
||||
Plot.__init__(self)
|
||||
|
@ -228,18 +235,20 @@ class ScatterPlot (Plot):
|
|||
Plot.__init__(self)
|
||||
fig = Figure(figsize=(5,4), dpi=72)
|
||||
self.ax = ax = fig.add_subplot(111)
|
||||
N = 200
|
||||
self.a = a = scipy.randn(N)
|
||||
self.b = b = scipy.randn(N)
|
||||
self.x_dataset = project.c_p.datasets[0]
|
||||
x = self.x_dataset._data
|
||||
self.a = a = x[:,0]
|
||||
self.b = b = x[:,1]
|
||||
ax.plot(a,b,'og')
|
||||
self.canvas = FigureCanvas(fig)
|
||||
self.add(self.canvas)
|
||||
rectprops = dict(facecolor='blue', edgecolor = 'black',
|
||||
alpha=0.2, fill=True)
|
||||
sel = RectangleSelector(ax, self.line_select_callback,
|
||||
drawtype='box',useblit=True)
|
||||
drawtype='box',useblit=True,rectprops=rectprops)
|
||||
|
||||
self.canvas.show()
|
||||
|
||||
|
||||
def line_select_callback(self,event1, event2):
|
||||
'event1 and event2 are the press and release events'
|
||||
x1, y1 = event1.xdata, event1.ydata
|
||||
|
@ -250,9 +259,15 @@ class ScatterPlot (Plot):
|
|||
ydata = self.b
|
||||
xdata = self.a
|
||||
index =scipy.nonzero((xdata<x2) & (xdata>x1) & (ydata<y1) & (ydata>y2))
|
||||
if len(index)==0:
|
||||
logger.log('debug','No points selected!')
|
||||
else:
|
||||
logger.log('debug','Selected:\n%s'%index)
|
||||
ids = self.x_dataset.index_to_id('samples',index)
|
||||
logger.log('debug','Selected identifiers:\n%s'%ids)
|
||||
xdata_new = scipy.take(xdata,index)
|
||||
ydata_new = scipy.take(ydata,index)
|
||||
#self.ax.plot(xdata,ydata,'og')
|
||||
# self.ax.plot(xdata,ydata,'og')
|
||||
self.ax.plot(xdata_new,ydata_new,'or')
|
||||
self.canvas.draw()
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import dataset
|
||||
import scipy
|
||||
|
||||
class Project:
|
||||
def __init__(self,name="Testing"):
|
||||
|
@ -27,9 +28,10 @@ class Project:
|
|||
if modifier != observer:
|
||||
observer.update(self)
|
||||
|
||||
def set_selection(self,dim_name,selection):
|
||||
def set_selection(self,sel_obj,dim_name,selection):
|
||||
"""Sets selection and notifies observers"""
|
||||
current_selection = set(selection)
|
||||
sel_obj.current_selection[dim_name] = current_selection
|
||||
self.current_selection[dim_name] = current_selection
|
||||
self.notify()
|
||||
|
||||
|
@ -52,3 +54,18 @@ class Project:
|
|||
|
||||
# FIXME: Singleton project should be removed.
|
||||
c_p = Project()
|
||||
|
||||
#add test data
|
||||
x = scipy.rand(2000,3)
|
||||
def_list = [ ['samples',[]], ['genes',['a','b','c']] ]
|
||||
test_data = dataset.Dataset(x,def_list)
|
||||
c_p.add_dataset(test_data)
|
||||
|
||||
y = scipy.rand(2,2)
|
||||
def_list = [ ['samples',['1','2']], ['yclasses',['C','N']] ]
|
||||
test_data = dataset.Dataset(y,def_list)
|
||||
c_p.add_dataset(test_data)
|
||||
|
||||
#add selection object
|
||||
c_p.sel_obj = dataset.Selection()
|
||||
|
||||
|
|
Reference in New Issue