data_process.py
Go to the documentation of this file.
00001 import numpy as np
00002 from scipy import interpolate
00003 
00004 ##
00005 # filter a list given indices
00006 # @param alist a list
00007 # @param indices indices in that list to select
00008 def filter(alist, indices):    
00009     rlist = []
00010     for i in indices:
00011         rlist.append(alist[i])
00012     return rlist
00013 
00014 ##
00015 # Given a list of 1d time arrays, find the sequence that started first and
00016 # subtract all sequences from its first time recording.
00017 #
00018 # @param list_of_time_arrays a list of 1d arrays 
00019 # @return list_of_time_arrays adjusted so that time arrays would start at 0
00020 def equalize_times(list_of_time_arrays):
00021     start_times = []
00022     end_times = []
00023     for tarray in list_of_time_arrays:
00024         start_times.append(tarray[0,0])
00025         end_times.append(tarray[0,-1])
00026 
00027     #print start_times
00028     #print end_times
00029     #import pdb
00030     #pdb.set_trace()
00031     min_start = np.min(start_times)
00032     max_end = np.max(end_times)
00033 
00034     adjusted_list_of_time_arrays = []
00035     for tarray in list_of_time_arrays:
00036         adjusted_list_of_time_arrays.append(tarray - min_start)
00037 
00038     return adjusted_list_of_time_arrays, min_start, max_end
00039 
00040 ##
00041 # calc dx/dt
00042 # @param t matrix 1xn
00043 # @param x matrix mxn
00044 def gradient(t, x):
00045     #pdb.set_trace()
00046     dx = x[:, 2:] - x[:, 0:-2]
00047     dt = t[0, 2:] - t[0, 0:-2]
00048     dx_dt = np.multiply(dx, 1/dt)
00049     #pdb.set_trace()
00050     dx_dt = np.column_stack((dx_dt[:,0], dx_dt))
00051     dx_dt = np.column_stack((dx_dt, dx_dt[:,-1]))
00052     return dx_dt
00053 
00054 ##
00055 # 1D interpolation
00056 #
00057 # @param x 1xn mat x to interpolate from
00058 # @param y 1xn mat y to interpolate from
00059 # @param xquery 1xn mat of query x's 
00060 def interpolate_1d(x, y, xquery):
00061     try:
00062         x = x.A1
00063         y = y.A1
00064         xquery = xquery.A1
00065         minx = np.min(x)
00066         minx_query = np.min(xquery)
00067 
00068         maxx = np.max(x)
00069         maxx_querry = np.max(xquery)
00070 
00071         if minx_query <= minx:
00072             x = np.concatenate((np.array([minx_query-.01]), x))
00073             y = np.concatenate((np.array([y[0]]), y))
00074 
00075         if maxx <= maxx_querry:
00076             x = np.concatenate((x, np.array([maxx_querry+.01])))
00077             y = np.concatenate((y, np.array([y[-1]])))
00078 
00079         f = interpolate.interp1d(x, y)
00080         return f(xquery)
00081     except ValueError, e:
00082         pdb.set_trace()
00083         print e
00084 
00085 ##
00086 # Given a histogram with params, calculate 
00087 def histogram_get_bin_numb(n, min_index, bin_size, nbins):
00088     bin_numb = int(np.floor((n - min_index) / bin_size))
00089     if bin_numb == nbins:
00090         bin_numb = bin_numb - 1
00091     return bin_numb
00092 
00093 ##
00094 #
00095 #
00096 # @param index_list_list a list of list of indices to histogram by
00097 # @param elements_list_list a list of list of elements to place in histogram bins
00098 # @param bin_size size of bins in index_list_list units
00099 # @param min_index optional argument for mininum index to create histogram over
00100 # @param max_index optional argument for maximum index to create histogram over
00101 def histogram(index_list_list, elements_list_list, bin_size, min_index=None, max_index=None):
00102     if min_index is None:
00103         min_index = np.min(np.concatenate(index_list_list))
00104     if max_index is None:
00105         max_index = np.max(np.concatenate(index_list_list))
00106 
00107     index_range = (max_index - min_index) 
00108     nbins = int(np.ceil(index_range / bin_size))
00109     bins = []
00110     for i in range(nbins):
00111         bins.append([])
00112 
00113     #pdb.set_trace()
00114 
00115     #Each slice contains the data for one trial, idx is the trial number
00116     for trial_number, element_list_slice in enumerate(zip(*elements_list_list)):
00117         #Iterate by using the length of the first set of data in the given trial
00118         for i in range(len(element_list_slice[0])):
00119             bin_numb = histogram_get_bin_numb(index_list_list[trial_number][i], min_index, bin_size, nbins)
00120             elements = [el_list[i] for el_list in element_list_slice]
00121             if bin_numb < 0 or bin_numb > nbins:
00122                 continue
00123             bins[bin_numb].append(elements)
00124 
00125     return bins, np.arange(min_index, max_index, bin_size)        
00126 
00127 ##
00128 # smooth the data using a window with requested size.
00129 # 
00130 # This method is based on the convolution of a scaled window with the signal.
00131 # The signal is prepared by introducing reflected copies of the signal 
00132 # (with the window size) in both ends so that transient parts are minimized
00133 # in the begining and end part of the output signal.
00134 # 
00135 # output:
00136 #     the smoothed signal
00137 #     
00138 # example:
00139 # 
00140 # t=linspace(-2,2,0.1)
00141 # x=sin(t)+randn(len(t))*0.1
00142 # y=smooth(x)
00143 # 
00144 # see also: 
00145 # 
00146 # numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve
00147 # scipy.signal.lfilter
00148 # 
00149 # Copied from http://www.scipy.org/Cookbook/SignalSmooth
00150 # 
00151 # @param    x the input signal 
00152 # @param    window_len the dimension of the smoothing window; should be an odd integer
00153 # @param    window the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
00154 #           flat window will produce a moving average smoothing.
00155 # @return   the smoothed signal function
00156 def signal_smooth(x,window_len=11,window='hamming'):
00157 
00158     if x.ndim != 1:
00159         raise ValueError, "smooth only accepts 1 dimension arrays."
00160 
00161     if x.size < window_len:
00162         raise ValueError, "Input vector needs to be bigger than window size."
00163 
00164 
00165     if window_len<3:
00166         return x
00167 
00168 
00169     if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
00170         raise ValueError, "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'"
00171 
00172     s=np.r_[x[window_len:1:-1],x,x[-1:-window_len:-1]]
00173 
00174     # s=numpy.r_[2*x[0]-x[window_len:1:-1],x,2*x[-1]-x[-1:-window_len:-1]]
00175     #print(len(s))
00176     if window == 'flat': #moving average
00177         w=np.ones(window_len,'d')
00178     else:
00179         w=eval('np.'+window+'(window_len)')
00180 
00181     y=np.convolve(w/w.sum(),s,mode='same')
00182     return y[window_len-1:-window_len+1]
00183 
00184 ##
00185 # Returns the variance of the series x given mean function y
00186 # over a window of size window_len.
00187 # @param x the original signal
00188 # @param y the smoothed signal function
00189 # @param window_len size of the window to calculate variances over
00190 # @return the variance function
00191 def signal_variance(x, y, window_len=10):
00192     if len(x) != len(y):
00193         raise ValueError, "Must have same length"
00194 
00195     vars = []
00196     for i in range(len(x)):
00197         cursum = 0. 
00198         cura = i - window_len/2
00199         curb = i + window_len/2
00200         if cura < 0:
00201             cura = 0
00202         if curb > len(x):
00203             curb = len(x)
00204         for xval in x[cura:curb]:
00205             cursum += (xval - y[i])**2
00206         vars += [cursum / (curb-cura)]
00207     vars += [vars[len(vars)-1]]
00208     return vars
00209 
00210 ##
00211 # TODO docs
00212 # Returns the variance of the series x given mean function y
00213 # over a window of size window_len.
00214 # @param x the original signal
00215 # @param y the smoothed signal function
00216 # @param window_len size of the window to calculate variances over
00217 # @return the variance function
00218 def signal_list_variance(x_list, means, window_len=10, num_samples=30, resample=1):
00219     # if len(x_list[0]) != len(means):
00220     #     raise ValueError, "Must have same length"
00221 
00222     vars = []
00223     num_samples_in_mean = num_samples / len(x_list)
00224     for i in range(0, len(means), resample):
00225         cursum = 0.
00226         cura = i - window_len/2
00227         curb = i + window_len/2
00228         if cura < 0:
00229             cura = 0
00230         if curb > len(means):
00231             curb = len(means)
00232         step = (curb - cura) / num_samples_in_mean
00233         n = 0
00234         for x in x_list:
00235             if cura >= len(x):
00236                 continue
00237 
00238             ccurb = curb
00239             cstep = step
00240             if ccurb >= len(x):
00241                 ccurb = len(x)
00242                 cstep = (ccurb - cura) / num_samples_in_mean
00243             if cstep > 0:
00244                 for xval in x[cura:ccurb:cstep]:
00245                     cursum += (xval - means[i])**2
00246                     n += 1
00247         vars += [np.sqrt(cursum)/(n)]
00248     return np.array(vars)


hrl_lib
Author(s): Cressel Anderson, Travis Deyle, Advait Jain, Hai Nguyen, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:34:06