Beispiel #1
0
 def setUp(self):
     """
     Setting up nonlinear least squares cost function tests
     """
     self.options = Options()
     fitting_problem = FittingProblem(self.options)
     self.cost_function = NLLSCostFunc(fitting_problem)
     fitting_problem.function = lambda x, p1: x + p1
     self.x_val = np.array([1, 8, 11])
     self.y_val = np.array([6, 10, 20])
Beispiel #2
0
    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)
Beispiel #3
0
    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
Beispiel #4
0
    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 setUp(self):
     """
     Setting up tests
     """
     options = Options()
     options.cost_func_type = "nlls"
     self.fitting_problem = FittingProblem(options)
     self.cost_func = NLLSCostFunc(self.fitting_problem)
     self.jacobian = Jacobian(self.cost_func)
Beispiel #7
0
class TestNLLSCostFunc(TestCase):
    """
    Class to test the NLLSCostFunc class
    """
    def setUp(self):
        """
        Setting up nonlinear least squares cost function tests
        """
        self.options = Options()
        fitting_problem = FittingProblem(self.options)
        self.cost_function = NLLSCostFunc(fitting_problem)
        fitting_problem.function = lambda x, p1: x + p1
        self.x_val = np.array([1, 8, 11])
        self.y_val = np.array([6, 10, 20])

    def test_eval_r_raise_error(self):
        """
        Test that eval_r raises and error
        """
        self.assertRaises(exceptions.CostFuncError,
                          self.cost_function.eval_r,
                          params=[1, 2, 3],
                          x=[2],
                          y=[3, 4])

    def test_eval_r_correct_evaluation(self):
        """
        Test that eval_r is running the correct function
        """
        eval_result = self.cost_function.eval_r(x=self.x_val,
                                                y=self.y_val,
                                                params=[5])
        self.assertTrue(all(eval_result == np.array([0, -3, 4])))

    def test_eval_cost(self):
        """
        Test that eval_cost is correct
        """
        eval_result = self.cost_function.eval_cost(params=[5],
                                                   x=self.x_val,
                                                   y=self.y_val)
        self.assertEqual(eval_result, 25)
 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)
Beispiel #9
0
def make_cost_function(file_name='cubic.dat', minimizers=None):
    """
    Helper function that returns a simple fitting problem
    """
    options = Options()
    if minimizers:
        options.minimizers = minimizers

    bench_prob_dir = os.path.dirname(inspect.getfile(mock_problems))
    fname = os.path.join(bench_prob_dir, file_name)

    fitting_problem = parse_problem_file(fname, options)
    fitting_problem.correct_data()
    cost_func = NLLSCostFunc(fitting_problem)
    return cost_func
    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