예제 #1
0
 def test_fit_function_identical_to_strategy_method(self):
     model = SimpleModel()
     strategy = NmpfitStrategy(seed=123)
     strategy_result = strategy.fit(model, DATA)
     strategy_result.time = None
     model_result = fit(DATA, model, strategy=strategy)
     model_result.time = None
     self.assertEqual(strategy_result, model_result)
예제 #2
0
 def test_model_fit_method_identical_to_strategy_method(self):
     model = SimpleModel()
     strategy = NmpfitStrategy(seed=123)
     data = np.array(.5)
     strategy_result = strategy.fit(model, data)
     strategy_result.time = None
     model_result = model.fit(data, strategy)
     model_result.time = None
     self.assertEqual(strategy_result, model_result)
예제 #3
0
    def test_subset_data_fit_result_stores_model(self):
        model = self._make_model()
        holo = get_example_data('image0001')

        np.random.seed(40)
        fitter = NmpfitStrategy(npixels=100, maxiter=1)
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', UserWarning)
            # ignore not-converged warnings since we only do 2 iterations
            fitted = fitter.fit(model, holo)

        self.assertEqual(model, fitted.model)
예제 #4
0
    def test_subset_data_fit_result_is_saveable(self):
        model = self._make_model()
        holo = get_example_data('image0001')

        np.random.seed(40)
        fitter = NmpfitStrategy(npixels=100, maxiter=1)
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', UserWarning)
            # ignore not-converged warnings since we only do 2 iterations
            fitted = fitter.fit(model, holo)

        result = fitted  # was fix_flat(fitted)
        assert_read_matches_write(result)
예제 #5
0
    def test_fitted_uncertainties_similar_to_nmpfit(self):
        data = make_fake_data()
        model = make_model()

        fitter_scipy = LeastSquaresScipyStrategy(npixels=300)
        result_scipy = fitter_scipy.fit(model, data)
        uncertainties_scipy = pack_uncertainties_into_dict(result_scipy)

        fitter_nmp = NmpfitStrategy(npixels=300)
        result_nmp = fitter_nmp.fit(model, data)
        uncertainties_nmp = pack_uncertainties_into_dict(result_nmp)

        for key in uncertainties_scipy.keys():
            self.assertTrue(
                np.isclose(
                    uncertainties_scipy[key],
                    uncertainties_nmp[key],
                    rtol=0.1, atol=0))
예제 #6
0
    def test_fitted_parameters_similar_to_nmpfit(self):
        data = make_fake_data()
        model = make_model()

        np.random.seed(40)
        fitter_scipy = LeastSquaresScipyStrategy(npixels=300, ftol=1e-6)
        result_scipy = fitter_scipy.fit(model, data)
        params_scipy = result_scipy.parameters

        np.random.seed(40)
        fitter_nmp = NmpfitStrategy(npixels=300, ftol=1e-6)
        result_nmp = fitter_nmp.fit(model, data)
        params_nmp = result_nmp.parameters

        for key in params_scipy.keys():
            self.assertAlmostEqual(params_scipy[key],
                                   params_nmp[key],
                                   places=4)
예제 #7
0
def test_optimization_with_maxiter_of_2():
    gold_fit_dict = {
        '0:r': 0.52480509800531849,
        '1:center.1': 14.003687569304704,
        'alpha': 0.93045027963762217,
        '0:center.2': 19.93177549652841,
        '1:r': 0.56292664494653732,
        '0:center.1': 15.000340621607815,
        '1:center.0': 14.020984607646726,
        '0:center.0': 15.000222185576494,
        '1:center.2': 20.115613202192328
    }

    #calculate a hologram with known particle positions to do a fit against
    schema = detector_grid(shape=100, spacing=.1)

    s1 = Sphere(center=(15, 15, 20), n=1.59, r=0.5)
    s2 = Sphere(center=(14, 14, 20), n=1.59, r=0.5)
    cluster = Spheres([s1, s2])
    holo = calc_holo(schema, cluster, 1.33, .66, illum_polarization=(1, 0))

    #trying to do a fast fit:
    guess1 = Sphere(center=(prior.Uniform(5, 25, guess=15),
                            prior.Uniform(5, 25, 15), prior.Uniform(5, 25,
                                                                    20)),
                    r=(prior.Uniform(.4, .6, guess=.45)),
                    n=1.59)
    guess2 = Sphere(center=(prior.Uniform(5, 25, guess=14),
                            prior.Uniform(5, 25, 14), prior.Uniform(5, 25,
                                                                    20)),
                    r=(prior.Uniform(.4, .6, guess=.45)),
                    n=1.59)
    par_s = Spheres([guess1, guess2])

    model = AlphaModel(par_s,
                       medium_index=1.33,
                       illum_wavelen=.66,
                       illum_polarization=(1, 0),
                       alpha=prior.Uniform(.1, 1, .6))
    optimizer = Nmpfit(maxiter=2)
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        result = optimizer.fit(model, holo)
    assert_obj_close(gold_fit_dict, result.parameters, rtol=1e-5)