def spline_coeff(lat, time): time = time[~np.isnan(lat)] time_diff = time[1:]-time[:-1] counter = 0 indeces_remotion = np.array([]) for i in range(0, time_diff.shape[0]): if time_diff[i] > 60: if counter < 5: slack = np.arange(i - counter, i + 1) indeces_remotion = np.append(indeces_remotion, slack) counter = 0 else: counter += 1 time = np.delete(time, indeces_remotion) lat = lat[~np.isnan(lat)] lat = np.delete(lat, indeces_remotion) if lat.shape[0] > 5: save_data = optimal_knots(lat, time) t2 = time[save_data[1:-1]] try2 = LSQUnivariateSpline(time, lat, t2, k=3) spl_coeff = try2.get_coeffs() knots = np.r_[(time[save_data[0]],) * (3 + 1), t2, (time[save_data[-1]-1],) * (3 + 1)] return [spl_coeff, knots] # in this case returns spline coeff and knots else: return ["not enough data", lat] # in this case returns original data
def calc_coeffs(self): coeff = [] for v, (g, kdata) in enumerate(zip(self.basegrid, self.knot_data)): ranges = [(slice(r[0], r[1], t[0] * 1j) if kd.used() else slice( (r[1] - r[0]) / 2.0, r[1], 1j)) for r, t, kd in zip(self.ranges, g[0], kdata)] fnc = self.calc_grid(ranges, g[1], g[2])[v] for n in range(len(ranges)): kd = self.knot_data[v, n] if not kd.used(): continue s = list(fnc.shape) s[n] = kd.num_coeffs() out = np.empty(s) s[n] = 1 x = np.mgrid[ranges[n]] for i in np.ndindex(tuple(s)): i = list(i) i[n] = slice(None) ss = LSQUnivariateSpline(x, fnc[i], kd.get_knots(), bbox=kd.get_bbox(), k=kd.get_order() - 1) out[i] = ss.get_coeffs() fnc = out coeff.append(fnc) return coeff
def b_ink(x, fnca, k): "return t, bcoef" ca = [] for fnc in fnca: t = [None] * len(x) for n in range(len(x)): cf = np.empty_like(fnc) s = list(fnc.shape) s[n] = 1 xx = x[n] xxe = xx[-1] xxe += (xxe - xx[-2]) * 0.1 kk = k[n] start = kk // 2 end = -(kk - start) knots = xx[start:end].copy() if kk % 2: knots[:-1] += knots[1:] knots[-1] += xx[end] knots *= 0.5 for idx in np.ndindex(tuple(s)): idx = list(idx) idx[n] = slice(None) #ss = InterpolatedUnivariateSpline(x[n], fnc[idx], bbox=(xx[0],xxe), k=k[n]-1) ss = LSQUnivariateSpline(xx, fnc[idx], knots, bbox=(xx[0], xxe), k=kk - 1) cf[idx] = ss.get_coeffs() t[n] = np.pad(knots, kk, 'constant', constant_values=(xx[0], xxe)) fnc = cf ca.append(fnc) return t, ca
def b_ink(x, fnca, k): "return t, bcoef" ca = [] for fnc in fnca: t = [None]*len(x) for n in range(len(x)): cf = np.empty_like(fnc) s = list(fnc.shape) s[n] = 1 xx = x[n] xxe = xx[-1] xxe += (xxe-xx[-2]) * 0.1 kk = k[n] start = kk // 2 end = -(kk - start) knots = xx[start:end].copy() if kk % 2: knots[:-1] += knots[1:] knots[-1] += xx[end] knots *= 0.5 for idx in np.ndindex(tuple(s)): idx = list(idx) idx[n] = slice(None) #ss = InterpolatedUnivariateSpline(x[n], fnc[idx], bbox=(xx[0],xxe), k=k[n]-1) ss = LSQUnivariateSpline(xx, fnc[idx], knots, bbox=(xx[0],xxe), k=kk-1) cf[idx] = ss.get_coeffs() t[n] = np.pad(knots, kk, 'constant', constant_values=(xx[0],xxe)) fnc = cf ca.append(fnc) return t, ca
def calc_coeffs(self): coeff = [] for v, (g, kdata) in enumerate(zip(self.basegrid, self.knot_data)): ranges = [(slice(r[0], r[1], t[0]*1j) if kd.used() else slice((r[1]-r[0])/2.0,r[1],1j)) for r, t, kd in zip(self.ranges,g[0],kdata)] fnc = self.calc_grid(ranges, g[1], g[2])[v] for n in range(len(ranges)): kd = self.knot_data[v,n] if not kd.used(): continue s = list(fnc.shape) s[n] = kd.num_coeffs() out = np.empty(s) s[n] = 1 x = np.mgrid[ranges[n]] for i in np.ndindex(tuple(s)): i = list(i) i[n] = slice(None) ss = LSQUnivariateSpline(x, fnc[i], kd.get_knots(), bbox=kd.get_bbox(), k=kd.get_order()-1) out[i] = ss.get_coeffs() fnc = out coeff.append(fnc) return coeff
def fit_cubic(self): """Fit (x,y) arrays using a cubic Spline approximation. It uses LSQUnivariateSpline from scipy.interpolate which uses order-1 knots. """ x = self.x y = self.y xmin,xmax = x.min(),x.max() npieces = self.order rn = (xmax - xmin)/npieces tn = [(xmin+rn*k) for k in range(1,npieces)] zu = LSQUnivariateSpline(x,y,tn) self.coeff = zu.get_coeffs() self.fx = lambda x: x self.evfunc = lambda p,x: zu(x) self.ier = None return
m = (xmin <= t) & (t <= xmax) m1 = ni.maximum_filter1d(m, 3) t1 = t[m1] spl = LSQUnivariateSpline(x1, y1, t1[1:-1], bbox = [t1[0], t1[-1]]) spl = LSQUnivariateSpline(x1, y1, t2[1:-3], bbox = [t2[0], t2[-3]]) coeffs = np.empty(len(t)+2, dtype="f") coeffs.fill(np.nan) if np.all(m1): coeffs[:] = spl.get_coeffs() else: coeffs[1:-1][m1] = spl.get_coeffs()[1:-1] coeffs_list.append([coeffs]) print len(spl.get_knots()), len(spl.get_coeffs()) plot(x1, y1) plot(xx, spl(xx)) aa = np.concatenate(coeffs_list, axis=0) aaf = np.empty_like(aa) tt = np.arange(aa.shape[0]) for ix in range(aa.shape[-1]):
y1 = np.concatenate( (np.zeros(shape=len(arg1) // 10), y1, np.zeros(shape=len(arg1) // 10))) s1 = psf_ref['sig_y'][i].flatten()[arg1] s1 = np.concatenate((np.average(s1) * np.ones(shape=len(arg1) // 10), s1, np.average(s1) * np.ones(shape=len(arg1) // 10))) ym1 = median_filter(y1, size=len(y1) // 20) res1 = y1 - ym1 rms1 = rmsd(y1, ym1) mask1 = abs(res1) > 3.0 * rms1 x2 = x1[~mask1] y2 = y1[~mask1] s2 = s1[~mask1] ya2 = uniform_filter1d(y2, size=len(y2) // 20) splines2 = LSQUnivariateSpline(x2, y2, t=knots, bbox=[xb, xe], k=deg2) tck_coeffs1 = np.append(tck_coeffs1, [splines2.get_coeffs()], axis=0) ax1 = plt.Axes( fig, [subfig_left[flag1 % 3], subfig_bottom[flag1 // 3], 0.25, 0.13]) fig.add_axes(ax1) ax1.errorbar(x1[mask1], y1[mask1], yerr=s1[mask1], fmt='r.', ms=6, zorder=0, alpha=0.5) ax1.errorbar(x2, y2, yerr=s2, fmt='.',