def check_4(self,f=f1,per=0,s=0,a=0,b=2*pi,N=20,xb=None,xe=None, ia=0,ib=2*pi,dx=0.2*pi): if xb is None: xb=a if xe is None: xe=b x=a+(b-a)*arange(N+1,dtype=float)/float(N) # nodes x1=a+(b-a)*arange(1,N,dtype=float)/float(N-1) # middle points of the nodes v,v1=f(x),f(x1) nk=[] put(" u = %s N = %d"%(repr(round(dx,3)),N)) put(" k : [x(u), %s(x(u))] Error of splprep Error of splrep "%(f(0,None))) for k in range(1,6): tckp,u=splprep([x,v],s=s,per=per,k=k,nest=-1) tck=splrep(x,v,s=s,per=per,k=k) uv=splev(dx,tckp) err1 = abs(uv[1]-f(uv[0])) err2 = abs(splev(uv[0],tck)-f(uv[0])) assert_(err1 < 1e-2) assert_(err2 < 1e-2) put(" %d : %s %.1e %.1e"%\ (k,repr([round(z,3) for z in uv]), err1, err2)) put("Derivatives of parametric cubic spline at u (first function):") k=3 tckp,u=splprep([x,v],s=s,per=per,k=k,nest=-1) for d in range(1,k+1): uv=splev(dx,tckp,d) put(" %s "%(repr(uv[0])))
def check_4(self,f=f1,per=0,s=0,a=0,b=2*pi,N=20,xb=None,xe=None, ia=0,ib=2*pi,dx=0.2*pi): if xb is None: xb = a if xe is None: xe = b x = a+(b-a)*arange(N+1,dtype=float)/float(N) # nodes x1 = a + (b-a)*arange(1,N,dtype=float)/float(N-1) # middle points of the nodes v,v1 = f(x),f(x1) put(" u = %s N = %d" % (repr(round(dx,3)),N)) put(" k : [x(u), %s(x(u))] Error of splprep Error of splrep " % (f(0,None))) for k in range(1,6): tckp,u = splprep([x,v],s=s,per=per,k=k,nest=-1) tck = splrep(x,v,s=s,per=per,k=k) uv = splev(dx,tckp) err1 = abs(uv[1]-f(uv[0])) err2 = abs(splev(uv[0],tck)-f(uv[0])) assert_(err1 < 1e-2) assert_(err2 < 1e-2) put(" %d : %s %.1e %.1e" % (k,repr([round(z,3) for z in uv]), err1, err2)) put("Derivatives of parametric cubic spline at u (first function):") k = 3 tckp,u = splprep([x,v],s=s,per=per,k=k,nest=-1) for d in range(1,k+1): uv = splev(dx,tckp,d) put(" %s " % (repr(uv[0])))
def fitEdge(og,e,VERBOSE=False): path = og[e[0]][e[1]]['wpath'] #pstart=og.node[e[0]]['wcrd'] #pend = og.node[e[1]]['wcrd'] #path.extend([pend]) #p = [pstart] #p.extend(path) ae = np.array(path) if( ae.shape[0] > 3 ): # there are not enough points to fit with if( VERBOSE ): print("refitting edge with %d points"%len(path)) s = ae.shape[0]/2.0 fit2 = splprep(ae.transpose(),task=0,full_output =1, s=s)[0] u = np.array(list(range(ae.shape[0]+1))).\ astype(np.float64)/(ae.shape[0]) # location of spline points og[e[0]][e[1]]['d0'] = splev(u,fit2[0],der=0) # first derivative (tangent) of spline og[e[0]][e[1]]['d1'] =np.array( splev(u,fit2[0],der=1)) # second derivative (curvature) of spline og[e[0]][e[1]]['d2'] = np.array(splev(u,fit2[0],der=2)) else: if( VERBOSE ): print("no or insufficient points to fit") og[e[0]][e[1]]['d0'] = None og[e[0]][e[1]]['d1'] = None og[e[0]][e[1]]['d2'] = None
def fitEdge(og, e, VERBOSE=False): path = og[e[0]][e[1]]['wpath'] #pstart=og.node[e[0]]['wcrd'] #pend = og.node[e[1]]['wcrd'] #path.extend([pend]) #p = [pstart] #p.extend(path) ae = np.array(path) if (ae.shape[0] > 3): # there are not enough points to fit with if (VERBOSE): print("refitting edge with %d points" % len(path)) s = ae.shape[0] / 2.0 fit2 = splprep(ae.transpose(), task=0, full_output=1, s=s)[0] u = np.array(list(range(ae.shape[0]+1))).\ astype(np.float64)/(ae.shape[0]) # location of spline points og[e[0]][e[1]]['d0'] = splev(u, fit2[0], der=0) # first derivative (tangent) of spline og[e[0]][e[1]]['d1'] = np.array(splev(u, fit2[0], der=1)) # second derivative (curvature) of spline og[e[0]][e[1]]['d2'] = np.array(splev(u, fit2[0], der=2)) else: if (VERBOSE): print("no or insufficient points to fit") og[e[0]][e[1]]['d0'] = None og[e[0]][e[1]]['d1'] = None og[e[0]][e[1]]['d2'] = None
def test_1d_shape(self): x = [1,2,3,4,5] y = [4,5,6,7,8] tck = splrep(x, y) z = splev([1], tck) assert_equal(z.shape, (1,)) z = splev(1, tck) assert_equal(z.shape, ())
def test_1d_shape(self): x = [1,2,3,4,5] y = [4,5,6,7,8] tck = splrep(x, y) z = splev([1], tck) assert_equal(z.shape, (1,)) z = splev(1, tck) assert_equal(z.shape, ())
def test_2d_shape(self): x = [1, 2, 3, 4, 5] y = [4, 5, 6, 7, 8] tck = splrep(x, y) t = np.array([[1.0, 1.5, 2.0, 2.5], [3.0, 3.5, 4.0, 4.5]]) z = splev(t, tck) z0 = splev(t[0], tck) z1 = splev(t[1], tck) assert_equal(z, np.row_stack((z0, z1)))
def test_2d_shape(self): x = [1, 2, 3, 4, 5] y = [4, 5, 6, 7, 8] tck = splrep(x, y) t = np.array([[1.0, 1.5, 2.0, 2.5], [3.0, 3.5, 4.0, 4.5]]) z = splev(t, tck) z0 = splev(t[0], tck) z1 = splev(t[1], tck) assert_equal(z, np.row_stack((z0, z1)))
def test_splantider_vs_splint(self): # Check antiderivative vs. FITPACK spl2 = splantider(self.spl) # no extrapolation, splint assumes function is zero outside # range xx = np.linspace(0, 1, 20) for x1 in xx: for x2 in xx: y1 = splint(x1, x2, self.spl) y2 = splev(x2, spl2) - splev(x1, spl2) assert_allclose(y1, y2)
def test_splantider_vs_splint(self): # Check antiderivative vs. FITPACK spl2 = splantider(self.spl) # no extrapolation, splint assumes function is zero outside # range xx = np.linspace(0, 1, 20) for x1 in xx: for x2 in xx: y1 = splint(x1, x2, self.spl) y2 = splev(x2, spl2) - splev(x1, spl2) assert_allclose(y1, y2)
def test_splder_vs_splev(self): # Check derivative vs. FITPACK for n in range(3+1): # Also extrapolation! xx = np.linspace(-1, 2, 2000) if n == 3: # ... except that FITPACK extrapolates strangely for # order 0, so let's not check that. xx = xx[(xx >= 0) & (xx <= 1)] dy = splev(xx, self.spl, n) spl2 = splder(self.spl, n) dy2 = splev(xx, spl2) assert_allclose(dy, dy2)
def _loss(params, beams, x_nodes): import time from scipy.interpolate.fitpack import splev, splrep import pysynphot as S ### Spline continuum + gaussian line # l0 = 6563.*(1+params[1]) # if (l0 < 1.12e4) | (l0 > 1.63e4): # l0 = 1.e4 # # line = S.GaussianSource(params[2], l0, 10) tck = splrep(x_nodes, params, k=3, s=0) xcon = np.arange(0.9e4,1.8e4,0.01e4) ycon = splev(xcon, tck, der=0, ext=0) spec = S.ArraySpectrum(xcon, ycon, fluxunits='flam', keepneg=True)#+line lnprob = 0 dof = 0 for key in beams.keys(): beam = beams[key] modelf = beam.compute_model(beam.clip_thumb, xspec=spec.wave, yspec=spec.flux, in_place=False) lnprob += np.sum(((beam.cutout_scif-modelf)**2/beam.cutout_varf)[beam.cutout_maskf]) dof += beam.cutout_maskf.sum() print params, lnprob, lnprob/(dof-len(params)) #time.sleep(0.2) return lnprob
def _loss(params, beams, x_nodes): import time from scipy.interpolate.fitpack import splev, splrep import pysynphot as S ### Spline continuum + gaussian line # l0 = 6563.*(1+params[1]) # if (l0 < 1.12e4) | (l0 > 1.63e4): # l0 = 1.e4 # # line = S.GaussianSource(params[2], l0, 10) tck = splrep(x_nodes, params, k=3, s=0) xcon = np.arange(0.9e4, 1.8e4, 0.01e4) ycon = splev(xcon, tck, der=0, ext=0) spec = S.ArraySpectrum(xcon, ycon, fluxunits='flam', keepneg=True) #+line lnprob = 0 dof = 0 for key in beams.keys(): beam = beams[key] modelf = beam.compute_model(beam.clip_thumb, xspec=spec.wave, yspec=spec.flux, in_place=False) lnprob += np.sum(((beam.cutout_scif - modelf)**2 / beam.cutout_varf)[beam.cutout_maskf]) dof += beam.cutout_maskf.sum() print params, lnprob, lnprob / (dof - len(params)) #time.sleep(0.2) return lnprob
def __init__(self, knots_x, knots_y, n_points): self.knots_x = knots_x self.knots_y = knots_y tck = splrep(knots_x, knots_y) self.spline_x = np.linspace(knots_x[0], knots_x[-1], n_points) self.spline_y = splev(self.spline_x, tck)
def _obj_beam_fit(params, beams, x_nodes): """ params = [2.953e-03, 1.156e+00, 1.297e+01, 9.747e-01 , 9.970e-01 , 8.509e-01, 1.076e+00 , 1.487e+00 , 7.864e-01, 1.072e+00 , 1.015e+00] """ import time from scipy.interpolate.fitpack import splev, splrep import pysynphot as S ### Spline continuum + gaussian line l0 = 6563.*(1+params[1]) if (l0 < 1.12e4) | (l0 > 1.63e4): return -np.inf line = S.GaussianSource(params[2], l0, 10) tck = splrep(x_nodes, params[3:], k=3, s=0) xcon = np.arange(0.9e4,1.8e4,0.01e4) ycon = splev(xcon, tck, der=0, ext=0) spec = S.ArraySpectrum(xcon, ycon, fluxunits='flam', keepneg=True)+line lnprob = 0 for key in beams.keys(): beam = beams[key] modelf = beam.compute_model(beam.clip_thumb, xspec=spec.wave, yspec=spec.flux, in_place=False) lnprob += -0.5*np.sum(((beam.cutout_scif-params[0]-modelf)**2/beam.cutout_varf)[beam.cutout_maskf]) if ~np.isfinite(lnprob): lnprob = -np.inf print params, lnprob #time.sleep(0.2) return lnprob
def check_3(self, f=f1, per=0, s=0, a=0, b=2 * pi, N=20, xb=None, xe=None, ia=0, ib=2 * pi, dx=0.2 * pi): if xb is None: xb = a if xe is None: xe = b x = a + (b - a) * arange(N + 1, dtype=float) / float(N) # nodes v = f(x) put(" k : Roots of s(x) approx %s x in [%s,%s]:" % (f(None), repr(round(a, 3)), repr(round(b, 3)))) for k in range(1, 6): tck = splrep(x, v, s=s, per=per, k=k, xe=xe) if k == 3: roots = sproot(tck) assert_allclose(splev(roots, tck), 0, atol=1e-10, rtol=1e-10) assert_allclose(roots, pi * array([1, 2, 3, 4]), rtol=1e-3) put(' %d : %s' % (k, repr(roots.tolist()))) else: assert_raises(ValueError, sproot, tck)
def __init__(self, knots_x, knots_y, n_points): self.knots_x = knots_x self.knots_y = knots_y tck = splrep(knots_x, knots_y) self.spline_x = np.linspace(knots_x[0], knots_x[-1], n_points) self.spline_y = splev(self.spline_x, tck)
def make_diff_func(self) : ''' everytime parameters are changed, the diffusion function must be REMADE !! ''' if self.difftype == 'const': self.diff = lambda y: zeros(len(y), float) + self.param[0] self.diff_u= lambda y: zeros(len(y), float) + 0. elif self.difftype == 'power': self.diff = lambda y: y**self.param[0] \ * (self.param[1]+self.param[2]*y+self.param[3]*y**2) self.diff_u= lambda y: self.param[0]*y**(self.param[0]-1.) \ * (self.param[1]+self.param[2]*y+self.param[3]*y**2) + \ y**self.param[0] * (self.param[2]+2.*self.param[3]*y) elif self.difftype == 'bspline': #create an interp object from scipy.interpolate.fitpack import splrep,splev #paramuval should be list of u values #param should be list of D(u) values #create the data of the cubic bspline: # no smoother so s = 0 self.splinedata = splrep(self.paramuval, self.param, xb = None, xe = None, s=0, k = 3, full_output = 0, quiet = 1) self.diff = lambda y: splev(y, self.splinedata, der = 0) self.diff_u = lambda y: splev(y, self.splinedata, der = 1) elif self.difftype == 'bspline1storder': #create an interp object from scipy.interpolate.fitpack import splrep,splev #paramuval should be list of u values #param should be list of D(u) values #create the data of the linear bspline: # no smoother so s = 0 self.splinedata = splrep(self.paramuval, self.param, xb = None, xe = None, s=0, k = 1, full_output = 0, quiet = 1) self.diff = lambda y: splev(y, self.splinedata, der = 0) self.diff_u = lambda y: splev(y, self.splinedata, der = 1) elif self.difftype == '1D2pint': #interpolation with 2 points (=piecewise linear) self.diff = GridUtils.GridFunc1D([self.paramuval],self.param) self.diff_u = None else: print ('Unknown diffusion type given', self.difftype) sys.exit() #value of diffusion is again in agreement with par self.modified = False
def inward_normals(self, positions=None): """Return unit-vectors facing inwards at the points specified in the positions variable (of all points, if not specified). Note that fractional positions are acceptable, as these values are calculated via spline interpolation.""" if positions is None: positions = numpy.arange(len(self.points)) tck, uout = self.to_spline() points = numpy.transpose(fitpack.splev(positions, tck)) first_der = numpy.transpose(fitpack.splev(positions, tck, 1)) inward_normals = numpy.empty_like(first_der) inward_normals[:, 0] = -first_der[:, 1] inward_normals[:, 1] = first_der[:, 0] if self.signed_area() > 0: inward_normals *= -1 inward_normals /= numpy.sqrt( (inward_normals**2).sum(axis=1))[:, numpy.newaxis] return inward_normals
def check_1(self, f=f1, per=0, s=0, a=0, b=2 * pi, N=20, at=0, xb=None, xe=None): if xb is None: xb = a if xe is None: xe = b x = a + (b - a) * arange(N + 1, dtype=float) / float(N) # nodes x1 = a + (b - a) * arange(1, N, dtype=float) / float( N - 1) # middle points of the nodes v, v1 = f(x), f(x1) nk = [] def err_est(k, d): # Assume f has all derivatives < 1 h = 1.0 / float(N) tol = 5 * h**(.75 * (k - d)) if s > 0: tol += 1e5 * s return tol for k in range(1, 6): tck = splrep(x, v, s=s, per=per, k=k, xe=xe) if at: t = tck[0][k:-k] else: t = x1 nd = [] for d in range(k + 1): tol = err_est(k, d) err = norm2(f(t, d) - splev(t, tck, d)) / norm2(f(t, d)) assert_(err < tol, (k, d, err, tol)) nd.append((err, tol)) nk.append(nd) put("\nf = %s s=S_k(x;t,c) x in [%s, %s] > [%s, %s]" % (f(None), repr(round(xb, 3)), repr(round(xe, 3)), repr(round( a, 3)), repr(round(b, 3)))) if at: str = "at knots" else: str = "at the middle of nodes" put(" per=%d s=%s Evaluation %s" % (per, repr(s), str)) put(" k : |f-s|^2 |f'-s'| |f''-.. |f'''-. |f''''- |f'''''") k = 1 for l in nk: put(' %d : ' % k) for r in l: put(' %.1e %.1e' % r) put('\n') k = k + 1
def position_getter(contour): from scipy.interpolate import fitpack l_points = numpy.linspace(begin, end, l_samples, endpoint=True) top_spline, bottom_spline = contour.axis_top_bottom_to_spline() c_points = len(contour.points) top_params = fitpack.splev(l_points, top_spline) % c_points bottom_params = fitpack.splev(l_points, bottom_spline) % c_points contour_spline, uout = contour.to_spline() top_points = numpy.transpose(fitpack.splev(top_params, contour_spline)) bottom_points = numpy.transpose( fitpack.splev(bottom_params, contour_spline)) mesh_points = numpy.empty((d_samples, l_samples, 2)) interp_points = numpy.linspace(0, 1, d_samples) for i, ((tx, ty), (bx, by)) in enumerate(zip(top_points, bottom_points)): mesh_points[:, i, 0] = numpy.interp(interp_points, [0, 1], [tx, bx]) mesh_points[:, i, 1] = numpy.interp(interp_points, [0, 1], [ty, by]) return mesh_points
def __call__(self, x, nu=0): xb, xe = self._data[3:5] xb += -1E-8 xe += 1E-8 x = np.asarray(x) y = fitpack.splev(x, self._eval_args, der=nu) valid = np.all(np.vstack([x <= xe, x >= xb]), axis=0) y[~valid] = np.nan return y
def compute_colors(N): xref = np.linspace(0, 1, CMRref.shape[0]) x = np.linspace(0, 1, N) cmap = np.zeros((N, 3)) for i in range(3): tck = splrep(xref, CMRref[:, i], s=0) # cubic spline (default) without smoothing cmap[:, i] = splev(x, tck) # Limit to range [0,1] cmap -= np.min(cmap) cmap /= np.max(cmap) return cmap
def test_splev_der_k(): # regression test for gh-2188: splev(x, tck, der=k) gives garbage or crashes # for x outside of knot range # test case from gh-2188 tck = (np.array([0., 0., 2.5, 2.5]), np.array([-1.56679978, 2.43995873, 0., 0.]), 1) t, c, k = tck x = np.array([-3, 0, 2.5, 3]) # an explicit form of the linear spline assert_allclose(splev(x, tck), c[0] + (c[1] - c[0]) * x / t[2]) assert_allclose(splev(x, tck, 1), (c[1] - c[0]) / t[2]) # now check a random spline vs splder np.random.seed(1234) x = np.sort(np.random.random(30)) y = np.random.random(30) t, c, k = splrep(x, y) x = [t[0] - 1., t[-1] + 1.] tck2 = splder((t, c, k), k) assert_allclose(splev(x, (t, c, k), k), splev(x, tck2))
def compute_colors(N): xref = np.linspace(0, 1, CMRref.shape[0]) x = np.linspace(0, 1, N) cmap = np.zeros((N, 3)) for i in range(3): tck = splrep(xref, CMRref[:, i], s=0) # cubic spline (default) without smoothing cmap[:, i] = splev(x, tck) # Limit to range [0,1] cmap -= np.min(cmap) cmap /= np.max(cmap) return cmap
def fitPath(self,maxiter=40, s=20000): """fits a least squares spline through the path find descriptions of maxiter and s from scipy documentation""" try: pth = path.getCrds() pp = [pth[:,0],pth[:,1],pth[:,2]] if( len(pp[0]) <= 3 ): # there are not enough points to fit with self.splinePoints = pp self.splineTangents = None self.splineCurvature = None return else: s = len(pp[0])/2.0 cont = 1 j=0 while( j < maxiter ): fit2 = splprep(pp,task=0,full_output =1, s=s)[0] u = na.array(range(pp[0].shape[0]+1)).\ astype(na.float64)/(pp[0].shape[0]) # location of spline points self.splinePoints = splev(u,fit2[0],der=0) # first derivative (tangent) of spline valsd = splev(u,fit2[0],der=1) self.splineTangents = na.array(valsd).astype(na.float64) # second derivative (curvature) of spline valsc = splev(u,fit2[0],der=2) self.splineCurvature = na.array(valsc).astype(na.float64) success = 1 # this doesn't make much sense if( success ): # how should I be determining success? break else: s = s*0.9 j += 1 return True except Exception, error: print "failed in fitPath()",error
def test_extrapolation_modes(self): # test extrapolation modes # * if ext=0, return the extrapolated value. # * if ext=1, return 0 # * if ext=2, raise a ValueError # * if ext=3, return the boundary value. x = [1,2,3] y = [0,2,4] tck = splrep(x, y, k=1) rstl = [[-2, 6], [0, 0], None, [0, 4]] for ext in (0, 1, 3): assert_array_almost_equal(splev([0, 4], tck, ext=ext), rstl[ext]) assert_raises(ValueError, splev, [0, 4], tck, ext=2)
def test_splev_der_k(): # regression test for gh-2188: splev(x, tck, der=k) gives garbage or crashes # for x outside of knot range # test case from gh-2188 tck = (np.array([0., 0., 2.5, 2.5]), np.array([-1.56679978, 2.43995873, 0., 0.]), 1) t, c, k = tck x = np.array([-3, 0, 2.5, 3]) # an explicit form of the linear spline assert_allclose(splev(x, tck), c[0] + (c[1] - c[0]) * x/t[2]) assert_allclose(splev(x, tck, 1), (c[1]-c[0]) / t[2]) # now check a random spline vs splder np.random.seed(1234) x = np.sort(np.random.random(30)) y = np.random.random(30) t, c, k = splrep(x, y) x = [t[0] - 1., t[-1] + 1.] tck2 = splder((t, c, k), k) assert_allclose(splev(x, (t, c, k), k), splev(x, tck2))
def fitPath(self, maxiter=40, s=20000): """fits a least squares spline through the path find descriptions of maxiter and s from scipy documentation""" try: pth = path.getCrds() pp = [pth[:, 0], pth[:, 1], pth[:, 2]] if (len(pp[0]) <= 3): # there are not enough points to fit with self.splinePoints = pp self.splineTangents = None self.splineCurvature = None return else: s = len(pp[0]) / 2.0 cont = 1 j = 0 while (j < maxiter): fit2 = splprep(pp, task=0, full_output=1, s=s)[0] u = na.array(range(pp[0].shape[0]+1)).\ astype(na.float64)/(pp[0].shape[0]) # location of spline points self.splinePoints = splev(u, fit2[0], der=0) # first derivative (tangent) of spline valsd = splev(u, fit2[0], der=1) self.splineTangents = na.array(valsd).astype(na.float64) # second derivative (curvature) of spline valsc = splev(u, fit2[0], der=2) self.splineCurvature = na.array(valsc).astype(na.float64) success = 1 # this doesn't make much sense if (success): # how should I be determining success? break else: s = s * 0.9 j += 1 return True except Exception, error: print "failed in fitPath()", error
def test_extrapolation_modes(self): # test extrapolation modes # * if ext=0, return the extrapolated value. # * if ext=1, return 0 # * if ext=2, raise a ValueError # * if ext=3, return the boundary value. x = [1, 2, 3] y = [0, 2, 4] tck = splrep(x, y, k=1) rstl = [[-2, 6], [0, 0], None, [0, 4]] for ext in (0, 1, 3): assert_array_almost_equal(splev([0, 4], tck, ext=ext), rstl[ext]) assert_raises(ValueError, splev, [0, 4], tck, ext=2)
def check_1(self, f=f1, per=0, s=0, a=0, b=2 * pi, N=20, at=0, xb=None, xe=None): if xb is None: xb = a if xe is None: xe = b x = a + (b - a) * arange(N + 1, dtype=float) / float(N) # nodes x1 = a + (b - a) * arange(1, N, dtype=float) / float(N - 1) # middle points of the nodes v, v1 = f(x), f(x1) nk = [] def err_est(k, d): # Assume f has all derivatives < 1 h = 1.0 / float(N) tol = 5 * h ** (0.75 * (k - d)) if s > 0: tol += 1e5 * s return tol for k in range(1, 6): tck = splrep(x, v, s=s, per=per, k=k, xe=xe) if at: t = tck[0][k:-k] else: t = x1 nd = [] for d in range(k + 1): tol = err_est(k, d) err = norm2(f(t, d) - splev(t, tck, d)) / norm2(f(t, d)) assert_(err < tol, (k, d, err, tol)) nd.append((err, tol)) nk.append(nd) put( "\nf = %s s=S_k(x;t,c) x in [%s, %s] > [%s, %s]" % (f(None), repr(round(xb, 3)), repr(round(xe, 3)), repr(round(a, 3)), repr(round(b, 3))) ) if at: str = "at knots" else: str = "at the middle of nodes" put(" per=%d s=%s Evaluation %s" % (per, repr(s), str)) put(" k : |f-s|^2 |f'-s'| |f''-.. |f'''-. |f''''- |f'''''") k = 1 for l in nk: put(" %d : " % k) for r in l: put(" %.1e %.1e" % r) put("\n") k = k + 1
def spli_basex(p, x, knots=None, deg=3, order=0): """Vandermonde type matrix for splines. Returns a matrix whose columns are the values of the b-splines of deg `deg` associated with the knot sequence `knots` evaluated at the points `x`. Parameters ---------- p : array_like Parameter array containing: - the number of knots - the lower bound of the approximation - the upper bound of the approximation x : array_like Points at which to evaluate the b-splines. deg : int Degree of the splines. Default: cubic splines knots : array_like List of knots. The convention here is that the interior knots have been extended at both ends by ``deg + 1`` extra knots - see augbreaks. If not given the default is equidistant grid order : int Evaluate the derivative of the spline Returns ------- vander : ndarray Vandermonde like matrix of shape (m,n), where ``m = len(x)`` and ``n = len(augbreaks) - deg - 1`` Notes ----- The knots exending the interior points are usually taken to be the same as the endpoints of the interval on which the spline will be evaluated. """ n, a, b = p[0], p[1], p[2] if knots is None: knots = np.linspace(a, b, n + 1 - deg) augbreaks = np.concatenate((a * np.ones((deg)), knots, b * np.ones((deg)))) m = len(augbreaks) - deg - 1 v = np.empty((m, len(x))) d = np.eye(m, len(augbreaks)) for i in range(m): v[i] = spl.splev(x, (augbreaks, d[i], deg), order) return v.T
def spli_basex(p, x ,knots=None , deg = 3 , order = 0 ): """Vandermonde type matrix for splines. Returns a matrix whose columns are the values of the b-splines of deg `deg` associated with the knot sequence `knots` evaluated at the points `x`. Parameters ---------- p : array_like Parameter array containing: - the number of knots - the lower bound of the approximation - the upper bound of the approximation x : array_like Points at which to evaluate the b-splines. deg : int Degree of the splines. Default: cubic splines knots : array_like List of knots. The convention here is that the interior knots have been extended at both ends by ``deg + 1`` extra knots - see augbreaks. If not given the default is equidistant grid order : int Evaluate the derivative of the spline Returns ------- vander : ndarray Vandermonde like matrix of shape (m,n), where ``m = len(x)`` and ``n = len(augbreaks) - deg - 1`` Notes ----- The knots exending the interior points are usually taken to be the same as the endpoints of the interval on which the spline will be evaluated. """ n , a , b = p[0] , p[1] , p[2] if knots is None: knots = np.linspace(a , b , n + 1 - deg) augbreaks = np.concatenate(( a * np.ones((deg)),knots, b * np.ones((deg)))) m = len(augbreaks) - deg - 1 v = np.empty((m, len(x))) d = np.eye(m, len(augbreaks)) for i in range(m): v[i] = spl.splev(x, (augbreaks, d[i], deg),order) return v.T
def check_3(self, f=f1, per=0, s=0, a=0, b=2 * pi, N=20, xb=None, xe=None, ia=0, ib=2 * pi, dx=0.2 * pi): if xb is None: xb = a if xe is None: xe = b x = a + (b - a) * arange(N + 1, dtype=float) / float(N) # nodes v = f(x) put(" k : Roots of s(x) approx %s x in [%s,%s]:" % (f(None), repr(round(a, 3)), repr(round(b, 3)))) for k in range(1, 6): tck = splrep(x, v, s=s, per=per, k=k, xe=xe) if k == 3: roots = sproot(tck) assert_allclose(splev(roots, tck), 0, atol=1e-10, rtol=1e-10) assert_allclose(roots, pi * array([1, 2, 3, 4]), rtol=1e-3) put(" %d : %s" % (k, repr(roots.tolist()))) else: assert_raises(ValueError, sproot, tck)
def interp_masked1d(marr, kind='linear'): """ Interpolates masked values in an array according to the given method. Parameters ---------- marr : MaskedArray Array to fill kind : {'constant', 'linear', 'cubic', quintic'}, optional Type of interpolation """ if np.ndim(marr) > 1: raise ValueError("array must be 1 dimensional!") # marr = marray(marr, copy=True) if getmask(marr) is nomask: return marr # unmaskedIndices = (~marr._mask).nonzero()[0] if unmaskedIndices.size < 2: return marr # kind = kind.lower() if kind == 'constant': return forward_fill(marr) try: k = {'linear' : 1, 'cubic' : 3, 'quintic' : 5}[kind.lower()] except KeyError: raise ValueError("Unsupported interpolation type.") first_unmasked, last_unmasked = flatnotmasked_edges(marr) vals = marr.data[unmaskedIndices] from scipy.interpolate import fitpack tck = fitpack.splrep(unmaskedIndices, vals, k=k) maskedIndices = marr._mask.nonzero()[0] interpIndices = maskedIndices[(maskedIndices > first_unmasked) & \ (maskedIndices < last_unmasked)] marr[interpIndices] = fitpack.splev(interpIndices, tck).astype(marr.dtype) return marr
def interp_masked1d(marr, kind='linear'): """ Interpolates masked values in an array according to the given method. Parameters ---------- marr : MaskedArray Array to fill kind : {'constant', 'linear', 'cubic', quintic'}, optional Type of interpolation """ if np.ndim(marr) > 1: raise ValueError("array must be 1 dimensional!") # marr = marray(marr, copy=True) if getmask(marr) is nomask: return marr # unmaskedIndices = (~marr._mask).nonzero()[0] if unmaskedIndices.size < 2: return marr # kind = kind.lower() if kind == 'constant': return forward_fill(marr) try: k = {'linear': 1, 'cubic': 3, 'quintic': 5}[kind.lower()] except KeyError: raise ValueError("Unsupported interpolation type.") first_unmasked, last_unmasked = flatnotmasked_edges(marr) vals = marr.data[unmaskedIndices] from scipy.interpolate import fitpack tck = fitpack.splrep(unmaskedIndices, vals, k=k) maskedIndices = marr._mask.nonzero()[0] interpIndices = maskedIndices[(maskedIndices > first_unmasked) & \ (maskedIndices < last_unmasked)] marr[interpIndices] = fitpack.splev(interpIndices, tck).astype(marr.dtype) return marr
def spline_derivatives(self, begin, end, derivatives=1): """Calculate derivative or derivatives of the contour using a spline fit, optionally over only the periodic slice specified by 'begin' and 'end'.""" try: l = len(derivatives) unpack = False except: unpack = True derivatives = [derivatives] tck, uout = self.to_spline() points = utility_tools.inclusive_periodic_slice( range(len(self.points)), begin, end) ret = [ numpy.transpose(fitpack.splev(points, tck, der=d)) for d in derivatives ] if unpack: ret = ret[0] return ret
def splvander(x, deg, knots, deriv): """ original source: https://mail.python.org/pipermail/scipy-user/2012-July/032677.html Vandermonde type matrix for splines. Returns a matrix whose columns are the values of the b-splines of deg `deg` associated with the knot sequence `knots` evaluated at the points `x`. Parameters ---------- x : array_like Points at which to evaluate the b-splines. deg : int Degree of the splines. knots : array_like List of knots. The convention here is that the interior knots have been extended at both ends by ``deg + 1`` extra knots. Returns ------- vander : ndarray Vandermonde like matrix of shape (m,n), where ``m = len(x)`` and ``m = len(knots) - deg - 1`` Notes ----- The knots exending the interior points are usually taken to be the same as the endpoints of the interval on which the spline will be evaluated. """ from scipy.interpolate import fitpack as spl import numpy as np m = len(knots) - deg - 1 v = np.zeros((m, len(x))) d = np.eye(m, len(knots)) for i in range(m): v[i] = spl.splev(x, (knots, d[i], deg), der=deriv) return v.T
def interp_masked1d(marr, kind='linear'): """interp_masked1d(marr, king='linear') Interpolates masked values in marr according to method kind. kind must be one of 'constant', 'linear', 'cubic', quintic' """ if numeric.ndim(marr) > 1: raise ValueError("array must be 1 dimensional!") # marr = marray(marr, copy=True) if getmask(marr) is nomask: return marr # unmaskedIndices = (~marr._mask).nonzero()[0] if unmaskedIndices.size < 2: return marr # kind = kind.lower() if kind == 'constant': return forward_fill(marr) try: k = {'linear' : 1, 'cubic' : 3, 'quintic' : 5}[kind.lower()] except KeyError: raise ValueError("Unsupported interpolation type.") first_unmasked, last_unmasked = flatnotmasked_edges(marr) vals = marr.data[unmaskedIndices] tck = fitpack.splrep(unmaskedIndices, vals, k=k) maskedIndices = marr._mask.nonzero()[0] interpIndices = maskedIndices[(maskedIndices > first_unmasked) & \ (maskedIndices < last_unmasked)] marr[interpIndices] = fitpack.splev(interpIndices, tck).astype(marr.dtype) return marr
def _obj_beam_fit(params, beams, x_nodes): """ params = [2.953e-03, 1.156e+00, 1.297e+01, 9.747e-01 , 9.970e-01 , 8.509e-01, 1.076e+00 , 1.487e+00 , 7.864e-01, 1.072e+00 , 1.015e+00] """ import time from scipy.interpolate.fitpack import splev, splrep import pysynphot as S ### Spline continuum + gaussian line l0 = 6563. * (1 + params[1]) if (l0 < 1.12e4) | (l0 > 1.63e4): return -np.inf line = S.GaussianSource(params[2], l0, 10) tck = splrep(x_nodes, params[3:], k=3, s=0) xcon = np.arange(0.9e4, 1.8e4, 0.01e4) ycon = splev(xcon, tck, der=0, ext=0) spec = S.ArraySpectrum(xcon, ycon, fluxunits='flam', keepneg=True) + line lnprob = 0 for key in beams.keys(): beam = beams[key] modelf = beam.compute_model(beam.clip_thumb, xspec=spec.wave, yspec=spec.flux, in_place=False) lnprob += -0.5 * np.sum(((beam.cutout_scif - params[0] - modelf)**2 / beam.cutout_varf)[beam.cutout_maskf]) if ~np.isfinite(lnprob): lnprob = -np.inf print params, lnprob #time.sleep(0.2) return lnprob
import numpy as np import matplotlib.pyplot as plt from scipy.interpolate.fitpack import splrep, splev from util.point import Point from walking.helpers import bezier_curves from walking.helpers.bezier_curves import LineSegment x = (0, 2.0, 6.0, 8.0) y = (-47.7, -44.7, -44.7, -47.7) tck = splrep(x, y) x2 = np.linspace(0, 8) y2 = splev(x2, tck) points = [Point((x_, 0, y_)) for (x_, y_) in zip(x, y)] l1 = LineSegment(points[0], points[1]) l2 = LineSegment(points[1], points[2]) l3 = LineSegment(points[2], points[3]) yy2 = [ bezier_curves.get_interpolated_position(x2_ / 8, l1, l2, l3)[2] for x2_ in x2 ] plt.plot(x, y, 'o', x, y, '--', x2, y2, x2, yy2) plt.show()
def interpolate_points(self, positions): """Use spline interpolation to determine the spatial positions at the contour positions specified (fracitonal positions are thus acceptable).""" tck, uout = self.to_spline() return numpy.transpose(fitpack.splev(positions, tck))
resp = read_LFI_response(respfile) horn = resp.keys['horn'] rad = resp.keys['radiometer'] det = resp.keys['detector'] sky_spline = fit.splrep(resp.sky_volt_out,resp.sky_volt_in,s=0.0) ref_spline = fit.splrep(resp.load_volt_out,resp.load_volt_in,s=0.0) for od in range(91,953): rawfile=rawdata_dir+'od%s_rca%s.fits' % (od,diode) corrfile=adc_corr_data_dir+'od%s_rca%s.fits' % (od,diode) if not(os.path.exists(corrfile)): if os.path.exists(rawfile): hdulist = fits.open(rawfile) data = hdulist[1].data sky_volt = data.field('SKY') load_volt = data.field('REF') sky_cor = fit.splev(sky_volt,sky_spline) load_cor = fit.splev(load_volt,ref_spline) data.field('SKY')[:] = sky_cor data.field('REF')[:] = load_cor hdulist[1].header.update('hierarch instrument','LFI_ADC_corr') hdulist[1].header.update('hierarch ADC_correction','True') print 'ADC correcting %s -> %s' % (rawfile,corrfile) hdulist.writeto(corrfile) if len(fl)==0: for od in range(91,953): rawfile=rawdata_dir+'od%s_rca%s.fits' % (od,diode) corrfile=adc_corr_data_dir+'od%s_rca%s.fits' % (od,diode) if os.path.exists(rawfile): if not(os.path.exists(corrfile)): shutil.copy(rawfile,corrfile)
def setup_fit(): snlim = 0 snlim = 1./np.sqrt(len(beams)) Rmax = 5 for key in FLT.keys(): beam = beams[key] beam.cutout_sci = beam.get_cutout(FLT[key].im['SCI'].data)*1 beam.cutout_dq = beam.get_cutout(FLT[key].im['DQ'].data)*1 beam.cutout_err = beam.get_cutout(FLT[key].im['ERR'].data)*1 beam.cutout_mask = (beam.cutout_dq-(beam.cutout_dq & 512) == 0) & (beam.cutout_err > 0) & (beam.cutout_err < 10) sh = beam.cutout_sci.shape yp, xp = np.indices(beam.thumb.shape) r = np.sqrt((xp-sh[0]/2)**2+(yp-sh[0]/2)**2) beam.norm = beam.thumb[r <= Rmax].sum()/1.e-17 beam.clip_thumb = beam.thumb*(r <= Rmax) beam.compute_model(beam.clip_thumb) sn = beam.model/beam.cutout_err #beam.mask &= (sn > 0.5) & (beam.err > 0) beam.cutout_mask &= (sn > snlim) & (beam.cutout_err > 0) beam.cutout_sci[~beam.cutout_mask] = 0 beam.cutout_err[~beam.cutout_mask] = 0 beam.cutout_var = beam.cutout_err**2 beam.cutout_scif = beam.cutout_sci.flatten() beam.cutout_varf = beam.cutout_var.flatten() beam.cutout_maskf = beam.cutout_mask.flatten() ds9.view((beam.cutout_scif-beam.cutout_modelf).reshape(beam.cutout_sci.shape)*beam.cutout_mask) ## x_nodes = np.arange(1.0e4,1.71e4,0.1e4) x_nodes = np.arange(1.0e4,1.71e4,0.03e4) x_nodes = np.arange(1.05e4,1.71e4,0.075e4) init = np.append([0, 1.148, 500], np.ones(len(x_nodes))) init = np.append([0, 1.2078, 500], np.ones(len(x_nodes))) step_sig = np.append([0.01,0.1,50], np.ones(len(x_nodes))*0.1) init = np.append([0, 1.0, 500], np.ones(len(x_nodes))) step_sig = np.append([0.0,0.2,150], np.ones(len(x_nodes))*0.1) ### don't fit redshift #init = np.append([0, 1.0, 0], np.ones(len(x_nodes))) #step_sig = np.append([0.0,0.,0], np.ones(len(x_nodes))*0.2) obj_fun = _obj_beam_fit obj_args = [beams, x_nodes] ndim, nwalkers = len(init), len(init)*2 p0 = [(init+np.random.normal(size=ndim)*step_sig) for i in xrange(nwalkers)] NTHREADS, NSTEP = 2, 5 sampler = emcee.EnsembleSampler(nwalkers, ndim, obj_fun, args = obj_args, threads=NTHREADS) # t0 = time.time() result = sampler.run_mcmc(p0, NSTEP) t1 = time.time() print 'Sampler: %.1f s' %(t1-t0) param_names = ['bg', 'z', 'Ha'] param_names.extend(['spl%d' %i for i in range(len(init)-3)]) import unicorn.interlace_fit chain = unicorn.interlace_fit.emceeChain(chain=sampler.chain, param_names=param_names) #obj_fun(chain.map, beams) #obj_fun(init, beams, x_nodes) params = chain.map*1. #params = init ### Show spectra from scipy.interpolate.fitpack import splev, splrep # NDRAW=100 # draw = chain.draw_random(NDRAW) # for i in range(NDRAW): # line = S.GaussianSource(draw[i,2], 6563.*(1+draw[i,1]), 10) # tck = splrep(x_nodes, draw[i,3:], k=3, s=0) # xcon = np.arange(0.9e4,1.8e4,0.01e4) # ycon = splev(xcon, tck, der=0, ext=0) # spec = S.ArraySpectrum(xcon, ycon, fluxunits='flam', keepneg=True)+line # plt.plot(spec.wave, spec.flux, alpha=0.1, color='red') # line = S.GaussianSource(params[2], 6563.*(1+params[1]), 10) tck = splrep(x_nodes, params[3:], k=3, s=0) xcon = np.arange(0.9e4,1.8e4,0.01e4) ycon = splev(xcon, tck, der=0, ext=0) pspec = S.ArraySpectrum(xcon, ycon, fluxunits='flam', keepneg=True)+line for key in beams.keys(): beam = beams[key] beam.compute_model(beam.clip_thumb, xspec=pspec.wave, yspec=pspec.flux, in_place=True) xfull, yfull, mfull, cfull = [], [], [], [] for i, key in enumerate(beams.keys()): beam = beams[key] #ds9.frame(i+1) #ds9.view((beam.sci-beam.model)*beam.mask) ls = 'steps-mid' marker = 'None' #ls = '-' #ls, marker = 'None', '.' xfull = np.append(xfull, beam.lam) ybeam = (beam.cutout_sci-params[0]*beam.cutout_mask).sum(axis=0)[sh[0]/2:-sh[0]/2]/beam.ysens/beam.norm yfull = np.append(yfull, ybeam) #plt.plot(beam.lam, ybeam, color='black', alpha=0.5, linestyle=ls, marker=marker) cbeam = ((beam.cutout_m-beam.omodel)*beam.cutout_mask).sum(axis=0)[sh[0]/2:-sh[0]/2]/beam.ysens/beam.norm cfull = np.append(cfull, cbeam) mbeam = ((beam.model)*beam.mask).sum(axis=0)[sh[0]/2:-sh[0]/2]/beam.ysens/beam.norm mfull = np.append(mfull, mbeam) plt.plot(beam.lam, ybeam-cbeam, color='black', linestyle=ls, alpha=0.5) plt.plot(beam.lam, mbeam, color='blue', linestyle=ls, alpha=0.5) #nx = 20 kern = np.ones(nx)/nx so = np.argsort(xfull) plt.plot(xfull[so][nx/2::nx], nd.convolve((yfull-cfull)[so], kern)[nx/2::nx], color='orange', linewidth=2, alpha=0.8, linestyle='steps-mid') plt.plot(xfull[so][nx/2::nx], nd.convolve(mfull[so], kern)[nx/2::nx], color='green', linewidth=2, alpha=0.8, linestyle='steps-mid') plt.ylim(0,5) plt.xlim(1.e4,1.78e4) from scipy.optimize import fmin_bfgs p0 = fmin_bfgs(_loss, init[3:], args=(beams, x_nodes), gtol=1.e-3, epsilon=1.e-3, maxiter=100) params = np.array(init) params[3:] = p0
yi = np.zeros(np.shape(xi)) yi_hd = np.zeros(np.shape(xi_hd)) yi[:] = f(xi[:]) yi_hd = f(xi_hd[:]) #xi = np.linspace(-2, 2, 101) #yi = 10 * xi / (1 + 100 * np.power(xi, 2)) data = np.c_[xi, yi] p = poly.NewtonPolynomial(points=data) d4p = deriv.dr_dxr(p, r=4) norm = norm.Norm(d4p) T, eps = cut.cutab(norm, xi, eps, r) tck = fitpack.splrep(xi, yi, t=T[1:-1]) fit = fitpack.splev(xi, tck) error = np.abs(fit - yi) print '*' * 10 + " FIRST ERROR RATIO " + '*' * 10 + '\n\n' print "error / tol = %f" % (error.max() / tol) while error.max() > tol: T, eps = cut.cutab(norm, xi, eps, r) print "\n\nRun %d" % run_count print "eps = %e in outer loop" % eps eps = eps / 2 tck = fitpack.splrep(xi, yi, t=T[1:-1]) fit = fitpack.splev(xi, tck) fit_hd = fitpack.splev(xi_hd, tck) fT = np.zeros(np.shape(T)) fT[:] = f(T[:]) error = np.abs(fit - yi)
def setup_fit(): snlim = 0 snlim = 1. / np.sqrt(len(beams)) Rmax = 5 for key in FLT.keys(): beam = beams[key] beam.cutout_sci = beam.get_cutout(FLT[key].im['SCI'].data) * 1 beam.cutout_dq = beam.get_cutout(FLT[key].im['DQ'].data) * 1 beam.cutout_err = beam.get_cutout(FLT[key].im['ERR'].data) * 1 beam.cutout_mask = (beam.cutout_dq - (beam.cutout_dq & 512) == 0) & ( beam.cutout_err > 0) & (beam.cutout_err < 10) sh = beam.cutout_sci.shape yp, xp = np.indices(beam.thumb.shape) r = np.sqrt((xp - sh[0] / 2)**2 + (yp - sh[0] / 2)**2) beam.norm = beam.thumb[r <= Rmax].sum() / 1.e-17 beam.clip_thumb = beam.thumb * (r <= Rmax) beam.compute_model(beam.clip_thumb) sn = beam.model / beam.cutout_err #beam.mask &= (sn > 0.5) & (beam.err > 0) beam.cutout_mask &= (sn > snlim) & (beam.cutout_err > 0) beam.cutout_sci[~beam.cutout_mask] = 0 beam.cutout_err[~beam.cutout_mask] = 0 beam.cutout_var = beam.cutout_err**2 beam.cutout_scif = beam.cutout_sci.flatten() beam.cutout_varf = beam.cutout_var.flatten() beam.cutout_maskf = beam.cutout_mask.flatten() ds9.view((beam.cutout_scif - beam.cutout_modelf).reshape( beam.cutout_sci.shape) * beam.cutout_mask) ## x_nodes = np.arange(1.0e4, 1.71e4, 0.1e4) x_nodes = np.arange(1.0e4, 1.71e4, 0.03e4) x_nodes = np.arange(1.05e4, 1.71e4, 0.075e4) init = np.append([0, 1.148, 500], np.ones(len(x_nodes))) init = np.append([0, 1.2078, 500], np.ones(len(x_nodes))) step_sig = np.append([0.01, 0.1, 50], np.ones(len(x_nodes)) * 0.1) init = np.append([0, 1.0, 500], np.ones(len(x_nodes))) step_sig = np.append([0.0, 0.2, 150], np.ones(len(x_nodes)) * 0.1) ### don't fit redshift #init = np.append([0, 1.0, 0], np.ones(len(x_nodes))) #step_sig = np.append([0.0,0.,0], np.ones(len(x_nodes))*0.2) obj_fun = _obj_beam_fit obj_args = [beams, x_nodes] ndim, nwalkers = len(init), len(init) * 2 p0 = [(init + np.random.normal(size=ndim) * step_sig) for i in xrange(nwalkers)] NTHREADS, NSTEP = 2, 5 sampler = emcee.EnsembleSampler(nwalkers, ndim, obj_fun, args=obj_args, threads=NTHREADS) # t0 = time.time() result = sampler.run_mcmc(p0, NSTEP) t1 = time.time() print 'Sampler: %.1f s' % (t1 - t0) param_names = ['bg', 'z', 'Ha'] param_names.extend(['spl%d' % i for i in range(len(init) - 3)]) import unicorn.interlace_fit chain = unicorn.interlace_fit.emceeChain(chain=sampler.chain, param_names=param_names) #obj_fun(chain.map, beams) #obj_fun(init, beams, x_nodes) params = chain.map * 1. #params = init ### Show spectra from scipy.interpolate.fitpack import splev, splrep # NDRAW=100 # draw = chain.draw_random(NDRAW) # for i in range(NDRAW): # line = S.GaussianSource(draw[i,2], 6563.*(1+draw[i,1]), 10) # tck = splrep(x_nodes, draw[i,3:], k=3, s=0) # xcon = np.arange(0.9e4,1.8e4,0.01e4) # ycon = splev(xcon, tck, der=0, ext=0) # spec = S.ArraySpectrum(xcon, ycon, fluxunits='flam', keepneg=True)+line # plt.plot(spec.wave, spec.flux, alpha=0.1, color='red') # line = S.GaussianSource(params[2], 6563. * (1 + params[1]), 10) tck = splrep(x_nodes, params[3:], k=3, s=0) xcon = np.arange(0.9e4, 1.8e4, 0.01e4) ycon = splev(xcon, tck, der=0, ext=0) pspec = S.ArraySpectrum(xcon, ycon, fluxunits='flam', keepneg=True) + line for key in beams.keys(): beam = beams[key] beam.compute_model(beam.clip_thumb, xspec=pspec.wave, yspec=pspec.flux, in_place=True) xfull, yfull, mfull, cfull = [], [], [], [] for i, key in enumerate(beams.keys()): beam = beams[key] #ds9.frame(i+1) #ds9.view((beam.sci-beam.model)*beam.mask) ls = 'steps-mid' marker = 'None' #ls = '-' #ls, marker = 'None', '.' xfull = np.append(xfull, beam.lam) ybeam = (beam.cutout_sci - params[0] * beam.cutout_mask).sum( axis=0)[sh[0] / 2:-sh[0] / 2] / beam.ysens / beam.norm yfull = np.append(yfull, ybeam) #plt.plot(beam.lam, ybeam, color='black', alpha=0.5, linestyle=ls, marker=marker) cbeam = ((beam.cutout_m - beam.omodel) * beam.cutout_mask).sum( axis=0)[sh[0] / 2:-sh[0] / 2] / beam.ysens / beam.norm cfull = np.append(cfull, cbeam) mbeam = ((beam.model) * beam.mask).sum( axis=0)[sh[0] / 2:-sh[0] / 2] / beam.ysens / beam.norm mfull = np.append(mfull, mbeam) plt.plot(beam.lam, ybeam - cbeam, color='black', linestyle=ls, alpha=0.5) plt.plot(beam.lam, mbeam, color='blue', linestyle=ls, alpha=0.5) #nx = 20 kern = np.ones(nx) / nx so = np.argsort(xfull) plt.plot(xfull[so][nx / 2::nx], nd.convolve((yfull - cfull)[so], kern)[nx / 2::nx], color='orange', linewidth=2, alpha=0.8, linestyle='steps-mid') plt.plot(xfull[so][nx / 2::nx], nd.convolve(mfull[so], kern)[nx / 2::nx], color='green', linewidth=2, alpha=0.8, linestyle='steps-mid') plt.ylim(0, 5) plt.xlim(1.e4, 1.78e4) from scipy.optimize import fmin_bfgs p0 = fmin_bfgs(_loss, init[3:], args=(beams, x_nodes), gtol=1.e-3, epsilon=1.e-3, maxiter=100) params = np.array(init) params[3:] = p0
import numpy as np import matplotlib.pyplot as plt from scipy.interpolate.fitpack import splrep, splev from util.point import Point from walking.helpers import bezier_curves from walking.helpers.bezier_curves import LineSegment x = (0, 2.0, 6.0, 8.0) y = (-47.7, -44.7, -44.7, -47.7) tck = splrep(x, y) x2 = np.linspace(0, 8) y2 = splev(x2, tck) points = [Point((x_, 0, y_)) for (x_, y_) in zip(x, y)] l1 = LineSegment(points[0], points[1]) l2 = LineSegment(points[1], points[2]) l3 = LineSegment(points[2], points[3]) yy2 = [bezier_curves.get_interpolated_position(x2_/8, l1, l2, l3)[2] for x2_ in x2] plt.plot(x, y, 'o', x, y, '--', x2, y2, x2, yy2) plt.show()