def plot(self, im): """ Plots and call auxfun_drag class for moving and removing points. """ # small hack in case there are any 0 intensity images! img = io.imread(im) maxIntensity = np.max(img) if maxIntensity == 0: maxIntensity = np.max(img) + 255 divider = make_axes_locatable(self.axes) cax = divider.append_axes("right", size="5%", pad=0.05) self.drs = [] if ( self.visualization_rdb.GetSelection() == 0 ): # i.e. for color scheme for individuals self.Colorscheme = visualization.get_cmap( len(self.individual_names), self.cfg["colormap"] ) self.norm, self.colorIndex = self.image_panel.getColorIndices( im, self.individual_names ) cbar = self.figure.colorbar( self.ax, cax=cax, spacing="proportional", ticks=self.colorIndex ) cbar.set_ticklabels(self.individual_names) else: # i.e. for color scheme for all bodyparts self.Colorscheme = visualization.get_cmap( len(self.all_bodyparts), self.cfg["colormap"] ) self.norm, self.colorIndex = self.image_panel.getColorIndices( im, self.all_bodyparts ) cbar = self.figure.colorbar( self.ax, cax=cax, spacing="proportional", ticks=self.colorIndex ) cbar.set_ticklabels(self.all_bodyparts) for ci, ind in enumerate(self.individual_names): col_idx = ( 0 # variable for iterating through the colorscheme for all bodyparts ) image_points = [] if ind == "single": if self.visualization_rdb.GetSelection() == 0: for c, bp in enumerate(self.uniquebodyparts): self.points = [ self.Dataframe[self.scorer][ind][bp]["x"].values[self.iter], self.Dataframe[self.scorer][ind][bp]["y"].values[self.iter], self.Dataframe[self.scorer][ind][bp]["likelihood"].values[ self.iter ], ] self.likelihood = self.points[2] # fix move to corner if self.move2corner: ny, nx = np.shape(img)[0], np.shape(img)[1] if self.points[0] > nx or self.points[0] < 0: print("fixing x for ", bp) self.points[0] = self.center[0] if self.points[1] > ny or self.points[1] < 0: print("fixing y for ", bp) self.points[1] = self.center[1] if self.likelihood < self.threshold: self.circle = [ patches.Circle( (self.points[0], self.points[1]), radius=self.markerSize, facecolor="None", edgecolor=self.Colorscheme(ci), alpha=self.alpha, ) ] else: self.circle = [ patches.Circle( (self.points[0], self.points[1]), radius=self.markerSize, fc=self.Colorscheme(ci), alpha=self.alpha, ) ] self.axes.add_patch(self.circle[0]) self.dr = auxfun_drag.DraggablePoint( self.circle[0], bp, individual_names=ind, likelihood=self.likelihood, ) self.dr.connect() self.dr.coords = MainFrame.getLabels( self, self.iter, ind, self.uniquebodyparts )[c] self.drs.append(self.dr) self.updatedCoords.append(self.dr.coords) else: for c, bp in enumerate(self.uniquebodyparts): self.points = [ self.Dataframe[self.scorer][ind][bp]["x"].values[self.iter], self.Dataframe[self.scorer][ind][bp]["y"].values[self.iter], self.Dataframe[self.scorer][ind][bp]["likelihood"].values[ self.iter ], ] self.likelihood = self.points[2] # fix move to corner if self.move2corner: ny, nx = np.shape(img)[0], np.shape(img)[1] if self.points[0] > nx or self.points[0] < 0: print("fixing x for ", bp) self.points[0] = self.center[0] if self.points[1] > ny or self.points[1] < 0: print("fixing y for ", bp) self.points[1] = self.center[1] if self.likelihood < self.threshold: self.circle = [ patches.Circle( (self.points[0], self.points[1]), radius=self.markerSize, fc="None", edgecolor=self.Colorscheme(col_idx), alpha=self.alpha, ) ] else: self.circle = [ patches.Circle( (self.points[0], self.points[1]), radius=self.markerSize, fc=self.Colorscheme(col_idx), alpha=self.alpha, ) ] self.axes.add_patch(self.circle[0]) col_idx = col_idx + 1 self.dr = auxfun_drag.DraggablePoint( self.circle[0], bp, individual_names=ind, likelihood=self.likelihood, ) self.dr.connect() self.dr.coords = MainFrame.getLabels( self, self.iter, ind, self.uniquebodyparts )[c] self.drs.append(self.dr) self.updatedCoords.append(self.dr.coords) else: if self.visualization_rdb.GetSelection() == 0: for c, bp in enumerate(self.multianimalbodyparts): self.points = [ self.Dataframe[self.scorer][ind][bp]["x"].values[self.iter], self.Dataframe[self.scorer][ind][bp]["y"].values[self.iter], self.Dataframe[self.scorer][ind][bp]["likelihood"].values[ self.iter ], ] self.likelihood = self.points[2] # fix move to corner if self.move2corner: ny, nx = np.shape(img)[0], np.shape(img)[1] if self.points[0] > nx or self.points[0] < 0: print("fixing x for ", bp) self.points[0] = self.center[0] if self.points[1] > ny or self.points[1] < 0: print("fixing y for ", bp) self.points[1] = self.center[1] if self.likelihood < self.threshold: self.circle = [ patches.Circle( (self.points[0], self.points[1]), radius=self.markerSize, fc="None", edgecolor=self.Colorscheme(ci), alpha=self.alpha, ) ] else: self.circle = [ patches.Circle( (self.points[0], self.points[1]), radius=self.markerSize, fc=self.Colorscheme(ci), alpha=self.alpha, ) ] self.axes.add_patch(self.circle[0]) self.dr = auxfun_drag.DraggablePoint( self.circle[0], bp, individual_names=ind, likelihood=self.likelihood, ) self.dr.connect() self.dr.coords = MainFrame.getLabels( self, self.iter, ind, self.multianimalbodyparts )[c] self.drs.append(self.dr) self.updatedCoords.append(self.dr.coords) else: for c, bp in enumerate(self.multianimalbodyparts): self.points = [ self.Dataframe[self.scorer][ind][bp]["x"].values[self.iter], self.Dataframe[self.scorer][ind][bp]["y"].values[self.iter], self.Dataframe[self.scorer][ind][bp]["likelihood"].values[ self.iter ], ] self.likelihood = self.points[2] # fix move to corner if self.move2corner: ny, nx = np.shape(img)[0], np.shape(img)[1] if self.points[0] > nx or self.points[0] < 0: print("fixing x for ", bp) self.points[0] = self.center[0] if self.points[1] > ny or self.points[1] < 0: print("fixing y for ", bp) self.points[1] = self.center[1] if self.likelihood < self.threshold: self.circle = [ patches.Circle( (self.points[0], self.points[1]), radius=self.markerSize, fc="None", edgecolor=self.Colorscheme(col_idx), alpha=self.alpha, ) ] else: self.circle = [ patches.Circle( (self.points[0], self.points[1]), radius=self.markerSize, fc=self.Colorscheme(col_idx), alpha=self.alpha, ) ] self.axes.add_patch(self.circle[0]) col_idx = col_idx + 1 self.dr = auxfun_drag.DraggablePoint( self.circle[0], bp, individual_names=ind, likelihood=self.likelihood, ) self.dr.connect() self.dr.coords = MainFrame.getLabels( self, self.iter, ind, self.multianimalbodyparts )[c] self.drs.append(self.dr) self.updatedCoords.append(self.dr.coords) self.figure.canvas.draw()
def plot_labels(self): """ Plots the labels of the analyzed video """ self.vid.set_to_frame(self.currFrame) frame = self.vid.read_frame() if frame is not None: divider = make_axes_locatable(self.axes) cax = divider.append_axes("right", size="5%", pad=0.05) if self.multianimal: # take into account of all the bodyparts for the colorscheme. Sort the bodyparts to have same order as in the config file self.all_bodyparts = np.array(self.multianimalbodyparts + self.uniquebodyparts) _, return_idx = np.unique(self.all_bodyparts, return_index=True) self.all_bodyparts = list( self.all_bodyparts[np.sort(return_idx)]) if (self.visualization_rdb.GetSelection() == 0 ): # i.e. for color scheme for individuals self.Colorscheme = visualization.get_cmap( len(self.individual_names), self.cfg["colormap"]) self.norm, self.colorIndex = self.image_panel.getColorIndices( frame, self.individual_names) cbar = self.figure.colorbar(self.ax, cax=cax, spacing="proportional", ticks=self.colorIndex) cbar.set_ticklabels(self.individual_names) else: # i.e. for color scheme for all bodyparts self.Colorscheme = visualization.get_cmap( len(self.all_bodyparts), self.cfg["colormap"]) self.norm, self.colorIndex = self.image_panel.getColorIndices( frame, self.all_bodyparts) cbar = self.figure.colorbar(self.ax, cax=cax, spacing="proportional", ticks=self.colorIndex) cbar.set_ticklabels(self.all_bodyparts) for ci, ind in enumerate(self.individual_names): col_idx = ( 0 ) # variable for iterating through the colorscheme for all bodyparts image_points = [] if ind == "single": if self.visualization_rdb.GetSelection() == 0: for c, bp in enumerate(self.uniquebodyparts): pts = self.Dataframe.xs( (ind, bp), level=("individuals", "bodyparts"), axis=1, ).values self.circle = patches.Circle( pts[self.currFrame, :2], radius=self.markerSize, fc=self.Colorscheme(ci), alpha=self.alpha, ) self.axes.add_patch(self.circle) else: for c, bp in enumerate(self.uniquebodyparts): pts = self.Dataframe.xs( (ind, bp), level=("individuals", "bodyparts"), axis=1, ).values self.circle = patches.Circle( pts[self.currFrame, :2], radius=self.markerSize, fc=self.Colorscheme(col_idx), alpha=self.alpha, ) self.axes.add_patch(self.circle) col_idx = col_idx + 1 else: if self.visualization_rdb.GetSelection() == 0: for c, bp in enumerate(self.multianimalbodyparts): pts = self.Dataframe.xs( (ind, bp), level=("individuals", "bodyparts"), axis=1, ).values self.circle = patches.Circle( pts[self.currFrame, :2], radius=self.markerSize, fc=self.Colorscheme(ci), alpha=self.alpha, ) self.axes.add_patch(self.circle) else: for c, bp in enumerate(self.multianimalbodyparts): pts = self.Dataframe.xs( (ind, bp), level=("individuals", "bodyparts"), axis=1, ).values self.circle = patches.Circle( pts[self.currFrame, :2], radius=self.markerSize, fc=self.Colorscheme(col_idx), alpha=self.alpha, ) self.axes.add_patch(self.circle) col_idx = col_idx + 1 self.figure.canvas.draw() else: self.norm, self.colorIndex = self.image_panel.getColorIndices( frame, self.bodyparts) cbar = self.figure.colorbar(self.ax, cax=cax, spacing="proportional", ticks=self.colorIndex) cbar.set_ticklabels(self.bodyparts) for bpindex, bp in enumerate(self.bodyparts): color = self.colormap(self.norm(self.colorIndex[bpindex])) self.points = [ self.Dataframe.xs((bp, "x"), level=(-2, -1), axis=1).values[self.currFrame], self.Dataframe.xs((bp, "y"), level=(-2, -1), axis=1).values[self.currFrame], 1.0, ] circle = [ patches.Circle( (self.points[0], self.points[1]), radius=self.markerSize, fc=color, alpha=self.alpha, ) ] self.axes.add_patch(circle[0]) self.figure.canvas.draw() else: print("Invalid frame")
def ubercal_skysub(tims, targetwcs, survey, brickname, bands, mp, subsky_radii=None, plots=False, plots2=False, ps=None, verbose=False): """With the ubercal option, we (1) read the full-field mosaics ('bandtims') for a given bandpass and put them all on the same 'system' using the overlapping pixels; (2) apply the derived corrections to the in-field 'tims'; (3) build the coadds (per bandpass) from the 'tims'; and (4) subtract the median sky from the mosaic (after aggressively masking objects and reference sources). """ from tractor.sky import ConstantSky from legacypipe.reference import get_reference_sources, get_reference_map from legacypipe.coadds import make_coadds from legacypipe.survey import get_rgb, imsave_jpeg from astropy.stats import sigma_clipped_stats if plots or plots2: import os import matplotlib.pyplot as plt if plots: import matplotlib.patches as patches refs, _ = get_reference_sources(survey, targetwcs, targetwcs.pixel_scale(), ['r'], tycho_stars=False, gaia_stars=False, large_galaxies=True, star_clusters=False) pixscale = targetwcs.pixel_scale() width = targetwcs.get_width() * pixscale / 3600 # [degrees] bb, bbcc = targetwcs.radec_bounds(), targetwcs.radec_center( ) # [degrees] pad = 0.5 * width # [degrees] delta = np.max((np.diff(bb[0:2]), np.diff(bb[2:4]))) / 2 + pad / 2 xlim = bbcc[0] - delta, bbcc[0] + delta ylim = bbcc[1] - delta, bbcc[1] + delta plt.clf() _, allax = plt.subplots(1, 3, figsize=(12, 5), sharey=True, sharex=True) for ax, band in zip(allax, ('g', 'r', 'z')): ax.set_xlabel('RA (deg)') ax.text(0.9, 0.05, band, ha='center', va='bottom', transform=ax.transAxes, fontsize=18) if band == 'g': ax.set_ylabel('Dec (deg)') ax.get_xaxis().get_major_formatter().set_useOffset(False) # individual CCDs these = np.where([tim.band == band for tim in tims])[0] col = plt.cm.Set1(np.linspace(0, 1, len(tims))) for ii, indx in enumerate(these): tim = tims[indx] #wcs = tim.subwcs wcs = tim.imobj.get_wcs() cc = wcs.radec_bounds() ax.add_patch( patches.Rectangle((cc[0], cc[2]), cc[1] - cc[0], cc[3] - cc[2], fill=False, lw=2, edgecolor=col[these[ii]], label='ccd{:02d}'.format(these[ii]))) ax.legend(ncol=2, frameon=False, loc='upper left', fontsize=10) # output mosaic footprint cc = targetwcs.radec_bounds() ax.add_patch( patches.Rectangle((cc[0], cc[2]), cc[1] - cc[0], cc[3] - cc[2], fill=False, lw=2, edgecolor='k')) if subsky_radii: racen, deccen = targetwcs.crval for rad in subsky_radii: ax.add_patch( patches.Circle((racen, deccen), rad / 3600, fill=False, edgecolor='black', lw=2)) else: for gal in refs: ax.add_patch( patches.Circle((gal.ra, gal.dec), gal.radius, fill=False, edgecolor='black', lw=2)) ax.set_ylim(ylim) ax.set_xlim(xlim) ax.invert_xaxis() ax.set_aspect('equal') plt.subplots_adjust(bottom=0.12, wspace=0.05, left=0.12, right=0.97, top=0.95) plt.savefig( os.path.join(survey.output_dir, 'metrics', 'cus', '{}-ccdpos.jpg'.format(ps.basefn))) if plots: plt.figure(figsize=(8, 6)) mods = [] for tim in tims: imcopy = tim.getImage().copy() tim.sky.addTo(imcopy, -1) mods.append(imcopy) C = make_coadds(tims, bands, targetwcs, mods=mods, callback=None, mp=mp) imsave_jpeg(os.path.join(survey.output_dir, 'metrics', 'cus', '{}-pipelinesky.jpg'.format(ps.basefn)), get_rgb(C.comods, bands), origin='lower') refs, _ = get_reference_sources(survey, targetwcs, targetwcs.pixel_scale(), ['r'], tycho_stars=True, gaia_stars=True, large_galaxies=True, star_clusters=True) refmask = get_reference_map(targetwcs, refs) == 0 # True=skypix skydict = {'radii': subsky_radii} allbands = np.array([tim.band for tim in tims]) for band in sorted(set(allbands)): print('Working on band {}'.format(band)) I = np.where(allbands == band)[0] bandtims = [ tims[ii].imobj.get_tractor_image(gaussPsf=True, pixPsf=False, subsky=False, dq=True, apodize=False) for ii in I ] # Derive the ubercal correction and then apply it. x = coadds_ubercal(bandtims, coaddtims=[tims[ii] for ii in I], plots=plots, plots2=plots2, ps=ps, verbose=True) skydict[band] = {'ccds': [tims[ii].name for ii in I], 'delta': x} # Apply the correction and return the tims for jj, (correction, ii) in enumerate(zip(x, I)): tims[ii].data += correction tims[ii].sky = ConstantSky(0.0) # Also correct the full-field mosaics bandtims[jj].data += correction bandtims[jj].sky = ConstantSky(0.0) ## Check-- #for jj, correction in enumerate(x): # fulltims[jj].data += correction #newcorrection = coadds_ubercal(fulltims) #print(newcorrection) H, W, pixscale = targetwcs.get_height(), targetwcs.get_width( ), targetwcs.pixel_scale() C = make_coadds(tims, bands, targetwcs, callback=None, sbscale=False, mp=mp) for coimg, coiv, band in zip(C.coimgs, C.cowimgs, bands): if subsky_radii: # Estimate the sky background from an annulus surrounding the object # (assumed to be at the center of the mosaic, targetwcs.crval). _, x0, y0 = targetwcs.radec2pixelxy(targetwcs.crval[0], targetwcs.crval[1]) xcen, ycen = np.round(x0 - 1).astype('int'), np.round(y0 - 1).astype('int') ymask, xmask = np.ogrid[-ycen:H - ycen, -xcen:W - xcen] #cenmask = (xmask**2 + ymask**2) <= (subsky_radii[0] / pixscale)**2 # True=object pixels inmask = (xmask**2 + ymask**2) <= (subsky_radii[1] / pixscale)**2 outmask = (xmask**2 + ymask**2) <= (subsky_radii[2] / pixscale)**2 skymask = (outmask * 1 - inmask * 1) == 1 # True=skypix # Find and mask objects, then get the sky. skypix = _build_objmask(coimg, coiv, refmask * (coiv > 0)) skypix = np.logical_and(skypix, skymask) #plt.imshow(skypix, origin='lower') ; plt.savefig('junk.png') else: skypix = refmask * (coiv > 0) skypix = _build_objmask(coimg, coiv, skypix) skymean, skymedian, skysig = sigma_clipped_stats( coimg, mask=np.logical_not(skypix), sigma=3.0) skydict[band].update({ 'mean': skymean, 'median': skymedian, 'sigma': skysig, 'npix': np.sum(skypix) }) I = np.where(allbands == band)[0] #print('Band', band, 'Coadd sky:', skymedian) if plots2: plt.clf() plt.hist(coimg.ravel(), bins=50, range=(-3, 3), density=True) plt.axvline(skymedian, color='k') for ii in I: #print('Tim', tims[ii], 'median', np.median(tims[ii].data)) plt.hist((tims[ii].data - skymedian).ravel(), bins=50, range=(-3, 3), histtype='step', density=True) plt.title('Band %s: tim pix & skymedian' % band) ps.savefig() # Produce skymedian-subtracted, masked image for later RGB plot coimg -= skymedian coimg[~skypix] = 0. #coimg[np.logical_not(skymask * (coiv > 0))] = 0. for ii in I: tims[ii].data -= skymedian #print('Tim', tims[ii], 'after subtracting skymedian: median', np.median(tims[ii].data)) if plots2: plt.clf() plt.imshow(get_rgb(C.coimgs, bands), origin='lower', interpolation='nearest') ps.savefig() for band in bands: for tim in tims: if tim.band != band: continue plt.clf() C = make_coadds([tim], bands, targetwcs, callback=None, sbscale=False, mp=mp) plt.imshow(get_rgb(C.coimgs, bands).sum(axis=2), cmap='gray', interpolation='nearest', origin='lower') plt.title('Band %s: tim %s' % (band, tim.name)) ps.savefig() if plots: C = make_coadds(tims, bands, targetwcs, callback=None, mp=mp) imsave_jpeg(os.path.join(survey.output_dir, 'metrics', 'cus', '{}-customsky.jpg'.format(ps.basefn)), get_rgb(C.coimgs, bands), origin='lower') if plots2: plt.clf() for coimg, band in zip(C.coimgs, bands): plt.hist(coimg.ravel(), bins=50, range=(-0.5, 0.5), histtype='step', label=band) plt.legend() plt.title('After adjustment: coadds (sb scaled)') ps.savefig() return tims, skydict
def show_images(img, xy='nan', xys='nan', xys2='nan', xys3='nan'): dim = len(img) sqd = int(np.sqrt(dim)) sqx = sqd + (dim - sqd * sqd > 0) sqy = sqd + (dim - sqd * sqd > sqd) fig, ax = plt.subplots(sqx, sqy) for i in range(dim): axi = ax[i % sqx, int(i / sqx)] axi.grid(False) axi.imshow(img[i], cmap="Greys") if xy != 'nan': xy_i = xy[i] for j in range(0, len(xy_i), 2): x, y = xy_i[j:j + 2] rect = patches.Rectangle((y - 1, x - 1), 2, 2, linewidth=1, edgecolor='r', fill=False) axi.add_patch(rect) if xys != 'nan': xys_i = xys[i] for j in range(0, len(xys_i), 3): x, y, s = xys_i[j:j + 3] rect = patches.Rectangle((y - 1, x - 1), 2, 2, linewidth=1, edgecolor='r', fill=False) circle = patches.Circle((y, x), s / 10, fill=False, edgecolor="b") axi.add_patch(rect) axi.add_patch(circle) if xys2 != 'nan': xys2_i = xys2[i] for j in range(0, len(xys2_i), 4): x, y, s1, s2 = xys2_i[j:j + 4] rect = patches.Rectangle((y - 1, x - 1), 2, 2, linewidth=1, edgecolor='r', fill=False) axi.add_patch(rect) ellipse = patches.Ellipse((y, x), s2, s1, 0, fill=False, edgecolor="b") axi.add_patch(ellipse) if xys3 != 'nan': xys3_i = xys3[i] for j in range(0, len(xys3_i), 5): x, y, s1, s2, s3 = xys3_i[j:j + 5] rect = patches.Rectangle((y - 1, x - 1), 2, 2, linewidth=1, edgecolor='r', fill=False) axi.add_patch(rect) ellipse = patches.Ellipse((y, x), s2 / 10, s1 / 10, s3 * 180 / np.pi, fill=False, edgecolor="b") axi.add_patch(ellipse) plt.draw() return fig
def main(): # Get arguments: folders = sys.argv[1] merge_dirfile = sys.argv[2] png_path = sys.argv[3] stack_path = sys.argv[4] stack_rootname = sys.argv[5] box_size = int(sys.argv[6]) phaori_shift = np.array([float(sys.argv[7].split(',')[0]), float(sys.argv[7].split(',')[1])]) # How many degrees to offset the phase origins in order to get a protein in the center of the particle for a zero-tilt image apix = float(sys.argv[8]) # pixel size in Angstroems microscope_voltage = float(sys.argv[9]) # microscope voltage in kV microscope_cs = float(sys.argv[10]) # microscope spherical aberration in mm ampcontrast = 1.0-float(sys.argv[11])**2 # amplitude contrast of CTF (obtained here from phase contrast) magnification = float(sys.argv[12]) sigcc = float(sys.argv[13]) if sys.argv[14] == 'y': invert_micrograph = True else: invert_micrograph = False if sys.argv[15] == 'y': normalize_box = True else: normalize_box = False if sys.argv[16] == 'y': calculate_defocus_tilted = True else: calculate_defocus_tilted = False if sys.argv[17] == 'y': save_phase_flipped = True else: save_phase_flipped = False if sys.argv[18] == 'y': save_ctf_multiplied = True else: save_ctf_multiplied = False if sys.argv[19] == 'y': save_wiener_filtered = True else: save_wiener_filtered = False wiener_constant = float(sys.argv[20]) sigma = float(sys.argv[21]) # Sigma for normalization of the windowed images (if normalize_box == True) sigma_rad = float(sys.argv[22]) # Radius for normalization of the windowed images (if normalize_box == True), for estimating AVG and STD if sys.argv[23] == 'Defocus/Lattice': tiltgeom = '' elif sys.argv[23] == 'Defocus': tiltgeom = 'DEFOCUS_' elif sys.argv[23] == 'Lattice': tiltgeom = 'LATTICE_' elif sys.argv[23] == 'Merge': tiltgeom = 'MERGE_' if sys.argv[24] == 'Micrograph': ctfcor = True # stack_rootname = stack_rootname + '_ctfcor' else: ctfcor = False if sys.argv[25] == 'y': save_pick_fig = True else: save_pick_fig = False if sys.argv[26] == 'y': use_masked_image = True else: use_masked_image = False if sys.argv[27] == 'y': do_resample = True else: do_resample = False n_threads = int(sys.argv[28]) if n_threads < 1: n_threads = 1 this_thread = int(sys.argv[29]) # End arguments f = open(merge_dirfile,'r') img_dirs = f.readlines() f.close() # Constant parameters to be written on the .par file of this dataset: shx = 0.0 shy = 0.0 occ = 100.0 logp = 0 sig = 0.5 # This has nothing to do with the normalization SIGMA! score = 0.0 chg = 0.0 idx = 0 # box_fail = 0 phaori_err = 0 prog = 0.0 N = len(img_dirs) # batch_size = round(float(N)/n_threads) batch_size = int( round( float( N ) / n_threads ) ) first_img = ( this_thread - 1 ) * batch_size if this_thread < n_threads: last_img = first_img + batch_size else: last_img = N img_dirs = img_dirs[first_img:last_img] n = first_img + 1 print( '\nJob %d/%d picking particles from micrographs %d to %d...\n' % (this_thread, n_threads, n, last_img) ) # Open the .par file to store all particle parameters: f = open(stack_path+stack_rootname+'_1_r1-%.4d.par' % this_thread, 'w+') # Open the master coordinates file: mcf = open(stack_path+stack_rootname+'_coordinates_master-%.4d.txt' % this_thread, 'w+') for d in img_dirs: d = d.strip() imname = d.split('/')[-1] try: # Read in all relevant image parameters: params = Read2dxCfgFile(folders+d+'/2dx_image.cfg') except: print( '\nProblem with image %s!\n' % d ) continue # if ctfcor: # try: # mrc = glob.glob(folders+d+'/image_ctfcor.mrc')[0] # img = ioMRC.readMRC(mrc)[0] # Read image # bf = open(folders+d+'/image_ctfcor.box', 'w+') # except: # print '\nCTF-corrected micrograph not found for image %s!\n' % d # continue # else: if use_masked_image: # First we look for the masked, zero-padded, normalized micrograph: try: # # There might be some funny numbers appended to the file name so we have to look for the shortest one to get the right file: # mrclist = glob.glob(folders+d+'/m'+imname+'*.mrc') # lenlist = [] # for m in mrclist: # lenlist.append(len(m)) # shortest_idx = np.argsort(lenlist)[0] # # print mrclist[shortest_idx] # mrc = mrclist[shortest_idx] # bf = open(os.path.splitext(mrc)[0]+'.box', 'w+') # # mrc = sorted(glob.glob(folders+d+'/m'+imname+'*.mrc'))[0] # # bf = open(folders+d+'/m'+imname+'.box', 'w+') mrc = folders + d + '/' + params['imagename'] + '.mrc' sys.stdout = open(os.devnull, "w") # Suppress output img = ioMRC.readMRC(mrc)[0][0] # Read image sys.stdout = sys.__stdout__ bf = open(folders + d + '/' + params['imagename'] + '.box', 'w+') except: # If it doesn't exist, we try the unmasked, zero-padded, normalized micrograph: try: # mrclist = glob.glob(folders+d+'/'+imname+'*.mrc') # lenlist = [] # for m in mrclist: # lenlist.append(len(m)) # shortest_idx = np.argsort(lenlist)[0] # # print mrclist[shortest_idx] # mrc = mrclist[shortest_idx] # bf = open(os.path.splitext(mrc)[0]+'.box', 'w+') # # mrc = sorted(glob.glob(folders+d+'/m'+imname+'*.mrc'))[0] # # bf = open(folders+d+'/m'+imname+'.box', 'w+') mrc = folders + d + '/' + params['nonmaskimagename'] + '.mrc' sys.stdout = open(os.devnull, "w") # Suppress output img = ioMRC.readMRC(mrc)[0][0] # Read image sys.stdout = sys.__stdout__ bf = open(folders + d + '/' + params['nonmaskimagename'] + '.box', 'w+') except: # If neither exist we skip this image print( '::\nProblem with image %s!\n' % d ) continue else: # If the user requires, we go directly to the unmasked, zero-padded, normalized micrograph: try: # mrclist = glob.glob(folders+d+'/'+imname+'*.mrc') # lenlist = [] # for m in mrclist: # lenlist.append(len(m)) # shortest_idx = np.argsort(lenlist)[0] # # print mrclist[shortest_idx] # mrc = mrclist[shortest_idx] # bf = open(os.path.splitext(mrc)[0]+'.box', 'w+') # # mrc = sorted(glob.glob(folders+d+'/m'+imname+'*.mrc'))[0] # # bf = open(folders+d+'/m'+imname+'.box', 'w+') mrc = folders + d + '/' + params['nonmaskimagename'] + '.mrc' sys.stdout = open(os.devnull, "w") # Suppress output img = ioMRC.readMRC(mrc)[0][0] # Read image sys.stdout = sys.__stdout__ bf = open(folders + d + '/' + params['nonmaskimagename'] + '.box', 'w+') except: # If neither exist we skip this image print( '::\nProblem with image %s!' % d ) print( '::' ) continue # Here we check whether the pixel size defined in the image cfg file agrees with the desired one: # print params.keys() apixold = apix if ( params['sample_pixel'] < 0.99 * apix ) or ( params['sample_pixel'] > 1.01 * apix ): # Should not differ by more than 1% ! try: apixold = params['stepdigitizer'] * 1e4 / params['magnification'] # We give it a second chance by calculating from stepdigitizer and magnification # print params['stepdigitizer'], params['magnification'] except KeyError: params_master = Read2dxCfgFile(folders+d+'/../2dx_master.cfg') # Try to fetch stepdigitizer information from the group's 2dx_master.cfg file apixold = params_master['stepdigitizer'] * 1e4 / params['magnification'] # We give it a second chance by calculating from stepdigitizer and magnification if ( apixold < 0.99 * apix ) or ( apixold > 1.01 * apix ): # Should not differ by more than 1% ! if do_resample: # We resample the micrograph in Fourier space to bring it to the desired pixel size: print( apixold, apix ) img = util.Resample( img, apix=apixold, newapix=apix ) else: print( '::\nSkipping image %s: pixel size of this image seems to be different from the one defined (%f A).' % (d, apix) ) print( '::\nPlease check it if you would like to include this image.' ) print( '::' ) continue # TO DO: if magnification differs (by failing the tests above), should we resample the micrograph to desired mag? print( '::\nNow boxing unit cells of micrograph %d/%d.\n' % (n, N) ) print( mrc ) try: if invert_micrograph: img = -1.0 * img # img = spx.EMNumPy.em2numpy(img) profile = glob.glob(folders+d+'/*profile.dat')[0] dat = ReadProfileDat(profile) ccmean = np.mean(dat[:,4]) ccstd = np.std(dat[:,4]) cc_thr = ccmean + sigcc * ccstd print( ':\nImage average value:%.2f' % img.mean() ) print( ':Image standard deviation:%.2f' % img.std() ) print( ':' ) print( ':CC scores average value:%.2f' % ccmean ) print( ':CC scores standard deviation:%.2f' % ccstd ) print( ':Only particles with CC score above %.2f will be picked.\n' % cc_thr ) # # Get several values related to defocus, astigmatism and tilting: # params = Read2dxCfgFile(folders+d+'/2dx_image.cfg') # w = img.get_xsize() w = img.shape[0] # Get the unit-cell vectors: if sum(params['PHAORI']) == 0: PhaOriX = 0 PhaOriY = 0 a = [0,0] b = [0,0] else: a,b = LatticeReciprocal2Real(params['u'], params['v'], w * apix / apixold ) # These parameters are w.r.t to the original image size # Convert from Numpy-array to list: a = [a[0], a[1]] b = [b[0], b[1]] x,y = CalculatePickingPositions(dat, a, b, w * apix / apixold, params['PHAORI'], phaori_shift, params[tiltgeom+'TLTANG']) # These parameters are w.r.t to the original image size x *= apixold / apix y *= apixold / apix # Plot the picking profile: if save_pick_fig: meanimg = img.mean() stdimg = img.std() climimg = [meanimg - 2 * stdimg, meanimg + 2 * stdimg] fig1 = plt.figure() plt.imshow(img, cmap=cm.gray, vmin=climimg[0], vmax=climimg[1]) Axes1 = fig1.gca() # The following values are constant within each crystal: phi = 90.0 - params[tiltgeom+'TAXA'] theta = params[tiltgeom+'TLTANG'] psi = 270.0 - params[tiltgeom+'TLTAXIS'] ang = params['AST_ANGLE'] max_good = np.sum( dat[:,4] < cc_thr ) # The maximum number of particles we may extract from this micrograph boxes = np.zeros( (max_good, box_size, box_size), dtype='float32' ) if save_phase_flipped: boxespf = np.zeros( (max_good, box_size, box_size), dtype='float32' ) if save_ctf_multiplied: boxescm = np.zeros( (max_good, box_size, box_size), dtype='float32' ) if save_wiener_filtered: boxeswf = np.zeros( (max_good, box_size, box_size), dtype='float32' ) idx_start = idx # Absolute index in the beginning of this crystal m = 0 ptcl_idx = np.arange( dat.shape[0] ) # print 'ptcl_idx[-1] is ',ptcl_idx[-1] # if shuffle_order: # np.random.seed( seed=n ) # Fix random seed to get reproducible results # np.random.shuffle( ptcl_idx ) for i in ptcl_idx: # for i in np.arange(dat.shape[0]): # print i,m try: # Adjust the picking coordinates for the .box file and picking plots: xbox_plot = x[i] + w/2 ybox_plot = y[i] + w/2 xbox = xbox_plot - box_size/2 ybox = ybox_plot - box_size/2 # if dat[i,4] < cc_thr or np.isnan(x[i]) or np.isnan(y[i]): if dat[i,4] < cc_thr: if save_pick_fig: # Write red patch on image to be saved as .png describing the picking positions: Axes1.add_patch(patches.Circle((xbox_plot, ybox_plot), edgecolor='magenta', facecolor='none', linewidth=0.8, radius=box_size/8)) # Axes1.add_patch(patches.Rectangle(xy=(xbox, ybox), width=box_size, height=box_size, edgecolor='red', facecolor='none', linewidth=0.2)) else: # Extract the particle from the micrograph at specified position: # NOTE THAT X,Y CONVENTIONS FOR ioMRC/NUMPY ARE INVERTED IN RELATION TO SPARX/EMAN!!! # box = spx.Util.window(img,int(box_size),int(box_size),1,int(round(x[i])),int(round(y[i]))) xi = int(round(x[i])) yi = int(round(y[i])) # print xi-w/2-box_size/2, xi-w/2+box_size/2 box_ext = img[yi-w//2-box_size//2:yi-w//2+box_size//2, xi-w//2-box_size//2:xi-w//2+box_size//2] # Normalize box to zero mean and constant pre-defined sigma: if normalize_box: # box = NormalizeStack([box], sigma)[0] # try: box = util.NormalizeImg( box_ext, std=sigma, radius=sigma_rad ) else: box = box_ext # except Warning: # raise ZeroDivisionError( "Standard deviation of image is zero!" ) # Sometimes the box contains weird values that may be tricky to detect. Testing the mean of the pixels for NaN and Inf seems to work: # tmpmean = box.mean() # if np.isnan( tmpmean ) or np.isinf( tmpmean ): # print tmpmean tmpstd = box.std() if np.isnan( tmpstd ) or np.isinf( tmpstd ): # print tmpstd print( "The box of CC peak (%d,%d) at position (%d,%d) in micrograph %d/%d contains strange values (NaN or Inf)! Will be discarded." % (dat[i,0], dat[i,1], int(round(x[i])), int(round(y[i])), n, N) ) raise ValueError boxes[m,:,:] = box # if m == 0: # boxes[0,:,:] = box # else: # # print box.shape # boxes = np.append( boxes, box.reshape( ( 1, box_size, box_size) ), axis=0 ) if calculate_defocus_tilted or save_phase_flipped or save_ctf_multiplied or save_wiener_filtered: RLDEF1,RLDEF2 = CalculateDefocusTilted(x[i], y[i], apix, params[tiltgeom+'TLTAXIS'], params[tiltgeom+'TLTANG'], params['DEFOCUS1'], params['DEFOCUS2']) else: RLDEF1 = params['DEFOCUS1'] RLDEF2 = params['DEFOCUS2'] if RLDEF1 <= 0.0 or RLDEF2 <= 0.0: print( "The box of CC peak (%d,%d) at position (%d,%d) in micrograph %d/%d has a negative defocus value! Tilt geometry for this crystal is probably wrong. Particle will be discarded." % (dat[i,0], dat[i,1], int(round(x[i])), int(round(y[i])), n, N) ) raise ValueError # Write .par file with the parameters for each particle in the dataset: print( ' %d' % (idx+1),' %.2f' % psi,' %.2f' % theta,' %.2f' % phi,' %.2f' % shx,' %.2f' % shy,' %d' % magnification,' %d' % n,' %.2f' % RLDEF1,' %.2f' % RLDEF2,' %.2f' % ang,' %.2f' % occ,' %d' % logp,' %.4f' % sig,' %.2f' % score,' %.2f' % chg, file=f ) # Write the picking information to the .box file: print( '%d' % xbox, '\t%d' % ybox, '\t%d' % box_size, '\t%d' % box_size, file=bf ) # Write the picking and defocus information to the master coordinates file: print( '%d' % (idx+1),'\t%d' % n,'\t%s' % folders+d+'/'+imname,'\t%d' % xbox, '\t%d' % ybox, '\t%d' % box_size, '\t%d' % box_size,'\t%.2f' % RLDEF1,'\t%.2f' % RLDEF2, file=mcf ) # Write image to the particle stack: # if idx == 0: # # If this is the first image, we initiate as a normal .mrcs stack. # box.write_image(stack_path+stack_rootname+'-%.4d.mrcs' % this_thread, idx) # else: # # Subsequent images are directly appended to the file: # with open(stack_path+stack_rootname+'-%.4d.mrcs' % this_thread, 'ab') as mrcf: # spx.EMNumPy.em2numpy(box).tofile(mrcf) # if (save_phase_flipped or save_wiener_filtered) and not ctfcor: # # Convert CTF parameters to SPARX convention: # defocus = (RLDEF1+RLDEF2)/2 # ast = RLDEF1-RLDEF2 # if params['AST_ANGLE'] < 0.0: # astang = 360.0 + params['AST_ANGLE'] # else: # astang = params['AST_ANGLE'] # # Generate SPARX CTF object: # p = [defocus * 1e-4, microscope_cs, microscope_voltage, apix, 0, ampcontrast * 100, ast * 1e-4, astang] # spx.set_ctf(box, p) # ctf = spx.generate_ctf(p) # Phase-flip the image: if save_phase_flipped: # Apply CTF correction on whole micrograph to reduce delocalization effects: # imgctfcor = spx.filt_ctf( img, ctf, binary=1 ) # boxctfcor = spx.Util.window( imgctfcor, int( box_size ), int( box_size ), 1, int( round( x[i] ) ), int( round( y[i] ) ) ) if ctfcor: imgctfcor = CTF.CorrectCTF( img, DF1=RLDEF1, DF2=RLDEF2, AST=params['AST_ANGLE'], WGH=ampcontrast, apix=apix, Cs=microscope_cs, kV=microscope_voltage, phase_flip=True, return_ctf=False, invert_contrast=False )[0] boxctfcor = imgctfcor[yi-w//2-box_size//2:yi-w//2+box_size//2, xi-w//2-box_size//2:xi-w//2+box_size//2] else: boxctfcor = CTF.CorrectCTF( box_ext, DF1=RLDEF1, DF2=RLDEF2, AST=params['AST_ANGLE'], WGH=ampcontrast, apix=apix, Cs=microscope_cs, kV=microscope_voltage, phase_flip=True, return_ctf=False, invert_contrast=False )[0] if normalize_box: # try: boxctfcor = util.NormalizeImg( boxctfcor, std=sigma, radius=sigma_rad ) # except Warning: # raise ZeroDivisionError( "Standard deviation of image is zero!" ) boxespf[m,:,:] = boxctfcor # if m == 0: # boxespf[0,:,:] = boxctfcor # else: # boxespf = np.append( boxespf, boxctfcor.reshape( ( 1, box_size, box_size) ), axis=0 ) # Write image to the particle stack: # if idx == 0: # # If this is the first image, we initiate as a normal .mrcs stack. # boxctfcor.write_image( stack_path+stack_rootname+'_phase-flipped-%.4d.mrcs' % this_thread, idx ) # else: # # Subsequent images are directly appended to the file: # with open( stack_path+stack_rootname+'_phase-flipped-%.4d.mrcs' % this_thread, 'ab' ) as mrcf: # spx.EMNumPy.em2numpy(boxctfcor).tofile( mrcf ) # CTF-multiply the image: if save_ctf_multiplied: # # Apply CTF correction on whole micrograph to reduce delocalization effects: # imgctfcor = spx.filt_ctf( img, ctf, binary=0 ) # boxctfcor = spx.Util.window( imgctfcor, int( box_size ), int( box_size ), 1, int( round( x[i] ) ), int( round( y[i] ) ) ) if ctfcor: imgctfcor = CTF.CorrectCTF( img, DF1=RLDEF1, DF2=RLDEF2, AST=params['AST_ANGLE'], WGH=ampcontrast, apix=apix, Cs=microscope_cs, kV=microscope_voltage, ctf_multiply=True, return_ctf=False, invert_contrast=False )[0] boxctfcor = imgctfcor[yi-w//2-box_size//2:yi-w//2+box_size//2, xi-w//2-box_size//2:xi-w//2+box_size//2] else: boxctfcor = CTF.CorrectCTF( box_ext, DF1=RLDEF1, DF2=RLDEF2, AST=params['AST_ANGLE'], WGH=ampcontrast, apix=apix, Cs=microscope_cs, kV=microscope_voltage, ctf_multiply=True, return_ctf=False, invert_contrast=False )[0] if normalize_box: # try: boxctfcor = util.NormalizeImg( boxctfcor, std=sigma, radius=sigma_rad ) # except Warning: # raise ZeroDivisionError( "Standard deviation of image is zero!" ) boxescm[m,:,:] = boxctfcor # if m == 0: # boxescm[0,:,:] = boxctfcor # else: # boxescm = np.append( boxescm, boxctfcor.reshape( ( 1, box_size, box_size) ), axis=0 ) # Write image to the particle stack: # if idx == 0: # # If this is the first image, we initiate as a normal .mrcs stack. # boxctfcor.write_image( stack_path+stack_rootname+'_wiener-filtered-%.4d.mrcs' % this_thread, idx ) # else: # # Subsequent images are directly appended to the file: # with open( stack_path+stack_rootname+'_wiener-filtered-%.4d.mrcs' % this_thread, 'ab' ) as mrcf: # spx.EMNumPy.em2numpy(boxctfcor).tofile( mrcf ) # Wiener-filter the image: if save_wiener_filtered: # # Apply CTF correction on whole micrograph to reduce delocalization effects: # imgctfcor = spx.filt_ctf( img, ctf, binary=0 ) # boxctfcor = spx.Util.window( imgctfcor, int( box_size ), int( box_size ), 1, int( round( x[i] ) ), int( round( y[i] ) ) ) if ctfcor: imgctfcor = CTF.CorrectCTF( img, DF1=RLDEF1, DF2=RLDEF2, AST=params['AST_ANGLE'], WGH=ampcontrast, apix=apix, Cs=microscope_cs, kV=microscope_voltage, wiener_filter=True, return_ctf=False, invert_contrast=False, C=wiener_constant )[0] boxctfcor = imgctfcor[yi-w//2-box_size//2:yi-w//2+box_size//2, xi-w//2-box_size//2:xi-w//2+box_size//2] else: boxctfcor = CTF.CorrectCTF( box_ext, DF1=RLDEF1, DF2=RLDEF2, AST=params['AST_ANGLE'], WGH=ampcontrast, apix=apix, Cs=microscope_cs, kV=microscope_voltage, wiener_filter=True, return_ctf=False, invert_contrast=False, C=wiener_constant )[0] if normalize_box: # try: boxctfcor = util.NormalizeImg( boxctfcor, std=sigma, radius=sigma_rad ) # except Warning: # raise ZeroDivisionError( "Standard deviation of image is zero!" ) boxeswf[m,:,:] = boxctfcor # if m == 0: # boxeswf[0,:,:] = boxctfcor # else: # boxeswf = np.append( boxeswf, boxctfcor.reshape( ( 1, box_size, box_size) ), axis=0 ) # Write image to the particle stack: # if idx == 0: # # If this is the first image, we initiate as a normal .mrcs stack. # boxctfcor.write_image( stack_path+stack_rootname+'_wiener-filtered-%.4d.mrcs' % this_thread, idx ) # else: # # Subsequent images are directly appended to the file: # with open( stack_path+stack_rootname+'_wiener-filtered-%.4d.mrcs' % this_thread, 'ab' ) as mrcf: # spx.EMNumPy.em2numpy(boxctfcor).tofile( mrcf ) if save_pick_fig: # Write green patch on image to be saved as .png describing the picking positions: Axes1.add_patch(patches.Circle((xbox_plot, ybox_plot), edgecolor='lime', facecolor='none', linewidth=0.8, radius=box_size/8)) # Axes1.add_patch(patches.Rectangle(xy=(xbox, ybox), width=box_size, height=box_size, edgecolor='lime', facecolor='none', linewidth=0.2)) m += 1 idx += 1 except ( RuntimeError, ValueError, ZeroDivisionError, IndexError ) as e: if save_pick_fig: # Write red patch on image to be saved as .png describing the picking positions: Axes1.add_patch(patches.Circle((xbox_plot, ybox_plot), edgecolor='magenta', facecolor='none', linewidth=0.8, radius=box_size/8)) # Axes1.add_patch(patches.Rectangle(xy=(xbox, ybox), width=box_size, height=box_size, edgecolor='red', facecolor='none', linewidth=0.2)) print( 'Failed to box CC peak (%d,%d) at position (%d,%d) in micrograph %d/%d!' % (dat[i,0], dat[i,1], int(round(x[i])), int(round(y[i])), n, N) ) # except ValueError: # if save_pick_fig: # # Write red patch on image to be saved as .png describing the picking positions: # # Axes1.add_patch(patches.Circle((dat[i,2], dat[i,3]), edgecolor='red', facecolor='none', linewidth=0.2, radius=20)) # Axes1.add_patch(patches.Rectangle(xy=(xbox, ybox), width=box_size, height=box_size, edgecolor='red', facecolor='none', linewidth=0.2)) # print 'Failed to box CC peak (%d,%d) at position (%d,%d) in micrograph %d/%d!' % (dat[i,0], dat[i,1], int(round(x[i])), int(round(y[i])), n, N) # except ZeroDivisionError: # if save_pick_fig: # # Write red patch on image to be saved as .png describing the picking positions: # # Axes1.add_patch(patches.Circle((dat[i,2], dat[i,3]), edgecolor='red', facecolor='none', linewidth=0.2, radius=20)) # Axes1.add_patch(patches.Rectangle(xy=(xbox, ybox), width=box_size, height=box_size, edgecolor='red', facecolor='none', linewidth=0.2)) # print 'Failed to box CC peak (%d,%d) at position (%d,%d) in micrograph %d/%d!' % (dat[i,0], dat[i,1], int(round(x[i])), int(round(y[i])), n, N) # Particles are written to stacks in crystal batches, thus saving fopen() calls: sys.stdout = open(os.devnull, "w") # Suppress output ioMRC.writeMRC( boxes[:m,:,:], stack_path+stack_rootname+'-%.4d.mrcs' % this_thread, dtype='float32', idx=idx_start ) if save_phase_flipped: ioMRC.writeMRC( boxespf[:m,:,:], stack_path+stack_rootname+'_phase-flipped-%.4d.mrcs' % this_thread, dtype='float32', idx=idx_start ) if save_ctf_multiplied: ioMRC.writeMRC( boxescm[:m,:,:], stack_path+stack_rootname+'_ctf-multiplied-%.4d.mrcs' % this_thread, dtype='float32', idx=idx_start ) if save_wiener_filtered: ioMRC.writeMRC( boxeswf[:m,:,:], stack_path+stack_rootname+'_wiener-filtered-%.4d.mrcs' % this_thread, dtype='float32', idx=idx_start ) sys.stdout = sys.__stdout__ print( '\nBoxed %d/%d CC peaks from micrograph %d/%d.\n' % (m, dat.shape[0], n, N) ) # # Update the counts in the MRC headers: # # First the normal particle stack: # header = ioMRC.readMRCHeader( stack_path+stack_rootname+'-%.4d.mrcs' % this_thread ) # header['dimensions'][0] = idx # # Now we write the header back: # with open( stack_path+stack_rootname+'-%.4d.mrcs' % this_thread, 'rb+' ) as mrcf: # ioMRC.writeMRCHeader( mrcf, header, endchar = '<' ) # # Then the phase-flipped: # if save_phase_flipped and not ctfcor: # header = ioMRC.readMRCHeader( stack_path+stack_rootname+'_phase-flipped-%.4d.mrcs' % this_thread ) # header['dimensions'][0] = idx # # Now we write the header back: # with open( stack_path+stack_rootname+'_phase-flipped-%.4d.mrcs' % this_thread, 'rb+' ) as mrcf: # ioMRC.writeMRCHeader( mrcf, header, endchar = '<' ) # # Then the Wiener-filtered: # if save_wiener_filtered and not ctfcor: # header = ioMRC.readMRCHeader( stack_path+stack_rootname+'_wiener-filtered-%.4d.mrcs' % this_thread ) # header['dimensions'][0] = idx # # Now we write the header back: # with open( stack_path+stack_rootname+'_wiener-filtered-%.4d.mrcs' % this_thread, 'rb+' ) as mrcf: # ioMRC.writeMRCHeader( mrcf, header, endchar = '<' ) # print '<<@progress: %d>>' % round(n*100.0/N) # Report progress to the GUI: prog += 75.0/N if prog >= 1.0: print( '<<@progress: +%d>>' % round(prog) ) prog -= np.floor(prog) if save_pick_fig: fig1.savefig(png_path+'mic_%.3d_' % n+imname+'_picking.png', dpi=300) plt.close(fig1) n += 1 except RuntimeError: # print '::PROBLEM WITH MICROGRAPH %d/%d!!! Maybe it was not found?' % (n, N) # print '::' # print mrc print( '\nPROBLEM WITH MICROGRAPH:' ) print( '%s' % mrc ) print( 'Maybe it was not found?' ) except ValueError: # print '::PROBLEM WITH CC PROFILE FOR IMAGE %d/%d!!!' % (n, N) # print '::' # print mrc print( '\nPROBLEM WITH CC PROFILE FOR IMAGE:' ) print( '%s' % mrc ) bf.close() # n += 1 # print '::Total boxed unit cells:%d' % idx # print '::Failed to box %d unit cells.' % box_fail # print '<<@progress: +%d>>' % round(prog/0.01) print( '\nJob %d/%d finished picking particles.\n' % (this_thread, n_threads) ) f.close() mcf.close()
terrain = plt.contour(model_run.zs[0, near:far, left:right], terrain_levels, alpha=1, colors='w', vmin=-800, vmax=3000, zorder=4, linewidths=1.75) #Plot Point at peak peak = np.where(model_run.zs[0, near:far, left:right].values == np.max(model_run.zs[0, near:far, left:right].values)) ax.add_patch( patches.Circle((peak[1][0], peak[0][0]), 7, color='w', zorder=20)) except: pass #Draw border ax.add_patch( patches.Rectangle((1.5, 0), right - left - 3, far - near - 1, fill=False, zorder=10, linewidth=2)) #Titles
def _make_circle(vert, color, radius, **kwargs): return mpatches.Circle(xy=vert, radius=radius, facecolor=color, **kwargs)
calc_args = { 'k': kSP, 'w0': w0, 'L': L, 'res': res, 'exact': False, 'normalize': True, 'find_zeroes': True } axis_args = {'aspect': 'equal', 'xticks': [], 'yticks': []} circle_args = {'facecolor': 'none', 'linestyle': 'dashed'} ax = fig.add_subplot(gs[0], **axis_args) p = calculation_figure(ax, 3, 50e-6, **calc_args) # Manually put the circle in since the goddamn contour plot doesn't work ax.add_patch(patches.Circle((0, 0), 4.0, **circle_args)) ax.set_xlim(-width * 5e5, width * 5e5) ax.set_ylim(-width * 5e5, width * 5e5) ax.subfigure_label('(a)', pos='lower right') ax = fig.add_subplot(gs[1], **axis_args) p = calculation_figure(ax, 3.25, 50e-6, **calc_args) ax.set_xlim(-width * 5e5, width * 5e5) ax.set_ylim(-width * 5e5, width * 5e5) ax.subfigure_label('(b)', pos='lower right') # 3.50 shows a numerical artifact that causes the vortex to split into two # (of the same charge), for some reason - not only is this not physical, it # didn't show up in other calculations. Using 3.51 until I can figure out what # went wrong. ax = fig.add_subplot(gs[2], **axis_args)
def plot_ULS_section(x, y, xr, yr, x_sb, y_sb, Asb, sb_cog, Fc, Fr, Mcx, Mcy, Mrx, Mry, Mx, My, alpha_deg, na_y): ''' Returns a plot of ULS section state for given neutral axis location ''' phi = sc.compute_moment_vector_angle(Mx, My) C, T = sc.compute_C_T_forces(Fc, Fr) Mx_C, My_C, Mx_T, My_T = sc.compute_C_T_moments(C, T, Mcx, Mcy, Mry, Mrx, Fr, alpha_deg) ex_C, ey_C, ex_T, ey_T = sc.compute_C_T_forces_eccentricity( C, T, My_C, Mx_C, Mx_T, My_T) # Find collision points between neutral axis and concrete section # NOTE Only for plotting puposes, not used in calc na_xint, na_yint = geometry.line_polygon_collisions(alpha_deg, na_y, x, y) fig, ax = plt.subplots() plt.gca().set_aspect('equal', adjustable='box') plt.style.use('seaborn-white') # Full concrete section x_plot = x x_plot.append(x_plot[0]) y_plot = y y_plot.append(y_plot[0]) plt.plot(x_plot, y_plot, '-', color='k', linewidth=1) # Concrete section # Coordinate axes # plt.plot([-1.2*b/2, 1.2*b/2], [0, 0], 'k', linewidth=0.3) # TODO Should be more general, maybe pass thoguh plastic centroid? # plt.plot([0, 0], [-1.2*h/2, 1.2*h/2], 'k', linewidth=0.3) # plt.annotate('$x$', (b/2+b/8, 0), verticalalignment='center') # plt.annotate('$y$', (0, h/2+h/8), horizontalalignment='center') plt.plot(na_xint, na_yint, color='k', linewidth=1) # Plot neutral axis plt.plot( [ex_C, ex_T], [ey_C, ey_T], '.', color='b') # Resulting compression and tension force of the section plt.annotate('$C$', (ex_C, ey_C), color='b', horizontalalignment='left', verticalalignment='center', fontsize=12) plt.annotate('$T$', (ex_T, ey_T), color='b', horizontalalignment='left', verticalalignment='center', fontsize=12) # Plot stress block if Asb != 0: # Create list of stress block coords. in the format [[x0, y0], [x1, y2], ..., [xn, yn]] for plotting as a polygon patch # TODO List comprehension! sb_coords = [] for i in range(len(x_sb)): sb_coords.append([x_sb[i], y_sb[i]]) ax.add_patch( patches.Polygon((sb_coords), facecolor='silver', edgecolor='k', linewidth=1)) # Plot centroid of stress block if Asb != 0: plt.plot(sb_cog[0], sb_cog[1], 'x', color='grey', markersize='4') # TODO Radius of rebars on the plot should match the actual radius of the bars # Plot rebars for i in range(len(xr)): ax.add_patch( patches.Circle((xr[i], yr[i]), radius=8, hatch='/////', facecolor='silver', edgecolor='k', linewidth=1)) # margin = b/4 # plt.axis((-(b/2+margin), b/2+margin, -(h/2+margin), h/2+margin)) # Set axis limits plt.show()
gall, galb, radii = [], [], [] for run in runlist: gall += [run.pointing_dir.l_deg()] galb += [run.pointing_dir.b_deg()] radii += [1.75] if src == 'Sgr A*': co = 'green' la = 'Data Observation Regions' elif src == 'Sgr A* Off': co = 'blue' la = 'Background Observation Regions' patch = mpatches.Circle((0, 0), radius=1, facecolor=co, edgecolor='black', linewidth=2, label=la) patches += [patch] fig.show_circles(gall, galb, radii, facecolor=co, zorder=2, edgecolor=None) fig.show_circles(gall, galb, radii, zorder=3, linewidth=1) cols = {'Sgr A*': 'orange', 'Sgr A* Off': 'red'} for src in sources: srcdir = db.source2dir(src) fig.show_markers(srcdir.l_deg(), srcdir.b_deg(), marker='v', color='black', edgecolor=cols[src], label='%s Position' % src,
def Vasarely(obs, exp, fname): # First make sure normalized obs_total = sum(obs.values()) * 1.0 exp_total = sum(exp.values()) * 1.0 for gt in obs.keys(): obs[gt] = obs[gt] / obs_total for gt in exp.keys(): exp[gt] = exp[gt] / exp_total # Get alleles box_w = 1 box_h = box_w alleles = set() for gt in list(obs.keys()) + list(exp.keys()): alleles.add(gt[0]) alleles.add(gt[1]) alleles = sorted(list(alleles)) cm = plt.cm.Greys.from_list("freq", ["white", "black"]) n_alleles = len(alleles) fig, ax = plt.subplots() patches = [] colors = [] # Get expected (square) for i in range(len(alleles)): for j in range(len(alleles)): x = i * box_w y = j * box_h rect = mpatches.Rectangle([x, y], box_w, box_h) patches.append(rect) colors.append(cm(exp.get((alleles[i], alleles[j]), 0))) # Get observed (circle) for i in range(len(alleles)): for j in range(len(alleles)): x = i * box_w y = j * box_h circ = mpatches.Circle([x + box_w / 2.0, y + box_h / 2.0], box_w * 0.4) patches.append(circ) colors.append(cm(obs.get((alleles[i], alleles[j]), 0))) # Plot shapes collection = PatchCollection(patches, color=colors) ax.add_collection(collection) plt.subplots_adjust(left=0, right=1, bottom=0, top=1) # Plot labels for i in range(len(alleles)): xaxis_coord = [i * box_w + box_w / 2.0, -1 * box_h / 4.0] yaxis_coord = [-1 * box_w / 5.0, i * box_h + box_h / 2.0] for coord in [xaxis_coord, yaxis_coord]: plt.text(coord[0], coord[1], alleles[i], ha="center", family='sans-serif', size=14) plt.axis('equal') plt.axis('off') plt.savefig(fname)
def visualize_contour_semi(model, d_i, x_data, y_data, ul_x, ul_y, basis, val_loader, args, with_lds=False, save_filename='prob_cont', show_contour=True): line_width = 10 range_x = np.arange(-2.0, 2.1, 0.05) a_inv = linalg.inv(np.dot(basis, basis.T)) train_x_org = np.dot(x_data, np.dot(basis.T, a_inv)) test_x_org = np.zeros((range_x.shape[0]**2, 2)) train_x_1_ind = np.where(y_data == 1)[0] train_x_0_ind = np.where(y_data == 0)[0] ul_x_org = np.dot(ul_x, np.dot(basis.T, a_inv)) ul_x_1_ind = np.where(ul_y == 1)[0] ul_x_0_ind = np.where(ul_y == 0)[0] for i in range(range_x.shape[0]): for j in range(range_x.shape[0]): test_x_org[range_x.shape[0] * i + j, 0] = range_x[i] test_x_org[range_x.shape[0] * i + j, 1] = range_x[j] test_x = np.dot(test_x_org, basis) model.eval() f_p_y_given_x = model(torch.FloatTensor(test_x).to(args.device)) pred = nfunc.softmax(f_p_y_given_x, dim=1)[:, 1].cpu().detach().numpy() z = np.zeros((range_x.shape[0], range_x.shape[0])) for i in range(range_x.shape[0]): for j in range(range_x.shape[0]): z[i, j] = pred[range_x.shape[0] * i + j] y, x = np.meshgrid(range_x, range_x) font_size = 20 rc = 'r' bc = 'b' if d_i == "1": rescale = 1.0 # /np.sqrt(500) arc1 = plot_patch.Arc(xy=(0.5 * rescale, -0.25 * rescale), width=2.0 * rescale, height=2.0 * rescale, angle=0, theta1=270, theta2=180, linewidth=line_width, alpha=0.15, color=rc) arc2 = plot_patch.Arc(xy=(-0.5 * rescale, +0.25 * rescale), width=2.0 * rescale, height=2.0 * rescale, angle=0, theta1=90, theta2=360, linewidth=line_width, alpha=0.15, color=bc) fig = plt.gcf() frame = fig.gca() frame.add_artist(arc1) frame.add_artist(arc2) else: rescale = 1.0 # /np.sqrt(500) circle1 = plot_patch.Circle((0, 0), 1.0 * rescale, color=rc, alpha=0.2, fill=False, linewidth=line_width) circle2 = plot_patch.Circle((0, 0), 0.15 * rescale, color=bc, alpha=0.2, fill=False, linewidth=line_width) fig = plt.gcf() frame = fig.gca() frame.add_artist(circle1) frame.add_artist(circle2) plt.scatter(ul_x_org[ul_x_1_ind, 0] * rescale, ul_x_org[ul_x_1_ind, 1] * rescale, s=2, marker='o', c=rc, label='$y=1$') plt.scatter(ul_x_org[ul_x_0_ind, 0] * rescale, ul_x_org[ul_x_0_ind, 1] * rescale, s=2, marker='o', c=bc, label='$y=0$') plt.scatter(train_x_org[train_x_1_ind, 0] * rescale, train_x_org[train_x_1_ind, 1] * rescale, s=50, marker='o', c=rc, label='$y=1$', edgecolor='black', linewidth=1) plt.scatter(train_x_org[train_x_0_ind, 0] * rescale, train_x_org[train_x_0_ind, 1] * rescale, s=50, marker='o', c=bc, label='$y=0$', edgecolor='black', linewidth=1) err_num, loss = evaluate_classifier(model, val_loader, args.device) err_rate = 1.0 * err_num / len(val_loader.dataset) lds_part = "" if with_lds: eps = args.eps args.eps = 0.5 args.k = 5 reg_component = VAT(args) x_data = x_data.to(args.device) ave_lds = 0 for t in range(20): ave_lds += reg_component(model, x_data, kl_way=1) ave_lds /= 20 lds_part = ' $\widetilde{\\rm LDS}=%.3f$' % ave_lds args.k = 1 args.eps = eps if save_filename is None: plt.show(block=False) else: cs = None levels = [0.05, 0.2, 0.35, 0.5, 0.65, 0.8, 0.95] cs = plt.contour(x * rescale, y * rescale, z, 7, cmap='bwr', vmin=0., vmax=1.0, linewidths=8., levels=levels) plt.setp(cs.collections, linewidth=1.0) plt.contour(x * rescale, y * rescale, z, 1, cmap='binary', vmin=0, vmax=0.5, linewidths=2.0) # plt.tight_layout() # plt.savefig(save_filename) if show_contour: plt.title('%s\nError %g%s' % (args.exp_marker, err_rate, lds_part)) plt.xticks(fontsize=font_size) plt.yticks(fontsize=font_size) plt.xlim([-2. * rescale, 2. * rescale]) plt.ylim([-2. * rescale, 2. * rescale]) plt.xticks([-2.0, -1.0, 0, 1, 2.0], fontsize=font_size) plt.yticks([-2.0, -1.0, 0, 1, 2.0], fontsize=font_size) if show_contour: color_bar = plt.colorbar(cs) color_bar.ax.tick_params(labelsize=font_size)
def visualize_adv_points(model, d_i, x_data, y_data, ul_x, ul_y, adv_x, adv_y, basis, it, rate, args, save_filename='prob_cont', show_contour=True): line_width = 10 range_x = np.arange(-2.0, 2.1, 0.05) a_inv = linalg.inv(np.dot(basis, basis.T)) train_x_org = np.dot(x_data, np.dot(basis.T, a_inv)) test_x_org = np.zeros((range_x.shape[0]**2, 2)) train_x_1_ind = np.where(y_data == 1)[0] train_x_0_ind = np.where(y_data == 0)[0] ul_x_org = np.dot(ul_x, np.dot(basis.T, a_inv)) ul_x_1_ind = np.where(ul_y == 1)[0] ul_x_0_ind = np.where(ul_y == 0)[0] adv_x_org = np.dot(adv_x, np.dot(basis.T, a_inv)) adv_x_1_ind = np.where(adv_y == 1)[0] adv_x_0_ind = np.where(adv_y == 0)[0] for i in range(range_x.shape[0]): for j in range(range_x.shape[0]): test_x_org[range_x.shape[0] * i + j, 0] = range_x[i] test_x_org[range_x.shape[0] * i + j, 1] = range_x[j] test_x = np.dot(test_x_org, basis) model.eval() f_p_y_given_x = model(torch.FloatTensor(test_x).to(args.device)) pred = nfunc.softmax(f_p_y_given_x, dim=1)[:, 1].cpu().detach().numpy() z = np.zeros((range_x.shape[0], range_x.shape[0])) for i in range(range_x.shape[0]): for j in range(range_x.shape[0]): z[i, j] = pred[range_x.shape[0] * i + j] y, x = np.meshgrid(range_x, range_x) font_size = 20 rc = 'r' bc = 'b' if d_i == "1": rescale = 1.0 # /np.sqrt(500) arc1 = plot_patch.Arc(xy=(0.5 * rescale, -0.25 * rescale), width=2.0 * rescale, height=2.0 * rescale, angle=0, theta1=270, theta2=180, linewidth=line_width, alpha=0.15, color=rc) arc2 = plot_patch.Arc(xy=(-0.5 * rescale, +0.25 * rescale), width=2.0 * rescale, height=2.0 * rescale, angle=0, theta1=90, theta2=360, linewidth=line_width, alpha=0.15, color=bc) fig = plt.gcf() frame = fig.gca() frame.add_artist(arc1) frame.add_artist(arc2) else: rescale = 1.0 # /np.sqrt(500) circle1 = plot_patch.Circle((0, 0), 1.0 * rescale, color=rc, alpha=0.2, fill=False, linewidth=line_width) circle2 = plot_patch.Circle((0, 0), 0.15 * rescale, color=bc, alpha=0.2, fill=False, linewidth=line_width) fig = plt.gcf() frame = fig.gca() frame.add_artist(circle1) frame.add_artist(circle2) frame.axes.get_yaxis().set_visible(False) colors = [ 'b', 'r', 'orange', 'purple', 'cyan', 'yellow', 'brown', 'y', 'c', 'lime', ] plt.scatter(adv_x_org[adv_x_1_ind, 0] * rescale, adv_x_org[adv_x_1_ind, 1] * rescale, s=25, marker='o', c=colors, label='$y=1$') plt.scatter(adv_x_org[adv_x_0_ind, 0] * rescale, adv_x_org[adv_x_0_ind, 1] * rescale, s=25, marker='o', c=colors, label='$y=0$') plt.scatter(ul_x_org[ul_x_1_ind, 0] * rescale, ul_x_org[ul_x_1_ind, 1] * rescale, s=10, marker='o', c=colors, label='$y=1$') plt.scatter(ul_x_org[ul_x_0_ind, 0] * rescale, ul_x_org[ul_x_0_ind, 1] * rescale, s=10, marker='o', c=colors, label='$y=0$') gc = 'g' grc = 'gray' plt.scatter(train_x_org[train_x_1_ind, 0] * rescale, train_x_org[train_x_1_ind, 1] * rescale, s=50, marker='o', c=gc, label='$y=1$', edgecolor='black', linewidth=1) plt.scatter(train_x_org[train_x_0_ind, 0] * rescale, train_x_org[train_x_0_ind, 1] * rescale, s=50, marker='o', c=grc, label='$y=0$', edgecolor='black', linewidth=1) if save_filename is None: plt.show(block=False) else: levels = [0.05, 0.2, 0.35, 0.5, 0.65, 0.8, 0.95] cs = plt.contour(x * rescale, y * rescale, z, 7, cmap='bwr', vmin=0., vmax=1.0, linewidths=8., levels=levels) plt.setp(cs.collections, linewidth=1.0) plt.contour(x * rescale, y * rescale, z, 1, cmap='binary', vmin=0, vmax=0.5, linewidths=2.0) plt.xticks(fontsize=font_size) plt.yticks(fontsize=font_size) plt.xlim([-2. * rescale, 2. * rescale]) plt.ylim([-2. * rescale, 2. * rescale]) plt.xticks([-2.0, -1.0, 0, 1, 2.0], fontsize=font_size) plt.yticks([-2.0, -1.0, 0, 1, 2.0], fontsize=font_size) fig.subplots_adjust(right=0.88) cbar_ax = fig.add_axes([0.90, 0.05, 0.02, 0.7]) color_bar = plt.colorbar(cs, cbar_ax) color_bar.ax.tick_params(labelsize=font_size)
import matplotlib.patheffects as PathEffects import numpy as np fig, ax = plt.subplots(1, 1) for y in [(1/4), (2/4), (3/4)]: circle = mpatches.Circle((0.2,y), 0.05, color='red') ax.add_patch(circle) for y in [(3/8), (5/8)]: circle = mpatches.Circle((0.7,y), 0.05, color='orange') ax.add_patch(circle) for y in [(3/8), (5/8)]: arrow = mpatches.Arrow(0.78, y, 0.1, 0, width=0.1, color='black') ax.add_patch(arrow) for y in [(1/4), (2/4), (3/4)]: arrow = mpatches.Arrow(0.02, y, 0.125, 0, width=0.1, color='red') ax.add_patch(arrow)
def anime_ff(self, vx, vy, alfa): #'''#FF fig = plt.figure() ims = [] X = [] Y = [] x = 0 y = 0 ssp = [-0.5, -0.5] #V = self.target.get_V() for index in np.arange(start=1, stop=len(vx) - 1, step=1, dtype=int): x = x + vx[index] * self.dt y = y + vy[index] * self.dt X.append(x) Y.append(y) #vehi = np.matrix([[0.4, 0.4, -0.4, -0.4],[0.4, -0.4, -0.4, 0.4]]) vehi = np.matrix([[0, 0.35, 0.35, -0.35, -0.35, 0], [0.7, 0.35, -0.35, -0.35, 0.35, 0.7]]) Rot = np.matrix([[np.cos(-alfa[index]), -np.sin(-alfa[index])], [np.sin(-alfa[index]), np.cos(-alfa[index])]]) STATE = [[x], [y]] newvehi = Rot * vehi + STATE img = plt.plot(X, Y, marker=".", color="#FAAC58") + plt.plot( newvehi[0, :], newvehi[1, :], marker="p", color="#FAAC58") plt.title("FF") #plt.title("V = {}m/s".format(V[index])) #print("V = {}m/s".format(V[index])) #plt.title("V = "+ str(V[index])[:4]) plt.axis("equal") plt.grid(True) ims.append(img) plt.pause(1) ani = animation.ArtistAnimation(fig, ims, interval=1) #### ax = plt.axes() ############################ field ############################# ######################################################################## sotowaku = patches.Rectangle(xy=(-13.3 + 0.5, -0.5), width=13.3, height=10, ec='#FAAC58', fill=False) ax.add_patch(sotowaku) forest = patches.Rectangle(xy=(-2.45 + 0.5, -0.5), width=2.45, height=8, ec='#FAAC58', fill=False) ax.add_patch(forest) bridge = patches.Rectangle(xy=(-1.725 + 0.5, 6.5 - 0.5), width=1, height=1.5, ec='#FAAC58', fill=False) ax.add_patch(bridge) bri1 = patches.Circle(xy=(-1.725 + 0.5, 6.5 - 0.5), radius=0.08, ec='#FAAC58', fill=False) ax.add_patch(bri1) bri2 = patches.Circle(xy=(-1.725 + 0.5, 6.5 + 1.5 - 0.5), radius=0.08, ec='#FAAC58', fill=False) ax.add_patch(bri2) d = patches.Circle(xy=(-1 * (1.225 + ssp[0]), 2 + ssp[1]), radius=0.08, ec='#FAAC58', fill=False) ax.add_patch(d) e = patches.Circle(xy=(-1 * (1.225 + ssp[0]), 3.5 + ssp[1]), radius=0.08, ec='#FAAC58', fill=False) ax.add_patch(e) f = patches.Circle(xy=(-1 * (1.225 + ssp[0]), 5 + ssp[1]), radius=0.08, ec='#FAAC58', fill=False) ax.add_patch(f) ######################################################################## ######################################################################## #''' plt.show()
def plot_topomap(data, pos, vmax=None, vmin=None, cmap='RdBu_r', sensors=True, res=64, axis=None, names=None, show_names=False, mask=None, mask_params=None, outlines='head', image_mask=None, contours=6, image_interp='bilinear'): """Plot a topographic map as image Parameters ---------- data : array, length = n_points The data values to plot. pos : array, shape = (n_points, 2) For each data point, the x and y coordinates. vmin : float | callable The value specfying the lower bound of the color range. If None, and vmax is None, -vmax is used. Else np.min(data). If callable, the output equals vmin(data). vmax : float | callable The value specfying the upper bound of the color range. If None, the maximum absolute value is used. If vmin is None, but vmax is not, defaults to np.min(data). If callable, the output equals vmax(data). cmap : matplotlib colormap Colormap. sensors : bool | str Add markers for sensor locations to the plot. Accepts matplotlib plot format string (e.g., 'r+' for red plusses). If True, a circle will be used (via .add_artist). Defaults to True. res : int The resolution of the topomap image (n pixels along each side). axis : instance of Axis | None The axis to plot to. If None, the current axis will be used. names : list | None List of channel names. If None, channel names are not plotted. show_names : bool | callable If True, show channel names on top of the map. If a callable is passed, channel names will be formatted using the callable; e.g., to delete the prefix 'MEG ' from all channel names, pass the function lambda x: x.replace('MEG ', ''). If `mask` is not None, only significant sensors will be shown. mask : ndarray of bool, shape (n_channels, n_times) | None The channels to be marked as significant at a given time point. Indices set to `True` will be considered. Defaults to None. mask_params : dict | None Additional plotting parameters for plotting significant sensors. Default (None) equals: dict(marker='o', markerfacecolor='w', markeredgecolor='k', linewidth=0, markersize=4) outlines : 'head' | dict | None The outlines to be drawn. If 'head', a head scheme will be drawn. If dict, each key refers to a tuple of x and y positions. The values in 'mask_pos' will serve as image mask. If None, nothing will be drawn. Defaults to 'head'. image_mask : ndarray of bool, shape (res, res) | None The image mask to cover the interpolated surface. If None, it will be computed from the outline. contours : int | False | None The number of contour lines to draw. If 0, no contours will be drawn. image_interp : str The image interpolation to be used. All matplotlib options are accepted. Returns ------- im : matplotlib.image.AxesImage The interpolated data. cn : matplotlib.contour.ContourSet The fieldlines. """ import matplotlib.pyplot as plt data = np.asarray(data) if data.ndim > 1: err = ("Data needs to be array of shape (n_sensors,); got shape " "%s." % str(data.shape)) raise ValueError(err) elif len(data) != len(pos): err = ("Data and pos need to be of same length. Got data of shape %s, " "pos of shape %s." % (str(), str())) vmin, vmax = _setup_vmin_vmax(data, vmin, vmax) plt.xticks(()) plt.yticks(()) pos, outlines = _check_outlines(pos, outlines) pos_x = pos[:, 0] pos_y = pos[:, 1] ax = axis if axis else plt.gca() ax.set_frame_on(False) if any([not pos_y.any(), not pos_x.any()]): raise RuntimeError('No position information found, cannot compute ' 'geometries for topomap.') if outlines is None: xmin, xmax = pos_x.min(), pos_x.max() ymin, ymax = pos_y.min(), pos_y.max() else: xlim = np.inf, -np.inf, ylim = np.inf, -np.inf, mask_ = np.c_[outlines['mask_pos']] xmin, xmax = (np.min(np.r_[xlim[0], mask_[:, 0] * 1.01]), np.max(np.r_[xlim[1], mask_[:, 0] * 1.01])) ymin, ymax = (np.min(np.r_[ylim[0], mask_[:, 1] * 1.01]), np.max(np.r_[ylim[1], mask_[:, 1] * 1.01])) # interpolate data xi = np.linspace(xmin, xmax, res) yi = np.linspace(ymin, ymax, res) Xi, Yi = np.meshgrid(xi, yi) Zi = _griddata(pos_x, pos_y, data, Xi, Yi) if outlines is None: _is_default_outlines = False elif isinstance(outlines, dict): _is_default_outlines = any([k.startswith('head') for k in outlines]) if _is_default_outlines and image_mask is None: # prepare masking image_mask, pos = _make_image_mask(outlines, pos, res) if image_mask is not None and not _is_default_outlines: Zi[~image_mask] = np.nan if mask_params is None: mask_params = DEFAULTS['mask_params'].copy() elif isinstance(mask_params, dict): params = dict((k, v) for k, v in DEFAULTS['mask_params'].items() if k not in mask_params) mask_params.update(params) else: raise ValueError('`mask_params` must be of dict-type ' 'or None') # plot map and countour im = ax.imshow(Zi, cmap=cmap, vmin=vmin, vmax=vmax, origin='lower', aspect='equal', extent=(xmin, xmax, ymin, ymax), interpolation=image_interp) # plot outline linewidth = mask_params['markeredgewidth'] if isinstance(outlines, dict): for k, (x, y) in outlines.items(): if 'mask' in k: continue ax.plot(x, y, color='k', linewidth=linewidth) # This tackles an incomprehensible matplotlib bug if no contours are # drawn. To avoid rescalings, we will always draw contours. # But if no contours are desired we only draw one and make it invisible . no_contours = False if contours in (False, None): contours, no_contours = 1, True cont = ax.contour(Xi, Yi, Zi, contours, colors='k', linewidths=linewidth) if no_contours is True: for col in cont.collections: col.set_visible(False) if _is_default_outlines: from matplotlib import patches # remove nose offset and tweak patch = patches.Circle((0.5, 0.4687), radius=.46, clip_on=True, transform=ax.transAxes) im.set_clip_path(patch) ax.set_clip_path(patch) if cont is not None: for col in cont.collections: col.set_clip_path(patch) if sensors is not False and mask is None: _plot_sensors(pos_x, pos_y, sensors=sensors, ax=ax) elif sensors and mask is not None: idx = np.where(mask)[0] ax.plot(pos_x[idx], pos_y[idx], **mask_params) idx = np.where(~mask)[0] _plot_sensors(pos_x[idx], pos_y[idx], sensors=sensors, ax=ax) elif not sensors and mask is not None: idx = np.where(mask)[0] ax.plot(pos_x[idx], pos_y[idx], **mask_params) if show_names: if show_names is True: show_names = lambda x: x show_idx = np.arange(len(names)) if mask is None else np.where(mask)[0] for ii, (p, ch_id) in enumerate(zip(pos, names)): if ii not in show_idx: continue ch_id = show_names(ch_id) ax.text(p[0], p[1], ch_id, horizontalalignment='center', verticalalignment='center', size='x-small') plt.subplots_adjust(top=.95) return im, cont
keypoints = keypoints.to(torch.int16).cpu().numpy()[:, :2] else: box = box.detach().numpy() keypoints = keypoints.detach().numpy()[:, :2] rect = patches.Rectangle((box[0], box[1]), box[2] - box[0], box[3] - box[1], linewidth=2, edgecolor='b', facecolor='none') ax.add_patch(rect) # 17 keypoints for k in keypoints: circle = patches.Circle((k[0], k[1]), radius=2, facecolor='r') ax.add_patch(circle) # draw path # left arm path = Path(keypoints[5:10:2], codes) line = patches.PathPatch(path, linewidth=2, facecolor='none', edgecolor='r') ax.add_patch(line) # right arm path = Path(keypoints[6:11:2], codes) line = patches.PathPatch(path, linewidth=2,
axes = fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False) axes.set_xlim(0, size[0]) axes.set_ylim(0, size[1]) radius = 255.0 theta = 0 dtheta = 5.5 / 180.0 * math.pi for i in range(500): theta += dtheta x = 256 + radius * math.cos(theta) y = 256 + 32 + radius * math.sin(theta) r = 10.1 - i * 0.02 radius -= 0.45 patch = patches.Circle((x, y), r, lw=1.0, color='None', ec='k', fc='None') axes.add_patch(patch) for i in range(0, 39): r = 4 thickness = (i + 1) / 10.0 x = 20 + i * 12.5 - r y = 16 patch = patches.Circle((x, y), r, lw=thickness, color='None', ec='k', fc='None') axes.add_patch(patch)
def radviz(frame, class_column, ax=None, color=None, colormap=None, **kwds): """ Plot a multidimensional dataset in 2D. Each Series in the DataFrame is represented as a evenly distributed slice on a circle. Each data point is rendered in the circle according to the value on each Series. Highly correlated `Series` in the `DataFrame` are placed closer on the unit circle. RadViz allow to project a N-dimensional data set into a 2D space where the influence of each dimension can be interpreted as a balance between the influence of all dimensions. More info available at the `original article <http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.135.889>`_ describing RadViz. Parameters ---------- frame : `DataFrame` Pandas object holding the data. class_column : str Column name containing the name of the data point category. ax : :class:`matplotlib.axes.Axes`, optional A plot instance to which to add the information. color : list[str] or tuple[str], optional Assign a color to each category. Example: ['blue', 'green']. colormap : str or :class:`matplotlib.colors.Colormap`, default None Colormap to select colors from. If string, load colormap with that name from matplotlib. kwds : optional Options to pass to matplotlib scatter plotting method. Returns ------- axes : :class:`matplotlib.axes.Axes` See Also -------- pandas.plotting.andrews_curves : Plot clustering visualization Examples -------- .. plot:: :context: close-figs >>> df = pd.DataFrame({ ... 'SepalLength': [6.5, 7.7, 5.1, 5.8, 7.6, 5.0, 5.4, 4.6, ... 6.7, 4.6], ... 'SepalWidth': [3.0, 3.8, 3.8, 2.7, 3.0, 2.3, 3.0, 3.2, ... 3.3, 3.6], ... 'PetalLength': [5.5, 6.7, 1.9, 5.1, 6.6, 3.3, 4.5, 1.4, ... 5.7, 1.0], ... 'PetalWidth': [1.8, 2.2, 0.4, 1.9, 2.1, 1.0, 1.5, 0.2, ... 2.1, 0.2], ... 'Category': ['virginica', 'virginica', 'setosa', ... 'virginica', 'virginica', 'versicolor', ... 'versicolor', 'setosa', 'virginica', ... 'setosa'] ... }) >>> rad_viz = pd.plotting.radviz(df, 'Category') """ import matplotlib.pyplot as plt import matplotlib.patches as patches def normalize(series): a = min(series) b = max(series) return (series - a) / (b - a) n = len(frame) classes = frame[class_column].drop_duplicates() class_col = frame[class_column] df = frame.drop(class_column, axis=1).apply(normalize) if ax is None: ax = plt.gca(xlim=[-1, 1], ylim=[-1, 1]) to_plot = {} colors = _get_standard_colors(num_colors=len(classes), colormap=colormap, color_type='random', color=color) for kls in classes: to_plot[kls] = [[], []] m = len(frame.columns) - 1 s = np.array([(np.cos(t), np.sin(t)) for t in [2.0 * np.pi * (i / float(m)) for i in range(m)]]) for i in range(n): row = df.iloc[i].values row_ = np.repeat(np.expand_dims(row, axis=1), 2, axis=1) y = (s * row_).sum(axis=0) / row.sum() kls = class_col.iat[i] to_plot[kls][0].append(y[0]) to_plot[kls][1].append(y[1]) for i, kls in enumerate(classes): ax.scatter(to_plot[kls][0], to_plot[kls][1], color=colors[i], label=pprint_thing(kls), **kwds) ax.legend() ax.add_patch(patches.Circle((0.0, 0.0), radius=1.0, facecolor='none')) for xy, name in zip(s, df.columns): ax.add_patch(patches.Circle(xy, radius=0.025, facecolor='gray')) if xy[0] < 0.0 and xy[1] < 0.0: ax.text(xy[0] - 0.025, xy[1] - 0.025, name, ha='right', va='top', size='small') elif xy[0] < 0.0 and xy[1] >= 0.0: ax.text(xy[0] - 0.025, xy[1] + 0.025, name, ha='right', va='bottom', size='small') elif xy[0] >= 0.0 and xy[1] < 0.0: ax.text(xy[0] + 0.025, xy[1] - 0.025, name, ha='left', va='top', size='small') elif xy[0] >= 0.0 and xy[1] >= 0.0: ax.text(xy[0] + 0.025, xy[1] + 0.025, name, ha='left', va='bottom', size='small') ax.axis('equal') return ax
def drawMap(self): myPosX = self.myPosX myPosY = self.myPosY myDirect = self.myDirect ax = self.mapFig.add_subplot(111) ax.cla() ax.set_xlim(-fieldWidth, fieldWidth) ax.set_ylim(-fieldHeight, fieldHeight) r1 = patches.Rectangle(xy=(-centerBoxWidth / 2, -centerBoxHeight / 2), width=centerBoxWidth, height=centerBoxHeight, ec='#000000', fill=False) r2 = patches.Rectangle(xy=(-otherBoxDistance - otherBoxWidth / 2, -otherBoxDistance - otherBoxHeight / 2), width=otherBoxWidth, height=otherBoxHeight, ec='#000000', fill=False) r3 = patches.Rectangle(xy=(+otherBoxDistance - otherBoxWidth / 2, -otherBoxDistance - otherBoxHeight / 2), width=otherBoxWidth, height=otherBoxHeight, ec='#000000', fill=False) r4 = patches.Rectangle(xy=(-otherBoxDistance - otherBoxWidth / 2, +otherBoxDistance - otherBoxHeight / 2), width=otherBoxWidth, height=otherBoxHeight, ec='#000000', fill=False) r5 = patches.Rectangle(xy=(+otherBoxDistance - otherBoxWidth / 2, +otherBoxDistance - otherBoxHeight / 2), width=otherBoxWidth, height=otherBoxHeight, ec='#000000', fill=False) a1 = patches.Arrow(myPosX, myPosY, np.cos(myDirect) * 15, np.sin(myDirect) * 15, 10, ec='#0000FF') c1 = patches.Circle(xy=(myPosX, myPosY), radius=6, ec='#0000FF') ax.add_patch(r1) ax.add_patch(r2) ax.add_patch(r3) ax.add_patch(r4) ax.add_patch(r5) ax.add_patch(a1) ax.add_patch(c1) x = np.linspace(-fieldWidth, fieldWidth - 1, fieldWidth * 2) y = x + fieldHeight ax.plot(x, y, "r-") x = np.linspace(-fieldWidth, fieldWidth - 1, fieldWidth * 2) y = x - fieldHeight ax.plot(x, y, "r-") x = np.linspace(-fieldWidth, fieldWidth - 1, fieldWidth * 2) y = -x + fieldHeight ax.plot(x, y, "r-") x = np.linspace(-fieldWidth, fieldWidth - 1, fieldWidth * 2) y = -x - fieldHeight ax.plot(x, y, "r-")
letters[key][1], key, fontsize=10, horizontalalignment='center', verticalalignment='center', color='r') for key in real_node_positions: ax.text(real_node_positions[key][0], real_node_positions[key][1], key, fontsize=12, horizontalalignment='center', verticalalignment='center', color='b') circle = patches.Circle(real_node_positions[key], 0.5, facecolor='y') ax.add_patch(circle) # colors = {} # for key in real_node_positions: # color = list(np.random.choice(range(255), size=3)/255.0) # colors[key] = color distances = {} for key in real_node_positions: distances[key] = {} for test_letter in test_letters: for key in real_node_positions: #ax.text(real_node_positions[key][0], real_node_positions[key][1], key, fontsize=12, horizontalalignment='center', color='g')
# random dots in data space, and overlays a semi-transparent # :class:`~matplotlib.patches.Circle` centered in the middle of the axes # with a radius one quarter of the axes -- if your axes does not # preserve aspect ratio (see :meth:`~matplotlib.axes.Axes.set_aspect`), # this will look like an ellipse. Use the pan/zoom tool to move around, # or manually change the data xlim and ylim, and you will see the data # move, but the circle will remain fixed because it is not in *data* # coordinates and will always remain at the center of the axes. fig, ax = plt.subplots() x, y = 10 * np.random.rand(2, 1000) ax.plot(x, y, 'go', alpha=0.2) # plot some data in data coordinates circ = mpatches.Circle((0.5, 0.5), 0.25, transform=ax.transAxes, facecolor='blue', alpha=0.75) ax.add_patch(circ) plt.show() ############################################################################### # .. _blended_transformations: # # Blended transformations # ======================= # # Drawing in *blended* coordinate spaces which mix *axes* with *data* # coordinates is extremely useful, for example to create a horizontal # span which highlights some region of the y-data but spans across the # x-axis regardless of the data limits, pan or zoom level, etc. In fact
def test_dataloader_getitem(self): unloader = transforms.ToPILImage() self.normalized_labels = True # img_path = 'e:/ML_data/images/train2014/COCO_train2014_000000000092.jpg' # label_path = 'e:/ML_data/labels/train2014/COCO_train2014_000000000092.txt' img_path = 'E:/trafficlight_detect/light_img/IMG_20181124_125821.jpg' label_path = 'E:/trafficlight_detect/labels/IMG_20181124_125821.txt' # Extract image as PyTorch tensor img = transforms.ToTensor()(Image.open(img_path).convert('RGB')) fig, ax = plt.subplots(1) image_orig = unloader(img) ax.imshow(image_orig) #ax.add_patch( patches.Rectangle( xy=( 225, 131), width=75, height=50, linewidth=1 )) plt.show() # Handle images with less than three channels if len(img.shape) != 3: img = img.unsqueeze(0) img = img.expand((3, img.shape[1:])) _, h, w = img.shape h_factor, w_factor = (h, w) if self.normalized_labels else (1, 1) img, pad = pad_to_square(img, 0) _, padded_h, padded_w = img.shape image_padded = unloader(img) fig, ax = plt.subplots(1) ax.imshow(image_padded) plt.show() boxes = torch.from_numpy(np.loadtxt(label_path).reshape(-1, 5)) x1 = w_factor * (boxes[:, 1] - boxes[:, 3] / 2) y1 = h_factor * (boxes[:, 2] - boxes[:, 4] / 2) x2 = w_factor * (boxes[:, 1] + boxes[:, 3] / 2) y2 = h_factor * (boxes[:, 2] + boxes[:, 4] / 2) fig, ax = plt.subplots(1) ax.imshow(image_orig) #ax.add_patch( patches.Rectangle( xy=( 225, 131), width=75, height=50, linewidth=1 )) for box_i, (xx1, yy1, xx2, yy2) in enumerate(zip(x1, y1, x2, y2)): ax.add_patch( patches.Rectangle(xy=(xx1.item(), yy1.item()), width=(xx2 - xx1).item(), height=(yy2 - yy1).item(), linewidth=1)) plt.show() # Adjust for added padding x1 += pad[0] y1 += pad[2] x2 += pad[1] y2 += pad[3] fig, ax = plt.subplots(1) ax.imshow(image_padded) #ax.add_patch( patches.Rectangle( xy=( 225, 131), width=75, height=50, linewidth=1 )) for box_i, (xx1, yy1, xx2, yy2) in enumerate(zip(x1, y1, x2, y2)): ax.add_patch( patches.Rectangle(xy=(xx1.item(), yy1.item()), width=(xx2 - xx1).item(), height=(yy2 - yy1).item(), linewidth=1)) plt.show() boxes[:, 1] = ((x1 + x2) / 2) / padded_w boxes[:, 2] = ((y1 + y2) / 2) / padded_h boxes[:, 3] = boxes[:, 3] * w_factor / padded_w boxes[:, 4] = boxes[:, 4] * h_factor / padded_h fig, ax = plt.subplots(1) ax.imshow(image_padded) #ax.add_patch( patches.Rectangle( xy=( 225, 131), width=75, height=50, linewidth=1 )) for box_i in range(len(boxes)): x = boxes[box_i, 1] * padded_w y = boxes[box_i, 2] * padded_h width = boxes[box_i, 3] * padded_w height = boxes[box_i, 3] * padded_h ax.add_patch( patches.Circle(xy=(x.item(), y.item()), radius=10, linewidth=1)) plt.show()
fig = plt.figure(1, figsize=(8,8)) ax = fig.add_axes([0, 0, 1, 1]) ax.set_xlim(-1, 2*N+1) ax.set_ylim(-1, 2*N+1) for i in range(0, no): if i is FcNo: nd_color = "r" elif i in BdNo: nd_color = "b" else: nd_color = "c" node_plot = patches.Circle((No[i, 0] + v[i*2], No[i,1] + v[i*2+1]), 0.1, color=nd_color) ax.add_patch(node_plot) t = plt.text(No[i,0], No[i,1], str(i)) for i in range(0, len(St)): if i in BrokenSt: st_color = "y" else: st_color = "b" strut_plot = Line2D((No[St[i, 0], 0] + v[St[i, 0] * 2], No[St[i, 1], 0] + v[St[i, 1] * 2]), (No[St[i, 0], 1] + v[St[i, 0] * 2 + 1], No[St[i, 1], 1] + v[St[i, 1] * 2 + 1]), color=st_color) ax.add_line(strut_plot) t = plt.text(
def launch_simulation(size_scene,obstacles,exits,agents,time_simulation,dt): history_agents = [] print "Initialization of the agents..." #create navigation field for each agent precision = 0.5#nodes per unity navigation_maps = {} for agent in agents: if agent.size not in navigation_maps: debug_precision = precision #to make sure that precision*agent.size is an integer if not precision*agent.size==int(precision*agent.size): debug_precision = 1 graph = GridGraph(size_scene,debug_precision) graph.prepare_graph_for_fast_marching(obstacles,exits,agent) fast_marching_method(graph, graph.to_node(agent.position)) navigation_maps[agent.size] = graph #shows the distance map to the exit after applying the fast marching method #imshow(graph.distances,interpolation='nearest',origin='lower') #show() for agent in agents: agent.navigation_map = navigation_maps[agent.size] loading_bar = 0 print "Simulation running..." #start simulation for t in range(int(time_simulation/dt)): #loading bar if int(t/float(int(time_simulation/dt))*100)==loading_bar: print "#", loading_bar = loading_bar+10 #initialize patches for the agents for one frame of the animation patches_agents = [] numpy.random.seed(1) pick_agent = numpy.random.choice(len(agents),len(agents),replace=False) for i in pick_agent: #update the speed of the agent agents[i].update_speed(agents,obstacles) #update the position of the agent agents[i].update_position(agents,obstacles,exits,dt,size_scene) #add patch of the agent for one frame patches_agents.append(patches.Circle(agents[i].position,agents[i].size,color=[agents[i].get_color_agent(),0,0])) # history_agents.append(patches_agents) agents = [v for i, v in enumerate(agents) if not v.has_reached_exit] if not agents: break; print '' print 'Simulation finished at time '+str((t+1)*dt)+'s' display_simulation(history_agents, obstacles, exits, size_scene,dt,3)
def plotting(t): print(t) #Create Fig fig = plt.figure(num=None, figsize=(18, 9), facecolor='w', edgecolor='k') #Loop over subplots for run in range(1, 7): #loop over u u_run_name = [ 'ul_no', 'ul_small', 'ul_tall', 'uh_no', 'uh_small', 'uh_tall' ] u_model_run = eval(u_run_name[run - 1]) #loop over v v_run_name = [ 'vl_no', 'vl_small', 'vl_tall', 'vh_no', 'vh_small', 'vh_tall' ] v_model_run = eval(v_run_name[run - 1]) # #loop over w # w_run_name = ['wl_no', 'wl_small', 'wl_tall','wh_no', 'wh_small', 'wh_tall'] # w_model_run = eval(w_run_name[run-1]) #loop over z z_run_name = [ 'zl_no', 'zl_small', 'zl_tall', 'zh_no', 'zh_small', 'zh_tall' ] z_model_run = eval(z_run_name[run - 1]) ############################# Plot xy ################################### ax = plt.subplot(230 + run, aspect='equal') plt.subplots_adjust(left=0.06, bottom=0.15, right=0.98, top=0.95, wspace=0.06, hspace=0.01) ax.axis('equal') ax.axis('off') #Plot U-Wind wind_plot_xy = ax.contourf(u_model_run[t, :, :], levels, cmap=shifted_cmap, extend='both', alpha=1, zorder=3) # #Plot W-Wind # w_wind_int = np.copy(np.mean(w_model_run[t:t+10,:,:,:], axis = (0,1))) # w_wind = np.copy(scipy.ndimage.filters.uniform_filter(w_wind_int, 20)) # w_wind_plot = ax.contour(w_wind, wlevels, colors = 'k', extend = 'both', alpha = 1, zorder = 12, linewidths = 1.7) # #clab = ax.clabel(w_wind_plot, w_wind_plot.levels, fontsize=11, inline=1, zorder = 13, fmt='%1.2f') # #plt.setp(clab, path_effects=[PathEffects.withStroke(linewidth=1.5, foreground="w")], zorder = 12) # plt.setp(w_wind_plot.collections, path_effects=[PathEffects.withStroke(linewidth=2, foreground="w")], zorder = 12) #Wind Vectors U_t_int = np.copy(u_model_run[t, :, :]) U_t = np.copy(scipy.ndimage.filters.uniform_filter(U_t_int, 10)) V_t_int = np.copy(v_model_run[t, :, :]) V_t = np.copy(scipy.ndimage.filters.uniform_filter(V_t_int, 10)) yy = np.arange(5, far - near, 30) xx = np.arange(0, right - left, 30) points = np.meshgrid(yy, xx) y, x = np.meshgrid(yy, xx) quiv = ax.quiver(x, y, U_t[points], V_t[points], zorder=3, edgecolors='w', linewidth=0.5, scale=190) #Land, water, terrain levels_water = [1.5, 2.5] terrain_levels = np.arange(0, 300.1, 200) terrain_levels_tall = np.arange(0, 2200.1, 400) terrain_ticks = np.arange(0, 2500.1, 250) #Plot Lake water = plt.contour(land, levels_water, alpha=1, colors=('blue'), zorder=4, linewidths=4) #Plot Terrain try: terrain = plt.contour(z_model_run, terrain_levels, alpha=1, colors='w', vmin=-800, vmax=3000, zorder=4, linewidths=1.75) #Plot Point at peak peak = np.where(z_model_run == np.max(z_model_run)) ax.add_patch( patches.Circle((peak[1][0], peak[0][0]), 7, color='w', zorder=20)) except: pass #Draw border ax.add_patch( patches.Rectangle((1.5, 0), right - left - 3, far - near - 1, fill=False, zorder=10, linewidth=2)) #Titles if run == 1: plt.title("No Mountain", fontsize=26, y=0.98) ax.text(-0.08, 0.67, 'Low Wind', transform=ax.transAxes, fontsize=26, rotation=90) if run == 2: plt.title("500m Mountain", fontsize=26, y=0.98) if run == 3: plt.title("2000m Mountain", fontsize=26, y=0.98) if run == 4: ax.text(-0.08, 0.67, 'High Wind', transform=ax.transAxes, fontsize=26, rotation=90) if run == 6: props = dict(boxstyle='square', facecolor='white', alpha=1, linewidth=2) ax.text(0.35, -0.2, 'Elapsed time: {:,} seconds'.format(t * 90), transform=ax.transAxes, bbox=props, fontsize=16) #Colorbars cbaxes = fig.add_axes([0.27, 0.09, 0.5, 0.045]) cbar = plt.colorbar(wind_plot_xy, orientation='horizontal', cax=cbaxes, ticks=levels_ticks) cbar.ax.tick_params(labelsize=19) plt.text(0.35, -1.85, 'U-wind $\mathregular{[ms^{-1}]}$', fontsize=24) #Quiver plt.quiverkey(quiv, X=-1.65, Y=-0.2, U=20, linewidth=0.75, color='k', label='20 $\mathregular{ms^{-1}}$', labelpos='W', fontproperties={'size': '28'}) #Save and Close plt.savefig( "/uufs/chpc.utah.edu/common/home/u1013082/public_html/phd_plots/cm1/png_for_gifs/flow_terrain_{:03d}.png" .format(t), dpi=65) plt.close(fig) plt.switch_backend('Agg')
def plot_ppi_crosshair(site, ranges, angles=None, proj=None, elev=0., ax=None, **kwargs): """Plots a Crosshair for a Plan Position Indicator (PPI). .. versionchanged:: 0.6.0 using osr objects instead of PROJ.4 strings as parameter Parameters ---------- site : tuple Tuple of coordinates of the radar site. If `proj` is not used, this simply becomes the offset for the origin of the coordinate system. If `proj` is used, values must be given as (longitude, latitude) tuple of geographical coordinates. ranges : list List of ranges, for which range circles should be drawn. If `proj` is None arbitrary units may be used (such that they fit with the underlying PPI plot. Otherwise the ranges must be given in meters. angles : list List of angles (in degrees) for which straight lines should be drawn. These lines will be drawn starting from the center and until the largest range. proj : osr spatial reference object GDAL OSR Spatial Reference Object describing projection The function will calculate lines and circles according to georeferenced coordinates taking beam propagation, earth's curvature and scale effects due to projection into account. Depending on the projection, crosshair lines might not be straight and range circles might appear elliptical (also check if the aspect of the axes might not also be responsible for this). elev : float or array of same shape as az Elevation angle of the scan or individual azimuths. May improve georeferencing coordinates for larger elevation angles. ax : matplotlib Axes object If given, the crosshair will be plotted into this axes object. If None matplotlib's current axes (function gca()) concept will be used to determine the axes. Keyword Arguments ----------------- line : dict dictionary, which will be passed to the crosshair line objects using the standard keyword inheritance mechanism. If not given defaults will be used. circle : dict dictionary, which will be passed to the range circle line objects using the standard keyword inheritance mechanism. If not given defaults will be used. See the file plot_ppi_example.py in the examples folder for examples on how this works. See also -------- wradlib.vis.plot_ppi - plotting a PPI in cartesian coordinates Returns ------- ax : matplotlib Axes object The axes object into which the PPI was plotted """ # if we didn't get an axes object, find the current one if ax is None: ax = pl.gca() if angles is None: angles = [0, 90, 180, 270] # set default line keywords linekw = dict(color='gray', linestyle='dashed') # update with user settings linekw.update(kwargs.get('line', {})) # set default circle keywords circkw = dict(edgecolor='gray', linestyle='dashed', facecolor='none') # update with user settings circkw.update(kwargs.get('circle', {})) # determine coordinates for 'straight' lines if proj: # projected # reproject the site coordinates psite = georef.reproject(*site, projection_target=proj) # these lines might not be straigt so we approximate them with 10 # segments. Produce polar coordinates rr, az = np.meshgrid(np.linspace(0, ranges[-1], 10), angles) # and reproject using polar2lonlatalt to convert from polar to geographic nsewx, nsewy = georef.reproject(*georef.polar2lonlatalt_n(rr, az, elev, site)[:2], projection_target=proj) else: # no projection psite = site rr, az = np.meshgrid(np.linspace(0, ranges[-1], 2), angles) # use simple trigonometry to calculate coordinates nsewx, nsewy = (psite[0] + rr * np.cos(np.radians(az)), psite[1] + rr * np.sin(np.radians(az))) # mark the site, just in case nothing else would be drawn ax.plot(*psite, marker='+', **linekw) # draw the lines for i in range(len(angles)): ax.add_line(mpl.lines.Line2D(nsewx[i, :], nsewy[i, :], **linekw)) # draw the range circles for r in ranges: if proj: # produce an approximation of the circle x, y = georef.reproject(*georef.polar2lonlatalt_n(r, np.arange(360), elev, site)[:2], projection_target=proj) ax.add_patch(patches.Polygon(np.concatenate([x[:, None], y[:, None]], axis=1), **circkw)) else: # in the unprojected case, we may use 'true' circles. ax.add_patch(patches.Circle(psite, r, **circkw)) # there should be not much wrong, setting the axes aspect to equal by default ax.set_aspect('equal') # return the axes object for later use return ax
def plot_size(self, npREF): ######################################################################## ######################################################################## ########################### model ############################## ######################################################################## #''' ax = plt.axes() COUNT = np.arange(start=0, stop=426, step=1, dtype=int) ssp = [-0.5, -0.5] for count in COUNT: r = patches.Rectangle(xy=(npREF.T[0][count] - 0.35, npREF.T[1][count] - 0.35), width=0.7, height=0.7, ec='#F5A9A9', fill=False) ax.add_patch(r) #c = patches.Circle(xy=(npNEW_LOB.T[0][count],npNEW_LOB.T[1][count]), radius=0.4, ec='#F5A9A9',fill=False) #ax.add_patch(c) ############################ field ############################# ######################################################################## sotowaku = patches.Rectangle(xy=(-13.3 + 0.5, -0.5), width=13.3, height=10, ec='#FAAC58', fill=False) ax.add_patch(sotowaku) forest = patches.Rectangle(xy=(-2.45 + 0.5, -0.5), width=2.45, height=8, ec='#FAAC58', fill=False) ax.add_patch(forest) bridge = patches.Rectangle(xy=(-1.725 + 0.5, 6.5 - 0.5), width=1, height=1.5, ec='#FAAC58', fill=False) ax.add_patch(bridge) bri1 = patches.Circle(xy=(-1.725 + 0.5, 6.5 - 0.5), radius=0.08, ec='#FAAC58', fill=False) ax.add_patch(bri1) bri2 = patches.Circle(xy=(-1.725 + 0.5, 6.5 + 1.5 - 0.5), radius=0.08, ec='#FAAC58', fill=False) ax.add_patch(bri2) d = patches.Circle(xy=(-1 * (1.225 + ssp[0]), 2 + ssp[1]), radius=0.08, ec='#FAAC58', fill=False) ax.add_patch(d) e = patches.Circle(xy=(-1 * (1.225 + ssp[0]), 3.5 + ssp[1]), radius=0.08, ec='#FAAC58', fill=False) ax.add_patch(e) f = patches.Circle(xy=(-1 * (1.225 + ssp[0]), 5 + ssp[1]), radius=0.08, ec='#FAAC58', fill=False) ax.add_patch(f) #''' ######################################################################## ######################################################################## ######################################################################## plt.axis("equal") plt.grid(True) plt.show()
# %% Visualizing the streamlines of a 2D vector field def cylinder_stream_function(U=1, R=1): r = sympy.sqrt(x ** 2 + y ** 2) theta = sympy.atan2(y, x) return U * (r - R ** 2 / r) * sympy.sin(theta) def velocity_field(psi): u = sympy.lambdify((x, y), psi.diff(y), 'numpy') v = sympy.lambdify((x, y), -psi.diff(x), 'numpy') return u, v psi = cylinder_stream_function() U_func, V_func = velocity_field(psi) xmin, xmax, ymin, ymax = -3, 3, -3, 3 Y, X = np.ogrid[ymin:ymax:128j, xmin:xmax:128j] U, V = U_func(X, Y), V_func(X, Y) M = (X ** 2 + Y ** 2) < 1. U = np.ma.masked_array(U, mask=M) V = np.ma.masked_array(V, mask=M) shape = patches.Circle((0, 0), radius=1., lw=2., fc='w', ec='k', zorder=0) plt.gca().add_patch(shape) plt.streamplot(X, Y, U, V, color='k') plt.axes().set_aspect('equal') plt.show()
def circle(self, center, radius, color='blue'): self.ax.add_patch(pch.Circle(center, radius, fill=False, color=color))