diff --git a/fluents/plots.py b/fluents/plots.py index 1c90695..bc53747 100644 --- a/fluents/plots.py +++ b/fluents/plots.py @@ -9,7 +9,8 @@ import matplotlib from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas from matplotlib.backend_bases import NavigationToolbar2,cursors from matplotlib.backends.backend_gtk import FileChooserDialog,cursord -from matplotlib.widgets import SubplotTool,RectangleSelector +from matplotlib.widgets import SubplotTool,RectangleSelector,Lasso +from matplotlib.nxutils import points_inside_poly from matplotlib.axes import Subplot from matplotlib.figure import Figure from matplotlib import cm,cbook @@ -459,7 +460,7 @@ class Plot (View): """ self.selection_listener = listener - def update_selection(self, ids, key): + def update_selection(self, ids, key=None): """Returns updated current selection from ids. If a key is pressed we use the appropriate mode. @@ -641,6 +642,15 @@ class ScatterMarkerPlot(Plot): ids = self.update_selection(ids, key) self.selection_listener(self.current_dim, ids) + def lasso_select_callback(self, verts, key=None): + self.canvas.draw_idle() + xys = scipy.c_[self.xaxis_data[:,scipy.newaxis], self.yaxis_data[:,scipy.newaxis]] + index = scipy.nonzero(points_inside_poly(xys, verts))[0] + ids = self.dataset_1.get_identifiers(self.current_dim, index) + ids = self.update_selection(ids, key) + self.selection_listener(self.current_dim, ids) + self.canvas.widgetlock.release(self._lasso) + def set_current_selection(self, selection): ids = selection[self.current_dim] # current identifiers index = self.dataset_1.get_indices(self.current_dim, ids) @@ -750,6 +760,15 @@ class ScatterPlot(Plot): ids = self.update_selection(ids, key) self.selection_listener(self.current_dim, ids) + def lasso_select_callback(self, verts, key=None): + self.canvas.draw_idle() + xys = scipy.c_[self.xaxis_data[:,scipy.newaxis], self.yaxis_data[:,scipy.newaxis]] + index = scipy.nonzero(points_inside_poly(xys, verts))[0] + ids = self.dataset_1.get_identifiers(self.current_dim, index) + ids = self.update_selection(ids, key) + self.selection_listener(self.current_dim, ids) + self.canvas.widgetlock.release(self._lasso) + def set_current_selection(self, selection): ids = selection[self.current_dim] # current identifiers if len(ids)==0: @@ -857,6 +876,23 @@ class NetworkPlot(Plot): ids = self.update_selection(ids, key) self.selection_listener(self.current_dim, ids) + def lasso_select_callback(self, verts, key=None): + pos = self.keywords['pos'] + xys = [] + node_ids = [] + c = 0 + for name,(x,y) in pos.items(): + node_ids.append(name) + xys.append((x,y)) + c+=1 + + self.canvas.draw_idle() + index = scipy.nonzero(points_inside_poly(xys, verts))[0] + ids = [node_ids[i] for i in index] + ids = self.update_selection(ids, key) + self.selection_listener(self.current_dim, ids) + self.canvas.widgetlock.release(self._lasso) + def set_current_selection(self, selection): ids = selection[self.current_dim] # current identifiers node_set = set(self.graph.nodes()) @@ -1135,6 +1171,29 @@ class ZoomPlotMode (PlotMode): self.canvas.draw() +class SelectPlotMode2 (PlotMode): + def __init__(self, plot): + PlotMode.__init__(self, plot, 'lassoselect', + 'Select within lasso', 'lasso') + self._selectors = {} + + def activate(self): + self._button_press = self.canvas.mpl_connect( + 'button_press_event', self._on_select) + + def deactivate(self): + self._mpl_disconnect_all() + self.plot._lasso = None + + def _on_select(self, event): + if self.canvas.widgetlock.locked(): return + if event.inaxes is None: return + self.plot._lasso = Lasso(event.inaxes, (event.xdata, event.ydata), self.plot.lasso_select_callback) + self.plot._lasso.line.set_linewidth(1) + self.plot._lasso.line.set_linestyle('--') + # get a lock on the widget + self.canvas.widgetlock(self.plot._lasso) + class SelectPlotMode (PlotMode): def __init__(self, plot): PlotMode.__init__(self, plot, 'select', @@ -1162,7 +1221,6 @@ class SelectPlotMode (PlotMode): end.xdata, end.ydata, end.key) - class PlotToolbar(gtk.Toolbar): def __init__(self, plot): @@ -1182,6 +1240,7 @@ class PlotToolbar(gtk.Toolbar): self.add_mode(PanPlotMode(self.plot)) self.add_mode(ZoomPlotMode(self.plot)) self.add_mode(SelectPlotMode(self.plot)) + self.add_mode(SelectPlotMode2(self.plot)) self.insert(gtk.SeparatorToolItem(), -1) self.set_style(gtk.TOOLBAR_ICONS)