示例#1
0
    def test_multipledataset_corefinement(self):
        # test corefinement of three datasets
        e361 = np.loadtxt(os.path.join(CURDIR, 'e361r.txt'))
        e365 = np.loadtxt(os.path.join(CURDIR, 'e365r.txt'))
        e366 = np.loadtxt(os.path.join(CURDIR, 'e366r.txt'))

        coefs361 = np.zeros(16)
        coefs361[0] = 2
        coefs361[1] = 1.
        coefs361[2] = 2.07
        coefs361[4] = 6.36
        coefs361[6] = 2e-5
        coefs361[7] = 3
        coefs361[8] = 10
        coefs361[9] = 3.47
        coefs361[11] = 4
        coefs361[12] = 200
        coefs361[13] = 1
        coefs361[15] = 3

        coefs365 = np.copy(coefs361)
        coefs366 = np.copy(coefs361)
        coefs365[4] = 3.47
        coefs366[4] = -0.56

        qvals361, rvals361, evals361 = np.hsplit(e361, 3)
        qvals365, rvals365, evals365 = np.hsplit(e365, 3)
        qvals366, rvals366, evals366 = np.hsplit(e366, 3)

        lowlim = np.zeros(16)
        lowlim[4] = -0.8
        hilim = 2 * coefs361
        
        bounds = list(zip(lowlim, hilim))
        params361 = curvefitter.to_parameters(coefs361, bounds=bounds,
                                       varies=[False] * 16)
        params365 = curvefitter.to_parameters(coefs365, bounds=bounds,
                                       varies=[False] * 16)
        params366 = curvefitter.to_parameters(coefs366, bounds=bounds,
                                       varies=[False] * 16)
        assert_(len(params361), 16)
        assert_(len(params365), 16)
        assert_(len(params366), 16)

        fit = [1, 6, 8, 12, 13]
        for p in fit:
            params361['p%d' % p].vary = True
            params365['p%d' % p].vary = True
            params366['p%d' % p].vary = True

        a = CurveFitter(reflect_fitfunc, qvals361.flatten(),
                        np.log10(rvals361.flatten()),
                        params361)
        b = CurveFitter(reflect_fitfunc, qvals365.flatten(),
                        np.log10(rvals365.flatten()),
                        params365)
        c = CurveFitter(reflect_fitfunc, qvals366.flatten(),
                        np.log10(rvals366.flatten()),
                        params366)

        g = GlobalFitter([a, b, c], constraints=['d1:p8=d0:p8',
                                                 'd2:p8=d0:p8',
                                                 'd1:p12=d0:p12',
                                                 'd2:p12 = d0:p12'],
                         kws={'seed':1})

        indiv_chisqr = (a.residuals(a.params) ** 2
                        + b.residuals(b.params) ** 2
                        + c.residuals(c.params) ** 2)
        global_chisqr = g.residuals(g.params) ** 2
        assert_almost_equal(indiv_chisqr.sum(), global_chisqr.sum())
        # import time
        res = g.fit('differential_evolution')
        # start = time.time()
        # g.emcee(params=res.params, nwalkers=300, steps=500, workers=1)
        # finish = time.time()
        # print(finish - start)
        assert_almost_equal(res.chisqr, 0.774590447535, 4)
示例#2
0
 def test_residuals_length(self):
     # the residuals should be the same length as the data
     a = GlobalFitter([self.f])
     residuals = a.residuals(a.params)
     assert_equal(residuals.size, a.fitters[0].ydata.size)
示例#3
0
    def test_multipledataset_corefinement(self):
        # test corefinement of three datasets
        e361 = np.loadtxt(os.path.join(CURDIR, 'e361r.txt'))
        e365 = np.loadtxt(os.path.join(CURDIR, 'e365r.txt'))
        e366 = np.loadtxt(os.path.join(CURDIR, 'e366r.txt'))

        coefs361 = np.zeros(16)
        coefs361[0] = 2
        coefs361[1] = 1.
        coefs361[2] = 2.07
        coefs361[4] = 6.36
        coefs361[6] = 2e-5
        coefs361[7] = 3
        coefs361[8] = 10
        coefs361[9] = 3.47
        coefs361[11] = 4
        coefs361[12] = 200
        coefs361[13] = 1
        coefs361[15] = 3

        coefs365 = np.copy(coefs361)
        coefs366 = np.copy(coefs361)
        coefs365[4] = 3.47
        coefs366[4] = -0.56

        qvals361, rvals361, evals361 = np.hsplit(e361, 3)
        qvals365, rvals365, evals365 = np.hsplit(e365, 3)
        qvals366, rvals366, evals366 = np.hsplit(e366, 3)

        lowlim = np.zeros(16)
        lowlim[4] = -0.8
        hilim = 2 * coefs361

        bounds = list(zip(lowlim, hilim))
        params361 = curvefitter.to_parameters(coefs361,
                                              bounds=bounds,
                                              varies=[False] * 16)
        params365 = curvefitter.to_parameters(coefs365,
                                              bounds=bounds,
                                              varies=[False] * 16)
        params366 = curvefitter.to_parameters(coefs366,
                                              bounds=bounds,
                                              varies=[False] * 16)
        assert_(len(params361), 16)
        assert_(len(params365), 16)
        assert_(len(params366), 16)

        fit = [1, 6, 8, 12, 13]
        for p in fit:
            params361['p%d' % p].vary = True
            params365['p%d' % p].vary = True
            params366['p%d' % p].vary = True

        a = CurveFitter(reflect_fitfunc,
                        (qvals361.flatten(), np.log10(rvals361.flatten())),
                        params361)
        b = CurveFitter(reflect_fitfunc,
                        (qvals365.flatten(), np.log10(rvals365.flatten())),
                        params365)
        c = CurveFitter(reflect_fitfunc,
                        (qvals366.flatten(), np.log10(rvals366.flatten())),
                        params366)

        g = GlobalFitter([a, b, c], constraints=['d1:p8=d0:p8',
                                                 'd2:p8=d0:p8',
                                                 'd1:p12=d0:p12',
                                                 'd2:p12 = d0:p12'],
                         kws={'seed': 1})

        indiv_chisqr = (a.residuals(a.params) ** 2 +
                        b.residuals(b.params) ** 2 +
                        c.residuals(c.params) ** 2)
        global_chisqr = g.residuals(g.params) ** 2
        assert_almost_equal(indiv_chisqr.sum(), global_chisqr.sum())
        # import time
        res = g.fit('differential_evolution')
        # start = time.time()
        # g.emcee(params=res.params, nwalkers=300, steps=500, workers=1)
        # finish = time.time()
        # print(finish - start)
        assert_almost_equal(res.chisqr, 0.774590447535, 4)

        # updating of constraints should happen during the fit
        assert_almost_equal(a.params['p12'].value, res.params['p12_d0'].value)
        assert_almost_equal(b.params['p12'].value, a.params['p12'].value)
        assert_almost_equal(c.params['p12'].value, a.params['p12'].value)

        g.params['p8_d0'].value = 10.123456
        # shouldn't need to call update constraints within the gfitter, that
        # happens when you retrieve a specific value
        assert_almost_equal(g.params['p8_d1'].value, g.params['p8_d0'].value)
        # However, you have to call model or residuals to redistribute the
        # parameters to the original fitters
        g.model()
        assert_almost_equal(a.params['p8'].value, 10.123456)
        assert_almost_equal(b.params['p8'].value, 10.123456)