コード例 #1
0
 def test_fit_eval_func1(self):
     """NonLinearLeastSquaresFit: Basic function fitting and evaluation using data from a known function."""
     interp = NonLinearLeastSquaresFit(self.vFunc, self.init_params)
     interp.fit(self.x, self.y)
     y = interp(self.x)
     assert_almost_equal(interp.params, self.true_params, decimal=7)
     assert_almost_equal(y, self.y, decimal=5)
コード例 #2
0
 def test_fit_eval_gauss(self):
     """NonLinearLeastSquaresFit: Check fit on a 2-D log Gaussian function."""
     interp2 = NonLinearLeastSquaresFit(self.func2, self.init_params2)
     interp2.fit(self.x2, self.y2)
     y2 = interp2(self.x2)
     assert_almost_equal(interp2.params, self.true_params2, decimal=10)
     assert_almost_equal(y2, self.y2, decimal=10)
コード例 #3
0
 def test_fit_eval_gauss(self):
     """NonLinearLeastSquaresFit: Check fit on a 2-D log Gaussian function."""
     interp2 = NonLinearLeastSquaresFit(self.func2, self.init_params2)
     interp2.fit(self.x2, self.y2)
     y2 = interp2(self.x2)
     assert_almost_equal(interp2.params, self.true_params2, decimal=10)
     assert_almost_equal(y2, self.y2, decimal=10)
コード例 #4
0
 def test_fit_eval_func1(self):
     """NonLinearLeastSquaresFit: Basic function fitting and evaluation using data from a known function."""
     interp = NonLinearLeastSquaresFit(self.vFunc, self.init_params)
     interp.fit(self.x, self.y)
     y = interp(self.x)
     assert_almost_equal(interp.params, self.true_params, decimal=7)
     assert_almost_equal(y, self.y, decimal=5)
コード例 #5
0
 def test_fit_eval_linear(self):
     """NonLinearLeastSquaresFit: Do linear problem and check Jacobian."""
     lin = LinearLeastSquaresFit()
     lin.fit(self.x3, self.y3, std_y=2.0)
     nonlin = NonLinearLeastSquaresFit(self.func3, self.init_params3,
                                       func_jacobian=self.jac3)
     nonlin.fit(self.x3, self.y3, std_y=2.0)
     # A correct Jacobian helps a lot...
     assert_almost_equal(nonlin.params, self.true_params3, decimal=11)
     assert_almost_equal(nonlin.cov_params, lin.cov_params, decimal=11)
     nonlin_nojac = NonLinearLeastSquaresFit(self.func3, self.init_params3)
     nonlin_nojac.fit(self.x3, self.y3, std_y=0.1)
     assert_almost_equal(nonlin_nojac.params, self.true_params3, decimal=5)
コード例 #6
0
 def test_enabled_params(self):
     """NonLinearLeastSquaresFit: Check whether subset of parameters can be optimised."""
     lin = LinearLeastSquaresFit()
     lin.fit(self.x3[self.enabled_params_int, :], self.y3, std_y=2.0)
     lin_cov_params = np.zeros(
         (len(self.true_params3), len(self.true_params3)))
     lin_cov_params[np.ix_(self.enabled_params_int,
                           self.enabled_params_int)] = lin.cov_params
     nonlin = NonLinearLeastSquaresFit(self.func3, self.init_params3,
                                       self.enabled_params_int, self.jac3)
     nonlin.fit(self.x3, self.y3, std_y=2.0)
     assert_almost_equal(nonlin.params, self.true_params3, decimal=11)
     assert_almost_equal(nonlin.cov_params, lin_cov_params, decimal=11)
     nonlin = NonLinearLeastSquaresFit(self.func3, self.init_params3,
                                       self.enabled_params_bool, self.jac3)
     nonlin.fit(self.x3, self.y3, std_y=2.0)
     assert_almost_equal(nonlin.params, self.true_params3, decimal=11)
     assert_almost_equal(nonlin.cov_params, lin_cov_params, decimal=11)
コード例 #7
0
 def test_fit_eval_linear(self):
     """NonLinearLeastSquaresFit: Compare to LinearLeastSquaresFit on a linear problem (and check use of Jacobian)."""
     lin = LinearLeastSquaresFit()
     lin.fit(self.x3, self.y3, std_y=2.0)
     nonlin = NonLinearLeastSquaresFit(self.func3, self.init_params3, func_jacobian=self.jac3)
     nonlin.fit(self.x3, self.y3, std_y=2.0)
     # A correct Jacobian helps a lot...
     assert_almost_equal(nonlin.params, self.true_params3, decimal=11)
     assert_almost_equal(nonlin.cov_params, lin.cov_params, decimal=11)
     nonlin_nojac = NonLinearLeastSquaresFit(self.func3, self.init_params3)
     nonlin_nojac.fit(self.x3, self.y3, std_y=0.1)
     assert_almost_equal(nonlin_nojac.params, self.true_params3, decimal=6)
コード例 #8
0
 def test_enabled_params(self):
     """NonLinearLeastSquaresFit: Check whether subset of parameters can be optimised."""
     lin = LinearLeastSquaresFit()
     lin.fit(self.x3[self.enabled_params_int, :], self.y3, std_y=2.0)
     lin_cov_params = np.zeros((len(self.true_params3), len(self.true_params3)))
     lin_cov_params[np.ix_(self.enabled_params_int, self.enabled_params_int)] = lin.cov_params
     nonlin = NonLinearLeastSquaresFit(self.func3, self.init_params3, self.enabled_params_int, self.jac3)
     nonlin.fit(self.x3, self.y3, std_y=2.0)
     assert_almost_equal(nonlin.params, self.true_params3, decimal=11)
     assert_almost_equal(nonlin.cov_params, lin_cov_params, decimal=11)
     nonlin = NonLinearLeastSquaresFit(self.func3, self.init_params3, self.enabled_params_bool, self.jac3)
     nonlin.fit(self.x3, self.y3, std_y=2.0)
     assert_almost_equal(nonlin.params, self.true_params3, decimal=11)
     assert_almost_equal(nonlin.cov_params, lin_cov_params, decimal=11)
コード例 #9
0
def generatespiral(totextent,
                   tottime,
                   tracktime=1,
                   sampletime=1,
                   kind='uniform',
                   mirrorx=False):
    totextent = np.float(totextent)
    tottime = np.float(tottime)
    sampletime = np.float(sampletime)
    nextrazeros = int(np.float(tracktime) / sampletime)
    print 'nextrazeros', nextrazeros
    tracktime = nextrazeros * sampletime
    radextent = np.float(totextent) / 2.0
    if (kind == 'dense-core'):
        c = np.sqrt(2) * 180.0 / (16.0 * np.pi)
        narms = 2 * int(
            np.sqrt(tottime / c + (tracktime / c)**2) - tracktime / c
        )  #ensures even number of arms - then scan pattern ends on target (if odd it will not)
        ntime = int((tottime - tracktime * narms) / (sampletime * narms))
        armrad = radextent * (np.linspace(0, 1, ntime))
        armtheta = np.linspace(0, np.pi, ntime)
        armx = armrad * np.cos(armtheta)
        army = armrad * np.sin(armtheta)
    elif (kind == 'approx'):
        c = 180.0 / (16.0 * np.pi)
        narms = 2 * int(
            np.sqrt(tottime / c + (tracktime / c)**2) - tracktime / c
        )  #ensures even number of arms - then scan pattern ends on target (if odd it will not)
        ntime = int((tottime - tracktime * narms) / (sampletime * narms))
        armrad = radextent * (np.linspace(0, 1, ntime))
        armtheta = np.linspace(0, np.pi, ntime)
        armx = armrad * np.cos(armtheta)
        army = armrad * np.sin(armtheta)
        dist = np.sqrt((armx[:-1] - armx[1:])**2 + (army[:-1] - army[1:])**2)
        narmrad = np.cumsum(np.concatenate([np.array([0]), 1.0 / dist]))
        narmrad *= radextent / max(narmrad)
        narmtheta = narmrad / radextent * np.pi
        armx = narmrad * np.cos(narmtheta)
        army = narmrad * np.sin(narmtheta)
    else:  #'uniform'
        c = 180.0 / (16.0 * np.pi)
        narms = 2 * int(
            np.sqrt(tottime / c + (tracktime / c)**2) - tracktime / c
        )  #ensures even number of arms - then scan pattern ends on target (if odd it will not)
        ntime = int((tottime - tracktime * narms) / (sampletime * narms))
        armx = np.zeros(ntime)
        army = np.zeros(ntime)
        #must be on curve x=t*cos(np.pi*t),y=t*sin(np.pi*t)
        #intersect (x-x0)**2+(y-y0)**2=1/ntime**2 with spiral
        lastr = 0.0
        for it in range(1, ntime):
            data = np.array([1.0 / ntime])
            indep = np.array([armx[it - 1], army[it - 1]
                              ])  #last calculated coordinate in arm, is x0,y0
            initialparams = np.array([lastr + 1.0 / ntime])
            fitter = NonLinearLeastSquaresFit(spiral, initialparams)
            fitter.fit(indep, data)
            lastr = fitter.params[0]
            armx[it] = lastr * np.cos(2.0 * np.pi * lastr)
            army[it] = lastr * np.sin(2.0 * np.pi * lastr)

        maxrad = np.sqrt(armx[it]**2 + army[it]**2)
        armx = armx * radextent / maxrad
        army = army * radextent / maxrad
    # ndist=sqrt((armx[:-1]-armx[1:])**2+(army[:-1]-army[1:])**2)
    # print ndist

    compositex = [[] for ia in range(narms)]
    compositey = [[] for ia in range(narms)]
    ncompositex = [[] for ia in range(narms)]
    ncompositey = [[] for ia in range(narms)]
    reverse = False
    for ia in range(narms):
        rot = -ia * np.pi * 2.0 / narms
        x = armx * np.cos(rot) - army * np.sin(rot)
        y = armx * np.sin(rot) + army * np.cos(rot)
        nrot = ia * np.pi * 2.0 / narms
        nx = armx * np.cos(nrot) - army * np.sin(nrot)
        ny = armx * np.sin(nrot) + army * np.cos(nrot)
        if (nextrazeros > 0):
            x = np.r_[np.repeat(0.0, nextrazeros), x]
            y = np.r_[np.repeat(0.0, nextrazeros), y]
            nx = np.r_[np.repeat(0.0, nextrazeros), nx]
            ny = np.r_[np.repeat(0.0, nextrazeros), ny]
        if reverse:
            reverse = False
            x = x[::-1]
            y = y[::-1]
            nx = nx[::-1]
            ny = ny[::-1]
        else:
            reverse = True
        if (mirrorx):
            compositex[ia] = -x
            compositey[ia] = y
            ncompositex[ia] = -nx
            ncompositey[ia] = ny
        else:
            compositex[ia] = x
            compositey[ia] = y
            ncompositex[ia] = nx
            ncompositey[ia] = ny

    return compositex, compositey, ncompositex, ncompositey