def getAllRegionHepSpecsPieChart(request): #totalRegionsHepSpecs = Zone.objects.all().aggregate(total_hepSpecs=Sum('hepspecs')) totalRegionHepSpecs = sum([zn.hepspectotal() for zn in Zone.objects.all()]) if (totalRegionHepSpecs == None): totalRegionHepSpecs = 0 #totalAllocatedHepSpecs = TopLevelAllocationByZone.objects.all().extra(select = {'total': 'SUM((hepspec_fraction * zone.hepspecs)/100)'}).values('hepspec_fraction', 'zone__hepspecs', 'total') topLevelAllocationObjects = TopLevelAllocation.objects.all().values('hepspec') totalAllocHepSpecs = 0.0 for oneObject in topLevelAllocationObjects: if (oneObject['hepspec'] != None): totalAllocHepSpecs = totalAllocHepSpecs + oneObject['hepspec'] fig = Figure(figsize=(4,4)) canvas = FigureCanvas(fig) ax = fig.add_subplot(111) labels = [] fracs = [] allotedPer = 0 if totalRegionHepSpecs > 0: allotedPer = (totalAllocHepSpecs/totalRegionHepSpecs) * 100 freePer = 100 - allotedPer labels.append('Free') fracs.append(freePer) if allotedPer > 0: labels.append('Allocated') fracs.append(allotedPer) patches, texts, autotexts = ax.pie(fracs, explode=None, labels=labels, colors=('g', 'r', 'c', 'm', 'y', 'k', 'w', 'b'), autopct='%.2f%%', pctdistance=0.4, labeldistance=1.1, shadow=False) ax.set_title('\n Total Hepspec Allocation - All Regions \n Total: ' + str(round(totalRegionHepSpecs, 3)), fontdict=None, verticalalignment='bottom') ax.grid(True) #fig.canvas.mpl_connect('button_press_event', onclick) response=HttpResponse(content_type='image/png') canvas.print_png(response) canvas.draw() return response
def chart(self): clf() img_dpi=72 width=400 height=300 fig=figure(dpi=img_dpi, figsize=(width/img_dpi, height/img_dpi)) x=arange(0, 2*pi+0.1, 0.1) sine=plot(x, sin(x)) legend(sine, "y=sin x", "upper right") xlabel('x') ylabel('y=sin x') grid(True) canvas = FigureCanvasAgg(fig) canvas.draw() size = (int(canvas.figure.get_figwidth())*img_dpi, int(canvas.figure.get_figheight())*img_dpi) buf=canvas.tostring_rgb() im=PILImage.fromstring('RGB', size, buf, 'raw', 'RGB', 0, 1) imgdata=StringIO() im.save(imgdata, 'PNG') self.REQUEST.RESPONSE.setHeader('Pragma', 'no-cache') self.REQUEST.RESPONSE.setHeader('Content-Type', 'image/png') return imgdata.getvalue() # <markdowncell> # 2. Then create an external method in ZMI (e.g. Id -\> mplchart, module # name -\> mpl, function name -\> chart). # # 3. Click the Test tab and you should see the sine plot. # # * * * * * # # CategoryCookbookMatplotlib #
def getZoneHepSpecsPieChart(request): regionName = request.REQUEST.get("regionname", "") zoneName = request.REQUEST.get("zonename", "") totalZoneHepSpecs = Zone.objects.get(name=zoneName, region__name=regionName) if totalZoneHepSpecs.hepspecs == None: totalZoneHepSpecs.hepspecs = 0 else: totalZoneHepSpecs.hepspecs = totalZoneHepSpecs.hepspectotal() topLevelAllocationByZoneObjects = TopLevelAllocationByZone.objects.filter(zone__name=zoneName, zone__region__name=regionName).values('hepspec_fraction', 'zone__hepspecs') totalAllocHepSpecs = 0.0 for oneObject in topLevelAllocationByZoneObjects: if ( (oneObject['hepspec_fraction'] != None) and (oneObject['zone__hepspecs'] != None) ): totalAllocHepSpecs = totalAllocHepSpecs + ((oneObject['hepspec_fraction'] * oneObject['zone__hepspecs'])/100) fig = Figure(figsize=(4,4)) canvas = FigureCanvas(fig) ax = fig.add_subplot(111) labels = [] fracs = [] allotedPer = 0 if (totalZoneHepSpecs.hepspecs) > 0: allotedPer = (totalAllocHepSpecs/totalZoneHepSpecs.hepspecs) * 100 freePer = 100 - allotedPer labels.append('Free') fracs.append(freePer) if (allotedPer > 0): labels.append('Allocated') fracs.append(allotedPer) patches, texts, autotexts = ax.pie(fracs, explode=None, labels=labels, colors=('g', 'r', 'c', 'm', 'y', 'k', 'w', 'b'), autopct='%.2f%%', pctdistance=0.4, labeldistance=1.1, shadow=False) ax.set_title('\n Hepspec Allocation - Zone - ' + zoneName + '\n Region: ' + regionName + '(Total: ' + str(round(totalZoneHepSpecs.hepspecs, 3)) + ')', fontdict=None, verticalalignment='bottom') ax.grid(True) response=HttpResponse(content_type='image/png') canvas.print_png(response) canvas.draw() return response
def draw(self): """ Draw the figure using the agg renderer """ self.canvas.clear() FigureCanvasAgg.draw(self) if self.blitbox is None: l, b, w, h = self.figure.bbox.bounds w, h = int(w), int(h) buf_rgba = self.get_renderer().buffer_rgba() else: bbox = self.blitbox l, b, r, t = bbox.extents w = int(r) - int(l) h = int(t) - int(b) t = int(b) + h reg = self.copy_from_bbox(bbox) buf_rgba = reg.to_string() texture = Texture.create(size=(w, h)) texture.flip_vertical() with self.canvas: Color(1.0, 1.0, 1.0, 1.0) self.img_rect = Rectangle(texture=texture, pos=self.pos, size=(w, h)) texture.blit_buffer(bytes(buf_rgba), colorfmt="rgba", bufferfmt="ubyte") self.img_texture = texture
def show(self): self.figure.tight_layout() FigureCanvasAgg.draw(self) if PORT is None: return if matplotlib.__version__ < '1.2': buffer = self.tostring_rgb(0, 0) else: buffer = self.tostring_rgb() if len(set(buffer)) <= 1: # do not plot empty return render = self.get_renderer() width = int(render.width) plot_index = index if os.getenv("PYCHARM_MATPLOTLIB_INTERACTIVE", False) else -1 try: sock = socket.socket() sock.connect((HOST, PORT)) sock.send(struct.pack('>i', width)) sock.send(struct.pack('>i', plot_index)) sock.send(struct.pack('>i', len(buffer))) sock.send(buffer) except OSError as _: # nothing bad. It just means, that our tool window doesn't run yet pass
def small_plot(data): x, y, area, colors = data fig = Figure(figsize=(3, 3), dpi=80) axes = fig.add_axes([0.0, 0.0, 1.0, 1.0], alpha=1.0) axes.set_frame_on(False) axes.set_xticks([]) axes.set_yticks([]) # axes.set_xlim(5, 6) # axes.set_ylim(5, 6) axes.scatter(x, y, s=area, c=colors, alpha=0.5) axes.set_xlim(4, 7) axes.set_ylim(4, 7) canvas = FigureCanvasAgg(fig) canvas.draw() # canvas = FigureCanvasQTAgg(fig) # buf = canvas.tostring_rgb() buf = fig.canvas.tostring_rgb() ncols, nrows = fig.canvas.get_width_height() img = np.fromstring(buf, dtype=np.uint8).reshape(nrows, ncols, 3) return img
class HistogramMonitor: def __init__(self, control): self.control = control print('Initing graph..') self.fig = plt.figure() self.ax = self.fig.add_subplot(1, 1, 1) self.canvas = FigureCanvas(self.fig) # Hold = False -> the new chart replaces the old self.ax.hold(False) #plt.ion() # start interactive mode to get the figure self.ax.bar(range(256), [0]*256) self.ax.set_xlabel('Brightness') self.ax.set_ylabel('n') self.canvas.print_figure('Histogram') #time.sleep(1) print('Graph drawn!') def update(self): newHistogram = self.control.getNewHistogram() newError = self.control.getError() # Update graph if the histogram has been updated if newHistogram: self.ax.bar(range(len(newHistogram)), newHistogram) print("Plotted a new histogram") else: print("No new histogram available") # Update title #title = "Error: {0}".format(newError) #self.ax.title = title # Draw the changed plot self.canvas.draw()
def spec_plot(x, y, img, file_name, figure_size=(10,5), transparent=False): import matplotlib import matplotlib.figure import matplotlib.cm from matplotlib.backends.backend_agg import FigureCanvasAgg figure_size = None F = first_moment_analysis(x,y) fig = matplotlib.figure.Figure(figure_size, 144, linewidth=0, facecolor="white") if transparent : self.figure.figurePatch.set_alpha(0.0) canvas = FigureCanvasAgg(fig) p = fig.add_subplot(211) p.set_position([0.1,0.3,0.8,0.6]) p.plot(x, y, '-') fm = F.as_trace() p.plot(fm[0],fm[1],"r-") p.set_xlim(x[0],x[-1]) p.set_xlabel("position") p.set_ylabel("intensity") p.set_title("X-ray emission spectrum, first moment=%.2f"%(F.first_moment)) p2 = fig.add_subplot(212) p2.set_position([0.1, 0.05, 0.8, 0.2]) im=p2.imshow(img.as_numpy_array(), cmap='spectral') im.set_interpolation("none") position=fig.add_axes([0.91,0.1,0.015,0.35]) # p2.imshow(img.as_numpy_array(), cmap=matplotlib.cm.gist_yarg) p3=fig.colorbar(im, orientation='vertical', cax=position) # p2.set_position([0.1, 0.05, 0.8, 0.2]) canvas.draw() fig.savefig(file_name, dpi=200, format="png")
def mpl_plot_ewma_embedded(figname, xs, ys, span): # create the figure and axes for the plot fig = Figure(figsize=(8, 6), dpi=75, facecolor=(1, 1, 1), edgecolor=(0, 0, 0)) ax = fig.add_subplot(111) # calculate the moving average ewma_ys = ewma(ys, span=span) # plot the data ax.plot(xs, ys, alpha=0.4, label="Raw") ax.plot(xs, ewma_ys, label="EWMA") ax.legend() # write the figure to a temporary image file filename = os.path.join(os.environ["TEMP"], "xlplot_%s.png" % figname) canvas = FigureCanvas(fig) canvas.draw() canvas.print_png(filename) # Show the figure in Excel as a Picture object on the same sheet # the function is being called from. xl = xl_app() caller = xlfCaller() sheet = xl.Range(caller.address).Worksheet # if a picture with the same figname already exists then get the position # and size from the old picture and delete it. for old_picture in sheet.Pictures(): if old_picture.Name == figname: height = old_picture.Height width = old_picture.Width top = old_picture.Top left = old_picture.Left old_picture.Delete() break else: # otherwise place the picture below the calling cell. top_left = sheet.Cells(caller.rect.last_row+2, caller.rect.last_col+1) top = top_left.Top left = top_left.Left width, height = fig.bbox.bounds[2:] # insert the picture # Ref: http://msdn.microsoft.com/en-us/library/office/ff198302%28v=office.15%29.aspx picture = sheet.Shapes.AddPicture(Filename=filename, LinkToFile=0, # msoFalse SaveWithDocument=-1, # msoTrue Left=left, Top=top, Width=width, Height=height) # set the name of the new picture so we can find it next time picture.Name = figname # delete the temporary file os.unlink(filename) return "[Plotted '%s']" % figname
def draw( self ): """ Draw the figure when xwindows is ready for the update """ if DEBUG: print "FigureCanvasQtAgg.draw", self self.replot = True FigureCanvasAgg.draw(self) self.update()
def draw(self): """ Draw the figure with Agg, and queue a request for a Qt draw. """ # The Agg draw is done here; delaying it until the paintEvent # causes problems with code that uses the result of the # draw() to update plot elements. FigureCanvasAgg.draw(self) self.update()
def export_close_figure(self, fobject): """ Export as a string and close the figure. """ canvas = FigureCanvasAgg(fobject) canvas.draw() size, buf = canvas.get_width_height(), canvas.tostring_rgb() #close and return data close() return size, buf
def paintEvent( self, e ): """ Draw to the Agg backend and then copy the image to the qt.drawable. In Qt, all drawing should be done inside of here when a widget is shown onscreen. """ #FigureCanvasQT.paintEvent( self, e ) if DEBUG: print 'FigureCanvasQtAgg.paintEvent: ', self, \ self.get_width_height() if self.replot: FigureCanvasAgg.draw(self) self.replot = False if self.blitbox is None: # matplotlib is in rgba byte order. QImage wants to put the bytes # into argb format and is in a 4 byte unsigned int. Little endian # system is LSB first and expects the bytes in reverse order # (bgra). if QtCore.QSysInfo.ByteOrder == QtCore.QSysInfo.LittleEndian: stringBuffer = self.renderer._renderer.tostring_bgra() else: stringBuffer = self.renderer._renderer.tostring_argb() qImage = QtGui.QImage(stringBuffer, self.renderer.width, self.renderer.height, QtGui.QImage.Format_ARGB32) p = QtGui.QPainter(self) p.drawPixmap(QtCore.QPoint(0, 0), QtGui.QPixmap.fromImage(qImage)) # draw the zoom rectangle to the QPainter if self.drawRect: # JB draw dashed white line on solid black to get contrast p.setPen( QtGui.QPen( QtCore.Qt.black, 1, QtCore.Qt.SolidLine ) ) p.drawRect( self.rect[0], self.rect[1], self.rect[2], self.rect[3] ) p.setPen( QtGui.QPen( QtCore.Qt.white, 1, QtCore.Qt.DotLine ) ) p.drawRect( self.rect[0], self.rect[1], self.rect[2], self.rect[3] ) p.end() else: bbox = self.blitbox l, b, r, t = bbox.extents w = int(r) - int(l) h = int(t) - int(b) t = int(b) + h reg = self.copy_from_bbox(bbox) stringBuffer = reg.to_string_argb() qImage = QtGui.QImage(stringBuffer, w, h, QtGui.QImage.Format_ARGB32) pixmap = QtGui.QPixmap.fromImage(qImage) p = QtGui.QPainter( self ) p.drawPixmap(QtCore.QPoint(l, self.renderer.height-t), pixmap) p.end() self.blitbox = None self.drawRect = False
def scatter2d(X, y, fname): from matplotlib.figure import Figure from matplotlib.backends.backend_agg import FigureCanvasAgg f = Figure() ax = f.add_subplot(111) ax.scatter(X[:, 0], X[:, 1], c=y, s=2, edgecolors='none') ax.set_xlim(0, 1) ax.set_ylim(0, 1) canvas = FigureCanvasAgg(f) canvas.draw() f.savefig(fname)
def set_plot_state(self, extra_high=False, extra_low=False): """ Build image state that wx.html understand by plotting, putting it into wx.FileSystem image object : extrap_high,extra_low: low/high extrapolations are possible extra-plots """ # some imports import wx import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg # we use simple plot, not plotpanel # make matlab figure fig = plt.figure() fig.set_facecolor('w') graph = fig.add_subplot(111) # data plot graph.errorbar(self.data.x, self.data.y, yerr=self.data.dy, fmt='o') # low Q extrapolation fit plot if not extra_low == 'False': graph.plot(self.theory_lowQ.x, self.theory_lowQ.y) # high Q extrapolation fit plot if not extra_high == 'False': graph.plot(self.theory_highQ.x, self.theory_highQ.y) graph.set_xscale("log", nonposx='clip') graph.set_yscale("log", nonposy='clip') graph.set_xlabel('$\\rm{Q}(\\AA^{-1})$', fontsize=12) graph.set_ylabel('$\\rm{Intensity}(cm^{-1})$', fontsize=12) canvas = FigureCanvasAgg(fig) # actually make image canvas.draw() # make python.Image object # size w, h = canvas.get_width_height() # convert to wx.Image wximg = wx.EmptyImage(w, h) # wxim.SetData(img.convert('RGB').tostring() ) wximg.SetData(canvas.tostring_rgb()) # get the dynamic image for the htmlwindow wximgbmp = wx.BitmapFromImage(wximg) # store the image in wx.FileSystem Object wx.FileSystem.AddHandler(wx.MemoryFSHandler()) # use wx.MemoryFSHandler self.imgRAM = wx.MemoryFSHandler() # AddFile, image can be retrieved with 'memory:filename' self.imgRAM.AddFile('img_inv.png', wximgbmp, wx.BITMAP_TYPE_PNG) self.wximgbmp = 'memory:img_inv.png' self.image = fig
def time_basic_plot(): fig = Figure() canvas = FigureCanvas(fig) ax = WCSAxes(fig, [0.15, 0.15, 0.7, 0.7], wcs=MSX_WCS) fig.add_axes(ax) ax.set_xlim(-0.5, 148.5) ax.set_ylim(-0.5, 148.5) canvas.draw()
def test_plot_topomap_interactive(): """Test interactive topomap projection plotting.""" import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure evoked = read_evokeds(evoked_fname, baseline=(None, 0))[0] evoked.pick_types(meg='mag') evoked.info['projs'] = [] assert not evoked.proj evoked.add_proj(compute_proj_evoked(evoked, n_mag=1)) plt.close('all') fig = Figure() canvas = FigureCanvas(fig) ax = fig.gca() kwargs = dict(vmin=-240, vmax=240, times=[0.1], colorbar=False, axes=ax, res=8, time_unit='s') evoked.copy().plot_topomap(proj=False, **kwargs) canvas.draw() image_noproj = np.frombuffer(canvas.tostring_rgb(), dtype='uint8') assert len(plt.get_fignums()) == 1 ax.clear() evoked.copy().plot_topomap(proj=True, **kwargs) canvas.draw() image_proj = np.frombuffer(canvas.tostring_rgb(), dtype='uint8') assert not np.array_equal(image_noproj, image_proj) assert len(plt.get_fignums()) == 1 ax.clear() evoked.copy().plot_topomap(proj='interactive', **kwargs) canvas.draw() image_interactive = np.frombuffer(canvas.tostring_rgb(), dtype='uint8') assert_array_equal(image_noproj, image_interactive) assert not np.array_equal(image_proj, image_interactive) assert len(plt.get_fignums()) == 2 proj_fig = plt.figure(plt.get_fignums()[-1]) _fake_click(proj_fig, proj_fig.axes[0], [0.5, 0.5], xform='data') canvas.draw() image_interactive_click = np.frombuffer( canvas.tostring_rgb(), dtype='uint8') assert_array_equal(image_proj, image_interactive_click) assert not np.array_equal(image_noproj, image_interactive_click) _fake_click(proj_fig, proj_fig.axes[0], [0.5, 0.5], xform='data') canvas.draw() image_interactive_click = np.frombuffer( canvas.tostring_rgb(), dtype='uint8') assert_array_equal(image_noproj, image_interactive_click) assert not np.array_equal(image_proj, image_interactive_click)
def print_png(self, filename, *args, **kwargs): '''Call the widget function to make a png of the widget. ''' fig = FigureCanvasAgg(self.figure) FigureCanvasAgg.draw(fig) l, b, w, h = self.figure.bbox.bounds texture = Texture.create(size=(w, h)) texture.blit_buffer(bytes(fig.get_renderer().buffer_rgba()), colorfmt='rgba', bufferfmt='ubyte') texture.flip_vertical() img = Image(texture) img.save(filename)
def _render_figure(self, pixmap, width, height): if DEBUG: print 'FigureCanvasGTKAgg.render_figure' FigureCanvasAgg.draw(self) if DEBUG: print 'FigureCanvasGTKAgg.render_figure pixmap', pixmap buf = self.buffer_rgba(0,0) ren = self.get_renderer() w = int(ren.width) h = int(ren.height) pixbuf = gtk.gdk.pixbuf_new_from_data( buf, gtk.gdk.COLORSPACE_RGB, True, 8, w, h, w*4) pixmap.draw_pixbuf(pixmap.new_gc(), pixbuf, 0, 0, 0, 0, w, h, gtk.gdk.RGB_DITHER_NONE, 0, 0) if DEBUG: print 'FigureCanvasGTKAgg.render_figure done'
def time_basic_plot(self): fig = Figure() canvas = FigureCanvas(fig) ax = WCSAxes(fig, [0.15, 0.15, 0.7, 0.7], wcs=WCS(self.msx_header)) fig.add_axes(ax) ax.set_xlim(-0.5, 148.5) ax.set_ylim(-0.5, 148.5) canvas.draw()
def draw_plot_to_file(file_name, *args, **kwds): """ For offline plotting, which turns out to be necessary when running the status plot through pyana. """ import matplotlib import matplotlib.figure from matplotlib.backends.backend_agg import FigureCanvasAgg figure = matplotlib.figure.Figure((16, 10)) canvas = FigureCanvasAgg(figure) draw_plot(figure, *args, **kwds) canvas.draw() figure.savefig(file_name, format="png")
def time_basic_plot_with_grid(): fig = Figure() canvas = FigureCanvas(fig) ax = WCSAxes(fig, [0.15, 0.15, 0.7, 0.7], wcs=MSX_WCS) fig.add_axes(ax) ax.grid(color='red', alpha=0.5, linestyle='solid') ax.set_xlim(-0.5, 148.5) ax.set_ylim(-0.5, 148.5) canvas.draw()
def mplfig_to_npimage(fig): """ Converts a matplotlib figure to a RGB frame after updating the canvas""" # only the Agg backend now supports the tostring_rgb function from matplotlib.backends.backend_agg import FigureCanvasAgg canvas = FigureCanvasAgg(fig) canvas.draw() # update/draw the elements # get the width and the height to resize the matrix l,b,w,h = canvas.figure.bbox.bounds w, h = int(w), int(h) # exports the canvas to a string buffer and then to a numpy nd.array buf = canvas.tostring_rgb() image= np.fromstring(buf,dtype=np.uint8) return image.reshape(h,w,3)
def time_contourf_with_transform(): fig = Figure() canvas = FigureCanvas(fig) ax = WCSAxes(fig, [0.15, 0.15, 0.7, 0.7], wcs=MSX_WCS) fig.add_axes(ax) ax.contourf(DATA, transform=ax.get_transform(TWOMASS_WCS)) # The limits are to make sure the contours are in the middle of the result ax.set_xlim(32.5, 150.5) ax.set_ylim(-64.5, 64.5) canvas.draw()
def plot_gp(X, y, nstd, lscale, order, n_azi_buckets, param_var, cv_dir): basisfns, b, B = setup_azi_basisfns(order, n_azi_buckets, param_var) sgp = SparseGP(X=X, y=y, basisfns = basisfns, param_mean=b, param_cov=B, hyperparams=[nstd, 1.0, lscale, lscale], sta="FITZ", dfn_str="lld", wfn_str=wfn_str) sgp.save_trained_model(os.path.join(cv_dir, "fitz%d_gp%d_%d.sgp" % (lscale, order, n_azi_buckets))) from matplotlib.figure import Figure from matplotlib.backends.backend_agg import FigureCanvasAgg f = Figure() ax = f.add_subplot(111) plot_distance(X, y, sgp, ax, nstd) ax.set_xlim([1900, 10000]) ax.set_ylim([-2, 4]) canvas = FigureCanvasAgg(f) canvas.draw() f.savefig(os.path.join(cv_dir, "gp%d_%d_%d.png" % (lscale, order, n_azi_buckets)), bbox_inches='tight')
def set_widget_value(cls,widget,x,vdb=None, addr=None): out = StringIO() img_dpi=72 width=400 height=300 f=pylab.figure(dpi=img_dpi, figsize=(width/img_dpi, height/img_dpi)) a=f.gca() a.bar(range(len(x)),x,0.5) out=StringIO() canvas = FigureCanvasAgg(f) canvas.draw() size = (int(canvas.figure.get_figwidth())*img_dpi, int(canvas.figure.get_figheight())*img_dpi) buf=canvas.tostring_rgb() im=PIL.Image.fromstring('RGB', size, buf, 'raw', 'RGB', 0, 1) x=PIL2NumPy(im) widget.f(x)
def paintEvent(self, e): """ Draw to the Agg backend and then copy the image to the qt.drawable. In Qt, all drawing should be done inside of here when a widget is shown onscreen. """ p = QtGui.QPainter( self ) # only replot data when needed if type(self.replot) is bool: # might be a bbox for blitting if (self.replot ): #stringBuffer = str( self.buffer_rgba(0,0) ) FigureCanvasAgg.draw( self ) # matplotlib is in rgba byte order. # qImage wants to put the bytes into argb format and # is in a 4 byte unsigned int. little endian system is LSB first # and expects the bytes in reverse order (bgra). if ( QtCore.QSysInfo.ByteOrder == QtCore.QSysInfo.LittleEndian ): stringBuffer = self.renderer._renderer.tostring_bgra() else: stringBuffer = self.renderer._renderer.tostring_argb() qImage = QtGui.QImage(stringBuffer, self.renderer.width, self.renderer.height, QtGui.QImage.Format_ARGB32) self.pixmap = self.pixmap.fromImage(qImage) p.drawPixmap(QtCore.QPoint(0, 0), self.pixmap) # draw the zoom rectangle to the QPainter if (self.drawRect): p.setPen(QtGui.QPen(Qt.black, 1, Qt.DotLine)) p.drawRect(self.rect[0], self.rect[1], self.rect[2], self.rect[3]) # we are blitting here else: bbox = self.replot w, h = int(bbox.width), int(bbox.height) l, t = bbox.ll().x().get(), bbox.ur().y().get() reg = self.copy_from_bbox(bbox) stringBuffer = reg.to_string() qImage = QtGui.QImage(stringBuffer, w, h, QtGui.QImage.Format_ARGB32) self.pixmap = self.pixmap.fromImage(qImage) p.drawPixmap(QtCore.QPoint(l, self.renderer.height-t), self.pixmap) p.end() self.replot = False self.drawRect = False
def plot_matrix(M, filename): # def add_alpha(M_img): # M_img[:, :, 3] = ( (3.9215686 - np.sum(M_img, axis=2)) > 0 ) * 1.0 M = M / scipy.stats.scoreatpercentile(M, 99.9) M_img = cm.Purples(M) # add_alpha(M_img) f = Figure((11, 8)) ax = f.add_subplot(111) ax.imshow(M_img) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) canvas = FigureCanvasAgg(f) canvas.draw() f.savefig(filename, bbox_inches="tight", dpi=300, transparent=True)
def _animate(self, frames, func, **animArgs): if self._outputParam is None: raise NotImplemented images = [] figure = plt.Figure(figsize=(6,6), dpi=300, facecolor='w') axes = figure.add_subplot(111) canvas = FigureCanvasAgg(figure) for frame in frames: axes.clear() func(frame, axes) canvas.draw() image = np.fromstring(canvas.tostring_rgb(), dtype=np.uint8) image.shape = canvas.get_width_height() + (3,) images.append(image) self._render(images)
def time_basic_plot_with_grid_and_overlay(): fig = Figure() canvas = FigureCanvas(fig) ax = WCSAxes(fig, [0.15, 0.15, 0.7, 0.7], wcs=MSX_WCS) fig.add_axes(ax) ax.grid(color='red', alpha=0.5, linestyle='solid') ax.set_xlim(-0.5, 148.5) ax.set_ylim(-0.5, 148.5) overlay = ax.get_coords_overlay('fk5') overlay.grid(color='purple', ls='dotted') canvas.draw()
def plt_plot_filters(x, y, shape, rows, cols, selected_unit=None, selected_unit_color=None, title=None, x_axis_label=None, y_axis_label=None, share_axes=True, log_scale=0): shape = (ceil(shape[1] / 80), ceil(shape[0] / 80)) fig = Figure(figsize=shape) canvas = FigureCanvas(fig) ax, highlighted_ax, right_ax, bottom_ax, curr, right, bottom = None, None, None, None, None, None, None if selected_unit is not None: row = selected_unit / cols col = selected_unit % cols curr = selected_unit bottom = (selected_unit + cols) if row < rows - 1 else None right = (selected_unit + 1) if col < cols - 1 else None for i in xrange(x.shape[0]): if share_axes: ax = fig.add_subplot(rows, cols, (i + 1), axisbelow=False, sharex=ax, sharey=ax) else: ax = fig.add_subplot(rows, cols, (i + 1), axisbelow=False) if y is not None: if log_scale == 1: ax.semilogy(y, x[i], linewidth=1) else: ax.plot(y, x[i], linewidth=1) else: if log_scale == 1: ax.semilogy(x[i], linewidth=1) else: ax.plot(x[i], linewidth=1) ax.set_xlim(left=0, right=x.shape[1] - 1) ax.get_xaxis().set_visible(i >= ((rows - 1) * cols)) ax.get_yaxis().set_visible(i % cols == 0) if i == curr: highlighted_ax = ax if i == bottom: bottom_ax = ax if i == right: right_ax = ax if x_axis_label is not None: ax.set_xlabel(x_axis_label) if y_axis_label is not None: ax.set_ylabel(y_axis_label) if highlighted_ax is not None: for axis in ['top', 'bottom', 'left', 'right']: highlighted_ax.spines[axis].set_linewidth(2.5) highlighted_ax.spines[axis].set_color(selected_unit_color) if bottom_ax is not None: bottom_ax.spines['top'].set_linewidth(2) bottom_ax.spines['top'].set_color(selected_unit_color) if right_ax is not None: right_ax.spines['left'].set_linewidth(2) right_ax.spines['left'].set_color(selected_unit_color) if title is not None: fig.suptitle(title) fig.tight_layout() fig.subplots_adjust(hspace=0, wspace=0) canvas.draw() # draw the canvas, cache the renderer l, b, w, h = fig.bbox.bounds w, h = int(w), int(h) im = fromstring(canvas.tostring_rgb(), dtype='uint8') im.shape = h, w, 3 return im
def render(self): try: if self.data==None or len(self.data)==0: return if self.xlimits==None or self.ylimits==None: return except: return render = False if self.needupd or not self.plotimg: self.needupd=0 if self.main_display_list != 0: glDeleteLists(self.main_display_list,1) self.main_display_list = 0 if self.main_display_list == 0: self.main_display_list = glGenLists(1) glNewList(self.main_display_list,GL_COMPILE) render = True lighting = glIsEnabled(GL_LIGHTING) glDisable(GL_LIGHTING) EMShape.font_renderer=self.font_renderer # Important ! Each window has to have its own font_renderer. Only one context active at a time, so this is ok. GL.glPushMatrix() # overcome depth issues glTranslate(0,0,5) for k,s in list(self.shapes.items()): s.draw(self.scr2plot) GL.glPopMatrix() if render: fig=Figure((old_div(self.width(),72.0),old_div(self.height(),72.0)),dpi=72.0) ax=fig.add_axes((.1,.1,.88,.88),autoscale_on=False,xlim=self.xlimits,ylim=self.ylimits,xscale=self.axisparms[2],yscale=self.axisparms[3]) #if self.axisparms[0] and len(self.axisparms[0])>0 : ax.set_xlabel(self.axisparms[0],size="xx-large") #if self.axisparms[1] and len(self.axisparms[1])>0 : ax.set_ylabel(self.axisparms[1],size="xx-large") ax.tick_params(axis='x', labelsize="x-large") ax.tick_params(axis='y', labelsize="x-large") canvas=FigureCanvasAgg(fig) if self.inspector == None: self.inspector = self.get_inspector() #tostack = [] usedkeys = [] #colors = [] if self.alignment == "center": histalign = "mid" elif self.alignment == "edge": histalign = "left" for k in list(self.axes.keys()): if not self.visibility[k]: continue dcurr = self.data[k][self.axes[k][0]] color = colortypes[self.pparm[k][0]] alpha = self.pparm[k][1] rwidth = self.pparm[k][2] self.bins[k],self.edges = np.histogram(dcurr,self.nbins,range=self.xlimits,density=self.normed) width = (self.edges[1]-self.edges[0])*rwidth if self.cumulative: self.bins[k] = np.cumsum(self.bins[k]) if self.normed: self.bins[k] /= np.sum(self.bins[k]) self.bins[k] /= len(list(self.axes.keys())) if self.histtype == "bar": if self.stacked and len(usedkeys) > 0: bottom = self.getTotals(keys=usedkeys) ax.bar(self.edges[:-1],self.bins[k], width, color=color,bottom=bottom, align=self.alignment, log=self.logy, orientation=self.orientation, alpha=alpha) else: ax.bar(self.edges[:-1],self.bins[k], width, color=color, align=self.alignment, log=self.logy, orientation=self.orientation, alpha=alpha) usedkeys.append(k) elif self.histtype == "step" or self.histtype == "stepfilled": if self.stacked == False: ax.hist(self.bins[k],bins=self.edges,color=None,range=self.xlimits,histtype=self.histtype, align=histalign, orientation=self.orientation,alpha=self.inspector.alpha.getValue(),normed=self.normed,cumulative=self.cumulative,log=self.logy,stacked=self.stacked) else: tostack.append(self.bins[k]) colors.append(color) if self.histtype == "step" or self.histtype == "stepfilled": if self.stacked == True: ax.hist(tostack,bins=self.edges,color=colors,range=self.xlimits,histtype=self.histtype,orientation=self.orientation,align=histalign,alpha=self.inspector.alpha.getValue(),normed=self.normed,cumulative=self.cumulative,log=self.logy,stacked=self.stacked) self.autoscale(True) ax.set_ylim(self.ylimits) canvas.draw() self.plotimg = canvas.tostring_rgb() # save this and convert to bitmap as needed # this try except block is because the developers of matplotlib have been changing their API try: # this would work for matplotlib 0.98 self.scrlim=(ax.get_window_extent().xmin,ax.get_window_extent().ymin,ax.get_window_extent().xmax-ax.get_window_extent().xmin,ax.get_window_extent().ymax-ax.get_window_extent().ymin) except: try: # this should work for matplotlib 0.91 self.scrlim=(ax.get_window_extent().xmin(),ax.get_window_extent().ymin(),ax.get_window_extent().xmax()-ax.get_window_extent().xmin(),ax.get_window_extent().ymax()-ax.get_window_extent().ymin()) except: print('there is a problem with your matplotlib') return self.plotlim=(ax.get_xlim()[0],ax.get_ylim()[0],ax.get_xlim()[1]-ax.get_xlim()[0],ax.get_ylim()[1]-ax.get_ylim()[0]) if not self.glflags.npt_textures_unsupported(): self.__texture_plot(self.plotimg) else: GL.glRasterPos(0,self.height()-1) GL.glPixelZoom(1.0,-1.0) # print "paint ",self.width(),self.height(), self.width()*self.height(),len(a) GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT,1) GL.glDrawPixels(self.width(),self.height(),GL.GL_RGB,GL.GL_UNSIGNED_BYTE,self.plotimg) else: try: glCallList(self.main_display_list) except: pass if render : glEndList() try: glCallList(self.main_display_list) except: pass if lighting : glEnable(GL_LIGHTING)
def initialize(self, ref=None): if ref == None: ref = 0 self.ref = ref FigureCanvasAgg.draw(self.canvas) self._save_background()
def get_RGB(figure): canvas = FigureCanvas(figure) width, height = figure.get_size_inches() * figure.get_dpi() canvas.draw() # draw the canvas, cache the renderer image = np.frombuffer(canvas.tostring_rgb(), dtype='uint8').reshape(int(height), int(width), 3) return image
def visualize(self, map, traj_info_dict, save_file='', max_traj=None, annotate_starting_loc=True, figsize=None, limits=None, xticks=None, yticks=None): """ :param max_traj: only draw trajecies with indices <= max_traj """ fig = plt.Figure(figsize=figsize) ax = fig.add_subplot(111) canvas = FigureCanvas(fig) canvas.draw() vis = sim_renderer.SimRenderer(map, ax, canvas) vis.render(True) vis.render(True) g = self.graph vis.clear() vis.render(True) for (u, v), data in g.edges.items(): if max_traj is not None: if u[0] > max_traj or v[0] > max_traj: continue sample_u = traj_info_dict[u[0]]['samples'][u[1]] sample_v = traj_info_dict[v[0]]['samples'][v[1]] pos_u = sample_u['pos'] pos_v = sample_v['pos'] vis.plotter.fixed_arrow2( 'edge %d %d %d %d' % (u[0], u[1], v[0], v[1]), pos_u[0], pos_u[1], pos_v[0], pos_v[1], **self._get_vis_style(u, v)) if annotate_starting_loc: for traj_idx, traj in self.trajs.items(): start_node = traj[0] sample = traj_info_dict[start_node[0]]['samples'][ start_node[1]] x, y = sample['pos'] vis.plotter.text('traj %d' % traj_idx, x, y, '%d' % traj_idx, fontsize=12, bbox={ 'facecolor': 'white', 'alpha': 0.5, 'pad': 10 }) if limits is not None: ax.set_xlim(limits[0], limits[1]) ax.set_ylim(limits[2], limits[3]) if xticks is not None: ax.set_xticks(xticks) if yticks is not None: ax.set_yticks(yticks) if save_file != '': fig.tight_layout(pad=0.1) fig.savefig(save_file) return vis.get_image()
def pipefinder(image_path,image_save): import laspy import scipy import numpy as np import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas import cv2 import warnings import pandas as pd from sklearn.cluster import DBSCAN from sklearn import metrics from sklearn import preprocessing from mpl_toolkits.mplot3d import Axes3D from matplotlib import path import os.path warnings.filterwarnings('ignore') inFile = laspy.file.File(image_path) dataset = np.vstack([inFile.x, inFile.y, inFile.z, inFile.red, inFile.green, inFile.blue]).transpose() max1 = dataset[:,0].max() min1 = dataset[:,0].min() max2 = dataset[:,1].max() min2 = dataset[:,1].min() max3 = dataset[:,2].max() min3 = dataset[:,2].min() height = max3-min3 color = dataset[:,3:6]/65535 dataset1 = dataset[:,0:3] # This is required for remove ground points dataset2 = dataset1[dataset1[:,2] > min3 + 0.4 * height] color2 = color[dataset1[:,2] > min3 + 0.4 * height] dataset1 = dataset2 color = color2 # Do DBSCAN clustering clustering = DBSCAN(eps=0.1, min_samples=5, leaf_size=10).fit(dataset1) core_samples_mask = np.zeros_like(clustering.labels_, dtype=bool) core_samples_mask[clustering.core_sample_indices_] = True labels = clustering.labels_ # Number of clusters in labels, ignoring noise if present. n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0) n_noise_ = list(labels).count(-1) print('Estimated number of clusters: %d' % n_clusters_) print('Estimated number of noise points: %d' % n_noise_) unique_labels = set(labels) classfication = [] for k in unique_labels: class_member_mask = (labels == k) xyz = dataset1[class_member_mask & core_samples_mask] classfication.append(len(xyz)) top_class = [classfication.index(x) for x in classfication if x>=0.1 * max(classfication)] print(top_class) top_number = len(top_class) # OpenCV to compare image similarity image_tot = [[0]]*len(top_class) fig = [[0]]*len(top_class) warnings.simplefilter("ignore", DeprecationWarning) #for k, col in zip(unique_labels, colors) for i in range(0,top_number): fig[i] = plt.figure(figsize=[20, 10]) ax = fig[i].add_subplot(111) canvas = FigureCanvas(fig[i]) k = top_class[i] class_member_mask = (labels == k) #print(k,class_member_mask) if k in top_class: xyz = dataset1[class_member_mask & core_samples_mask] XX = xyz[:,0] YY = xyz[:,1] ZZ = xyz[:,2] YY2D = YY ax.scatter(XX, YY2D, c='k', s=3) # Turn off tick labels ax.set_yticklabels([]) ax.set_xticklabels([]) ax.axis('off') plt.close() canvas.draw() image = np.fromstring(fig[i].canvas.tostring_rgb(), dtype=np.uint8, sep='') image = image.reshape(fig[i].canvas.get_width_height()[::-1] + (3,)) image_tot[i] = cv2.cvtColor(image,cv2.COLOR_RGB2BGR) path_sample = '../../Hybird_data_codes/pipe_images/pipe07.2.png' img_sample = cv2.imread(path_sample, cv2.IMREAD_GRAYSCALE) #pipe_index = [] score_max = 0.0 pipe_index = 0 for i in range(0,len(top_class)): img_compare = image_tot[i] orb = cv2.ORB_create() kp_a, desc_a = orb.detectAndCompute(img_sample, None) kp_b, desc_b = orb.detectAndCompute(img_compare, None) # initialize the bruteforce matcher bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # match.distance is a float between {0:100} - lower means more similar matches = bf.match(desc_a, desc_b) similar_regions = [i for i in matches if i.distance < 35] if len(matches) == 0: print(0) score = len(similar_regions)/len(matches) if (score > score_max): pipe_index = top_class[i] score_max = score fig = plt.figure() ax = fig.add_subplot(111, projection='3d') colors = [plt.cm.Spectral(each) for each in np.linspace(0.3, 1, top_number)] for i,col in zip(range(0,top_number),colors): k = top_class[i] #print(k,col) class_member_mask = (labels == k) #col_pipe = color[class_member_mask & core_samples_mask] if k == pipe_index: xyz = dataset1[class_member_mask & core_samples_mask] col_pipe = color[class_member_mask & core_samples_mask] print('pipe data points:', len(xyz)) ax.scatter(xyz[:, 0], xyz[:, 1], xyz[:, 2], c=col_pipe, marker=".") pipe_data = xyz pipe_csv = np.concatenate((pipe_data, col_pipe * 255), axis=1) pipe_csv.shape pd.DataFrame(pipe_csv).to_csv(image_save) image_save_short = os.path.basename(image_save) #return "Nice Job! 3D pipe(s) has been extracted and saved as " + image_save_short
def render_state(locations, figsize=(32, 32), ball_radius=0.08, box_size=1., n_children=None, fixed_ball_radius=False, render_only_last_level=False, edges=None, fixed_sides=0, curtain=False): """Renders the state of the environment from its locations (T, 2, K).""" images = [] BG_COLOR = 'black' BALL_COLORS = ['black', 'blue', '#00ff00', 'red', 'white', 'cyan', 'magenta', 'yellow'] BALL_COLORS.remove(BG_COLOR) n_total = locations.shape[-1] # handle the hierarchy nl = 0 nn = [locations.shape[-1]] if n_children is not None: if '-' in n_children: nc = list(map(int, n_children.split('-'))) nl = len(nc) # number of nodes in each level of the graph that have children nn = [1] # root node for l in range(1, nl + 1): nn.append(nn[-1] * nc[l - 1]) sides = [] for l in range(nl + 1): for n in range(nn[l]): sample = np.random.sample() if fixed_sides < 9: # 9 means sample a random shape sides.append(fixed_sides) elif sample < 1/3: sides.append(0) elif sample < 2/3: sides.append(3) else: sides.append(4) ch, cw = 12, 12 xc = np.random.randint(5, 32-cw-5) yc = np.random.randint(5, 32-ch-5) for i in range(locations.shape[0]): loc = locations[i] fig = Figure(figsize=(figsize[0] / 100, figsize[1] / 100)) fig.patch.set_facecolor(BG_COLOR) canvas = FigureCanvas(fig) ax = fig.gca() ax.xaxis.set_major_locator(NullLocator()) ax.yaxis.set_major_locator(NullLocator()) # for p in range(locations.shape[-1]): p = 0 br = ball_radius for l in range(nl + 1): for n in range(nn[l]): x_pos = (loc[0, p] + box_size) / (2 * box_size) y_pos = (loc[1, p] + box_size) / (2 * box_size) if nl == 0: cc = BALL_COLORS[p % len(BALL_COLORS)] else: cc = BALL_COLORS[p % len(BALL_COLORS)] if not render_only_last_level and nl > 0: if l == 0: cc = 'gold' elif l == 1: cc = 'silver' def get_polygon_coords(xc, yc, edge_len=0.10, sides=3): L = edge_len if sides == 3: L *= 2 N = sides R = L / (2 * np.sin(np.pi / N)) xy = np.ndarray((N, 2), dtype=float) for i in range(N): xy[i, 0] = xc + R * np.cos(np.pi / N * (1 + 2 * i)) xy[i, 1] = yc + R * np.sin(np.pi / N * (1 + 2 * i)) return xy if not (l < nl and render_only_last_level): if sides[p] == 0: particle = plt.Circle((x_pos, y_pos), br, color=cc, clip_on=False) else: xy = get_polygon_coords(x_pos, y_pos, edge_len=br*2, sides=sides[p]) particle = plt.Polygon(xy, color=cc, fill=True, clip_on=False) ax.add_artist(particle) if edges is not None and not render_only_last_level and p < n_total-1: edg = edges[i] for pp in range(p, n_total-1): if edg[p * (n_total - 1) + pp]: x1 = (loc[0, p] + box_size) / (2 * box_size) x2 = (loc[0, pp+1] + box_size) / (2 * box_size) y1 = (loc[1, p] + box_size) / (2 * box_size) y2 = (loc[1, pp+1] + box_size) / (2 * box_size) llll = mlines.Line2D([x1, x2], [y1, y2], color='white', linewidth=1.4, linestyle=':' # '-', '--', '-.', ':', '' ) ax.add_artist(llll) p += 1 if not fixed_ball_radius: br /= 2 ax.axis('off') # Draw image canvas.draw() # Convert to numpy array flat_image = np.frombuffer(canvas.tostring_rgb(), dtype='uint8') image = flat_image.reshape(fig.canvas.get_width_height()[::-1] + (3,)) if curtain: im = image.copy() im[yc:yc+ch, xc:xc+cw, :] = 0 image = im images.append(image) plt.close(fig) return np.array(images)
def main(): blockSize = 40 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") wtgs = torch.load('TimeSeriesLearningSkip_best_loss.pth.tar') model = TimeSeriesLearningSkip() model.load_state_dict(wtgs['state_dict']) model.to(device) frameQt = 28001 vcap = cv2.VideoCapture("clipe2.mp4") #width = vcap.get(cv2.CAP_PROP_FRAME_WIDTH) # float #height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT) # float fourcc = cv2.VideoWriter_fourcc('F', 'M', 'P', '4') face_cascade = cv2.CascadeClassifier( 'cascadeFolder/haarcascade_frontalface_default.xml') rois = [] colors = {} face_expressions = {} font = cv2.FONT_HERSHEY_SIMPLEX savedFrame = [] frameNum = 0 finalArousal = [[0] * frameQt, [0] * frameQt] finalValence = [[0] * frameQt, [0] * frameQt] with torch.no_grad(): model.eval() while (vcap.isOpened()): print('Examining frame %d' % frameNum) sucs, imgv = vcap.read() if sucs: imgv = cv2.resize(imgv, (640, 480)) faces = detectFace(face_cascade, imgv) newROIs = isNewROI(rois, faces) for (x, y, w, h) in newROIs: rois.append((x, y, w, h)) roins = findROI(rois, faces) for rncls in set(roins): if rncls not in colors.keys(): colors[rncls] = getRandomColors() savedFrame.append([imgv, {}]) for fnum, b in enumerate(faces): if fnum >= len(roins): continue cv2.rectangle(imgv, (b[0], b[1]), (b[0] + b[2], b[1] + b[3]), colors[roins[fnum]], 2) savedFrame[-1][1][roins[fnum]] = (b[0], b[1] + b[3] + 12) #print("Extraindo face %d" % (roins[fnum])) fImage = imgv[b[1]:b[1] + b[3], b[0]:b[0] + b[2]] fImage = cv2.resize(fImage, (100, 100)) - 128 fImage = fImage / 128 if roins[fnum] not in face_expressions.keys(): face_expressions[roins[fnum]] = [[], []] face_expressions[roins[fnum]][0].append(fImage) face_expressions[roins[fnum]][1].append(frameNum) #cv2.imwrite(os.path.join('frames_face',"roi_" + str(roins[fnum]) + "_frame_"+str(frame_number)+".jpg"),fImage) erase = [] for dface in face_expressions: if len(face_expressions[dface][0]) == blockSize: erase.append(dface) a, v = getArousalValence(face_expressions[dface][0], model) for idx, frame in enumerate( face_expressions[dface][1]): finalValence[dface][idx + (frameNum - blockSize)] = v[idx] finalArousal[dface][idx + (frameNum - blockSize)] = a[idx] cv2.putText(savedFrame[frame][0], getResult(a[idx], v[idx]), savedFrame[frame][1][dface], font, 0.5, colors[dface], 2, cv2.LINE_AA) cv2.putText(savedFrame[frame][0], "arousal: %f" % a[idx], (savedFrame[frame][1][dface][0], savedFrame[frame][1][dface][1] + 20), font, 0.5, colors[dface], 2, cv2.LINE_AA) cv2.putText(savedFrame[frame][0], "Valence: %f" % v[idx], (savedFrame[frame][1][dface][0], savedFrame[frame][1][dface][1] + 35), font, 0.5, colors[dface], 2, cv2.LINE_AA) elif len(face_expressions[dface][0]) > blockSize: erase.append(dface) for d in erase: del face_expressions[d] frameNum += 1 if frameNum == frameQt: break else: break print('Outputing video') flatten = lambda t: [item for sublist in t for item in sublist] tks = np.arange( min([min(flatten(finalValence)), min(flatten(finalArousal))]), max([max(flatten(finalValence)), max(flatten(finalArousal))]), 0.05) fig, ax = plt.subplots(2, 2) canvas = FigureCanvas(fig) #ax = fig.gca() ax[0][0].set_yticks(tks) ax[0][1].set_yticks(tks) ax[1][0].set_yticks(tks) ax[1][1].set_yticks(tks) width, height = fig.get_size_inches() * fig.get_dpi() out = cv2.VideoWriter('output2.avi', fourcc, 20.0, (int(width), int(height) * 2)) for idxF, frm in enumerate(savedFrame): ax[0][0].set(xlabel='', ylabel='Valence') ax[1][0].set(xlabel='Frame', ylabel='Arousal') ax[0][0].plot([fnx for fnx in range(frameNum)], finalValence[0][:frameNum]) ax[0][0].plot([idxF, idxF], [tks.min(), tks.max()]) ax[1][0].plot([fnx for fnx in range(frameNum)], finalArousal[0][:frameNum]) ax[1][0].plot([idxF, idxF], [tks.min(), tks.max()]) ax[0][1].set(xlabel='', ylabel='') ax[1][1].set(xlabel='Frame', ylabel='') ax[0][1].plot([fnx for fnx in range(frameNum)], finalValence[1][:frameNum]) ax[0][1].plot([idxF, idxF], [tks.min(), tks.max()]) ax[1][1].plot([fnx for fnx in range(frameNum)], finalArousal[1][:frameNum]) ax[1][1].plot([idxF, idxF], [tks.min(), tks.max()]) canvas.draw() im = np.fromstring(canvas.tostring_rgb(), dtype='uint8').reshape(int(height), int(width), 3) nconc = np.concatenate((frm[0], im)) out.write(nconc) ax[0][0].clear() ax[0][1].clear() ax[1][0].clear() ax[1][1].clear() vcap.release() out.release() cv2.destroyAllWindows()
def create_dataset(n_sample=60000): """Creates toy dataset and save to disk.""" concept = np.reshape(np.random.randint(2, size=15 * n_sample), (-1, 15)).astype(np.bool_) concept[:15, :15] = np.eye(15) fig = Figure(figsize=(3, 3)) canvas = FigureCanvas(fig) axes = fig.gca() axes.set_xlim([0, 10]) axes.set_ylim([0, 10]) axes.axis('off') width, height = fig.get_size_inches() * fig.get_dpi() width = int(width) height = int(height) location = [(1.3, 1.3), (3.3, 1.3), (5.3, 1.3), (7.3, 1.3), (1.3, 3.3), (3.3, 3.3), (5.3, 3.3), (7.3, 3.3), (1.3, 5.3), (3.3, 5.3), (5.3, 5.3), (7.3, 5.3), (1.3, 7.3), (3.3, 7.3), (5.3, 7.3)] location_bool = np.zeros(15) x = np.zeros((n_sample, width, height, 3)) color_array = ['green', 'red', 'blue', 'black', 'orange', 'purple', 'yellow'] for i in range(n_sample): if i % 1000 == 0: print('{} images are created'.format(i)) if concept[i, 5] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], 'x', color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) if concept[i, 6] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], '3', color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) if concept[i, 7] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], 's', color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) if concept[i, 8] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], 'p', color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) if concept[i, 9] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], '_', color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) if concept[i, 10] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], 'd', color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) if concept[i, 11] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], 'd', color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) if concept[i, 12] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], 11, color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) if concept[i, 13] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], 'o', color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) if concept[i, 14] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], '.', color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) if concept[i, 0] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], '+', color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) if concept[i, 1] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], '1', color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) if concept[i, 2] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], '*', color=color_array[np.random.randint(100) % 7], markersize=30, mew=3, ms=5) if concept[i, 3] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], '<', color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) if concept[i, 4] == 1: a = np.random.randint(15) while location_bool[a] == 1: a = np.random.randint(15) location_bool[a] = 1 axes.plot( location[a][0], location[a][1], 'h', color=color_array[np.random.randint(100) % 7], markersize=20, mew=4, ms=8) canvas.draw() image = np.fromstring( canvas.tostring_rgb(), dtype='uint8').reshape(width, height, 3) x[i, :, :, :] = image # imgplot = plt.imshow(image) # plt.show() # create label by booling functions y = np.zeros((n_sample, 15)) y[:, 0] = ((1 - concept[:, 0] * concept[:, 2]) + concept[:, 3]) > 0 y[:, 1] = concept[:, 1] + (concept[:, 2] * concept[:, 3]) y[:, 2] = (concept[:, 3] * concept[:, 4]) + (concept[:, 1] * concept[:, 2]) y[:, 3] = np.bitwise_xor(concept[:, 0], concept[:, 1]) y[:, 4] = concept[:, 1] + concept[:, 4] y[:, 5] = (1 - (concept[:, 0] + concept[:, 3] + concept[:, 4])) > 0 y[:, 6] = np.bitwise_xor(concept[:, 1] * concept[:, 2], concept[:, 4]) y[:, 7] = concept[:, 0] * concept[:, 4] + concept[:, 1] y[:, 8] = concept[:, 2] y[:, 9] = np.bitwise_xor(concept[:, 0] + concept[:, 1], concept[:, 3]) y[:, 10] = (1 - (concept[:, 2] + concept[:, 4])) > 0 y[:, 11] = concept[:, 0] + concept[:, 3] + concept[:, 4] y[:, 12] = np.bitwise_xor(concept[:, 1], concept[:, 2]) y[:, 13] = (1 - (concept[:, 0] * concept[:, 4] + concept[:, 3])) > 0 y[:, 14] = np.bitwise_xor(concept[:, 4], concept[:, 3]) np.save('x_data.npy', x) np.save('y_data.npy', y) np.save('concept_data.npy', concept) return width, height
class Graph(object): def __init__(self, *args, **kw): super(Graph, self).__init__(*args, **kw) def layoutFigure(self, legend): prefs = self.prefs # Get the main Figure object # self.figure = Figure() figure = self.figure self.canvas = FigureCanvasAgg(figure) dpi = prefs["dpi"] width = float(prefs["width"]) height = float(prefs["height"]) width_inch = width / dpi height_inch = height / dpi figure.set_size_inches(width_inch, height_inch) figure.set_dpi(dpi) figure.set_facecolor(prefs.get("background_color", "white")) figure_padding = float(prefs["figure_padding"]) figure_left_padding = float( prefs.get("figure_left_padding", figure_padding)) figure_right_padding = float( prefs.get("figure_right_padding", figure_padding)) figure_top_padding = float( prefs.get("figure_top_padding", figure_padding)) figure_bottom_padding = float( prefs.get("figure_bottom_padding", figure_padding)) text_size = prefs.get("text_size", 8) text_padding = prefs.get("text_padding", 5) ####################################### # Make the graph title title = prefs.get("title", "") subtitle = "" title_size = 0 title_padding = 0 if title: title_size = prefs.get("title_size", 1.5 * text_size) title_padding = float( prefs.get("title_padding", 1.5 * text_padding)) figure.text( 0.5, 1.0 - (title_size + figure_padding) / height, title, ha="center", va="bottom", size=pixelToPoint(title_size, dpi), ) subtitle = prefs.get("subtitle", "") if subtitle: sublines = subtitle.split("\n") nsublines = len(sublines) subtitle_size = prefs.get("subtitle_size", 1.2 * text_size) subtitle_padding = float( prefs.get("subtitle_padding", 1.2 * text_padding)) top_offset = subtitle_size + subtitle_padding + title_size + figure_padding for subline in sublines: figure.text( 0.5, 1.0 - (top_offset) / height, subline, ha="center", va="bottom", size=pixelToPoint(subtitle_size, dpi), fontstyle="italic", ) top_offset += subtitle_size + subtitle_padding ######################################## # Evaluate the plot area dimensions graph_width = width - figure_left_padding - figure_right_padding graph_height = height - figure_top_padding - figure_bottom_padding if title: graph_height = graph_height - title_padding - title_size if subtitle: graph_height = graph_height - nsublines * (subtitle_size + subtitle_padding) graph_left = figure_left_padding graph_bottom = figure_bottom_padding ######################################### # Make the plot time stamp if requested flag = prefs.get("graph_time_stamp", True) if flag: timeString = "Generated on " + datetime.datetime.utcnow().strftime( "%Y-%m-%d %H:%M:%S ") + "UTC" time_size = prefs["text_size"] * 0.8 figure.text(0.995, 0.005, timeString, ha="right", va="bottom", size=pixelToPoint(time_size, dpi), fontstyle="italic") ######################################### # Make the graph Legend if requested legend_flag = prefs["legend"] legend_ax = None column_width = legend.column_width if legend_flag: legend_position = prefs["legend_position"] # legend_width = float(prefs['legend_width']) # legend_height = float(prefs['legend_height']) legend_width, legend_height, legend_max_height = legend.getLegendSize( ) legend_padding = float(prefs["legend_padding"]) if legend_position in ["right", "left"]: # One column in case of vertical legend legend_width = column_width + legend_padding legend_height = min(graph_height, legend_max_height) bottom = (height - title_size - title_padding - legend_height) / 2.0 / height if legend_position == "right": left = 1.0 - (figure_padding + legend_width) / width else: left = figure_padding / width graph_left = graph_left + legend_width graph_width = graph_width - legend_width - legend_padding elif legend_position == "bottom": bottom = figure_padding / height left = (width - legend_width) / 2.0 / width graph_height = graph_height - legend_height - legend_padding graph_bottom = graph_bottom + legend_height + legend_padding legend_rect = (left, bottom, legend_width / width, legend_height / height) legend_ax = figure.add_axes(legend_rect) ########################################### # Make the plot spots plot_grid = prefs["plot_grid"] nx = int(plot_grid.split(":")[0]) ny = int(plot_grid.split(":")[1]) plot_axes = [] for j in range(ny - 1, -1, -1): for i in range(nx): plot_rect = ( (graph_left + graph_width * i / nx) / width, (graph_bottom + graph_height * j / ny) / height, graph_width / nx / width, graph_height / ny / height, ) plot_axes.append(figure.add_axes(plot_rect)) return legend_ax, plot_axes def makeTextGraph(self, text="Empty image"): """Make an empty text image""" self.figure = Figure() figure = self.figure self.canvas = FigureCanvasAgg(figure) prefs = self.prefs dpi = prefs["dpi"] width = float(prefs["width"]) height = float(prefs["height"]) width_inch = width / dpi height_inch = height / dpi figure.set_size_inches(width_inch, height_inch) figure.set_dpi(dpi) figure.set_facecolor("white") text_size = prefs.get("text_size", 12) figure.text(0.5, 0.5, text, horizontalalignment="center", size=pixelToPoint(text_size, dpi)) def makeGraph(self, data, *args, **kw): start = time.time() # Evaluate all the preferences self.prefs = evalPrefs(*args, **kw) prefs = self.prefs if DEBUG: print("makeGraph time 1", time.time() - start) start = time.time() if "text_image" in prefs: self.makeTextGraph(str(prefs["text_image"])) return # Evaluate the number of plots and their requested layout metadata = prefs.get("metadata", {}) plot_grid = prefs.get("plot_grid", "1:1") nx = int(plot_grid.split(":")[0]) ny = int(plot_grid.split(":")[1]) nPlots = nx * ny if nPlots == 1: if not isinstance(data, list): data = [data] if not isinstance(metadata, list): metadata = [metadata] else: if not isinstance(data, list): # return S_ERROR('Single data for multiplot graph') print("Single data for multiplot graph") return if not isinstance(metadata, list): metaList = [] for _ in range(nPlots): metaList.append(metadata) metadata = metaList # Initialize plot data graphData = [] plot_prefs = [] for i in range(nPlots): plot_prefs.append(evalPrefs(prefs, metadata[i])) gdata = GraphData(data[i]) if i == 0: plot_type = plot_prefs[i]["plot_type"] if "sort_labels" in plot_prefs[i]: reverse = plot_prefs[i].get("reverse_labels", False) gdata.sortLabels(plot_prefs[i]["sort_labels"], reverse_order=reverse) if "limit_labels" in plot_prefs[i]: if plot_prefs[i]["limit_labels"] > 0: gdata.truncateLabels(plot_prefs[i]["limit_labels"]) if "cumulate_data" in plot_prefs[i]: gdata.makeCumulativeGraph() plot_title = plot_prefs[i].get("plot_title", "") if plot_title != "NoTitle": begin = "" end = "" if "starttime" in plot_prefs[i] and "endtime" in plot_prefs[i]: begin = to_timestamp(plot_prefs[i]["starttime"]) end = to_timestamp(plot_prefs[i]["endtime"]) elif gdata.key_type == "time": begin = gdata.min_key end = gdata.max_key if begin and end: time_title = add_time_to_title(begin, end) if plot_title: plot_title += ":" plot_prefs[i]["plot_title"] = plot_title + " " + time_title graphData.append(gdata) # Do not make legend for the plot with non-string keys (except for PieGraphs) if not graphData[0].subplots and graphData[ 0].key_type != "string" and not plot_type == "PieGraph": prefs["legend"] = False if prefs["legend"] and graphData[ 0].key_type != "string" and plot_type == "PieGraph": graphData[0].initialize(key_type="string") legend = Legend(graphData[0], None, prefs) self.figure = Figure() # Make Water Mark image = prefs.get("watermark", None) self.drawWaterMark(image) legend_ax, plot_axes = self.layoutFigure(legend) if DEBUG: print("makeGraph time layout", time.time() - start) start = time.time() # Make plots for i in range(nPlots): plot_type = plot_prefs[i]["plot_type"] try: # TODO: Remove when we moved to python3 exec("import %s" % plot_type) except ImportError: print("Trying to use python like import") try: exec("from . import %s" % plot_type) except ImportError as x: print("Failed to import graph type %s: %s" % (plot_type, str(x))) return None ax = plot_axes[i] plot = eval("%s.%s(graphData[i],ax,plot_prefs[i])" % (plot_type, plot_type)) plot.draw() if DEBUG: print("makeGraph time plots", time.time() - start) start = time.time() # Make legend if legend_ax: legend.setAxes(legend_ax) legend.draw() if DEBUG: print("makeGraph time legend", time.time() - start) start = time.time() # return S_OK() def drawWaterMark(self, imagePath=None): """Make the figure water mark""" prefs = self.prefs try: from PIL import Image, ImageEnhance except ImportError: return if not imagePath: if "watermark" in prefs: imagePath = os.path.expandvars( os.path.expanduser(prefs["watermark"])) if not imagePath: return try: image = Image.open(imagePath) enh = ImageEnhance.Contrast(image) i = enh.enhance(0.1) img_size = i.size resize = 1.0 if prefs["width"] < img_size[0]: resize = prefs["width"] / float(img_size[0]) if prefs["height"] < img_size[1]: resize = min(resize, prefs["height"] / float(img_size[1])) box = ( 0.5 - img_size[0] / float(prefs["width"]) * resize / 2.0, 0.5 - img_size[1] / float(prefs["height"]) * resize / 2.0, img_size[0] / float(prefs["width"]) * resize, img_size[1] / float(prefs["height"]) * resize, ) # print box ax_wm = self.figure.add_axes(box) ax_wm.imshow(i, origin="lower", aspect="equal", zorder=-10) ax_wm.axis("off") ax_wm.set_frame_on(False) ax_wm.set_clip_on(False) except Exception as e: print(e) def writeGraph(self, fname, fileFormat="PNG"): """Write out the resulting graph to a file with fname in a given format""" self.canvas.draw() if fileFormat.lower() == "png": self.canvas.print_png(fname) else: gLogger.error("File format '%s' is not supported!" % fileFormat)
def plot_dynamic_results(self, xx, xy, vel, omega, start, end, make_video): if make_video: # Use Agg backend for canvas from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas fig = plt.figure(constrained_layout=True, figsize=(16, 9)) else: fig = plt.figure(constrained_layout=True) gs = GridSpec(2, 4, figure=fig) vel_ax = fig.add_subplot(gs[0, :2]) vel_line, = vel_ax.plot([1], '-o', markersize=4, linewidth=2) vel_ax.set_xlabel('Time [s]') vel_ax.set_ylabel('Velocity [m/s]') vel_ax.set_ylim(-self.config.lin_vel_max - 0.1, self.config.lin_vel_max + 0.1) vel_ax.set_xlim(0, self.config.ts * len(xx)) vel_ax.grid('on') omega_ax = fig.add_subplot(gs[1, :2]) omega_line, = omega_ax.plot([1], '-o', markersize=4, linewidth=2) omega_ax.set_ylim(-self.config.ang_vel_max - 0.1, self.config.ang_vel_max + 0.1) omega_ax.set_xlim(0, self.config.ts * len(xx)) omega_ax.grid('on') omega_ax.set_xlabel('Time [s]') omega_ax.set_ylabel('Angular velocity [rad/s]') path_ax = fig.add_subplot(gs[:, 2:]) path_ax.plot(start[0], start[1], marker='*', color='g', markersize=15, label='Start') path_ax.plot(end[0], end[1], marker='*', color='r', markersize=15, label='End') self.ppp.plot_all(path_ax) path_line, = path_ax.plot([1], '-ob', alpha=0.7, markersize=5) path_ax.set_xlabel('X [m]', fontsize=15) path_ax.set_ylabel('Y [m]', fontsize=15) path_ax.axis('equal') if make_video: fig.tight_layout() legend_elems = [ Line2D([0], [0], color='k', label='Original Boundary'), Line2D([0], [0], color='g', label='Padded Boundary'), Line2D([0], [0], marker='o', color='b', label='Traversed Path', alpha=0.5), Line2D([0], [0], marker='*', color='g', label='Start Position', alpha=0.5), Line2D([0], [0], marker='*', color='r', label='End Position'), mpatches.Patch(color='b', label='Robot'), mpatches.Patch(color='r', label='Obstacle'), mpatches.Patch(color='y', label='Padded obstacle') ] path_ax.legend(handles=legend_elems, loc='lower left') obs = [object] * len(self.ppp.dyn_obs_list) obs_padded = [object] * len(self.ppp.dyn_obs_list) start_idx = 0 for i in range(start_idx, len(xx)): time = np.linspace(0, self.config.ts * i, i) omega_line.set_data(time, omega[:i]) vel_line.set_data(time, vel[:i]) path_line.set_data(xx[:i], xy[:i]) veh = plt.Circle((xx[i], xy[i]), self.config.vehicle_width / 2, color='b', alpha=0.7, label='Robot') path_ax.add_artist(veh) for j, obstacle in enumerate(self.ppp.dyn_obs_list): p1, p2, freq, x_radius, y_radius, angle = obstacle if self.sinus_object and j == 2: pos = self.ppp.generate_sinus_obstacle( p1, p2, freq, i * self.config.ts) else: pos = self.ppp.generate_obstacle(p1, p2, freq, i * self.config.ts) obs[j] = mpatches.Ellipse(pos, x_radius * 2, y_radius * 2, angle / (2 * math.pi) * 360, color='r', alpha=1, label='Obstacle') x_rad_pad = x_radius + self.config.vehicle_width / 2 + self.config.vehicle_margin y_rad_pad = y_radius + self.config.vehicle_width / 2 + self.config.vehicle_margin obs_padded[j] = mpatches.Ellipse(pos, x_rad_pad * 2, y_rad_pad * 2, angle / (2 * math.pi) * 360, color='y', alpha=0.7, label='Padded obstacle') path_ax.add_artist(obs_padded[j]) path_ax.add_artist(obs[j]) #path_ax.set_xlim([35, 47]) #path_ax.set_ylim([10, 22]) if make_video: # put pixel buffer in numpy array canvas = FigureCanvas(fig) canvas.draw() mat = np.array(canvas.renderer._renderer) mat = cv2.cvtColor(mat, cv2.COLOR_RGB2BGR) if i == start_idx: # create OpenCV video writer video = cv2.VideoWriter('video.mp4', cv2.VideoWriter_fourcc(*'avc1'), 10, (mat.shape[1], mat.shape[0])) # write frame to video video.write(mat) print(f'[INFO] wrote frame {i+1}/{len(xx)}') else: plt.draw() plt.pause(self.config.ts / 10) veh.remove() for j in range(len(self.ppp.dyn_obs_list)): obs[j].remove() obs_padded[j].remove() if make_video: # close video writer video.release() cv2.destroyAllWindows() plt.show()
def plt_plot_heatmap(data, shape, rows, cols, title=None, x_axis_label=None, y_axis_label=None, x_axis_values=None, y_axis_values=None, hide_axis=True, vmin=None, vmax=None): res = [] shape = (max(2, ceil(shape[1] / 80 / cols)), max(2, ceil(shape[0] / 80 / rows))) fig, ax = plt.subplots(1, 1, figsize=shape) canvas = FigureCanvas(fig) # for i in xrange(y.shape[0]): # sns.heatmap(y[i], ax=ax, vmin=minn, vmax=maxx) # canvas.draw() # draw the canvas, cache the renderer # # l, b, w, h = fig.bbox.bounds # w, h = int(w), int(h) # im = fromstring(canvas.tostring_rgb(), dtype='uint8') # im.shape = h, w, 3 # res.append(im) img = ax.imshow(zeros((data.shape[1], data.shape[2])), cmap='viridis', vmin=vmin if vmin is not None else data.min(), vmax=vmax if vmax is not None else data.max(), interpolation='none', aspect='auto') # get rid of spines and fix range of axes, rotate x-axis labels ax.spines['left'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') if hide_axis: ax.set_xticks([]) ax.set_yticks([]) ax.xaxis.set_ticklabels([]) ax.yaxis.set_ticklabels([]) fig.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.95, hspace=0, wspace=0) else: if title is not None: plt.title(title) if x_axis_label is not None: ax.set_xlabel(x_axis_label) if y_axis_label is not None: ax.set_ylabel(y_axis_label) if x_axis_values is not None: a = arange(0, x_axis_values.shape[0], 3) + 0.5 b = arange(x_axis_values.min(), x_axis_values.max() + 1.5, 1.5) ax.set_xticks(a) ax.set_xticklabels(b, rotation=90) if y_axis_values is not None: a = arange(0, y_axis_values.shape[0], 3) + 0.5 # c = roundup((y_axis_values.max() - y_axis_values.min()) / 11) # b = arange(y_axis_values.min(), y_axis_values.max(), c) b = linspace(y_axis_values.min(), y_axis_values.max(), num=10, dtype=int) ax.set_yticks(a) ax.set_yticklabels(b) # for tick in ax.get_xticklabels(): # tick.set_rotation(90) if not hide_axis: divider = make_axes_locatable(ax) # colorbar on the right of ax. Colorbar width in % of ax and space between them is defined by pad in inches cax = divider.append_axes('right', size='5%', pad=0.07) cb = fig.colorbar(img, cax=cax) # remove colorbar frame/spines cb.outline.set_visible(False) # don't stop after each subfigure change plt.show(block=False) if not hide_axis: fig.tight_layout() canvas.draw() # draw the canvas, cache the renderer # keep bg in memory background = fig.canvas.copy_from_bbox(ax.bbox) # start = time.time() for i in xrange(data.shape[0]): img.set_array(data[i]) # restore background fig.canvas.restore_region(background) ax.draw_artist(img) # fill in the axes rectangle fig.canvas.blit(ax.bbox) # loop through array # for i in xrange(data.shape[0]): # time.sleep(0.005) # img.set_array(data[i]) # canvas.draw() l, b, w, h = fig.bbox.bounds w, h = int(w), int(h) im = fromstring(canvas.tostring_rgb(), dtype='uint8') im.shape = h, w, 3 res.append(im) fig.clf() plt.clf() plt.close() return array(res)
def DisplayFigure(fig): canvas = FigureCanvas(fig) canvas.draw() s, (width, height) = canvas.print_to_buffer() X = np.frombuffer(s, np.uint8).reshape((height, width, 4)) DisplayArray(color.rgb2gray(X))
def imshow(self, data, *, keepdims=True): """Show the monochrome, RGB or RGBA image data, or a matplotlib Figure. Parameters ---------- data : numpy.ndarray, or matplotlib.figure.Figure The data to display in the viewer. * If `data` is a matplotlib Figure, that has already been composed elsewhere, then it gets rendered into a 3d RGBA array and displayed as color image. * If `data` is a numpy array, then it must either be a 2d or a 3d array for a grayscale or color image, respectively. In the color image case the last dim of the array must have size `3` for an RGB image or `4` for and RGBA image (RGB with an alpha channel). keepdims : bool, default=True Whether to preserve the current dimensions of the viewer, or resize it to fit the width and height of the current image. Details ------- To prevent memory leaks it is advisable to close the figure afterwards, using `plt.close(fig)`. However, creating a new figure and then closing it appears to incur excessive overhead. One possible workaround is to create a figure and axes only once, and, when updating it, recreate its contents on the axes before and clear them after any call to `.imshow`. In general matplotlib is not optimized for live redrawing, but lowering `figsize` and `dpi` may improve fps. """ if not self.isopen: return False # use the `agg` backend directly to create RGBA arrays from figures # https://matplotlib.org/stable/gallery/user_interfaces/canvasagg.html if isinstance(data, Figure): # assume all necessary plotting and artistry has been done canvas = FigureCanvasAgg(data) # XXX does not seem to leak mem # render into a buffer and convert to numpy array canvas.draw() data = np.array(canvas.buffer_rgba()) if not isinstance(data, np.ndarray): raise TypeError(f'`data` must be either a numpy array or a' f' matplotlib `Figure`. Got `{type(data)}`.') # figure out the image format if not data.dtype == np.uint8: raise TypeError(f'`data` must be `np.unit8`. Got `{data.dtype}`.') height, width, *channels = data.shape if not channels: # grayscale image format = 'I' elif len(channels) == 1: # color image optionally with alpha channel assert channels[0] in (3, 4) format = 'RGBA' if channels[0] == 4 else 'RGB' else: raise TypeError(f'`data` is not an image `{data.shape}`.') # ensure the current window's gl context before manipulating textures self.switch_to() # convert image data to gl texture texture = ImageData(width, height, format, data.tobytes(), pitch=-width * len(format)).get_texture() # resize if instructed or on the first call to `.imshow` if not hasattr(self, 'texture') or not keepdims: sw, sh = self.scale self.set_size(texture.width * sw, texture.height * sh) self.texture = texture # handle events and draw the data return self.render()
class Oengus(): """ We simply derive a new class of Frame as the man frame of our app""" def __init__(self, preset_view='Default', interactive=True, mainApp=None): self.IseultDir = os.path.join(os.path.dirname(__file__), '..') self.sims = [] self.cur_sim = 0 # the curent sim on the playback bar self.sim_names = [] self.sims_shown = [] # Create a dictionary to save the axes lims in case spatial # axes are shared across plots. # self.axes_lims = { # 'x': [None, None], # 'y': [None, None], # 'z': [None, None] # } self.interactive = interactive # Create the figure self.figure = Figure(edgecolor='none', facecolor='w', dpi=100) if self.interactive: from matplotlib.backends.backend_qt4agg import FigureCanvas self.canvas = FigureCanvas(self.figure) self.canvas.draw() self.mainApp = mainApp else: from matplotlib.backends.backend_agg import FigureCanvasAgg self.canvas = FigureCanvasAgg(self.figure) # self.canvas.mpl_connect('draw_event', self.on_draw) self.load_view(preset_view) # self.add_sim('sim0') # def on_draw(self, event): # print(event.name) def GenMainParamDict(self): ''' The function that reads in a config file and then makes MainParamDict to hold all of the main iseult parameters. It also sets all of the plots parameters.''' # Since configparser reads in strings we have to format the data. # First create MainParamDict with the default parameters, # the dictionary that will hold the parameters for the program. # See ./iseult_configs/Default.cfg for a description of what each # parameter does. self.MainParamDict = { '2DSlicePlane': 0, # 0 = x-y plane, 1 == x-z plane 'NumberOfSims': 2, 'Average1D': 0, 'WindowSize': '1200x700', 'yTop': 100.0, 'yBottom': 0.0, 'LinkTime': True, 'TimeUnits': None, 'Reload2End': True, 'ColorMap': 'viridis', 'FFTLeft': 0.0, 'ShowTitle': True, 'ImageAspect': 0, 'WaitTime': 0.01, 'MaxCols': 8, 'VAxesExtent': [4, 90, 0, 92], 'kRight': 1.0, 'DoLorentzBoost': False, 'NumOfRows': 3, 'MaxRows': 8, 'SetkLim': False, 'VCbarExtent': [4, 90, 94, 97], 'SkipSize': 5, 'xLeft': 0.0, 'NumFontSize': 11, 'AxLabelSize': 11, 'TitleFontSize': 15, 'FFTRelative': True, 'NumOfCols': 2, 'VSubPlotParams': { 'right': 0.95, 'bottom': 0.06, 'top': 0.93, 'wspace': 0.23, 'hspace': 0.15, 'left': 0.06 }, 'HAxesExtent': [18, 92, 0, -1], 'SetyLim': False, 'cmaps_with_green': [ 'viridis', 'Rainbow + White', 'Blue/Green/Red/Yellow', 'Cube YF', 'Linear_L' ], 'HSubPlotParams': { 'right': 0.95, 'bottom': 0.06, 'top': 0.91, 'wspace': 0.15, 'hspace': 0.3, 'left': 0.06 }, 'yLabelPad': 0, 'cbarLabelPad': 15, 'SetxLim': False, 'Rel2Shock': False, 'ConstantShockVel': True, 'xRight': 100.0, 'LinkSpatial': 2, 'HCbarExtent': [0, 4, 0, -1], 'xLabelPad': 0, 'annotateTextSize': 18, 'FFTRight': 200.0, 'ClearFig': True, 'HorizontalCbars': False, 'DivColorMap': 'BuYlRd', 'kLeft': 0.1, 'LoopPlayback': True, 'electron_color': '#fca636', 'electron_fit_color': 'yellow', 'ion_color': '#d6556d', 'ion_fit_color': 'r', 'shock_color': 'w', 'FigSize': [12.0, 6.22], 'dpi': 100, 'FFT_color': 'k', 'shock_method': 'Density Half Max', 'legendLabelSize': 11, 'sim_params': [] } self.MainParamDict.update(deepcopy(self.cfgDict['MainParamDict'])) self.electron_color = self.MainParamDict['electron_color'] self.ion_color = self.MainParamDict['ion_color'] self.shock_color = self.MainParamDict['shock_color'] self.ion_fit_color = self.MainParamDict['ion_fit_color'] self.electron_fit_color = self.MainParamDict['electron_fit_color'] self.MainParamDict['NumberOfSims'] = max( 1, self.MainParamDict['NumberOfSims']) # Loading a config file may change the stride... watch out! # for sim in self.sims: # if sim.xtra_stride != self.MainParamDict['PrtlStride']: # sim.xtra_stride = self.MainParamDict['PrtlStride'] # Loading a config file may change the number of sims if self.MainParamDict['NumberOfSims'] != len(self.sims): tmp_num = self.MainParamDict['NumberOfSims'] while len(self.sims) < tmp_num: self.add_sim(f'sim{len(self.sims)}') while tmp_num < len(self.sims): self.pop_sim() def calc_slices(self, ax, sim, n=None): # FIND THE SLICE attr, param = 'x', 'xSlice' if ax == 'y': attr, param = 'y', 'ySlice' elif ax == 'z': attr, param = 'z', 'zSlice' maxInd = len( sim.get_data(n, data_class='axes', attribute=attr)['data']) - 1 slice_percent = self.MainParamDict['sim_params'][sim.sim_num][param] slice = int(np.around(slice_percent * maxInd)) return slice def load_view(self, view_name): self.figure.clf() view_file = view_name.strip().replace(' ', '_') + '.yml' view_file = os.path.join(self.IseultDir, '.iseult_configs', view_file) if os.path.exists(view_file) and os.path.isfile(view_file): with open(view_file) as f: self.cfgDict = yaml.safe_load(f) else: print('Cannot find/load ' + preset_view.strip().replace(' ', '_') + '.yml in .iseult_configs.' + ' If the name of view contains whitespace,') print('either it must be enclosed in quotation marks' + "or given with whitespace replaced with '_'.") print('Name is case sensitive.') print('Reverting to Default view') default_file = os.path.join('.iseult_configs', 'Default.yml') with open(os.path.join(self.IseultDir, default_file)) as f: self.cfgDict = yaml.safe_load(f) self.GenMainParamDict() # self.figure.dpi = self.MainParamDict['dpi'] self.figure.figsize = self.MainParamDict['FigSize'] if self.MainParamDict['HorizontalCbars']: self.axes_extent = self.MainParamDict['HAxesExtent'] self.cbar_extent = self.MainParamDict['HCbarExtent'] self.SubPlotParams = self.MainParamDict['HSubPlotParams'] else: self.axes_extent = self.MainParamDict['VAxesExtent'] self.cbar_extent = self.MainParamDict['VCbarExtent'] self.SubPlotParams = self.MainParamDict['VSubPlotParams'] self.figure.subplots_adjust(**self.SubPlotParams) # Make the object hold the timestep info # Some options to set the way the spectral lines are dashed self.spect_plot_counter = 0 self.dashes_options = [[], [3, 1], [5, 1], [1, 1]] # Create the list of all of subplot wrappers self.SubPlotList = [[] for i in range(self.MainParamDict['MaxRows'])] self.showingCPUs = False # self.showingTotEnergy = False self.plot_types_dict = { 'ScalarFlds': scalarFldsPlot, 'VectorFlds': vectorFldsPlot, 'PhasePlot': phasePlot, 'ScalarVsTime': scalar_vs_timePlot, 'Spectra': SpectralPlot, # 'SpectraPlot': SpectralPanel, # 'FFTPlots': FFTPanel, # 'TotalEnergyPlot': TotEnergyPanel, # 'Moments': MomentsPanel } for i in range(self.MainParamDict['MaxRows']): for j in range(self.MainParamDict['MaxCols']): tmp_str = f"Chart{i}_{j}" if tmp_str in self.cfgDict.keys(): tmpchart_type = self.cfgDict[tmp_str]['chart_type'] self.SubPlotList[i].append( self.plot_types_dict[tmpchart_type]( self, (i, j), self.cfgDict[tmp_str])) try: show_cpus = self.cfgDict[tmp_str]['show_cpu_domains'] self.showingCPUs += show_cpus except KeyError: pass else: # The graph isn't specified in the config file, # just set it equal to scalar field plots self.SubPlotList[i].append( self.plot_types_dict['ScalarFlds'](self, (i, j), {})) self.calc_sims_shown() def calc_sims_shown(self): self.sims_shown = [] for i in range(self.MainParamDict['NumOfRows']): for j in range(self.MainParamDict['NumOfCols']): sim_nums = self.SubPlotList[i][j].get_sim_nums() for sim_num in sim_nums: if sim_num not in self.sims_shown: self.sims_shown.append(sim_num) self.sims_shown.sort() def add_sim(self, name): sim_num = len(self.sims) self.sims.append(picSim(name=name, num=sim_num)) self.sim_names.append(name) sim_params = { 'shock_method': 'Density Half Max', 'Average1D': 0, '2DSlicePlane': 0, # 0 = x-y plane, 1 == x-z plane 'PrtlStride': 5, 'xSlice': 0.0, 'ySlice': 0.5, 'zSlice': 0.5 } if len(self.MainParamDict['sim_params']) < len(self.sims): self.MainParamDict['sim_params'].append(sim_params) else: self.MainParamDict['sim_params'][-1].update(sim_params) if self.sims[-1].xtra_stride != sim_params['PrtlStride']: self.sims[-1].xtra_stride = sim_params['PrtlStride'] if self.MainParamDict['LinkTime']: unit = self.MainParamDict['TimeUnits'] cur_t = self.sims[self.cur_sim].get_time(units=unit) self.sims[-1].set_time(cur_t, units=unit) self.MainParamDict['NumberOfSims'] += 1 def pop_sim(self): # make sure the sim isn't shown. max_shown = max(self.sims_shown) if len(self.sims) > max_shown + 1: self.MainParamDict['sim_params'].pop(-1) self.sim_names.pop(-1) self.MainParamDict['NumberOfSims'] -= 1 return self.sims.pop(-1) return None def create_graphs(self): # divy up the figure into a bunch of subplots using GridSpec. self.gs0 = gridspec.GridSpec(self.MainParamDict['NumOfRows'], self.MainParamDict['NumOfCols']) for i in range(self.MainParamDict['NumOfRows']): for j in range(self.MainParamDict['NumOfCols']): self.SubPlotList[i][j].build_axes() self.SubPlotList[i][j].axis_info() for i in range(self.MainParamDict['NumOfRows']): for j in range(self.MainParamDict['NumOfCols']): self.SubPlotList[i][j].draw() # self.sim, -1) self.make_title() def make_title(self): if self.MainParamDict['ShowTitle']: sim = self.sims[0] outname = os.path.abspath(sim.outdir) try: f_end = sim.file_list[sim.get_time()] cur_time = int(round(sim.get_time(units='c_ompe'))) self.figure.suptitle(f'{outname}/*.{f_end} at ' + r'$\omega_{pe}t=$' + f'{cur_time}', size=self.MainParamDict['TitleFontSize']) except IndexError: self.figure.suptitle(f'{outname} is empty', size=self.MainParamDict['TitleFontSize']) def home(self): for i in range(self.MainParamDict['NumOfRows']): for j in range(self.MainParamDict['NumOfCols']): self.SubPlotList[i][j].go_home() if self.interactive: self.mainApp.toolbar.update() self.canvas.draw() def link_axes(self, ax, ax_type=None, subplot=None): ax_info = None lims = None dtype = None if ax_type == 'x': ax_info = subplot.x_axis_info lims = ax.get_xlim() if ax_type == 'y': ax_info = subplot.y_axis_info lims = ax.get_ylim() if ax_info is not None: dtype = ax_info['data_ax'] for elm in subplot.get_linked_ax(): if elm['data_ax'] == dtype: i, j = elm['pos'] if elm['axes'] == 'x': cur_lim = self.SubPlotList[i][j].axes.get_xlim() if (lims[0] != cur_lim[0] and lims[1] != cur_lim[1]): self.SubPlotList[i][j].axes.set_xlim(lims) elif elm['axes'] == 'y': cur_lim = self.SubPlotList[i][j].axes.get_ylim() if (lims[0] != cur_lim[0] and lims[1] != cur_lim[1]): self.SubPlotList[i][j].axes.set_ylim(lims) def draw_output(self): for i in range(self.MainParamDict['NumOfRows']): for j in range(self.MainParamDict['NumOfCols']): self.SubPlotList[i][j].save_axes_pos() self.SubPlotList[i][j].refresh() self.SubPlotList[i][j].load_axes_pos() if self.interactive: self.mainApp.toolbar.update() if self.MainParamDict['ShowTitle']: sim = self.sims[0] outname = os.path.abspath(sim.outdir) try: f_end = sim.file_list[sim.get_time()] cur_time = int(round(sim.get_time(units='c_ompe'))) self.figure.suptitle(f'{outname}/*.{f_end} at ' + r'$\omega_{pe}t=$' + f'{cur_time}', size=self.MainParamDict['TitleFontSize']) except IndexError: self.figure.suptitle(f'{outname} is empty', size=self.MainParamDict['TitleFontSize']) self.canvas.draw() if not self.interactive: s, (width, height) = self.canvas.print_to_buffer() return Image.frombytes('RGBA', (width, height), s) def make_movie(self, fname, start, stop, step, FPS, outdir): '''Record a movie''' # First find the last frame is stop is -1: if stop == -1: stop = len(self.sims[self.cur_sim]) # Now build all the frames we have to visit frame_arr = np.arange(start, stop, step) if frame_arr[-1] != stop: frame_arr = np.append(frame_arr, stop) # If total energy plot is showing, we have to # loop through everything twice. # if self.showing_total_energy_plt: # for k in frame_arr: # self.TimeStep.set(k) cmdstring = [ 'ffmpeg', # Set framerate to the the user selected option '-framerate', str(int(FPS)), '-pattern_type', 'glob', '-i', '-', '-c:v', 'prores', '-pix_fmt', 'yuv444p10le', os.path.join(os.path.join(outdir), fname) ] pipe = subprocess.Popen(cmdstring, stdin=subprocess.PIPE) print(frame_arr) for i in frame_arr: self.sims[self.cur_sim].set_time(i - 1, units=None) if self.MainParamDict['LinkTime']: unit = self.MainParamDict['TimeUnits'] cur_t = self.sims[self.cur_sim].get_time(units=None) print(cur_t) for sim_num in self.sims_shown: self.sims[sim_num].set_time(cur_t, units=None) self.draw_output() s, (width, height) = self.canvas.print_to_buffer() im = Image.frombytes('RGBA', (width, height), s) # The ffmpeg command we want to call. # ffmpeg -framerate [FPS] -i [NAME_***].png -c:v prores -pix_fmt # yuv444p10le [OUTPUTNAME].mov im.save(pipe.stdin, 'PNG') print(f"saving image {i} to pipe") pipe.stdin.close() pipe.wait() # Make sure all went well if pipe.returncode != 0: raise subprocess.CalledProcessError(pipe.returncode, cmdstring)
class RightFrame: """ This class is for creating right frame widgets which are used to draw graphics on canvas as well as embedding matplotlib figures in the tkinter. Farhad Kamangar 2018_06_03 """ def __init__(self, root, master, debug_print_flag=False): self.root = root self.master = master self.debug_print_flag = debug_print_flag width_px = root.winfo_screenwidth() height_px = root.winfo_screenheight() width_mm = root.winfo_screenmmwidth() height_mm = root.winfo_screenmmheight() # 2.54 cm = in width_in = width_mm / 25.4 height_in = height_mm / 25.4 width_dpi = width_px / width_in height_dpi = height_px / height_in if self.debug_print_flag: print('Width: %i px, Height: %i px' % (width_px, height_px)) print('Width: %i mm, Height: %i mm' % (width_mm, height_mm)) print('Width: %f in, Height: %f in' % (width_in, height_in)) print('Width: %f dpi, Height: %f dpi' % (width_dpi, height_dpi)) # self.canvas = self.master.canvas ######################################################################### # Set up the plotting frame and controls frame ######################################################################### master.rowconfigure(0, weight=10, minsize=200) master.columnconfigure(0, weight=1) master.rowconfigure(1, weight=1, minsize=20) self.right_frame = tk.Frame(self.master, borderwidth=10, relief='sunken') self.right_frame.grid(row=0, column=0, columnspan=1, sticky=tk.N + tk.E + tk.S + tk.W) self.matplotlib_width_pixel = self.right_frame.winfo_width() self.matplotlib_height_pixel = self.right_frame.winfo_height() # set up the frame which contains controls such as sliders and buttons self.controls_frame = tk.Frame(self.master) self.controls_frame.grid(row=1, column=0, sticky=tk.N + tk.E + tk.S + tk.W) self.controls_frame.rowconfigure(1, weight=1, minsize=20) self.draw_button = tk.Button(self.controls_frame, text="Draw", fg="red", width=16, command=self.graphics_draw_callback) self.plot_2d_button = tk.Button( self.controls_frame, text="Plot 2D", fg="red", width=16, command=self.matplotlib_plot_2d_callback) self.plot_3d_button = tk.Button( self.controls_frame, text="Plot 3D", fg="red", width=16, command=self.matplotlib_plot_3d_callback) self.draw_button.grid(row=0, column=0) self.plot_2d_button.grid(row=0, column=1) self.plot_3d_button.grid(row=0, column=2) self.right_frame.update() self.canvas = tk.Canvas(self.right_frame, relief='ridge', width=self.right_frame.winfo_width() - 110, height=self.right_frame.winfo_height()) if self.debug_print_flag: print("Right frame width, right frame height : ", self.right_frame.winfo_width(), self.right_frame.winfo_height()) self.canvas.rowconfigure(0, weight=1) self.canvas.columnconfigure(0, weight=1) self.canvas.grid(row=0, column=0, sticky=tk.N + tk.E + tk.S + tk.W) self.canvas.bind("<ButtonPress-1>", self.left_mouse_click_callback) self.canvas.bind("<ButtonRelease-1>", self.left_mouse_release_callback) self.canvas.bind("<B1-Motion>", self.left_mouse_down_motion_callback) self.canvas.bind("<ButtonPress-3>", self.right_mouse_click_callback) self.canvas.bind("<ButtonRelease-3>", self.right_mouse_release_callback) self.canvas.bind("<B3-Motion>", self.right_mouse_down_motion_callback) self.canvas.bind("<Key>", self.key_pressed_callback) self.canvas.bind("<Up>", self.up_arrow_pressed_callback) self.canvas.bind("<Down>", self.down_arrow_pressed_callback) self.canvas.bind("<Right>", self.right_arrow_pressed_callback) self.canvas.bind("<Left>", self.left_arrow_pressed_callback) self.canvas.bind("<Shift-Up>", self.shift_up_arrow_pressed_callback) self.canvas.bind("<Shift-Down>", self.shift_down_arrow_pressed_callback) self.canvas.bind("<Shift-Right>", self.shift_right_arrow_pressed_callback) self.canvas.bind("<Shift-Left>", self.shift_left_arrow_pressed_callback) self.canvas.bind("f", self.f_key_pressed_callback) self.canvas.bind("b", self.b_key_pressed_callback) # Create a figure for 2d plotting self.matplotlib_2d_fig = mpl.figure.Figure() # self.matplotlib_2d_fig.set_size_inches(4,2) self.matplotlib_2d_fig.set_size_inches( (self.right_frame.winfo_width() / width_dpi) - 0.5, self.right_frame.winfo_height() / height_dpi) self.matplotlib_2d_ax = self.matplotlib_2d_fig.add_axes( [.1, .1, .7, .7]) if self.debug_print_flag: print("Matplotlib figsize in inches: ", (self.right_frame.winfo_width() / width_dpi) - 0.5, self.right_frame.winfo_height() / height_dpi) self.matplotlib_2d_fig_x, self.matplotlib_2d_fig_y = 0, 0 self.matplotlib_2d_fig_loc = (self.matplotlib_2d_fig_x, self.matplotlib_2d_fig_y) # fig = plt.figure() # ax = fig.gca(projection='3d') # Create a figure for 3d plotting self.matplotlib_3d_fig = mpl.figure.Figure() self.matplotlib_3d_figure_canvas_agg = FigureCanvasAgg( self.matplotlib_3d_fig) # self.matplotlib_2d_fig.set_size_inches(4,2) self.matplotlib_3d_fig.set_size_inches( (self.right_frame.winfo_width() / width_dpi) - 0.5, self.right_frame.winfo_height() / height_dpi) self.matplotlib_3d_ax = self.matplotlib_3d_fig.add_axes( [.1, .1, .6, .6], projection='3d') self.matplotlib_3d_fig_x, self.matplotlib_3d_fig_y = 0, 0 self.matplotlib_3d_fig_loc = (self.matplotlib_3d_fig_x, self.matplotlib_3d_fig_y) def display_matplotlib_figure_on_tk_canvas(self): # Draw a matplotlib figure in a Tk canvas self.matplotlib_2d_ax.clear() X = np.linspace(0, 2 * np.pi, 100) # Y = np.sin(X) Y = np.sin(X * np.int((np.random.rand() + .1) * 10)) self.matplotlib_2d_ax.plot(X, Y) self.matplotlib_2d_ax.set_xlim([0, 2 * np.pi]) self.matplotlib_2d_ax.set_ylim([-1, 1]) self.matplotlib_2d_ax.grid(True, which='both') self.matplotlib_2d_ax.axhline(y=0, color='k') self.matplotlib_2d_ax.axvline(x=0, color='k') # plt.subplots_adjust(left=0.0, right=1.0, bottom=0.0, top=1.0) # Place the matplotlib figure on canvas and display it self.matplotlib_2d_figure_canvas_agg = FigureCanvasAgg( self.matplotlib_2d_fig) self.matplotlib_2d_figure_canvas_agg.draw() self.matplotlib_2d_figure_x, self.matplotlib_2d_figure_y, self.matplotlib_2d_figure_w, \ self.matplotlib_2d_figure_h = self.matplotlib_2d_fig.bbox.bounds self.matplotlib_2d_figure_w, self.matplotlib_2d_figure_h = int( self.matplotlib_2d_figure_w), int(self.matplotlib_2d_figure_h) self.photo = tk.PhotoImage(master=self.canvas, width=self.matplotlib_2d_figure_w, height=self.matplotlib_2d_figure_h) # Position: convert from top-left anchor to center anchor self.canvas.create_image( self.matplotlib_2d_fig_loc[0] + self.matplotlib_2d_figure_w / 2, self.matplotlib_2d_fig_loc[1] + self.matplotlib_2d_figure_h / 2, image=self.photo) tkagg.blit( self.photo, self.matplotlib_2d_figure_canvas_agg.get_renderer()._renderer, colormode=2) self.matplotlib_2d_fig_w, self.matplotlib_2d_fig_h = self.photo.width( ), self.photo.height() self.canvas.create_text(0, 0, text="Sin Wave", anchor="nw") def display_matplotlib_3d_figure_on_tk_canvas(self): self.matplotlib_3d_ax.clear() r = np.linspace(0, 6, 100) temp = np.random.rand() theta = np.linspace(-temp * np.pi, temp * np.pi, 40) r, theta = np.meshgrid(r, theta) X = r * np.sin(theta) Y = r * np.cos(theta) Z = np.sin(np.sqrt(X**2 + Y**2)) surf = self.matplotlib_3d_ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap="coolwarm", linewidth=0, antialiased=False) # surf = self.matplotlib_3d_ax.plot_surface(X, Y, Z, rcount=1, ccount=1, cmap='bwr', edgecolor='none'); self.matplotlib_3d_ax.set_xlim(-6, 6) self.matplotlib_3d_ax.set_ylim(-6, 6) self.matplotlib_3d_ax.set_zlim(-1.01, 1.01) self.matplotlib_3d_ax.zaxis.set_major_locator(LinearLocator(10)) self.matplotlib_3d_ax.zaxis.set_major_formatter( FormatStrFormatter('%.02f')) # Place the matplotlib figure on canvas and display it self.matplotlib_3d_figure_canvas_agg.draw() self.matplotlib_3d_figure_x, self.matplotlib_3d_figure_y, self.matplotlib_3d_figure_w, \ self.matplotlib_3d_figure_h = self.matplotlib_2d_fig.bbox.bounds self.matplotlib_3d_figure_w, self.matplotlib_3d_figure_h = int( self.matplotlib_3d_figure_w), int(self.matplotlib_3d_figure_h) if self.debug_print_flag: print("Matplotlib 3d figure x, y, w, h: ", self.matplotlib_3d_figure_x, self.matplotlib_3d_figure_y, self.matplotlib_3d_figure_w, self.matplotlib_3d_figure_h) self.photo = tk.PhotoImage(master=self.canvas, width=self.matplotlib_3d_figure_w, height=self.matplotlib_3d_figure_h) # Position: convert from top-left anchor to center anchor self.canvas.create_image( self.matplotlib_3d_fig_loc[0] + self.matplotlib_3d_figure_w / 2, self.matplotlib_3d_fig_loc[1] + self.matplotlib_3d_figure_h / 2, image=self.photo) tkagg.blit( self.photo, self.matplotlib_3d_figure_canvas_agg.get_renderer()._renderer, colormode=2) self.matplotlib_3d_fig_w, self.matplotlib_3d_fig_h = self.photo.width( ), self.photo.height() def key_pressed_callback(self, event): self.root.status_bar.set('%s', 'Key pressed') def up_arrow_pressed_callback(self, event): self.root.status_bar.set('%s', "Up arrow was pressed") def down_arrow_pressed_callback(self, event): self.root.status_bar.set('%s', "Down arrow was pressed") def right_arrow_pressed_callback(self, event): self.root.status_bar.set('%s', "Right arrow was pressed") def left_arrow_pressed_callback(self, event): self.root.status_bar.set('%s', "Left arrow was pressed") def shift_up_arrow_pressed_callback(self, event): self.root.status_bar.set('%s', "Shift up arrow was pressed") def shift_down_arrow_pressed_callback(self, event): self.root.status_bar.set('%s', "Shift down arrow was pressed") def shift_right_arrow_pressed_callback(self, event): self.root.status_bar.set('%s', "Shift right arrow was pressed") def shift_left_arrow_pressed_callback(self, event): self.root.status_bar.set('%s', "Shift left arrow was pressed") def f_key_pressed_callback(self, event): self.root.status_bar.set('%s', "f key was pressed") def b_key_pressed_callback(self, event): self.root.status_bar.set('%s', "b key was pressed") def left_mouse_click_callback(self, event): self.root.status_bar.set( '%s', 'Left mouse button was clicked. ' + 'x=' + str(event.x) + ' y=' + str(event.y)) self.x = event.x self.y = event.y self.canvas.focus_set() def left_mouse_release_callback(self, event): self.root.status_bar.set( '%s', 'Left mouse button was released. ' + 'x=' + str(event.x) + ' y=' + str(event.y)) self.x = None self.y = None def left_mouse_down_motion_callback(self, event): self.root.status_bar.set( '%s', 'Left mouse down motion. ' + 'x=' + str(event.x) + ' y=' + str(event.y)) self.x = event.x self.y = event.y def right_mouse_click_callback(self, event): self.root.status_bar.set( '%s', 'Right mouse down motion. ' + 'x=' + str(event.x) + ' y=' + str(event.y)) self.x = event.x self.y = event.y def right_mouse_release_callback(self, event): self.root.status_bar.set( '%s', 'Right mouse button was released. ' + 'x=' + str(event.x) + ' y=' + str(event.y)) self.x = None self.y = None def right_mouse_down_motion_callback(self, event): self.root.status_bar.set( '%s', 'Right mouse down motion. ' + 'x=' + str(event.x) + ' y=' + str(event.y)) self.x = event.x self.y = event.y def left_mouse_click_callback(self, event): self.root.status_bar.set( '%s', 'Left mouse button was clicked. ' + 'x=' + str(event.x) + ' y=' + str(event.y)) self.x = event.x self.y = event.y # self.focus_set() def frame_resized_callback(self, event): print("frame resize callback") def create_graphic_objects(self): self.canvas.delete("all") r = np.random.rand() self.drawing_objects = [] for scale in np.linspace(.1, 0.8, 20): self.drawing_objects.append( self.canvas.create_oval( int(scale * int(self.canvas.cget("width"))), int(r * int(self.canvas.cget("height"))), int((1 - scale) * int(self.canvas.cget("width"))), int((1 - scale) * int(self.canvas.cget("height"))))) def redisplay(self, event): self.create_graphic_objects() def matplotlib_plot_2d_callback(self): self.display_matplotlib_figure_on_tk_canvas() self.root.status_bar.set( '%s', "called matplotlib_plot_2d_callback callback!") def matplotlib_plot_3d_callback(self): self.display_matplotlib_3d_figure_on_tk_canvas() self.root.status_bar.set( '%s', "called matplotlib_plot_3d_callback callback!") def graphics_draw_callback(self): self.create_graphic_objects() self.root.status_bar.set('%s', "called the draw callback!")
class Viewer(object): FROZEN_COLOR = (0, 255, 255) # cyan HOLE_COLOR = (128, 128, 128) # gray NORMAL_COLOR = (255, 255, 255) # white MAP_DICT = {'F': FROZEN_COLOR, 'H': HOLE_COLOR, 'N': NORMAL_COLOR} ACITON_DICT = { frozen_lake.LEFT: 'LEFT', frozen_lake.DOWN: 'DOWN', frozen_lake.RIGHT: 'RIGHT', frozen_lake.UP: 'UP' } def __init__(self, maze, convert): self.__maze_canvas = None self.__ax = None self.__sg_dict = {} self.__nrows = None self.__ncols = None self.__fig = None self.__canvas = None self.__convert = convert self.__initialize_map_dict() self.__initialize_maze_canvas(maze) def __initialize_map_dict(self): self.__map_dict = copy.deepcopy(self.MAP_DICT) for src, dst in self.__convert.iteritems(): self.__map_dict[src] = self.__map_dict[dst] def __initialize_maze_canvas(self, maze): start = np.argwhere(maze == 'S')[0] goal = np.argwhere(maze == 'G')[0] self.__sg_dict['S'] = start self.__sg_dict['G'] = goal self.__nrows, self.__ncols = maze.shape canvas_shape = [self.__nrows, self.__ncols, 3] self.__maze_canvas = np.zeros(canvas_shape, dtype=np.uint8) self.__maze_canvas[:, :] = self.__map_dict['F'] self.__maze_canvas[maze == 'H'] = self.__map_dict['H'] self.__fig = Figure() self.__canvas = FigureCanvas(self.__fig) def render(self, state, lastaction, mode='mpl'): if lastaction is not None: action_str = self.ACITON_DICT[lastaction] else: action_str = 'None' if mode == 'mpl': ax = plt.gca() else: ax = self.__fig.gca() ax.cla() ax.set_xticklabels([]) ax.set_yticklabels([]) ax.set_xticks(np.arange(-0.5, self.__ncols - 0.5)) ax.set_yticks(np.arange(-0.5, self.__nrows - 0.5)) ax.set_title('Last action: %s' % action_str, fontsize=18) ax.grid(linestyle='dashed') ax.imshow(self.__maze_canvas, interpolation='nearest') self.__agent_location = np.array( [state // self.__ncols, state % self.__ncols]) for text, location in self.__sg_dict.iteritems(): if not np.allclose(self.__agent_location, location): y, x = location ax.text(x, y, text, va='center', ha='center', fontsize=24) y, x = self.__agent_location ax.text(x, y, 'A', va='center', ha='center', fontsize=24) if mode == 'rgb_array': self.__canvas.draw() width, height = self.__fig.get_size_inches() * self.__fig.get_dpi() img = np.fromstring(self.__canvas.tostring_rgb(), dtype='uint8') img = img.reshape(int(height), int(width), 3) return img else: return plt.pause(1e-6)
def draw(self): FigureCanvasAgg.draw(self) tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2) self._master.update_idletasks()
def plot(self): #fig = plt.figure(figsize=(11,7)) fig = Figure(figsize=(8, 4)) canvas = FigureCanvas(fig) ax = fig.add_subplot(1, 1, 1) moon_delta_midnight = [] moon_airmass = [] for i, x in enumerate(self.moon_altaz.secz): if x <= 5 and x > 1.0: moon_delta_midnight.append(self.delta_midnight[i]) moon_airmass.append(x) #plotting the beginning and ending of the astronomical night ax.axvline(x=self.astronomical_night[0], color='k', linestyle='-', linewidth=2) ax.text(self.astronomical_night[0] - 0.7, 0.93, 'Night begin', fontsize=10) ax.axvline(x=self.astronomical_night[-1], color='k', linestyle='-', linewidth=2) ax.text(self.astronomical_night[-1] - 0.7, 0.93, 'Night end', fontsize=10) #plot the moon line ax.plot(moon_delta_midnight, moon_airmass, 'w', label='Moon', linestyle='dashed') len_range = int(len(self.nightrange)) mid_len_range = int(len(self.nightrange) / 2) #plot the blue gradients for the astronomical night time for xi in range(0, mid_len_range + 1): ax.fill_between(self.nightrange[xi:mid_len_range + 1], 3, 1, self.sun_alt_beforemid[xi:mid_len_range + 1], facecolor='blue', zorder=0, alpha=0.008) for xi in range(len_range, mid_len_range, -1): ax.fill_between(self.nightrange[mid_len_range:xi], 3, 1, self.sun_alt_beforemid[mid_len_range:xi], facecolor='blue', zorder=0, alpha=0.008) #plot the scheduled target info for t in self.scheduled_targets: t_altaz = t.skycoord.transform_to(self.frame_night) dm = [ x for i, x in enumerate(self.delta_midnight) if t_altaz.secz[i] > 1 and t_altaz.secz[i] <= 3.5 ] cc = [x for x in t_altaz.secz if x > 1 and x <= 3.5] #plot the airmass chart ax.plot(dm, cc, label=t.name, linestyle='dashed') s_start = (t.start_observation - self.midnight).to('hour') / u.hour s_end = (t.end_observation - self.midnight).to('hour') / u.hour scheduled_time_range = np.linspace(s_start, s_end, 50) bool_range = [True for x in scheduled_time_range] #print(s_start, s_end, min(dm), max(dm), t.name) #this is happening because it is being set to observe outside its range of observability!!!! try: sc_aa_tmp = [ x for i, x in enumerate(cc) if dm[i] > s_start and dm[i] < s_end ] scheduled_altaz = np.linspace(sc_aa_tmp[0], sc_aa_tmp[-1], 50) ax.plot(scheduled_time_range, scheduled_altaz, color='red', linewidth=10, alpha=0.4) except: print("weird bug, yikes", t.name) #plot the observation range as a filled column ax.fill_between(scheduled_time_range, 3, 1, bool_range, facecolor='red', zorder=0, alpha=0.4) #plot the observation range on top of the airmass line ax.text((s_end + s_start) / 2.0, 2.625, t.name, ha='center', va='center', rotation=90, size=9) fig.subplots_adjust(right=0.75) ax.legend(loc='upper left', bbox_to_anchor=(1, 1), borderaxespad=0.) ax.grid(True, color='k') ax.set_xlim(self.nightrange[0], self.nightrange[len(self.nightrange) - 1]) ax.set_ylim(3, 0.95) ax.set_xlabel('Hours from EDT Midnight') ax.set_ylabel('Airmass') canvas.draw() buf = canvas.buffer_rgba() X = numpy.asarray(buf) tmpfile = BytesIO() fig.savefig(tmpfile, format="png") data = base64.b64encode(tmpfile.getbuffer()).decode("ascii") self.encoded_chart = data
def plt_plot_filters_blit(y, x, shape, rows, cols, title=None, x_axis_label=None, y_axis_label=None, log_scale=0, hide_axis=False): res = [] x = arange(0, y.shape[1]) if x is None else x # if log_scale == 1: # y = log(y) # elif log_scale == 2: # x = log(x) # elif log_scale == 3: # x = log(x) # y = log(y) shape = (max(2, ceil(shape[1] / 80 / cols)), max(2, ceil(shape[0] / 80 / rows))) fig, ax = plt.subplots(1, 1, figsize=shape) canvas = FigureCanvas(fig) ax.set_xlim(min(x), max(x)) ax.set_ylim(y.min(), y.max()) if hide_axis: ax.xaxis.set_ticklabels([]) ax.yaxis.set_ticklabels([]) fig.subplots_adjust(left=0.05, bottom=0.05, right=0.95, top=0.95, hspace=0, wspace=0) else: if x_axis_label is not None: ax.set_xlabel(x_axis_label) if y_axis_label is not None: ax.set_ylabel(y_axis_label) if title is not None: plt.title(title) line, = ax.plot([], [], lw=2) if not hide_axis: fig.tight_layout() canvas.draw() # draw the canvas, cache the renderer # keep bg in memory background = fig.canvas.copy_from_bbox(ax.bbox) for i in xrange(y.shape[0]): line.set_data(x, y[i]) # line.set_color() # restore background fig.canvas.restore_region(background) # redraw just the points ax.draw_artist(line) # fill in the axes rectangle fig.canvas.blit(ax.bbox) l, b, w, h = fig.bbox.bounds w, h = int(w), int(h) im = fromstring(canvas.tostring_rgb(), dtype='uint8') im.shape = h, w, 3 res.append(im) fig.clf() plt.clf() plt.close() return array(res)
for img in tqdm(glob.glob(args.i + '/*jpg')): # make a Figure and attach it to a canvas. fig = Figure(figsize=(10, 10), dpi=100) canvas = FigureCanvasAgg(fig) path = os.path.join(os.getcwd(), img) l = len(args.i) img_x = image.load(path=path, size=(640, 480), batch=True) #plt.imshow(img_x[0]) result_img = model(img_x / 255.0) bar_values = (1.0 - image.radian_prob(result_img, n=args.s)) * 100 result_img = cv2.resize(result_img[0, :, :, 0], (640, 480)) result_img = image.normalize(result_img) * 255.0 save_path = img[:l] + '/depth_' + img[l + 1:] # Do some plotting here ax = fig.add_subplot(111) ax.bar(direction, bar_values) # Retrieve a view on the renderer buffer canvas.draw() buf = canvas.buffer_rgba() # convert to a NumPy array bar_arr = np.asarray(buf) bar_arr = cv2.resize(bar_arr, (640, 480))[:, :, :-1] hstack = np.hstack((cv2.cvtColor(result_img, cv2.COLOR_GRAY2RGB), bar_arr)) result_img = Image.fromarray(np.uint8(hstack)) result_img.save(save_path)
def _to_rgbarray(self): canvas = FigureCanvas(self.viewer) canvas.draw() # draw the canvas, cache the renderer image = np.frombuffer(canvas.tostring_rgb(), dtype='uint8') image = image.reshape(canvas.get_width_height()[::-1] + (3, )) return image
def main(argv): cwd = os.getcwd().replace("\\", "/") parser = argparse.ArgumentParser(description="Project 5: Visual odometry") parser.add_argument('--undistort', action="store_true", help="Undistort images") parser.add_argument('--built_in', action="store_true", help="Undistort images") args = parser.parse_args(argv) undistort = args.undistort builtIn = args.built_in if undistort: find_images(cwd) # Look for input images input_images_dir = "%s/undistorted_input_images" % cwd files = [] root = "" for tup in os.walk(input_images_dir): root, dirs, files = tup files.sort() # files = files[:100] sys.stdout.write("\nFound %d input images..." % len(files)) if len(files) == 0: sys.stdout.write("\n") exit(0) # Extract camera parameters and form camera matrix fx, fy, cx, cy, G_camera_image, LUT = ReadCameraModel( "%s/provided_data/model" % cwd) K = np.array([[fx, 0.0, cx], [0.0, fy, cy], [0.0, 0.0, 1.0]], dtype=np.float64) # Odometry calculations sys.stdout.write("\n") base_pose = np.identity(4) positions = np.zeros((0, 2), dtype=np.float64) trajectory = [] cv_base_pose = np.identity(4) # Read input images current_frame = cv2.imread("%s/%s" % (root, files[0]))[::2, ::2, :] height = current_frame.shape[0] width = current_frame.shape[1] H = np.identity(4) # for i in range(len(files) - 1): for i in range(300, 600, 2): # Find matches between two successive frames sys.stdout.write("\rProcessing frame %d." % i) next_frame = cv2.imread("%s/%s" % (root, files[i + 1]))[::2, ::2, :] # Use OpenCV's built-in epipolar geometry calculations if builtIn: feat_detector = eval("cv2.xfeatures2d.SIFT_create()" ) if False else cv2.ORB_create(nfeatures=500) kp1, dees1 = feat_detector.detectAndCompute(current_frame, None) kp2, dees2 = feat_detector.detectAndCompute(next_frame, None) bf = cv2.BFMatcher() matches = bf.match(dees1, dees2) U = [] V = [] for m in matches: pts_1 = kp1[m.queryIdx] x1, y1 = pts_1.pt pts_2 = kp2[m.trainIdx] x2, y2 = pts_2.pt U.append((x1, y1)) V.append((x2, y2)) U = np.array(U) V = np.array(V) E_cv, _ = cv2.findEssentialMat(U, V, focal=fx, pp=(cx, cy), method=cv2.RANSAC, prob=0.999, threshold=0.5) _, cv_R, cv_t, mask = cv2.recoverPose(E_cv, U, V, focal=fx, pp=(cx, cy)) if np.linalg.det(cv_R) < 0: cv_R = -cv_R cv_t = -cv_t new_pose = np.hstack((cv_R, cv_t)) new_pose = np.vstack((new_pose, np.array([0, 0, 0, 1]))) x1 = (H[0][3]) z1 = (H[2][3]) H = H.dot(new_pose) x = (H[0][3]) z = (H[2][3]) final_points = np.append(positions, np.array([[x, z]], dtype=np.float64), axis=0) # final_points = np.append(final_points, np.array([[x1, x, z1, z]], dtype=np.float64), axis=0) # img_current_frame = cv2.resize(img_current_frame, (0,0), fx=0.5, fy=0.5) # cv2.imshow('11',img_current_frame) # plt.plot([x1, x], [-z1, -z], 'go') # plt.pause(0.01) else: # Find fundamental matrix between two successive frames kp1, kp2, matches = findMatchesBetweenImages(current_frame, next_frame, num_matches=30) F = find_fundamental(kp1, kp2, matches, (height, width)) # Find inliers using fundamental matrix vector_1 = np.concatenate( (kp1, np.ones((kp1.shape[0], 1), dtype=np.float64)), axis=1) vector_1 = np.expand_dims(vector_1, axis=2) vector_2 = np.concatenate( (kp2, np.ones((kp1.shape[0], 1), dtype=np.float64)), axis=1) vector_2 = np.expand_dims(vector_2, axis=1) distance = np.matmul(np.matmul(vector_2, F), vector_1).squeeze() distance_indices = np.where(distance < 0.05) inliers_1 = kp1[distance_indices] inliers_2 = kp2[distance_indices] # Find essential matrix from fundamental matrix and camera calibration matrix E = find_essential(F, K) # Find best camera pose R, C = camera_pose_estimate(E) camera_pose = best_camera_pose(R, C, base_pose, inliers_1, inliers_2) camera_pose = np.concatenate( (camera_pose, np.array([[0, 0, 0, 1]], dtype=np.float)), axis=0) base_pose = np.dot(base_pose, camera_pose) positions = np.append(positions, np.array([x1, x, z1, z], dtype=np.float64), axis=0) # positions = np.append(positions, np.expand_dims(base_pose, axis=0), axis=0) # Output position # sys.stdout.write("\n") # for ii in base_pose: # sys.stdout.write("\n[ ") # for jj in ii: # sys.stdout.write(" %8.4f " % jj) # sys.stdout.write(" ]") # sys.stdout.write("\n") # match_images = show_matches(current_frame, next_frame, kp1, kp2, reduced=False) # if i == 50: # cv2.imwrite("match_image.png", match_images) current_frame = next_frame # plt.show() # Plot camera location using this implementation sys.stdout.write("\nProcessed a total of %d images." % len(files)) fig = plt.figure(figsize=(10.0, 8.0)) plt_width = int((fig.get_size_inches() * fig.get_dpi())[0]) plt_height = int((fig.get_size_inches() * fig.get_dpi())[1]) canvas = FigureCanvas(fig) plt.title("Camera locations") plt.xlabel("Frame Number (out of %d frames)" % positions.shape[0]) plt.ylabel("Distance") # t_axis = np.arange(positions.shape[0]) # locations = np.matmul(positions[:, :3, :3], np.expand_dims(positions[:, :3, 3], axis=2)) x_pos = positions[:, 0] y_pos = positions[:, 1] # z_pos = positions[:, 2, 3] plt.scatter(x_pos, y_pos, label="Location, built-in plot", s=1.0, c="#0000FF", alpha=0.4) plt.legend(loc="best", markerscale=8.0) canvas.draw() plot_image = np.frombuffer(canvas.tostring_rgb(), dtype=np.uint8).reshape(plt_height, plt_width, 3) cv2.imwrite("output_plot.png", plot_image) plt.close(fig) # Plot camera location using OpenCV built-in functionality # cv_fig = plt.figure(figsize=(10.0, 8.0)) # cv_plt_width = int((cv_fig.get_size_inches() * cv_fig.get_dpi())[0]) # cv_plt_height = int((cv_fig.get_size_inches() * cv_fig.get_dpi())[1]) # cv_canvas = FigureCanvas(cv_fig) # path_array = np.array(trajectory) # plt.title("Camera locations from OpenCV functions") # plt.xlabel("X") # plt.ylabel("Y") # plt.scatter(path_array[:, 0], path_array[:, 1], color='r') # cv_canvas.draw() # cv_plot_image = np.frombuffer(cv_canvas.tostring_rgb(), dtype=np.uint8).reshape(cv_plt_height, cv_plt_width, 3) # cv2.imwrite("built_in_plot.png", cv_plot_image) # plt.close(cv_fig) sys.stdout.write("\n")
def pixtegrate(func, bounds, npoints=10000, sleep=-1.): """ Args: func (function): Function to integrate bounds (tuple): 2-tuple for lower and upper bounds of range npoints (int): Number of points used to sample the function Returns: integral (float) """ xmin, xmax = bounds xvals = np.linspace(xmin, xmax, npoints) try: # First assume the function operates on arrays yvals = func(xvals) except: # Then assume it operates on a single element yvals = np.vectorize(func)(xvals) ymin, ymax = yvals.min(), yvals.max() # Make a figure with no padding/margins and a black background fig, ax = plt.subplots() canvas = FigureCanvas(fig) fig.subplots_adjust(left=0, bottom=0, right=1, top=1) fig.set_facecolor("black") ax.set_xmargin(0.) ax.set_ymargin(0.) ax.axis("off") # Color y>=0 red and y<0 blue ax.fill_between(xvals, yvals, where=(yvals >= 0.), edgecolor="r", facecolor="r", linewidth=0.) ax.fill_between(xvals, yvals, where=(yvals < 0.), edgecolor="b", facecolor="b", linewidth=0.) # Draw and count red/blue pixels canvas.draw() width, height = fig.get_size_inches() * fig.get_dpi() img = np.fromstring(canvas.tostring_rgb(), dtype='uint8').reshape(int(height), int(width), 3) nred = (img[:, :, 0] > 254).sum() nblue = (img[:, :, 2] > 254).sum() # Count filled fraction of image (red-blue), and normalize to actual bounding box integral = (nred - nblue) / (width * height) * ((ymax - ymin) * (xmax - xmin)) # Figure garbage collection plt.close() if sleep > 0.: time.sleep(sleep) return integral
def draw_map(self, shapefiles, **kwargs): """DRAWS THE MAP USED THE SPECIFIED SHAPEFILE. EACH SHAPE IS AN IMAGE. Args: shapefile: list of shapefiles (currently only uses the first entry) kwargs: color_range: list of field names to be used fro color scaling (currently only uses the first entry) Returns: N/A """ #Clear canvas, fill with water, read in shapefile self.delete('land', 'water') self.draw_water() sf = shapefiles[0] #Create colormap and norm for us in color scaling if 'color_range' in kwargs: color_range = kwargs.pop('color_range') color_name = color_range[0] if color_name != []: valuerange, minim, smallpos, strdict = self.colorrange( sf, color_name) colormap = cm.get_cmap('Reds', 48) norm = colors.Normalize(minim, minim + valuerange) #Draw each shape in shapefile for shaperec in sf.iterShapeRecords(): # convert shapefile geometries into shapely geometries # to extract the polygons of a multipolygon polygon = shapely.geometry.shape(shaperec.shape) if polygon.geom_type == 'Polygon': polygon = [polygon] for land in polygon: coordinates = sum((self.to_canvas_coordinates(*c) for c in land.exterior.coords), ()) #Set appropriate color if color_name != []: value = shaperec.record[color_name] if strdict != []: value = strdict[value] normvalue = norm(value) fill1 = colormap(normvalue)[0:3] fill = [int(x * 255) for x in fill1] alpha = 0.5 if self.null_zeros == 1: if value == 0: #alpha = 0 fill = self.winfo_rgb('black') alpha = 0.3 #elif value == 0 and self.location in ['Indonesia']: # fill = self.winfo_rgb('black') # alpha = 1 else: fill = self.winfo_rgb('green') alpha = 0 #Add the image to the list of images newimage = self.draw_polygon(coordinates, fill=fill, alpha=alpha) self.polyimages.append(newimage) #Add Colorbar to map in bottom left corner if color_name != []: plt.ioff() fig = plt.figure(figsize=(2, 8)) ax1 = fig.add_axes([0.05, 0.01, 0.5, 0.95]) canvas = FigureCanvas(fig) cb1 = colorbar.ColorbarBase(ax1, cmap=colormap, norm=norm, orientation='vertical') if self.color_title != []: cb1.set_label(self.color_title) else: cb1.set_label(color_name) canvas.draw() s, (width, height) = canvas.print_to_buffer() im = Image.frombytes("RGBA", (width, height), s) im = im.resize((2 * 65, 8 * 65), Image.ANTIALIAS) self.colorimage = ImageTk.PhotoImage(im) self.create_image(0, self.screenheight, image=self.colorimage, anchor='sw') plt.close(fig) del (canvas) del (s)
def render_state_tracking(self): """ Render state progression Returns ------- np.array RBG data of state tracking """ def plot_single(ax=None, index=None, title=None, x=False, y=False): if ax is None: plt.xlabel("Episode") plt.ylabel("State") elif x and y: ax.set_ylabel("State") ax.set_xlabel("Episode") elif x: ax.set_xlabel("Episode") elif y: ax.set_ylabel("State") if index is not None: ys = [state[index] for state in self.overall_states] else: ys = self.overall_states if ax is None: p = plt.plot( np.arange(len(self.overall_states)), ys, label="Episode state", color="g", ) else: p = ax.plot( np.arange(len(self.overall_states)), ys, label="Episode state", color="g", ) p2 = None if self.state_interval: if index is not None: y_ints = [] for interval in self.state_intervals: y_ints.append([state[index] for state in interval]) else: y_ints = self.state_intervals if ax is None: p2 = plt.plot( np.arange(len(self.state_intervals)), [np.mean(interval) for interval in y_ints], label="Mean interval state", color="orange", ) plt.legend(loc="upper left") else: p2 = ax.plot( np.arange(len(self.state_intervals)) * self.state_interval, [np.mean(interval) for interval in y_ints], label="Mean interval state", color="orange", ) ax.legend(loc="upper left") return p, p2 if self.state_type == spaces.Discrete: figure = plt.figure(figsize=(20, 20)) canvas = FigureCanvas(figure) p, p2 = plot_single() canvas.draw() elif self.state_type == spaces.Dict: raise NotImplementedError elif self.state_type == spaces.Tuple: raise NotImplementedError elif (self.state_type == spaces.MultiDiscrete or self.state_type == spaces.MultiBinary or self.state_type == spaces.Box): if self.state_type == spaces.MultiDiscrete: state_length = len(self.env.observation_space.nvec) elif self.state_type == spaces.MultiBinary: state_length = self.env.observation_space.n else: state_length = len(self.env.observation_space.high) if state_length == 1: figure = plt.figure(figsize=(20, 20)) canvas = FigureCanvas(figure) p, p2 = plot_single() elif state_length < 5: dim = 1 figure, axarr = plt.subplots(state_length) else: dim = state_length % 4 figure, axarr = plt.subplots(state_length % 4, state_length // dim) figure.suptitle("State over time") canvas = FigureCanvas(figure) for i in range(state_length): if state_length == 1: continue x = False if i % dim == dim - 1: x = True if state_length < 5: p, p2 = plot_single(axarr[i], i, y=True, x=x) else: y = i % state_length // dim == 0 p, p2 = plot_single(axarr[i % dim, i // dim], i, x=x, y=y) canvas.draw() width, height = figure.get_size_inches() * figure.get_dpi() img = np.fromstring(canvas.tostring_rgb(), dtype="uint8").reshape(int(height), int(width), 3) return img
def fig2img(fig): canvas = FigureCanvasAgg(fig) canvas.draw() s, (width, height) = canvas.print_to_buffer() im = Image.frombytes("RGBA", (width, height), s) return im
def addshapes(self, sf, **kwargs): """ADD ADDITIONAL SHAPES TO MAP, AS TK POLYGONS (NOT IMAGES) Args: sf: pyshp shapefile (not a filename!) kwargs: outline: string description of color (e.g. 'black'). Default is black color_range: name of field to be used for color scaling. Default is a constant orange color. Returns: N/A """ #Select the outline color if 'outline' in kwargs: outline = kwargs.pop('outline') else: outline = 'black' #Define colormap if 'color_range' in kwargs: color_name = kwargs.pop('color_range') if color_name != []: valuerange, minim, smallpos, strdict = self.colorrange( sf, color_name) colormap = cm.get_cmap('RdYlGn', 48) norm = colors.Normalize(minim, minim + valuerange) #Select the longform title for use on the colorbar if 'color_title' in kwargs: colortitle = kwargs.pop('color_title') else: colortitle = [] #Draw each shape as canvas polygon for shaperec in sf.iterShapeRecords(): # convert shapefile geometries into shapely geometries # to extract the polygons of a multipolygon polygon = shaperec.shape polygon = shapely.geometry.shape(polygon) #Assign color to each shape based on colornorm if color_name != []: value = shaperec.record[color_name] if strdict != []: value = strdict[value] normvalue = norm(value) fill1 = colormap(normvalue)[0:3] fill = [int(x * 255) for x in fill1] fill = self.rgb2hex(fill) else: fill = 'orange' #Create the shape if polygon.geom_type == 'Polygon': polygon = [polygon] for land in polygon: self.create_polygon(sum((self.to_canvas_coordinates(*c) for c in land.exterior.coords), ()), fill=fill, outline=outline, tags=('land', )) #Add Colorbar to map in bottom left corner if color_name != []: plt.ioff() fig = plt.figure(figsize=(2, 8)) ax1 = fig.add_axes([0.05, 0.01, 0.5, 0.95]) canvas = FigureCanvas(fig) cb1 = colorbar.ColorbarBase(ax1, cmap=colormap, norm=norm, orientation='vertical') if colortitle != []: cb1.set_label(colortitle) else: cb1.set_label(color_name) canvas.draw() s, (width, height) = canvas.print_to_buffer() im = Image.frombytes("RGBA", (width, height), s) im = im.resize((2 * 65, 8 * 65), Image.ANTIALIAS) self.colorimage2 = ImageTk.PhotoImage(im) self.create_image(2 * 65, self.screenheight, image=self.colorimage2, anchor='sw') plt.close(fig) del (canvas) del (s)
def create_frame(frame, data, frame_idx, start_frame, end_frame): print_names = data["names"] loss_names = data["loss_names"] plt.style.use('dark_background') fig = plt.figure(figsize=(10, 4.5), dpi=100) canvas = FigureCanvas(fig) main_fig = gridspec.GridSpec(2, 1, wspace=0.0, hspace=0.0, height_ratios=[6, 1]) # figure handle for the video frames ax = plt.Subplot(fig, main_fig[0]) ax.imshow(frame) ax.set_xticks([]) ax.set_yticks([]) fig.add_subplot(ax) # frame_fig = gridspec.GridSpecFromSubplotSpec( # 1, 1, subplot_spec=main_fig[0], wspace=0.0, hspace=0.0) # ax = plt.Subplot(fig, frame_fig[0]) # ax.imshow(frame) # ax.set_xticks([]) # ax.set_yticks([]) # fig.add_subplot(ax) # create the handles for the ethogram style plots inner = gridspec.GridSpecFromSubplotSpec(len(print_names), 1, subplot_spec=main_fig[1], wspace=0.0, hspace=0.0) label_colors = [ 'cyan', 'yellow', 'lime', 'red', 'magenta', 'lavenderblush' # 'tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown' ] label_names = ["Lift", "Hand", "Grab", "Supinate", "At Mouth", "Chew"] # ylabels = ["ground truth", "wasserstein"] bar_height = 0.01 ylabels = print_names # data_mat = [data["gt"], data["pred"]] num_frames = data["gt"].shape[0] # for j in range(len(print_names)): for j in range(1): loss_key = loss_names[j] ax = plt.Subplot(fig, inner[j]) # create the prediction bar for k in range(num_frames): if any(data[loss_key][k, :] > 0): idx = numpy.argmax(data[loss_key][k, :]) # ax.plot([k, k], [0, 1], color=(0,1.0,0.0,1.0)) # try: ax.plot([k, k], [0, bar_height], label_colors[idx]) # except: # import pdb; pdb.set_trace() # plot frame indicator ax.plot([start_frame + frame_idx, start_frame + frame_idx], [0, bar_height], 'snow') # [0, bar_height], '0.6') # ax.plot(data["lift"][:, j+1]) # ax.set_ylabel(ylabels[j]) ax.set_ylim([0, bar_height]) # ax.set_xlim([0, num_frames]) ax.set_xlim([start_frame, end_frame]) ax.set_xticks([]) # if j != len(print_names) - 1: # ax.set_xticks([]) ax.set_yticks([]) fig.add_subplot(ax) plt.tight_layout() # main_fig.update(wspace=0.05, hspace=0.05) canvas.draw() # draw the canvas, cache the renderer s, (width, height) = canvas.print_to_buffer() # Option 2a: Convert to a NumPy array. X = numpy.fromstring(s, numpy.uint8).reshape((height, width, 4)) plt.close('all') # ... bgr ... bgr_frame = X[:, :, :3].copy() # copy and get rid of alpha chan bgr_frame[:, :, 0] = X[:, :, 2] bgr_frame[:, :, 2] = X[:, :, 0] # add the time to the frame. # import pdb; pdb.set_trace() font = cv2.FONT_HERSHEY_SIMPLEX seconds_count = (frame_idx / 500.0) # temp = numpy.floor(seconds_count / 10) temp = numpy.floor(frame_idx / 10) offset = 0 while temp > 0: offset = offset + 1 temp = numpy.floor(temp / 10) # xoffset = 870 - offset * 20 # cv2.putText(bgr_frame, "%.2f" % seconds_count, (xoffset, 360), font, 1, (255, 255, 0), 2) xoffset = 930 - offset * 15 # cv2.putText(bgr_frame, "%.3f" % seconds_count, (xoffset, 360), font, 0.75, (255, 255, 255), 1) cv2.putText(bgr_frame, "%d" % frame_idx, (xoffset, 360), font, 0.75, (255, 255, 255), 1) # add label texts? # cv2.putText(bgr_frame, "GT", (270, 290), font, 0.5, (255, 255, 255), 1) # cv2.putText(bgr_frame, "Wasserstein", (200, 310), font, 0.5, (255, 255, 255), 1) # cv2.putText(bgr_frame, "Matching", (220, 330), font, 0.5, (255, 255, 255), 1) # cv2.putText(bgr_frame, "MSE", (255, 350), font, 0.5, (255, 255, 255), 1) cv2.putText(bgr_frame, "GT", (270, 350), font, 0.5, (255, 255, 255), 1) # for each label, add the label names base_x = 310 y_coords = [290, 310, 330, 350] y_coords = [350, 310, 330, 350] x_offsets = [310, 345, 395, 440, 515, 595] label_bgr = [(255, 255, 0), (0, 255, 255), (0, 255, 0), (0, 0, 255), (255, 0, 255), (245, 240, 255)] alpha = 0 # for i in range(len(y_coords)): for i in range(1): loss_key = loss_names[i] for j in range(len(label_colors)): # for the loss, find the closest label idx from the current # frame. label_idxs = numpy.argwhere(data[loss_key][:, j]).flatten() min_dist = numpy.inf for k in range(len(label_idxs)): if numpy.abs((frame_idx + start_frame) - label_idxs[k]) < min_dist: min_dist = numpy.abs((frame_idx + start_frame) - label_idxs[k]) alpha = 1 - numpy.min([30, min_dist]) / 30 color = list(label_bgr[j]) for k in range(len(color)): color[k] = int(color[k] * alpha) color = tuple(color) # if min_dist < 30: # import pdb; pdb.set_trace() # import pdb; pdb.set_trace() if alpha > 0: cv2.putText(bgr_frame, label_names[j], (x_offsets[j], y_coords[i]), font, 0.5, color, 1) return bgr_frame
def plt_plot_filters_fast(y, x, shape, rows, cols, title=None, x_axis_label=None, y_axis_label=None, share_axes=True, log_scale=0): res = [] shape = (ceil(shape[1] / 80 / cols), ceil(shape[0] / 80 / rows)) fig, ax = plt.subplots(1, 1, figsize=shape) canvas = FigureCanvas(fig) # ax.set_aspect('equal') if share_axes: if x is not None: min_x, max_x = min(x), max(x) else: min_x, max_x = 0, y.shape[1] min_y, max_y = y.min(), y.max() ax.set_xlim(min_x, max_x) ax.set_ylim(min_y, max_y) # ax.hold(True) plt.subplots_adjust(left=0.185, bottom=0.125, right=0.98, top=0.98) # plt.show(False) # plt.draw() # background = fig.canvas.copy_from_bbox(ax.bbox) # points = ax.plot(x[0], linewidth=1)[0] for i in xrange(y.shape[0]): if x is not None: if log_scale == 1: ax.semilogy(x, y[i], linewidth=1) else: ax.plot(x, y[i], linewidth=1) else: if log_scale == 1: ax.semilogy(y[i], linewidth=1) else: ax.plot(y[i], linewidth=1) if x_axis_label is not None: ax.set_xlabel(x_axis_label) if y_axis_label is not None: ax.set_ylabel(y_axis_label) if title is not None: plt.title(title) # plt.autoscale(enable=True, axis='y', tight=True) # plt.tight_layout() # Turn off axes and set axes limits # ax.axis('off') canvas.draw() # draw the canvas, cache the renderer l, b, w, h = fig.bbox.bounds w, h = int(w), int(h) im = fromstring(canvas.tostring_rgb(), dtype='uint8') im.shape = h, w, 3 res.append(im) # ax.cla() fig.clf() return array(res)