Commit fff08a1e authored by Stephen Smith's avatar Stephen Smith Committed by Smith
Browse files

fix rebase issues

parent e6268148
......@@ -13,5 +13,7 @@ setup(
install_requires=['mantid'],
version=os.environ['MANTID_VERSION_STR'],
packages=find_packages(exclude=['*.test']),
package_data={'': ['*.ui']},
)
package_data={
'': ['*.ui'],
'widgets': ['*.map'],
},)
......@@ -14,7 +14,7 @@ from mantidqtinterfaces.Muon.GUI.Common.corrections_tab_widget.corrections_model
from mantidqtinterfaces.Muon.GUI.Common.corrections_tab_widget.background_corrections_model import (BackgroundCorrectionsModel,
DEFAULT_USE_RAW)
from mantidqtinterfaces.Muon.GUI.Common.test_helpers.context_setup import setup_context
from mantidqtinterfaces.Muon.GUI.Common.utilities.workspace_data_utils import DEFAULT_X_LOWER, DEFAULT_X_UPPER
from mantidqtinterfaces.Muon.GUI.Common.utilities.workspace_data_utils import DEFAULT_X_LOWER, DEFAULT_X_UPPER, X_OFFSET
class BackgroundCorrectionsModelTest(unittest.TestCase):
......@@ -203,8 +203,8 @@ class BackgroundCorrectionsModelTest(unittest.TestCase):
x_lower, x_upper = self.model.x_limits_of_workspace(run, group)
self.assertEqual(x_lower, 0.0)
self.assertEqual(x_upper, 20000.0)
self.assertEqual(x_lower, 0.0 - X_OFFSET)
self.assertEqual(x_upper, 20000.0 + X_OFFSET)
def test_that_x_limits_of_workspace_will_return_the_default_x_values_if_there_are_no_workspaces_loaded(self):
run, group = "84447", "top"
......
......@@ -14,6 +14,7 @@ from mantidqtinterfaces.Muon.GUI.Common.fitting_widgets.basic_fitting.basic_fitt
from mantidqtinterfaces.Muon.GUI.Common.muon_pair import MuonPair
from mantidqtinterfaces.Muon.GUI.Common.muon_base_pair import MuonBasePair
from mantidqtinterfaces.Muon.GUI.Common.test_helpers.context_setup import setup_context
from mantidqtinterfaces.Muon.GUI.Common.utilities.workspace_data_utils import X_OFFSET
from mantidqtinterfaces.Muon.GUI.Common.utilities.workspace_utils import StaticWorkspaceWrapper
......@@ -653,8 +654,8 @@ class BasicFittingModelTest(unittest.TestCase):
x_lower, x_upper = self.model.x_limits_of_workspace(self.model.current_dataset_name)
self.assertEqual(x_lower, 0.0)
self.assertEqual(x_upper, 20000.0)
self.assertEqual(x_lower, 0.0 - X_OFFSET)
self.assertEqual(x_upper, 20000.0 + X_OFFSET)
def test_that_x_limits_of_current_dataset_will_return_the_default_x_values_if_there_are_no_workspaces_loaded(self):
x_lower, x_upper = self.model.x_limits_of_workspace(self.model.current_dataset_name)
......
......@@ -14,6 +14,7 @@ from mantidqt.utils.qt.testing import start_qapplication
from mantidqtinterfaces.Muon.GUI.Common.calculate_pair_and_group import run_pre_processing
from mantidqtinterfaces.Muon.GUI.Common.utilities.load_utils import load_workspace_from_filename
from mantidqtinterfaces.Muon.GUI.Common.test_helpers.context_setup import setup_context
from mantidqtinterfaces.Muon.GUI.Common.muon_base_pair import MuonBasePair
from mantidqtinterfaces.Muon.GUI.Common.muon_diff import MuonDiff
from mantidqtinterfaces.Muon.GUI.Common.muon_group import MuonGroup
from mantidqtinterfaces.Muon.GUI.Common.muon_pair import MuonPair
......
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2021 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
import unittest
from unittest import mock
from mantid.api import AnalysisDataService, FrameworkManager, FunctionFactory
from mantid.simpleapi import CreateSampleWorkspace
from mantidqtinterfaces.Muon.GUI.Common.corrections_tab_widget.corrections_model import CorrectionsModel
from mantidqtinterfaces.Muon.GUI.Common.corrections_tab_widget.background_corrections_model import (BackgroundCorrectionsModel,
DEFAULT_USE_RAW)
from mantidqtinterfaces.Muon.GUI.Common.test_helpers.context_setup import setup_context
from mantidqtinterfaces.Muon.GUI.Common.utilities.workspace_data_utils import DEFAULT_X_LOWER, DEFAULT_X_UPPER
class BackgroundCorrectionsModelTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
FrameworkManager.Instance()
def setUp(self):
context = setup_context()
context._do_rebin = mock.Mock(return_value=False)
self.corrections_model = CorrectionsModel(context)
self.model = BackgroundCorrectionsModel(self.corrections_model, context)
self.model.clear_background_corrections_data()
self.runs = ["84447", "84447", "84447", "84447"]
self.groups = ["fwd", "bwd", "top", "bottom"]
self.use_raws = [DEFAULT_USE_RAW] * len(self.groups)
self.start_xs = [15.0] * 4
self.end_xs = [30.0] * 4
self.a0s = [0.0] * 4
self.a0_errors = [0.0] * 4
self.statuses = ["No background correction"] * 4
self.fitted_a0 = 1.234
self.fitted_a0_error = 0.005
self.fitted_function = self._setup_fitted_function()
self.fit_status = "success"
self.chi_squared = 2.2
def tearDown(self):
self.model = None
AnalysisDataService.clear()
def test_that_the_model_and_context_has_been_initialized_with_the_expected_data(self):
self.assertTrue(self.model.is_background_mode_none())
self.assertEqual(self.model._corrections_context.selected_function, "Flat Background + Exp Decay")
self.assertEqual(self.model._corrections_context.selected_group, "All")
self.assertEqual(self.model._corrections_context.show_all_runs, False)
def test_that_set_background_correction_mode_will_set_the_background_mode_to_auto_as_expected(self):
self.model.set_background_correction_mode("Auto")
self.assertTrue(not self.model.is_background_mode_none())
self.assertTrue(self.model.is_background_mode_auto())
self.assertTrue(not self.model.is_background_mode_manual())
def test_that_set_background_correction_mode_will_set_the_background_mode_to_manual_as_expected(self):
self.model.set_background_correction_mode("Manual")
self.assertTrue(not self.model.is_background_mode_none())
self.assertTrue(not self.model.is_background_mode_auto())
self.assertTrue(self.model.is_background_mode_manual())
def test_that_set_selected_function_will_set_the_selected_function_in_the_context(self):
function = "Flat Background + Exp Decay"
self.model.set_selected_function(function)
self.assertEqual(self.model._corrections_context.selected_function, function)
def test_that_set_selected_group_will_set_the_selected_group_in_the_context(self):
group = "fwd"
self.model.set_selected_group(group)
self.assertEqual(self.model._corrections_context.selected_group, group)
def test_that_set_show_all_runs_will_set_show_all_runs_to_true(self):
show_all_runs = True
self.model.set_show_all_runs(show_all_runs)
self.assertEqual(self.model._corrections_context.show_all_runs, show_all_runs)
def test_that_populate_background_corrections_data_will_populate_default_background_correction_data(self):
self.model.x_limits_of_workspace = mock.Mock(return_value=(0.0, 30.0))
self._populate_background_corrections_data()
for run, group in zip(self.runs, self.groups):
correction_data = self.model._corrections_context.background_correction_data[tuple([run, group])]
self.assertEqual(correction_data.use_raw, DEFAULT_USE_RAW)
self.assertEqual(correction_data.start_x, 15.0)
self.assertEqual(correction_data.end_x, 30.0)
self.assertEqual(correction_data.flat_background.getParameterValue("A0"), 0.0)
self.assertEqual(correction_data.flat_background.getError("A0"), 0.0)
def test_that_set_start_x_will_set_the_start_x_in_the_background_correction_data_for_a_specific_domain(self):
run = "84447"
self._populate_background_corrections_data()
self.model.set_start_x(run, "fwd", 5.0)
self.model.set_start_x(run, "bwd", 6.0)
self.model.set_start_x(run, "top", 7.0)
self.model.set_start_x(run, "bottom", 8.0)
self.assertEqual(self.model.start_x(run, "fwd"), 5.0)
self.assertEqual(self.model.start_x(run, "bwd"), 6.0)
self.assertEqual(self.model.start_x(run, "top"), 7.0)
self.assertEqual(self.model.start_x(run, "bottom"), 8.0)
def test_that_set_end_x_will_set_the_end_x_in_the_background_correction_data_for_a_specific_domain(self):
run = "84447"
self._populate_background_corrections_data()
self.model.set_end_x(run, "fwd", 5.0)
self.model.set_end_x(run, "bwd", 6.0)
self.model.set_end_x(run, "top", 7.0)
self.model.set_end_x(run, "bottom", 8.0)
self.assertEqual(self.model.end_x(run, "fwd"), 5.0)
self.assertEqual(self.model.end_x(run, "bwd"), 6.0)
self.assertEqual(self.model.end_x(run, "top"), 7.0)
self.assertEqual(self.model.end_x(run, "bottom"), 8.0)
def test_that_set_background_will_set_the_background_in_the_background_correction_data_for_a_specific_domain(self):
run = "84447"
self.model.x_limits_of_workspace = mock.Mock(return_value=(0.0, 30.0))
self.model.set_show_all_runs(True)
self._populate_background_corrections_data()
self.model.set_background(run, "fwd", 1.0)
self.model.set_background(run, "bwd", 2.0)
self.model.set_background(run, "top", 3.0)
self.model.set_background(run, "bottom", 4.0)
_, groups, _, _, _, backgrounds, _, _ = self.model.selected_correction_data()
self.assertEqual(groups, ["fwd", "bwd", "top", "bottom"])
self.assertEqual(backgrounds, [1.0, 2.0, 3.0, 4.0])
def test_that_selected_correction_data_returns_all_correction_data_if_all_runs_and_groups_are_selected(self):
self.model.x_limits_of_workspace = mock.Mock(return_value=(0.0, 30.0))
self.model.set_show_all_runs(True)
self._populate_background_corrections_data()
runs, groups, use_raws, start_xs, end_xs, a0s, a0_errors, statuses = self.model.selected_correction_data()
self.assertEqual(runs, self.runs)
self.assertEqual(groups, self.groups)
self.assertEqual(use_raws, self.use_raws)
self.assertEqual(start_xs, self.start_xs)
self.assertEqual(end_xs, self.end_xs)
self.assertEqual(a0s, self.a0s)
self.assertEqual(a0_errors, self.a0_errors)
self.assertEqual(statuses, self.statuses)
def test_that_selected_correction_data_returns_all_correction_data_for_a_specific_run_and_group(self):
run = "84447"
group = "bwd"
self.model.x_limits_of_workspace = mock.Mock(return_value=(0.0, 30.0))
self.model.set_selected_group(group)
self.corrections_model.set_current_run_string(run)
self._populate_background_corrections_data()
runs, groups, use_raws, start_xs, end_xs, a0s, a0_errors, statuses = self.model.selected_correction_data()
self.assertEqual(runs, [run])
self.assertEqual(groups, [group])
self.assertEqual(use_raws, [DEFAULT_USE_RAW])
self.assertEqual(start_xs, [15.0])
self.assertEqual(end_xs, [30.0])
self.assertEqual(a0s, [0.0])
self.assertEqual(a0_errors, [0.0])
self.assertEqual(statuses, ["No background correction"])
def test_that_all_runs_and_groups_returns_the_expected_run_and_group_lists(self):
self._populate_background_corrections_data()
runs, groups = self.model.all_runs_and_groups()
self.assertEqual(runs, self.runs)
self.assertEqual(groups, self.groups)
def test_that_all_runs_and_groups_returns_empty_lists_when_no_correction_data_exists(self):
runs, groups = self.model.all_runs_and_groups()
self.assertEqual(runs, [])
self.assertEqual(groups, [])
def test_that_x_limits_of_workspace_will_return_the_x_limits_of_the_workspace(self):
run, group = "84447", "top"
workspace_name = f"HIFI{run}; Group; {group}; Counts; MA"
self.model.get_counts_workspace_name = mock.Mock(return_value=workspace_name)
CreateSampleWorkspace(OutputWorkspace=workspace_name)
x_lower, x_upper = self.model.x_limits_of_workspace(run, group)
self.assertEqual(x_lower, 0.0 - X_OFFSET)
self.assertEqual(x_upper, 20000.0 + X_OFFSET)
def test_that_x_limits_of_workspace_will_return_the_default_x_values_if_there_are_no_workspaces_loaded(self):
run, group = "84447", "top"
workspace_name = f"HIFI{run}; Group; {group}; Counts; MA"
self.model.get_counts_workspace_name = mock.Mock(return_value=workspace_name)
x_lower, x_upper = self.model.x_limits_of_workspace(run, group)
self.assertEqual(x_lower, DEFAULT_X_LOWER)
self.assertEqual(x_upper, DEFAULT_X_UPPER)
def test_that_run_background_correction_for_all_will_run_without_error_when_the_start_and_end_x_is_good(self):
self.model.set_selected_function("Flat Background")
self._populate_background_corrections_data()
for run, group in zip(self.runs, self.groups):
self.model.set_start_x(run, group, 0.0)
self.model.set_end_x(run, group, 1.0)
runs, groups = self.model.run_background_correction_for_all()
self.assertEqual(runs, self.runs)
self.assertEqual(groups, self.groups)
def test_that_run_background_correction_for_all_will_cause_an_exception_when_the_start_and_end_x_are_out_of_range(self):
self.model.set_background_correction_mode("Auto")
self._populate_background_corrections_data()
for run, group in zip(self.runs, self.groups):
self.model.set_start_x(run, group, 20.0)
self.model.set_end_x(run, group, 25.0)
self.assertRaises(ValueError, self.model.run_background_correction_for_all)
@mock.patch("scientific_interfaces.Muon.GUI.Common.corrections_tab_widget.background_corrections_model.run_Fit")
def test_that_reset_background_function_data_will_reset_the_background_function_data_after_a_fit(self, mock_run_fit):
mock_run_fit.return_value = (self.fitted_function, self.fit_status, self.chi_squared)
self.model.x_limits_of_workspace = mock.Mock(return_value=(0.0, 30.0))
self.model.set_show_all_runs(True)
self.model.set_background_correction_mode("Auto")
self._populate_background_corrections_data()
runs, groups = self.model.run_background_correction_for_all()
_, _, _, _, _, a0s, a0_errors, _ = self.model.selected_correction_data()
self.assertEqual(a0s, [self.fitted_a0] * 4)
self.assertEqual(a0_errors, [self.fitted_a0_error] * 4)
self.assertEqual(runs, self.runs)
self.assertEqual(groups, self.groups)
runs, groups = self.model.reset_background_subtraction_data()
_, _, _, _, _, a0s, a0_errors, _ = self.model.selected_correction_data()
self.assertEqual(a0s, [0.0] * 4)
self.assertEqual(a0_errors, [0.0] * 4)
self.assertEqual(runs, self.runs)
self.assertEqual(groups, self.groups)
def test_that_run_background_correction_for_will_run_without_error_when_the_start_and_end_x_is_good(self):
run, group = "84447", "bwd"
self.model.set_selected_function("Flat Background")
self._populate_background_corrections_data()
self.model.set_start_x(run, group, 0.0)
self.model.set_end_x(run, group, 1.0)
runs, groups = self.model.run_background_correction_for(run, group)
self.assertEqual(runs, [run])
self.assertEqual(groups, [group])
def test_that_run_background_correction_for_will_cause_an_exception_when_the_start_and_end_x_are_out_of_range(self):
run, group = "84447", "bwd"
self.model.set_background_correction_mode("Auto")
self._populate_background_corrections_data()
self.model.set_start_x(run, group, 20.0)
self.model.set_end_x(run, group, 25.0)
self.assertRaises(ValueError, self.model.run_background_correction_for, run, group)
def _populate_background_corrections_data(self):
workspace_name = "HIFI84447; Group; fwd; Counts; MA"
CreateSampleWorkspace(OutputWorkspace=workspace_name)
self.model.get_counts_workspace_name = mock.Mock(return_value=workspace_name)
self.corrections_model.run_number_strings = mock.Mock(return_value=self.runs)
self.mock_group_names = mock.PropertyMock(return_value=self.groups)
type(self.model._context.group_pair_context).group_names = self.mock_group_names
self.model.populate_background_corrections_data()
def _setup_fitted_function(self):
function = FunctionFactory.createFunction("FlatBackground")
function.setParameter("A0", self.fitted_a0)
function.setError("A0", self.fitted_a0_error)
return function
if __name__ == '__main__':
unittest.main()
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
from collections import OrderedDict
import unittest
from mantidqt.utils.qt.testing import start_qapplication
from mantid.api import AnalysisDataService, WorkspaceFactory, WorkspaceGroup
from mantid.kernel import FloatTimeSeriesProperty, StringPropertyWithValue
from unittest import mock
from mantidqtinterfaces.Muon.GUI.Common.contexts.fitting_contexts.fitting_context import FittingContext, FitInformation, FitParameters
from mantidqtinterfaces.Muon.GUI.Common.utilities.workspace_utils import StaticWorkspaceWrapper
def create_test_workspace(ws_name=None,
time_series_logs=None,
string_value_logs=None):
"""
Create a test workspace.
:param ws_name: An optional name for the workspace
:param time_series_logs: A set of (name, (values,...))
:param string_value_logs: A set of (name, value) pairs
:return: The new workspace
"""
fake_ws = WorkspaceFactory.create('Workspace2D', 1, 1, 1)
run = fake_ws.run()
if time_series_logs is not None:
for name, values in time_series_logs:
tsp = FloatTimeSeriesProperty(name)
for item in values:
try:
time, value = item[0], item[1]
except TypeError:
time, value = "2000-05-01T12:00:00", item
tsp.addValue(time, value)
run.addProperty(name, tsp, replace=True)
if string_value_logs is not None:
for name, value in string_value_logs:
run.addProperty(
name, StringPropertyWithValue(name, value), replace=True)
ws_name = ws_name if ws_name is not None else 'fitting_context_model_test'
AnalysisDataService.Instance().addOrReplace(ws_name, fake_ws)
return fake_ws
def create_test_workspacegroup(group_name=None, size=None, items=None):
if size is not None and items is not None:
raise ValueError("Provide either size or items not both.")
group_name = group_name if group_name is not None else 'fitting_context_testgroup'
group = WorkspaceGroup()
if size is not None:
for i in range(size):
ws_name = '{}_{}'.format(group_name, i)
fake_ws = create_test_workspace(ws_name)
group.addWorkspace(fake_ws)
elif items is not None:
for item in items:
group.addWorkspace(item)
ads = AnalysisDataService.Instance()
ads.addOrReplace(group_name, group)
return group
def create_test_fit_parameters(test_parameters, global_parameters=None):
# needs to look like a standard fit table
fit_table = [{
'Name': name,
'Value': value,
'Error': error
} for name, (value, error) in test_parameters.items()]
parameter_workspace = mock.MagicMock()
parameter_workspace.workspace.__iter__.return_value = fit_table
return FitParameters(parameter_workspace, global_parameters)
@start_qapplication
class FittingContextTest(unittest.TestCase):
def setUp(self):
self.fitting_context = FittingContext()
self.mock_active_fit_history = mock.PropertyMock(return_value=[])
type(self.fitting_context).active_fit_history = self.mock_active_fit_history
def test_items_can_be_added_to_fitting_context(self):
fit_information_object = FitInformation(mock.MagicMock(),
'MuonGuassOsc',
mock.MagicMock(),
mock.MagicMock(),
mock.MagicMock())
self.fitting_context.all_latest_fits = mock.MagicMock(return_value=[fit_information_object])
self.fitting_context.add_fit(fit_information_object)
self.assertEqual(fit_information_object,
self.fitting_context.all_latest_fits()[0])
def test_fitfunctions_gives_list_of_unique_function_names(self):
test_fit_function = 'MuonGuassOsc'
self.fitting_context.all_latest_fits = mock.MagicMock(return_value=[FitInformation(mock.MagicMock(),
test_fit_function,
mock.MagicMock(),
mock.MagicMock(),
mock.MagicMock())])
self.fitting_context.add_fit_from_values(mock.MagicMock(),
test_fit_function,
mock.MagicMock(),
mock.MagicMock(),
mock.MagicMock())
self.fitting_context.add_fit_from_values(mock.MagicMock(),
test_fit_function,
mock.MagicMock(),
mock.MagicMock(),
mock.MagicMock())
fit_functions = self.fitting_context.fit_function_names()
self.assertEqual(len(fit_functions), 1)
self.assertEqual(test_fit_function, fit_functions[0])
def test_can_add_fits_without_first_creating_fit_information_objects(self):
fit_function_name = 'MuonGuassOsc'
fit_information_object = FitInformation(mock.MagicMock(), fit_function_name, mock.MagicMock(),
mock.MagicMock(), mock.MagicMock())
self.fitting_context.all_latest_fits = mock.MagicMock(return_value=[fit_information_object])
self.fitting_context.add_fit_from_values(mock.MagicMock(), fit_function_name, mock.MagicMock(),
mock.MagicMock(), mock.MagicMock())
self.assertEqual(fit_information_object,
self.fitting_context.all_latest_fits()[0])
def test_can_add_fits_with_global_parameters_without_creating_fit_information(
self):
parameter_workspace = mock.MagicMock()
input_workspace = mock.MagicMock()
fit_function_name = 'MuonGuassOsc'
global_params = ['A']
fit_information_object = FitInformation([input_workspace], fit_function_name, mock.MagicMock(),
parameter_workspace, mock.MagicMock(), global_params)
self.fitting_context.all_latest_fits = mock.MagicMock(return_value=[fit_information_object])
self.fitting_context.add_fit_from_values([input_workspace], fit_function_name, mock.MagicMock(),
parameter_workspace, mock.MagicMock(), global_params)
self.assertEqual(fit_information_object,
self.fitting_context.all_latest_fits()[0])
def test_parameters_are_readonly(self):
test_parameters = OrderedDict([('Height', (10., 0.4)), ('A0', (1,
0.01)),
('Cost function', (0.1, 0.))])
fit_params = create_test_fit_parameters(test_parameters)
fit_info = FitInformation(mock.MagicMock(), mock.MagicMock(),
mock.MagicMock(), fit_params._parameter_workspace, mock.MagicMock())
self.assertRaises(AttributeError, setattr, fit_info, "parameters",
fit_params)
def test_log_names_returns_logs_from_all_fits_by_default(self):
time_series_logs = (('ts_1', (1., )), ('ts_2', (3., )), ('ts_3', [2.]),
('ts_4', [3.]))
fake1 = create_test_workspace(
ws_name='fake1', time_series_logs=time_series_logs[:2])
fake2 = create_test_workspace(
ws_name='fake2', time_series_logs=time_series_logs[2:])
table_workspace = WorkspaceFactory.createTable()
fit1 = FitInformation(mock.MagicMock(), 'func1', [StaticWorkspaceWrapper(fake1.name(), fake1)],
StaticWorkspaceWrapper(fake1.name(), table_workspace), mock.MagicMock())
fit2 = FitInformation(mock.MagicMock(), 'func1', [StaticWorkspaceWrapper(fake2.name(), fake2)],
StaticWorkspaceWrapper(fake2.name(), table_workspace), mock.MagicMock())
self.fitting_context.add_fit(fit1)
self.fitting_context.add_fit(fit2)
self.mock_active_fit_history = mock.PropertyMock(return_value=[fit1, fit2])
type(self.fitting_context).active_fit_history = self.mock_active_fit_history
self.fitting_context.all_latest_fits = mock.MagicMock(return_value=[fit1, fit2])