def test_eval_model_correct_evaluation(self): """ Test that eval_model is running the correct function """ fitting_problem = FittingProblem(self.options) fitting_problem.function = lambda x, p1: x + p1 x_val = np.array([1, 8, 11]) eval_result = fitting_problem.eval_model(x=x_val, params=[5]) self.assertTrue(all(eval_result == np.array([6, 13, 16]))) fitting_problem.data_x = np.array([20, 21, 22]) eval_result = fitting_problem.eval_model(params=[5]) self.assertTrue(all(eval_result == np.array([25, 26, 27])))
class TestJacobianClass(TestCase): """ Tests for Jacobian classes """ def setUp(self): """ Setting up tests """ options = Options() options.cost_func_type = "nlls" self.fitting_problem = FittingProblem(options) self.fitting_problem.function = f self.fitting_problem.jacobian = J self.fitting_problem.data_x = np.array([1, 2, 3, 4, 5]) self.fitting_problem.data_y = np.array([1, 2, 4, 8, 16]) self.cost_func = NLLSCostFunc(self.fitting_problem) self.params = [6, 0.1] self.actual = J(x=self.fitting_problem.data_x, p=self.params) def test_scipy_two_point_eval(self): """ Test for ScipyTwoPoint evaluation is correct """ jac = Scipy(self.cost_func) jac.method = '2-point' eval_result = jac.eval(params=self.params) self.assertTrue(np.isclose(self.actual, eval_result).all()) def test_scipy_three_point_eval(self): """ Test for ScipyThreePoint evaluation is correct """ jac = Scipy(self.cost_func) jac.method = '3-point' eval_result = jac.eval(params=self.params) self.assertTrue(np.isclose(self.actual, eval_result).all()) def test_scipy_cs_eval(self): """ Test for ScipyCS evaluation is correct """ jac = Scipy(self.cost_func) jac.method = 'cs' eval_result = jac.eval(params=self.params) self.assertTrue(np.isclose(self.actual, eval_result).all()) def test_analytic_cutest_no_errors(self): """ Test analytic Jacobian """ self.fitting_problem.options.cost_func_type = "nlls" self.fitting_problem.format = "cutest" jac = Analytic(self.cost_func) eval_result = jac.eval(params=self.params) self.assertTrue(np.isclose(self.actual, eval_result).all()) def test_analytic_cutest_weighted(self): """ Test analytic Jacobian """ self.fitting_problem.options.cost_func_type = "weighted_nlls" e = np.array([1, 2, 1, 3, 1]) self.fitting_problem.data_e = e self.fitting_problem.format = "cutest" jac = Analytic(self.cost_func) eval_result = jac.eval(params=self.params) scaled_actual = self.actual / e[:, None] self.assertTrue(np.isclose(scaled_actual, eval_result).all()) def test_analytic_cutest_root(self): """ Test analytic Jacobian """ self.fitting_problem.options.cost_func_type = "root_nlls" self.fitting_problem.format = "cutest" jac = Analytic(self.cost_func) eval_result = jac.eval(params=self.params) scaled_actual = self.actual * \ self.fitting_problem.eval_model(self.params)[:, None] / 2 self.assertTrue(np.isclose(scaled_actual, eval_result).all()) def test_analytic_raise_error(self): """ Test analytic Jacobian raises an exception when problem.jacobian is not callable """ self.fitting_problem.jacobian = None with self.assertRaises(exceptions.NoJacobianError): Analytic(self.cost_func)