def update_baselines(self, method): """ Handle the baselines popup menu """ from ProcessingAlgorithms.SignalExtraction.baselines import baselines_by_squash blines = [] self.baselines = [] # remove any existing baselines if method == "Squash": peaks, sigs, heights = baselines_by_squash(self.spectrogram) blines.extend(peaks) # for n in range(len(heights)): # if heights[n] > 0.1: # blines.append(peaks[n]) # Now show the baselines in blines or remove any # if blines is empty if not blines: for b in self.baselines: self.axSpectrum.lines.remove(b['line']) self.baselines = [] # remove them else: edges = (self.spectrogram.intensity.min(), self.spectrogram.intensity.max()) for v in blines: bline = self.axSpectrum.plot([edges[0], edges[1]], [v, v], 'k-', alpha=0.4) self.baselines.append(dict(v=v, line=bline))
def saveBaselineIntensityImages(files, saveLoc: str = None, imageExt: str = "png"): """ Save a image file of the baseline as a function of time to the folder specified by saveLoc. """ if saveLoc == None: saveLoc = r"../baselineIntensityMaps" for i in tqdm.trange(len(files)): filename = f"../dig/{files[i]}" MySpect = Spectrogram(filename) peaks, _, heights = baselines_by_squash(MySpect) plt.plot( np.array(MySpect.time) * 1e6, MySpect.intensity[MySpect._velocity_to_index(peaks[0])]) plt.xlabel("Time ($\mu$s)") plt.ylabel("Intensity (db)") plt.title(f"{MySpect.data.filename}") path = os.path.join( saveLoc, files[i].replace(".dig", "") + f"_baselineIntensity.{imageExt}") if not os.path.exists(os.path.split(path)[0]): os.makedirs(os.path.split(path)[0]) plt.savefig(path) # Reset the state for the next data file. plt.clf() del MySpect
def mask_baselines(self): peaks, _, _ = baselines_by_squash(self.spectrogram) minimum = np.min(self.spectrogram.intensity) for peak in peaks: velo_index = self.spectrogram._velocity_to_index(peak) velo_index = velo_index - 20 for i in range(velo_index, velo_index + 40, 1): self.matrixToMatch[i][:] = minimum
def estimateStartTime(self): """ Compute an approximate value for the jump off time based upon the change in the baseline intensity. """ from ProcessingAlgorithms.SignalExtraction.baselines import baselines_by_squash from ProcessingAlgorithms.SignalExtraction.baselineTracking import baselineTracking peaks, _, _ = baselines_by_squash(self) self.estimatedStartTime_ = baselineTracking( self, peaks[0], 0.024761904761904763)
def analyze_noise(self, **kwargs): """ Analyze the noise by performing a double-exponential fit. Possible kwargs: v_min (baseline) v_max (5000) t_min (0) t_max (probe destruction) n_bins (100) Returns: DoubleExponential fit object, which includes fields beta, lam1, lam2, amp, mean, stdev, chisq, and prob_greater """ from ProcessingAlgorithms.Fitting.fit import DoubleExponential, Exponential from ProcessingAlgorithms.SignalExtraction.baselines import baselines_by_squash v_min = kwargs.get('v_min') if not v_min: peaks, _, hts = baselines_by_squash(self) v_min = peaks[0] v_max = kwargs.get('v_max', 5000) t_min = kwargs.get('t_min', self.t_start) t_max = kwargs.get('t_max', self.vertical_spike()) if not t_max: t_max = self.t_end n_bins = kwargs.get('n_bins', 100) # extract the region and convert to power my_slice = self.power(self.slice((t_min, t_max), (v_min, v_max))[2]) # set the cutoff for the maximum intensity we consider by # looking in histogram_levels cutoff = self.power(self.histogram_levels['ones'][1]) # construct the bins bins = np.linspace(0, cutoff, n_bins) histo, edges = np.histogram(my_slice, bins=bins, density=False) dub = DoubleExponential(edges[2:-1], histo[2:]) if dub.error: dub = Exponential(edges[2:-1], histo[2:]) dub.v_min = v_min dub.v_max = v_max dub.t_min = t_min dub.t_max = t_max dub.n_bins = n_bins return dub
def runAgainstTestSet(testSetFilename, guessAtOptimalThres, guessAtOptimalSkipUntil, errorPower: int = 1): data = pd.read_excel(testSetFilename) # Ignore the samples that we do not have the full data for. data = data.dropna() data.reset_index() # Reset so that it can be easily indexed. files = data["Filename"].to_list() bestGuessTimes = (data[data.columns[1]] * 1e6).to_list() # This will be the same times for the probe destruction dataset. It is generally ignored currently. bestGuessVels = data[data.columns[-1]].to_list() d = {"Filename": files} d["ProbeDestructionEstimateTime"] = np.zeros(len(files)) d["Error Versus Label"] = np.zeros(len(files)) for i in tqdm.trange(len(files)): filename = os.path.join(os.path.join("..", "dig"), f"{files[i]}") MySpect = Spectrogram(filename, overlap=0) peaks, _, heights = baselines_by_squash(MySpect, min_percent=0.75) timeEstimate = baselineTracking(MySpect, peaks[0], guessAtOptimalThres, guessAtOptimalSkipUntil) d["ProbeDestructionEstimateTime"][i] = timeEstimate d["Error Versus Label"][i] = np.power(bestGuessTimes[i] - timeEstimate, errorPower) errors = d["Error Versus Label"] print(f"The statistics for the test set specified by {testSetFilename}\n") print( f"Avg: {np.mean(errors)} Std: {np.std(errors)} Median: {np.median(errors)} Max: {np.max(errors)} Min: {np.min(errors)}" ) return d
def runExperiment(trainingFilePath, thresholds: list, skipUntilTimes: list = [], cmap: str = DEFMAP, errorPower: int = 1): data = pd.read_excel(trainingFilePath) # Ignore the samples that we do not have the full data for. data = data.dropna() data.reset_index() # Reset so that it can be easily indexed. files = data["Filename"].to_list() bestGuessTimes = (data[data.columns[1]] * 1e6).to_list() # This will be the same times for the probe destruction dataset. It is generally ignored currently. bestGuessVels = data[data.columns[-1]].to_list() d = {"Filename": files} if skipUntilTimes == []: skipUntilTimes = [12e-6] cmapAttempt = cmap if cmapAttempt in COLORMAPS.keys(): cmapAttempt = COLORMAPS[cmapAttempt] for i in range(len(thresholds)): d[f"threshold {thresholds[i]}"] = np.zeros( (len(skipUntilTimes), len(files))) d[f"threshold {thresholds[i]} error"] = np.zeros( (len(skipUntilTimes), len(files))) print("Running the experiment") for i in tqdm.trange(len(files)): filename = os.path.join(os.path.join("..", "dig"), f"{files[i]}") MySpect = Spectrogram(filename, overlap=0) peaks, _, heights = baselines_by_squash(MySpect, min_percent=0.75) baselineInd = MySpect._velocity_to_index(peaks[0]) intensity = MySpect.intensity[baselineInd] for skipTimeInd in range(len(skipUntilTimes)): skipXInds = MySpect._time_to_index(skipUntilTimes[skipTimeInd]) outputTimeInds = np.array(baselineExperiment( intensity, thresholds, skipXInds), dtype=int) for thresInd, thres in enumerate(thresholds): timeEstimate = MySpect.time[outputTimeInds[thresInd]] * 1e6 d[f"threshold {thres}"][skipTimeInd][i] = timeEstimate d[f"threshold {thres} error"][skipTimeInd][ i] = bestGuessTimes[i] - timeEstimate del MySpect # Compute summary statistics summaryStatistics = np.zeros((len(thresholds), len(skipUntilTimes), 5)) stats = ["Avg", "Std", "Median", "Max", "Min"] print(f"Computing the summary statistics: [{stats}]") for i in tqdm.trange(len(thresholds)): for j in range(len(skipUntilTimes)): q = np.power(d[f"threshold {thresholds[i]} error"][j], errorPower) summaryStatistics[i][j][0] = np.mean(q) summaryStatistics[i][j][1] = np.std(q) summaryStatistics[i][j][2] = np.median(q) summaryStatistics[i][j][3] = np.max(q) summaryStatistics[i][j][4] = np.min(q) for i in tqdm.trange(len(stats)): fig = plt.figure() ax = plt.gca() pcm = ax.pcolormesh(np.array(skipUntilTimes) * 1e6, thresholds, summaryStatistics[:, :, i], cmap=cmapAttempt) plt.title(f"{stats[i]} Error over the Files Tested") plt.ylabel("Thresholds") plt.xlabel("Time that you skip at the beginning ($\mu$s)") plt.gcf().colorbar(pcm, ax=ax) plt.show() # Need this for Macs. summaryFolder = r"../JumpOffTimeEstimates" if not os.path.exists(summaryFolder): os.makedirs(summaryFolder) for i in range(len(thresholds)): q = pd.DataFrame(summaryStatistics[i]) internal_a = str(thresholds[i]).replace(".", "_") saveSummaryFilename = f"summaryThresh{internal_a}.csv" q.to_csv(os.path.join(summaryFolder, saveSummaryFilename)) return summaryStatistics, d