Esempio n. 1
0
def load_expectation(filename):
    """
    Load an expected fitting problem from a json file.

    :param filename: The path to the expectation file
    :type filename: string
    :return: A fitting problem to test against
    :rtype: fitbenchmarking.parsing.FittingProblem
    """
    with open(filename, 'r') as f:
        expectation_dict = load(f)

    expectation = FittingProblem()
    expectation.name = expectation_dict['name']
    expectation.start_x = expectation_dict['start_x']
    expectation.end_x = expectation_dict['end_x']
    expectation.data_x = np.array(expectation_dict['data_x'])
    expectation.data_y = np.array(expectation_dict['data_y'])
    expectation.data_e = expectation_dict['data_e']
    if expectation.data_e is not None:
        expectation.data_e = np.array(expectation.data_e)
    expectation.functions = expectation_dict['functions']
    expectation.starting_values = expectation_dict['starting_values']
    expectation.value_ranges = expectation_dict['value_ranges']

    return expectation
Esempio n. 2
0
    def test_verify_problem(self):
        """
        Test that verify only passes if all required values are set.
        """
        fitting_problem = FittingProblem()
        with self.assertRaises(TypeError):
            fitting_problem.verify()
            self.fail('verify() passes when no values are set.')

        fitting_problem.starting_values = [['p1', [1]], ['p2', [2]]]
        with self.assertRaises(TypeError):
            fitting_problem.verify()
            self.fail('verify() passes starting values are set.')

        fitting_problem.data_x = np.array([1, 2, 3, 4, 5])
        with self.assertRaises(TypeError):
            fitting_problem.verify()
            self.fail('verify() passes when data_x is set.')

        fitting_problem.data_y = np.array([1, 2, 3, 4, 5])
        with self.assertRaises(TypeError):
            fitting_problem.verify()
            self.fail('verify() passes when data_y is set.')

        fitting_problem.functions = [[lambda x, p1, p2: p1 + p2, [1, 2]]]
        try:
            fitting_problem.verify()
        except TypeError:
            self.fail('verify() fails when all required values set.')

        fitting_problem.data_x = [1, 2, 3]
        with self.assertRaises(TypeError):
            fitting_problem.verify()
            self.fail('verify() passes for x values not numpy.')
Esempio n. 3
0
 def test_eval_starting_params(self):
     """
     Test that eval_starting_params returns the correct result
     """
     fitting_problem = FittingProblem()
     self.assertRaises(AttributeError,
                       fitting_problem.eval_starting_params,
                       function_id=0)
     fitting_problem.functions = [[lambda x, p1: x + p1, [3]],
                                  [lambda x, p1: x + p1 + 10, [7]]]
     fitting_problem.data_x = np.array([1])
     eval_result = fitting_problem.eval_starting_params(0)
     self.assertTrue(all(eval_result == np.array([4])))
     eval_result = fitting_problem.eval_starting_params(1)
     self.assertTrue(all(eval_result == np.array([18])))
Esempio n. 4
0
    def parse(self):
        """
        Parse the Fitbenchmark problem file into a Fitting Problem.

        :return: The fully parsed fitting problem
        :rtype: fitbenchmarking.parsing.fitting_problem.FittingProblem
        """
        fitting_problem = FittingProblem()

        self._entries = self._get_fitbenchmark_data_problem_entries()
        self._parsed_func = self._parse_function()

        fitting_problem.name = self._entries['name']

        data_points = self._get_data_points()

        fitting_problem.data_x = data_points[:, 0]
        fitting_problem.data_y = data_points[:, 1]
        if data_points.shape[1] > 2:
            fitting_problem.data_e = data_points[:, 2]

        # String containing the function name(s) and the starting parameter
        # values for each function
        self._mantid_equation = self._entries['function']

        fitting_problem.functions = self._fitbenchmark_func_definitions()

        # Print number of equations until better way of doing this is looked at
        equation_count = len(self._parsed_func)
        fitting_problem.equation = '{} Functions'.format(equation_count)

        fitting_problem.starting_values = self._get_starting_values()

        # start and end values in x range
        if 'fit_parameters' in self._entries:
            start_x, end_x = self._get_x_range()
            fitting_problem.start_x = start_x
            fitting_problem.end_x = end_x

        return fitting_problem
Esempio n. 5
0
    def parse(self):

        fitting_problem = FittingProblem()

        equation, data, starting_values, name = self._parse_line_by_line()
        data = self._parse_data(data)

        fitting_problem.data_x = data[:, 1]
        fitting_problem.data_y = data[:, 0]
        if len(data[0, :]) > 2:
            fitting_problem.data_e = data[:, 2]

        fitting_problem.name = name

        # String containing a mathematical expression
        fitting_problem.equation = self._parse_equation(equation)

        fitting_problem.starting_values = starting_values

        fitting_problem.functions = \
            nist_func_definitions(function=fitting_problem.equation,
                                  startvals=fitting_problem.starting_values)

        return fitting_problem
Esempio n. 6
0
 def test_eval_f(self):
     """
     Test that eval_f is running the correct function
     """
     fitting_problem = FittingProblem()
     self.assertRaises(AttributeError,
                       fitting_problem.eval_f,
                       x=2,
                       params=[1, 2, 3],
                       function_id=0)
     fitting_problem.functions = [[lambda x, p1: x + p1, [3]],
                                  [lambda x, p1: x + p1 + 10, [7]]]
     x_val = np.array([1])
     eval_result = fitting_problem.eval_f(x=x_val,
                                          params=[5],
                                          function_id=0)
     self.assertTrue(all(eval_result == np.array([6])))
     eval_result = fitting_problem.eval_f(x=x_val,
                                          params=[5],
                                          function_id=1)
     self.assertTrue(all(eval_result == np.array([16])))
     fitting_problem.data_x = np.array([20, 21, 22])
     eval_result = fitting_problem.eval_f(params=[5], function_id=0)
     self.assertTrue(all(eval_result == np.array([25, 26, 27])))