def plot(self, xmin=None, xmax=None, ymin=None, ymax=None): """ Plots the vel by the spec. :param xmin: optional xmin on the spectrum :param xmax: optional xmax on the spectrum :param ymin: optional ymin on the spectrum :param ymax: optional ymax on the spectrum """ plt.cla() if not self.smoothed: smooth.smooth(self.spec) # smooth the function (hanning) if not already done # plt.ion() # fig, ax = plt.subplots() if not self.base: self.ax.plot(self.vel, self.res, linewidth=1) # ymin = min(self.smo) # if not baselined, use max/min of the smoothed spectrum values # ymax = max(self.smo) else: self.ax.plot(self.vel, self.res, linewidth=1) # ymin = min(self.res) # if already baselined, use max/min of residual values # ymax = max(self.smo) self.ax.axhline(y=0, dashes=[5, 5]) self.ax.set(xlabel="Velocity (km/s)", ylabel="Flux (mJy)") self.ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax)) self.fig.canvas.draw()
def __init__(self, filename): self.filename = 'A{:06}.fits'.format(filename) self.smoothed = False self.n = -1 self.m = [] self.vel = [] self.spec = [] self.freq = [] self.yfit = [] self.res = [] self.smo = [] self.rms = 0 self.__load() self.smo = smooth.smooth(self.spec) self.res = self.smo self.smoothed = True plt.ion() plt.rcParams["figure.figsize"] = (10, 6) self.fig = plt.figure() self.ax = self.fig.add_subplot() self.cid = None self.__plot() self.baseline() self.__plot() input('Press Enter to end Baseline.')
def func(self, browser): """ Smooth traces Options: 1) Window type 2) Window length """ ############################################ # ANALYSIS FUNCTION # Get options window = str(self.comboBox.currentText()) try: window_len = float(self.smoothLength.text()) except ValueError: aux.error_box('Invalid Window Length') 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' # Smooth data results, itemsToPlot = [], [] for item in plotWidget.plotDataItems: # Copy attributes and add some new ones attrs = item.attrs attrs['smooth_window_type'] = window attrs['smooth_window_length'] = window_len # Smooth traceSmooth = smooth.smooth(item.data, window_len=window_len, window=window) results.append([item.text(0), traceSmooth, attrs]) # Store smoothed item smoothItem = aux.make_h5item('smooth', traceSmooth, item.attrs) itemsToPlot.append(smoothItem) # Plot results pgplot.plot_multipleData(browser, plotWidget, itemsToPlot, clear=False, color='#F2EF44') # Store results aux.save_results(browser, parentText + '_smooth', results)
def __plot(self, xmin=None, xmax=None, ymin=None, ymax=None): plt.cla() if not self.smoothed: self.res = smooth.smooth(self.spec) # smooth the function (hanning) if not already done self.ax.plot(self.vel, self.res, linewidth=1) self.ax.axhline(y=0, dashes=[5, 5]) self.ax.set(xlabel="Velocity (km/s)", ylabel="Flux (mJy)") self.ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax)) self.fig.canvas.draw()
def func(self, browser): """ Smooth traces Options: 1) Window type 2) Window length """ ############################################ # ANALYSIS FUNCTION # Get options window = str(self.comboBox.currentText()) try: window_len = float(self.smoothLength.text()) except ValueError: aux.error_box('Invalid Window Length') 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' # Smooth data results, itemsToPlot = [], [] for item in plotWidget.plotDataItems: # Copy attributes and add some new ones attrs = item.attrs attrs['smooth_window_type'] = window attrs['smooth_window_length'] = window_len # Smooth traceSmooth = smooth.smooth(item.data, window_len=window_len, window=window) results.append([item.text(0), traceSmooth, attrs]) # Store smoothed item smoothItem = aux.make_h5item('smooth', traceSmooth, item.attrs) itemsToPlot.append(smoothItem) # Plot results pgplot.plot_multipleData(browser, plotWidget, itemsToPlot, clear=False, color='#F2EF44') # Store results aux.save_results(browser, parentText+'_smooth', results)
def func(self, browser): """ Smooth traces Options: 1) Window type 2) Window length """ ############################################ # ANALYSIS FUNCTION # Get options window = str(self.comboBox.currentText()) window_len = float(self.smoothLength.text()) # 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' # Smooth data results = [] for item in plotWidget.plotDataItems: # Copy attributes and add some new ones attrs = item.attrs attrs['smooth_window_type'] = window attrs['smooth_window_length'] = window_len # Smooth traceSmooth = smooth.smooth(item.data, window_len=window_len, window=window) results.append([item.text(0), traceSmooth, attrs]) # Plot smoothed trace #x = np.arange(0, len(traceSmooth)*item.attrs['dt'], item.attrs['dt']) #plotWidget.plot(x, traceSmooth, pen=pg.mkPen('#F2EF44', width=1)) smoothItem = aux.make_h5item('smooth', traceSmooth, item.attrs) pgplot.browse_singleData(browser, plotWidget, smoothItem, clear=False, color='#F2EF44') # Store results aux.save_results(browser, parentText+'_smooth', 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 __init__(self, filename=None, smo=None, gauss=False, twopeak=False, trap=False, light_mode=False, vel=None, spec=None, rms=None, agc=None): self.base = True # for now self.smoothed = False self.boxcar = False # tracks if the spectrum has been boxcar smoothed self.n = 0 self.vel = [] self.spec = [] self.freq = [] self.yfit = [] self.res = [] # and smooth same variable self.rms = 0 self.w50 = 0 self.w50err = 0 self.w20 = 0 self.w20err = 0 self.vsys = 0 self.vsyserr = 0 self.flux = 0 self.fluxerr = 0 self.SN = 0 if not light_mode: plt.style.use('dark_background') plt.ion() plt.rcParams["figure.figsize"] = (10, 6) self.fig = plt.figure() self.ax = self.fig.add_subplot() self.cid = None if filename is not None: self.filename = 'A{:06}.fits'.format(filename) self.load() if smo is None: self.res = smooth.smooth(self.spec) else: self.res = smooth.smooth(self.spec, smooth_type=smo) self.boxcar = True else: self.vel = vel self.res = spec self.spec = spec self.rms = rms self.filename = 'A{:06}.fits'.format(int(agc)) self.smoothed = True self.plot() if filename is not None: self.calcRMS() if gauss: self.gauss() self.__write_file(self.__get_comments()) elif twopeak: self.twopeakfit() self.__write_file(self.__get_comments()) elif trap: self.trapezoidal_fit() self.__write_file(self.__get_comments()) else: input('Press Enter to Exit')
import numpy as np from analysis import smooth from console import utils as ndaq # Parameters posThs = 1.66 negThs = 1.62 final_smth_window = 100 # Get data and items data = ndaq.get_data() item = ndaq.get_items() dt = item[0].attrs['dt'] # Smooth data = smooth.smooth(data, window_len=4, window='hanning') # Threshold detection functions compPositive = lambda a, b: a > b compNegative = lambda a, b: a < b # Go through data i = 0 eStart, eEnd = [], [] while i < len(data): if compPositive(data[i], posThs): eStart.append(i) while i < len(data) and compPositive(data[i], posThs - 0.02): i += 1 eEnd.append(i - 1) elif compNegative(data[i], negThs):
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): """ 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 threshold = float(self.browser.ui.dataPlotsWidget.cursorThsPos) noiseSafety = float(self.eventNoiseSafety.text()) smoothFactor = float(self.eventSmooth.text()) direction = str(self.eventDirection.currentText()) c1, c2 = aux.get_cursors(self.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(self.browser.ui.dataPlotsWidget.plotDataItems, 'dt') dt = dtList[0] item = self.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(self.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 self.eventItemsIndex = listIndexes # Plot results self.show_events(data, np.array(xOnsets), np.array(yOnsets), dt)
import numpy as np from analysis import smooth from console import utils as ndaq # Parameters posThs = 1.66 negThs = 1.62 final_smth_window = 100 # Get data and items data = ndaq.get_data() item = ndaq.get_items() dt = item[0].attrs['dt'] # Smooth data = smooth.smooth(data, window_len=4, window='hanning') # Threshold detection functions compPositive = lambda a, b: a > b compNegative = lambda a, b: a < b # Go through data i = 0 eStart, eEnd = [], [] while i<len(data): if compPositive(data[i], posThs): eStart.append(i) while i<len(data) and compPositive(data[i], posThs-0.02): i+=1 eEnd.append(i-1) elif compNegative(data[i], negThs):