Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

fitbenchmarking / fitbenchmarking / 2330948078

16 May 2022 - 9:26 coverage decreased (-0.2%) to 86.542%
2330948078

Pull #1036

github

GitHub
Merge f94846412 into ed08b4d37
Pull Request #1036: Enable max runtime in curve fitting

0 of 46 new or added lines in 6 files covered. (0.0%)

5 existing lines in 4 files now uncovered.

3421 of 3953 relevant lines covered (86.54%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/fitbenchmarking/controllers/horace_controller.py
1
"""
2
Implements a controller to the lm implementation in Herbert/Horace
3
"""
4

5
import matlab
!
6
import numpy as np
!
7

8
from fitbenchmarking.controllers.base_controller import Controller
!
9
from fitbenchmarking.controllers.matlab_mixin import MatlabMixin
!
10

11

12
class HoraceController(MatlabMixin, Controller):
!
13
    """
14
    Controller for fit in Herbert
15
    """
16

17
    algorithm_check = {
!
18
        'all': ['lm-lsqr'],
19
        'ls': ['lm-lsqr'],
20
        'deriv_free': ['lm-lsqr'],
21
        'general': [],
22
        'simplex': [],
23
        'trust_region': [],
24
        'levenberg-marquardt': ['lm-lsqr'],
25
        'gauss_newton': [],
26
        'bfgs': [],
27
        'conjugate_gradient': [],
28
        'steepest_descent': [],
29
        'global_optimization': []}
30

31
    incompatible_problems = ['mantid']
!
32

33
    def __init__(self, cost_func):
!
34
        """
35
        Initialises variables used for temporary storage.
36

37
        :param cost_func: Cost function object selected from options.
38
        :type cost_func: subclass of
39
                :class:`~fitbenchmarking.cost_func.base_cost_func.CostFunc`
40
        """
41
        super().__init__(cost_func)
!
42
        self._fit_params = None
!
43

44
    def setup(self):
!
45
        """
46
        Setup for Matlab fitting
47
        """
48
        # Initialize Horace in the Matlab engine
49
        self.eng.evalc('horace_on')
!
50

51
        # Convert initial params into matlab array
52
        self.eng.workspace['x_mat'] = matlab.double(
!
53
            self.data_x.tolist())
54
        self.eng.workspace['y_mat'] = matlab.double(
!
55
            np.zeros(self.data_y.shape).tolist())
56
        self.eng.workspace['e_mat'] = matlab.double(
!
57
            np.ones(self.data_y.shape).tolist())
58
        self.eng.evalc("W = struct('x', x_mat, 'y', y_mat, 'e', e_mat)")
!
59
        self.eng.workspace['initial_params'] = matlab.double(
!
60
            [self.initial_params])
61

62
        # serialize cost_func.eval_r and open within matlab engine
63
        # so that matlab fitting function can be called
NEW
64
        self.eng.workspace['eval_f'] = self.py_to_mat('eval_r')
!
65
        self.eng.evalc('f_wrapper = @(x, p) double(eval_f(p))')
!
66

67
        # Setup multifit data structures
68
        self.eng.evalc('kk = multifit(W)')
!
69
        self.eng.evalc('kk = kk.set_fun(f_wrapper)')
!
70
        self.eng.evalc('kk = kk.set_pin(initial_params)')
!
71

72
    def fit(self):
!
73
        """
74
        Run problem with Horace
75
        """
76
        self.eng.evalc('[fitted_data, fit_params] = kk.fit')
!
77
        self._fit_params = self.eng.workspace['fit_params']
!
78

79
    def cleanup(self):
!
80
        """
81
        Convert the result to a numpy array and populate the variables results
82
        will be read from.
83
        """
84
        if int(self._fit_params['converged']) == 0:
!
85
            self.flag = 2
!
86
        else:
87
            self.flag = 0
!
88

89
        self.final_params = self._fit_params['p'][0]
!
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2022 Coveralls, Inc