def func(self, browser): """ Subtract a baseline from the currently plotted traces. Baseline is the average of all datapoints between the current position of the data cursors. Options: 1) keep original traces intact and create processed copies """ ############################################ # ANALYSIS FUNCTION plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.oneDimToolStackedWidget # Copy data if required if self.keepData.isChecked(): aux.make_data_copy(browser, plotWidget) # Get dt dt = aux.get_attr(plotWidget.plotDataItems, 'dt')[0] # Make average between cursors and subract for each trace for item in plotWidget.plotDataItems: bslData, c1 = aux.get_dataRange(plotWidget, item) bsl = np.mean(bslData) item.data = item.data - bsl item.analysis['baselineStart'] = c1 item.analysis['baselineEnd'] = c1+len(bslData) # Re-plot data pgplot.replot(browser, plotWidget)
def func(self, browser): """ Subtract a baseline from the currently plotted traces. Baseline is the average of all datapoints between the current position of the data cursors. Options: 1) keep original traces intact and create processed copies """ ############################################ # ANALYSIS FUNCTION plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.oneDimToolStackedWidget # Copy data if required if self.keepData.isChecked(): aux.make_data_copy(browser, plotWidget) # Get dt dt = aux.get_attr(plotWidget.plotDataItems, 'dt')[0] # Make average between cursors and subract for each trace for item in plotWidget.plotDataItems: bslData, c1 = aux.get_dataRange(plotWidget, item) bsl = np.mean(bslData) item.data = item.data - bsl item.analysis['baselineStart'] = c1 item.analysis['baselineEnd'] = c1 + len(bslData) # Re-plot data pgplot.replot(browser, plotWidget)
def func(self, browser): """ Plot histogram from selected data Options: 1) clear axes 2) bin size 3) normalised 4) orientation 5) type 6) color """ ############################################ # ANALYSIS FUNCTION # Read options clearAxis = self.clearAxis.isChecked() binSize = float(self.binSize.text()) orientation = str(self.orientation.currentText()) histtype = str(self.type.currentText()) # Get selected item in data tree traces = self.plotWidget.plotDataItems dt = traces[0].attrs['dt'] # Get X and Y data and check cursors data, c1 = aux.get_dataRange(self.plotWidget, traces[0]) # Calculate number of bins dataRange = np.abs(data.max() - data.min()) nbins = np.ceil(dataRange / binSize) # Remove all existing axes if clearAxis: for ax in self.canvas.fig.axes: self.canvas.fig.delaxes(ax) # Create grid nPlots, width_ratios = 1, [1] gs = gridspec.GridSpec(1, nPlots, width_ratios=width_ratios) # Create subplots ax = [] for plot in range(nPlots): ax.append(self.canvas.fig.add_subplot(gs[plot])) # Plot histogram h = ax[0].hist(data, bins=nbins, orientation=orientation, histtype=histtype, normed=True, color='k') self.canvas.draw()
def get_data(): """ Return the data currently plotted. """ data = [] for item in browser.ui.dataPlotsWidget.plotDataItems: dataRange, c1 = aux.get_dataRange(browser.ui.dataPlotsWidget, item) data.append(dataRange) data = np.array(data) # this will return an error if dts are different (data is not the same len) attrs = item.attrs if data.shape[0]==1: return data.ravel() else: return data
def func(self, browser): """ Plot histogram from selected data Options: 1) clear axes 2) bin size 3) normalised 4) orientation 5) type 6) color """ ############################################ # ANALYSIS FUNCTION # Read options clearAxis = self.clearAxis.isChecked() binSize = float(self.binSize.text()) orientation = str(self.orientation.currentText()) histtype = str(self.type.currentText()) # Get selected item in data tree traces = self.plotWidget.plotDataItems dt = traces[0].attrs['dt'] # Get X and Y data and check cursors data, c1 = aux.get_dataRange(self.plotWidget, traces[0]) # Calculate number of bins dataRange = np.abs(data.max()-data.min()) nbins = np.ceil(dataRange/binSize) # Remove all existing axes if clearAxis: for ax in self.canvas.fig.axes: self.canvas.fig.delaxes(ax) # Create grid nPlots, width_ratios = 1, [1] gs = gridspec.GridSpec(1, nPlots, width_ratios=width_ratios) # Create subplots ax = [] for plot in range(nPlots): ax.append(self.canvas.fig.add_subplot(gs[plot])) # Plot histogram h = ax[0].hist(data, bins=nbins, orientation=orientation, histtype=histtype, normed=True, color='k') self.canvas.draw()
def get_data(): """ Return the data currently plotted. """ data = [] for item in browser.ui.dataPlotsWidget.plotDataItems: dataRange, c1 = aux.get_dataRange(browser.ui.dataPlotsWidget, item) data.append(dataRange) data = np.array( data ) # this will return an error if dts are different (data is not the same len) attrs = item.attrs if data.shape[0] == 1: return data.ravel() else: return data
def func(self, browser): """ Analyse tracking data for user defined zones in the arena. Currently the two zones are defined by a divider on the X or Y coordinates. Properties measured: 1 - time spent in each zone normalized to total time 2 - number of zone transitions Options: 1) Position of the divider between the two zones """ ############################################ # ANALYSIS FUNCTION # Read options zoneDivider = float(self.plotWidget.zoneDividerCursorPos) # Get data and check cursors traces = self.plotWidget.plotDataItems dt = traces[0].attrs['dt'] data, c1 = aux.get_dataRange(self.plotWidget, traces[0]) # Get time in each zone zoneTime = [] totalTime = float(len(data)) zone1 = np.sum(data < zoneDivider) zone2 = totalTime - zone1 zoneTime.append(zone1 / totalTime) zoneTime.append(zone2 / totalTime) # Get number of zone transitions transitions = np.sum(np.diff(data < zoneDivider)) # Store data results = [] results.append(['Time', zoneTime]) results.append(['Transitions', [transitions]]) aux.save_results(self.browser, 'Zone_analysis', results)
def func(self, browser): """ Plot tracking data in XY from currently plotted X and Y data. Output goes to Matplotlib tab. Take data between cursors if they are active or the whole trace if there are no cursors. Options: 1) Plot histogram 2) Plot long axis over time """ ############################################ # ANALYSIS FUNCTION # Read options hist = self.histOption.isChecked() axisTime = self.plotAxisOption.isChecked() color = str(self.color.currentText()) # Get plotted traces traces = self.plotWidget.plotDataItems dt = traces[0].attrs['dt'] # Get X and Y data and check cursors X, c1 = aux.get_dataRange(self.plotWidget, traces[0]) Y, c1 = aux.get_dataRange(self.plotWidget, traces[1]) # Make sure Y is the long axis if X.max() > Y.max(): Yold = Y Y = X X = Yold # Remove all existing axes for ax in self.canvas.fig.axes: self.canvas.fig.delaxes(ax) # Create grid if hist: if axisTime: nPlots, width_ratios = 3, [1, 4, 1] histAx = 2 else: nPlots, width_ratios = 2, [1, 1] histAx = 1 elif axisTime: nPlots, width_ratios = 2, [1, 5] else: nPlots, width_ratios = 1, [1] gs = gridspec.GridSpec(1, nPlots, width_ratios=width_ratios) # Create subplots ax = [] for plot in range(nPlots): ax.append(self.canvas.fig.add_subplot(gs[plot])) # Plot tracking ax[0].plot(X, Y, color) # Plot long axis over time if axisTime: xaxis = np.arange(0, len(Y), dt) ax[1].plot(xaxis, Y, color) # Plot histogram if hist: h = ax[histAx].hist(Y, bins=30, orientation='horizontal', histtype='stepfilled', normed=True, color=color) self.canvas.draw()
def func(self, browser): """ Calculate average trace from currently plotted traces. If cursors are selected the average is calculated for the range within the cursors. Options: 1) create new entry in Working Data tree with the result 2) plot average with orginal traces 3) caclulate SD and SEM """ ############################################ # ANALYSIS FUNCTION plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.oneDimToolStackedWidget results = [] # Get data and cursors data = [] for item in plotWidget.plotDataItems: dt = item.attrs['dt'] dataRange, c1 = aux.get_dataRange(plotWidget, item) data.append(dataRange) data = np.array(data) # Calculate average and make h5item for plotting try: #avgData = np.nanmean(data[:, c1:c2], 0) avgData = np.mean(data, 0) avgItem = aux.make_h5item('avg', avgData, plotWidget.plotDataItems[0].attrs) results.append(['avg_trace', avgData, item.attrs]) except ValueError: aux.error_box('Cannot calculate average on data with different lengths', sys.exc_info(), 'Please ensure that all traces have the same length') return # Calculate SD and SEM if self.sdBox.isChecked(): #sdData = np.nanstd(data[:, c1:c2], 0) sdData = np.std(data, 0) avgPlusSDItem = aux.make_h5item('avg+sd', avgData+sdData, plotWidget.plotDataItems[0].attrs) avgMinusSDItem = aux.make_h5item('avg-sd', avgData-sdData, plotWidget.plotDataItems[0].attrs) results.append(['avg+sd', avgData+sdData, item.attrs]) results.append(['avg-sd', avgData-sdData, item.attrs]) if self.semBox.isChecked(): #semData = np.nanstd(data[:, c1:c2], 0)/np.sqrt(len(data)) semData = np.std(data, 0)/np.sqrt(len(data)) avgPlusSEMItem = aux.make_h5item('avg+sem', avgData+semData, plotWidget.plotDataItems[0].attrs) avgMinusSEMItem = aux.make_h5item('avg-sem', avgData-semData, plotWidget.plotDataItems[0].attrs) results.append(['avg+sem', avgData+semData, item.attrs]) results.append(['avg-sem', avgData-semData, item.attrs]) # Plot data if self.showTraces.isChecked(): clear = False else: clear = True pgplot.browse_singleData(browser, plotWidget, avgItem, clear=clear, color='r') # Store data if self.storeResult.isChecked(): aux.save_results(browser, item.parent().text(0)+'_average', results)
def func(self, browser): """ Temporary event detection function using amplitude threshold only. Noise safety is for when coming down from peak, go down an extra amount from threshold before starting to search for the next event. """ ############################################ # ANALYSIS FUNCTION # Read detection options try: threshold = float(self.browser.ui.dataPlotsWidget.cursorThsPos) except NameError: aux.error_box('No threshold selected') return try: noiseSafety = float(self.eventNoiseSafety.text()) smoothFactor = float(self.eventSmooth.text()) direction = str(self.eventDirection.currentText()) minDuration = float(self.eventMinDuration.text()) detectionTrace = str(self.detectionTrace.currentText()) minInterval = float(self.eventMinInterval.text()) except NameError: aux.error_box('Invalid detection value') bslWindow = 1.0 slowestRise = 0.5 #minEventInterval = 5000.0 # Ensure that noise safety has the same sign as the threshold noiseSafety = np.sign(threshold) * abs(noiseSafety) # Get widgets plotWidget = browser.ui.dataPlotsWidget # Get dt list and attrs for use in concatenated data dtList = aux.get_attr(self.browser.ui.dataPlotsWidget.plotDataItems, 'dt') dt = dtList[0] self.dt = dt item = self.browser.ui.dataPlotsWidget.plotDataItems[0] attrs = item.attrs # Get data currently plotted and concatenate in a single sweep data = [] for item in plotWidget.plotDataItems: trace, c1 = aux.get_dataRange(plotWidget, item) data.append(trace) data = np.array(data).ravel() # Get derivative trace and filter dtrace = None if detectionTrace == 'derivative': print threshold dtrace = np.diff(data) dtrace = acq4filter.besselFilter(dtrace, 2000, 1, dt / 1000, 'low', True) # Smooth self.trace = data if smoothFactor > 1: data = smooth.smooth(data, window_len=smoothFactor, window='hanning') # Comparison functions if direction == 'negative': comp = lambda a, b: a < b elif direction == 'positive': comp = lambda a, b: a > b # Correct times for dt minEventInterval = minInterval / dt minDuration = minDuration / dt bslWindow = bslWindow / dt slowestRise = slowestRise / dt # Run detection eventCounter, i = 0, 0 #+bslWindow/dt+slowestRise/dt iLastDetection = 0 self.xOnsets, self.yOnsets = [], [] bsl = 0 if dtrace is not None: detectionData = dtrace else: detectionData = data while i < len(detectionData): # Sliding baseline #bsl = np.mean(data[i-bslWindow-slowestRise:i]) if comp(detectionData[i] - bsl, threshold): print i, i - iLastDetection if i - iLastDetection > minEventInterval: # Min inter-event interval self.xOnsets.append(i) self.yOnsets.append(data[i]) eventCounter += 1 iLastDetection = i while i < len(detectionData) and comp( detectionData[i] - bsl, (threshold - noiseSafety)): i += 1 # skip values if index in bounds AND until the value is below/above threshold again if i - iLastDetection < minDuration: # Event is too brief self.xOnsets.pop() self.yOnsets.pop() eventCounter -= 1 else: i += 1 # for min event interval else: i += 1 frequency = eventCounter / (len(data) * dt) * 1000 # in Hz print eventCounter, 'events detected at', frequency, 'Hz' # Store event onsets and peaks in h5 data tree results = [] results.append(['trace', np.array(self.trace), attrs]) results.append(['xOnsets', np.array(self.xOnsets)]) results.append(['yOnsets', np.array(self.yOnsets)]) results.append(['number', np.array([eventCounter])]) results.append(['frequency', np.array([frequency])]) aux.save_results(browser, 'Event_Detection', results) # Plot results self.show_events(data, np.array(self.xOnsets), np.array(self.yOnsets), dt) # Turn cursors off (the plot has been cleared so there are no cursors displayed) self.browser.ui.actionShowCursors.setChecked(False) plotWidget.cursor = False
def func(self, browser): """ Calculate the probability of having an event per time bin Very simple threshold crossing event detection. Options: 1) Threshold for detecting spikes 2) Time bin size (ms) 3) Event direction """ ############################################ # ANALYSIS FUNCTION # Read detection options try: threshold = float(self.browser.ui.dataPlotsWidget.cursorThsPos) except NameError: aux.error_box('No threshold selected') return try: timeBin = np.abs(float(self.timeBin.text())) except ValueError: aux.error_box('Invalid time bin') return direction = str(self.eventDirection.currentText()) # Get widgets plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.oneDimToolStackedWidget # Get parent text of plotted items try: parentText = plotWidget.plotDataItems[0].parent().text(0) # Assumes all plotted data have the same parent except AttributeError: # Parent = None parentText = 'Data' # Detect events in each trace if direction=='negative': comp = lambda a, b: a < b elif direction=='positive': comp = lambda a, b: a > b # Check time bin (using the first plotted item) item = plotWidget.plotDataItems[0] dt = item.attrs['dt'] data, c1 = aux.get_dataRange(plotWidget, item) if (timeBin/dt==0) or (len(data)<timeBin/dt): aux.error_box('Invalid time bin', infoText='Make sure time bin is smaller than the selected data range') return # Detect events results = [] plotWidget.clear() for item in plotWidget.plotDataItems: dt = item.attrs['dt'] data, c1 = aux.get_dataRange(plotWidget, item) apCounter, i = 0, 0 xOnsets, yOnsets = [], [] while i<len(data): if comp(data[i],threshold): xOnsets.append(i) yOnsets.append(data[i]) apCounter+=1 while i<len(data) and comp(data[i],threshold): i+=1 # skip values if index in bounds AND until the value is below/above threshold again else: i+=1 # Get events per time bin binSize = int(timeBin/dt) nbins = np.ceil(len(data)/binSize) eventCounter = [] for b in np.arange(1, nbins+1): count = np.sum((xOnsets>(b-1)*binSize) & (xOnsets<(b*binSize))) eventCounter.append(count) bins = np.arange(0+binSize/2., nbins*binSize+binSize/2., binSize)*dt # Store data results.append(['event_counts', np.array(eventCounter)]) # Plot detected events self.show_events(data, np.array(xOnsets), np.array(yOnsets), dt) # Turn cursors off (the plot has been cleared so there are no cursors displayed) self.browser.ui.actionShowCursors.setChecked(False) plotWidget.cursor = False # Store results results.append(['time_bins', bins]) aux.save_results(browser, parentText+'_eventProbability', results)
def func(self, browser): """ Perform some operations on selected traces. Options: 1) keep original traces intact and create processed copies 2) delete the data points between the cursors 3) offset the start of the trace by a number of X-axis points (adds NaNs) """ ############################################ # ANALYSIS FUNCTION # Get widgets plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.oneDimToolStackedWidget # Get options if self.offsetBox.isChecked(): try: nOffsetPoints = float(self.offsetPoints.text()) except ValueError: aux.error_box('Invalid number of points') return if self.scaleBox.isChecked(): try: vScale = float(self.scaleValue.text()) except ValueError: aux.error_box('Invalid scale entry') return # Copy data if required if self.keepData.isChecked(): aux.make_data_copy(browser, plotWidget) # Iterate through traces i = 0 for item in plotWidget.plotDataItems: # Get dt and data range dt = item.attrs['dt'] data, c1, cx1, cx2 = aux.get_dataRange(plotWidget, item, cursors=True) # Check baseline if 'baselineStart' in item.analysis: bsl = 0 # the mean of the baseline will always be 0 else: bsl = np.mean(item.data[0:round(1./dt)]) # Scale data if self.scaleBox.isChecked(): item.data = item.data * vScale if self.normalizeBox.isChecked(): peakDirection = str(self.peakComboBox.currentText()) if peakDirection=='Positive peak': peak = np.max(data) else: peak = -np.min(data) item.data = item.data / peak # Delete points if self.delPointsBox.isChecked(): item.data = np.delete(item.data, np.s_[c1:c1+len(data)+1]) # Add offset if self.offsetBox.isChecked(): offset = int(nOffsetPoints/dt) item.data = np.insert(item.data, 0, np.zeros(offset) + np.nan) # Operation against selected data (currently only works for one selected data item) if self.selectedBox.isChecked(): operation = str(self.selectedComboBox.currentText()) if operation=='Subtract': op = lambda a, b: a - b if operation=='Add': op = lambda a, b: a + b try: selectedItem = browser.ui.workingDataTree.selectedItems()[i] item.data = op(item.data, selectedItem.data) except IndexError: aux.error_box('Number of selected data items does not match number of plotted traces') return i+=1 # Re-plot data pgplot.replot(browser, plotWidget)
def func(self, browser): """ Fit data traces within cursor-defined range Options: 1) Equation 2) Initial guesses """ ############################################ # ANALYSIS FUNCTION # Get widgets plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.oneDimToolStackedWidget # Replot data to clear previous measure plot points pgplot.replot(browser, plotWidget) # Get parent text of plotted items try: parentText = plotWidget.plotDataItems[0].parent().text(0) # Assumes all plotted data have the same parent except AttributeError: # Parent = None parentText = 'Data' # Get current function currentFuncmap = self.dataFit.fitfuncmap[str(self.comboBox.currentText())] # Read initial guesses pInit = [] try: nParam = len(currentFuncmap[2]) for n in range(nParam): pInit.append(float(self.p[n].text())) except ValueError: aux.error_box('Invalid value in initial guess') return # Fit data fitResults, fitTraces, fitTracesAttrs = [], [], [] for item in plotWidget.plotDataItems: # Get data range to fit dt = item.attrs['dt'] fitTracesAttrs.append(item.attrs) yData, c1, cx1, cx2 = aux.get_dataRange(plotWidget, item, cursors=True) xRange = pgplot.make_xvector(yData, dt)+cx1 self.dataFit.c1 = cx1 # Fit func = currentFuncmap[0] fitParams = self.dataFit.fit(func, xRange, yData, pInit) fitResults.append(fitParams) print fitParams # Plot fitted function over trace if self.extendBox.isChecked(): xRange = pgplot.make_xvector(item.data, dt) #xRange = np.arange(plotWidget.viewBox.viewRange()[0][0], plotWidget.viewBox.viewRange()[0][1], dt) fittedTrace = func(xRange, *fitParams) plotWidget.plot(xRange, fittedTrace, pen=pg.mkPen('r', width=1)) fitTraces.append(fittedTrace) # Store results if self.storeBox.isChecked(): fitResults = np.array(fitResults) results = [] for n in range(np.shape(fitResults)[1]): results.append([str(self.plabels[n].text()), fitResults[:,n]]) for n in range(len(fitTraces)): results.append(['fitTraces_'+str(n), fitTraces[n], fitTracesAttrs[n]]) aux.save_results(browser, parentText+'_fit', results)
def func(self, browser): """ Fit data traces within cursor-defined range Options: 1) Equation 2) Initial guesses """ ############################################ # ANALYSIS FUNCTION # Get widgets plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.oneDimToolStackedWidget # Replot data to clear previous measure plot points pgplot.replot(browser, plotWidget) # Get parent text of plotted items try: parentText = plotWidget.plotDataItems[0].parent().text( 0) # Assumes all plotted data have the same parent except AttributeError: # Parent = None parentText = 'Data' # Get current function currentFuncmap = self.dataFit.fitfuncmap[str( self.comboBox.currentText())] # Read initial guesses pInit = [] try: nParam = len(currentFuncmap[2]) for n in range(nParam): pInit.append(float(self.p[n].text())) except ValueError: aux.error_box('Invalid value in initial guess') return # Fit data fitResults, fitTraces, fitTracesAttrs = [], [], [] for item in plotWidget.plotDataItems: # Get data range to fit dt = item.attrs['dt'] fitTracesAttrs.append(item.attrs) yData, c1, cx1, cx2 = aux.get_dataRange(plotWidget, item, cursors=True) xRange = pgplot.make_xvector(yData, dt) + cx1 self.dataFit.c1 = cx1 # Fit func = currentFuncmap[0] fitParams = self.dataFit.fit(func, xRange, yData, pInit) fitResults.append(fitParams) print fitParams # Plot fitted function over trace if self.extendBox.isChecked(): xRange = pgplot.make_xvector(item.data, dt) #xRange = np.arange(plotWidget.viewBox.viewRange()[0][0], plotWidget.viewBox.viewRange()[0][1], dt) fittedTrace = func(xRange, *fitParams) plotWidget.plot(xRange, fittedTrace, pen=pg.mkPen('r', width=1)) fitTraces.append(fittedTrace) # Store results if self.storeBox.isChecked(): fitResults = np.array(fitResults) results = [] for n in range(np.shape(fitResults)[1]): results.append([str(self.plabels[n].text()), fitResults[:, n]]) for n in range(len(fitTraces)): results.append( ['fitTraces_' + str(n), fitTraces[n], fitTracesAttrs[n]]) aux.save_results(browser, parentText + '_fit', results)
def func(self, browser): """ Calculate the probability of having an event per time bin Very simple threshold crossing event detection. Options: 1) Threshold for detecting spikes 2) Time bin size (ms) 3) Event direction """ ############################################ # ANALYSIS FUNCTION # Read detection options try: threshold = float(self.browser.ui.dataPlotsWidget.cursorThsPos) except NameError: aux.error_box('No threshold selected') return try: timeBin = np.abs(float(self.timeBin.text())) except ValueError: aux.error_box('Invalid time bin') return direction = str(self.eventDirection.currentText()) # Get widgets plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.oneDimToolStackedWidget # Get parent text of plotted items try: parentText = plotWidget.plotDataItems[0].parent().text( 0) # Assumes all plotted data have the same parent except AttributeError: # Parent = None parentText = 'Data' # Detect events in each trace if direction == 'negative': comp = lambda a, b: a < b elif direction == 'positive': comp = lambda a, b: a > b # Check time bin (using the first plotted item) item = plotWidget.plotDataItems[0] dt = item.attrs['dt'] data, c1 = aux.get_dataRange(plotWidget, item) if (timeBin / dt == 0) or (len(data) < timeBin / dt): aux.error_box( 'Invalid time bin', infoText= 'Make sure time bin is smaller than the selected data range') return # Detect events results = [] plotWidget.clear() for item in plotWidget.plotDataItems: dt = item.attrs['dt'] data, c1 = aux.get_dataRange(plotWidget, item) apCounter, i = 0, 0 xOnsets, yOnsets = [], [] while i < len(data): if comp(data[i], threshold): xOnsets.append(i) yOnsets.append(data[i]) apCounter += 1 while i < len(data) and comp(data[i], threshold): i += 1 # skip values if index in bounds AND until the value is below/above threshold again else: i += 1 # Get events per time bin binSize = int(timeBin / dt) nbins = np.ceil(len(data) / binSize) eventCounter = [] for b in np.arange(1, nbins + 1): count = np.sum((xOnsets > (b - 1) * binSize) & (xOnsets < (b * binSize))) eventCounter.append(count) bins = np.arange(0 + binSize / 2., nbins * binSize + binSize / 2., binSize) * dt # Store data results.append(['event_counts', np.array(eventCounter)]) # Plot detected events self.show_events(data, np.array(xOnsets), np.array(yOnsets), dt) # Turn cursors off (the plot has been cleared so there are no cursors displayed) self.browser.ui.actionShowCursors.setChecked(False) plotWidget.cursor = False # Store results results.append(['time_bins', bins]) aux.save_results(browser, parentText + '_eventProbability', results)
def func(self, browser): """ Generate a FI curve from current steps Options: 1) Threshold for detecting spikes """ ############################################ # ANALYSIS FUNCTION # Read detection options try: threshold = float(self.browser.ui.dataPlotsWidget.cursorThsPos) except NameError: aux.error_box('No threshold selected') return # Get widgets plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.oneDimToolStackedWidget # Get parent text of plotted items try: parentText = plotWidget.plotDataItems[0].parent().text(0) # Assumes all plotted data have the same parent except AttributeError: # Parent = None parentText = 'Data' # Detect APs in each trace comp = lambda a, b: a > b apNumber, apFrequency, results = [], [], [] plotWidget.clear() for item in plotWidget.plotDataItems: dt = item.attrs['dt'] data, c1 = aux.get_dataRange(plotWidget, item) apCounter, i = 0, 0 xOnsets, yOnsets = [], [] while i<len(data): if comp(data[i],threshold): xOnsets.append(i) yOnsets.append(data[i]) apCounter+=1 while i<len(data) and comp(data[i],threshold): i+=1 # skip values if index in bounds AND until the value is below/above threshold again else: i+=1 # Calculate AP frequency as 1/mean(ISI) isi = np.mean(np.diff(xOnsets))*dt # in ms if isi>0: freq = 1/(isi/1000.) else: freq = 0.0 # Store number of APs and mean frequency apNumber.append(apCounter) apFrequency.append(freq) # Plot detected APs self.show_events(data, np.array(xOnsets), np.array(yOnsets), dt) # Turn cursors off (the plot has been cleared so there are no cursors displayed) self.browser.ui.actionShowCursors.setChecked(False) plotWidget.cursor = False # Store results results.append(['AP_number', np.array(apNumber)]) results.append(['AP_frequency', np.array(apFrequency)]) aux.save_results(browser, parentText+'_FI', results)
def func(self, browser): """ Generate a FI curve from current steps Options: 1) Threshold for detecting spikes """ ############################################ # ANALYSIS FUNCTION # Read detection options try: threshold = float(self.browser.ui.dataPlotsWidget.cursorThsPos) except NameError: aux.error_box('No threshold selected') return # Get widgets plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.oneDimToolStackedWidget # Get parent text of plotted items try: parentText = plotWidget.plotDataItems[0].parent().text( 0) # Assumes all plotted data have the same parent except AttributeError: # Parent = None parentText = 'Data' # Detect APs in each trace comp = lambda a, b: a > b apNumber, apFrequency, results = [], [], [] plotWidget.clear() for item in plotWidget.plotDataItems: dt = item.attrs['dt'] data, c1 = aux.get_dataRange(plotWidget, item) apCounter, i = 0, 0 xOnsets, yOnsets = [], [] while i < len(data): if comp(data[i], threshold): xOnsets.append(i) yOnsets.append(data[i]) apCounter += 1 while i < len(data) and comp(data[i], threshold): i += 1 # skip values if index in bounds AND until the value is below/above threshold again else: i += 1 # Calculate AP frequency as 1/mean(ISI) isi = np.mean(np.diff(xOnsets)) * dt # in ms if isi > 0: freq = 1 / (isi / 1000.) else: freq = 0.0 # Store number of APs and mean frequency apNumber.append(apCounter) apFrequency.append(freq) # Plot detected APs self.show_events(data, np.array(xOnsets), np.array(yOnsets), dt) # Turn cursors off (the plot has been cleared so there are no cursors displayed) self.browser.ui.actionShowCursors.setChecked(False) plotWidget.cursor = False # Store results results.append(['AP_number', np.array(apNumber)]) results.append(['AP_frequency', np.array(apFrequency)]) aux.save_results(browser, parentText + '_FI', results)
def func(self, browser): """ Perform some operations on selected traces. Options: 1) keep original traces intact and create processed copies 2) delete the data points between the cursors 3) offset the start of the trace by a number of X-axis points (adds NaNs) """ ############################################ # ANALYSIS FUNCTION # Get widgets plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.oneDimToolStackedWidget # Get options if self.offsetBox.isChecked(): try: nOffsetPoints = float(self.offsetPoints.text()) except ValueError: aux.error_box('Invalid number of points') return if self.scaleBox.isChecked(): try: vScale = float(self.scaleValue.text()) except ValueError: aux.error_box('Invalid scale entry') return # Copy data if required if self.keepData.isChecked(): aux.make_data_copy(browser, plotWidget) # Iterate through traces i = 0 for item in plotWidget.plotDataItems: # Get dt and data range dt = item.attrs['dt'] data, c1, cx1, cx2 = aux.get_dataRange(plotWidget, item, cursors=True) # Check baseline if 'baselineStart' in item.analysis: bsl = 0 # the mean of the baseline will always be 0 else: bsl = np.mean(item.data[0:round(1. / dt)]) # Scale data if self.scaleBox.isChecked(): item.data = item.data * vScale if self.normalizeBox.isChecked(): peakDirection = str(self.peakComboBox.currentText()) if peakDirection == 'Positive peak': peak = np.max(data) else: peak = -np.min(data) item.data = item.data / peak # Delete points if self.delPointsBox.isChecked(): item.data = np.delete(item.data, np.s_[c1:c1 + len(data) + 1]) # Add offset if self.offsetBox.isChecked(): offset = int(nOffsetPoints / dt) item.data = np.insert(item.data, 0, np.zeros(offset) + np.nan) # Operation against selected data (currently only works for one selected data item) if self.selectedBox.isChecked(): operation = str(self.selectedComboBox.currentText()) if operation == 'Subtract': op = lambda a, b: a - b if operation == 'Add': op = lambda a, b: a + b try: selectedItem = browser.ui.workingDataTree.selectedItems( )[i] item.data = op(item.data, selectedItem.data) except IndexError: aux.error_box( 'Number of selected data items does not match number of plotted traces' ) return i += 1 # Re-plot data pgplot.replot(browser, plotWidget)
def func(self, browser): """ Measure selected properties or statistics in the region defined by the data cursors. Rise time and onset are calculated by finding the peak and walking back until the limits are crossed. Onset limit is baseline mean; if no baseline has been set, it is taken as the mean value of the first 1 ms of data. Options: 1) create new entries in Working Data tree with the results """ ############################################ # ANALYSIS FUNCTION # Get widgets plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.oneDimToolStackedWidget # Replot data to clear previous measure plot points pgplot.replot(browser, plotWidget) # Iterate through traces dataMin, dataMax, dataMean, dataMedian, dataSEM = [], [], [], [], [] dataRiseTime, dataOnset, dataDecay = [], [], [] for item in plotWidget.plotDataItems: # Get dt and data range dt = item.attrs['dt'] data, c1, cx1, cx2 = aux.get_dataRange(plotWidget, item, cursors=True) # Check baseline if 'baselineStart' in item.analysis: bsl = 0 # the mean of the baseline will always be 0 else: bsl = np.mean(data[0:round(1./dt)]) # Measure selected parameters if self.minBox.isChecked(): y = np.min(data) x = np.argmin(data) dataMin.append(y) aux.plot_point(plotWidget, c1, x, y, dt) if self.maxBox.isChecked(): y = np.max(data) x = np.argmax(data) dataMax.append(y) aux.plot_point(plotWidget, c1, x, y, dt) if self.meanBox.isChecked(): y = np.mean(data) dataMean.append(y) plotWidget.plot([cx1,cx2], [y,y], pen=pg.mkPen('#CF1C04', width=1)) if self.medianBox.isChecked(): y = np.median(data) dataMedian.append(y) plotWidget.plot([cx1,cx2], [y,y], pen=pg.mkPen('#CF1C04', width=1)) if self.semBox.isChecked(): y = np.std(data)/np.sqrt(len(data)) dataSEM.append(y) plotWidget.plot([cx1,cx2], [y,y], pen=pg.mkPen('#CF1C04', width=1)) if self.riseTimeBox.isChecked(): try: lowerLimit = float(self.riseLowerLimit.text())/100 upperLimit = float(self.riseUpperLimit.text())/100 except ValueError: aux.error_box('Invalid limits') return # Get peak xPeak, yPeak = self.get_peak(data) # Get limits lowerLimit = lowerLimit*(yPeak-bsl)+bsl upperLimit = upperLimit*(yPeak-bsl)+bsl # Find limit crosses lowerCrossed, upperCrossed = False, False i = 0 while lowerCrossed==False or upperCrossed==False: if (upperCrossed==False) and self.comp(data[xPeak-i],upperLimit): xUpper = xPeak-i upperCrossed = True if self.comp(data[xPeak-i], lowerLimit): xLower = xPeak-i lowerCrossed = True i+=1 dataRiseTime.append((xUpper-xLower)*dt) # Plot points aux.plot_point(plotWidget, c1, xLower, lowerLimit, dt) aux.plot_point(plotWidget, c1, xUpper, upperLimit, dt) if self.onsetBox.isChecked(): xPeak, yPeak = self.get_peak(data) bslCrossed = False i = 0 while bslCrossed==False: if self.comp(data[xPeak-i], bsl): onset = xPeak-i bslCrossed = True i+=1 aux.plot_point(plotWidget, c1, onset, bsl, dt) dataOnset.append(onset*dt) if self.decayTimeBox.isChecked(): try: decayLimit = float(self.decayLimit.text())/100 except ValueError: aux.error_box('Invalid limits') return xPeak, yPeak = self.get_peak(data) yDecay = decayLimit*(yPeak-bsl)+bsl decayCrossed = False i = 0 while decayCrossed==False: if self.comp(data[xPeak+i], yDecay): decayTime = xPeak+i decayCrossed = True i+=1 aux.plot_point(plotWidget, c1, decayTime, yDecay, dt) dataDecay.append(decayTime*dt) # Store results results = [] if self.storeBox.isChecked(): if self.minBox.isChecked(): results.append(['Minimum', np.array(dataMin)]) if self.maxBox.isChecked(): results.append(['Maximum', np.array(dataMax)]) if self.meanBox.isChecked(): results.append(['Mean', np.array(dataMean)]) if self.medianBox.isChecked(): results.append(['Median', np.array(dataMedian)]) if self.semBox.isChecked(): results.append(['SEM', np.array(dataSEM)]) if self.riseTimeBox.isChecked(): results.append(['RiseTime', np.array(dataRiseTime)]) if self.onsetBox.isChecked(): results.append(['Onset', np.array(dataOnset)]) if self.decayTimeBox.isChecked(): results.append(['Decay', np.array(dataDecay)]) aux.save_results(browser, 'Measurements', results)
def func(self, browser): """ Plot tracking data in XY from currently plotted X and Y data. Output goes to Matplotlib tab. Take data between cursors if they are active or the whole trace if there are no cursors. Options: 1) Plot histogram 2) Plot long axis over time """ ############################################ # ANALYSIS FUNCTION # Read options hist = self.histOption.isChecked() axisTime = self.plotAxisOption.isChecked() color = str(self.color.currentText()) # Get plotted traces traces = self.plotWidget.plotDataItems dt = traces[0].attrs['dt'] # Get X and Y data and check cursors X, c1 = aux.get_dataRange(self.plotWidget, traces[0]) Y, c1 = aux.get_dataRange(self.plotWidget, traces[1]) # Make sure Y is the long axis if X.max() > Y.max(): Yold = Y Y = X X = Yold # Remove all existing axes for ax in self.canvas.fig.axes: self.canvas.fig.delaxes(ax) # Create grid if hist: if axisTime: nPlots, width_ratios = 3, [1,4,1] histAx = 2 else: nPlots, width_ratios = 2, [1,1] histAx = 1 elif axisTime: nPlots, width_ratios = 2, [1,5] else: nPlots, width_ratios = 1, [1] gs = gridspec.GridSpec(1, nPlots, width_ratios=width_ratios) # Create subplots ax = [] for plot in range(nPlots): ax.append(self.canvas.fig.add_subplot(gs[plot])) # Plot tracking ax[0].plot(X, Y, color) # Plot long axis over time if axisTime: xaxis = np.arange(0, len(Y), dt) ax[1].plot(xaxis, Y, color) # Plot histogram if hist: h = ax[histAx].hist(Y, bins=30, orientation='horizontal', histtype='stepfilled', normed=True, color=color) self.canvas.draw()
def func(self, browser): """ Temporary event detection function using amplitude threshold only. Noise safety is for when coming down from peak, go down an extra amount from threshold before starting to search for the next event. """ ############################################ # ANALYSIS FUNCTION # Read detection options try: threshold = float(self.browser.ui.dataPlotsWidget.cursorThsPos) except NameError: aux.error_box('No threshold selected') return try: noiseSafety = float(self.eventNoiseSafety.text()) smoothFactor = float(self.eventSmooth.text()) direction = str(self.eventDirection.currentText()) minDuration = float(self.eventMinDuration.text()) detectionTrace = str(self.detectionTrace.currentText()) minInterval = float(self.eventMinInterval.text()) except NameError: aux.error_box('Invalid detection value') bslWindow = 1.0 slowestRise = 0.5 #minEventInterval = 5000.0 # Ensure that noise safety has the same sign as the threshold noiseSafety = np.sign(threshold) * abs(noiseSafety) # Get widgets plotWidget = browser.ui.dataPlotsWidget # Get dt list and attrs for use in concatenated data dtList = aux.get_attr(self.browser.ui.dataPlotsWidget.plotDataItems, 'dt') dt = dtList[0] self.dt = dt item = self.browser.ui.dataPlotsWidget.plotDataItems[0] attrs = item.attrs # Get data currently plotted and concatenate in a single sweep data = [] for item in plotWidget.plotDataItems: trace, c1 = aux.get_dataRange(plotWidget, item) data.append(trace) data = np.array(data).ravel() # Get derivative trace and filter dtrace = None if detectionTrace=='derivative': print threshold dtrace = np.diff(data) dtrace = acq4filter.besselFilter(dtrace, 2000, 1, dt/1000, 'low', True) # Smooth self.trace = data if smoothFactor > 1: data = smooth.smooth(data, window_len=smoothFactor, window='hanning') # Comparison functions if direction=='negative': comp = lambda a, b: a < b elif direction=='positive': comp = lambda a, b: a > b # Correct times for dt minEventInterval = minInterval/dt minDuration = minDuration/dt bslWindow = bslWindow/dt slowestRise = slowestRise/dt # Run detection eventCounter,i = 0,0 #+bslWindow/dt+slowestRise/dt iLastDetection = 0 self.xOnsets, self.yOnsets = [], [] bsl = 0 if dtrace is not None: detectionData = dtrace else: detectionData = data while i<len(detectionData): # Sliding baseline #bsl = np.mean(data[i-bslWindow-slowestRise:i]) if comp(detectionData[i]-bsl,threshold): print i, i-iLastDetection if i-iLastDetection>minEventInterval: # Min inter-event interval self.xOnsets.append(i) self.yOnsets.append(data[i]) eventCounter+=1 iLastDetection = i while i<len(detectionData) and comp(detectionData[i]-bsl,(threshold-noiseSafety)): i+=1 # skip values if index in bounds AND until the value is below/above threshold again if i-iLastDetection < minDuration: # Event is too brief self.xOnsets.pop() self.yOnsets.pop() eventCounter-=1 else: i+=1 # for min event interval else: i+=1 frequency = eventCounter/(len(data)*dt)*1000 # in Hz print eventCounter, 'events detected at', frequency, 'Hz' # Store event onsets and peaks in h5 data tree results = [] results.append(['trace', np.array(self.trace), attrs]) results.append(['xOnsets', np.array(self.xOnsets)]) results.append(['yOnsets', np.array(self.yOnsets)]) results.append(['number', np.array([eventCounter])]) results.append(['frequency', np.array([frequency])]) aux.save_results(browser, 'Event_Detection', results) # Plot results self.show_events(data, np.array(self.xOnsets), np.array(self.yOnsets), dt) # Turn cursors off (the plot has been cleared so there are no cursors displayed) self.browser.ui.actionShowCursors.setChecked(False) plotWidget.cursor = False
def func(self, browser): """ Measure selected properties or statistics in the region defined by the data cursors. Rise time and onset are calculated by finding the peak and walking back until the limits are crossed. Onset limit is baseline mean; if no baseline has been set, it is taken as the mean value of the first 1 ms of data. Options: 1) create new entries in Working Data tree with the results """ ############################################ # ANALYSIS FUNCTION # Get widgets plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.oneDimToolStackedWidget # Replot data to clear previous measure plot points pgplot.replot(browser, plotWidget) # Iterate through traces dataMin, dataMax, dataMean, dataMedian, dataSEM = [], [], [], [], [] dataRiseTime, dataOnset, dataDecay = [], [], [] for item in plotWidget.plotDataItems: # Get dt and data range dt = item.attrs['dt'] data, c1, cx1, cx2 = aux.get_dataRange(plotWidget, item, cursors=True) # Check baseline if 'baselineStart' in item.analysis: bsl = 0 # the mean of the baseline will always be 0 else: bsl = np.mean(data[0:round(1. / dt)]) # Measure selected parameters if self.minBox.isChecked(): y = np.min(data) x = np.argmin(data) dataMin.append(y) aux.plot_point(plotWidget, c1, x, y, dt) if self.maxBox.isChecked(): y = np.max(data) x = np.argmax(data) dataMax.append(y) aux.plot_point(plotWidget, c1, x, y, dt) if self.meanBox.isChecked(): y = np.mean(data) dataMean.append(y) plotWidget.plot([cx1, cx2], [y, y], pen=pg.mkPen('#CF1C04', width=1)) if self.medianBox.isChecked(): y = np.median(data) dataMedian.append(y) plotWidget.plot([cx1, cx2], [y, y], pen=pg.mkPen('#CF1C04', width=1)) if self.semBox.isChecked(): y = np.std(data) / np.sqrt(len(data)) dataSEM.append(y) plotWidget.plot([cx1, cx2], [y, y], pen=pg.mkPen('#CF1C04', width=1)) if self.riseTimeBox.isChecked(): try: lowerLimit = float(self.riseLowerLimit.text()) / 100 upperLimit = float(self.riseUpperLimit.text()) / 100 except ValueError: aux.error_box('Invalid limits') return # Get peak xPeak, yPeak = self.get_peak(data) # Get limits lowerLimit = lowerLimit * (yPeak - bsl) + bsl upperLimit = upperLimit * (yPeak - bsl) + bsl # Find limit crosses lowerCrossed, upperCrossed = False, False i = 0 while lowerCrossed == False or upperCrossed == False: if (upperCrossed == False) and self.comp( data[xPeak - i], upperLimit): xUpper = xPeak - i upperCrossed = True if self.comp(data[xPeak - i], lowerLimit): xLower = xPeak - i lowerCrossed = True i += 1 dataRiseTime.append((xUpper - xLower) * dt) # Plot points aux.plot_point(plotWidget, c1, xLower, lowerLimit, dt) aux.plot_point(plotWidget, c1, xUpper, upperLimit, dt) if self.onsetBox.isChecked(): xPeak, yPeak = self.get_peak(data) bslCrossed = False i = 0 while bslCrossed == False: if self.comp(data[xPeak - i], bsl): onset = xPeak - i bslCrossed = True i += 1 aux.plot_point(plotWidget, c1, onset, bsl, dt) dataOnset.append(onset * dt) if self.decayTimeBox.isChecked(): try: decayLimit = float(self.decayLimit.text()) / 100 except ValueError: aux.error_box('Invalid limits') return xPeak, yPeak = self.get_peak(data) yDecay = decayLimit * (yPeak - bsl) + bsl decayCrossed = False i = 0 while decayCrossed == False: if self.comp(data[xPeak + i], yDecay): decayTime = xPeak + i decayCrossed = True i += 1 aux.plot_point(plotWidget, c1, decayTime, yDecay, dt) dataDecay.append(decayTime * dt) # Store results results = [] if self.storeBox.isChecked(): if self.minBox.isChecked(): results.append(['Minimum', np.array(dataMin)]) if self.maxBox.isChecked(): results.append(['Maximum', np.array(dataMax)]) if self.meanBox.isChecked(): results.append(['Mean', np.array(dataMean)]) if self.medianBox.isChecked(): results.append(['Median', np.array(dataMedian)]) if self.semBox.isChecked(): results.append(['SEM', np.array(dataSEM)]) if self.riseTimeBox.isChecked(): results.append(['RiseTime', np.array(dataRiseTime)]) if self.onsetBox.isChecked(): results.append(['Onset', np.array(dataOnset)]) if self.decayTimeBox.isChecked(): results.append(['Decay', np.array(dataDecay)]) aux.save_results(browser, 'Measurements', results)