def plot(x, y_list, x_title = 'Time', plot_title = None, measurements=['ave']): """ This plots x vs y. it supports multiple y lists, but they all must be of the same length. args -> y_list should be a list of tuples. [(title, [values]), (title, [values]),...] -> x_title defaults to 'Time' -> plot_title defaults to filename '_summary' -> measurements is an array of strings. of additional calculations to plot on each graph - ave - - - max ^ / min V - bestfit,deg o """ print('Plotting : ', ','.join([y[0] for y in y_list])) # plt.ion() # make plotting interactive #CHECK THE ARGS # ensure they supplied some y_vals assert(y_list) assert(len(y_list) >= 1) assert(type(y_list[0]) is tuple) # ensure it's a list of tuples. #ensure all the arrays are of the same length data_lengths = [len(v[1]) for v in y_list] # get the lengths of all the datasets data_length_set = set(data_lengths) # make a set out of it, which has unique elems assert(len(data_length_set) == 1) # ensure there is only one unique length. assert(len(y_list[0][1]) == len(x)) # ensure x and y's match as well. # plot them! plt.figure() # start a new figure if(not plot_title): # set a title, default to current time. plot_title = time.strftime('%d %H:%M') plt.title(plot_title) cur_plot = 1 print('y_list is: {}'.format(y_list)) for y in y_list: y_title = y[0] # the y title y_vals = y[1] # the list of y values to be plotted plt.subplot(len(y_list),1,cur_plot) # initialize the plot plt.plot(x,y_vals) # plot the data for m in measurements: # plot stuff about each plot print(m) if(m is 'ave'): plt.plot(x, meanarray(y_vals), 'm--') if(m is 'max'): plt.plot(x, maxarray(y_vals), 'k^') if(m is 'min'): plt.plot(x, minarray(y_vals), 'kv') if('bestfit' in m): deg = m.split(',')[1] plt.plot(x, polyfit(x, y_vals, d=deg), 'mo') plt.ylabel(y_title) # set the title. I've had enough of unlabeled charts cur_plot = cur_plot +1 # go to the next chart plt.show()
def process_raw_data(filename = None, maxima = None, binsize=20, deg=3, delta=.004, xlabel='Time (s)', title = None, **kwargs): ''' Args: ======= -> maxima T/F deteck peaks as maxima or minima -> label This function is the main entry point. given a file it will do the whole processing... thing... this is mainly just to make it easier. this is very much dependant on the structuer of the datafile! If you change acquisition!!! change this!!!! ''' # ASSUMPTIONS!!!!!! change these. there may be more subtle assumptions # further in. inputV = 'Voltage_0' # input signal, to track phase changes outputV = 'Voltage_1' # output signal, to track particles x = 'X_Value' # time array, r = parse(filename=filename, **kwargs) # parse the datafile assert(inputV in r) # __ASSUMPTIONS__ we know this to be the input signal assert(outputV in r) # __ASSUMPTIONS__ we know this to be the output signal # add some useful info r['input ave'] = [mean(r[inputV])] * len(r[inputV]) r['output ave'] = [mean(r[outputV])] * len(r[outputV]) # ============================= ''' Smooth the output to remove large peaks resulting from rare noise events ''' r['smoothed output'] = moving_average(r[outputV], n=binsize) r['bin size'] = binsize # ============================= ''' remove instrumental drift. By fitting a polynomial to the output signal, then subtracting it. ''' r['polyfit to output'] = polyfit(r[x],r[outputV], d=deg) r['deg'] = deg r['output adjusted'] = r['smoothed output'] - r['polyfit to output'] # ============================== ''' Run the detect peak algorithm to count the peaks in the datafile. ''' mx, mn = find_peaks(r[x], r['output adjusted'], delta=delta) if maxima: r['particles'] = mx r['number of particles'] = len(mx) else: r['particles'] = mn r['number of particles'] = len(mn) r['max'] = maxima r['delta'] = delta # ============================= ''' The hard part is done. just plot the results. I want to produce one summary graph. that displays the processing pipeline and one that is just the results. ''' plots = [ ('Raw Output', r[outputV]), ('Smoothed Output', r['smoothed output']), ('Result', r['output adjusted']) ] plot(r[x], plots, x_title=xlabel, plot_title=title, measurements=[]) # ============================= # and we're done. return the results just for goodness sake. return r