Пример #1
0
        def nosql_draw2(check_func, match):
            from matplotlib.backends.backend_agg import FigureCanvas

            try:
                from matplotlib.backends.backend_agg import Figure
            except ImportError:
                from matplotlib.figure import Figure

            was_interactive = mpl.is_interactive()
            if was_interactive:
                mpl.interactive(False)
            # fnum = 32
            fig = Figure()
            canvas = FigureCanvas(fig)  # NOQA
            # fig.clf()
            ax = fig.add_subplot(1, 1, 1)
            if check_func is not None and check_func():
                return
            ax, xywh1, xywh2 = match.show(ax=ax)
            if check_func is not None and check_func():
                return
            savekw = {
                # 'dpi' : 60,
                'dpi': 80,
            }
            axes_extents = pt.extract_axes_extents(fig)
            # assert len(axes_extents) == 1, 'more than one axes'
            extent = axes_extents[0]
            with io.BytesIO() as stream:
                # This call takes 23% - 15% of the time depending on settings
                fig.savefig(stream, bbox_inches=extent, **savekw)
                stream.seek(0)
                data = np.fromstring(stream.getvalue(), dtype=np.uint8)
            if check_func is not None and check_func():
                return
            pt.plt.close(fig)
            image = cv2.imdecode(data, 1)
            thumbsize = 221
            max_dsize = (thumbsize, thumbsize)
            dsize, sx, sy = vt.resized_clamped_thumb_dims(
                vt.get_size(image), max_dsize)
            if check_func is not None and check_func():
                return
            image = vt.resize(image, dsize)
            vt.imwrite(fpath, image)
            if check_func is not None and check_func():
                return
Пример #2
0
def make_plot(ts, dat, ylabel='Data', ylog=False, enums=None,
              ts2=None, dat2=None, y2label=None,
              y2log=False,  enums2=None,
              tmin=None, tmax=None, fname=None,
              ymin=None, ymax=None, y2min=None, y2max=None):

    fig    = Figure(figsize=(8., 5.0), dpi=300)
    gspec  = GridSpec(1, 1)
    canvas = FigureCanvas(fig)
    axes   = fig.add_subplot(gspec[0], facecolor='#FFFFFF')



    if ylog: #  and False:
        axes.set_yscale('log', basey=10)
        pos = np.where(dat>0)
        ts  = ts[pos]
        dat = dat[pos]

    tvals  = [ts2date(t) for t in ts]

    if enums is not None:
        pad = min(0.8, 0.1*len(enums))
        axes.set_ylim(-pad, len(enums)-1+pad)
        axes.set_yticks(range(len(enums)))
        axes.set_yticklabels(enums)

    try:
        axes.get_yaxis().get_major_formatter().set_useOffset(False)
    except:
        pass


    if tmin is not None and tmax is not None:
        axes.set_xlim((ts2date(tmin), ts2date(tmax)))
    if ymin is not None or ymax is not None:
        if ymin is not None:
            ymin = float(ymin)
        else:
            ymin = min(dat)
        if ymax is not None:
            ymax = float(ymax)
        else:
            ymax = max(dat)
        axes.set_ylim((ymin, ymax))

    axes.set_xlabel('Date', fontproperties=mplfont)
    axes.set_ylabel(ylabel, color='b', fontproperties=mplfont)
    axes.xaxis.set_major_formatter(DateFormatter("%H:%M\n%b-%d"))
    axes.plot(tvals, dat, color='b', **plotopts)
    for x in axes.get_xticklabels():
        x.set_rotation(0)
        x.set_ha('center')

    axes.grid(True, zorder=-5)


    if ts2 is not None:
        if len(fig.get_axes()) < 2:
            ax = axes.twinx()
        axes = fig.get_axes()[1]
        axes0 = fig.get_axes()[0]
        axes0.set_zorder(axes.get_zorder()+10)
        axes0.patch.set_visible(False)
        axes0.grid(axes.get_zorder()-5)
        axes.grid(False)
        try:
            axes.get_yaxis().get_major_formatter().set_useOffset(False)
        except:
            pass

        if y2log:
            axes.set_yscale('log', basey=10)
            pos = np.where(dat2>0)
            ts2 = ts2[pos]
            dat2 = dat2[pos]
        t2vals  = [ts2date(t) for t in ts2]
        if enums2 is not None:
            pad = min(0.8, 0.1*len(enums2))
            axes.set_ylim(-pad, len(enums2)-1+pad)
            axes.set_yticks(range(len(enums2)))
            axes.set_yticklabels(enums2)
        plotopts['zorder'] = 20
        axes.plot(t2vals, dat2, color='r', **plotopts)
        axes.set_ylabel(y2label, color='r', fontproperties=mplfont)
        if tmin is not None and tmax is not None:
            axes.set_xlim((ts2date(tmin), ts2date(tmax)))

        if y2min is not None or y2max is not None:
            if y2min is not None:
                y2min = float(y2min)
            else:
                y2min = min(dat2)
            if y2max is not None:
                y2max = float(y2max)
            else:
                y2max = max(dat2)

            axes.set_ylim((y2min, y2max))


    auto_margins(fig, canvas, axes, gspec)
    if fname is None:
        figdata = io.BytesIO()
        fig.savefig(figdata, format='png', facecolor='#FDFDFA')
        figdata.seek(0)
        return base64.b64encode(figdata.getvalue())
    else:
        fig.savefig(fname)
        return fname
Пример #3
0
    def nosql_draw(check_func, rchip1_fpath, rchip2_fpath, kpts1, kpts2):
        # This gets executed in the child thread and does drawing async style
        #from matplotlib.backends.backend_pdf import FigureCanvasPdf as FigureCanvas
        #from matplotlib.backends.backend_pdf import Figure
        #from matplotlib.backends.backend_svg import FigureCanvas
        #from matplotlib.backends.backend_svg import Figure
        from matplotlib.backends.backend_agg import FigureCanvas
        try:
            from matplotlib.backends.backend_agg import Figure
        except ImportError:
            from matplotlib.figure import Figure

        kpts1_ = vt.offset_kpts(kpts1, (0, 0), (resize_factor, resize_factor))
        kpts2_ = vt.offset_kpts(kpts2, (0, 0), (resize_factor, resize_factor))

        #from matplotlib.figure import Figure
        if check_func is not None and check_func():
            return

        rchip1 = vt.imread(rchip1_fpath)
        rchip1 = vt.resize_image_by_scale(rchip1, resize_factor)
        if check_func is not None and check_func():
            return
        rchip2 = vt.imread(rchip2_fpath)
        rchip2 = vt.resize_image_by_scale(rchip2, resize_factor)
        if check_func is not None and check_func():
            return

        try:
            idx = cm.daid2_idx[daid]
            fm   = cm.fm_list[idx]
            fsv  = None if cm.fsv_list is None else cm.fsv_list[idx]
            fs   = None if fsv is None else fsv.prod(axis=1)
        except KeyError:
            fm = []
            fs = None
            fsv = None

        maxnum = 200
        if fs is not None and len(fs) > maxnum:
            # HACK TO ONLY SHOW TOP MATCHES
            sortx = fs.argsort()[::-1]
            fm = fm.take(sortx[:maxnum], axis=0)
            fs = fs.take(sortx[:maxnum], axis=0)

        was_interactive = mpl.is_interactive()
        if was_interactive:
            mpl.interactive(False)
        #fnum = 32
        fig = Figure()
        canvas = FigureCanvas(fig)  # NOQA
        #fig.clf()
        ax = fig.add_subplot(1, 1, 1)
        if check_func is not None and check_func():
            return
        #fig = pt.plt.figure(fnum)
        #H1 = np.eye(3)
        #H2 = np.eye(3)
        #H1[0, 0] = .5
        #H1[1, 1] = .5
        #H2[0, 0] = .5
        #H2[1, 1] = .5
        ax, xywh1, xywh2 = pt.show_chipmatch2(rchip1, rchip2, kpts1_, kpts2_, fm,
                                              fs=fs, colorbar_=False, ax=ax)
        if check_func is not None and check_func():
            return
        savekw = {
            # 'dpi' : 60,
            'dpi' : 80,
        }
        axes_extents = pt.extract_axes_extents(fig)
        #assert len(axes_extents) == 1, 'more than one axes'
        extent = axes_extents[0]
        with io.BytesIO() as stream:
            # This call takes 23% - 15% of the time depending on settings
            fig.savefig(stream, bbox_inches=extent, **savekw)
            stream.seek(0)
            data = np.fromstring(stream.getvalue(), dtype=np.uint8)
        if check_func is not None and check_func():
            return
        pt.plt.close(fig)
        image = cv2.imdecode(data, 1)
        thumbsize = 221
        max_dsize = (thumbsize, thumbsize)
        dsize, sx, sy = vt.resized_clamped_thumb_dims(vt.get_size(image), max_dsize)
        if check_func is not None and check_func():
            return
        image = vt.resize(image, dsize)
        vt.imwrite(fpath, image)
        if check_func is not None and check_func():
            return