def test_scalebar_showhide(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.hide() f.scalebar.show(0.1) f.close()
def test_scalebar_length(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_length(0.01) f.scalebar.set_length(0.1) f.close()
def test_scalebar_addremove(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.remove_scalebar() f.add_scalebar(0.1) f.close()
def test_scalebar_addremove(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.remove_scalebar() f.add_scalebar(0.1) f.close()
def test_scalebar_length(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_length(0.01) f.scalebar.set_length(0.1) f.close()
def test_scalebar_frame(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_frame(True) f.scalebar.set_frame(False) f.close()
def test_scalebar_frame(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_frame(True) f.scalebar.set_frame(False) f.close()
def test_scalebar_showhide(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.hide() f.scalebar.show(0.1) f.close()
def test_scalebar_font(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_font(size='small', weight='bold', stretch='normal', family='serif', style='normal', variant='normal') f.close()
def test_scalebar_alpha(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_alpha(0.1) f.scalebar.set_alpha(0.2) f.scalebar.set_alpha(0.5) f.close()
def test_scalebar_alpha(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_alpha(0.1) f.scalebar.set_alpha(0.2) f.scalebar.set_alpha(0.5) f.close()
def test_scalebar_color(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_color('black') f.scalebar.set_color('#003344') f.scalebar.set_color((1.0, 0.4, 0.3)) f.close()
def test_scalebar_corner(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) for corner in ['top', 'bottom', 'left', 'right', 'top left', 'top right', 'bottom left', 'bottom right']: f.scalebar.set_corner(corner) f.close()
def test_scalebar_label(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_label('1 pc') f.scalebar.set_label('5 AU') f.scalebar.set_label('2"') f.close()
def test_scalebar_label(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_label('1 pc') f.scalebar.set_label('5 AU') f.scalebar.set_label('2"') f.close()
def test_scalebar_color(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_color('black') f.scalebar.set_color('#003344') f.scalebar.set_color((1.0, 0.4, 0.3)) f.close()
def test_scalebar_corner(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) for corner in [ 'top', 'bottom', 'left', 'right', 'top left', 'top right', 'bottom left', 'bottom right' ]: f.scalebar.set_corner(corner) f.close()
def test_scalebar_font(): data = np.zeros((16, 16)) f = FITSFigure(data) f.add_scalebar(0.1) f.scalebar.set_font(size='small', weight='bold', stretch='normal', family='serif', style='normal', variant='normal') f.close()
def test_scalebar_add_invalid(): data = np.zeros((16, 16)) f = FITSFigure(data) with pytest.raises(TypeError): f.add_scalebar()
def test_scalebar_add_invalid(): data = np.zeros((16, 16)) f = FITSFigure(data) with pytest.raises(TypeError): f.add_scalebar()
def make_polmap(filename, title=None, figure=None, subplot=(1, 1, 1)): hawc = fits.open(filename) p = hawc['DEBIASED PERCENT POL'] # % theta = hawc['ROTATED POL ANGLE'] # deg stokes_i = hawc['STOKES I'] # I error_i = hawc['ERROR I'] # error I # 1. plot Stokes I # convert from Jy/pix to Jy/sq. arcsec pxscale = stokes_i.header['CDELT2'] * 3600 # map scale in arcsec/pix stokes_i.data /= pxscale**2 error_i.data /= pxscale**2 fig = FITSFigure(stokes_i, figure=figure, subplot=subplot) # 2. perform S/N cut on I/\sigma_I err_lim = 100 mask = np.where(stokes_i.data / error_i.data < err_lim) # 3. mask out low S/N vectors by setting masked indices to NaN p.data[mask] = np.nan # 4. plot vectors scalevec = 0.4 # 1pix = scalevec * 1% pol # scale vectors to make it easier to see fig.show_vectors(p, theta, scale=scalevec, step=2) # step size = display every 'step' vectors # step size of 2 is effectively Nyquist sampling # --close to the beam size # 5. plot contours ncontours = 30 fig.show_contour(stokes_i, cmap=cmap, levels=ncontours, filled=True, smooth=1, kernel='box') fig.show_contour(stokes_i, colors='gray', levels=ncontours, smooth=1, kernel='box', linewidths=0.3) # Show image fig.show_colorscale(cmap=cmap) # If title, set it if title: fig.set_title(title) # Add colorbar fig.add_colorbar() fig.colorbar.set_axis_label_text('Flux (Jy/arcsec$^2$)') # Add beam indicator fig.add_beam(facecolor='red', edgecolor='black', linewidth=2, pad=1, corner='bottom left') fig.add_label(0.02, 0.02, 'Beam FWHM', horizontalalignment='left', weight='bold', relative=True, size='small') # Add vector scale # polarization vectors are displayed such that 'scalevec' * 1% pol is 1 pix long # must translate pixel size to angular degrees since the 'add_scalebar' function assumes a physical scale vectscale = scalevec * pxscale / 3600 fig.add_scalebar(5 * vectscale, "p = 5%", corner='top right', frame=True) return stokes_i, p, mask, fig
def polmap(self, idi=50.0, pdp=3.0, pmax=30.0, color='rainbow', scalevec=0.4, clevels=100, imin=0.0, imax=None, size_x=9.0, size_y=9.0, dpi=100.0): # Initialization # idi : Signal-to-noise ratio for Stokes I total intensity. Default is # idi = 50.0, which leads to an upper limit of at least dP = 5.0% # for the error on the polarization fraction P. # pdp : Signal-to-noise ratio for the de-biased polarization fraction P. # Default is pdp = 3.0, which is the commonly accepted detection # threshold in the litterature. # pmax : Maximum polarization fraction allowed. # color : Color scheme used for the plots. Default is rainbow. # scalevec : Length of the vectors plotted on the map. # clevels : Number of contours plotted on the contour plot. # imin : Minimum value plotted in the Stokes I intensity map. # Default is 0.0. # imax : Maximum value plotted in the Stokes I intensity map. # If no value is provided, maximum is automatically detected. # size_x : Width of the figure in inches. # size_y : Height of the figure in inches. # dpi : Number of pixels per inch for the figure. print() print('======================================') print('Plotting a delightful polarization map') print('======================================') print() # Creating a new fits object for APLpy's FITSFigure method to recognize # for the Stokes I total intensity plot_hdu = fits.PrimaryHDU(data=self.I, header=self.header) # Loading the data in an APLpy figure polmap = FITSFigure(plot_hdu, dpi=dpi, figsize=(size_x, size_y)) print('Plotting the Stokes I total intensity map') print() # Showing the pixelated image and setting the aspect ratio polmap.show_colorscale(cmap=color, vmin=imin, vmax=imax) # Plotting a filled contour plot of the data polmap.show_contour(cmap=color, levels=clevels, filled=True, extend='both', vmin=imin, vmax=imax) # Adding a colorbar to the plot polmap.add_colorbar(pad=0.125) # Adding the units to the colorbar polmap.colorbar.set_axis_label_text(self.units) # Moving the colorbar to be on top of the figure polmap.colorbar.set_location('top') # Creating a new fits object for APLpy's FITSFigure method to recognize # for the polarization fraction P pref = fits.PrimaryHDU(data=self.P, header=self.header) pmap = pref.copy() # for the magnetic field angle B oref = fits.PrimaryHDU(data=self.O, header=self.header) omap = oref.copy() # A copy of the Primary HDU is created before any masking is done. # This fixes an issue where modifying pmap was carried to other # instances of this method. Future fix should make sure pref is # closed/deleted at the end of this method. # Muting the numpy alerts triggered by nan values np.warnings.filterwarnings('ignore') print() print('Applying happy selection criteria on polarization vectors') print() # Creating a mask to hide polarization vectors with low signal-to-noise ratios imask_01 = np.where( self.I / self.dI < idi) # Total intensity SNR threshold imask_02 = np.where(self.I < 0.0) # Total intensity positive threshold pmask = np.where(self.P / self.dP < pdp) # Polarization SNR threshold pmaxmask = np.where(self.P > pmax) # Polarization SNR threshold # Masking all the indices for which the selection criteria failed pmap.data[imask_01] = np.nan pmap.data[imask_02] = np.nan pmap.data[pmask] = np.nan pmap.data[pmaxmask] = np.nan print('Plotting the polarization vectors') print() # Plotting the polarization vectors polmap.show_vectors(pmap, omap, scale=scalevec) # Adding the beam size polmap.add_beam(facecolor='white', edgecolor='black', linewidth=2, pad=1, corner='bottom left') # Adding the vector scale bar vectscale = scalevec * self.pixel / 3600 polmap.add_scalebar(5 * vectscale, "p = 5%", corner='top right', frame=True) # Removing the pixelated structure under the figure polmap.hide_colorscale() print('Returning the APLpy figure as output, please feel free to' + ' improve it (see online APLpy.FITSFigure documentation)') print() print('Don\'t forget to save the results using the' + ' .savefig(\'name.png\') function') print() print('Don\'t give up!') return polmap
#gc.show_contour(levels=levelsI,\ # filled=True,smooth=1,kernel='box',cmap='viridis') #beam gc.add_beam(color='red') #title gc.set_title(title, fontsize=title_size) ##Pol map p = quality_cuts(stkI, stkIerr, p, perr, SNRp_cut, p_cut, SNRi_cut) #2. polmap gc.show_vectors(p,pa,scale=scalevec,\ step=2,color='black',linewidth=4.0) gc.show_vectors(p,pa,scale=scalevec,\ step=2,color='grey',linewidth=2.0) #show label gc.add_label(0.1,0.95,'Total Flux',fontsize=label_fontsize,\ color='white',weight='bold',relative=True) #legend vector vecscale = scalevec * pxscale / 3600 gc.add_scalebar(vec_legend*vecscale,'P ='+np.str(vec_legend)+'%',\ corner='bottom right',frame=True,color='black',facecolor='blue') #Figure parameters gc.tick_labels.set_font(size=tick_labels) gc.axis_labels.set_font(size=label_plot) fig.savefig(figname, dpi=300) os.system('open ' + figname)