def FPCA(DataDict, SmoothFac=10, k=3): """ """ #import function specific modules from scipy.interpolate import UnivariateSpline # if 'time' is a pandas DatetimeIndex, convert to days as type(float) if isinstance(DataDict['time'], pd.DatetimeIndex): x = np.array((DataDict['time']-DataDict['time'][0]) / np.timedelta64(1, 'D')) else: x = DataDict['time'] NumRealizations, NumObs, NumResponses = np.shape(DataDict['data']) coef = np.zeros_like(DataDict['data']) for ir in range(NumResponses): for ireal in range(NumRealizations): y = PriorFlows['data'][0, :, 0] spl = UnivariateSpline(x, y, s=SmoothFac, k=k) if ireal == 0: ncoef = len(spl.get_coeffs()) coef = np.zeros((NumRealizations,ncoef,NumResponses)) coef[ireal, :, ir] = spl.get_coeffs() "Not completed - should be redefined completely"
class ValueFunctionSpline: def __init__(self,X,y,k,sigma,beta): self.sigma = sigma self.beta = beta if sigma == 1.0: y = np.exp((1-beta)*y) else: y = ((1-beta)*(1.0-sigma)*y)**(1.0/(1.0-sigma)) self.f = UnivariateSpline(X,y,k=k,s=0) def fit(self,X,y,k): if self.sigma == 1.0: y = np.exp((1-self.beta)*y) else: y = ((1-self.beta)*(1.0-self.sigma)*y)**(1.0/(1.0-self.sigma)) self.f = UnivariateSpline(X,y,k=k,s=0)#.fit(X,y,k) def getCoeffs(self): return self.f.get_coeffs() def __call__(self,X,d = None): if d==None: if self.sigma == 1.0: return np.log(self.f(X))/(1-self.beta) else: return (self.f(X)**(1.0-self.sigma))/((1.0-self.sigma)*(1-self.beta)) if d==1: return self.f(X)**(-self.sigma) * self.f(X,1)/(1-self.beta) raise Exception('Error: d must equal None or 1')
def splinefit(x, y, degree): results = {} # print len(x), " ", len(y) s = UnivariateSpline(x, y, s=degree) # spline Coefficients results['spline'] = s.get_coeffs() # fit values, and mean yhat = s(x) # display2(x, y, yhat, 1) ybar = sum(y) / len(y) results['residual'] = rss = s.get_residual() # also can be calcualte below # for i in range(0, len(y)): # rss += (y[i] - yhat[i]) ** 2 sstot = sum([(yi - ybar)**2 for yi in y]) ssreg = sstot - rss results['determination'] = ssreg / sstot return results
def testWiki(): yk3 = UnivariateSpline(xx, yy, k=2, s=0) knots = yk3.get_knots() coeffs = yk3.get_coeffs() #ipdb.set_trace() r1 = yk3(xxx) r2 = gety(xxx, xx, yy, coeffs) plt.plot(xx, yy, "k.", markerSize=10) plt.plot(xxx, r1, "g.-", markerSize=1) plt.plot(xxx, r2, "r.-", markerSize=1) plt.show(0)
class PolicyRulesSpline: def __init__(self,X,y,k): self.f = UnivariateSpline(X,y,k=k,s=0) def fit(self,X,y,k): self.f = UnivariateSpline(X,y,k=k,s=0)#.fit(X,y,k) def getCoeffs(self): return self.f.get_coeffs() def __call__(self,X,d = None): if d==None: return self.f(X) if d==1: return self.f(X,1) raise Exception('Error: d must equal None or 1')
def fit_and_plot_mpl_bspline(x,y): # Fit B-spline with matplotlib and plot it SPLINE_ORDER = 3 spl = UnivariateSpline(x,y,k=SPLINE_ORDER) plt.plot(x,y,'r') plt.plot(x,spl(x),'g') knots = spl.get_knots() coeffs = spl.get_coeffs() yr = plt.ylim() for knot in knots: plt.axvline(knot,color='g') #for coeff in coeffs: #plt.text(???,(yr[0]+yr[1])/2,"%f" % coeff) plt.show()
def df_model(self, ydata, parameters=None): """ Degrees of freedom used in the fit. """ if parameters is not None: self.set_parameters(parameters) std_estimation = np.std(ydata) if std_estimation == 0: weights = None else: weights = 1 / std_estimation * np.ones(len(ydata)) spline = UnivariateSpline(self.xdata, ydata[self.index_xdata], k=self.order, s=self.smoothing_factor, w=weights) return len(spline.get_coeffs())
class DualSplineSmoother(object): ''' claselfdocs ''' def __init__(self, yp, workdir, scale, sm=200): ''' Constructor ''' yp = np.array(yp) self.l = len(yp)/2 self.xPos = (self.l-2)/2 #fPos = (self.l-2)/2 + 2 tnsc = 2/scale print tnsc plt.rcParams['font.size'] = 24 plt.rcParams['lines.linewidth'] = 2.4 self.workdir = workdir avProfilePoints = yp[:self.l] self.avx = np.append(np.append([0], np.sort(np.tanh(tnsc*avProfilePoints[:self.xPos]))),[1]) self.av = avProfilePoints[self.xPos:] sigmaProfilePoints = yp[self.l:] self.sigmax = np.append(np.append([0], np.sort(np.tanh(tnsc*sigmaProfilePoints[:self.xPos]))),[1]) self.sigma = sigmaProfilePoints[self.xPos:] self.m = UnivariateSpline(self.avx, self.av) print "Created spline with " + str(len(self.m.get_knots())) + " knots" self.s = UnivariateSpline(self.sigmax, self.sigma) print "Created spline with " + str(len(self.s.get_knots())) + " knots" def saveSpline(self, filename): tp = np.linspace(0, 1, 1000) with open(filename ,"w+") as f: for i in range(0, 1000): f.write( str(tp[i]) + " , " + str(self.m(tp[i])) ) if i < 999: f.write("\n") f.close() def saveSigmaSpline(self, filename): tp = np.linspace(0, 1, 1000) with open(filename ,"w+") as f: for i in range(0, 1000): f.write( str(tp[i]) + " , " + str(self.s(tp[i])) ) if i < 999: f.write("\n") f.close() def showSpline(self, order=0): plt.clf() print "Spline full information:" print self.m.get_knots() print self.m.get_coeffs() print self.m.get_residual() tp = np.linspace(0, 1, 1000) plt.subplot(211) plt.scatter(self.avx,self.av) plt.plot(tp,self.m(tp)) plt.subplot(212) plt.scatter(self.sigmax,self.sigma) plt.plot(tp,self.s(tp)) plt.savefig(self.workdir+"/splineFit.pdf") if order > 0: plt.clf() plt.subplot(211) plt.plot(tp,self.m(tp,1)) plt.subplot(212) plt.plot(tp,self.s(tp,1)) plt.savefig(self.workdir+"/splineDerivative.pdf") def plotSplineData(self, dataContainer, yscale): plt.clf() plt.xlim(0,1) plt.ylim(0,yscale) tp = np.linspace(0, 1, 100) plt.scatter(dataContainer.points[0],dataContainer.points[1]+dataContainer.background, c='b', marker='o', s=5) plt.plot(tp, self.m(tp)+dataContainer.background,'r', linewidth=2) plt.plot(tp, self.m(tp)+np.sqrt(self.s(tp))+dataContainer.background,'r--', linewidth=2) plt.plot(tp, self.m(tp)-np.sqrt(self.s(tp))+dataContainer.background,'r--', linewidth=2) plt.plot(tp, np.zeros(100) + dataContainer.background, '--', c='#BBBBBB', alpha=0.8) plt.savefig(self.workdir+"/splineVsData.pdf") def plotBinnedData(self, dataContainer): plt.clf() tp = np.linspace(0, 1, dataContainer.numBins) tpHD = np.linspace(0, 1, 500) plt.plot(self.m(tpHD), self.s(tpHD)) plt.plot(dataContainer.avs, np.power(dataContainer.stds,2), 'o') plt.savefig(self.workdir+"/noiseVsBins.pdf") plt.clf() plt.plot(tpHD, self.m(tpHD),'r', linewidth=2) plt.plot(tp, dataContainer.avs, 'o') plt.savefig(self.workdir+"/splineVsBins.pdf") plt.clf() plt.plot(tpHD, self.s(tpHD),'r', linewidth=2) plt.plot(tp, np.power(dataContainer.stds,2), 'o') plt.savefig(self.workdir+"/spatialNoiseVsBins.pdf") def plotFisherInfo(self, dataContainer, ymax, ymaxsq): plt.clf() t = np.linspace(0, 1, 500) minf = lambda x: -1 * self.m(x) minx = fminbound(minf, 0, 1) fval = self.m(minx) noisemean = UnivariateSpline(self.m(t)/fval, self.s(t)/fval/fval) self.se = noisemean(0) fi = lambda a, sa, sp: 2*np.power(sa, 2)/ (np.power(a,2)*2*sa+np.power(sp,2)) fiapp = lambda a, sa, sp: sa / (np.power(a,2)) plt.xlim(0, 1) plt.ylim(0, ymaxsq) print 'whop whop' plt.plot(t, fi(self.m(t,1)/fval, self.s(t)/fval/fval-self.se, self.s(t, 1)/fval/fval)) plt.plot(t, fiapp(self.m(t,1)/fval, self.s(t)/fval/fval-self.se, self.s(t, 1)/fval/fval), 'r') plt.savefig(self.workdir+"/variance.pdf") plt.clf() plt.ylim(0, ymax) plt.plot(t, np.sqrt(fi(self.m(t,1)/fval, self.s(t)/fval/fval-self.se, self.s(t, 1)/fval/fval))) plt.plot(t, np.sqrt(fiapp(self.m(t,1)/fval, self.s(t)/fval/fval-self.se, self.s(t, 1)/fval/fval)), 'r') plt.savefig(self.workdir+"/stddev.pdf") plt.clf() plt.plot(t, 1/fi(self.m(t,1)/fval, self.s(t)/fval/fval-self.se, self.s(t, 1)/fval/fval)) plt.plot(t, 1/fiapp(self.m(t,1)/fval, self.s(t)/fval/fval-self.se, self.s(t, 1)/fval/fval), 'r') plt.savefig(self.workdir+"/fisherInfo.pdf")
class SplineMinizer(Minimizer): """ Spline Fitter. """ def __init__(self, k=3, s=None): self.k = k self.s = s # Set initalize parameters to zero. self.parameters = Parameters() for i in range(self.k + 1): self.parameters.add(name='c{}'.format(i), value=0) def _sorter(self, x, y=None, tol=1e-5): """sort x (and y) according to x values The spline call requires that x must be increasing. This function sorts x. If there are non-unique values, it adds a small numerical difference using tol. """ # Copy x to leave it unchanged. x_ = np.copy(x) # Get non-unique terms idx = np.arange(len(x_)) u, u_idx = np.unique(x, return_index=True) idx = np.delete(idx, u_idx) # Add noise to non-unique x_[idx] = x_[idx] + np.random.uniform(1, 9.9, size=len(idx)) * tol # Now sort x idx = np.argsort(x_) if y is None: return x_[idx] else: return x_[idx], y[idx] def function(self, x, *coefs): # Order of polynomial k = self.k # Number of coefficients n = k + 1 # Knot positions t_arr = np.zeros(n * 2, dtype=float) t_arr[:n] = min(x) t_arr[n:] = max(x) # Coefficients c_arr = np.zeros(n * 2, dtype=float) c_arr[:n] = coefs # Build Spline function tck = [t_arr, c_arr, k] model = UnivariateSpline._from_tck(tck) # Return predicted function return model(x) def predict(self, x): return self._spline(x) def transform(self, x, y): ymodel = self.predict(x) return (y - ymodel) + x def fit(self, x, y): # Sort values for fit x_, y_ = self._sorter(x, y) # Fit spline. self._spline = UnivariateSpline(x=x_, y=y_, k=self.k, s=self.s) for i, coef in enumerate(self._spline.get_coeffs()): if 'c{}'.format(i) in self.parameters: self.parameters['c{}'.format(i)].value = coef else: err = "\n\nspline fit failed. Try increasing the value\n" err += " of `s` when initializing the spline model.\n" raise RuntimeError(err)
from SetUp import *
class SplineSmoother(object): ''' claselfdocs ''' def __init__(self, yp, workdir, scale, sm=200): ''' Constructor ''' self.workdir = workdir yp = np.array(yp) self.l = len(yp) self.xPos = (self.l-2)/2 #fPos = (self.l-2)/2 + 2 tnsc = 2/scale print tnsc plt.rcParams['font.size'] = 24 plt.rcParams['lines.linewidth'] = 2.4 self.workdir = workdir self.avx = np.append(np.append([0], np.sort(np.tanh(tnsc*yp[:self.xPos]))),[1]) self.av = yp[self.xPos:] self.m = UnivariateSpline(self.avx, self.av) plt.rcParams['font.size'] = 24 plt.rcParams['lines.linewidth'] = 2.4 print "Created spline with " + str(len(self.m.get_knots())) + " knots" def saveSpline(self, filename): tp = np.linspace(0, 1, 1000) with open(filename ,"w+") as f: for i in range(0, 1000): f.write( str(tp[i]) + " , " + str(self.m(tp[i])) ) if i < 999: f.write("\n") f.close() def showSpline(self, order=0): plt.clf() print "Spline full information:" print self.m.get_knots() print self.m.get_coeffs() print self.m.get_residual() tp = np.linspace(0, 1, 1000) plt.scatter(self.avx, self.av) plt.plot(tp,self.m(tp)) plt.savefig(self.workdir+"/splineFit.pdf") if order > 0: plt.plot(tp,self.m(tp,1)) if order > 1: plt.plot(tp,self.m(tp,2)) plt.savefig(self.workdir+"/splineDerivative.pdf") def plotSplineData(self, dataContainer, s, p, se, yscale): plt.clf() plt.xlim(0,1) plt.ylim(0,yscale) tp = np.linspace(0, 1, 500) plt.scatter(dataContainer.points[0],dataContainer.points[1]+dataContainer.background, c='b', marker='o', s=5) plt.plot(tp, self.m(tp)+dataContainer.background,'r', linewidth=2) plt.plot(tp, self.m(tp)+np.sqrt(s*(p*self.m(tp)*self.m(tp)+self.m(tp)+se))+dataContainer.background,'r--', linewidth=2) plt.plot(tp, self.m(tp)-np.sqrt(s*(p*self.m(tp)*self.m(tp)+self.m(tp)+se))+dataContainer.background,'r--', linewidth=2) plt.plot(tp, np.zeros(500) + dataContainer.background, '--', c='#BBBBBB', alpha=0.8) plt.savefig(self.workdir+"/splineVsData.pdf") def plotBinnedData(self, dataContainer, s, p, se, xmin, xmax): plt.clf() sigma = lambda x: s * (p*x*x + x + se) t = np.linspace(xmin, xmax, 500) plt.xlim(xmin, xmax) plt.plot(t, sigma(t)) plt.plot(dataContainer.avs, np.power(dataContainer.stds,2), 'o') plt.savefig(self.workdir+"/noiseVsBins.pdf") plt.clf() tp = np.linspace(0, 1, dataContainer.numBins) plt.plot(tp, self.m(tp),'r', linewidth=2) plt.plot(tp, dataContainer.avs, 'o') plt.savefig(self.workdir+"/splineVsBins.pdf") def plotFisherInfo(self, dataContainer, s, p, se, ymax, ymaxsq): plt.clf() t = np.linspace(0, 1, 1000) minf = lambda x: -1 * self.m(x) minx = fminbound(minf, 0, 1) fval = self.m(minx) s = s/fval se = se/fval p = p*fval print fval, s, p, se fi = lambda m, mp: 2*np.power(s * (p * np.power(m,2) + m),2)/(np.power(mp,2) * (2 * s * (p * np.power(m,2) + m) + np.power(s * (2 * p * m + 1), 2))) fiapp = lambda m, mp: s * (p * np.power(m,2) + m) / np.power(mp,2) plt.xlim(0, 1) plt.ylim(0, ymaxsq) plt.plot(t, fi(self.m(t)/fval, self.m(t, 1)/fval)) plt.plot(t, fiapp(self.m(t)/fval, self.m(t, 1)/fval), 'r') plt.savefig(self.workdir+"/variance.pdf") plt.clf() plt.ylim(0, ymax) plt.plot(t, np.sqrt(fi(self.m(t)/fval, self.m(t, 1)/fval))) plt.plot(t, np.sqrt(fiapp(self.m(t)/fval, self.m(t, 1)/fval)), 'r') plt.savefig(self.workdir+"/stddev.pdf") plt.clf() plt.plot(t, 1/fi(self.m(t)/fval, self.m(t, 1)/fval)) plt.plot(t, 1/fiapp(self.m(t)/fval, self.m(t, 1)/fval), 'r') plt.savefig(self.workdir+"/fisherInfo.pdf") with open(self.workdir+"/variance.csv" ,"w+") as f: fisher=np.sqrt(fi(self.m(t)/fval, self.m(t, 1)/fval)) for i in range(0, 1000): f.write( str(t[i]) + " , " + str(fisher[i]) ) if i < 999: f.write("\n") f.close()
pl.figure() #Plot experimental data points and interpolated curve for k,perc in enumerate(percs): partition = [g(a,b) for a,b in zip(gs_PEG1k_add,gs_PEG200)] print perc,partition[k] pl.plot(0.01*perc,partition[k],'b',marker= 's',linestyle = '--', linewidth= 2.5, markersize= 4, markevery= 1) y=[.03,.06,.09,.12,.15] z=[2.106,1.70,1.367,1.209,1.231] x = interpolate.UnivariateSpline(y,z,k=3) xx = interpolate.UnivariateSpline(y,z,k=2) #xxx = interpolate.UnivariateSpline(y,z,k=1) yy=np.arange(0,.16,.01) zz=x(yy) zzz=xx(yy) #zzzz=xxx(yy) coeff = UnivariateSpline.get_coeffs(xx) print coeff # pl.plot(yy,zzz+1.0,'b',linestyle=':', linewidth=0.2)#label= labelss) # pl.plot(yy,zzz+0.6,'b',linestyle=':', linewidth=0.2)#label= labelss) # pl.plot(yy,zzz+0.2,'b',linestyle=':', linewidth=0.2)#label= labelss) pl.plot(yy,zzz,'b',linestyle=':', linewidth=0.2)#label= labelss) #Plot theory partition coeff curves for different fixed vals of deltaF and big polymer vol frac for i,phi_b in enumerate(phi_bs): phis = np.linspace(0.0, 1.0-phi_b-phi_water_min, 100) # for df in dfs: for j,df in enumerate(dfs): try: ps = [P(phi,phi_b,df) for phi in phis] except: continue resids = [abs(f(p,phi,phi_b,df)) for p,phi in zip(ps, phis)] max_resid = max(resids) print 'Largest residual for df=%f: %e' % (df, max_resid)
spl_0 = UnivariateSpline(hist_per_pe.bin_centers[220:511:1], hist_per_pe.data[10][220:511:1], s=0, k=3) ''' spl_0 = [] for i,pe in enumerate(pes): spl_0.append(UnivariateSpline(hist_per_pe.bin_centers[220:511:1], hist_per_pe.data[i][220:511:1], s=0,k=3)) print(pe,len(spl_0[-1].get_knots())) ''' #plt.figure(0) #plt.step(hist_per_pe.bin_centers[220:511:1],hist_per_pe.data[-1][220:511:1]) #plt.plot(xs, spl_0(xs), 'k-', lw=1) knt0 = spl_0.get_knots() wei0 = spl_0.get_coeffs() print(knt0) #knt0 = hist_per_pe.bin_centers[221:510:1] #print(knt0) #print(5./0) list_coef = [] for i, pe in enumerate(pes): if i > 99: continue #t = hist_per_pe.bin_centers[236:509:2] spl.append( LSQUnivariateSpline(hist_per_pe.bin_centers[220:511:1], hist_per_pe.data[i][220:511:1], knt0[1:-1])) list_coef.append(list(spl[-1].get_coeffs())) # Get the knots
def spline_anomaly(df, smooth=0.5, plot=False, B=False, turning=False, filtered=False, threshold=1): """ Accepts: a Pandas dataframe of shape: obmt rate w1_rate 1. float float float or equivalent. This dataframe should be a hit neighbourhood as generated by isolate_anomaly(). The function will work on larger datasets but there is nothing to be gained by splining these - and the time taken to do so would be significantly larger. Runs scipy's splining algorithms on the hit neighbourhoods to generate a spline to fit the data. Kwargs: smooth (float, default=0.5): smoothing factor for the generated splines. plot (bool, default=False): if True, generates a plot of the data and the spline. B (bool, default=False): if True, generates B splines. turning (bool, default=False): if True, plots all the turning points alongside a normal plot. filtered (bool, default=False): if True, plots the filterd turning points alongside a normal plot. threshold (float, default=1.0): the threshold for filtering turning points Returns: tuple of the arrays of knots and coefficients (in that order) of the fitted spline. """ # Create spline. spl = UnivariateSpline(df['obmt'], df['rate'] - df['w1_rate']) spl.set_smoothing_factor(smooth) knots, coeffs = (spl.get_knots(), spl.get_coeffs()) xs = np.linspace(df['obmt'].tolist()[0], df['obmt'].tolist()[-1], 10000) if B: spl = BSpline(knots, coeffs, 3) # Plot original data and spline. if plot or turning or filtered: plt.scatter(df['obmt'], df['rate'] - df['w1_rate']) plt.plot(xs, spl(xs)) if filtered or turning: # Calls get_turning_points() to isolate the turning points. if turning: turning_points = get_turning_points(df) elif filtered: turning_points = filter_turning_points(get_turning_points(df), threshold=threshold) plt.scatter(turning_points['obmt'], turning_points['rate'] - turning_points['w1_rate'], color='red') plt.show() return (knots, coeffs)
import numpy as np #from sklearn import decomposition import matplotlib matplotlib.use("cairo") import matplotlib.pyplot as plt plt.style.use('ggplot') # Use the ggplot style from scipy.interpolate import UnivariateSpline x = np.linspace(0.0, 1.0, 150) y = 2 * np.sin((x + 0.5) * 5) / (x + 0.1) s = UnivariateSpline(x, y, k=3) print(s.get_coeffs()) derivs = s.derivatives(0.0) print(derivs) xs = np.linspace(0.0, 1.0, 1000) ys = s(xs) ys2 = derivs[0] + xs * derivs[1] + np.power(xs, 2) * derivs[2] / 2 + np.power( xs, 3) * derivs[3] / 6 plt.plot(x, y, '.-', label='data') plt.plot(xs, ys, zorder=10, label='smoothed') plt.plot(xs, ys2, alpha=0.5, zorder=0, label='taylor') plt.ylim([-6, 12]) plt.legend() plt.savefig("spline_test.pdf")
5.78812583e+01, 9.40718450e+01 ]) ## Now get scipy's spline fit. k = 3 tck = interpolate.splrep(x_nodes, y_nodes, k=k, s=0) knots = tck[0] coeffs = tck[1] print('knot points=', knots) print('coefficients=', coeffs) ## Now try scipy's object-oriented version. The result is exactly the same as "tck": the knots are the ## same and the coeffs are the same, they are just queried in a different way. uspline = UnivariateSpline(x_nodes, y_nodes, s=0) uspline_knots = uspline.get_knots() uspline_coeffs = uspline.get_coeffs() ## Here are scipy's native spline evaluation methods. Again, "ytck" and "y_uspline" are exactly equal. ytck = interpolate.splev(x, tck) y_uspline = uspline(x) y_knots = uspline(knots) ## Now let's try our manually-calculated evaluation function. y_eval = bspleval(x, knots, coeffs, k, debug=False) plt.plot(x, ytck, label='tck') plt.plot(x, y_uspline, label='uspline') plt.plot(x, y_eval, label='manual') ## Next plot the knots and nodes. plt.plot(x_nodes, y_nodes, 'ko', markersize=7, label='input nodes') ## nodes
def draw(self,x_diag,y_diag,x_anti,y_anti,degree): s=UnivariateSpline(x_diag,y_diag,k=degree) coeffs_diag=s.get_coeffs()
class SplineSmoother(object): ''' claselfdocs ''' def __init__(self, yp, workdir, scale, sm=200): ''' Constructor ''' self.workdir = workdir yp = np.array(yp) self.l = len(yp) self.xPos = (self.l - 2) / 2 #fPos = (self.l-2)/2 + 2 tnsc = 2 / scale print tnsc plt.rcParams['font.size'] = 24 plt.rcParams['lines.linewidth'] = 2.4 self.workdir = workdir self.avx = np.append( np.append([0], np.sort(np.tanh(tnsc * yp[:self.xPos]))), [1]) self.av = yp[self.xPos:] self.m = UnivariateSpline(self.avx, self.av) plt.rcParams['font.size'] = 24 plt.rcParams['lines.linewidth'] = 2.4 print "Created spline with " + str(len(self.m.get_knots())) + " knots" def saveSpline(self, filename): tp = np.linspace(0, 1, 1000) with open(filename, "w+") as f: for i in range(0, 1000): f.write(str(tp[i]) + " , " + str(self.m(tp[i]))) if i < 999: f.write("\n") f.close() def showSpline(self, order=0): plt.clf() print "Spline full information:" print self.m.get_knots() print self.m.get_coeffs() print self.m.get_residual() tp = np.linspace(0, 1, 1000) plt.scatter(self.avx, self.av) plt.plot(tp, self.m(tp)) plt.savefig(self.workdir + "/splineFit.pdf") if order > 0: plt.plot(tp, self.m(tp, 1)) if order > 1: plt.plot(tp, self.m(tp, 2)) plt.savefig(self.workdir + "/splineDerivative.pdf") def plotSplineData(self, dataContainer, s, p, se, yscale): plt.clf() plt.xlim(0, 1) plt.ylim(0, yscale) tp = np.linspace(0, 1, 500) plt.scatter(dataContainer.points[0], dataContainer.points[1] + dataContainer.background, c='b', marker='o', s=5) plt.plot(tp, self.m(tp) + dataContainer.background, 'r', linewidth=2) plt.plot(tp, self.m(tp) + np.sqrt(s * (p * self.m(tp) * self.m(tp) + self.m(tp) + se)) + dataContainer.background, 'r--', linewidth=2) plt.plot(tp, self.m(tp) - np.sqrt(s * (p * self.m(tp) * self.m(tp) + self.m(tp) + se)) + dataContainer.background, 'r--', linewidth=2) plt.plot(tp, np.zeros(500) + dataContainer.background, '--', c='#BBBBBB', alpha=0.8) plt.savefig(self.workdir + "/splineVsData.pdf") def plotBinnedData(self, dataContainer, s, p, se, xmin, xmax): plt.clf() sigma = lambda x: s * (p * x * x + x + se) t = np.linspace(xmin, xmax, 500) plt.xlim(xmin, xmax) plt.plot(t, sigma(t)) plt.plot(dataContainer.avs, np.power(dataContainer.stds, 2), 'o') plt.savefig(self.workdir + "/noiseVsBins.pdf") plt.clf() tp = np.linspace(0, 1, dataContainer.numBins) plt.plot(tp, self.m(tp), 'r', linewidth=2) plt.plot(tp, dataContainer.avs, 'o') plt.savefig(self.workdir + "/splineVsBins.pdf") def plotFisherInfo(self, dataContainer, s, p, se, ymax, ymaxsq): plt.clf() t = np.linspace(0, 1, 1000) minf = lambda x: -1 * self.m(x) minx = fminbound(minf, 0, 1) fval = self.m(minx) s = s / fval se = se / fval p = p * fval print fval, s, p, se fi = lambda m, mp: 2 * np.power(s * (p * np.power(m, 2) + m), 2) / ( np.power(mp, 2) * (2 * s * (p * np.power(m, 2) + m) + np.power(s * (2 * p * m + 1), 2))) fiapp = lambda m, mp: s * (p * np.power(m, 2) + m) / np.power(mp, 2) plt.xlim(0, 1) plt.ylim(0, ymaxsq) plt.plot(t, fi(self.m(t) / fval, self.m(t, 1) / fval)) plt.plot(t, fiapp(self.m(t) / fval, self.m(t, 1) / fval), 'r') plt.savefig(self.workdir + "/variance.pdf") plt.clf() plt.ylim(0, ymax) plt.plot(t, np.sqrt(fi(self.m(t) / fval, self.m(t, 1) / fval))) plt.plot(t, np.sqrt(fiapp(self.m(t) / fval, self.m(t, 1) / fval)), 'r') plt.savefig(self.workdir + "/stddev.pdf") plt.clf() plt.plot(t, 1 / fi(self.m(t) / fval, self.m(t, 1) / fval)) plt.plot(t, 1 / fiapp(self.m(t) / fval, self.m(t, 1) / fval), 'r') plt.savefig(self.workdir + "/fisherInfo.pdf") with open(self.workdir + "/variance.csv", "w+") as f: fisher = np.sqrt(fi(self.m(t) / fval, self.m(t, 1) / fval)) for i in range(0, 1000): f.write(str(t[i]) + " , " + str(fisher[i])) if i < 999: f.write("\n") f.close()
def fit_trace(trace, deg=5): spl = UnivariateSpline(trace.sample_f, trace.trace_f, k=deg) return [deg, spl.get_coeffs().tolist(), spl.get_knots().tolist()]
import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import UnivariateSpline nItems = 500 x = np.linspace(-3, 3, nItems) y = np.exp(-x**2) + 0.1 * np.random.randn(nItems) plt.plot(x, y, 'ro', ms=5) smoothFactor = float(nItems) / 50.0 spl = UnivariateSpline(x, y, k = 2, s = smoothFactor) xs = np.linspace(-3, 3, 1000) plt.plot(xs, spl(xs), 'g', lw=3) coeffs = spl.get_coeffs() knots = spl.get_knots() print 'coeffs', coeffs print 'knots', knots knotsY = spl(knots) plt.plot(knots, knotsY, 'bo') #spl.set_smoothing_factor(0.5) #print 'coeffs', spl.get_coeffs() #print 'knots', spl.get_knots() #plt.plot(xs, spl(xs), 'b', lw=3) plt.show()