def runAnalysis(self): #get params windowSize = self.getValue('windowSize') averageSize = self.getValue('averageSize') #get data window dataWindow = self.getValue('analysisWindow') #get image array A = dataWindow.image #get array shape frames, height, width = A.shape #print(A.shape) #plot roi in center if dataWindow.currentROI == None: centerROI = makeROI('rectangle', [[10, 10], [height - 20, width - 20]], window=dataWindow) else: centerROI = dataWindow.currentROI #plot roi average trace plot = centerROI.plot() #get trace data trace = centerROI.getTrace() #define function to get moving average from trace def moving_average(x, n=windowSize): return np.convolve(x, np.ones((n, )) / n, mode='valid') #get trace moving average movingAverage = moving_average(trace, n=windowSize) #set moving average values <= 0 to 0.0000001 movingAverage[movingAverage <= 0] = 0.0000001 #add moving average trace to plot plot.p1.plot(movingAverage, pen=(1, 3), symbol=None) #identify peak of trace peakFrame = np.argmax(movingAverage) #plot peak plot.p1.plot(np.array([peakFrame]), np.array([movingAverage[peakFrame]]), pen=None, symbol='o', symbolSize=20) #average frames around peak start = int(peakFrame - averageSize / 2) end = int(peakFrame + averageSize / 2) #prevent indexing from reaching past ends of trace if start < 0: start = 0 if end > frames: end = frames #get peak images averageImages = A[start:end] #display average stack #averageImageStack_Win = Window(averageImages, 'Peak Images') #average peak images averageImage = np.mean(averageImages, axis=0) #display average #averageImage_Win = Window(averageImage,'Averaged Peak Images') #make stack of averaged images scaleImage_stack = np.ones_like(A) scaleImage_stack = scaleImage_stack * averageImage scaleImage_stack = scaleImage_stack[int(windowSize / 2):-(int(windowSize / 2) - 1)] #scale moving average 0 -> 1 scale = movingAverage / max(movingAverage) #scale averaged image stack scaleImage_stack = np.multiply(scaleImage_stack, scale[:, None, None]) #show scale images #scaleWindow = Window(scaleImage_stack,'Scaled Average Images') #subtract scaled average from original stack A_subtract = np.zeros_like(A) A_subtract[int(windowSize / 2):-(int(windowSize / 2) - 1)] = ( A[int(windowSize / 2):-(int(windowSize / 2) - 1)] - scaleImage_stack) #show subtracted image stack subtractWindow = Window(A_subtract, 'Subtracted Images')
filename = r"C:\Users\g_dic\OneDrive\Desktop\trial_2_Cal520_cip3.tif" #filename = r"C:\Users\g_dic\OneDrive\Desktop\2021_02_05_KRAP_800ms001.tif" windowSize = 100 #averageSize = 100 #open file in window dataWindow = open_file(filename) #get image array A = dataWindow.imageArray() #get array shape frames, height, width = A.shape #plot roi in center centerROI = makeROI('rectangle', [[10, 10], [height - 20, width - 20]], window=dataWindow) #plot roi average trace plot = centerROI.plot() #get trace data trace = centerROI.getTrace() #define function to get moving average from trace def moving_average(x, n=windowSize): return np.convolve(x, np.ones((n, )) / n, mode='valid') #get trace moving average movingAverage = moving_average(trace, n=windowSize)
def autodetectFlash(self): rangeStart = self.getValue('flashRangeStart') rangeEnd = self.getValue('flashRangeEnd') windowSize = self.getValue('windowSize') showAverage = self.getValue('showAverage') useROI = self.getValue('useROI') #get data window dataWindow = self.getValue('window') #get image array A = dataWindow.image #get array shape frames, height, width = A.shape #plot roi in center if useROI: if dataWindow.currentROI == None: print('No ROI detected: generating center ROI') centerROI = makeROI('rectangle', [[10, 10], [height - 20, width - 20]], window=dataWindow) else: centerROI = dataWindow.currentROI else: centerROI = makeROI('rectangle', [[10, 10], [height - 20, width - 20]], window=dataWindow) #plot roi average trace if showAverage: plot = centerROI.plot() #get trace data trace = centerROI.getTrace() #define function to get moving average from trace def moving_average(x, n=windowSize): return np.convolve(x, np.ones((n, )) / n, mode='valid') #get trace moving average movingAverage = moving_average(trace, n=windowSize) #set moving average values <= 0 to 0.0000001 movingAverage[movingAverage <= 0] = 0.0000001 #add moving average trace to plot if showAverage: plot.p1.plot(movingAverage, pen=(1, 3), symbol=None) #identify peak of trace - first frame of flash flashStart = rangeStart + np.argmax(movingAverage[rangeStart:rangeEnd]) #identify 1st drop after peak - end of flash flashEnd = flashStart + np.argmin(movingAverage[flashStart:rangeEnd]) #plot flash start and end if showAverage: plot.p1.plot(np.array([flashStart]), np.array([movingAverage[flashStart]]), pen=None, symbol='o', symbolSize=20) plot.p1.plot(np.array([flashEnd]), np.array([movingAverage[flashEnd]]), pen=None, symbol='o', symbolSize=20) #remove center ROI if showAverage == False: centerROI.delete() return flashStart, flashEnd