Skip to content

Commit

Permalink
Merge e4b134b into 99eed25
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew McCluskey committed Sep 19, 2019
2 parents 99eed25 + e4b134b commit 341f128
Show file tree
Hide file tree
Showing 49 changed files with 394 additions and 542 deletions.
8 changes: 4 additions & 4 deletions fitbenchmarking/fitbenchmark_one_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import sys
import numpy as np

from fitting import prerequisites as prereq
from fitting import misc
from fitting.plotting import plots
from fitbenchmarking.fitting import prerequisites as prereq
from fitbenchmarking.fitting import misc
from fitbenchmarking.fitting.plotting import plots

from utils.logging_setup import logger
from fitbenchmarking.utils.logging_setup import logger


def fitbm_one_prob(user_input, problem):
Expand Down
1 change: 0 additions & 1 deletion fitbenchmarking/fitting/mantid/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

13 changes: 7 additions & 6 deletions fitbenchmarking/fitting/mantid/externals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
from __future__ import (absolute_import, division, print_function)

import mantid.simpleapi as msapi
from utils.logging_setup import logger
from fitbenchmarking.utils.logging_setup import logger


def gen_func_obj(function_name, params_set):
"""
Generates a mantid function object.
@param function_name :: the name of the function to be generated
@params_set :: set of parameters per function extracted from the problem definition file
@params_set :: set of parameters per function extracted from the
problem definition file
@returns :: mantid function object that can be called in python
"""
params_set = (params_set.split(', ties'))[0]

exec "function_object = msapi." + function_name + "("+ params_set +")"
exec "function_object = msapi." + function_name + "(" + params_set + ")"

return function_object

Expand All @@ -39,12 +40,12 @@ def set_ties(function_object, ties):
for tie in ties_per_func:
"""
param_str is a string of the parameter name in the mantid format
For a Mantid Composite Function, a formatted parameter name would
For a Mantid Composite Function, a formatted parameter name would
start with the function number and end with the parameter name.
For instance, f0.A would refer to a parameter A of the first
For instance, f0.A would refer to a parameter A of the first
function is a Composite Function.
"""
param_str = 'f'+str(idx)+'.'+(tie.split("'"))[0]
param_str = 'f' + str(idx) + '.' + (tie.split("'"))[0]
function_object.fix(param_str)

return function_object
Expand Down
15 changes: 7 additions & 8 deletions fitbenchmarking/fitting/mantid/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import numpy as np
import mantid.simpleapi as msapi

from utils.logging_setup import logger
from fitting import misc
from fitting.plotting import plot_helper
from fitbenchmarking.utils.logging_setup import logger
from fitbenchmarking.fitting import misc
from fitbenchmarking.fitting.plotting import plot_helper


MAX_FLOAT = sys.float_info.max
Expand Down Expand Up @@ -41,13 +41,14 @@ def benchmark(problem, wks_created, function, minimizers, cost_function):
chi_sq, min_chi_sq, best_fit = \
chisq(status, fit_wks, min_chi_sq, best_fit, minimizer)
individual_result = \
misc.create_result_entry(problem, status, chi_sq, runtime, minimizer,
function, fin_function_def)
misc.create_result_entry(problem, status, chi_sq, runtime,
minimizer, function, fin_function_def)

results_problem.append(individual_result)

return results_problem, best_fit


def fit(problem, wks_created, function, minimizer,
cost_function='Least squares'):
"""
Expand All @@ -65,8 +66,6 @@ def fit(problem, wks_created, function, minimizer,
the final function definition
and how much time it took for the fit to finish (float)
"""


fit_result, t_start, t_end = None, None, None
try:
ignore_invalid = get_ignore_invalid(problem, cost_function)
Expand Down Expand Up @@ -127,7 +126,7 @@ def parse_result(fit_result, t_start, t_end):

status = 'failed'
fit_wks, fin_function_def, runtime = None, None, np.nan
if not fit_result is None:
if fit_result is not None:
status = fit_result.OutputStatus
fit_wks = fit_result.OutputWorkspace
fin_function_def = str(fit_result.Function)
Expand Down
5 changes: 2 additions & 3 deletions fitbenchmarking/fitting/mantid/prepare_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

from __future__ import (absolute_import, division, print_function)

import copy
import numpy as np
import mantid.simpleapi as msapi

from utils.logging_setup import logger


def wks_cost_function(problem, use_errors=True):
"""
Expand Down Expand Up @@ -38,6 +35,7 @@ def wks_cost_function(problem, use_errors=True):

return wks_created, cost_function


def setup_errors(problem):
"""
Gets errors on the data points from the problem object if there are
Expand All @@ -57,6 +55,7 @@ def setup_errors(problem):
# True errors
return problem.data_e


def convert_back(wks_used, problem, use_errors):
"""
Convert back so data is of equal lengths.
Expand Down
1 change: 0 additions & 1 deletion fitbenchmarking/fitting/mantid/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

241 changes: 119 additions & 122 deletions fitbenchmarking/fitting/mantid/tests/test_func_def.py
Original file line number Diff line number Diff line change
@@ -1,135 +1,132 @@
from __future__ import (absolute_import, division, print_function)

import unittest
import os
import numpy as np
import mantid.simpleapi as msapi

import sys
test_dir = os.path.dirname(os.path.realpath(__file__))
parent_dir = os.path.dirname(os.path.normpath(test_dir))
parent_dir = os.path.dirname(os.path.normpath(parent_dir))
main_dir = os.path.dirname(os.path.normpath(parent_dir))
sys.path.insert(0, main_dir)
from fitbenchmarking.fitting.mantid.func_def import (
function_definitions
)
from fitbenchmarking.fitting.mantid.func_def import (
parse_function_definitions
)

from fitting.mantid.func_def import function_definitions
from fitting.mantid.func_def import parse_function_definitions
from fitbenchmarking.parsing.parse_nist_data import (
FittingProblem as NISTFittingProblem
)
from fitbenchmarking.parsing.parse_fitbenchmark_data import (
FittingProblem as FBFittingProblem
)
from fitbenchmarking.mock_problem_files.get_problem_files import get_file

from parsing.parse_nist_data import FittingProblem as NISTFittingProblem
from parsing.parse_fitbenchmark_data import FittingProblem as FBFittingProblem
from mock_problem_files.get_problem_files import get_file

class MantidTests(unittest.TestCase):

def NIST_problem(self):
"""
Helper function.
Sets up the problem object for the nist problem file Misra1a.dat
"""

data_pattern = np.array([[10.07, 77.6],
[14.73, 114.9],
[17.94, 141.1],
[23.93, 190.8],
[29.61, 239.9],
[35.18, 289.0],
[40.02, 332.8],
[44.82, 378.4],
[50.76, 434.8],
[55.05, 477.3],
[61.01, 536.8],
[66.40, 593.1],
[75.47, 689.1],
[81.78, 760.0]])

fname = get_file('NIST_Misra1a.dat')
prob = NISTFittingProblem(fname)
prob.name = 'Misra1a'
prob.equation = 'b1*(1-exp(-b2*x))'
prob.starting_values = [['b1', [500.0, 250.0]],
['b2', [0.0001, 0.0005]]]
prob.data_x = data_pattern[:, 1]
prob.data_y = data_pattern[:, 0]

return prob

def Neutron_problem(self):
"""
Sets up the problem object for the neutron problem file:
ENGINX193749_calibration_peak19.txt
"""

fname = get_file('FB_ENGINX193749_calibration_peak19.txt')
prob = FBFittingProblem(fname)
prob.name = 'ENGINX 193749 calibration, spectrum 651, peak 19'
prob.equation = ("name=LinearBackground,A0=0,A1=0;"
"name=BackToBackExponential,"
"I=597.076,A=1,B=0.05,X0=24027.5,S=22.9096")
prob.starting_values = None
prob.start_x = 23919.5789114
prob.end_x = 24189.3183142

return prob

def create_wks_NIST_problem_with_errors(self):
"""
Helper function.
Creates a mantid workspace using the data provided by the
NIST problem Misra1a.
"""

prob = self.NIST_problem()
data_e = np.sqrt(abs(prob.data_y))
wks_exp = msapi.CreateWorkspace(DataX=prob.data_x, DataY=prob.data_y,
DataE=data_e)
return wks_exp

def create_wks_NIST_problem_without_errors(self):
"""
Helper function.
Creates a mantid workspace using the data provided by the
NIST problem Misra1a.
"""

prob = self.NIST_problem()
wks_exp = msapi.CreateWorkspace(DataX=prob.data_x, DataY=prob.data_y)
return wks_exp

def test_functionDefinitions_return_NIST_functions(self):

prob = self.NIST_problem()

function_defs = function_definitions(prob)
function_defs_expected = \
["name=fitFunction,b1=500.0,b2=0.0001",
"name=fitFunction,b1=250.0,b2=0.0005"]

self.assertListEqual(function_defs_expected, function_defs)

def test_functionDefinitions_return_neutron_function(self):

prob = self.Neutron_problem()

function_defs = function_definitions(prob)
function_defs = [str(function_defs[0])]
function_defs_expected = \
[("name=LinearBackground,A0=0,A1=0;name=BackToBackExponential,"
"I=597.076,A=1,B=0.05,X0=24027.5,S=22.9096")]

self.assertListEqual(function_defs_expected, function_defs)

def test_parseNistFunctionDefinitions_get_true_function(self):

prob = self.NIST_problem()
nb_start_vals = 2

function_defs = parse_function_definitions(prob, nb_start_vals)
function_defs_expected = \
["name=fitFunction,b1=500.0,b2=0.0001",
"name=fitFunction,b1=250.0,b2=0.0005"]

self.assertListEqual(function_defs_expected, function_defs)
def NIST_problem(self):
"""
Helper function.
Sets up the problem object for the nist problem file Misra1a.dat
"""

data_pattern = np.array([[10.07, 77.6],
[14.73, 114.9],
[17.94, 141.1],
[23.93, 190.8],
[29.61, 239.9],
[35.18, 289.0],
[40.02, 332.8],
[44.82, 378.4],
[50.76, 434.8],
[55.05, 477.3],
[61.01, 536.8],
[66.40, 593.1],
[75.47, 689.1],
[81.78, 760.0]])

fname = get_file('NIST_Misra1a.dat')
prob = NISTFittingProblem(fname)
prob.name = 'Misra1a'
prob.equation = 'b1*(1-exp(-b2*x))'
prob.starting_values = [['b1', [500.0, 250.0]],
['b2', [0.0001, 0.0005]]]
prob.data_x = data_pattern[:, 1]
prob.data_y = data_pattern[:, 0]

return prob

def Neutron_problem(self):
"""
Sets up the problem object for the neutron problem file:
ENGINX193749_calibration_peak19.txt
"""

fname = get_file('FB_ENGINX193749_calibration_peak19.txt')
prob = FBFittingProblem(fname)
prob.name = 'ENGINX 193749 calibration, spectrum 651, peak 19'
prob.equation = ("name=LinearBackground,A0=0,A1=0;"
"name=BackToBackExponential,"
"I=597.076,A=1,B=0.05,X0=24027.5,S=22.9096")
prob.starting_values = None
prob.start_x = 23919.5789114
prob.end_x = 24189.3183142

return prob

def create_wks_NIST_problem_with_errors(self):
"""
Helper function.
Creates a mantid workspace using the data provided by the
NIST problem Misra1a.
"""

prob = self.NIST_problem()
data_e = np.sqrt(abs(prob.data_y))
wks_exp = msapi.CreateWorkspace(DataX=prob.data_x, DataY=prob.data_y,
DataE=data_e)
return wks_exp

def create_wks_NIST_problem_without_errors(self):
"""
Helper function.
Creates a mantid workspace using the data provided by the
NIST problem Misra1a.
"""

prob = self.NIST_problem()
wks_exp = msapi.CreateWorkspace(DataX=prob.data_x, DataY=prob.data_y)
return wks_exp

def test_functionDefinitions_return_NIST_functions(self):
prob = self.NIST_problem()

function_defs = function_definitions(prob)
function_defs_expected = \
["name=fitFunction,b1=500.0,b2=0.0001",
"name=fitFunction,b1=250.0,b2=0.0005"]

self.assertListEqual(function_defs_expected, function_defs)

def test_functionDefinitions_return_neutron_function(self):
prob = self.Neutron_problem()

function_defs = function_definitions(prob)
function_defs = [str(function_defs[0])]
function_defs_expected = \
[("name=LinearBackground,A0=0,A1=0;name=BackToBackExponential,"
"I=597.076,A=1,B=0.05,X0=24027.5,S=22.9096")]

self.assertListEqual(function_defs_expected, function_defs)

def test_parseNistFunctionDefinitions_get_true_function(self):
prob = self.NIST_problem()
nb_start_vals = 2

function_defs = parse_function_definitions(prob, nb_start_vals)
function_defs_expected = \
["name=fitFunction,b1=500.0,b2=0.0001",
"name=fitFunction,b1=250.0,b2=0.0005"]

self.assertListEqual(function_defs_expected, function_defs)


if __name__ == "__main__":
unittest.main()
unittest.main()
Loading

0 comments on commit 341f128

Please sign in to comment.