def test_SimulationBuilderCallback(self): """ Testing simulation construction using Python callback """ pars = ba.Parameters() pars.add(ba.Parameter("par0", 0.0)) pars.add(ba.Parameter("par1", 1.0)) builder = SimulationBuilder() data = builder.create_data() # adding simulation callback and experimental data objective = ba.FitObjective() objective.addSimulationAndData(builder.build_simulation, data, 1.0) self.assertEqual(builder.m_ncalls, 0) self.assertEqual(objective.numberOfFitElements(), 0) # running objective function objective.evaluate(pars) self.assertEqual(builder.m_ncalls, 1) self.assertEqual(objective.numberOfFitElements(), builder.size()) self.assertEqual(builder.m_pars["par0"], 0.0) self.assertEqual(builder.m_pars["par1"], 1.0) # checking arrays of experimental and simulated data expected_sim = [] expected_data = [] for i in range(0, builder.size()): expected_sim.append(0.0) expected_data.append(1.0) self.assertEqual(expected_sim, list(objective.simulation_array())) self.assertEqual(expected_data, list(objective.experimental_array()))
def __init__(self): self.m_x = np.linspace(0.0, 10.0, 100) self.m_params = ba.Parameters() self.m_params.add(ba.Parameter('amp', 10.0)) self.m_params.add(ba.Parameter('decay', 0.05)) self.m_params.add(ba.Parameter('phase', 1.0)) self.m_params.add(ba.Parameter('frequency', 4.0)) self.m_eps_data = np.linspace(0.0, 10.0, 100) self.m_eps_data.fill(0.01) self.m_data = decaying_sin(self.m_params, self.m_x)
def test_ParametersSetIterator(self): """ Testing of python iterator over defined fit parameters. """ pars = ba.Parameters() self.assertEqual(pars.size(), 0) pars.add(ba.Parameter("par0", 1.0, ba.AttLimits.limitless())) pars.add(ba.Parameter("par1", 2.0, ba.AttLimits.limitless())) expected_names = ["par0", "par1"] for index, p in enumerate(pars): self.assertEqual(p.name(), expected_names[index])
def test_DecayingSinFit(self): params = ba.Parameters() params.add(ba.Parameter('amp', 1, ba.AttLimits.positive())) params.add(ba.Parameter('decay', 0.1, ba.AttLimits.positive())) params.add(ba.Parameter('phase', 0.1, ba.AttLimits.limited(0.0, 3.1))) params.add(ba.Parameter('frequency', 1.0, ba.AttLimits.positive())) model = DecayingSin() minimizer = ba.Minimizer() result = minimizer.minimize(model.objective_function, params) print(result.toString()) # check found parameter values np.testing.assert_almost_equal(result.parameters().values(), model.m_params.values(), 3)
def test_ParameterAttribute(self): """ Testing p.value attribute """ par = ba.Parameter("par", 1.0) self.assertEqual(par.value, 1.0) par.value = 42.0 self.assertEqual(par.value, 42.0)
def test_SimpleMinimizer(self): minimizer = ba.Minimizer() minimizer.setMinimizer("Test") pars = ba.Parameters() pars.add(ba.Parameter("par0", 0.0)) pars.add(ba.Parameter("par1", 1.0)) pars.add(ba.Parameter("par2", 2.0)) helper = TestMinimizerHelper() result = minimizer.minimize(helper.objective_function, pars) # return value of objective function was propagated to MinimizerResult self.assertEqual(result.minValue(), 42.0) # objective function was called twice #(once by test minimizer, and second time during return type deduction) self.assertEqual(helper.m_ncalls, 2) # starting values of fit parameters were correctly send to objective func self.assertEqual(list(helper.m_pars.values()), [0.0, 1.0, 2.0])
def test_RosenbrockFit(self): """ Testing fit of rosenbrock function """ params = ba.Parameters() params.add( ba.Parameter("x", -1.2, ba.AttLimits.limited(-5.0, 5.0), 0.01)) params.add( ba.Parameter("y", 1.0, ba.AttLimits.limited(-5.0, 5.0), 0.01)) model = Rosenbrock() minimizer = ba.Minimizer() result = minimizer.minimize(model.objective_function, params) print(result.toString()) # check found parameter values np.testing.assert_almost_equal(result.parameters().values(), model.m_expected_params, 3) # check found minimum np.testing.assert_almost_equal(result.minValue(), model.m_expected_minimum, 3)
def test_FittingObserver(self): """ Testing simulation construction using Python callback """ pars = ba.Parameters() pars.add(ba.Parameter("par0", 0.0)) pars.add(ba.Parameter("par1", 1.0)) # adding simulation callback and experimental data builder = SimulationBuilder() data = builder.create_data() objective = ba.FitObjective() objective.addSimulationAndData(builder.build_simulation, data, 1.0) # adding observer observer = FitObserver() objective.initPlot(5, observer.update) # running objective function 11 times for i in range(0, 11): objective.evaluate(pars) self.assertEqual(observer.m_ncalls, 3) self.assertEqual(observer.m_iterations, [1, 6, 11])