예제 #1
0
    def setUp(self):
        self.xdata = np.linspace(-4, 4, 100)
        self.p0 = np.array([0., 1., 0.0, 1.])
        self.bounds = [(-1, 1), (0, 2), (-1, 1.), (0.001, 2)]

        self.params = curvefitter.to_parameters(self.p0 + 0.2, bounds=self.bounds)
        self.final_params = curvefitter.to_parameters(self.p0, bounds=self.bounds)

        self.ydata = gauss(self.xdata, self.final_params)
        self.f = CurveFitter(gauss, (self.xdata, self.ydata), self.params)
예제 #2
0
    def setUp(self):
        theoretical = np.loadtxt(os.path.join(path, 'gauss_data.txt'))
        xvals, yvals, evals = np.hsplit(theoretical, 3)
        self.xvals = xvals.flatten()
        self.yvals = yvals.flatten()
        self.evals = evals.flatten()

        self.best_weighted = [-0.00246095, 19.5299, -8.28446e-2, 1.24692]

        self.best_weighted_errors = [0.0220313708486, 1.12879436221,
                                     0.0447659158681, 0.0412022938883]

        self.best_weighted_chisqr = 77.6040960351

        self.best_unweighted = [-0.10584111872702096, 19.240347049328989,
                                0.0092623066070940396, 1.501362314145845]

        self.best_unweighted_errors = [0.34246565477, 0.689820935208,
                                       0.0411243173041, 0.0693429375282]

        self.best_unweighted_chisqr = 497.102084956

        self.p0 = np.array([0.1, 20., 0.1, 0.1])
        self.bounds = [(-1, 1), (0, 30), (-5., 5.), (0.001, 2)]

        self.params = curvefitter.to_parameters(self.p0, bounds=self.bounds)
예제 #3
0
    def setUp(self):
        coefs = np.zeros((16))
        coefs[0] = 2
        coefs[1] = 1.
        coefs[2] = 2.07
        coefs[4] = 6.36
        coefs[6] = 2.e-07
        coefs[7] = 3
        coefs[8] = 40
        coefs[9] = 3.47
        coefs[11] = 3
        coefs[12] = 200.
        coefs[13] = 2
        coefs[15] = 3
        self.coefs = coefs

        self.best_fit = np.array([2, 8.9692e-01, 2.07, 0., 6.36, 0.,
                                  3.3588842e-07, 2.8938204, 38.129,
                                  3.47, 0., 3.0, 2.5910e+02, 2.5406,
                                  0., 3.])

        lowlim = np.zeros(16)
        hilim = 2 * coefs
        self.params = curvefitter.to_parameters(coefs, bounds=zip(lowlim, hilim),
                                         varies=[False] * 16)

        fname = os.path.join(CURDIR, 'c_PLP0011859_q.txt')

        theoretical = np.loadtxt(fname)
        qvals, rvals, evals, dummy = np.hsplit(theoretical, 4)
        rvals = np.log10(rvals)
        self.f = curvefitter.CurveFitter(reflect_fitfunc, qvals.flatten(),
                                         rvals.flatten(), self.params)
예제 #4
0
파일: test_reflect.py 프로젝트: refnx/refnx
    def test_reflectivity_model(self):
        # test reflectivity calculation with values generated from Motofit
        params = curvefitter.to_parameters(self.coefs)

        fitfunc = reflect.ReflectivityFitFunction(dq=0.)
        model = fitfunc.model(self.qvals, params)

        assert_almost_equal(model, self.rvals)
예제 #5
0
파일: test_reflect.py 프로젝트: refnx/refnx
    def setUp(self):
        self.coefs = np.zeros(12)
        self.coefs[0] = 1.
        self.coefs[1] = 1.
        self.coefs[4] = 2.07
        self.coefs[7] = 3
        self.coefs[8] = 100
        self.coefs[9] = 3.47
        self.coefs[11] = 2

        self.layer_format = reflect.coefs_to_layer(self.coefs)

        theoretical = np.loadtxt(os.path.join(path, 'theoretical.txt'))
        qvals, rvals = np.hsplit(theoretical, 2)
        self.qvals = qvals.flatten()
        self.rvals = rvals.flatten()

        # e361 is an older dataset, but well characterised
        self.coefs361 = np.zeros(16)
        self.coefs361[0] = 2
        self.coefs361[1] = 1.
        self.coefs361[2] = 2.07
        self.coefs361[4] = 6.36
        self.coefs361[6] = 2e-5
        self.coefs361[7] = 3
        self.coefs361[8] = 10
        self.coefs361[9] = 3.47
        self.coefs361[11] = 4
        self.coefs361[12] = 200
        self.coefs361[13] = 1
        self.coefs361[15] = 3
        lowlim = np.zeros(16)
        lowlim[1] = 0.1
        lowlim[4] = 6.2
        hilim = 2 * self.coefs361

        bounds = list(zip(lowlim, hilim))
        e361 = np.loadtxt(os.path.join(path, 'e361r.txt'))
        self.qvals361, self.rvals361, self.evals361 = np.hsplit(e361, 3)
        np.seterr(invalid='raise')

        self.params361 = curvefitter.to_parameters(self.coefs361,
                                                   bounds=bounds,
                                                   varies=[False] * 16)
        fit = [1, 4, 6, 8, 12, 13]
        for p in fit:
            self.params361['p%d' % p].vary = True
예제 #6
0
    def test_globfit_modelvals_degenerate_layers(self):
        # try fitting dataset with a deposited layer split into two degenerate
        # layers
        coefs = np.zeros((20))
        coefs[0] = 3
        coefs[1] = 1.
        coefs[2] = 2.07
        coefs[4] = 6.36
        coefs[6] = 2e-6
        coefs[7] = 3
        coefs[8] = 30
        coefs[9] = 3.47
        coefs[11] = 4
        coefs[12] = 125
        coefs[13] = 2
        coefs[15] = 4
        coefs[16] = 125
        coefs[17] = 2
        coefs[19] = 4

        lowlim = np.zeros(20)
        hilim = 2 * coefs
        bounds = zip(lowlim, hilim)
        params = curvefitter.to_parameters(coefs,
                                           bounds=bounds,
                                           varies=[False] * 20)

        fit = np.array([6, 7, 8, 11, 12, 13, 15, 16, 17, 19])
        for p in fit:
            params['p%d' % p].vary = True

        self.f.params = params
        a = GlobalFitter([self.f], constraints=['d0:p16=d0:p12',
                                                'd0:p17=d0:p13',
                                                'd0:p19=d0:p15'])

        res = a.fit(method='differential_evolution')

        values = list(res.params.valuesdict().values())

        assert_equal(values[12], values[16])
        assert_equal(values[13], values[17])
        assert_equal(values[15], values[19])
예제 #7
0
파일: test_reflect.py 프로젝트: refnx/refnx
    def test_smeared_reflectivity_fitter(self):
        # test smeared reflectivity calculation with values generated from
        # Motofit (quadrature precsion order = 13)
        theoretical = np.loadtxt(os.path.join(path, 'smeared_theoretical.txt'))
        qvals, rvals, dqvals = np.hsplit(theoretical, 3)

        '''
        the order of the quadrature precision used to create these smeared
        values in Motofit was 13.
        Do the same here
        '''
        params = curvefitter.to_parameters(self.coefs)
        fitfunc = RFF(quad_order=13)
        fitter = CurveFitter(fitfunc,
                             (qvals, rvals),
                             params,
                             fcn_kws={'dqvals': dqvals})

        model = fitter.model(params)

        assert_almost_equal(model, rvals)
예제 #8
0
    def test_reflectivity_fit(self):
        # a smoke test to make sure the reflectivity fit proceeds
        params = curvefitter.to_parameters(self.coefs)
        params['p1'].value = 1.1

        fitfunc = reflect.ReflectivityFitFunction()
        fitter = CurveFitter(fitfunc, self.qvals, self.rvals, params)
        fitter.fit()

        transform = reflect.Transform('logY')
        yt, et = transform.transform(self.qvals361,
                                     self.rvals361,
                                     self.evals361)
        kws = {'transform':transform.transform}
        fitter2 = CurveFitter(fitfunc,
                              self.qvals361,
                              yt,
                              self.params361,
                              edata=et,
                              fcn_kws=kws)
        fitter2.fit('differential_evolution')
예제 #9
0
파일: test2.py 프로젝트: tjmurdoch/refnx
# coefs[6] is the background
# these will both be 1 and 0 respectively to start off with
coefs = reflect.layer_to_coefs(layers)
lowlim = reflect.layer_to_coefs(lowlim)
hilim = reflect.layer_to_coefs(hilim)

coefs[1] = 1.0
coefs[6] = 3.e-6

lowlim[1] = 0.6
hilim[1] = 1.2

lowlim[6] = 0.
hilim[6] = 9e-6

bounds = zip(lowlim, hilim)

# create a parameter instance
parameters = curvefitter.to_parameters(coefs, bounds=bounds, varies=[False] * 16)

# which parameters do you want to allow to vary
fitted_parameters = np.array([1, 6, 7, 8, 11, 12, 13, 15])
for fit in fitted_parameters:
    parameters['p%d' % fit].vary = True

# use resolution smearing and fit on a logR scale (transform the data as well)
fitter = reflect.ReflectivityFitFunction(xdata, ydata, parameters, edata=dydata,
                                    fcn_kws = {'dqvals': dxdata})

fitter.model(parameters)
예제 #10
0
 def test_abeles(self):
     # test reflectivity calculation with values generated from Motofit
     p = curvefitter.to_parameters(self.coefs)
     calc = reflect_fitfunc(self.qvals, p)
     calc = np.power(10, calc)
     assert_almost_equal(calc, self.rvals)
예제 #11
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)
예제 #12
0
파일: test.py 프로젝트: tjmurdoch/refnx
def run():
    DATASET_NAME = 'c_PLP0011859_q.txt'
    USE_DIFFERENTIAL_EVOLUTION = True

    #load the data
    dataset = ReflectDataset()
    with open(DATASET_NAME) as f:
        dataset.load(f)

    xdata, ydata, dydata, dxdata = dataset.data

    # create an array to hold the parameters, also create limits
    layers = np.array([[0,   2.07, 0, 0],     # fronting medium
                       [30,  3.47, 0, 3],     # 1st layer
                       [250, 2.00, 0, 3],     # 2nd layer
                       [0,   6.36, 0, 3]])     # backing medium

    lowlim = np.array([[0,   2.07, 0, 0],     # fronting medium
                       [15,  3.47, 0, 1],     # 1st layer
                       [200, 0.10, 0, 1],     # 2nd layer
                       [0,   6.36, 0, 1]])     # backing medium

    hilim = np.array([[0,   2.07, 0, 0],     # fronting medium
                      [50,  3.47, 0, 9],     # 1st layer
                      [300, 3.00, 0, 9],     # 2nd layer
                      [0,   6.36, 0, 9]])     # backing medium

    # create a linear array of the parameters
    # coefs[1] is the scale factor
    # coefs[6] is the background
    # these will both be 1 and 0 respectively to start off with
    coefs = reflect.layer_to_coefs(layers)
    lowlim = reflect.layer_to_coefs(lowlim)
    hilim = reflect.layer_to_coefs(hilim)

    coefs[1] = 1.0
    coefs[6] = 3.e-6

    lowlim[1] = 0.6
    hilim[1] = 1.2

    lowlim[6] = 0.
    hilim[6] = 9e-6

    bounds = zip(lowlim, hilim)

    # create a parameter instance
    parameters = curvefitter.to_parameters(coefs, bounds=bounds, varies=[False] * 16)

    # which parameters do you want to allow to vary
    fitted_parameters = np.array([1, 6, 7, 8, 11, 12, 13, 15])
    for fit in fitted_parameters:
        parameters['p%d' % fit].vary = True

    # use resolution smearing and fit on a logR scale (transform the data as well)
    t = reflect.Transform('logY').transform
    ydata, dydata = t(xdata, ydata, dydata)
    fcn_kws = {'dqvals': dxdata, 'transform': t}

    # create the fit instance
    fitter = reflect.ReflectivityFitFunction(xdata, ydata, parameters, edata=dydata,
                                        fcn_kws=fcn_kws)

    #do the fit
    np.random.seed(1)
    if USE_DIFFERENTIAL_EVOLUTION:
        fitter.fit(method='differential_evolution')
예제 #13
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)