def find_intersection(x1,y1,x2,y2): #http://stackoverflow.com/questions/8094374/python-matplotlib-find-intersection-of-lineplots import scipy.interpolate as interpolate import scipy.optimize as optimize p1=interpolate.PiecewisePolynomial(x1,y1[:,np.newaxis]) p2=interpolate.PiecewisePolynomial(x2,y2[:,np.newaxis]) def pdiff(x): return p1(x)-p2(x) xs=np.r_[x1,x2] xs.sort() x_min=xs.min() x_max=xs.max() x_mid=xs[:-1]+np.diff(xs)/2 roots=set() for val in x_mid: root,infodict,ier,mesg = optimize.fsolve(pdiff,val,full_output=True) # ier==1 indicates a root has been found if ier==1 and x_min<root<x_max: roots.add(root[0]) roots=np.array(list(roots)) return roots,p1(roots)
def catmullrom(self): derivs = self.find_derivatives() u = numpy.arange(0.0, self.x.size) polys = [] polys.append( interp.PiecewisePolynomial(u, zip(self.x, derivs[0]), orders=3, direction=None)) polys.append( interp.PiecewisePolynomial(u, zip(self.y, derivs[1]), orders=3, direction=None)) return polys, u
def half_amp_dur(self, cluster): """ Half amplitude duration of a spike Parameters ---------- A: ndarray An nSpikes x nElectrodes x nSamples array Returns ------- had: float The half-amplitude duration for the channel (electrode) that has the strongest (highest amplitude) signal. Units are ms """ from scipy import interpolate, optimize waveforms = self.waveforms[self.cut == cluster, :, :] best_chan = np.argmax(np.max(np.mean(waveforms, 0), 1)) mn_wvs = np.mean(waveforms, 0) wvs = mn_wvs[best_chan, :] half_amp = np.max(wvs) / 2 half_amp = np.zeros_like(wvs) + half_amp t = np.linspace(0, 1 / 1000., 50) # create functions from the data using PiecewisePolynomial p1 = interpolate.PiecewisePolynomial(t, wvs[:, np.newaxis]) p2 = interpolate.PiecewisePolynomial(t, half_amp[:, np.newaxis]) xs = np.r_[t, t] xs.sort() x_min = xs.min() x_max = xs.max() x_mid = xs[:-1] + np.diff(xs) / 2 roots = set() for val in x_mid: root, infodict, ier, mesg = optimize.fsolve( lambda x: p1(x) - p2(x), val, full_output=True) if ier == 1 and x_min < root < x_max: roots.add(root[0]) roots = list(roots) if len(roots) > 1: r = np.abs(np.diff(roots[0:2]))[0] else: r = np.nan return r
def pla(data, period=15): N = int(len(data)/period) orig_x = range(0,len(data)) tck = splrep(orig_x, data,s=0) test_xs = np.linspace(0,len(data),N) spline_ys = splev(test_xs, tck) spline_yps = splev(test_xs, tck, der=1) xi = np.unique(tck[0]) yi = [[splev(x, tck, der=j) for j in xrange(3)] for x in xi] P = interpolate.PiecewisePolynomial(xi,yi,orders=1) test_ys = P(test_xs) #inter_y = interp0(test_xs, test_ys, orig_x) inter_y = interp1(test_xs, test_ys, orig_x) mae = sqrt(mean_absolute_error(inter_y, data)) # mae = np.var(inter_y-data) return mae