for k in out.keys(): if (k[:8] == 'PMTmatch') and\ (('PMT' + k[8:]) in pmtpa): out[k][i_piezo] = pmtpa['PMT' + k[8:]][ out['PMTmatch_ix'][i_piezo]] elif (k[:8] == 'PMTmatch') and\ (('pmt' + k[8:]) in pmtpa): out[k][i_piezo] = pmtpa['pmt' + k[8:]][ out['PMTmatch_ix'][i_piezo]] return out except Exception as e: return default_output if __name__ == "__main__": from SBCcode.DataHandling.GetSBCEvent import GetEvent as GE from SBCcode.DataHandling.ReadBinary import ReadBlock as RB runid = "20170928_5" ev = GE("/bluearc/storage/SBC-17-data/{runid}/".format(runid=runid), 22, "fastDAQ") ac = RB( "/pnfs/coupp/persistent/grid_output/SBC-17-T0Test3/output/{runid}/AcousticAnalysis_{runid}.bin" .format(runid=runid)) pmt = RB( "/pnfs/coupp/persistent/grid_output/SBC-17/output/{runid}/PMTpulseAnalysis_{runid}.bin" .format(runid=runid)) timing = TimingAnalysis(ev, aa=ac, pmtpa=pmt) pass
t0 = np.nan out["bubble_t0"][i_piezo] = t0 t0_index = closest_index(fastdaq_time, t0) out["piezoE"][i_piezo,:,:] = CalcPiezoE(raw_piezo - led_only, fastdaq_time, t_wins, f_bins, fastdaq_time[t0_index]) except: continue return out if __name__ == "__main__": import os from SBCcode.DataHandling.GetSBCEvent import GetEvent as GE events = {"20171006_3": [38], #"20170803_2": [24], #"20170928_5": [36] } raw_dir = "/bluearc/storage/SBC-17-data/" tau = 0.0038163479219674467 out = [] for k,v in events.items(): ev_path = os.path.join(raw_dir, k) for ev in v: ev_data = GE(ev_path, ev, "fastDAQ") out.append(AcousticAnalysis(ev_data, tau)) for x in out: print(x["bubble_t0"])
def run(data_directory, output_file, data_set = "SBC-17", start=0, n_events=5): ## Inputs: ## data_directory: Directory where the raw data is ## output_file: Location to store output binary file. ## data_set: String signifying which dataset we're using so we can filter out known 'bad-data'... ## start: Starts on event number 'start ## n_events: Number of events to process LOWER = 15000 UPPER = 40000 ## LOWER and UPPER are the frequency limits we place on the Spectogram #if os.path.isfile(output_file): # already_scanned = RB(output_file) full_stop = False with open(output_file, "w") as f: try: fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) # <-- Locks the file until f.__exit__() except BlockingIOError: print("Critical: File is currently locked by another user/process. Aborting.") return except: print("Unsupported on non-Linux/Mac OS's. Aborting.") return already_scanned = {"run_id": [], "tau": []} evs_processed = 0 all_ids = [i for i in os.listdir(data_directory) if os.path.isdir(os.path.join(data_directory, i)) and i != "scratch"] n_skip = 0 _counter = 0 for ev_id in all_ids: if data_set == "SBC-17": f_template = re.compile("^[0-9]{8}_[0-9]+") ## Until the year 10,000 this should work. . . f_match = re.match(f_template, ev_id) if f_match: d = int(ev_id.split("_")[0]) if d < 20170630: continue # Skips all data that happened before June 30 2017. all_evs = [i for i in os.listdir(os.path.join(data_directory, ev_id)) if os.path.isdir(os.path.join(data_directory, ev_id, i))] all_evs = sorted(all_evs, key=int) for ev in all_evs: if start > _counter: _counter += 1 continue if full_stop: #f.write(_last_out_str) print("Processed {} events ({}-{})".format(evs_processed-1, start, start + evs_processed-1)) return if evs_processed >= n_events: #f.write(_last_out_str) print("Processed {} events ({}-{})".format(evs_processed, start, start + evs_processed)) return if (ev_id, ev) in already_scanned["run_id"]: n_skip += 1 print("Skipping", (ev_id, ev), ". Previously scanned.") else: ## We grab the event and extract the Piezo traces print(os.path.join(RAW_DIRECTORY, ev_id, ev), end=" >>>>> ") ev_data = GE(os.path.join(RAW_DIRECTORY, ev_id), ev, "fastDAQ") try: piezo1 = ev_data["fastDAQ"]["Piezo1"] piezo2 = ev_data["fastDAQ"]["Piezo2"] except Exception as e: print("********************Exception! Fix this.********************") continue ## We grab the timebase from the event data and find the mean time interval timebase = ev_data["fastDAQ"]["time"] xextent = (min(timebase), max(timebase)) dt = np.mean(np.diff(timebase)) ## We start to create our plots and name them for future use fig, axarr = plt.subplots(4,2, figsize=(15,9), sharex=True) spectrum1_ax = axarr[0][0] spectrum2_ax = axarr[0][1] raw1_ax = axarr[1][0] raw2_ax = axarr[1][1] filtered1_ax = axarr[2][0] filtered2_ax = axarr[2][1] fit1_ax = axarr[3][0] fit2_ax = axarr[3][1] #conv1_ax = axarr[4][0] #conv2_ax = axarr[4][1] statistics1_axes = axarr[-1][0] statistics2_axes = axarr[-1][1] ## Create the specgrams and extract useful information such as the spectrum (Pxx), the frequency bins, the time ## interval bins, and the matplotlib image object spectrum1, freqs1, bins1, im1 = spectrum1_ax.specgram(piezo1, Fs=1./dt, NFFT=256, xextent=xextent, mode="magnitude") spectrum2, freqs2, bins2, im2 = spectrum2_ax.specgram(piezo2, Fs=1./dt, NFFT=256, xextent=xextent, mode="magnitude") spectrum1_ax.set_title("Piezo1 -- Disabled since we only use 2 for now") spectrum2_ax.set_title("Piezo2") spectrum1_ax.set_ylim(extend_window([LOWER, UPPER], 0.05)) spectrum2_ax.set_ylim(extend_window([LOWER, UPPER], 0.05)) ## Plot the raw signal and set some labels/other aesthetic choices raw1_ax.plot(timebase, piezo1) raw2_ax.plot(timebase, piezo2) ## Now we start our signal filtering! ## First step is to find the indices of all the frequencies we are interested in. These are ## the frequencies between LOWER and UPPER defined above. good_indices1 = _bandpass(freqs1, LOWER, UPPER) good_indices2 = _bandpass(freqs2, LOWER, UPPER) ## Now we go through and calculate the sums of the fft power spectrum for each time, only counting the ## frequencies we are interested in N1 = len(bins1) N2 = len(bins2) # In theory N1=N2 because of the sample rate of the DAQ but rather safe than sorry. spectrum1_sums = _spectrum_sums(spectrum1, good_indices1, N1) spectrum2_sums = _spectrum_sums(spectrum2, good_indices2, N2) ## Now we apply a median filter to the data to attempt to remove some noise. spectrum1_sums = scipy.signal.medfilt(spectrum1_sums) spectrum2_sums = scipy.signal.medfilt(spectrum2_sums) ## Rescale the time window so we can plot it appropriately. rescaled_time1 = rescale_window(xextent, bins1) rescaled_time2 = rescale_window(xextent, bins2) ## Plot the spectrum sums. filtered1_ax.plot(rescaled_time1, spectrum1_sums) filtered2_ax.plot(rescaled_time2, spectrum2_sums) ## Extract t0 from previous analysis reco_data = RB(os.path.join(RECO_DIRECTORY, "{}/AcousticAnalysis_{}.bin".format(ev_id, ev_id))) old_t0 = sum(reco_data["bubble_t0"][int(ev)])/2 ## Attempt to independently determine t0 from the filtered spectrum sum new_t0_1 = t0_finder(spectrum1_sums, rescaled_time1) new_t0_2 = t0_finder(spectrum2_sums, rescaled_time2) ## Exponential fit # scipy.optimize.curve_fit(lambda t,a,b: a*numpy.exp(b*t), x, y) fitx1, fity1, a1, b1 = exponential_fit(rescaled_time1, spectrum1_sums, new_t0_1) fitx2, fity2, a2, b2 = exponential_fit(rescaled_time2, spectrum2_sums, new_t0_2) filtered1_ax.axvspan(fitx1[0], fitx1[-1], facecolor="g", alpha=0.5) filtered2_ax.axvspan(fitx2[0], fitx2[-1], facecolor="g", alpha=0.5) filtered1_ax.plot(fitx1, fity1, "r-", linewidth=3) filtered2_ax.plot(fitx2, fity2, "r-", linewidth=3) fit1_ax.plot(fitx1, fity1,"r-", linewidth=3) fit2_ax.plot(fitx2, fity2, "r-", linewidth=3) fit1_ax.plot(rescaled_time1, spectrum1_sums) fit2_ax.plot(rescaled_time2, spectrum2_sums) fit1_ax.set_xlim([fitx1[0], fitx1[-1]]) fit2_ax.set_xlim([fitx2[0], fitx2[-1]]) ## Plot previous_t0 on all of the axes for ax in axarr.flatten()[:-2]: #for ax in inner: #ax.set_xlim(extend_window([old_t0*(1+0.03), old_t0*(1-0.03)], 0.1)) ax.set_xlim(xextent) ax.axvline(x=old_t0, color="k", linewidth=1) ## Show some statistics in the final plot statistics1_axes.text(0.5, 0.5, "Old t0: {}\nNew t0: {}\n% Diff: {:.5f}%\nTau: {}".\ format(old_t0, new_t0_1, 100*abs((old_t0 - new_t0_1) / old_t0), 1. / b1), fontsize=12, transform=statistics1_axes.transAxes, horizontalalignment="center", verticalalignment="center") remove_ticks(spectrum2_ax) remove_ticks(raw1_ax) remove_ticks(raw2_ax) remove_ticks(filtered1_ax) remove_ticks(filtered2_ax) remove_ticks(fit1_ax) remove_ticks(fit2_ax) #remove_ticks(conv1_ax) #remove_ticks(conv2_ax) remove_ticks(statistics1_axes) remove_ticks(statistics2_axes) plt.subplots_adjust(bottom=0.2, hspace=0, wspace=0) statistics2_axes.text(0.5, 0.5, "Old t0: {}\nNew t0: {}\n% Diff: {:.5f}%\nTau: {}". \ format(old_t0, new_t0_2, 100 * abs((old_t0 - new_t0_2) / old_t0), 1. / b2), fontsize=12, transform=statistics2_axes.transAxes, horizontalalignment="center", verticalalignment="center") actions = FitCheckButtonActions(event_id=ev_id, ev=ev, tau=1./b2, out_file_obj=f) g_fit = plt.axes([0.543, 0.05, 0.357, 0.15]) g_but = Button(g_fit, "Piezo2\nGood Fit", hovercolor="green") g_but.label.set_fontsize(45) g_but.on_clicked(actions.good_fit) b_fit = plt.axes([0.125,0.05,0.359,0.15]) b_but = Button(b_fit, "Piezo2\nBad Fit", hovercolor="red") b_but.label.set_fontsize(45) b_but.on_clicked(actions.bad_fit) close_fit = plt.axes([0.91, 0.05, .08, 0.15]) close_button = Button(close_fit, "Stop\nall", hovercolor="red") close_button.label.set_fontsize(14) close_button.on_clicked(actions.stop) ## Title the figure fig.suptitle("Run: {} --- Event: {}".format(ev_id, ev)) win = plt.gcf().canvas.manager.window if PyQt4import: win.setWindowFlags(win.windowFlags() | QtCore.Qt.CustomizeWindowHint) win.setWindowFlags(win.windowFlags() & ~QtCore.Qt.WindowCloseButtonHint) plt.show() full_stop = actions.full_stop evs_processed += 1 return # <-- This return shouldn't be reached since our exit condition is above
def ProcessSingleRun2(rundir, dataset='SBC-2017', recondir='.', process_list=None): # Inputs: # rundir: Location of raw data # dataset: Indicator used for filtering which analyses to run # recondir: Location of recon data/where we want to output our binary files # process_list: List of analyses modules to run. example: ["acoustic", "event", "images"] # Outputs: Nothing. Saves binary files to recondir. if process_list is None: process_list = [ ] # This is needed since lists are mutable objects. If you have a default argument # as a mutable object, then the default argument can *change* across multiple # function calls since the argument is created ONCE when the function is defined. runname = os.path.basename(rundir) runid_str = runname.split('_') runid = np.int32(runid_str) #run_recondir = os.path.join(recondir, runname) run_recondir = recondir if not os.path.isdir(run_recondir): os.mkdir(run_recondir) if dataset == 'SBC-2017': loadlist = [] for process in process_list: if any(process.lower().strip() == x for x in ['dytran', 'acoustic']): loadlist.append('fastDAQ') loadlist = list(set(loadlist)) else: loadlist = ['~'] acoustic_out = [] acoustic_default = calculate_t0(None, None, None, None) process_list = [p.lower().strip() for p in process_list] print("Starting run " + rundir) eventlist = BuildEventList(rundir) for ev in eventlist: t0 = time.time() print('Starting event ' + runname + '/' + str(ev)) npev = np.array([ev], dtype=np.int32) thisevent = GE(rundir, ev, *loadlist) print('Time to load event: '.rjust(35) + str(time.time() - t0) + ' seconds') if "acoustic" in process_list: # Acoustic analysis t1 = time.time() if dataset == 'SBC-2017': tau_peak = 0.0025884277467056165 # <-- This comes from TauResultAnalysis.py (J.G.) tau_average = 0.0038163479219674467 # <-- This also ^^ lower_f = 20000 upper_f = 40000 piezo1_fit_type = 0 piezo2_fit_type = 0 # See documentation of corr_t0_finder for desc of fit_types try: acoustic_out.append( calculate_t0(data=thisevent, tau=tau_average, piezo1_fit_type=piezo1_fit_type, piezo2_fit_type=piezo2_fit_type, lower=lower_f, upper=upper_f, view_plots=False)) except: acoustic_out.append(copy.deepcopy(acoustic_default)) acoustic_out[-1]['runid'] = runid acoustic_out[-1]['ev'] = npev et = time.time() - t1 print('Acoustic analysis: '.rjust(35) + str(et) + ' seconds') print('*** Full event analysis *** '.rjust(35) + str(time.time() - t0) + ' seconds') if "acoustic" in process_list: WB(os.path.join(run_recondir, 'AcousticAnalysis_' + runname + '.bin'), acoustic_out, rowdef=1, initialkeys=['runid', 'ev'], drop_first_dim=False) return
def update(self): # Method to update figure with new plots of selected event data. if self.lastind is None: return dataind = self.lastind # clear all plots except position vs time lag axes[1, 0].cla() axes[1, 1].cla() axes[2, 0].cla() axes[2, 1].cla() axes[3, 0].cla() axes[3, 1].cla() axes[1, 0].set_title('Piezo 1 trace. Red line is acoustic t0') axes[1, 1].set_title('Piezo 2 trace. Red line is acoustic t0') axes[2, 0].set_title( 'Zoomed in on t0. Blue lines are close PMT triggers, purple is selected' ) axes[2, 1].set_title( 'Zoomed in on t0. Blue lines are close PMT triggers, purple is selected' ) axes[3, 0].set_title( 'Selected PMT trace. Press R for next trigger in time, L for previous' ) axes[3, 1].set_title( 'Selected PMT trace. Press R for next trigger in time, L for previous' ) # Get the fastDAQ data of the selected event point. Saves the piezo traces. # Get the pmt trace data of the selected event point. Find the pmt triggers around the acoustic t0 and plot them # as lines in the zoomed in Piezo trace if self.new_event == True: self.ev_data = GE(os.path.join(RAW_DIRECTORY, self.event_id), self.ev_number, "fastDAQ") self.piezo1 = self.ev_data["fastDAQ"]["Piezo1"] self.piezo2 = self.ev_data["fastDAQ"]["Piezo2"] self.timebase = self.ev_data["fastDAQ"]["time"] self.pmt_data = GE(os.path.join(RAW_DIRECTORY, self.event_id), self.ev_number, "PMTtraces") self.pmt_times = (self.pmt_data['PMTtraces']['t0_sec'] + self.pmt_data['PMTtraces']['t0_frac'] ) - align_times[self.lastind] self.close_pmt_times1 = ( self.pmt_times - self.bubble_time1 < .001) & (self.pmt_times - self.bubble_time1 > -.001) self.close_pmt_times2 = ( self.pmt_times - self.bubble_time2 < .001) & (self.pmt_times - self.bubble_time2 > -.001) self.pmt_index = 0 for pmt in self.pmt_times[self.close_pmt_times1]: axes[2, 0].axvline(x=pmt, color='b') for pmt in self.pmt_times[self.close_pmt_times2]: axes[2, 1].axvline(x=pmt, color='b') # Plot the selected PMT trace in the bottom row plots. Assuming the matched PMT trigger is the same for both # Piezo traces, and the acoustic t0s are not significantly different between the Piezos, the plots will have # the same trace. If the index variable for the triggers close to the acoustic t0 is greater than the number # of plotted triggers, the try block will fail and the except block will reset the index to zero and start over. # Whichever pmt trigger is being read out will be yellow on the zoomed in Piezo trace plot. try: trigger_indexes1 = np.where(self.close_pmt_times1 == True)[0] traces1 = self.pmt_data['PMTtraces']['traces'][ trigger_indexes1[self.pmt_index], 0, :] dt1 = self.pmt_data['PMTtraces']['dt'][ trigger_indexes1[self.pmt_index], 0] v_scale1 = self.pmt_data['PMTtraces']['v_scale'][ trigger_indexes1[self.pmt_index], 0] v_offset1 = self.pmt_data['PMTtraces']['v_offset'][ trigger_indexes1[self.pmt_index], 0] xd1 = np.arange( self.pmt_data['PMTtraces']['traces'].shape[2]) * dt1 yd_fine1 = traces1 * v_scale1 \ + v_offset1 axes[2, 0].axvline( x=self.pmt_times[self.close_pmt_times1][self.pmt_index], color='m') axes[3, 0].plot(xd1, yd_fine1) trigger_indexes2 = np.where(self.close_pmt_times2 == True)[0] traces2 = self.pmt_data['PMTtraces']['traces'][ trigger_indexes2[self.pmt_index], 0, :] dt2 = self.pmt_data['PMTtraces']['dt'][ trigger_indexes2[self.pmt_index], 0] v_scale2 = self.pmt_data['PMTtraces']['v_scale'][ trigger_indexes2[self.pmt_index], 0] v_offset2 = self.pmt_data['PMTtraces']['v_offset'][ trigger_indexes2[self.pmt_index], 0] xd2 = np.arange( self.pmt_data['PMTtraces']['traces'].shape[2]) * dt2 yd_fine2 = traces2 * v_scale2 \ + v_offset2 axes[2, 1].axvline( x=self.pmt_times[self.close_pmt_times2][self.pmt_index], color='m') axes[3, 1].plot(xd2, yd_fine2) except IndexError: try: self.pmt_index = 0 trigger_indexes1 = np.where(self.close_pmt_times1 == True)[0] traces1 = self.pmt_data['PMTtraces']['traces'][ trigger_indexes1[self.pmt_index], 0, :] dt1 = self.pmt_data['PMTtraces']['dt'][ trigger_indexes1[self.pmt_index], 0] v_scale1 = self.pmt_data['PMTtraces']['v_scale'][ trigger_indexes1[self.pmt_index], 0] v_offset1 = self.pmt_data['PMTtraces']['v_offset'][ trigger_indexes1[self.pmt_index], 0] xd1 = np.arange( self.pmt_data['PMTtraces']['traces'].shape[2]) * dt1 yd_fine1 = traces1 * v_scale1 \ + v_offset1 axes[2, 0].axvline( x=self.pmt_times[self.close_pmt_times1][self.pmt_index], color='m') axes[3, 0].plot(xd1, yd_fine1) trigger_indexes2 = np.where(self.close_pmt_times2 == True)[0] traces2 = self.pmt_data['PMTtraces']['traces'][ trigger_indexes2[self.pmt_index], 0, :] dt2 = self.pmt_data['PMTtraces']['dt'][ trigger_indexes2[self.pmt_index], 0] v_scale2 = self.pmt_data['PMTtraces']['v_scale'][ trigger_indexes2[self.pmt_index], 0] v_offset2 = self.pmt_data['PMTtraces']['v_offset'][ trigger_indexes2[self.pmt_index], 0] xd2 = np.arange( self.pmt_data['PMTtraces']['traces'].shape[2]) * dt2 yd_fine2 = traces2 * v_scale2 \ + v_offset2 axes[2, 1].axvline( x=self.pmt_times[self.close_pmt_times2][self.pmt_index], color='mnn') axes[3, 1].plot(xd2, yd_fine2) except IndexError: print('hello') # Plot the acoustic t0 on the zoomed in Piezo trace. Then plot the piezo traces and the filtered traces, and plot # the filtered traces zoomed in around the acoustic t0. # axes[1,0].axvline(x=self.bubble_time1, color="r") # axes[1,1].axvline(x=self.bubble_time2, color="r") axes[1, 0].axvline(x=self.bubble_time1, color="r") axes[1, 1].axvline(x=self.bubble_time2, color="r") axes[1, 0].plot(self.timebase, self.piezo1) filter_d = np.exp(-.1) axes[1, 0].plot( self.timebase, sig.lfilter( np.float64([1]) - filter_d, np.float64([1, -filter_d]), self.piezo1), 'g') axes[1, 1].plot(self.timebase, self.piezo2) filter_d = np.exp(-.1) axes[1, 1].plot( self.timebase, sig.lfilter( np.float64([1]) - filter_d, np.float64([1, -filter_d]), self.piezo2), 'g') # Shows the event run id, event number, run type, and fourth and sixth pressure tranducer measurements axes[2, 0].text(0.05, 0.9, 'event_id={}\nevent={}'.format(self.event_id, self.ev_number), transform=axes[2, 0].transAxes, va='top') axes[2, 1].text(0.05, 0.9, 'run_type={}\npressure3={}\npressure5={}'.format( self.run_type, self.pressure3, self.pressure5), transform=axes[2, 1].transAxes, va='top') axes[2, 0].plot( self.timebase, sig.lfilter( np.float64([1]) - filter_d, np.float64([1, -filter_d]), self.piezo1), 'g') # axes[2,0].set_xlim(self.bubble_time1-.0005,self.bubble_time1+.0005) axes[2, 0].set_xlim(self.bubble_time1 - .0005, self.bubble_time1 + .0005) # axes[2,0].axvline(x=self.bubble_time1, color="r") axes[2, 0].axvline(x=self.bubble_time1, color="r") axes[2, 1].plot( self.timebase, sig.lfilter( np.float64([1]) - filter_d, np.float64([1, -filter_d]), self.piezo2), 'g') # axes[2, 1].set_xlim(self.bubble_time2 - .0005, self.bubble_time2 + .0005) axes[2, 1].set_xlim(self.bubble_time2 - .0005, self.bubble_time2 + .0005) # axes[2, 1].axvline(x=self.bubble_time2, color="r") axes[2, 1].axvline(x=self.bubble_time2, color="r") # Make the yellow marker for the new selected data points visible # ax2.set_ylim(-3, 3) self.selected1.set_visible(True) self.selected2.set_visible(True) self.selected1.set_data(xs1[dataind], ys1[dataind]) self.selected2.set_data(xs2[dataind], ys2[dataind]) # self.text.set_text('selected: %d' % dataind) fig.canvas.draw()
# print("{}: {}".format(k, v)) runs = get_runs("/bluearc/storage/SBC-17-data/") runs = trim_runlist(runs, start="20171006_0", stop="20171008_0") for run in runs: if run == "20171006_3": evoi = 38 elif run == "20171007_6": evoi = 3 else: continue print() print(run) ev_path = os.path.join("/bluearc/storage/SBC-17-data/", run) data = GE(ev_path, evoi, "fastDAQ") calculate_t0(data, 0.0025884277, piezo1_fit_type=0, piezo2_fit_type=0, lower=20000, upper=40000, view_plots=True) #ProcessSingleRun2(ev_path, recondir="/pnfs/coupp/persistent/grid_output/SBC-17/T0Test/", # process_list=["acoustic"]) # t1 = time.time() # event_data = GE(ev_path, str(), "fastDAQ") # t2 = time.time() # print("\tTook {}s to load event.".format(t2-t1)) # d = calculate_t0(data = event_data, # tau = 0.0025884277,
import matplotlib.pyplot as plt import numpy as np import gc from SBCcode.DataHandling.ReadBinary import ReadBlock as RB from SBCcode.DataHandling.GetSBCEvent import GetEvent as GE runid = "20170928_0" events = [18, 19, 20, 21, 22, 23, 24, 25, 26] acoustic_data = RB( "/pnfs/coupp/persistent/grid_output/SBC-17-T0Test3/output/{runid}/AcousticAnalysis_{runid}.bin" .format(runid=runid)) fig, ax = plt.subplots(nrows=3, ncols=3) ax = ax.flatten() for ev in range(len(events)): gc.collect() evdata = GE("/bluearc/storage/SBC-17-data/{runid}/".format(runid=runid), events[ev], "fastDAQ") time = evdata["fastDAQ"]["time"] piezo = evdata["fastDAQ"]["Piezo2"] ax[ev].plot(time, piezo, color="b", zorder=1) t0 = acoustic_data["bubble_t0"][events[ev]][1] if not np.isnan(t0): plt.axvline(t0, color="m", linewidth=3, zorder=3) #ax[ev].set_xlim(t0-0.) plt.show()
corr1 = np.correlate(sp1_sums, corr1_signaly, "same") corr2 = np.correlate(sp2_sums, corr2_signaly, "same") t2 = time.time() print("\tCorr took {}s".format(t2-t1)) corr1t = rescaled_t1 - 0.5 * corr_n1 * corr_dt1 corr2t = rescaled_t2 - 0.5 * corr_n2 * corr_dt2 new_t01 = find_t0_from_corr(corr1t, corr1) new_t02 = find_t0_from_corr(corr2t, corr2) return new_t01, new_t02 if __name__ == "__main__": TAU_PEAK = 0.0025884277467056165 # <-- This comes from TauResultAnalysis.py TAU_AVERAGE = 0.0038163479219674467 # <-- This also ^^ RAW_DIRECTORY = "/bluearc/storage/SBC-17-data/" RECO_DIRECTORY = "/pnfs/coupp/persistent/grid_output/SBC-17/output" VIEW_PLOTS = True plt.ioff() for i in range(1): data = GE(os.path.join(RAW_DIRECTORY, "20170928_5"), 36, "fastDAQ") t0 = time.time() print(run(data, TAU_AVERAGE, VIEW_PLOTS)) t1 = time.time() print("Event: {}. Finding t0 took {} seconds".format(i, t1-t0)) #run(RAW_DIRECTORY, "20170630_0", "0", TAU_AVERAGE, VIEW_PLOTS) #run(RAW_DIRECTORY, "20170627_0", "3", TAU_AVERAGE, VIEW_PLOTS)
import matplotlib.pyplot as plt import numpy as np import scipy.signal import scipy from SBCcode.DataHandling.GetSBCEvent import GetEvent as GE from SBCcode.DataHandling.ReadBinary import ReadBlock as RB if __name__ == "__main__": runid = "20170709_8" event = 84 raw = "/bluearc/storage/SBC-17-data/" rec = "/pnfs/coupp/persistent/grid_output/SBC-17-T0Test3/output" a = GE(os.path.join(raw, runid), event, "fastDAQ", "PMTtraces") acoustic = RB( os.path.join(rec, runid, "AcousticAnalysis_{runid}.bin".format(runid=runid))) print("fastDAQ") print("\t", a["fastDAQ"].keys()) print("\nPMTtraces") print("\t", a["PMTtraces"].keys()) print("\nAcoustic") print("\t", acoustic.keys()) # piezo and led #plt.plot(a["fastDAQ"]["time"], a["fastDAQ"]["Piezo1"]) print("Getting t0 for: {} -- {}".format(acoustic["runid"][event], acoustic["ev"][event])) t0 = 1e3 * acoustic["bubble_t0"][event][1]
def run(data_directory, output_file, data_set="SBC-17", start=0, n_events=5): ## Want to go through events and show the raw piezo trace, then click to select a t0. ## Display the t0, ask for confirmation, and then save to an output file. full_stop = False with open(output_file, "w") as f: try: fcntl.flock( f, fcntl.LOCK_EX | fcntl.LOCK_NB) # <-- Locks the file until f.__exit__() except BlockingIOError: print( "Critical: File is currently locked by another user/process. Aborting." ) return except: print("Unsupported on non-Linux/Mac OS's. Aborting.") return evs_processed = 0 all_ids = [ i for i in os.listdir(data_directory) if os.path.isdir(os.path.join(data_directory, i)) and i != "scratch" ] n_skip = 0 _counter = 0 for ev_id in all_ids: if data_set == "SBC-17": f_template = re.compile( "^[0-9]{8}_[0-9]+" ) ## Until the year 10,000 this should work. . . f_match = re.match(f_template, ev_id) if f_match: d = int(ev_id.split("_")[0]) if d < 20170630: continue # Skips all data that happened before June 30 2017. all_evs = [ i for i in os.listdir(os.path.join(data_directory, ev_id)) if os.path.isdir(os.path.join(data_directory, ev_id, i)) ] all_evs = sorted(all_evs, key=int) for ev in all_evs: if start > _counter: _counter += 1 continue if full_stop: # f.write(_last_out_str) print("Processed {} events ({}-{})".format( evs_processed - 1, start, start + evs_processed - 1)) return if evs_processed >= n_events: # f.write(_last_out_str) print("Processed {} events ({}-{})".format( evs_processed, start, start + evs_processed)) return else: ## We grab the event and extract the Piezo traces print(os.path.join(RAW_DIRECTORY, ev_id, ev), end=" >>>>> ") ev_data = GE(os.path.join(RAW_DIRECTORY, ev_id), ev, "fastDAQ") try: piezo2 = ev_data["fastDAQ"]["Piezo2"] except Exception as e: print( "********************Exception! Fix this.********************" ) continue ## We grab the timebase from the event data and find the mean time interval timebase = ev_data["fastDAQ"]["time"] xextent = (min(timebase), max(timebase)) dt = np.mean(np.diff(timebase)) plt.plot(timebase, piezo2) drawing_ax = plt.gca() plt.ioff() actions = mplActions(timebase, piezo2, ev_id, ev, f, drawing_ax) cid = plt.gcf().canvas.mpl_connect( 'button_press_event', actions.on_click) g_fit = plt.axes([0.543, 0.05, 0.357, 0.15]) g_but = Button(g_fit, "Confirm", hovercolor="green") g_but.label.set_fontsize(45) g_but.on_clicked(actions.confirm) b_fit = plt.axes([0.125, 0.05, 0.359, 0.15]) b_but = Button(b_fit, "Skip", hovercolor="red") b_but.label.set_fontsize(45) b_but.on_clicked(actions.skip) close_fit = plt.axes([0.91, 0.05, .08, 0.15]) close_button = Button(close_fit, "Stop\nall", hovercolor="red") close_button.label.set_fontsize(14) close_button.on_clicked(actions.stop) plt.show() full_stop = actions.full_stop evs_processed += 1 return