Projects/laydi
Projects
/
laydi
Archived
7
0
Fork 0

Comments and cleanup in LineViewPlot

This commit is contained in:
Arnar Flatberg 2006-10-17 13:58:33 +00:00
parent ba55678472
commit bf951990f5
1 changed files with 90 additions and 57 deletions

View File

@ -398,96 +398,128 @@ class Plot (View):
class LineViewPlot(Plot): class LineViewPlot(Plot):
"""Line view of current selection, no interaction """Line view plot with percentiles.
Only works on 2d-arrays
A line view of vectors across a specified dimension of input dataset.
No selection interaction is defined.
Only support for 2d-arrays.
input: input:
-- major_axis : dim_number for line dim (see scipy.ndarray for axis def.) -- major_axis : dim_number for line dim (see scipy.ndarray for axis def.)
-- minor_axis : needs definition only for higher order arrays -- minor_axis : needs definition only for higher order arrays
ps: slow (cant get linecollection and blit to work)
fixme: slow (cant get linecollection and blit to work)
""" """
def __init__(self, dataset, major_axis=1, minor_axis=None, name="Line view"): def __init__(self, dataset, major_axis=1, minor_axis=None, name="Line view"):
self.use_blit = False
self._last_index = []
self._data = dataset.asarray()
self.dataset = dataset self.dataset = dataset
Plot.__init__(self, name) self._data = dataset.asarray()
self.ax = self.fig.add_subplot(111)
#self.ax.set_title(self.get_title())
self.current_dim = self.dataset.get_dim_name(major_axis)
if len(self._data.shape)==2 and not minor_axis: if len(self._data.shape)==2 and not minor_axis:
minor_axis = major_axis-1 minor_axis = major_axis-1
self.major_axis = major_axis
self.minor_axis = minor_axis
Plot.__init__(self, name)
self.use_blit = False #fixme: blitting does work
self.current_dim = self.dataset.get_dim_name(major_axis)
# make axes
self.ax = self.fig.add_subplot(111)
#initial draw #initial draw
x_axis = scipy.arange(self._data.shape[minor_axis]) x_axis = scipy.arange(self._data.shape[minor_axis])
self.line_segs=[] self.line_segs=[]
for xi in range(self._data.shape[major_axis]): for xi in range(self._data.shape[major_axis]):
yi = self._data.take([xi], major_axis) yi = self._data.take([xi], major_axis).ravel()
self.line_segs.append([(xx,yy) for xx,yy in izip(x_axis,yi)]) self.line_segs.append([(xx,yy) for xx,yy in izip(x_axis, yi)])
#background # draw background
xax = scipy.arange(self._data.shape[0]) self._set_background(self.ax)
# add canvas
self.add(self.canvas)
self.canvas.show()
# add toolbar
#FIXME: Lineview plot cannot do selections -> disable in toolbar
#self._toolbar = PlotToolbar(self)
self.canvas.mpl_connect('resize_event', self.clear_background)
def _set_background(self,ax):
"""Add three patches representing [min max],[5,95] and [25,75] percentiles, and a line at the median.
"""
# settings
patch_color = 'b' #blue
patch_lw = 0 #no edges
patch_alpha = .15 # transparancy
median_color = 'b' #blue
median_width = 1.5 #linewidth
percentiles = [0, 5, 25, 50, 75, 100]
# ordinate
xax = scipy.arange(self._data.shape[self.minor_axis])
#vertices
verts_0 = [] #100,0 verts_0 = [] #100,0
verts_1 = [] # 90,10 verts_1 = [] # 90,10
verts_2 = [] # 75,25 verts_2 = [] # 75,25
med = [] med = []
# add top vertices the low vertices (do i need an order?)#background
for i in xax: for i in xax:
pp = prctile(self._data[i,:], [0.,5.,25,50.,75.,95.,100]) prct = prctile(self._data.take([i], self.minor_axis), percentiles)
verts_0.append((i,pp[0])) verts_0.append((i, prct[0]))
verts_1.append((i,pp[1])) verts_1.append((i, prct[1]))
verts_2.append((i,pp[2])) verts_2.append((i, prct[2]))
med.append(prct[3])
for i in xax[::-1]: for i in xax[::-1]:
pp = prctile(self._data[i,:], [0.,5.,25,50.,75.,95.,100]) prct = prctile(self._data.take([i], self.minor_axis), percentiles)
verts_0.append((i, pp[-1])) verts_0.append((i, prct[-1]))
verts_1.append((i, pp[-2])) verts_1.append((i, prct[-2]))
verts_2.append((i, pp[-3])) verts_2.append((i, prct[-3]))
med.append(pp[3])
bck0 = Polygon(verts_0, alpha=.15, lw=0) # make polygons from vertices
bck1 = Polygon(verts_1, alpha=.15, lw=0) bck0 = Polygon(verts_0, alpha=patch_alpha, lw=patch_lw,
bck2 = Polygon(verts_2, alpha=.15, lw=0) facecolor=patch_color)
bck1 = Polygon(verts_1, alpha=patch_alpha, lw=patch_lw,
self.ax.add_patch(bck0) facecolor=patch_color)
self.ax.add_patch(bck1) bck2 = Polygon(verts_2, alpha=patch_alpha, lw=patch_lw,
self.ax.add_patch(bck2) facecolor=patch_color)
self.ax.plot(xax,med, 'b')
self.ax.autoscale_view() # add polygons to axes
ax.add_patch(bck0)
self.add(self.canvas) ax.add_patch(bck1)
self.canvas.show() ax.add_patch(bck2)
# median line
#FIXME: Lineview plot cannot do selections -> disable in toolbar ax.plot(xax, med, median_color, linewidth=median_width)
self._toolbar = PlotToolbar(self)
self.canvas.mpl_connect('resize_event', self.clear_background)
def clear_background(self, event): def clear_background(self, event):
"""Callback on resize event. Clears the background.
"""
self._background = None self._background = None
def set_current_selection(self, selection): def set_current_selection(self, selection):
"""Draws the current selection.
"""
ids = selection[self.current_dim] # current identifiers ids = selection[self.current_dim] # current identifiers
index = self.dataset.get_indices(self.current_dim, ids) index = self.dataset.get_indices(self.current_dim, ids)
if len(index)==0: # do we have a selection
return
if self.use_blit: if self.use_blit:
if self._background is None: if self._background is None:
self._bbox = self.ax.bbox.deepcopy() self._bbox = self.ax.bbox.deepcopy()
self._background = self.canvas.copy_from_bbox(self.ax.bbox) self._background = self.canvas.copy_from_bbox(self.ax.bbox)
self.canvas.restore_region(self._background) self.canvas.restore_region(self._background)
if len(index)>0: # do we have a selection if len(self.ax.collections)>0: # remove old linecollection
if len(self.ax.collections)>0: self.ax.collections = []
self.ax.collections = [] segs = [self.line_segs[i] for i in index]
segs = [self.line_segs[i] for i in index] line_coll = LineCollection(segs, colors=(1,0,0,1))
line_coll = LineCollection(segs, colors=(1,0,0,1)) line_coll.set_clip_box(self.ax.bbox)
line_coll.set_clip_box(self.ax.bbox) self.ax.update_datalim(line_coll.get_verts(self.ax.transData))
self.ax.update_datalim(line_coll.get_verts(self.ax.transData)) #draw
if self.use_blit:
if self.use_blit: self.ax.draw_artist(line_coll)
self.ax.draw_artist(line_coll) self.canvas.blit()
line_coll.get_clip_box().get_bounds() else:
self.canvas.blit() self.ax.add_collection(line_coll)
self.canvas.draw()
else:
self.ax.add_collection(line_coll)
self.canvas.draw()
class ScatterMarkerPlot(Plot): class ScatterMarkerPlot(Plot):
"""The ScatterMarkerPlot is faster than regular scatterplot, but """The ScatterMarkerPlot is faster than regular scatterplot, but
@ -596,6 +628,7 @@ class ScatterPlot(Plot):
assert y1<=y2 assert y1<=y2
index = scipy.nonzero((xdata>x1) & (xdata<x2) & (ydata>y1) & (ydata<y2))[0] index = scipy.nonzero((xdata>x1) & (xdata<x2) & (ydata>y1) & (ydata<y2))[0]
ids = self.dataset_1.get_identifiers(self.current_dim, index) ids = self.dataset_1.get_identifiers(self.current_dim, index)
self.selection_listener(self.current_dim, ids) self.selection_listener(self.current_dim, ids)
@ -610,7 +643,7 @@ class ScatterPlot(Plot):
if self._background is None: if self._background is None:
self._background = self.canvas.copy_from_bbox(self.ax.bbox) self._background = self.canvas.copy_from_bbox(self.ax.bbox)
self.canvas.restore_region(self._background) self.canvas.restore_region(self._background)
lw = scipy.zeros(self.xaxis_data.shape) lw = scipy.zeros(self.xaxis_data.shape,'f')
if len(index)>0: if len(index)>0:
lw.put(2.,index) lw.put(2.,index)
self.coll.set_linewidth(lw) self.coll.set_linewidth(lw)