Projects/laydi
Projects
/
laydi
Archived
7
0
Fork 0

mainly play in plots

This commit is contained in:
Arnar Flatberg 2006-04-18 14:25:46 +00:00
parent 1d48bd85c5
commit a8937ededb
4 changed files with 64 additions and 21 deletions

2
fluent
View File

@ -130,3 +130,5 @@ if __name__ == '__main__':
app.show()
gtk.main()

View File

@ -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
@ -30,19 +29,25 @@ class Dataset:
enum_ids = {}
if dim_name not in project.c_p.dim_names:
dim_name = project.c_p.suggest_dim_name(dim_name)
if not ids:
ids = self._create_identifiers(axis)
if not ids:
ids = self._create_identifiers(axis)
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
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"
def names(self,axis=0):
"""Returns identifier names of a dimension. NB: not in any order! """
@ -69,7 +74,11 @@ class Dataset:
"""Creates identifiers along an axis"""
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):

View File

@ -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,7 +169,9 @@ 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')
self.mark_active(True)
@ -180,7 +184,10 @@ class Plot (gtk.Frame):
self.set_shadow_type(gtk.SHADOW_IN)
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,11 +259,17 @@ class ScatterPlot (Plot):
ydata = self.b
xdata = self.a
index =scipy.nonzero((xdata<x2) & (xdata>x1) & (ydata<y1) & (ydata>y2))
xdata_new = scipy.take(xdata,index)
ydata_new = scipy.take(ydata,index)
#self.ax.plot(xdata,ydata,'og')
self.ax.plot(xdata_new,ydata_new,'or')
self.canvas.draw()
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_new,ydata_new,'or')
self.canvas.draw()
def get_toolbar(self, window):
self.toolbar = NavigationToolbar(self.canvas, window)

View File

@ -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()
@ -50,5 +52,20 @@ class Project:
dim_name = dim_name + "_t"
return dim_name
# FIXME: Singleton project should be removed.
# 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()