def setUp(self): self.opts = Options() self.opts.use_errors = True self.prob = FittingProblem(self.opts) self.prob.data_x = np.array([1, 2, 4, 3, 5]) self.prob.sorted_index = np.array([0, 1, 3, 2, 4]) self.prob.data_y = np.array([4, 3, 5, 2, 1]) self.prob.data_e = np.array([0.5, 0.2, 0.3, 0.1, 0.4]) self.prob.starting_values = [{'x': 1, 'y': 2}] self.prob.name = 'full name' self.prob.eval_model = lambda y, x: y[0] * x + y[1] cost_func = NLLSCostFunc(self.prob) jac = Scipy(cost_func) jac.method = "2-point" self.fr = FittingResult(options=self.opts, cost_func=cost_func, jac=jac, chi_sq=1.0, initial_params=[1.8], params=[1.2], runtime=2.0, minimizer='fit', error_flag=1) self.opts = Options() self.opts.use_errors = True self.dir = TemporaryDirectory() self.plot = plots.Plot(best_result=self.fr, options=self.opts, figures_dir=self.dir.name)
def generate_mock_results(): """ Generates results to test against :return: best results calculated using the chi_sq value, list of results and the options :rtype: tuple(list of best results, list of list fitting results, Options object) """ software = 'scipy_ls' options = Options() options.software = [software] num_min = len(options.minimizers[options.software[0]]) data_x = np.array([[1, 4, 5], [2, 1, 5]]) data_y = np.array([[1, 2, 1], [2, 2, 2]]) data_e = np.array([[1, 1, 1], [1, 2, 1]]) func = [fitting_function_1, fitting_function_2] problems = [FittingProblem(options), FittingProblem(options)] params_in = [[[.3, .11], [.04, 2], [3, 1], [5, 0]], [[4, 2], [3, .006], [.3, 10], [9, 0]]] starting_values = [{"a": .3, "b": .11}, {"a": 0, "b": 0}] error_in = [[1, 0, 2, 0], [0, 1, 3, 1]] link_in = [['link1', 'link2', 'link3', 'link4'], ['link5', 'link6', 'link7', 'link8']] min_chi_sq = [1, 1] acc_in = [[1, 5, 2, 1.54], [7, 3, 5, 1]] min_runtime = [4.2e-5, 5.0e-14] runtime_in = [[1e-2, 2.2e-3, 4.2e-5, 9.8e-1], [3.0e-10, 5.0e-14, 1e-7, 4.3e-12]] results_out = [] for i, p in enumerate(problems): p.data_x = data_x[i] p.data_y = data_y[i] p.data_e = data_e[i] p.function = func[i] p.name = "prob_{}".format(i) results = [] for j in range(num_min): p.starting_values = starting_values cost_func = NLLSCostFunc(p) jac = Scipy(cost_func) jac.method = '2-point' r = FittingResult(options=options, cost_func=cost_func, jac=jac, initial_params=starting_values, params=params_in[i][j]) r.chi_sq = acc_in[i][j] r.runtime = runtime_in[i][j] r.error_flag = error_in[i][j] r.support_page_link = link_in[i][j] r.minimizer = options.minimizers[software][j] results.append(r) results_out.append(results) return results_out, options, min_chi_sq, min_runtime
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_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 generate_mock_results(self): """ Generates results to test against :return: A list of results objects along with expected values for normallised accuracy and runtimes :rtype: tuple(list of FittingResults, list of list of float, list of list of float) """ self.num_problems = 4 self.num_minimizers = 2 results = [] options = Options() problem = FittingProblem(options) problem.starting_values = [{'a': 1, 'b': 2, 'c': 3}] acc_in = [[1, 5], [7, 3], [10, 8], [2, 3]] runtime_in = [[float('Inf'), 2.2e-3], [3.0e-10, 5.0e-14], [6.9e-7, 4.3e-5], [1.6e-13, 1.8e-13]] acc_expected = [] runtime_expected = [] for i in range(self.num_problems): acc_results = acc_in[i][:] acc_expected.append(list(acc_results) / np.min(acc_results)) runtime_results = runtime_in[i][:] runtime_expected.append( list(runtime_results) / np.min(runtime_results)) prob_results = [] cost_func = NLLSCostFunc(problem) jac = Scipy(cost_func) jac.method = "2-point" for j in range(self.num_minimizers): minimizer = 'min_{}'.format(j) prob_results.append( FittingResult(options=options, cost_func=cost_func, jac=jac, initial_params=[1, 2, 3], params=[1, 2, 3], chi_sq=acc_results[j], runtime=runtime_results[j], minimizer=minimizer)) results.append(prob_results) return results, acc_expected, runtime_expected
def test_init_with_dataset_id(self): """ Tests to check that the multifit id is setup correctly """ chi_sq = [10, 5, 1] minimizer = "test_minimizer" runtime = 0.01 params = [ np.array([1, 3, 4, 4]), np.array([2, 3, 57, 8]), np.array([4, 2, 5, 1]) ] initial_params = np.array([0, 0, 0, 0]) self.problem.data_x = [ np.array([3, 2, 1, 4]), np.array([5, 1, 2, 3]), np.array([6, 7, 8, 1]) ] self.problem.data_y = [ np.array([2, 1, 7, 40]), np.array([8, 9, 4, 2]), np.array([7, 4, 4, 2]) ] self.problem.data_e = [ np.array([1, 1, 1, 1]), np.array([2, 2, 2, 1]), np.array([2, 3, 4, 4]) ] self.problem.sorted_index = [ np.array([2, 1, 0, 3]), np.array([1, 2, 3, 0]), np.array([3, 0, 1, 2]) ] self.cost_func = NLLSCostFunc(self.problem) self.jac = Scipy(self.cost_func) self.jac.method = "2-point" self.result = FittingResult(options=self.options, cost_func=self.cost_func, jac=self.jac, chi_sq=chi_sq, runtime=runtime, minimizer=minimizer, initial_params=initial_params, params=params, error_flag=0, dataset_id=1) self.assertTrue( np.isclose(self.result.data_x, self.problem.data_x[1]).all()) self.assertTrue( np.isclose(self.result.data_y, self.problem.data_y[1]).all()) self.assertTrue( np.isclose(self.result.data_e, self.problem.data_e[1]).all()) self.assertTrue( np.isclose(self.result.sorted_index, self.problem.sorted_index[1]).all()) self.assertTrue(np.isclose(params[1], self.result.params).all()) self.assertEqual(chi_sq[1], self.result.chi_sq)
def setUp(self): """ Setting up FitBenchmarking results object """ self.options = Options() mock_problems_dir = os.path.dirname(inspect.getfile(mock_problems)) problem_dir = os.path.join(mock_problems_dir, "cubic.dat") self.problem = parse_problem_file(problem_dir, self.options) self.problem.correct_data() self.chi_sq = 10 self.minimizer = "test_minimizer" self.runtime = 0.01 self.params = np.array([1, 3, 4, 4]) self.initial_params = np.array([0, 0, 0, 0]) self.cost_func = NLLSCostFunc(self.problem) self.jac = Scipy(self.cost_func) self.jac.method = "2-point" self.result = FittingResult(options=self.options, cost_func=self.cost_func, jac=self.jac, chi_sq=self.chi_sq, runtime=self.runtime, minimizer=self.minimizer, initial_params=self.initial_params, params=self.params, error_flag=0) self.min_chi_sq = 0.1 self.result.min_chi_sq = self.min_chi_sq self.min_runtime = 1 self.result.min_runtime = self.min_runtime
def setUp(self): self.cost_func = make_cost_func() self.problem = self.cost_func.problem self.jac = Scipy(self.cost_func) self.jac.method = '2-point' self.shared_tests = ControllerSharedTesting()