def baseline(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 """ plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.toolStackedWidget c1, c2 = aux.get_cursors(plotWidget) # Check selected options for option in toolsWidget.baselineToolOptions: if option.isChecked(): if option.text()=='Keep original data': aux.make_data_copy(browser, plotWidget) # Get the data now, in case we are working on a copy plotWidget.clear() data = aux.get_data(browser) # Get dt list dt = aux.get_attr(plotWidget.plotDataItems, 'dt')[0] # Make average between cursors and subract for each trace for item in plotWidget.plotDataItems: bsl = np.mean(item.data[c1/dt:c2/dt]) item.data = item.data - bsl # Re-plot data pgplot.replot(browser, plotWidget) pgplot.replot_cursors(browser, plotWidget)
def custom_func(browser): """ Temporary function for user defined analysis. All options are harcoded and results are saved in a "Analysis_results" folder in Root Additional items can be created by appending to the 'results' list in the forms ['name', data_to_be_stored, data_attributes] """ # Get data and widgets plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.toolStackedWidget # Get dt dt = aux.get_attr(plotWidget.plotDataItems, 'dt')[0] # Analyse data results = [] for item in plotWidget.plotDataItems: # Copy attributes and add some new ones attrs = item.attrs # USER DEFINED TRANSFORMATION USER_TRANSFORMED_DATA = item.data # Append to storage list results.append(['NAME', USER_TRANSFORMED_DATA, attrs]) # Plot modified trace if needed x = np.arange(0, len(USER_TRANSFORMED_DATA)*dt, dt) plotWidget.plot(x, USER_TRANSFORMED_DATA, pen=pg.mkPen('#F2EF44', width=1)) # Store results aux.save_results(browser, 'Analysis_results', results)
def baseline(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 """ plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.toolStackedWidget c1, c2 = aux.get_cursors(plotWidget) # Check selected options for option in toolsWidget.baselineToolOptions: if option.isChecked(): if option.text() == 'Keep original data': aux.make_data_copy(browser, plotWidget) # Get the data now, in case we are working on a copy plotWidget.clear() data = aux.get_data(browser) # Get dt list dt = aux.get_attr(plotWidget.plotDataItems, 'dt')[0] # Make average between cursors and subract for each trace for item in plotWidget.plotDataItems: bsl = np.mean(item.data[c1 / dt:c2 / dt]) item.data = item.data - bsl # Re-plot data pgplot.replot(browser, plotWidget) pgplot.replot_cursors(browser, plotWidget)
def measure_cursor_stats(browser): """ Measure selected properties or statistics in the region defined by the data cursors. Options: 1) create new entries in Working Data tree with the results """ plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.toolStackedWidget data = aux.get_data(browser) c1, c2 = aux.get_cursors(plotWidget) dataIndex = plotWidget.plotDataIndex saveData = False # Get dt list dtList = aux.get_attr(plotWidget.plotDataItems, 'dt') # Go through data and check selected values to measure # Can probably do this in a more efficient way results = [] for option in toolsWidget.measureToolOptions: if option.isChecked(): if option.text() == 'Store result': saveData = True if option.text() == 'Minimum': dataMin = [] for t in range(len(data)): dt = dtList[t] y = np.min(data[t][c1 / dt:c2 / dt]) #x = np.where(data[t][c1:c2]==y) x = np.argmin(data[t][c1 / dt:c2 / dt]) dataMin.append(y) aux.plot_point(plotWidget, c1 / dt, x, y, dt) results.append(['Minimum', dataMin]) if option.text() == 'Maximum': dataMax = [] for t in range(len(data)): dt = dtList[t] y = np.max(data[t][c1 / dt:c2 / dt]) #x = np.where(data[t][c1:c2]==y) x = np.argmax(data[t][c1 / dt:c2 / dt]) dataMax.append(y) aux.plot_point(plotWidget, c1 / dt, x, y, dt) results.append(['Maximum', dataMax]) if option.text() == 'Mean': dataMean = [] for t in range(len(data)): dt = dtList[t] y = np.mean(data[t][c1 / dt:c2 / dt]) dataMean.append(y) plotWidget.plot([c1, c2], [y, y], pen=pg.mkPen('#CF1C04', width=1)) results.append(['Mean', dataMean]) # Store results if saveData: aux.save_results(browser, 'Measurements', results)
def measure_cursor_stats(browser): """ Measure selected properties or statistics in the region defined by the data cursors. Options: 1) create new entries in Working Data tree with the results """ plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.toolStackedWidget data = aux.get_data(browser) c1, c2 = aux.get_cursors(plotWidget) dataIndex = plotWidget.plotDataIndex saveData = False # Get dt list dtList = aux.get_attr(plotWidget.plotDataItems, 'dt') # Go through data and check selected values to measure # Can probably do this in a more efficient way results = [] for option in toolsWidget.measureToolOptions: if option.isChecked(): if option.text()=='Store result': saveData = True if option.text()=='Minimum': dataMin = [] for t in range(len(data)): dt = dtList[t] y = np.min(data[t][c1/dt:c2/dt]) #x = np.where(data[t][c1:c2]==y) x = np.argmin(data[t][c1/dt:c2/dt]) dataMin.append(y) aux.plot_point(plotWidget, c1/dt, x, y, dt) results.append(['Minimum', dataMin]) if option.text()=='Maximum': dataMax = [] for t in range(len(data)): dt = dtList[t] y = np.max(data[t][c1/dt:c2/dt]) #x = np.where(data[t][c1:c2]==y) x = np.argmax(data[t][c1/dt:c2/dt]) dataMax.append(y) aux.plot_point(plotWidget, c1/dt, x, y, dt) results.append(['Maximum', dataMax]) if option.text()=='Mean': dataMean = [] for t in range(len(data)): dt = dtList[t] y = np.mean(data[t][c1/dt:c2/dt]) dataMean.append(y) plotWidget.plot([c1,c2], [y,y], pen=pg.mkPen('#CF1C04', width=1)) results.append(['Mean', dataMean]) # Store results if saveData: aux.save_results(browser, 'Measurements', results)
def average_traces(browser): """ Calculate average trace from currently plotted traces. Options: 1) create new entry in Working Data tree with the result 2) plot average with orginal traces """ plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.toolStackedWidget plotWidget.clear() data = aux.get_data(browser) # Get dt list (at this stage they will all be the same # because otherwise get_data would have thrown an error dt = aux.get_attr(plotWidget.plotDataItems, 'dt')[0] # Calculate average avgData = np.mean(data, 0) # Check selected options for option in toolsWidget.avgToolOptions: if option.isChecked(): if option.text() == 'Store result': results = [] # Get attributes from plotted items item = plotWidget.plotDataItems[0] attrs = item.attrs # Store data results.append(['avg_trace', avgData, attrs]) aux.save_results(browser, item.parent().text(0) + '_average', results) if option.text() == 'Show traces': pgplot.replot(browser, plotWidget) # Plot average x = np.arange(0, len(avgData) * dt, dt) plotWidget.plot(x, avgData, pen=pg.mkPen('#CF1C04', width=2)) if browser.ui.actionShowCursors.isChecked(): pgplot.replot_cursors(browser, plotWidget)
def average_traces(browser): """ Calculate average trace from currently plotted traces. Options: 1) create new entry in Working Data tree with the result 2) plot average with orginal traces """ plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.toolStackedWidget plotWidget.clear() data = aux.get_data(browser) # Get dt list (at this stage they will all be the same # because otherwise get_data would have thrown an error dt = aux.get_attr(plotWidget.plotDataItems, 'dt')[0] # Calculate average avgData = np.mean(data,0) # Check selected options for option in toolsWidget.avgToolOptions: if option.isChecked(): if option.text()=='Store result': results = [] # Get attributes from plotted items item = plotWidget.plotDataItems[0] attrs = item.attrs # Store data results.append(['avg_trace', avgData, attrs]) aux.save_results(browser, item.parent().text(0)+'_average', results) if option.text()=='Show traces': pgplot.replot(browser, plotWidget) # Plot average x = np.arange(0, len(avgData)*dt, dt) plotWidget.plot(x, avgData, pen=pg.mkPen('#CF1C04', width=2)) if browser.ui.actionShowCursors.isChecked(): pgplot.replot_cursors(browser, plotWidget)
def custom_func(browser): """ Temporary function for user defined analysis. All options are harcoded and results are saved in a "Analysis_results" folder in Root Additional items can be created by appending to the 'results' list in the forms ['name', data_to_be_stored, data_attributes] """ # Get data and widgets plotWidget = browser.ui.dataPlotsWidget toolsWidget = browser.ui.toolStackedWidget # Get dt dt = aux.get_attr(plotWidget.plotDataItems, 'dt')[0] # Analyse data results = [] for item in plotWidget.plotDataItems: # Copy attributes and add some new ones attrs = item.attrs # USER DEFINED TRANSFORMATION USER_TRANSFORMED_DATA = item.data # Append to storage list results.append(['NAME', USER_TRANSFORMED_DATA, attrs]) # Plot modified trace if needed x = np.arange(0, len(USER_TRANSFORMED_DATA) * dt, dt) plotWidget.plot(x, USER_TRANSFORMED_DATA, pen=pg.mkPen('#F2EF44', width=1)) # Store results aux.save_results(browser, 'Analysis_results', results)
def event_detect(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. """ # Read detection options threshold = float(browser.ui.dataPlotsWidget.cursorThsPos) noiseSafety = float(browser.ui.toolStackedWidget.eventNoiseSafety.text()) smoothFactor = float(browser.ui.toolStackedWidget.eventSmooth.text()) direction = str(browser.ui.toolStackedWidget.eventDirection.currentText()) c1, c2 = aux.get_cursors(browser.ui.dataPlotsWidget) # Ensure that noise safety has the same sign as the threshold noiseSafety = np.sign(threshold) * abs(noiseSafety) # Get dt list and attrs for use in concatenated data dtList = aux.get_attr(browser.ui.dataPlotsWidget.plotDataItems, 'dt') dt = dtList[0] item = browser.ui.dataPlotsWidget.plotDataItems[0] attrs = item.attrs # Get data currently plotted within the cursors and concatenate in a single sweep data = aux.get_data(browser) if browser.ui.dataPlotsWidget.cursor1Pos: data = data[:, c1 / dt:c2 / dt] data = data.ravel() # Smooth original_data = data if smoothFactor > 1: data = smooth.smooth(data, window_len=smoothFactor, window='hanning') # Run detection if direction == 'negative': comp = lambda a, b: a < b elif direction == 'positive': comp = lambda a, b: a > b eventCounter, i = 0, 0 xOnsets, yOnsets = [], [] while i < len(data): if comp(data[i], threshold): xOnsets.append(i) yOnsets.append(data[i]) eventCounter += 1 while i < len(data) and comp(data[i], (threshold - noiseSafety)): i += 1 # skip values if index in bounds AND until the value is below/above threshold again 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(original_data), attrs]) results.append(['onsets', np.array(xOnsets)]) results.append(['peaks', np.array(yOnsets)]) results.append(['number', np.array([eventCounter])]) results.append(['frequency', np.array([frequency])]) listIndexes = aux.save_results(browser, 'Event_Detection', results) # Store list indexes temporarily in stack widget list for further event analysis browser.ui.toolStackedWidget.eventItemsIndex = listIndexes # browser.ui.toolStackedWidget.eventData.append(np.array(original_data)) # browser.ui.toolStackedWidget.eventData.append(np.array(data)) # browser.ui.toolStackedWidget.eventData.append(np.array(xOnsets)) # browser.ui.toolStackedWidget.eventData.append(dt) # Plot results show_events(browser, data, np.array(xOnsets), np.array(yOnsets), dt)
def event_detect(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. """ # Read detection options threshold = float(browser.ui.dataPlotsWidget.cursorThsPos) noiseSafety = float(browser.ui.toolStackedWidget.eventNoiseSafety.text()) smoothFactor = float(browser.ui.toolStackedWidget.eventSmooth.text()) direction = str(browser.ui.toolStackedWidget.eventDirection.currentText()) c1, c2 = aux.get_cursors(browser.ui.dataPlotsWidget) # Ensure that noise safety has the same sign as the threshold noiseSafety = np.sign(threshold) * abs(noiseSafety) # Get dt list and attrs for use in concatenated data dtList = aux.get_attr(browser.ui.dataPlotsWidget.plotDataItems, 'dt') dt = dtList[0] item = browser.ui.dataPlotsWidget.plotDataItems[0] attrs = item.attrs # Get data currently plotted within the cursors and concatenate in a single sweep data = aux.get_data(browser) if browser.ui.dataPlotsWidget.cursor1Pos: data = data[:,c1/dt:c2/dt] data = data.ravel() # Smooth original_data = data if smoothFactor > 1: data = smooth.smooth(data, window_len=smoothFactor, window='hanning') # Run detection if direction=='negative': comp = lambda a, b: a < b elif direction=='positive': comp = lambda a, b: a > b eventCounter,i = 0,0 xOnsets, yOnsets = [], [] while i<len(data): if comp(data[i],threshold): xOnsets.append(i) yOnsets.append(data[i]) eventCounter +=1 while i<len(data) and comp(data[i],(threshold-noiseSafety)): i+=1 # skip values if index in bounds AND until the value is below/above threshold again 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(original_data), attrs]) results.append(['onsets', np.array(xOnsets)]) results.append(['peaks', np.array(yOnsets)]) results.append(['number', np.array([eventCounter])]) results.append(['frequency', np.array([frequency])]) listIndexes = aux.save_results(browser, 'Event_Detection', results) # Store list indexes temporarily in stack widget list for further event analysis browser.ui.toolStackedWidget.eventItemsIndex = listIndexes # browser.ui.toolStackedWidget.eventData.append(np.array(original_data)) # browser.ui.toolStackedWidget.eventData.append(np.array(data)) # browser.ui.toolStackedWidget.eventData.append(np.array(xOnsets)) # browser.ui.toolStackedWidget.eventData.append(dt) # Plot results show_events(browser, data, np.array(xOnsets), np.array(yOnsets), dt)