def plot_expr_comparison(expr, gene, prefix=None, smoothed=0): mel = expr.select(**sel_startswith('melXmel_')).ix[gene] sim = expr.select(**sel_startswith('simXsim_')).ix[gene] hyb = expr.select(**sel_startswith(('melXsim', 'simXmel'))).ix[gene] if smoothed: mel = pd.rolling_mean(mel, smoothed, min_periods=1, center=True) sim = pd.rolling_mean(sim, smoothed, min_periods=1, center=True) hyb = pd.rolling_mean(hyb, smoothed, min_periods=1, center=True) pu.svg_heatmap((None, mel, sim, hyb), 'analysis_godot/results/spatial_diffs/{}.svg'.format(gene), cmap=(gene, cm.Reds, cm.Blues, pu.ISH), norm_rows_by=tuple([gene] + ['maxall'] * 7), **pu_kwargs)
def test_equalization_no_noise(equalizer, channel, transmitter, receiver, bits, titleText=None, SAVED=False): t_mod, mod_signal = transmitter.transmit_bits(bits) t_tx, tx_signal = channel.transmit_signal(mod_signal) t_rx, rec_signal = receiver.receive_signal(tx_signal) t_eq, eq_signal = equalizer.equalize_signal(rec_signal) if titleText is None: titleText = 'TX: %s, CH: %s, RX: %s, EQ: %s' % ( transmitter.name, channel.name, receiver.name, equalizer.name) plt.figure() plt.subplot(2, 2, 1) myplt.signal_plot(t_mod, mod_signal, titleText=titleText) plt.subplot(2, 2, 2) myplt.signal_plot(t_tx, tx_signal, titleText=None) plt.subplot(2, 2, 3) myplt.signal_plot(t_rx, rec_signal, titleText=None) plt.subplot(2, 2, 4) myplt.signal_plot(t_eq, eq_signal, titleText=None)
def main(self): plot_x_size = 8 plot_y_size = 6 fig = plt.figure(figsize=(plot_x_size, plot_y_size)) # Get plot points points, lons, lats = self._getPlotPointsArray() try: if (points == None): print "Failed to get plot points" return (1) except: print "WARNING: Update numpy to fix masked array bug" value_min = self.scale[0] value_max = self.scale[1] print "Colorbar range: %f to %f" % (value_min, value_max) if (self.color == None): cmap = cm.Spectral else: cmap = eval("cm.%s" % (self.color)) # Discrete intervals if (self.discretize[0]): print "Converting continuous color scale to discrete" cmap = PlotUtils().plotCmapDiscretize(cmap, self.discretize[1]) norm = mcolors.Normalize(vmin=value_min, vmax=value_max) # Plot the depth map PlotUtils().plotMapArray(fig, PLOT_MAP_LOC, \ [self.point1, self.point2], \ points, lons, lats, \ self.title, cmap, norm) # Plot colorbar PlotUtils().plotColorbar(self.value, \ PLOT_PROP_UNITS[self.value], \ cmap, norm, \ value_min, value_max, \ self.discretize[1]) print "Saving plot file %s" % (self.outfile) plt.savefig(self.outfile) plt.show() return 0
def test_eye_diagram(equalizer, channel, transmitter, receiver, noise_var=0.01, SAVED=False): plt.figure() equalizer.plot_eye_diagram(transmitter, channel, receiver, noise_var=noise_var) if SAVED: myplt.save_current( 'eye_rx_%s_ch_%s_eq_%s_ns_%.4f.png' % (receiver.name, channel.name, equalizer.name, noise_var), 'PLOT')
def plotstate(Mesh, U, field, fname): V = Mesh['V'] E = Mesh['E'] BE = Mesh['BE'] f = plt.figure(figsize=(12,6)) F = pu.getField(U, field) plt.tripcolor(V[:,0], V[:,1], triangles=E, facecolors=F, shading='flat', vmin=1.55, vmax=1.9) for i in range(len(BE)): x = [V[BE[i,0],0], V[BE[i,1],0]] y = [V[BE[i,0],1], V[BE[i,1],1]] plt.plot(x, y, '-', linewidth=2, color='black') #dosave = (len(fname) != 0) plt.axis('equal') #plt.axis([-100, 100,-100, 100]) plt.axis([-2, 10,-4, 4]) plt.colorbar() #plt.clim(0, 0.8) plt.title(field, fontsize=16) f.tight_layout() plt.show()#block=(not dosave)) #if (dosave): plt.savefig(fname) plt.close(f)
def test_pulse_shape_srrc(srrc_tx, K, alpha, SAVED=False): plt.figure() for transmitter in srrc_tx: transmitter.plot_impulse_response( titleText='Impulse response SRRC pulse') plt.legend(['K=%g, alpha=%.2f' % (k, al) for k in K for al in alpha]) if SAVED: myplt.save_current('tx_SRRC_impulse.png', 'PLOT') plt.figure() for transmitter in srrc_tx: transmitter.plot_freq_response( titleText='Frequency response SRRC pulse') plt.legend(['K=%g, alpha=%.2f' % (k, al) for k in K for al in alpha]) if SAVED: myplt.save_current('tx_SRRC_freq.png', 'PLOT')
def main(): # 读取数据 gdp = pd.read_excel("API_NY.GDP.MKTP.CD_DS2_zh_excel_v2_103680.xls") # 筛选:去掉世界、一些地区性的数据 fields = [ "世界", "收入国家", "地区", "南亚", "组织成员", "人口", "北美", "联盟", "IBRD", "IDA", "重债穷国" ] gdp["is_country"] = gdp.apply( lambda x: is_country(x["Country Name"], fields), axis=1) gdp = gdp[gdp["is_country"] == True] datas = [] for year in range(1960, 2019): year = str(year) gdp.sort_values(year, inplace=True, ascending=False) print(year, "==========================================") print(gdp[0:15][["Country Name", year]]) data = gdp[0:15] #排序,取前15名 data.sort_values(year, inplace=True, ascending=True) data[year] = data[year] / 10**11 datas.append( [year, data[year].tolist(), data["Country Name"].tolist()]) # 绘制动态图 plot = PlotUtils.Plot(datas) plot.showGif("gdp.gif")
def iSensors_HV_Type3Type4_Petal(names, ring, disk, result_dicts, itime): isensors = 0 if ring in [0, 1, 2, 3]: for iring in [0, 1, 2, 3]: index = PlotUtils.GetResultDictIndex(names, iring, disk) isensors += result_dicts[index]['isensor'].GetY()[itime] if ring in [4, 5]: for iring in [4, 5]: index = PlotUtils.GetResultDictIndex(names, iring, disk) isensors += result_dicts[index]['isensor'].GetY()[itime] return isensors
def plot_eye_diagram_after_noise(self, transmitter, mean=0, var=1, num_symbols=1000): random_symbols = np.random.randint(transmitter.M, size=num_symbols) titleText = ('Eye Diagram, TX : ' + transmitter.name + ' , CH : ' + self.name + ' , Noise Var: %.4f' % var) t, modulated_signal = transmitter.transmit_symbols(random_symbols) t, transmitted_signal = self.transmit_signal(modulated_signal) transmitted_signal = self.add_awgn(transmitted_signal, mean=mean, var=var) myplt.eye_diagram_plot(t, transmitted_signal, self.__T_p, titleText=titleText)
def GetMaxSf(self, width, ymin, isL): # Convert width in pad-frac to NBins width_axis = 1 - (self.pad_histogram.GetLeftMargin() + self.pad_histogram.GetLeftMargin()) nbins = self.pilot_hist.FindBin( self.xrangehigh) - self.pilot_hist.FindBin(self.xrangelow) + 1 #print "nbins=",nbins bin_width_frac = width_axis / nbins legend_width_bins = int( math.ceil((width + self.leg_axis_offset) / bin_width_frac) + 1) # 2. Figure out if best on L or R if isL: #print self.pilot_hist.FindBin(self.xrangelow) + legend_width_bins max_range = PlotUtils.GetTrueMaximum( self.maxhist, self.pilot_hist.FindBin(self.xrangelow) - 1, self.pilot_hist.FindBin(self.xrangelow) + legend_width_bins) else: max_range = PlotUtils.GetTrueMaximum( self.maxhist, self.maxhist.FindBin(self.xrangehigh) - legend_width_bins, self.maxhist.FindBin(self.xrangehigh)) # 3. Find height of highest bin in range # fiigure how much need to scale it down by #print width, ymin, isL, max_range if max_range == 0: return 1 else: if self.linear == False: ndivs_below_1 = -1 * math.log10(self.ymin_log) ndivs_max = math.log10(PlotUtils.GetTrueMaximum( self.maxhist)) + ndivs_below_1 ndivs_max_range = math.log10(max_range) + ndivs_below_1 y_max_sf = ndivs_max_range / (ndivs_max * (ymin - 0.03)) else: y_max_sf = max_range / ( PlotUtils.GetTrueMaximum(self.maxhist) * (ymin - 0.03)) #print y_max_sf return y_max_sf
def makeGraphLabelOnRight(graph, minMass, maxMass, text): lastY = PlotUtils.getGraphYvalues(graph)[-1] label = ROOT.TLatex(maxMass + 0.01 * (maxMass - minMass), lastY, text) label.SetTextAlign(12) return label
def plot_expr_comparison(expr, gene, prefix=None, smoothed=0): mel = expr.select(**sel_startswith('melXmel_')).ix[gene] sim = expr.select(**sel_startswith('simXsim_')).ix[gene] hyb = expr.select(**sel_startswith(('melXsim', 'simXmel'))).ix[gene] if smoothed: mel = pd.rolling_mean(mel, smoothed, min_periods=1, center=True) sim = pd.rolling_mean(sim, smoothed, min_periods=1, center=True) hyb = pd.rolling_mean(hyb, smoothed, min_periods=1, center=True) pu.svg_heatmap( (None, mel, sim, hyb), 'analysis_godot/results/spatial_diffs/{}.svg'.format(gene), cmap=(gene, cm.Reds, cm.Blues, pu.ISH), norm_rows_by=tuple([gene] + ['maxall']*7), **pu_kwargs )
def init1D(self,title,nBins,xMin,xMax,systEvents): self.title = title self.nBins = nBins self.xMin = xMin self.xMax = xMax self.systematicUniverses = systEvents self.eventToUnivMap = {} self.hist = PlotUtils.MnvH1D('h_%s'%self.title,self.title,self.nBins,self.xMin,self.xMax) self.hist.SetDirectory(0)
def init2DFromVecs(self,title,nXBins,xBins,nYBins,yBins,systEvents): self.title = title self.nXBins = nXBins self.xBins = xBins self.nYBins = nYBins self.yBins = yBins self.systematicUniverses = systEvents self.eventToUnivMap = {} self.hist = PlotUtils.MnvH2D('h_%s'%self.title,self.title,self.nXBins,self.xBins,self.nYBins,self.yBins) self.hist.SetDirectory(0)
def test_equalization_only_channel(equalizer, channel, bits, Fs, titleText=None, SAVED=False): bits = np.array([[s, *list(np.zeros(Fs - 1))] for s in bits]).flatten() t_bits = np.arange(0, np.size(bits) / Fs, 1 / Fs) t_tx, signal_tx = channel.transmit_signal(bits) t_eq, signal_eq = equalizer.equalize_signal(signal_tx) plt.figure() plt.subplot(3, 1, 1) myplt.signal_plot(t_bits, bits, discrete=True, titleText='Channel: %s, Equalizer: %s' % (channel.name, equalizer.name), xText='n', yText='b[n]') plt.subplot(3, 1, 2) myplt.signal_plot(t_tx, signal_tx, titleText=None, discrete=True) plt.subplot(3, 1, 3) myplt.signal_plot(t_eq, signal_eq, titleText=None, discrete=True)
def run_simulation_gray(self, filename, noise_var=0, img_basename='sample', DISPLAYED=False, EYES=False, SAVED=False): print('\n#################################################') print('Reading file as grayscale image: ', img_basename) img_in, dim = self.__ip.read_image_gray(filename, FITSIZE=True) if DISPLAYED: plt.figure() myplt.show_image(img_in, titleText='%s input' % img_basename) img_out = self.transmit_image(img_in, dim, noise_var=noise_var, EYES=EYES, SAVED=SAVED) if DISPLAYED: plt.figure() myplt.show_image( img_out, titleText='%s, tx: %s, ch: %s, eq: %s, noise:%.4f' % (img_basename, self.__tx.name, self.__ch.name, self.__eq.name, noise_var)) if SAVED: filename = ('%s_tx_%s_ch_%s_eq_%s_ns_%.4f.png' % (img_basename, self.__tx.name, self.__ch.name, self.__eq.name, noise_var)) ret = cv2.imwrite(os.path.join(IMG_OUT_PATH, filename), img_out) if ret: print('Output image saved.') else: print('Couldn"t save image output :(') print('Done with ', img_basename, '!') print('#################################################\n')
def test_modulation(transmitter, bits, Fs, SAVED=False): t, modulated_signal = transmitter.transmit_bits(bits) plt.figure() plt.subplot(2, 1, 1) myplt.signal_plot(np.arange(np.size(bits)), bits, discrete=True, titleText='Random signal and its %s modulated signal' % transmitter.name, xText='n', yText='b[n]') plt.subplot(2, 1, 2) myplt.signal_plot(t, modulated_signal, titleText=None) if SAVED: myplt.save_current('tx_%s_mod.png' % transmitter.name, 'PLOT') plt.figure() myplt.bode_plot(t, modulated_signal, titleText='Spectrum of modulated signal %s' % transmitter.name) if SAVED: myplt.save_current('tx_%s_mod_freq.png' % transmitter.name, 'PLOT')
def test_transmission(channel, transmitter, bits, SAVED=False): t, modulated_signal = transmitter.transmit_bits(bits) t, transmitted_signal = channel.transmit_signal(modulated_signal) plt.figure() plt.subplot(2, 1, 1) myplt.signal_plot(np.arange(np.size(bits)), bits, discrete=True, titleText='Transmitter: %s, Channel: %s' % (transmitter.name, channel.name), xText='n', yText='b[n]') plt.subplot(2, 1, 2) myplt.signal_plot(t, transmitted_signal, titleText=None) if SAVED: myplt.save_current('{}_modulated.png'.format(transmitter.name)) plt.figure() myplt.bode_plot(t, transmitted_signal, titleText='Spectrum Transmitter: %s, Channel: %s' % (transmitter.name, channel.name)) if SAVED: myplt.save_current('{}_modulated_freq.png'.format(transmitter.name))
def init3D(self,title,nXBins,xMin,xMax,nYBins,yMin,yMax,nZBins,zMin,zMax,systEvents): self.title = title self.nXBins = nXBins self.xMin = xMin self.xMax = xMax self.nYBins = nYBins self.yMin = yMin self.yMax = yMax self.nZBins = nZBins self.zMin = zMin self.zMax = zMax self.systematicUniverses = systEvents self.eventToUnivMap = {} self.hist = PlotUtils.MnvH3D('h_%s'%self.title,self.title,self.nXBins,self.xMin,self.xMax,self.nYBins,self.yMin,self.yMax,self.nZBins,self.zMin,self.zMax) self.hist.SetDirectory(0)
def Draw(self, sortByMax=True): self.pilot_hist = None if len(self.plot_elements): self.pilot_hist = self.plot_elements[0].hists[0].Clone("PILOT") self.pilot_hist.Reset() # Needs pilot hist if posible if self.stack_hists: self._finaliseStack() # Make max and pilot hist #pdb.set_trace() self.maxhist = PlotUtils.GetTrueMaximumHist( [x.GetTotalHist() for x in self.plot_elements]) if self.pilot_hist: self.pilot_hist = self.plot_elements[0].hists[0].Clone("PILOT") self.pilot_hist.Reset() if not self.xrangehigh: self.xrangelow = self.pilot_hist.GetXaxis().GetXmin() self.xrangehigh = self.pilot_hist.GetXaxis().GetXmax() self.MakePads() self.PlaceLegendSetYmax() self.PrepareElements() elements = self.SortElements(sortByMax) self.pad_histogram.cd() self.DrawElements(elements) if self.draw_ratio: self.pad_ratio.cd() self.DrawRatioPlot() self.pad_histogram.cd() self.DrawLegend(elements) self.DrawLabels()
def test_reception_no_noise(receiver, transmitter, channel, bits, titleText=None, SAVED=False): t_mod, mod_signal = transmitter.transmit_bits(bits) t_tx, tx_signal = channel.transmit_signal(mod_signal) t_rec, rec_signal = receiver.receive_signal(tx_signal) samp_signal = receiver.sample_sig_to_bits(t_rec, rec_signal) if titleText is None: titleText = 'TX: %s, CH: %s, RX: %s' % (transmitter.name, channel.name, receiver.name) plt.figure() plt.subplot(2, 2, 1) myplt.signal_plot(np.arange(np.size(bits)), bits, discrete=True, titleText=titleText, xText='n', yText='b[n]') plt.subplot(2, 2, 2) myplt.signal_plot(t_tx, tx_signal, titleText=None) plt.subplot(2, 2, 3) myplt.signal_plot(t_rec, rec_signal, titleText=None) plt.subplot(2, 2, 4) myplt.signal_plot(np.arange(np.size(samp_signal)), samp_signal, discrete=True, xText='n', yText='r[n]', titleText=None)
ix = diff_from_uniform.index[:50].intersection(expr.index) ix2 = diff_from_uniform2.index[:50].intersection(expr.index) plot_kwargs = dict( draw_row_labels=True, box_size=15, total_width=150, split_columns=True, col_sep='_sl', convert=True, progress_bar=True, ) PlotUtils.svg_heatmap(ase.ix[ix, :-1], 'analysis/results/diff_from_uniform.svg', row_labels=translate.ix[ix], norm_rows_by='center0pre', cmap=mpl.cm.RdBu, **plot_kwargs) PlotUtils.svg_heatmap(expr.ix[ix, :-1], 'analysis/results/diff_from_uniform_expr.svg', row_labels=translate.ix[ix], norm_rows_by='max', cmap=PlotUtils.ISH, **plot_kwargs) PlotUtils.svg_heatmap(ase.ix[ix2, :-1], 'analysis/results/diff_from_uniform2.svg', row_labels=translate.ix[ix2], norm_rows_by='center0pre', cmap=mpl.cm.RdBu, **plot_kwargs) PlotUtils.svg_heatmap(expr.ix[ix2, :-1],
plist = zyg_corrs.sort_values().index geneset = {g for gs in plist for g in gs.split('_')[0].split('+')} if 'optional_exon_lens' not in locals(): optional_exon_lens = dist_from_exon_to_transcript_end('Reference/mel_good.gtf', 'Reference/mel_good_exons.gtf', True) optional_exon_lens = pd.Series(optional_exon_lens) pu.svg_heatmap(( ase.ix[[spliceid.get_genes_in_exon(ex).split('_')[0].split('+')[0] for ex in plist]], #ase.ix[plist], psi.ix[plist]), 'analysis/results/psi-autocorr-fdr5.svg', cmap=(cm.RdBu, cm.viridis), norm_rows_by=('center0pre', 'fullvar'), row_labels=[('{:.03f}'.format(ac_many.loc[ex].mean()), psi.loc[ex].min(), psi.loc[ex].max(), #ex.split('_')[1], '{:.1f}kb'.format(min(txlens[txs_by_gene[ex.split('_')[0].split('+')[0]]])/1000), '{:.1f}kb'.format(max(txlens[txs_by_gene[ex.split('_')[0].split('+')[0]]])/1000), #'{:.02f}'.format(mean(optional_exon_lens[ex])), '{:.02f}'.format(zyg_corrs[ex]), #'{:.02f}'.format(psi.loc[ex].corr(rectified_ase.loc[spliceid.get_genes_in_exon(ex).split('_')[0].split('+')[0]])), spliceid.get_genes_in_exon(ex)) for ex in plist], **pu.kwargs_heatmap)
# Create a text file in the output directory to record the selection criteria writeSelection( options.outDir, Selection ) hConfig = {} hDict = {} for histo in Histos.keys(): hConfig[histo] = ConfigUtils.getPlotConfig( Histos[histo] ) for src in sList.keys(): fname = sList[src]['File'] print 'Open %s...' % fname t = AccessROOTUtils.getTree( fname, tree ) srcName = sList[src]['LegendName'].replace( ' ', '') hDict[src] = bookHistograms( srcName, hConfig ) hDict[src] = selection( t, Selection, hDict[src] ) for histo in Histos.keys(): hList = {} for src in sList.keys(): hList[src] = hDict[src][histo] hCfg = hConfig[histo] if 'DrawOpt' in hCfg.keys() and ( hCfg['DrawOpt'] == 'COLZ' or hCfg['DrawOpt'] == 'colz' ): PlotUtils.make2DPlot( hList, hConfig[histo], sList, options.outDir ) else: PlotUtils.makeOverlayPlot( hList, hConfig[histo], sList, options.outDir ) # if __name__ == "__main__"
def plotstate(Mesh, U, field, fname, clim1, clim2, color, plotExact, plotError): V = Mesh['V'] E = Mesh['E'] BE = Mesh['BE'] #f = plt.figure(figsize=(12,6)) F = pu.getField(U, field) if((plotExact == True) or (plotError == True)): Exact = F s = field.lower() if (s == 'pressure'): Exact = pu.p_a(V[:,0], V[:,1]) #F = ((pr - F)/pr)*100. elif (s == 'density'): Exact = pu.rho_a(V[:,0], V[:,1]) #F = ((rho_a - F)/rho_a)*100. elif (s == 'xmomentum'): Exact = pu.rho_a(V[:,0], V[:,1])*pu.u_a(V[:,0], V[:,1]) #F = ((ru_a - F)/ru_a)*100. elif (s == 'ymomentum'): Exact = pu.rho_a(V[:,0], V[:,1])*pu.v_a(V[:,0], V[:,1]) #F = ((rv_a - F)/rv_a)*100. elif (s == 'energy'): Exact = pu.E_a(V[:,0], V[:,1]) #F = ((E_a - F)/E_a)*100. elif (s == 'renergy'): Exact = pu.rE_a(V[:,0], V[:,1]) #F = ((rE_a - F)/rE_a)*100. elif (s == 'xvelocity'): Exact = pu.u_a(V[:,0], V[:,1]) #F = ((u_a - F)/u_a)*100. elif (s == 'yvelocity'): Exact = pu.v_a(V[:,0], V[:,1]) #F = ((v_a - F)/v_a)*100. if(plotError == True): F = np.abs(Exact - F) #F = ((Exact - F)/Exact)*100. if(plotExact == True): F = Exact clim1 = np.min(F) clim2 = np.max(F) if(F.shape[0] == V.shape[0]): plt.tripcolor(V[:,0], V[:,1], F, triangles=E, shading='gouraud', edgecolors=color, vmin=clim1, vmax=clim2, linewidth=1) #plt.tripcolor(V[:,0], V[:,1], F, triangles=E, shading='flat', edgecolors=color, vmin=clim1, vmax=clim2, linewidth=1) else: plt.tripcolor(V[:,0], V[:,1], triangles=E, facecolors=F, shading='flat', vmin=clim1, vmax=clim2, linewidth=1) for i in range(len(BE)): x = [V[BE[i,0],0], V[BE[i,1],0]] y = [V[BE[i,0],1], V[BE[i,1],1]] plt.plot(x, y, '-', linewidth=2, color='black') dosave = (len(fname) != 0) plt.axis('equal') #plt.axis([-100, 100,-100, 100]) #plt.axis([-0.5, 1.5, -0.75, 1.5]) #plt.colorbar() #plt.clim(0, 0.7) #plt.clim(9, 12) if(plotError == True): plt.title(field+' error', fontsize=16) elif(plotExact == True): plt.title(field+' exact', fontsize=16) else: plt.title(field, fontsize=16)
data['sim_dominant'] = len(sim_dom) data['num_semimat'] = len(semi_maternal) expected = (sim_parental - mel_parental)/(sim_parental + mel_parental) mel_dom = expected.ix[mel_dom].T.mean().sort_values().index sim_dom = expected.ix[sim_dom].T.mean().sort_values().index print("Making species bias figs") pu.svg_heatmap((expr, expected, ase), 'analysis/results/mel_dom.svg', index=mel_dom, norm_rows_by=('maxall', 'center0pre', 'center0pre'), cmap=(pu.ISH, cm.RdBu, cm.RdBu), progress_bar=True, row_labels=[( '{:6.1f}'.format( expr.ix[i].max() if i in expr.index else np.nan), chrom_of.get(i, '???'), i) for i in mel_dom], nan_replace='no', **pu.kwargs_heatmap) pu.svg_heatmap((expr, expected, ase), 'analysis/results/sim_dom.svg', index=sim_dom, norm_rows_by=('maxall', 'center0pre', 'center0pre'), cmap=(pu.ISH, cm.RdBu, cm.RdBu), progress_bar=True, row_labels=[( '{:6.1f}'.format( expr.ix[i].max() if i in expr.index else np.nan),
r2_peak_sxm_native = calculate_variance_explained( simXmel, xs_sxm, peak, res_peak_sxm) r2_peak_sxm_other = calculate_variance_explained( melXsim, xs_mxs, peak, res_peak_sxm) r2_peak_diff = (r2_peak_mxs_native - r2_peak_mxs_other.clip(0,1)).dropna().sort_values() r2_logist_diff = (r2_logist_mxs_native - r2_logist_mxs_other.clip(0,1)).dropna().sort_values() pbar = pb.ProgressBar() for gene in pbar(r2_peak_diff.select(pzyg.__contains__).index[-50:]): val = r2_peak_diff.ix[gene] pu.svg_heatmap(ase.ix[gene], 'analysis/results/transdiff/pd_mxs_{:.02f}-{}.svg'.format(val, fbgns[gene]), **kwargs ) pbar.finish() pbar = pb.ProgressBar() for gene in pbar(r2_logist_diff.select(pzyg.__contains__).index[-50:]): val = r2_logist_diff.ix[gene] pu.svg_heatmap(ase.ix[gene], 'analysis/results/transdiff/ld_mxs_{:.02f}-{}.svg'.format(val, fbgns[gene]), **kwargs ) r2_sxm_peak_diff = (r2_peak_sxm_native - r2_peak_sxm_other.clip(0,1)).dropna().sort_values()
zld_comp = zld_exp.select(lambda x: 'cyc14A' in x, axis=1) wt_comp = wt_exp.select(lambda x: 'cyc14A' in x, axis=1) diff_col = pd.Series(index=zld_comp.index) for gene in wt_exp.index: assert gene in zld_exp.index diff_col[gene] = diff_stat(zld_comp.ix[gene], wt_comp.ix[gene]) zld_exp['diff_col'] = diff_col wt_exp['diff_col'] = diff_col zld_exp.sort(column='diff_col', ascending=False, inplace=True) wt_exp.sort(column='diff_col', ascending=False, inplace=True) zld_fig_genes = zld_exp.select(lambda x: '14A' in x or '11' in x, axis=1) wt_fig_genes = wt_exp.select(lambda x: '14A' in x or '11' in x, axis=1) zld_fig_genes = zld_fig_genes[wt_fig_genes.max(axis=1) > 10][:120] wt_fig_genes = wt_fig_genes[wt_fig_genes.max(axis=1) > 10][:120] assert (zld_fig_genes.index == wt_fig_genes.index).all() import PlotUtils PlotUtils.svg_heatmap((wt_fig_genes, zld_fig_genes), 'analysis/results/cyc13diff.svg', norm_rows_by=wt_fig_genes.max(axis=1), draw_row_labels=True, cmap = (mpl.cm.Blues, mpl.cm.Reds), box_size=15, total_width=150)
def makePlot(csvFile, relative, includeExpected = True, fermiophobic = None, ymax = None, ignoreMassesExpected = None, exclude_null_expected = False, ): """ @param relative if true, the exclusion of the ratios (relative to the inputs given) are plotted. If False, these ratios are multiplied by the SM cross sections and branching ratios into gamma gamma """ masses, observedValues, expectedValues, \ expected_minus_2_sigma_values, \ expected_minus_1_sigma_values, \ expected_plus_1_sigma_values, \ expected_plus_2_sigma_values = PlotUtils.readCSV(csvFile, includeExpected) massesExpected = masses[:] # compile an overall list of mass points to be excluded for the expected limits expectedMassExclusionList = [] # mass points to be excluded explicitly specified if ignoreMassesExpected != None: expectedMassExclusionList.extend(ignoreMassesExpected) # auto-exclusion of those which are zero if exclude_null_expected: # find those mass points for which the expected limit is zero for mass, expectedLimit in zip(massesExpected, expectedValues): # TODO: maybe add some tolerance if expectedLimit == 0: expectedMassExclusionList.append(mass) if expectedMassExclusionList: # at least one mass point to be excluded (for the expected # limits) tolerance = 1e-4 for mass in expectedMassExclusionList: for i, value in enumerate(massesExpected): # print "TESTING MASS",value if abs(mass - value) < tolerance: # print "EXCLUDING",value del massesExpected[i] del expectedValues[i] del expected_minus_2_sigma_values[i] del expected_minus_1_sigma_values[i] del expected_plus_1_sigma_values[i] del expected_plus_2_sigma_values[i] break if not relative: # if we're plotting the absolute cross sections, we # need to know whether this is Standard Model or Fermiophobic assert(fermiophobic != None) if fermiophobic: typeName = "FP" else: typeName = "SM" observedValues = PlotUtils.multiplyArrayByXsectAndBR(masses, observedValues, fermiophobic) expectedValues = PlotUtils.multiplyArrayByXsectAndBR(massesExpected, expectedValues, fermiophobic) for values in (expected_minus_2_sigma_values, expected_minus_1_sigma_values, expected_plus_1_sigma_values, expected_plus_2_sigma_values): if values == None: continue # print locals()[name][-1] newValues = PlotUtils.multiplyArrayByXsectAndBR(massesExpected, values, fermiophobic) # print locals()[name][-1] for i,j in enumerate(newValues): values[i] = j # print "expected_plus_2_sigma_values",expected_plus_2_sigma_values if options.printAbsExclusion: print "limits on cross sections times branching ratio [pb]:" print " mass exp obs" for mass,exp,obs in zip(masses, expectedValues, observedValues): print " %.1f GeV: %7.4f %7.4f" % (mass, exp, obs) #---------------------------------------- # produce the plot #---------------------------------------- grObserved = PlotUtils.makeGraphFromArrays(masses, observedValues) gcSaver.append(grObserved) # make the cross section graphs for the fixed scaling values # (only if we're plotting the absolute cross section # exclusions) xsectGraphs = [] if fermiophobic: crossSectionScalingFactors = phobicCrossSectionScalingFactors else: crossSectionScalingFactors = smCrossSectionScalingFactors if not relative: # get interpolated cross sections at masses # where we calculated the exclusion smXsect = [ PlotUtils.getXsectTimesBR(mass, fermiophobic) for mass in masses ] if options.printXsect: print "used cross sections times branching ratio:" for mass,xsectTimesBR in zip(masses,smXsect): print " %.1f GeV: " % mass,xsectTimesBR # color = ROOT.TColor(38, ) color = ROOT.gROOT.GetColor(38) color.SetRGB(0, 0.6, 1) # create but do not draw yet the cross section graphs for scalingFactor in crossSectionScalingFactors: gr = PlotUtils.makeGraphFromArrays(masses, [ xsect * scalingFactor for xsect in smXsect]) gcSaver.append(gr) xsectGraphs.append(gr) gr.SetLineWidth(4) gr.SetLineStyle(ROOT.kDashed) # gr.SetLineColor(ROOT.kBlue) gr.SetLineColor(38) #---------------------------------------- # determine the y scale #---------------------------------------- if ymax == None: ymax = max(observedValues) if includeExpected: ymax = max(ymax, max(expectedValues)) if xsectGraphs: ymax = max(ymax, max([ PlotUtils.getGraphMaximumY(gr) for gr in xsectGraphs])) ymax *= 1.1 if not relative: if fermiophobic: # fix the y scale by hand in order not to # stretch it too much because of large # scaling factors for the theoretical expectation ymax = 0.1 #---------------------------------------- # create a dummy histogram to set the x range hdummy = ROOT.TH1F("hdummy","",1,min(masses),max(masses)) gcSaver.append(hdummy) hdummy.SetMaximum(ymax) hdummy.Draw("AXIS") ROOT.gStyle.SetOptTitle(0) #---------------------------------------- # sigma bands #---------------------------------------- if not classicMode and includeExpected and \ expected_minus_2_sigma_values != None and \ expected_minus_1_sigma_values != None and \ expected_plus_1_sigma_values != None and \ expected_plus_2_sigma_values != None: for sigmas, color in ( (2,ROOT.kYellow), (1,ROOT.kGreen)): plusValues = locals()['expected_plus_%d_sigma_values' % sigmas] minusValues = locals()['expected_minus_%d_sigma_values' % sigmas] # TODO: this (making the filled graph) should be made a function in PlotUtils # (is also used in ~/2011-09-vbf-cl/mystuff/random-experiments/plot.py) xvaluesForFilledGraph = massesExpected[:] yvaluesForFilledGraph = plusValues[:] xvaluesForFilledGraph.extend(massesExpected[::-1]) yvaluesForFilledGraph.extend(minusValues[::-1]) # just to be sure: duplicate first point xvaluesForFilledGraph.append(xvaluesForFilledGraph[0]) yvaluesForFilledGraph.append(yvaluesForFilledGraph[0]) grFilled = PlotUtils.makeGraphFromArrays(xvaluesForFilledGraph, yvaluesForFilledGraph) gcSaver.append(grFilled) grFilled.SetFillColor(color) grFilled.Draw("F") if classicMode: #---------------------------------------- # draw a filled area line for the observed limit #---------------------------------------- # add points at the corners of the plot # and add the first point at the end # to get a filled polygon, see http://root.cern.ch/root/roottalk/roottalk00/0256.html xvaluesForFilledGraph = masses[:] yvaluesForFilledGraph = observedValues[:] xvaluesForFilledGraph.append(masses[-1]) ; yvaluesForFilledGraph.append(ymax) xvaluesForFilledGraph.append(masses[0]) ; yvaluesForFilledGraph.append(ymax) xvaluesForFilledGraph.append(masses[0]) ; yvaluesForFilledGraph.append(observedValues[0]) grFilled = PlotUtils.makeGraphFromArrays(xvaluesForFilledGraph, yvaluesForFilledGraph) gcSaver.append(grFilled) grFilled.SetFillColor(ROOT.kOrange) grFilled.Draw("F") else: # draw a plain line for the observed grObserved = PlotUtils.makeGraphFromArrays(masses, observedValues) gcSaver.append(grObserved) grObserved.SetLineWidth(4) grObserved.Draw("C") #grObserved.Draw("L") #---------------------------------------- # expected graph #---------------------------------------- if includeExpected: grExpected = PlotUtils.makeGraphFromArrays(massesExpected, expectedValues) gcSaver.append(grExpected) grExpected.SetLineStyle(ROOT.kDashed) grExpected.SetLineWidth(4) grExpected.SetLineColor(ROOT.kRed) grExpected.Draw("C,same") #grExpected.Draw("L,same") label = makeGraphLabelOnRight(grExpected, min(masses), max(masses), "BG exp.") label.SetTextSize(label.GetTextSize() * 0.7) label.Draw() gcSaver.append(label) #---------------------------------------- # cross section graphs #---------------------------------------- for gr, scalingFactor in zip(xsectGraphs, crossSectionScalingFactors): #gr.Draw("L,same") gr.Draw("C,same") # label the cross section graph # get the y position at the end of the graph label = makeGraphLabelOnRight(gr, min(masses), max(masses), "%.0f * %s" % (scalingFactor, typeName)) label.SetTextSize(label.GetTextSize() * 0.7) label.Draw() gcSaver.append(label) #---------------------------------------- ROOT.gStyle.SetOptStat(0) hdummy.SetXTitle("m_{H} [GeV/c^{2}]") if relative: hdummy.SetYTitle("#sigma/#sigma(theo)") else: hdummy.SetYTitle("#sigma(%s) #times BR(#gamma#gamma) [pb]" % typeName) ROOT.gPad.SetGrid() label = ROOT.TLatex(0.5,0.85,"Excluded at 95% CL.") gcSaver.append(label) label.SetNDC(1) label.SetTextAlign(21) label.Draw() # make grid lines appear above graph hdummy.Draw("AXIGSAME") ROOT.gPad.Modified()
parser.add_option( "--observed-line-width", help="line width for observed graphs (set to zero to NOT ot show them)", type = int, default = 4, dest = "observedLineWidth", ) (options, ARGV) = parser.parse_args() #---------------------------------------- # process command line arguments #---------------------------------------- if len(ARGV) < 1: PlotUtils.optError("expected at least one non-option arguments.") # check whether any files were specified to contain ABSOLUTE cross # sections isabs = options.isabs options.isabs = set() for value in isabs: # check whether this is a file name if value in ARGV: # arriving here, it's a file name we know options.isabs.add(value) continue # try by position try:
parser.add_option( "--exclude-null-expected", help="removes those mass points for the expected limit graphs where the expected limit is zero", default = False, dest="exclude_null_expected", action = "store_true", ) (options, ARGV) = parser.parse_args() #---------------------------------------- if len(ARGV) != 1: PlotUtils.optError("expected exactly one non-option argument.") #---------------------------------------- if options.ignoreMassesExpected != None: options.ignoreMassesExpected = options.ignoreMassesExpected.split(',') options.ignoreMassesExpected = [ float(x) for x in options.ignoreMassesExpected ] #---------------------------------------------------------------------- import ROOT ROOT.gROOT.SetStyle("Plain") gcSaver = []
for gene in pb()(diffs): pu.svg_heatmap( ( None, expr_females.ix[[gene]], None, ase_females.ix[gene], None, ase_males.select(**sel_startswith('melXsim')).ix[gene], None, ase_males.select(**sel_startswith('simXmel')).ix[gene], None, expr_males.select(**sel_startswith('melXsim')).ix[[gene]], None, expr_males.select(**sel_startswith('simXmel')).ix[[gene]], ), 'analysis_godot/results/sex_diff/{}.svg'.format(gene), norm_rows_by=( 'female expression', 'max', 'females - L{:.03f} P{:.03f}'.format(female_logistic_r2[gene], female_peak_r2[gene]), 'center0pre', 'males - L{:.03f} P{:.03f}'.format(male_logistic_r2[gene], male_peak_r2[gene]), 'center0pre', '', 'center0pre', 'male expression', 'max', '', 'max', ), cmap=( None, pu.ISH, None, cm.RdBu, None, cm.RdBu, None, cm.RdBu, None, pu.ISH, None, pu.ISH, ), **pu_kwargs )
pred_ase = ((mel_expr_at_stage - sim_expr_at_matching) / denom) pred_ase.ix[denom < co] = 0 hyb_order = pred_ase.abs().sort_values().index scatter( mel_atlas_pos.ix[hyb_order, 'X', mel_stage], mel_atlas_pos.ix[hyb_order, 'Z', mel_stage], c=pred_ase.ix[hyb_order], cmap=cm.RdBu_r, vmin=-1, vmax=1, s=40, edgecolor=(0, 0, 0, 0) ) title(mel_stage + '/' + sim_stage) ax = gca() ax.set_aspect(1) ax.set_xlim(mel_atlas_pos.ix[:, 'X', mel_stage].min()-15, mel_atlas_pos.ix[:, 'X', mel_stage].max()+15) pu.minimize_ink(ax) savefig(path.join(cwd, 'analysis/results/{}_atlas_ase_M{}S{}' .format(target, mel_atlas_expr.minor_axis.get_loc(both_stage), sim_atlas_expr.minor_axis.get_loc(both_stage), )), transparent=True) figure() scatter( mel_atlas_pos.ix[mel_order, 'X', mel_stage], mel_atlas_pos.ix[mel_order, 'Z', mel_stage], c=(mel_expr_at_stage.ix[mel_order] ), cmap=cm.RdBu_r, vmin=-1, vmax=1, s=40, edgecolor=(0, 0, 0, 0) )
def makePlot(csvFnames, relative, includeExpected = True, fermiophobic = None, ymax = None, inputIsAbs = None, drawXsectBR = False, minMass = None, maxMass = None, plotLog = False ): """ @param relative if true, the exclusion of the ratios (relative to the inputs given) are plotted. If False, these ratios are multiplied by the SM cross sections and branching ratios into gamma gamma @param inputIsAbs is a list (set) of file names which should be treated as if they had ABSOLUTE limits on cross sections rather than relative limits. @param minMass and masMass can be used to restrict the plotting range """ #-------------------- # read the files #-------------------- data = [] color = 0 for fname in csvFnames: while True: color += 1 if color not in colorsToAvoid: break # define a name: if there is a comma in the file name assume that the # part before the comma is the actual file name and the part after it # is the label we should use # # if there is no comma, just use the basename (without .csv) as label pos = fname.find(',') if pos == -1: # not found name = os.path.basename(fname) if name.lower().endswith(".csv"): name = name[:-4] else: name = fname[pos+1:] fname = fname[:pos] masses, observedValues, expectedValues, \ expected_minus_2_sigma_values, \ expected_minus_1_sigma_values, \ expected_plus_1_sigma_values, \ expected_plus_2_sigma_values = PlotUtils.readCSV(open(fname), includeExpected) #-------------------- # filter on masses #-------------------- indices = range(len(masses)) if minMass != None: indices = [ i for i in indices if masses[i] >= minMass ] if maxMass != None: indices = [ i for i in indices if masses[i] <= maxMass ] masses = [ masses[i] for i in indices ] observedValues = [ observedValues[i] for i in indices ] expectedValues = [ expectedValues[i] for i in indices ] #-------------------- tmp = { "masses": masses, "observedValues": observedValues, "expectedValues": expectedValues, # for labels "name": name, # assign the color here "color": color, } data.append(tmp) #-------------------- # just to make sure we're not picking up something in the code afterwards del masses del observedValues del expectedValues #-------------------- if not relative: # if we're plotting the absolute cross sections, we # need to know whether this is Standard Model or Fermiophobic assert(fermiophobic != None) if fermiophobic: typeName = "FP" else: typeName = "SM" # convert to absolute cross sections for line, fname in zip(data, csvFnames): if fname in inputIsAbs: # already absolute continue line['observedValues'] = PlotUtils.multiplyArrayByXsectAndBR(line['masses'], line['observedValues'], fermiophobic) line['expectedValues'] = PlotUtils.multiplyArrayByXsectAndBR(line['masses'], line['expectedValues'], fermiophobic) else: # we're asked to plot relative results, convert to relative for those # inputs which are absolute for line, fname in zip(data, csvFnames): if not fname in inputIsAbs: # relative input, no need to convert continue line['observedValues'] = PlotUtils.divideArrayByXsectAndBR(line['masses'], line['observedValues'], fermiophobic) line['expectedValues'] = PlotUtils.divideArrayByXsectAndBR(line['masses'], line['expectedValues'], fermiophobic) #---------------------------------------- # legend #---------------------------------------- legend = ROOT.TLegend(options.legendXleft, options.legendYbottom, options.legendXright,options.legendYtop); gcSaver.append(legend) legend.SetShadowColor(0); legend.SetFillColor(0); legend.SetBorderSize(0); #---------------------------------------- # produce the 'observed' graphs #---------------------------------------- allGraphs = [] for line in data: gr = PlotUtils.makeGraphFromArrays(line['masses'], line['observedValues']) line['grObserved'] = gr gcSaver.append(gr) if options.observedLineWidth > 0: gr.SetLineWidth(options.observedLineWidth) else: # set default width for legend gr.SetLineWidth(4) gr.SetLineColor(line['color']) legend.AddEntry(gr,line['name'],"L") if options.observedLineWidth > 0: allGraphs.append(gr) #---------------------------------------- # produce the 'expected' graphs #---------------------------------------- if includeExpected: for line in data: grExpected = PlotUtils.makeGraphFromArrays(line['masses'], line['expectedValues']) gcSaver.append(grExpected) line['grExpected'] = grExpected grExpected.SetLineStyle(ROOT.kDashed) grExpected.SetLineWidth(4) grExpected.SetLineColor(line['color']) allGraphs.append(grExpected) # label = makeGraphLabelOnRight(grExpected, minMass, maxMass, "BG exp.") # label.SetTextSize(label.GetTextSize() * 0.7) # label.Draw() # gcSaver.append(label) #myCanvas = ROOT.TCanvas("myCanvas","Title Goes Here") #myCanvas.SetLogy(plotLog) #---------------------------------------- # produce the graph for the theoretical cross section #---------------------------------------- if drawXsectBR: # add a graph for the theoretical cross section # take the 'or' of all masses given import operator allMasses = sorted(reduce(operator.add, [ line['masses'] for line in data ] )) # for the moment, limit this to integer masses (in GeV) # (the cross section interpolation seems not yet to be functional) allMasses = sorted(list(set([ int(round(x)) for x in allMasses ]))) # print "allMasses=",allMasses theoXsectBr = [ PlotUtils.getXsectTimesBR(mass, fermiophobic) for mass in allMasses ] gr = PlotUtils.makeGraphFromArrays(allMasses, theoXsectBr) gr.SetLineWidth(4) gr.SetLineStyle(ROOT.kDotted) legend.AddEntry(gr,"theo. #sigma * BR","L") gcSaver.append(gr) allGraphs.append(gr) #---------------------------------------- # determine the y scale #---------------------------------------- if ymax == None: # determine this from the values, not from the graphs # (is there a way to do this from the graphs ?) ymax = max([value for line in data for value in line['observedValues'] ]) if includeExpected: ymax = max(ymax, max([value for line in data for value in line['expectedValues'] ])) ymax *= 1.1 # TODO: remove this if statement ?! if not relative: if fermiophobic: # fix the y scale by hand in order not to # stretch it too much because of large # scaling factors for the theoretical expectation ymax = 0.5 #---------------------------------------- # determine x scale (mass range) #---------------------------------------- allMasses = [value for line in data for value in line['masses'] ] actualMinMass = min(allMasses) actualMaxMass = max(allMasses) del allMasses #---------------------------------------- # create a dummy histogram to set the x range hdummy = ROOT.TH1F("hdummy","",1,actualMinMass,actualMaxMass) gcSaver.append(hdummy) hdummy.SetMaximum(ymax) hdummy.Draw() ROOT.gStyle.SetOptTitle(0) #---------------------------------------- # draw the graphs #---------------------------------------- for gr in allGraphs: gr.Draw("C,same") #gr.Draw("L,same") #---------------------------------------- ROOT.gStyle.SetOptStat(0) hdummy.SetXTitle("m_{H} [GeV/c^{2}]") hdummy.GetYaxis().SetTitleOffset(1.2 * hdummy.GetYaxis().GetTitleOffset()) if relative: hdummy.SetYTitle("#sigma/#sigma(theo)") else: hdummy.SetYTitle("#sigma(%s) * BR(#gamma#gamma) [pb]" % typeName) ROOT.gPad.SetGrid() if options.showTitle: label = ROOT.TLatex(0.5,0.85,"Excluded at 95% CL.") gcSaver.append(label) label.SetNDC(1) label.SetTextAlign(21) label.Draw() legend.Draw() ROOT.gPad.Modified() ROOT.gPad.Modified()
all_in_region_ix = (region[0] <= xs) & (xs < region[1]) mel_in_region_ix = (region[0] <= mel_xs) & (mel_xs < region[1]) sim_in_region_ix = (region[0] <= sim_xs) & (sim_xs < region[1]) parental_in_region_ix = (region[0] <= parental_xs) & (parental_xs < region[1]) gene_expr_level = parental.ix[peak_genes, parental_in_region_ix].min(axis=1) expr_in_region = ut.true_index(gene_expr_level > -1) non_mat_expr_genes = expr_in_region.difference(mat_genes) non_mat_expr_genes = mel_sim_ratio.ix[non_mat_expr_genes, mel_in_region_ix].mean(axis=1).sort_values().index nmegs.append(non_mat_expr_genes) pu.svg_heatmap(expr.ix[non_mat_expr_genes, all_in_region_ix], 'analysis/results/non_mat_{}_{:0.2f}-{:0.2f}_expr_genes.svg' .format(tf, region[0], region[1]), squeeze_rows=np.nanmean, **kwargs) mel_in_region = (melXmel.ix[non_mat_expr_genes, mel_in_region_ix] .divide(parental.ix[non_mat_expr_genes, :] .max(axis=1), axis=0) .mean(axis=1)) sim_in_region = (simXsim.ix[non_mat_expr_genes, sim_in_region_ix] .divide(parental.ix[non_mat_expr_genes, :] .max(axis=1), axis=0) .mean(axis=1)) res = stats.ttest_rel( mel_in_region, sim_in_region, nan_policy='omit',) distro_mels.append(mel_in_region.dropna()) distro_sims.append(sim_in_region.dropna())
'w') ) ase = ( pd.read_table('analysis_godot/ase_summary_by_read.tsv', **ut.pd_kwargs) .ix[target_gene] .select(ut.startswith(('melXsim', 'simXmel'))) ) rnaseq_expr = ( pd.read_table('analysis_godot/summary.tsv', **ut.pd_kwargs) .drop('---', axis=1, errors='ignore') .select(**ut.sel_startswith(('melXsim', 'simXmel'))) .rename(columns=lambda x: x.replace('FPKM', 'ase_value')) ) pu.svg_heatmap(ase, 'analysis/results/model_tweak/{}_ase.svg'.format(target_gene), norm_rows_by='center0pre', cmap=pu.cm.RdBu, **pu.kwargs_single_column ) n_good = ase.select(ut.startswith('melXsim_cyc14C_rep1')).count() for tf in (best_model .params.index.intersection( [key for key, value in changes.items() if value != 1]) ): mpl.cm.RdBu.set_bad((0.5, 0.5, 0.5)) mpl.cm.RdBu_r.set_bad((0.5, 0.5, 0.5)) fig, ax1, ax2 = plot_both(best_model, {tf: changes[tf]}, small_atlas.ix[small_atlas.in_central], xlims=xlims, ylims=ylims, denom_cutoff=0)
pu.svg_heatmap( ( None, mel.ix[gene], None, None, None, None, None, all_mel_pred.ix[gene], None, sim.ix[gene], None, None, None, None, None, all_sim_pred.ix[gene], None, ase.ix[gene], None, all_pred_ase_nan.ix[gene], None, hyb.ix[gene], ), 'analysis_godot/results/ase_preds/by_trans/{:+0.3f}-{}.svg'.format( trans.ix[gene], gene), norm_rows_by=( 'mel expression', 'max', '', '', '', '', 'predicted mel expression', 'max', 'sim expression', 'max', '', '', '', '', 'predicted sim expression', 'max', 'ASE', 'center0pre', 'predicted ASE - EMD {:.03f}'.format(ase_avgs.ix[gene, 'emd']), 'center0pre', 'hybrid expression', 'max', ), cmap=( None, pu.ISH, None, None, None, None, None, cm.Reds, None, pu.ISH, None, None, None, None, None, cm.Blues, None, cm.RdBu, None, cm.RdBu, None, pu.ISH, ), **pu_kwargs)
best_r2 = r2_peak.copy() for gene in r2_logist.index: # Note that I use "and not > " rather than "<" to catch nans if ((gene in best_r2 and not best_r2[gene] > r2_logist[gene]) or (gene not in best_r2)): best_r2[gene] = r2_logist[gene] best_r2.to_csv('analysis/results/{prefix}svase{suffix}_best'.format( prefix=args.prefix, suffix=args.suffix), sep='\t') pu.svg_heatmap(ase.ix[r2_logist_genes], 'analysis/results/{prefix}logist{suffix}_ase.svg' .format(prefix=args.prefix, suffix=args.suffix), norm_rows_by='center0pre', row_labels=['{:.02f} {}'.format(best_r2[g], g) for g in r2_logist_genes], cmap=cm.RdBu, **kwargs) pu.svg_heatmap(ase.ix[r2_peak_genes], 'analysis/results/{prefix}peak{suffix}_ase.svg' .format(prefix=args.prefix, suffix=args.suffix), norm_rows_by='center0pre', row_labels=['{:.02f} {}'.format(best_r2[g], g) for g in r2_peak_genes], cmap=cm.RdBu, **kwargs) pu.svg_heatmap(ase.ix[r2_logist_genes].select(**ut.sel_contains('rep1')), 'analysis/results/{prefix}logist{suffix}_ase_r1.svg'
maternals = {line.strip() for line in open('analysis/results/maternal.txt')} mel = expr.select(**ut.sel_startswith('melXmel_')) sim = expr.select(**ut.sel_startswith('simXsim_')) hyb = expr.select(**ut.sel_startswith(('melXsim', 'simXmel'))) emds = pd.Series(index=maternals, data=pd.np.nan) ones = pd.Series(index=expr.columns, data=1) for gene in tqdm(maternals): if gene not in expr.index: continue emds[gene] = dd.earth_mover_multi_rep( expr.ix[gene]+1, ones ) emds = emds.sort_values(na_position='first') emds2 = emds[-50:] kwargs = pu.kwargs_expr_heatmap.copy() kwargs['progress_bar'] = True kwargs.pop('norm_rows_by') pu.svg_heatmap((expr.ix[emds2.index], ase.ix[emds2.index]), 'analysis/results/matspatpat.svg', cmap=(pu.ISH, pu.cm.RdBu), norm_rows_by=('maxall', 'center0pre'), row_labels=[ ('{:.03f}'.format(emds[i]), i) for i in emds2.index ], **kwargs)
def onDisable(): PlotUtils.saveall() for i in range(0, 100): print 'CATS' return True
def transmit_image(self, img, dim, noise_var=0, EYES=False, SAVED=False): print('Preprocessing image ...') bits, maxval, minval = self.__ip.image_to_bits(img) print('Modulating bits ...') t_mod, mod_signal = self.__tx.transmit_bits(bits) N_sig = len(mod_signal) print('Transmitting signal ...') t_tx, tx_signal = self.__ch.transmit_signal(mod_signal) tx_signal = self.__ch.add_awgn(tx_signal, var=noise_var) print('Receiving signal ...') t_rec, rec_signal = self.__rx.receive_signal(tx_signal) print('Equalizing signal ...') t_eq, eq_signal = self.__eq.equalize_signal(rec_signal) print('Sampling signal ...') samp_signal = self.__rx.sample_sig_to_bits(t_eq, eq_signal)[:np.size(bits)] print('Postprocessing image ...') px_n_bits = np.ceil(np.log2(np.max(img))) img_out = self.__ip.bits_to_image(samp_signal, px_n_bits, dim, maxval, minval) if EYES: print('Plotting eye diagrams ...') # eye after transmitter plt.figure() myplt.eye_diagram_plot(t_mod, mod_signal, 1, eye_N=32, titleText='Eye Diagram after TX: %s' % self.__tx.name) if SAVED: myplt.save_current('eye_tx_%s.png' % self.__tx.name, 'PLOT') # eye after channel and noise plt.figure() myplt.eye_diagram_plot( t_tx[:N_sig], tx_signal[:N_sig], 1, eye_N=32, titleText='Eye Diagram after CH: %s Noise: %.4f' % (self.__ch.name, noise_var)) if SAVED: myplt.save_current( 'eye_tx_%s_ch_%s_ns_%.4f.png' % (self.__tx.name, self.__ch.name, noise_var), 'PLOT') # eye after matched filter plt.figure() myplt.eye_diagram_plot( t_rec[:N_sig], rec_signal[:N_sig], 1, eye_N=2 * 32, titleText='Eye Diagram after RX: %s Noise: %.4f' % (self.__rx.name, noise_var)) if SAVED: myplt.save_current( 'eye_rx_%s_ch_%s_ns_%.4f.png' % (self.__rx.name, self.__ch.name, noise_var), 'PLOT') # eye after equalizer plt.figure() myplt.eye_diagram_plot( t_eq[:N_sig], eq_signal[:N_sig], 1, eye_N=2 * 32, titleText='Eye Diagram after EQ: %s Noise: %.4f' % (self.__eq.name, noise_var)) if SAVED: myplt.save_current( 'eye_rx_%s_ch_%s_eq_%s_ns_%.4f.png' % (self.__rx.name, self.__ch.name, self.__eq.name, noise_var), 'PLOT') return img_out
) ranges = { 'meldominant': ('FBgn0034816', 'FBgn0250755'), 'simdominant': ('FBgn0004087', 'FBgn0038934'), } if 'sparse' in argv[1]: pu.svg_heatmap( data=exp_cdt.select(**sel_startswith(columns)), filename='analysis/results/all_sparse.svg', norm_rows_by='max', progress_bar=True, col_sep='_sl', total_width=120, box_height=1, split_columns=True, draw_box=True, draw_row_labels=False, draw_name=True, cmap_by_prefix=cmap_by_prefix, make_hyperlinks=True, convert=True, ) from sys import exit exit() elif '--all' in argv: max_height=8.5*300 max_width=7.25*300/18 sparsity = int(ceil(len(exp_cdt)/max_height)) pu.svg_heatmap( data=exp_cdt.select(**sel_startswith(columns))[::int(sparsity)],
slns = [] combinedSln = {} U = [] edgeVals = [] edges = [] M = 0 uAlloced = False for slnFile in slnFiles: sln = eu.importEbbSolution(slnFile) if((sln['version'] == 1) and combineSln): raise Exception("Cannot recombine solution files with file version 1") if(not combineSln): F = pu.getField(sln['U'], state) minVal = np.min(F) globalMin = min(globalMin, minVal) maxVal = np.max(F) globalMax = max(globalMax, maxVal) slns.append(sln) else: raise Exception("Recombing solutions not yet supported") print("globalMin = "+str(globalMin)) print("globalMax = "+str(globalMax)) globalMin = globalMin - 0.001 globalMax = globalMax + 0.001
parser.add_option( "--observed-line-width", help="line width for observed graphs (set to zero to NOT ot show them)", type=int, default=4, dest="observedLineWidth", ) (options, ARGV) = parser.parse_args() #---------------------------------------- # process command line arguments #---------------------------------------- if len(ARGV) < 1: PlotUtils.optError("expected at least one non-option arguments.") # check whether any files were specified to contain ABSOLUTE cross # sections isabs = options.isabs options.isabs = set() for value in isabs: # check whether this is a file name if value in ARGV: # arriving here, it's a file name we know options.isabs.add(value) continue # try by position try:
print(best_model.summary().as_latex(), file=open( path.join( dir, 'analysis/results/model_tweak/{}_model.tex'.format( target_gene)), 'w')) ase = (pd.read_table('analysis_godot/ase_summary_by_read.tsv', **ut.pd_kwargs).ix[target_gene].select( ut.startswith(('melXsim', 'simXmel')))) rnaseq_expr = (pd.read_table( 'analysis_godot/summary.tsv', **ut.pd_kwargs).drop('---', axis=1, errors='ignore').select( **ut.sel_startswith(('melXsim', 'simXmel'))).rename( columns=lambda x: x.replace('FPKM', 'ase_value'))) pu.svg_heatmap( ase, 'analysis/results/model_tweak/{}_ase.svg'.format(target_gene), norm_rows_by='center0pre', cmap=pu.cm.RdBu, **pu.kwargs_single_column) n_good = ase.select(ut.startswith('melXsim_cyc14C_rep1')).count() for tf in (best_model.params.index.intersection( [key for key, value in changes.items() if value != 1])): mpl.cm.RdBu.set_bad((0.5, 0.5, 0.5)) mpl.cm.RdBu_r.set_bad((0.5, 0.5, 0.5)) fig, ax1, ax2 = plot_both(best_model, {tf: changes[tf]}, small_atlas.ix[small_atlas.in_central], xlims=xlims, ylims=ylims, denom_cutoff=0) fig.savefig(
def makePlot(csvFnames, relative, includeExpected=True, fermiophobic=None, ymax=None, inputIsAbs=None, drawXsectBR=False, minMass=None, maxMass=None, plotLog=False): """ @param relative if true, the exclusion of the ratios (relative to the inputs given) are plotted. If False, these ratios are multiplied by the SM cross sections and branching ratios into gamma gamma @param inputIsAbs is a list (set) of file names which should be treated as if they had ABSOLUTE limits on cross sections rather than relative limits. @param minMass and masMass can be used to restrict the plotting range """ #-------------------- # read the files #-------------------- data = [] color = 0 for fname in csvFnames: while True: color += 1 if color not in colorsToAvoid: break # define a name: if there is a comma in the file name assume that the # part before the comma is the actual file name and the part after it # is the label we should use # # if there is no comma, just use the basename (without .csv) as label pos = fname.find(',') if pos == -1: # not found name = os.path.basename(fname) if name.lower().endswith(".csv"): name = name[:-4] else: name = fname[pos + 1:] fname = fname[:pos] masses, observedValues, expectedValues, \ expected_minus_2_sigma_values, \ expected_minus_1_sigma_values, \ expected_plus_1_sigma_values, \ expected_plus_2_sigma_values = PlotUtils.readCSV(open(fname), includeExpected) #-------------------- # filter on masses #-------------------- indices = range(len(masses)) if minMass != None: indices = [i for i in indices if masses[i] >= minMass] if maxMass != None: indices = [i for i in indices if masses[i] <= maxMass] masses = [masses[i] for i in indices] observedValues = [observedValues[i] for i in indices] expectedValues = [expectedValues[i] for i in indices] #-------------------- tmp = { "masses": masses, "observedValues": observedValues, "expectedValues": expectedValues, # for labels "name": name, # assign the color here "color": color, } data.append(tmp) #-------------------- # just to make sure we're not picking up something in the code afterwards del masses del observedValues del expectedValues #-------------------- if not relative: # if we're plotting the absolute cross sections, we # need to know whether this is Standard Model or Fermiophobic assert (fermiophobic != None) if fermiophobic: typeName = "FP" else: typeName = "SM" # convert to absolute cross sections for line, fname in zip(data, csvFnames): if fname in inputIsAbs: # already absolute continue line['observedValues'] = PlotUtils.multiplyArrayByXsectAndBR( line['masses'], line['observedValues'], fermiophobic) line['expectedValues'] = PlotUtils.multiplyArrayByXsectAndBR( line['masses'], line['expectedValues'], fermiophobic) else: # we're asked to plot relative results, convert to relative for those # inputs which are absolute for line, fname in zip(data, csvFnames): if not fname in inputIsAbs: # relative input, no need to convert continue line['observedValues'] = PlotUtils.divideArrayByXsectAndBR( line['masses'], line['observedValues'], fermiophobic) line['expectedValues'] = PlotUtils.divideArrayByXsectAndBR( line['masses'], line['expectedValues'], fermiophobic) #---------------------------------------- # legend #---------------------------------------- legend = ROOT.TLegend(options.legendXleft, options.legendYbottom, options.legendXright, options.legendYtop) gcSaver.append(legend) legend.SetShadowColor(0) legend.SetFillColor(0) legend.SetBorderSize(0) #---------------------------------------- # produce the 'observed' graphs #---------------------------------------- allGraphs = [] for line in data: gr = PlotUtils.makeGraphFromArrays(line['masses'], line['observedValues']) line['grObserved'] = gr gcSaver.append(gr) if options.observedLineWidth > 0: gr.SetLineWidth(options.observedLineWidth) else: # set default width for legend gr.SetLineWidth(4) gr.SetLineColor(line['color']) legend.AddEntry(gr, line['name'], "L") if options.observedLineWidth > 0: allGraphs.append(gr) #---------------------------------------- # produce the 'expected' graphs #---------------------------------------- if includeExpected: for line in data: grExpected = PlotUtils.makeGraphFromArrays(line['masses'], line['expectedValues']) gcSaver.append(grExpected) line['grExpected'] = grExpected grExpected.SetLineStyle(ROOT.kDashed) grExpected.SetLineWidth(4) grExpected.SetLineColor(line['color']) allGraphs.append(grExpected) # label = makeGraphLabelOnRight(grExpected, minMass, maxMass, "BG exp.") # label.SetTextSize(label.GetTextSize() * 0.7) # label.Draw() # gcSaver.append(label) #myCanvas = ROOT.TCanvas("myCanvas","Title Goes Here") #myCanvas.SetLogy(plotLog) #---------------------------------------- # produce the graph for the theoretical cross section #---------------------------------------- if drawXsectBR: # add a graph for the theoretical cross section # take the 'or' of all masses given import operator allMasses = sorted( reduce(operator.add, [line['masses'] for line in data])) # for the moment, limit this to integer masses (in GeV) # (the cross section interpolation seems not yet to be functional) allMasses = sorted(list(set([int(round(x)) for x in allMasses]))) # print "allMasses=",allMasses theoXsectBr = [ PlotUtils.getXsectTimesBR(mass, fermiophobic) for mass in allMasses ] gr = PlotUtils.makeGraphFromArrays(allMasses, theoXsectBr) gr.SetLineWidth(4) gr.SetLineStyle(ROOT.kDotted) legend.AddEntry(gr, "theo. #sigma * BR", "L") gcSaver.append(gr) allGraphs.append(gr) #---------------------------------------- # determine the y scale #---------------------------------------- if ymax == None: # determine this from the values, not from the graphs # (is there a way to do this from the graphs ?) ymax = max( [value for line in data for value in line['observedValues']]) if includeExpected: ymax = max( ymax, max([ value for line in data for value in line['expectedValues'] ])) ymax *= 1.1 # TODO: remove this if statement ?! if not relative: if fermiophobic: # fix the y scale by hand in order not to # stretch it too much because of large # scaling factors for the theoretical expectation ymax = 0.5 #---------------------------------------- # determine x scale (mass range) #---------------------------------------- allMasses = [value for line in data for value in line['masses']] actualMinMass = min(allMasses) actualMaxMass = max(allMasses) del allMasses #---------------------------------------- # create a dummy histogram to set the x range hdummy = ROOT.TH1F("hdummy", "", 1, actualMinMass, actualMaxMass) gcSaver.append(hdummy) hdummy.SetMaximum(ymax) hdummy.Draw() ROOT.gStyle.SetOptTitle(0) #---------------------------------------- # draw the graphs #---------------------------------------- for gr in allGraphs: gr.Draw("C,same") #gr.Draw("L,same") #---------------------------------------- ROOT.gStyle.SetOptStat(0) hdummy.SetXTitle("m_{H} [GeV/c^{2}]") hdummy.GetYaxis().SetTitleOffset(1.2 * hdummy.GetYaxis().GetTitleOffset()) if relative: hdummy.SetYTitle("#sigma/#sigma(theo)") else: hdummy.SetYTitle("#sigma(%s) * BR(#gamma#gamma) [pb]" % typeName) ROOT.gPad.SetGrid() if options.showTitle: label = ROOT.TLatex(0.5, 0.85, "Excluded at 95% CL.") gcSaver.append(label) label.SetNDC(1) label.SetTextAlign(21) label.Draw() legend.Draw() ROOT.gPad.Modified() ROOT.gPad.Modified()