示例#1
0
class LassoManager:
    def __init__(self, ax, data,labels=None, color_on='r', color_off='k'):
        self.axes = ax
        self.canvas = ax.figure.canvas
        self.data = data
        self.call_list = []

        self.Nxy = data.shape[0]
        self.color_on = colorConverter.to_rgba(color_on)
        self.color_off = colorConverter.to_rgba(color_off)

        facecolors = [self.color_on for _ in range(self.Nxy)]
        fig = ax.figure
        self.collection = RegularPolyCollection(
            fig.dpi, 6, sizes=(1,),
            facecolors=facecolors,
            edgecolors=facecolors,
            offsets = self.data,
            transOffset = ax.transData)

        ax.add_collection(self.collection, autolim=True)
        ax.autoscale_view()
        
        if labels is not None:
            ax.set_xlabel(labels[0])
            ax.set_ylabel(labels[1])
        self.cid = self.canvas.mpl_connect('button_press_event', self.onpress)
        self.ind = None
        self.canvas.draw()

    def register(self, callback_func):
        self.call_list.append(callback_func)
        
    def callback(self, verts):
        facecolors = self.collection.get_facecolors()
        edgecolors = self.collection.get_edgecolors()
        ind = nonzero(points_inside_poly(self.data, verts))[0]
        for i in range(self.Nxy):
            if i in ind:
                facecolors[i] = self.color_on 
                edgecolors[i] = self.color_on 
            else:
                facecolors[i] = self.color_off
                edgecolors[i] = self.color_off

        self.canvas.draw_idle()
        self.canvas.widgetlock.release(self.lasso)
        del self.lasso
        self.ind = ind

        for func in self.call_list:
            func(ind)
            
    def onpress(self, event):
        if self.canvas.widgetlock.locked(): return
        if event.inaxes is None: return
        self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata), self.callback)
        # acquire a lock on the widget drawing
        self.canvas.widgetlock(self.lasso)
示例#2
0
class LassoManager:
    def __init__(self, ax, data, fs_list, plkwargs, max_collection=500):
        if pyfusion.VERBOSE > 2:
            print("init, fs_list size=%d" % (len(fs_list)))
        self.axes = ax
        self.canvas = ax.figure.canvas
        self.data = data
        self.fs_list = fs_list
        self.plkwargs = plkwargs

        self.Nxy = len(data)

        facecolors = [d.color for d in data]
        self.xys = [(d.x, d.y) for d in data]
        fig = ax.figure
        # here we define the polygons that indicate that a point is registered
        # - use empty triangles for simplicity and speed
        # skip altogether if there are too many
        # not sure how much this saves - each point still is a datum
        if (self.Nxy < max_collection):
            self.collection = RegularPolyCollection(3,
                                                    0,
                                                    sizes=(100, ),
                                                    facecolors='',
                                                    edgecolors=facecolors,
                                                    offsets=self.xys,
                                                    transOffset=ax.transData)

            ax.add_collection(self.collection)
        else:
            self.collection = None

        self.cid = self.canvas.mpl_connect('button_press_event', self.onpress)
        print("connected cid=%d" % (self.cid))

        self.ind = None

    def callback(self, verts):
        path = Path(verts)
        self.ind = nonzero(path.contains_points(self.xys))[0]
        if pyfusion.VERBOSE > 2: print len(self.xys)
        if pyfusion.VERBOSE > 0:
            print("Try to match the following points to %d fs in list:" %
                  (len(self.fs_list)))
            print[self.xys[i] for i in ind]

        if self.collection != None:
            edgecolors = self.collection.get_edgecolors()
            for i in range(self.Nxy):
                if i in ind:
                    edgecolors[i] = Datum.colorin
                else:
                    edgecolors[i] = Datum.colorout

        print(self)
        xarr = [xy[0] for xy in self.xys]
        yarr = [xy[1] for xy in self.xys]
        fstarr = array(self.fs_list['t_mid'])
        fsfarr = array(self.fs_list['freq'])
        if pyfusion.VERBOSE > 5:
            print("xarr = %s, fsfarr = %s, fstarr=%s, ind = %s" %
                  (xarr, fsfarr, fstarr, ind))
        for i in self.ind:
            match = (abs(xarr[i] - fstarr) + abs(yarr[i] - fsfarr)) < 1e-3
            matchinds = match.nonzero()[0]
            if pyfusion.VERBOSE > 5: print("matches", matchinds)
            if len(matchinds) > 6:
                print('only showing first 6...')
                matchinds = matchinds[0:10]
            for m in matchinds:
                #self.fs_list[m].plot(**self.plkwargs)
                print("shot {0}, time {1} s".format(self.fs_list['shot'][m],
                                                    self.fs_list['t_mid'][m]))
        self.canvas.draw_idle()
        self.canvas.widgetlock.release(self.lasso)
        del self.lasso

    def onpress(self, event):
        if self.canvas.widgetlock.locked(): return
        if event.inaxes is None: return
        if pyfusion.VERBOSE > 7: print event.xdata
        # left button is normal - right does lasso - but this seesm to hang - crash?
        ## if event.button == 1: self.canvas.button_press_event(event)
        self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata),
                           self.callback)
        # acquire a lock on the widget drawing
        self.canvas.widgetlock(self.lasso)
示例#3
0
class LassoManager:
    def __init__(self, ax, data, labels=None, color_on='r', color_off='k'):
        self.axes = ax
        self.canvas = ax.figure.canvas
        self.data = data
        self.call_list = []

        self.Nxy = data.shape[0]
        self.color_on = colorConverter.to_rgba(color_on)
        self.color_off = colorConverter.to_rgba(color_off)

        facecolors = [self.color_on for _ in range(self.Nxy)]
        fig = ax.figure
        self.collection = RegularPolyCollection(fig.dpi,
                                                6,
                                                sizes=(1, ),
                                                facecolors=facecolors,
                                                edgecolors=facecolors,
                                                offsets=self.data,
                                                transOffset=ax.transData)

        ax.add_collection(self.collection, autolim=True)
        ax.autoscale_view()

        if labels is not None:
            ax.set_xlabel(labels[0])
            ax.set_ylabel(labels[1])
        self.cid = self.canvas.mpl_connect('button_press_event', self.onpress)
        self.ind = None
        self.canvas.draw()

    def register(self, callback_func):
        self.call_list.append(callback_func)

    def callback(self, verts):
        facecolors = self.collection.get_facecolors()
        edgecolors = self.collection.get_edgecolors()
        ind = nonzero(points_inside_poly(self.data, verts))[0]
        for i in range(self.Nxy):
            if i in ind:
                facecolors[i] = self.color_on
                edgecolors[i] = self.color_on
            else:
                facecolors[i] = self.color_off
                edgecolors[i] = self.color_off

        self.canvas.draw_idle()
        self.canvas.widgetlock.release(self.lasso)
        del self.lasso
        self.ind = ind

        for func in self.call_list:
            func(ind)

    def onpress(self, event):
        if self.canvas.widgetlock.locked(): return
        if event.inaxes is None: return
        self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata),
                           self.callback)
        # acquire a lock on the widget drawing
        self.canvas.widgetlock(self.lasso)
示例#4
0
class LassoManager:
    def __init__(self, ax, data, fs_list, plkwargs, max_collection=500):
        if pyfusion.VERBOSE>2:
            print("init, fs_list size=%d" % (len(fs_list)))
        self.axes = ax
        self.canvas = ax.figure.canvas
        self.data = data
        self.fs_list = fs_list
        self.plkwargs = plkwargs

        self.Nxy = len(data)

        facecolors = [d.color for d in data]
        self.xys = [(d.x, d.y) for d in data]
        fig = ax.figure
        # here we define the polygons that indicate that a point is registered
        # - use empty triangles for simplicity and speed
        # skip altogether if there are too many
        # not sure how much this saves - each point still is a datum
        if (self.Nxy<max_collection):
            self.collection = RegularPolyCollection(
                3, 0, sizes=(100,),facecolors='', edgecolors=facecolors,
                offsets = self.xys, transOffset = ax.transData)

            ax.add_collection(self.collection)
        else: self.collection = None    

        self.cid = self.canvas.mpl_connect('button_press_event', self.onpress)
        print("connected cid=%d" % (self.cid) )

        self.ind = None

    def callback(self, verts):
        ind = nonzero(points_inside_poly(self.xys, verts))[0]
        if pyfusion.VERBOSE>2: print len(self.xys)
        if pyfusion.VERBOSE>0: 
            print("Try to match the following points to %d fs in list:" % (len(self.fs_list)))
            print [self.xys[i] for i in ind]
            
        if self.collection != None:    
            edgecolors = self.collection.get_edgecolors()
            for i in range(self.Nxy):
                if i in ind:
                    edgecolors[i] = Datum.colorin
                else:
                    edgecolors[i] = Datum.colorout

        print(self)        
        xarr = [xy[0] for xy in self.xys]
        yarr = [xy[1] for xy in self.xys]
        fstarr = array(self.fs_list['t_mid'])
        fsfarr = array(self.fs_list['freq'])
        if pyfusion.VERBOSE>5:
            print("xarr = %s, fsfarr = %s, fstarr=%s, ind = %s" % (xarr, fsfarr, fstarr, ind))
        for i in ind:
            match = (abs(xarr[i] - fstarr) + abs(yarr[i] - fsfarr))< 1e-3
            matchinds = match.nonzero()[0]
            if pyfusion.VERBOSE>5: print("matches", matchinds)
            if len(matchinds)>6:
                print('only showing first 6...')
                matchinds = matchinds[0:10]
            for m in matchinds: 
                #self.fs_list[m].plot(**self.plkwargs)
                print("shot {0}, time {1} s".format(self.fs_list['shot'][m],self.fs_list['t_mid'][m]))
        self.canvas.draw_idle()
        self.canvas.widgetlock.release(self.lasso)
        del self.lasso
        self.ind = ind
    def onpress(self, event):
        if self.canvas.widgetlock.locked(): return
        if event.inaxes is None: return
        if pyfusion.VERBOSE>7: print event.xdata
        # left button is normal - right does lasso - but this seesm to hang - crash?
        ## if event.button == 1: self.canvas.button_press_event(event)  
        self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata), self.callback)
        # acquire a lock on the widget drawing
        self.canvas.widgetlock(self.lasso)