def contam(cube, instrument, targetName='noName', paRange=[0, 360], badPAs=np.asarray([]), tmpDir="", fig='', to_html=True): # Get data from FITS file if isinstance(cube, str): hdu = fits.open(cubeName) cube = hdu[0].data hdu.close() if instrument != 'NIRISS': trace1 = cube[0, :, :] # target star order 1 trace cube = cube[1:, :, :] # neighbor star order 1 and 2 traces in all the angles elif instrument=='NIRISS': trace1 = cube[0, :, :] trace2 = cube[1, :, :] cube = cube[2:, :, :] plotPAmin, plotPAmax = paRange # start calculations if not TRACES_PATH: return None lam_file = os.path.join(TRACES_PATH, 'NIRISS', 'lambda_order1-2.txt') ypix, lamO1, lamO2 = np.loadtxt(lam_file, unpack=True) nPA = cube.shape[0] rows = cube.shape[1] cols = cube.shape[2] print('cols ', cols) dPA = 360//nPA PA = np.arange(nPA)*dPA contamO1 = np.zeros([rows, nPA]) if instrument=='NIRISS': contamO2 = np.zeros([rows, nPA]) for row in np.arange(rows): i = np.argmax(trace1[row, :]) #tr = trace1[row, i-20:i+41] tr = trace1[row, i-20:i+41] w = tr/np.sum(tr**2) ww = np.tile(w, nPA).reshape([nPA, tr.size]) contamO1[row, :] = np.sum(cube[:, row, i-20:i+41]*ww, axis=1) if instrument=='NIRISS': if lamO2[row] < 0.6: continue i = np.argmax(trace2[row, :]) tr = trace2[row, i-20:i+41] w = tr/np.sum(tr**2) ww = np.tile(w, nPA).reshape([nPA, tr.size]) contamO2[row, :] = np.sum(cube[:, row, i-20:i+41]*ww, axis=1) TOOLS = 'pan, box_zoom, crosshair, reset, hover' y = np.array([0., 0.]) y1 = 0.07 y2 = 0.12 y3 = 0.17 y4 = 0.23 bad_PA_color = '#dddddd' bad_PA_alpha = 0.7 # Order 1~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Contam plot if instrument == 'NIRISS': xlim0 = lamO1.min() xlim1 = lamO1.max() elif instrument == 'NIRCam F322W2': xlim0 = lam0_nircam322w2 xlim1 = lam1_nircam322w2 elif instrument == 'NIRCam F444W': xlim0 = lam0_nircam444w xlim1 = lam1_nircam444w elif instrument == 'MIRI': xlim0 = 5 xlim1 = 12 ylim0 = PA.min()-0.5*dPA ylim1 = PA.max()+0.5*dPA color_mapper = LinearColorMapper(palette=inferno(8)[::-1], low=-4, high=1) color_mapper.low_color = 'white' color_mapper.high_color = 'black' s2 = figure(tools=TOOLS, width=500, height=500, title='Order 1 {} Contamination with {}'.format(targetName, instrument), x_range=Range1d(xlim0, xlim1), y_range=Range1d(ylim0, ylim1)) fig_data = np.log10(np.clip(contamO1.T, 1.e-10, 1.)) s2.image([fig_data], x=xlim0, y=ylim0, dw=xlim1-xlim0, dh=ylim1-ylim0, color_mapper=color_mapper) s2.xaxis.axis_label = 'Wavelength (um)' if instrument != 'NIRISS': s2.yaxis.axis_label = 'Aperture Position Angle (degrees)' # Add bad PAs bad_PA_color = '#555555' bad_PA_alpha = 0.6 #for ybad0, ybad1 in badPA: if len(badPAs)>0: tops, bottoms, lefts, rights = [], [], [], [] for idx in range(0, len(badPAs)): PAgroup = badPAs[idx] top_idx = np.max(PAgroup) bot_idx = np.min(PAgroup) tops.append(top_idx) bottoms.append(bot_idx) lefts.append(xlim0) rights.append(xlim1) s2.quad(top=tops, bottom=bottoms, left=lefts, right=rights, color=bad_PA_color, alpha=bad_PA_alpha) # Line plot s3 = figure(tools=TOOLS, width=150, height=500, x_range=Range1d(0, 100), y_range=s2.y_range, title=None) s3.line(100*np.sum(contamO1 >= 0.001, axis=0)/rows, PA-dPA/2, line_color='blue', legend='> 0.001') s3.line(100*np.sum(contamO1 >= 0.01, axis=0)/rows, PA-dPA/2, line_color='green', legend='> 0.01') s3.xaxis.axis_label = '% channels contam.' s3.yaxis.major_label_text_font_size = '0pt' # ~~~~~~ Order 2 ~~~~~~ # Contam plot if instrument=='NIRISS': xlim0 = lamO2.min() xlim1 = lamO2.max() ylim0 = PA.min()-0.5*dPA ylim1 = PA.max()+0.5*dPA xlim0 = 0.614 s5 = figure(tools=TOOLS, width=500, height=500, title='Order 2 {} Contamination with {}'.format(targetName, instrument), x_range=Range1d(xlim0, xlim1), y_range=s2.y_range) fig_data = np.log10(np.clip(contamO2.T, 1.e-10, 1.))[:, 300:] s5.image([fig_data], x=xlim0, y=ylim0, dw=xlim1-xlim0, dh=ylim1-ylim0, color_mapper=color_mapper) #s5.yaxis.major_label_text_font_size = '0pt' s5.xaxis.axis_label = 'Wavelength (um)' s5.yaxis.axis_label = 'Aperture Position Angle (degrees)' if len(badPAs)>0: tops, bottoms, lefts, rights = [], [], [], [] for idx in range(0, len(badPAs)): PAgroup = badPAs[idx] top_idx = np.max(PAgroup) bot_idx = np.min(PAgroup) tops.append(top_idx) bottoms.append(bot_idx) lefts.append(xlim0) rights.append(xlim1) s5.quad(top=tops, bottom=bottoms, left=lefts, right=rights, color=bad_PA_color, alpha=bad_PA_alpha) # Line plot s6 = figure(tools=TOOLS, width=150, height=500, y_range=s2.y_range, x_range=Range1d(100, 0), title=None) s6.line(100*np.sum(contamO2 >= 0.001, axis=0)/rows, PA-dPA/2, line_color='blue', legend='> 0.001') s6.line(100*np.sum(contamO2 >= 0.01, axis=0)/rows, PA-dPA/2, line_color='green', legend='> 0.01') s6.xaxis.axis_label = '% channels contam.' s6.yaxis.major_label_text_font_size = '0pt' if len(badPAs)>0: tops, bottoms, lefts, rights = [], [], [], [] for idx in range(0, len(badPAs)): PAgroup = badPAs[idx] top_idx = np.max(PAgroup) bot_idx = np.min(PAgroup) tops.append(top_idx) bottoms.append(bot_idx) lefts.append(0) rights.append(100) s3.quad(top=tops, bottom=bottoms, left=lefts, right=rights, color=bad_PA_color, alpha=bad_PA_alpha) if instrument=='NIRISS': s6.quad(top=tops, bottom=bottoms, left=rights, right=lefts, color=bad_PA_color, alpha=bad_PA_alpha) # ~~~~~~ Plotting ~~~~~~ if instrument!='NIRISS': fig = gridplot(children=[[s2, s3]]) else: fig = gridplot(children=[[s6, s5, s2, s3]]) return fig
def contam(cube, targetName='noName', paRange=[0, 360], badPA=[], tmpDir="", fig='', to_html=True): """ Generate the contamination plot. Parameters ---------- cube: array-like, str The data cube or FITS filename containing the data. targetName: str The name of the target. paRange: sequence The position angle range to consider. badPA: sequence Position angles to exclude. tmpDir: str A directory to write the files to. fig: matplotlib.figure, bokeh.figure A figure to add the plots to. to_html: bool Return the image as bytes for HTML. Returns ------- fig : matplotlib.pyplot or bokeh object The populated matplotlib or bokeh plot. """ # Get data from FITS file if isinstance(cube, str): # hdu = fits.open('cube_'+target+'.fits') hdu = fits.open(cubeName) cube = hdu[0].data hdu.close() trace2dO1 = cube[0, :, :] # order 1 trace2dO2 = cube[1, :, :] # order 2 cube = cube[2:, :, :] # all the angles plotPAmin, plotPAmax = paRange # start calculations loc = 'data/contam_visibility/lambda_order1-2.txt' lam_file = pkg_resources.resource_filename('exoctk', loc) ypix, lamO1, lamO2 = np.loadtxt(lam_file, unpack=True) ny = trace2dO1.shape[0] nPA = cube.shape[0] dPA = 360 // nPA PA = np.arange(nPA) * dPA contamO1 = np.zeros([ny, nPA]) contamO2 = np.zeros([ny, nPA]) for y in np.arange(ny): i = np.argmax(trace2dO1[y, :]) tr = trace2dO1[y, i - 20:i + 41] w = tr / np.sum(tr**2) ww = np.tile(w, nPA).reshape([nPA, tr.size]) contamO1[y, :] = np.sum(cube[:, y, i - 20:i + 41] * ww, axis=1) if lamO2[y] < 0.6: continue i = np.argmax(trace2dO2[y, :]) tr = trace2dO2[y, i - 20:i + 41] w = tr / np.sum(tr**2) ww = np.tile(w, nPA).reshape([nPA, tr.size]) contamO2[y, :] = np.sum(cube[:, y, i - 20:i + 41] * ww, axis=1) # Otherwise, it's a Bokeh plot if fig: TOOLS = 'pan, box_zoom, crosshair, reset, hover, save' y = np.array([0., 0.]) y1 = 0.07 y2 = 0.12 y3 = 0.17 y4 = 0.23 bad_PA_color = '#dddddd' bad_PA_alpha = 0.7 # Order 1 # Contam plot xlim0 = lamO1.min() xlim1 = lamO1.max() ylim0 = PA.min() - 0.5 * dPA ylim1 = PA.max() + 0.5 * dPA color_mapper = LinearColorMapper(palette=inferno(8)[::-1], low=-4, high=1) color_mapper.low_color = 'white' color_mapper.high_color = 'black' s2 = figure(tools=TOOLS, width=500, height=500, title=None, x_range=Range1d(xlim0, xlim1), y_range=Range1d(ylim0, ylim1)) fig_data = np.log10(np.clip(contamO1.T, 1.e-10, 1.)) s2.image([fig_data], x=xlim0, y=ylim0, dw=xlim1 - xlim0, dh=ylim1 - ylim0, color_mapper=color_mapper) s2.xaxis.axis_label = 'Wavelength (um)' s2.yaxis.axis_label = 'Position Angle (degrees)' # Line plot s3 = figure(tools=TOOLS, width=150, height=500, x_range=Range1d(0, 100), y_range=s2.y_range, title=None) s3.line(100 * np.sum(contamO1 >= 0.001, axis=0) / ny, PA - dPA / 2, line_color='blue', legend='> 0.001') s3.line(100 * np.sum(contamO1 >= 0.01, axis=0) / ny, PA - dPA / 2, line_color='green', legend='> 0.01') s3.xaxis.axis_label = '% channels contam.' s3.yaxis.major_label_text_font_size = '0pt' # Add bad PAs for ybad0, ybad1 in badPA: s2.patch([xlim0, xlim1, xlim1, xlim0], [ybad1, ybad1, ybad0, ybad0], color=bad_PA_color, alpha=bad_PA_alpha) s3.patch([0, 100, 100, 0], [ybad1, ybad1, ybad0, ybad0], color=bad_PA_color, alpha=bad_PA_alpha, legend='Bad PA') # Line list s1 = figure(tools=TOOLS, width=500, plot_height=100, x_range=s2.x_range, title=None) l = np.array([0.89, 0.99]) s1.line(l, y + y1, line_color='black', line_width=1.5) s1.add_layout( Label(x=l.mean(), y=y1, x_units='data', y_units='data', text='H2O', render_mode='css', text_font_size='8pt')) l = np.array([1.09, 1.2]) s1.line(l, y + y1, line_color='black', line_width=1.5) s1.add_layout( Label(x=l.mean(), y=y1, x_units='data', y_units='data', text='H2O', render_mode='css', text_font_size='8pt')) l = np.array([1.1, 1.24]) s1.line(l, y + y2, line_color='black', line_width=1.5) s1.add_layout( Label(x=l.mean(), y=y2, x_units='data', y_units='data', text='CH4', render_mode='css', text_font_size='8pt')) l = np.array([1.3, 1.51]) s1.line(l, y + y1, line_color='black', line_width=1.5) s1.add_layout( Label(x=l.mean(), y=y1, x_units='data', y_units='data', text='H2O', render_mode='css', text_font_size='8pt')) l = np.array([1.6, 1.8]) s1.line(l, y + y2, line_color='black', line_width=1.5) s1.add_layout( Label(x=l.mean(), y=y2, x_units='data', y_units='data', text='CH4', render_mode='css', text_font_size='8pt')) l = np.array([1.75, 2.05]) s1.line(l, y + y1, line_color='black', line_width=1.5) s1.add_layout( Label(x=l.mean(), y=y1, x_units='data', y_units='data', text='H2O', render_mode='css', text_font_size='8pt')) l = np.array([2.3, lamO1.max()]) s1.line(l, y + y1, line_color='black', line_width=1.5) s1.add_layout( Label(x=l.mean(), y=y1, x_units='data', y_units='data', text='H2O', render_mode='css', text_font_size='8pt')) l = np.array([2.15, 2.5]) s1.line(l, y + y2, line_color='black', line_width=1.5) s1.add_layout( Label(x=l.mean(), y=y2, x_units='data', y_units='data', text='CH4', render_mode='css', text_font_size='8pt')) l = np.array([1.1692, 1.1778]) s1.line(l[0], [y3, y3 + 0.02], line_color='black') s1.line(l[1], [y3, y3 + 0.02], line_color='black') s1.add_layout( Label(x=l.mean(), y=y3 + 0.02, x_units='data', y_units='data', text='K', render_mode='css', text_font_size='8pt')) l = np.array([1.2437, 1.2529]) s1.line(l[0], [y3, y3 + 0.02], line_color='black') s1.line(l[1], [y3, y3 + 0.02], line_color='black') s1.add_layout( Label(x=l.mean(), y=y3 + 0.02, x_units='data', y_units='data', text='K', render_mode='css', text_font_size='8pt')) l = np.array([1.5168]) s1.line(l[0], [y3, y3 + 0.02], line_color='black') s1.add_layout( Label(x=l.mean(), y=y3 + 0.02, x_units='data', y_units='data', text='K', render_mode='css', text_font_size='8pt')) l = np.array([1.1384, 1.1409]) s1.line(l[0], [y4, y4 + 0.02], line_color='black') s1.line(l[1], [y4, y4 + 0.02], line_color='black') s1.add_layout( Label(x=l.mean(), y=y4 + 0.02, x_units='data', y_units='data', text='Na', render_mode='css', text_font_size='8pt')) l = np.array([1.2682]) s1.line(l[0], [y4, y4 + 0.02], line_color='black') s1.add_layout( Label(x=l.mean(), y=y4 + 0.02, x_units='data', y_units='data', text='Na', render_mode='css', text_font_size='8pt')) l = np.array([2.2063, 2.2090]) s1.line(l[0], [y4, y4 + 0.02], line_color='black') s1.line(l[1], [y4, y4 + 0.02], line_color='black') s1.add_layout( Label(x=l.mean(), y=y4 + 0.02, x_units='data', y_units='data', text='Na', render_mode='css', text_font_size='8pt')) l = np.array([2.2935, 2.3227, 2.3525, 2.3830, 2.4141]) s1.line(l[0], [y3, y3 + 0.02], line_color='black') s1.line(l[1], [y3, y3 + 0.02], line_color='black') s1.line(l[2], [y3, y3 + 0.02], line_color='black') s1.line(l[3], [y3, y3 + 0.02], line_color='black') s1.line(l[4], [y3, y3 + 0.02], line_color='black') s1.line(l[[0, -1]], y + y3 + 0.02, line_color='black', line_width=1) s1.add_layout( Label(x=l[[0, -1]].mean(), y=y3 + 0.02, x_units='data', y_units='data', text='CO', render_mode='css', text_font_size='8pt')) s1.xaxis.major_label_text_font_size = '0pt' s1.yaxis.major_label_text_font_size = '0pt' # Order 2 # Contam plot xlim0 = lamO2.min() xlim1 = lamO2.max() ylim0 = PA.min() - 0.5 * dPA ylim1 = PA.max() + 0.5 * dPA xlim0 = 0.614 s5 = figure(tools=TOOLS, width=250, height=500, title=None, x_range=Range1d(xlim0, xlim1), y_range=s2.y_range) fig_data = np.log10(np.clip(contamO2.T, 1.e-10, 1.))[:, 300:] s5.image([fig_data], x=xlim0, y=ylim0, dw=xlim1 - xlim0, dh=ylim1 - ylim0, color_mapper=color_mapper) s5.yaxis.major_label_text_font_size = '0pt' s5.xaxis.axis_label = 'Wavelength (um)' # Line plot s6 = figure(tools=TOOLS, width=150, height=500, y_range=s2.y_range, x_range=Range1d(100, 0), title=None) s6.line(100 * np.sum(contamO2 >= 0.001, axis=0) / ny, PA - dPA / 2, line_color='blue', legend='> 0.001') s6.line(100 * np.sum(contamO2 >= 0.01, axis=0) / ny, PA - dPA / 2, line_color='green', legend='> 0.01') s6.xaxis.axis_label = '% channels contam.' s6.yaxis.major_label_text_font_size = '0pt' # Dummy plots for nice spacing s0 = figure(tools=TOOLS, width=150, plot_height=100, title=None) s0.outline_line_color = "white" s7 = figure(tools=TOOLS, width=150, plot_height=100, title=targetName) s7.outline_line_color = "white" # Add bad PAs for ybad0, ybad1 in badPA: s5.patch([xlim0, xlim1, xlim1, xlim0], [ybad1, ybad1, ybad0, ybad0], color=bad_PA_color, alpha=bad_PA_alpha) s6.patch([0, 100, 100, 0], [ybad1, ybad1, ybad0, ybad0], color=bad_PA_color, alpha=bad_PA_alpha, legend='Bad PA') # Line list s4 = figure(tools=TOOLS, width=250, plot_height=100, x_range=s5.x_range, title=None) l = np.array([0.89, 0.99]) s4.line(l, y + y1, line_color='black', line_width=1.5) s4.add_layout( Label(x=l.mean(), y=y1, x_units='data', y_units='data', text='H2O', render_mode='css', text_font_size='8pt')) l = np.array([1.09, 1.2]) s4.line(l, y + y1, line_color='black', line_width=1.5) s4.add_layout( Label(x=l.mean(), y=y1, x_units='data', y_units='data', text='H2O', render_mode='css', text_font_size='8pt')) l = np.array([1.1, 1.24]) s4.line(l, y + y2, line_color='black', line_width=1.5) s4.add_layout( Label(x=l.mean(), y=y2, x_units='data', y_units='data', text='CH4', render_mode='css', text_font_size='8pt')) l = np.array([1.3, lamO2.max()]) s4.line(l, y + y1, line_color='black', line_width=1.5) s4.add_layout( Label(x=l.mean(), y=y1, x_units='data', y_units='data', text='H2O', render_mode='css', text_font_size='8pt')) l = np.array([0.7665, 0.7699]) s4.line(l[0], [y3, y3 + 0.02], line_color='black') s4.line(l[1], [y3, y3 + 0.02], line_color='black') s4.add_layout( Label(x=l.mean(), y=y3 + 0.02, x_units='data', y_units='data', text='K', render_mode='css', text_font_size='8pt')) l = np.array([1.1692, 1.1778]) s4.line(l[0], [y3, y3 + 0.02], line_color='black') s4.line(l[1], [y3, y3 + 0.02], line_color='black') s4.add_layout( Label(x=l.mean(), y=y3 + 0.02, x_units='data', y_units='data', text='K', render_mode='css', text_font_size='8pt')) l = np.array([1.2437, 1.2529]) s4.line(l[0], [y3, y3 + 0.02], line_color='black') s4.line(l[1], [y3, y3 + 0.02], line_color='black') s4.add_layout( Label(x=l.mean(), y=y3 + 0.02, x_units='data', y_units='data', text='K', render_mode='css', text_font_size='8pt')) l = np.array([1.1384, 1.1409]) s4.line(l[0], [y4, y4 + 0.02], line_color='black') s4.line(l[1], [y4, y4 + 0.02], line_color='black') s4.add_layout( Label(x=l.mean(), y=y4 + 0.02, x_units='data', y_units='data', text='Na', render_mode='css', text_font_size='8pt')) l = np.array([1.2682]) s4.line(l[0], [y4, y4 + 0.02], line_color='black') s4.add_layout( Label(x=l.mean(), y=y4 + 0.02, x_units='data', y_units='data', text='Na', render_mode='css', text_font_size='8pt')) s4.xaxis.major_label_text_font_size = '0pt' s4.yaxis.major_label_text_font_size = '0pt' # put all the plots in a grid layout fig = gridplot(children=[[s7, s4, s1, s0], [s6, s5, s2, s3]]) return fig
def contam(cube, instrument, targetName='noName', paRange=[0, 360], badPAs=np.asarray([]), tmpDir="", fig='', to_html=True): lam_file = os.path.join(TRACES_PATH, 'NIRISS', 'lambda_order1-2.txt') ypix, lamO1, lamO2 = np.loadtxt(lam_file, unpack=True) rows, cols = cube.shape[1], cube.shape[2] PAmin, PAmax = paRange[0], paRange[1] PA = np.arange(PAmin, PAmax, 1) # Generate the contam figure if instrument == 'NIRISS': contamO1, contamO2 = nirissContam(cube) elif (instrument == 'NIRCam F322W2') or (instrument == 'NIRCam F444W'): contamO1 = nircamContam(cube, instrument) elif instrument == 'MIRI': contamO1 = miriContam(cube) TOOLS = 'pan, box_zoom, crosshair, reset, hover' bad_PA_color = '#dddddd' bad_PA_alpha = 0.7 dPA = 1 # Order 1~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Contam plot if instrument == 'NIRISS': xlim0 = lamO1.min() xlim1 = lamO1.max() elif instrument == 'NIRCam F322W2': xlim0 = LAM0_NIRCAM322W2 xlim1 = LAM1_NIRCAM322W2 elif instrument == 'NIRCam F444W': xlim0 = LAM0_NIRCAM444W xlim1 = LAM1_NIRCAM444W elif instrument == 'MIRI': xlim0 = 5 xlim1 = 12 ylim0 = PAmin - 0.5 ylim1 = PAmax + 0.5 color_mapper = LinearColorMapper(palette=PuBu[8][::-1][2:], low=-4, high=1) color_mapper.low_color = 'white' color_mapper.high_color = 'black' orders = 'Orders 1 & 2' if instrument == 'NIRCam' else 'Order 1' s2 = figure( tools=TOOLS, width=500, height=500, title='{} {} Contamination with {}'.format( orders, targetName, instrument), x_range=Range1d( xlim0, xlim1), y_range=Range1d( ylim0, ylim1)) contamO1 = contamO1 if 'NIRCam' in instrument else contamO1.T contamO1 = np.fliplr(contamO1) if ( instrument == 'MIRI') or ( instrument == 'NIRCam F322W2') else contamO1 fig_data = np.log10(np.clip(contamO1, 1.e-10, 1.) ) # [:, :361] # might this # index have somethig to # do w the choppiness # of o1 in all instruments # Begin plotting ~~~~~~~~~~~~~~~~~~~~~~~~ s2.image([fig_data], x=xlim0, y=ylim0, dw=xlim1 - xlim0, dh=ylim1 - ylim0, color_mapper=color_mapper) s2.xaxis.axis_label = 'Wavelength (um)' if instrument != 'NIRISS': s2.yaxis.axis_label = 'Aperture Position Angle (degrees)' # Add bad PAs bad_PA_color = '#555555' bad_PA_alpha = 0.6 # for ybad0, ybad1 in badPA: if len(badPAs) > 0: tops, bottoms, lefts, rights = [], [], [], [] for idx in range(0, len(badPAs)): PAgroup = badPAs[idx] top_idx = np.max(PAgroup) bot_idx = np.min(PAgroup) tops.append(top_idx) bottoms.append(bot_idx) lefts.append(xlim0) rights.append(xlim1) s2.quad(top=tops, bottom=bottoms, left=lefts, right=rights, color=bad_PA_color, alpha=bad_PA_alpha) # Line plot # ax = 1 if 'NIRCam' in instrument else 0 channels = cols if 'NIRCam' in instrument else rows s3 = figure(tools=TOOLS, width=150, height=500, x_range=Range1d(0, 100), y_range=s2.y_range, title=None) try: s3.line(100 * np.sum(contamO1 >= 0.001, axis=1) / channels, PA - dPA / 2, line_color='blue', legend_label='> 0.001') s3.line(100 * np.sum(contamO1 >= 0.01, axis=1) / channels, PA - dPA / 2, line_color='green', legend_label='> 0.01') except AttributeError: s3.line(100 * np.sum(contamO1 >= 0.001, axis=1) / channels, PA - dPA / 2, line_color='blue', legend='> 0.001') s3.line(100 * np.sum(contamO1 >= 0.01, axis=1) / channels, PA - dPA / 2, line_color='green', legend='> 0.01') s3.xaxis.axis_label = '% channels contam.' s3.yaxis.major_label_text_font_size = '0pt' # ~~~~~~ Order 2 ~~~~~~ # Contam plot if instrument == 'NIRISS': xlim0 = lamO2.min() xlim1 = lamO2.max() ylim0 = PA.min() - 0.5 * dPA ylim1 = PA.max() + 0.5 * dPA xlim0 = 0.614 s5 = figure( tools=TOOLS, width=500, height=500, title='Order 2 {} Contamination with {}'.format( targetName, instrument), x_range=Range1d( xlim0, xlim1), y_range=s2.y_range) fig_data = np.log10(np.clip(contamO2.T, 1.e-10, 1.))[:, 300:] s5.image( [fig_data], x=xlim0, y=ylim0, dw=xlim1 - xlim0, dh=ylim1 - ylim0, color_mapper=color_mapper) # s5.yaxis.major_label_text_font_size = '0pt' s5.xaxis.axis_label = 'Wavelength (um)' s5.yaxis.axis_label = 'Aperture Position Angle (degrees)' if len(badPAs) > 0: tops, bottoms, lefts, rights = [], [], [], [] for idx in range(0, len(badPAs)): PAgroup = badPAs[idx] top_idx = np.max(PAgroup) bot_idx = np.min(PAgroup) tops.append(top_idx) bottoms.append(bot_idx) lefts.append(xlim0) rights.append(xlim1) s5.quad(top=tops, bottom=bottoms, left=lefts, right=rights, color=bad_PA_color, alpha=bad_PA_alpha) # Line plot s6 = figure(tools=TOOLS, width=150, height=500, y_range=s2.y_range, x_range=Range1d(100, 0), title=None) try: s6.line(100 * np.sum(contamO2 >= 0.001, axis=0) / rows, PA - dPA / 2, line_color='blue', legend_label='> 0.001') s6.line(100 * np.sum(contamO2 >= 0.01, axis=0) / rows, PA - dPA / 2, line_color='green', legend_label='> 0.01') except AttributeError: s6.line(100 * np.sum(contamO2 >= 0.001, axis=0) / rows, PA - dPA / 2, line_color='blue', legend='> 0.001') s6.line(100 * np.sum(contamO2 >= 0.01, axis=0) / rows, PA - dPA / 2, line_color='green', legend='> 0.01') s6.xaxis.axis_label = '% channels contam.' s6.yaxis.major_label_text_font_size = '0pt' if len(badPAs) > 0: tops, bottoms, lefts, rights = [], [], [], [] for idx in range(0, len(badPAs)): PAgroup = badPAs[idx] top_idx = np.max(PAgroup) bot_idx = np.min(PAgroup) tops.append(top_idx) bottoms.append(bot_idx) lefts.append(0) rights.append(100) s3.quad(top=tops, bottom=bottoms, left=lefts, right=rights, color=bad_PA_color, alpha=bad_PA_alpha) if instrument == 'NIRISS': s6.quad(top=tops, bottom=bottoms, left=rights, right=lefts, color=bad_PA_color, alpha=bad_PA_alpha) # ~~~~~~ Plotting ~~~~~~ if instrument != 'NIRISS': fig = gridplot(children=[[s2, s3]]) else: fig = gridplot(children=[[s6, s5, s2, s3]]) return fig # , contamO1
# create figure for bar chart p = figure(x_range=Maj_mat, plot_width=1000, plot_height=600, toolbar_location=None) p.y_range.start = 0 p.xaxis.major_label_orientation = pi / 4 hover = HoverTool(tooltips=[('# Graduated', '@tooltip1'), ('# Left Institution', '@tooltip2')]) mapper = LinearColorMapper(palette=p_colorbartot, low=0, high=1) mapper.low_color = '#0000ff' mapper.high_color = code_maxtot ff = p.vbar(x='CIP_matf', top='dec_sumf', width=0.9, source=sourcef, fill_color=factor_cmap('CIP_matf', palette=palettef, factors=Maj_mat)) mm = p.vbar(x='CIP_matm', top='dec_summ', width=0.9, source=sourcem, fill_color=factor_cmap('CIP_matm', palette=palettem,
def plot_states(state_dict, state_types): palette.reverse() output_file("fatalities_per_state_rel.html") # hawaii en alaska weggehaald want dat fockte mn kaart op del us_states["HI"] del us_states["AK"] # vind long/lat van staten state_lons = [us_states[code]["lons"] for code in us_states] state_lats = [us_states[code]["lats"] for code in us_states] state_names = [us_states[code]["name"] for code in us_states] state_fatalities = [] for state_name in state_names: amount = state_dict[state_name] state_fatalities.append(amount) state_killer = [] for state_name in state_names: state_killer.append( max(state_types[state_name], key=state_types[state_name].get)) color_mapper = {} color_mapper = LinearColorMapper(palette=palette, low=5, high=50) t = sorted( zip(state_fatalities, state_killer, state_lons, state_lats, state_names)) state_lons = [x for _, _, x, _, _ in t] state_lats = [x for _, _, _, x, _ in t] state_names = [x for _, _, _, _, x in t] state_fatalities = [x for x, _, _, _, _ in t] state_killer = [x for _, x, _, _, _ in t] data = dict(x=state_lons, y=state_lats, name=state_names, rate=state_fatalities, cause=state_killer) color_mapper.low_color = 'blue' color_mapper.high_color = 'red' TOOLS = "pan,wheel_zoom,reset,hover,save" p = figure( title="Fatalities per 100,000 inhabitants", tools=TOOLS, toolbar_location="left", x_axis_location=None, y_axis_location=None, plot_width=800, plot_height=600, ) p.grid.grid_line_color = None color_bar = ColorBar(color_mapper=color_mapper, location=(0, 0)) p.add_layout(color_bar, 'right') p.grid.grid_line_color = None hover = p.select(dict(type=HoverTool)) hover.tooltips = [("State", "@name"), ("Gun deaths per 100k", "@rate"), ("Main cause of death:", "@cause")] hover.point_policy = "follow_mouse" p.patches( 'x', 'y', source=data, fill_color={ 'field': 'rate', 'transform': color_mapper }, fill_alpha=0.7, line_color="white", line_width=0.5, line_alpha=0.3, ) return (p)
def __create_heatmap(self, source): """Creates Heatmap Plot of Expenses. "Heatmap" Plot is a Figure plotting rectangles side by side, with every rectangle colored according to the defined color palette and the value associated with a given rectangle. Heatmap Plot in this case plots Week numbers on the X axis (week numbers from a year, e.g. 1, 13, 52) and days of the week (Monday, Tuesday, etc.) on the Y axis. This way, there are maximum 7 rectangles in a column and as many rows as weeks in the data. X axis is a Categorical Range of Week Numbers. Y axis is a Categorical Range of Strings representing numbers of days of the week (Monday - "0", Sunday - "6"). Y axis has also formatter attached to it, to format ticks into English 3-letter short acronyms for every day (e.g. Mon, Tue, etc.). ColorPalette is defined from ColorMap object which defines tints from a base color. Every second color from the palette is used for a bigger contrast. Additionally, ColorBar is attached to the Plot on the right side to provide color legend for a user. Returns created Plot p. """ y_weekdays = [str(x) for x in range(6, -1, -1) ] # there is always 7 days in a week x_weeks = list(map(lambda x: str(x), range(1, 53))) palette = list( reversed( [self.color_map.base_color_tints[i] for i in range(0, 10, 2)])) cmap = LinearColorMapper(palette=palette, low=0, high=1) cmap.low_color = "white" p = figure(height=180, width=1200, x_range=x_weeks, y_range=y_weekdays, x_axis_location="above", tooltips=self.heatmap_plot_tooltip, toolbar_location=None) p.rect(x="week", y="weekday", width=1, height=1, source=source, fill_color={ "field": "value", "transform": cmap }, line_color=None) p.axis.minor_tick_line_color = None p.axis.major_tick_line_color = None p.axis.axis_line_color = "white" p.axis.major_label_text_color = self.color_map.label_text_color p.axis.major_label_text_font_size = "13px" p.axis.group_text_color = self.color_map.label_text_color p.axis.group_text_font_size = "13px" p.yaxis.major_label_standoff = 25 weekday_mapper = { 0: "Mon", 1: "Tue", 2: "Wed", 3: "Thu", 4: "Fri", 5: "Sat", 6: "Sun" } weekday_formatter = FuncTickFormatter( args={"weekday_mapper": weekday_mapper}, code=""" return weekday_mapper[tick]; """) p.yaxis.formatter = weekday_formatter self.heatmap_color_mapper = cmap color_bar = ColorBar( color_mapper=cmap, ticker=BasicTicker(desired_num_ticks=len(palette)), formatter=PrintfTickFormatter(), label_standoff=10, border_line_color=None, location=(0, 0), major_label_text_font_size="12px", major_label_text_color=self.color_map.text_color) p.add_layout(color_bar, "right") return p