def create_scurve(scurves, scurveFile): parse_functions.removing_existing_file(scurveFile) print("Creating scurve file: " + scurveFile) import matplotlib.pylab as plt fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.set_ylim([0, 9]) for i in range(1,10,1): plt.axhline(y = i, color ="black", linestyle ="--", linewidth=0.1, dashes=(5, 10)) for strategy in scurves: length = len(scurves[strategy]) middle = int(length/2-1) markers_on = [0, middle, length-1] #plt.plot(scurves[strategy], config_generator.symbols[strategy], color=config_generator.colors[strategy], markevery=marks, label=config_generator.abbreviations[strategy]) plt.plot(scurves[strategy], config_generator.symbols[strategy], color=config_generator.colors[strategy], markevery=markers_on, label=config_generator.abbreviations[strategy], dashes=(5, 5)) plt.ylabel('Normalized Times') plt.locator_params(nbins=3) plt.xticks([]) # hide axis x plt.legend() # show line names plt.savefig(scurveFile, format='eps')
def set_axes_and_legend(ax): ax.spines['left'].set_position('zero') plt.locator_params(axis='x', nbins=4) ax.axes.yaxis.set_ticks([]) if has_seaborn: sns.despine(ax=ax) plt.legend(loc='upper right', fontsize='x-small')
def multiPlot(myData): ax=plt.subplot2grid((ncol,nrow),(yPanel,xPanel)) ax.hist(df[quantVar[myData]],alpha=0.5)#alpha?? ax.set_xlabel(quantVar[myData],fontsize=14,fontweight='bold') #ax.set_ylabel('Y',fontsize=14,fontweight='bold',rotation='Vertical') plt.locator_params(axis = 'x', nbins = 2) ax.tick_params(labelsize=14)
def set_axes_and_legend(ax): ax.spines["left"].set_position("zero") plt.locator_params(axis="x", nbins=4) ax.axes.yaxis.set_ticks([]) if has_seaborn: sns.despine(ax=ax) plt.legend(loc="upper right", fontsize="x-small")
def plot_pr_curve(precision, recall, title): plt.figure() plt.rcParams['figure.figsize'] = 7, 5 plt.locator_params(axis = 'x', nbins = 5) plt.plot(precision, recall, 'b-', linewidth=4.0, color = '#B0017F') plt.title(title) plt.xlabel('Precision') plt.ylabel('Recall') plt.rcParams.update({'font.size': 16})
def plot_convolved_rates_in_time(spike_data, prm): print('plotting spike rastas...') save_path = prm.get_output_path() + '/Figures/ConvolvedRates_InTime' if os.path.exists(save_path) is False: os.makedirs(save_path) for cluster_index in range(len(spike_data)): cluster_index = spike_data.cluster_id.values[cluster_index] - 1 spikes_on_track = plt.figure(figsize=(4, 5)) ax = spikes_on_track.add_subplot(1, 1, 1) # specify (nrows, ncols, axnum) firing_rate = spike_data.loc[cluster_index].spike_rate_in_time speed = spike_data.loc[cluster_index].speed_rate_in_time x_max = np.max(firing_rate) ax.plot(firing_rate, speed, '|', color='Black', markersize=4) plt.ylabel('Firing rate (Hz)', fontsize=12, labelpad=10) plt.xlabel('Speed (cm/sec)', fontsize=12, labelpad=10) plt.xlim(0, 200) ax.yaxis.set_ticks_position('left') ax.xaxis.set_ticks_position('bottom') plot_utility.style_track_plot(ax, 200) plot_utility.style_vr_plot(ax, x_max) plt.locator_params(axis='y', nbins=4) try: plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0) except ValueError: continue plt.savefig(save_path + '/' + spike_data.session_id[cluster_index] + '_rate_versus_SPEED_' + str(cluster_index + 1) + '.png', dpi=200) plt.close() spikes_on_track = plt.figure(figsize=(4, 5)) ax = spikes_on_track.add_subplot(1, 1, 1) # specify (nrows, ncols, axnum) position = spike_data.loc[cluster_index].location_rate_in_time ax.plot(firing_rate, position, '|', color='Black', markersize=4) plt.ylabel('Firing rate (Hz)', fontsize=12, labelpad=10) plt.xlabel('Location (cm)', fontsize=12, labelpad=10) plt.xlim(0, 200) ax.yaxis.set_ticks_position('left') ax.xaxis.set_ticks_position('bottom') plot_utility.style_track_plot(ax, 200) plot_utility.style_vr_plot(ax, x_max) plt.locator_params(axis='y', nbins=4) try: plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0) except ValueError: continue plt.savefig(save_path + '/' + spike_data.session_id[cluster_index] + '_rate_versus_POSITION_' + str(cluster_index + 1) + '.png', dpi=200) plt.close()
def wordshift_plot(topvals, xpadding=0.0, textopts={}): c1 = plt.rcParams['axes.color_cycle'][0] c2 = plt.rcParams['axes.color_cycle'][1] all_ixs = np.arange(len(topvals)) scolor = 0 is_pos = np.array((topvals.ws >= 0).tolist()) kw = { 'color': topvals.cols.values.tolist() } if 'cols' in topvals.columns else {} plt.barh(range(len(topvals)), topvals.ws.values, edgecolor='None', height=.4, **kw) plt.vlines(0, -20, 20, color='k', lw=LINEWIDTH) xmin, xmax = None, None invTrans = plt.gca().transData.inverted() for y, x in enumerate(topvals.ws): cword = topvals.index.values[y] ckw = textopts.copy() if 'cols' in topvals.columns: ckw['color'] = topvals.iloc[y].cols textobj = plt.text(x, y - 0.1, ' ' + cword + ' ', ha='left' if x > 0 else 'right', va='center', **ckw) plt.draw() we = textobj.get_window_extent() cxmax, _ = invTrans.transform((we.xmax, we.ymax)) if xmax is None or xmax < cxmax: xmax = cxmax cxmin, _ = invTrans.transform((we.xmin, we.ymin)) if xmin is None or xmin > cxmin: xmin = cxmin plt.xlim([xmin - xpadding, xmax + xpadding]) #plt.xlim([xmin*1.1-xpadding, xmax*1.1+xpadding]) plt.ylim([-0.5, len(topvals)]) plt.yticks([]) #plt.gca().spines['top'].setp('color', 'k') plt.gca().spines['left'].set_visible(False) plt.gca().spines['right'].set_visible(False) plt.locator_params(axis='x', nbins=5)
def run(): # Call the C NSAT print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(nsat.fnames.pickled) nsat.run_c_nsat() # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, nsat.fnames) states = c_nsat_reader.read_c_nsat_states() states_core0 = states[0][1] # wt = c_nsat_reader.read_c_nsat_weights_evo(0)[:, 1, 1] wt, pids = c_nsat_reader.read_synaptic_weights_history(post=[0]) in_spikelist = SL out_spikelist = nsat.importAER(nsat.read_from_file(nsat.fnames.events+'_core_0.dat'), sim_ticks=sim_ticks, id_list=[0]) # Plot the results fig = plt.figure(figsize=(10, 10)) i = 1 ax = fig.add_subplot(4, 1, i) ax.plot(states_core0[:-1, 0, i - 1], 'b', lw=3, label='$V_m$') ax.set_ylabel('$V_m$') ax.set_xticks([]) plt.locator_params(axis='y', nbins=4) i = 2 ax = fig.add_subplot(4, 1, i) ax.plot(states_core0[:-1, 0, i - 1], 'b', lw=3, label='$I_{syn}$') ax.set_ylabel('$I_{syn}$') ax.set_xticks([]) plt.locator_params(axis='y', nbins=4) for t in SL[0].spike_times: plt.axvline(t, color='k') i = 4 ax = fig.add_subplot(4, 1, i) for t in SL[0].spike_times: plt.axvline(t, color='k') ax.plot(states_core0[:-1, 0, 2], 'b', lw=3, label='$x_m$') ax.set_ylabel('$x_m$') plt.axhline(0, color='b', alpha=.5, linewidth=3) plt.locator_params(axis='y', nbins=4) i = 3 ax = fig.add_subplot(4, 1, i) for t in SL[0].spike_times: plt.axvline(t, color='k') ax.plot(wt[0][:, 1, 1], 'r', lw=3) ax.set_ylabel('$w$') ax.set_xticks([]) plt.locator_params(axis='y', nbins=4) plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('End %s:run()' % (os.path.splitext(os.path.basename(__file__))[0]))
# wt = c_nsat_reader.read_c_nsat_weights_evo(0)[:, 1, 1] wt = c_nsat_reader.read_c_nsat_weights_evo(0) in_spikelist = SL out_spikelist = nsat.importAER( nsat.read_from_file(c_nsat_writer.fname.events + '_core_0.dat'), sim_ticks=sim_ticks, id_list=[0]) # Plot the results fig = plt.figure(figsize=(10, 10)) i = 1 ax = fig.add_subplot(4, 1, i) ax.plot(states_core0[:-1, 0, i - 1], 'b', lw=3, label='$V_m$') ax.set_ylabel('$V_m$') ax.set_xticks([]) plt.locator_params(axis='y', nbins=4) i = 2 ax = fig.add_subplot(4, 1, i) ax.plot(states_core0[:-1, 0, i - 1], 'b', lw=3, label='$I_{syn}$') ax.set_ylabel('$I_{syn}$') ax.set_xticks([]) plt.locator_params(axis='y', nbins=4) for t in SL[0].spike_times: plt.axvline(t, color='k') i = 4 ax = fig.add_subplot(4, 1, i) for t in SL[0].spike_times: plt.axvline(t, color='k') ax.plot(states_core0[:-1, 0, 2], 'b', lw=3, label='$x_m$') ax.set_ylabel('$x_m$')
return fig_dim plt.figure(5, figsize=set_size(width / 2)) for i in range(filters.shape[1]): plt.plot(total_params, filters[:, i], label="Layer {}".format(i + 1), color=cmap(float(i) / filters.shape[1])) plt.plot(total_params, filt[i], label="Layer {} (fit)".format(i + 1), linestyle="dashed", color=cmap(float(i) / filters.shape[1])) plt.locator_params(axis='x', nbins=5) # plt.title("Layer's Filter vs Total Parameter") plt.xlabel("Total Parameters") plt.ylabel("Filters") plt.legend() plt.grid() plt.tight_layout() plt.savefig("savefigs/growth_{}_{}.pdf".format(args.model, args.dataset)) # ###### total_params = np.arange(1000, 100000000, 5000) filt = np.array([total_params**b for b in beta]) filt = np.multiply(filt.transpose(), alpha).transpose() print(filt[0]) for i in range(filters.shape[1]):
def run(): # Call the C NSAT print('Begin %s:run()' % (os.path.splitext(os.path.basename(__file__))[0])) cfg = nsat.ConfigurationNSAT.readfileb(nsat.fnames.pickled) nsat.run_c_nsat() # Load the results (read binary files) c_nsat_reader = nsat.C_NSATReader(cfg, nsat.fnames) states = c_nsat_reader.read_c_nsat_states() time_core0, states_core0 = states[core][0], states[core][1] wt, pids = c_nsat_reader.read_synaptic_weights_history( post=[130, 150, 120]) wt, pids = wt[0], pids[0] in_spikelist = SL out_spikelist = nsat.importAER(c_nsat_reader.read_events(0), sim_ticks=sim_ticks, id_list=[0]) plt.matplotlib.rcParams['figure.subplot.bottom'] = .1 plt.matplotlib.rcParams['figure.subplot.left'] = .2 plt.matplotlib.rcParams['figure.subplot.right'] = .98 plt.matplotlib.rcParams['figure.subplot.hspace'] = .1 plt.matplotlib.rcParams['figure.subplot.top'] = 1.0 # Plot the results fig = plt.figure(figsize=(14, 10)) i = 4 ax1 = fig.add_subplot(5, 1, i) for t in SL[100].spike_times: plt.axvline(t, color='g', alpha=.4) ax1.plot(states_core0[:-1, 0, 2], 'b', lw=3, label='$x_m$') ax1.set_ylabel('$x_m$') ax1.get_yaxis().set_label_coords(-0.12, 0.5) plt.setp(ax1.get_xticklabels(), visible=False) plt.axhline(0, color='b', alpha=.5, linewidth=3) plt.locator_params(axis='y', nbins=4) i = 5 ax = fig.add_subplot(5, 1, i, sharex=ax1) for t in SL[0].spike_times: plt.axvline(t, color='k', alpha=.4) ax.plot(wt[:, 19, 1], 'r', lw=3) # ax.imshow(wt[:, :, 1], aspect='auto', interpolation='nearest') ax.set_ylabel('$w$') ax.set_xlabel('Time Step') ax.get_yaxis().set_label_coords(-0.12, 0.5) plt.locator_params(axis='y', nbins=4) i = 2 ax = fig.add_subplot(5, 1, i, sharex=ax1) ax.plot(states_core0[:-1, 0, 0], 'b', lw=3, label='$V_m$') ax.set_ylabel('$V_m$') ax.get_yaxis().set_label_coords(-0.12, 0.5) plt.setp(ax.get_xticklabels(), visible=False) plt.locator_params(axis='y', nbins=4) i = 3 ax = fig.add_subplot(5, 1, i, sharex=ax1) ax.plot(states_core0[:-1, 0, 1], 'b', lw=3, label='$I_{syn}$') ax.set_ylabel('$I_{syn}$') ax.get_yaxis().set_label_coords(-0.12, 0.5) plt.setp(ax.get_xticklabels(), visible=False) plt.locator_params(axis='y', nbins=4) for t in np.ceil(SL[0].spike_times): plt.axvline(t, color='k', alpha=.4) ax1 = fig.add_subplot(5, 1, 1, sharex=ax1) out_spikelist.id_slice([0]).raster_plot(display=ax1, kwargs={'color': 'b'}) out_spikelist.id_slice(list(range(1, 30))).raster_plot(display=ax1, kwargs={'color': 'k'}) ax1.set_xlabel('') plt.setp(ax1.get_xticklabels(), visible=False) ax1.get_yaxis().set_label_coords(-0.12, 0.5) plt.tight_layout() plt.savefig('/tmp/%s.png' % (os.path.splitext(os.path.basename(__file__))[0])) plt.close() print('End %s:run()' % (os.path.splitext(os.path.basename(__file__))[0]))
def plot_spikes_on_track(spike_data, raw_position_data, processed_position_data, prm, prefix): print('plotting spike rastas...') save_path = prm.get_output_path() + '/Figures/spike_trajectories' if os.path.exists(save_path) is False: os.makedirs(save_path) rewarded_locations = np.array( processed_position_data['rewarded_stop_locations'].dropna(axis=0)) # rewarded_trials = np.array( processed_position_data['rewarded_trials'].dropna(axis=0)) for cluster_index in range(len(spike_data)): cluster_index = spike_data.cluster_id.values[cluster_index] - 1 x_max = max( np.array(spike_data.at[cluster_index, 'beaconed_trial_number'])) + 1 spikes_on_track = plt.figure(figsize=(4, (x_max / 32))) ax = spikes_on_track.add_subplot(1, 1, 1) # specify (nrows, ncols, axnum) #uncomment if you want to plot stops #ax.plot(beaconed[:,0], beaconed[:,1], 'o', color='LimeGreen', markersize=2, alpha=0.5) #ax.plot(nonbeaconed[:,0], nonbeaconed[:,1], 'o', color='LimeGreen', markersize=2, alpha=0.5) #ax.plot(probe[:,0], probe[:,1], 'o', color='LimeGreen', markersize=2, alpha=0.5) ax.plot(spike_data.loc[cluster_index].beaconed_position_cm, spike_data.loc[cluster_index].beaconed_trial_number, '|', color='Black', markersize=4) ax.plot(spike_data.loc[cluster_index].nonbeaconed_position_cm, spike_data.loc[cluster_index].nonbeaconed_trial_number, '|', color='Red', markersize=4) ax.plot(spike_data.loc[cluster_index].probe_position_cm, spike_data.loc[cluster_index].probe_trial_number, '|', color='Blue', markersize=4) ax.plot(rewarded_locations, rewarded_trials, '>', color='Red', markersize=3) plt.ylabel('Spikes on trials', fontsize=12, labelpad=10) plt.xlabel('Location (cm)', fontsize=12, labelpad=10) plt.xlim(0, 200) ax.yaxis.set_ticks_position('left') ax.xaxis.set_ticks_position('bottom') plot_utility.style_track_plot(ax, 200) plot_utility.style_vr_plot(ax, x_max) plt.locator_params(axis='y', nbins=4) try: plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0) except ValueError: continue plt.savefig(save_path + '/' + spike_data.session_id[cluster_index] + '_track_firing_Cluster_' + str(cluster_index + 1) + '.png', dpi=200) plt.close()
def freq_stack(): global nboot, RSqStore, stname, RFstack, RFstack_mean, RFstack_std from matplotlib import pylab as plt from stack_synthetics import generate_synthetics DeconMethod, highT, path2rf, outfile, baz1, baz2, SNR_min, \ nboot, Taup_Misfit_Min, Taup_Misfit_Max, average_type, allSyntheticParams = parse_inline_input() tmp = path2rf[0].split('/')[-1] #Manually change if you want to calc. goodness of fit CalculateGoodnessOfFit=False RSqStore={} #Dump information for the GRL publication if True: FileForPub = open('ForPub.txt', 'a') FileForPub.write('# \n') FileForPub.write('# %s \n' % tmp) FileForPub.write('# \n') #Prepare synthetics - set to true if a new database of synthetics needs to be generated (may take days!) if False: #for SyntheticParams in allSyntheticParams: # for RayParam in [0.09, 0.092, 0.094, 0.096, 0.098, 0.1, 0.102, 0.104, 0.106, 0.108, 0.11, 0.112, 0.114, 0.116, 0.118]: # generate_synthetics(SyntheticParams, RayParam) #Set up figture fig = plt.figure(1, figsize=(3.5, 1.75)) params = {'legend.fontsize': 4, 'figure.figsize': (3, 1.5), 'axes.labelsize': 5, 'axes.titlesize': 7, 'figure.titlesize': 7, 'figure.dpi': 500, 'xtick.labelsize': 5, 'ytick.labelsize': 5} plt.rcParams.update(params) #For the GRL paper, the goodness-of-fit calculation was carried out only at 16s # if CalculateGoodnessOfFit: ListOfPeriods = [16] else: ListOfPeriods = [20, 16, 12, 8, 4, 2] #Loop over periods and plot each RF # for isub, lowT in enumerate(ListOfPeriods): rect1 = [0.1, 0.15, 0.7, 0.8] rect2 = [0.82, 0.15, 0.1, 0.8] ax = fig.add_axes(rect1) ax4 = fig.add_axes(rect2) offset = isub if True: lenRFs, RPs, BAZIs, depths, NRFs = multi_station_stack(ax, ax4, path2rf, lowT, highT, offset=offset, label='%.0f-%.0f s' % (lowT, highT), baz1=baz1, baz2=baz2, SNR_min=SNR_min, Taup_Misfit_Min=Taup_Misfit_Min, Taup_Misfit_Max=Taup_Misfit_Max, average_type=average_type, DeconMethod=DeconMethod) frp=open('RP_BAZI/file.txt','w') for itmp,tmp in enumerate(RPs): frp.write('%f %f\n' % (RPs[itmp], BAZIs[itmp])) frp.close() if True: from numpy import histogram, arange, sum bin_edges = arange(0.09, 0.122, 0.002) Weights, bin_edges = histogram(RPs,bins=bin_edges,density=True) Weights = Weights/sum(Weights) rps = bin_edges[:-1] lines = [] labels = [] syns = [] for isyn in range(len(allSyntheticParams)): SyntheticParams = allSyntheticParams[isyn] SyntheticParams["DeconMethod"] = DeconMethod SyntheticParams["RayParams"] = rps SyntheticParams["Weights"] = Weights SyntheticParams["offset"] = offset SyntheticParams["nIter"] = 100 # for ITDD only SyntheticParams["lowT"] = lowT SyntheticParams["highT"] = highT #l0, RFsyn = add_synthetics(ax, SyntheticParams, Rescale=True) #lines.append(l0) #labels.append(SyntheticParams["label"]) #syns.append(RFsyn) if CalculateGoodnessOfFit: if lowT == 16.: for ii in range(len(depths)): from numpy import shape #print shape(depths), shape(RFstack), shape(RFstack_std), shape(syns) #FileForPub.write('%6.1f %e %e %e %e %e %e\n' % # (depths[ii], RFstack_mean[ii], RFstack_std[ii], syns[0][ii], syns[1][ii], syns[2][ii], syns[3][ii])) FileForPub.write('%6.1f %e %e ' % (depths[ii], RFstack_mean[ii], RFstack_std[ii])) for isyn in range(len(syns)): FileForPub.write('%e ' % (syns[isyn][ii])) FileForPub.write('\n') if False: if len(allSyntheticParams)>0: ax.legend(lines,labels) ax4.fill_betweenx(depths, NRFs, x2=0.0, facecolor='lightblue') ylims=[0., 310.] ax.set_ylim(ylims) ax4.set_ylim(ylims) ax.set_xlim([-1, 8]) ax.set_xticks([]) ax.set_ylabel('Depth (km)') ax.invert_yaxis() ax4.invert_yaxis() ax4.set_yticklabels([]) ax4.set_xlim(0, lenRFs*1.25) ax4.set_xticks([0, lenRFs]) ax4.set_xlabel('RFs used\nin stack') if False: plt.suptitle('%s\n%d Receiver Functions -- SNR > %.1f -- nboot = %d -- Average: %s -- %s' % ( path2rf, lenRFs, SNR_min, nboot, average_type, DeconMethod)) if True: left, bottom, width, height = [0.62, 0.50, 0.15, 0.15] ax2 = fig.add_axes([left, bottom, width, height]) (n, bins, patches) = ax2.hist(RPs) plt.annotate('Frequency', xy=(-0.25,0.5), xycoords='axes fraction', fontsize=3, rotation=90,va='center') plt.annotate('Ray Parameter (s/km)', xy=(0.5,-0.47), xycoords='axes fraction', fontsize=3,ha='center') plt.locator_params(axis='y', nbins=4) plt.locator_params(axis='x', nbins=5) plt.xlim([0.09, 0.120]) left, bottom, width, height = [0.62, 0.25, 0.15, 0.15] ax3 = fig.add_axes([left, bottom, width, height]) ax3.hist(BAZIs) plt.annotate('Frequency', xy=(-0.25,0.5), xycoords='axes fraction', fontsize=3, rotation=90, va='center') plt.annotate('Backazimuth (degrees)', xy=(0.5,-0.47), xycoords='axes fraction', fontsize=3, ha='center') plt.locator_params(axis='y', nbins=4) plt.locator_params(axis='x', nbins=5) plt.xlim([-180, 180]) plt.xticks([-180,-60,60,180]) for ax in [ax2, ax3]: for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(3) plt.setp(ax.xaxis.get_majorticklabels(), rotation=0) plt.setp(ax.yaxis.get_majorticklabels(), rotation=0) for tick in ax.yaxis.get_majorticklabels(): tick.set_x(+0.09) for tick in ax.xaxis.get_majorticklabels(): tick.set_y(+0.18) tick.set_rotation(45) plt.savefig(outfile) # # # if CalculateGoodnessOfFit: nsyn=len(allSyntheticParams) # fout=open('RSq_%s.out' % stname,'w') # fout.write('%d %d\n' % (nsyn,nboot)) for iboot in range(nboot): tmpa=iboot fout.write('%5d ' % tmpa) for isyn in range(1,nsyn+1): tmpb=RSqStore[(isyn, 16, iboot)] fout.write('%7e ' % tmpb) fout.write('\n') #tmpstr = '%5d %7e %7e %7e %7e\n' % (tmpa, tmpb, tmpc, tmpd, tmpe) #fout.write(tmpstr) fout.close() fout=open('Model_List.out','w') for isyn in range(nsyn): tmp=allSyntheticParams[isyn] tmpa,tmpb,tmpc=tmp["LABDepth"], tmp["LABdlnv"], tmp["LABTransitionThickness"] fout.write('%f %f %f\n' % (tmpa,tmpb,tmpc) ) fout.close() print 'Finished with %s ' % (outfile) return def add_synthetics(ax, syntheticParams, Rescale=False): from matplotlib import pylab as plt global RFstack, RFstack_mean, nboot, RSqStore, RFstack_std from TOOLS import stack_synthetics from scipy.stats import linregress depths, RF = stack_synthetics(syntheticParams) scale = 6.5 if syntheticParams["lowT"] > 10: scale = scale * 1.0 if syntheticParams["DeconMethod"] == 'ITDD': scale = scale * 2.0 refline = depths * 0. + syntheticParams["offset"] # plot everything wrt this line # Enhance deep from numpy import array RF = RF*scale RFstack_meanScl = RFstack_mean * scale RFstack_stdScl = RFstack_std * scale RFstackScl = RFstack * scale if Rescale: phi, _, _, _, _ = linregress(RF, RFstack_meanScl) print 'rescaling by factor %f' % (phi) RF = RF * phi tmp = RF xs = tmp+refline l0 = ax.plot(xs, depths, ls=syntheticParams["LineStyle"], color=syntheticParams["Color"], linewidth=0.5) for iboot in range(nboot): mask = (depths > 150.0) * (depths < 250.0) RSq = sum((RF - RFstackScl[iboot,:])**2 / (RFstack_stdScl)**2 * mask) RSq = RSq / sum(mask) #plt.figure(13) #plt.plot(RFstackScl[iboot,:] - RFstack_stdScl) #plt.plot(RFstackScl[iboot,:] + RFstack_stdScl) #plt.plot(RF,'--') #plt.show() key=(syntheticParams["isyn"],syntheticParams["lowT"],iboot) RSqStore[key]=RSq #print key, RSq return l0, RF / scale def multi_station_stack(ax, ax4, path2rf, lowT, highT, offset=0.0, label='', scale_bar=False, baz1=-999.0, baz2=999.0, SNR_min=0.0, Taup_Misfit_Min=-9999., Taup_Misfit_Max=9999., average_type='mean', DeconMethod='none'): """ This code performs the single-station stacking by reading in individual RFs from the .mat files. The name of the subroutine is accurate because multiple stations and or channels can be stacked together by making the list path2rf longer than one element. """ global RFstack_mean, RFstack, RFstack_std, nboot, stname from TOOLS import loadmat from numpy import zeros, isnan, std, mean, nanstd RFs_all = [] RPs_all = [] BAZIs_all = [] SNRs_all = [] for each_path2rf in path2rf: file_name = '%s/RF_Depth_%ds_%ds_%s_UsePostfilter_0.mat' % (each_path2rf, lowT, highT, DeconMethod) print '...loading %s' % (file_name) ###Open SNR file SNR = [] Taup_Misfit = [] snrfile = '%s/SNR_%ds_%ds.txt' % (each_path2rf, lowT, highT) file = open(snrfile) print "...%s snrfile loaded" % (snrfile) for line in file.readlines(): nfo = line.strip('\n').split() SNR.append(float(nfo[1])) Taup_Misfit.append(float(nfo[2])) file.close() ###Get matfile matfile = loadmat(file_name) print "...%s matfile loaded" % (file_name) RFs = matfile["rfs"][:, :] stdRF0 = nanstd(RFs, axis=0) BAZIs = matfile["BAZIsave"][:] RPs = matfile["RPsave"][:] depths = matfile["RF_Depth"][:, 0] meanRF0 = matfile["RF_Depth"][:, 1] # check for bad snrfile if len(SNR) != len(RFs): print '***Warning: len(SNR) != len(RFs) , %d , %d ' % (len(SNR), len(RFs)) dum = raw_input('Press enter to continue') stname = path2rf[0].split('/')[-1] #This is for printing data for bazi RFs if False: fout = open('%s_%02ds.txt' % (stname, lowT), 'w') for ii in range(len(RFs)): fout.write('%7f %7f \n' % (BAZIs[ii], SNR[ii])) tmp = RFs[ii] for jj in range(len(tmp)): fout.write(' %10e\n' % (tmp[jj])) fout.close() # cull out undesirables for ii in range(len(RFs)): if BAZIs[ii] >= baz1 and BAZIs[ii] <= baz2 and \ SNR[ii] >= SNR_min and \ Taup_Misfit[ii] >= Taup_Misfit_Min and Taup_Misfit[ii] <= Taup_Misfit_Max: # # check if RF deviates far from mean RF - Emily's method ndev = 0 for idep in range(len(depths)): if RFs[ii, idep] > meanRF0[idep] + 1.0 * stdRF0[idep] or \ RFs[ii, idep] < meanRF0[idep] - 1.0 * stdRF0[idep]: ndev = ndev + 1 determ = float(ndev) / float(len(depths)) if determ < 0.75: RFs_all.append(RFs[ii]) RPs_all.append(RPs[ii]) BAZIs_all.append(BAZIs[ii]) SNRs_all.append(SNR[ii]) ### Estimate spatial coherence #from TOOLS import estimate_spatial_coherence #station=path2rf[0].split('/')[-1] #Dmin, Dmax = 150.0, 250.0 #estimate_spatial_coherence(RFs_all, RPs_all, BAZIs_all, depths, Dmin, Dmax, # outfile='SPATCOH/spatcoh_%s_%ds_%dkm_%dkm.txt' % (station, lowT, Dmin, Dmax)) #fdump=open('out.datadump','w') # #fdump.write('%d\n' % len(RFs_all)) #for ii,tmp in enumerate(RFs_all): # header = '%10f %10f %10f\n' % (RPs_all[ii], BAZIs_all[ii], SNRs_all[ii]) # fdump.write(header) # for eachval in tmp: # fdump.write('%-10.3e ' % (eachval)) # # fdump.write('\n') # #fdump.close() #Dmin, Dmax = 80.0, 150.0 #estimate_spatial_coherence(RFs_all, RPs_all, BAZIs_all, depths, Dmin, Dmax, # outfile='SPATCOH/spatcoh_%s_%ds_%dkm_%dkm.txt' % (station, lowT, Dmin, Dmax)) #Dmin, Dmax = 150.0, 250.0 #estimate_spatial_coherence(RFs_all, RPs_all, BAZIs_all, depths, Dmin, Dmax, # outfile='SPATCOH/spatcoh_%s_%ds_%dkm_%dkm.txt' % (station, lowT, Dmin, Dmax)) ### Begin stacking and bootstrapping RFstack = zeros(nboot * len(RFs_all[0])).reshape(nboot, len(RFs_all[0])) NRFs = [] ## New section from numpy import nanmedian, shape, nanmean NRF, NDEP = shape(RFs_all) tmp=zeros(NRF*NDEP).reshape(NRF,NDEP) for ii in range(NRF): for jj in range(NDEP): tmp[ii,jj] = RFs_all[ii][jj] from numpy.random import randint for iboot in range(nboot): resamp=zeros(NRF*NDEP).reshape(NRF,NDEP) for ii in range(NRF): index=randint(0, NRF-1) resamp[ii,:]=tmp[index,:] if average_type == 'median': RFstack[iboot, :] = nanmedian(resamp,axis=0) elif average_type == 'mean': RFstack[iboot, :] = nanmean(resamp, axis=0) else: from sys import exit exit('Error: bad average type') for idep, depth in enumerate(depths): amp_at_dep = [] # list containing RF amps at depth for iRF in range(len(RFs_all)): if not isnan(RFs_all[iRF][idep]): amp_at_dep.append(RFs_all[iRF][idep]) # NRF = len(amp_at_dep) NRFs.append(NRF) # # for iboot in range(nboot): # if average_type == 'mean': # RFstack[iboot, idep] = mean(choice(amp_at_dep, NRF)) # elif average_type == 'median': # RFstack[iboot, idep] = median(choice(amp_at_dep, NRF)) # elif average_type == 'tmean': # tmp_m = mean(choice(amp_at_dep, NRF)) # tmp_std = std(choice(amp_at_dep, NRF)) # lwr = tmp_m - 2.0 * tmp_std # upr = tmp_m + 2.0 * tmp_std # tmp_tm = tmean(choice(amp_at_dep, NRF), limits=(lwr, upr)) # RFstack[iboot, idep] = tmp_tm RFstack_mean = mean(RFstack, axis=0) RFstack_std = std(RFstack, axis=0) refline = depths * 0. + offset # plot everything wrt this line scale = 6.0 if lowT > 10: scale = scale * 1.0 if DeconMethod == 'ITDD': scale = scale * 2.0 ax.plot(refline, depths, '--', color='black', linewidth=0.2) nsigma = 2.0 fmin = (RFstack_mean - nsigma * RFstack_std) * scale + refline fmax = (RFstack_mean + nsigma * RFstack_std) * scale + refline ax.fill_betweenx(depths, fmin, fmax, facecolor='gray', edgecolor='None') fmin = RFstack_mean * 0.0 + refline fmax = (RFstack_mean - nsigma * RFstack_std) * scale + refline ax.fill_betweenx(depths, fmin, fmax, where=fmax > fmin, facecolor='red', edgecolor='None') fmax = RFstack_mean * 0.0 + refline fmin = (RFstack_mean + nsigma * RFstack_std) * scale + refline ax.fill_betweenx(depths, fmin, fmax, where=fmax > fmin, facecolor='blue', edgecolor='None') ax.text(refline[0], 330, label, fontsize=5, horizontalalignment='center',rotation=30.0) #Scale Bar if True: ls=6.7 rs=ls+ scale * 0.1 ys=45. ax.plot([ls, rs], [ys, ys],linewidth=1, color='black') ax.text((ls+rs)/2.,ys-5.,'10% parent\namplitude', ha='center', weight='light', style='italic', fontsize=3.5) ax.annotate(stname, xy=(0.01,0.05) ,xycoords='axes fraction', weight='bold',fontsize=5) return len(RFs_all), RPs_all, BAZIs_all, depths, NRFs def parse_inline_input(): # These are the stacking params DeconMethod = raw_input('Deconvolution Method (ETMTM): \n').split()[0] highT = float(raw_input('High T in seconds (100): \n').split()[0]) numsta = int(raw_input('Number of channels (1): \n').split()[0]) path2rf=[] tmp = raw_input('Path for channels (separated by spaces): \n').split() for ista in range(numsta): path2rf.append(tmp[ista]) outfile = raw_input('Outfile: \n').split()[0] baz1, baz2 = raw_input('Baz1, Baz2: \n').split()[0:2] baz1,baz2 = float(baz1), float(baz2) SNR_min = float(raw_input('SNR Min: \n').split()[0]) nboot = int(raw_input('Nboot: \n').split()[0]) Taup_Misfit_Min, Taup_Misfit_Max = raw_input('Taup Misfit Min, Max: \n').split()[0:2] Taup_Misfit_Min, Taup_Misfit_Max = float(Taup_Misfit_Min), float(Taup_Misfit_Max) average_type = raw_input('Average Type (median): \n').split()[0] #Put synthetic params in a list of dictionary numsyn = int(raw_input('Number of synthetics (0): \n').split()[0]) allSyntheticParams=[] for isyn in range(numsyn): tmp={} a,b = raw_input('Moho: Depth (km), Fractional Velocity Jump \n').split()[0:2] tmp["MohoDepth"], tmp["Mohodlnv"] = float(a), float(b) a,b = raw_input('MLD: Depth (km), Fractional Velocity Jump \n').split()[0:2] tmp["MLDDepth"], tmp["MLDdlnv"] = float(a), float(b) a,b,c = raw_input('LAB: Depth (km), Fractional Velocity Jump \n').split()[0:3] print a,b,c tmp["LABDepth"], tmp["LABdlnv"], tmp["LABTransitionThickness"] = float(a), float(b), float(c) tmp["PulseWidth"] = float(raw_input('Pulse Width (s) \n').split()[0]) a,b = raw_input('Line Style and Color (- black): \n').split()[0:2] tmp['LineStyle']=a tmp['Color']=b tmp['label'] = raw_input('Line label: \n') tmp['propMat'] = True tmp['isyn'] = isyn + 1 #python counts from 0 allSyntheticParams.append(tmp) return DeconMethod, highT, path2rf, outfile, baz1, baz2,\ SNR_min, nboot, Taup_Misfit_Min, Taup_Misfit_Max, average_type, \ allSyntheticParams def write_depth_integrated_variance(RFstack_std, SNR_min): fout = open('depth_integrated_variance.txt', 'a') text = 'Depth-integrated variance, SNR_min = %f, %f\n' % (sum(RFstack_std), SNR_min) fout.write(text) fout.close() return def parse_user_input_old(): from sys import argv inputs = argv dummy = inputs.pop(0) DeconMethod = inputs.pop(0) # use popleft to take one arg at a time highT = float(inputs.pop(0)) # print inputs numsta = int(inputs.pop(0)) path2rf = [] for ista in range(numsta): path2rf.append(inputs.pop(0)) outfile = inputs.pop(0) baz1 = float(inputs.pop(0)) baz2 = float(inputs.pop(0)) SNR_min = float(inputs.pop(0)) nboot = int(inputs.pop(0)) Taup_Misfit_Min = float(inputs.pop(0)) Taup_Misfit_Max = float(inputs.pop(0)) average_type = inputs.pop(0) # print 'Saving to %s ' % (outfile) return DeconMethod, highT, path2rf, outfile, baz1, baz2, SNR_min, nboot, Taup_Misfit_Min, Taup_Misfit_Max, average_type
def draw_barchart(Time): df_frame = (df[df["date"].eq(Time)].sort_values( by="counts", ascending=True).tail(num_of_elements)) ax.clear() normal_colors = dict( zip(df["country"].unique(), rgb_colors_opacity)) dark_colors = dict(zip(df["country"].unique(), rgb_colors_dark)) ax.barh( df_frame["country"], df_frame["counts"], color=[normal_colors[x] for x in df_frame["country"]], height=0.8, edgecolor=([dark_colors[x] for x in df_frame["country"]]), linewidth="6", ) dx = float(df_frame["counts"].max()) / 200 for i, (value, name) in enumerate( zip(df_frame["counts"], df_frame["country"])): ax.text( value + dx, i + (num_of_elements / 50), " " + name, size=14, weight="bold", ha="left", va="center", fontdict={"fontname": "Trebuchet MS"}, ) ax.text( value + dx * 10, i - (num_of_elements / 50), f" {value:,.0f}", size=14, ha="left", va="center", ) time_unit_displayed = re.sub(r"\^(.*)", r"", str(Time)) ax.text( 1.0, 1.14, time_unit_displayed, transform=ax.transAxes, color="#666666", size=14, ha="right", weight="bold", fontdict={"fontname": "Trebuchet MS"}, ) # ax.text(-0.005, 1.06, 'Number of confirmed cases', transform=ax.transAxes, size=14, color='#666666') ax.text( -0.005, 1.14, "Number of {} cases ".format(field), transform=ax.transAxes, size=14, weight="bold", ha="left", fontdict={"fontname": "Trebuchet MS"}, ) ax.xaxis.set_major_formatter( ticker.StrMethodFormatter("{x:,.0f}")) ax.xaxis.set_ticks_position("top") ax.tick_params(axis="x", colors="#666666", labelsize=12) ax.set_yticks([]) ax.set_axisbelow(True) ax.margins(0, 0.01) ax.grid(which="major", axis="x", linestyle="-") plt.locator_params(axis="x", nbins=4) plt.box(False) plt.subplots_adjust( left=0.075, right=0.75, top=0.825, bottom=0.05, wspace=0.2, hspace=0.2, )
p = mets(met) p_met[nm] = p nm += 1 ########################################################################### tag = sys.argv[1].split('/')[-1].split('.')[0] print "Making the plots....." # Histograms of momentum plt.figure() plt.subplot(321) lch.hist_err(p_jets[p_jets>-999],bins=50,range=(0,400),fmt='o',markersize=5,color='black',ecolor='black') plt.title("%s: Jet momentum" % (tag)) plt.locator_params(nbins=6) #plt.xlabel("Momentum") plt.subplot(322) lch.hist_err(p_muons[p_muons>-999],bins=50,range=(0,300),fmt='o',markersize=5,color='red',ecolor='red') plt.title("%s: Muon momentum" % (tag)) #plt.xlabel("Momentum") plt.subplot(323) lch.hist_err(p_electrons[p_electrons>-999],bins=50,range=(0,200),fmt='o',markersize=5,color='green',ecolor='green') plt.title("%s: Electron momentum" % (tag)) #plt.xlabel("Momentum") plt.subplot(324) lch.hist_err(p_photons[p_photons>-999],bins=50,range=(0,200),fmt='o',markersize=5,color='orange',ecolor='orange') plt.title("%s: Photon momentum" % (tag))
c=colors.red, ls='dotted') plt.plot(xfit, pars[0][2] * polya(xfit, pars[0][0], pars[0][1], 1) / 10000.0, c=colors.red, ls='dashed') plt.plot(xfit, pars[0][3] * polya(xfit, pars[0][0], pars[0][1], 2) / 10000.0, c=colors.red, ls='-.') plt.ylabel(r'Counts$\times10^4$/ 2$\times10^{-4}$ a.u.', labelpad=15) plt.xlim(0, xmax) plt.ylim(0, 4) plt.tick_params(axis='x', which='both', labelbottom='off') plt.legend(loc='upper right', framealpha=0, fontsize=legendFS) plt.locator_params(axis='y', nticks=3) plt.yticks([0, 1, 2, 3, 4]) plt.sca(ax[1]) plt.errorbar(x[c], (n[c] / pFit3(x[c], pars[0])), yerr=(n[c] / pFit3(x[c], pars[0])) * np.sqrt((nerr[c] / n[c])**2 + (1.0 / pFit3(x[c], pars[0]))**2), marker='+', color=colors.red, linestyle='None') plt.axhline(1, c=colors.black, linestyle='dashed') plt.xlabel('Current [a.u.]') plt.ylabel(r'Ratio') plt.xlim(0, xmax) plt.ylim(0.85, 1.15) plt.yticks([0.9, 1, 1.1])