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]) log_names = self.fitting_context.log_names() self.assertEqual(len(time_series_logs), len(log_names)) for name, _ in time_series_logs: self.assertTrue(name in log_names, msg="{} not found in log list".format(name))
def test_that_all_latest_fits_will_return_the_two_most_recent_unique_fits(self): output_ws = WorkspaceFactory.create("Workspace2D", NVectors=3, YLength=5, XLength=5) table_workspace = WorkspaceFactory.createTable() output_ws_wrap1 = StaticWorkspaceWrapper("Output1", output_ws) parameter_ws_wrap1 = StaticWorkspaceWrapper("Parameter1", table_workspace) covariance_ws_wrap1 = StaticWorkspaceWrapper("Covariance1", table_workspace) output_ws_wrap2 = StaticWorkspaceWrapper("Output2", output_ws) parameter_ws_wrap2 = StaticWorkspaceWrapper("Parameter2", table_workspace) covariance_ws_wrap2 = StaticWorkspaceWrapper("Covariance2", table_workspace) fit1 = FitInformation(["Input1"], "GausOsc", [output_ws_wrap1], parameter_ws_wrap1, covariance_ws_wrap1) fit2 = FitInformation(["Input2"], "GausOsc", [output_ws_wrap2], parameter_ws_wrap2, covariance_ws_wrap2) fit3 = FitInformation(["Input1"], "GausOsc", [output_ws_wrap1], parameter_ws_wrap1, covariance_ws_wrap1) self.fitting_context.tf_asymmetry_mode = True self.fitting_context.simultaneous_fitting_mode = True self.fitting_context.active_fit_history = [fit1, fit2, fit3] self.assertEqual(self.fitting_context.active_fit_history[0], fit1) self.assertEqual(self.fitting_context.active_fit_history[1], fit2) self.assertEqual(self.fitting_context.active_fit_history[2], fit3) self.assertEqual(self.fitting_context.all_latest_fits()[0], fit2) self.assertEqual(self.fitting_context.all_latest_fits()[1], fit3) self.fitting_context.tf_asymmetry_mode = False self.assertEqual(self.fitting_context.active_fit_history, []) self.assertEqual(self.fitting_context.all_latest_fits()[0], fit2) self.assertEqual(self.fitting_context.all_latest_fits()[1], fit3)
def test_that_all_latest_fits_will_return_the_two_most_recent_unique_fits( self): output_ws = WorkspaceFactory.create("Workspace2D", NVectors=3, YLength=5, XLength=5) table_workspace = WorkspaceFactory.createTable() output_ws_wrap1 = StaticWorkspaceWrapper("Output1", output_ws) parameter_ws_wrap1 = StaticWorkspaceWrapper("Parameter1", table_workspace) covariance_ws_wrap1 = StaticWorkspaceWrapper("Covariance1", table_workspace) output_ws_wrap2 = StaticWorkspaceWrapper("Output2", output_ws) parameter_ws_wrap2 = StaticWorkspaceWrapper("Parameter2", table_workspace) covariance_ws_wrap2 = StaticWorkspaceWrapper("Covariance2", table_workspace) fit1 = FitInformation(["Input1"], "GausOsc", [output_ws_wrap1], parameter_ws_wrap1, covariance_ws_wrap1) fit2 = FitInformation(["Input2"], "GausOsc", [output_ws_wrap2], parameter_ws_wrap2, covariance_ws_wrap2) fit3 = FitInformation(["Input1"], "GausOsc", [output_ws_wrap1], parameter_ws_wrap1, covariance_ws_wrap1) self.fitting_context.active_fit_history = [fit1, fit2, fit3] self.assertEqual(self.fitting_context.active_fit_history[0], fit1) self.assertEqual(self.fitting_context.active_fit_history[1], fit2) self.assertEqual(self.fitting_context.active_fit_history[2], fit3) # fit3 == fit1 and fit3 is more recent, so 'all_latest_fits' will only return the most recent unique fits. self.assertEqual(self.fitting_context.all_latest_fits()[0], fit2) self.assertEqual(self.fitting_context.all_latest_fits()[1], fit3)
def test_remove_workspace_by_name_will_remove_a_fit_containing_a_specific_parameter_workspace( self): output_ws = WorkspaceFactory.create("Workspace2D", NVectors=3, YLength=5, XLength=5) table_workspace = WorkspaceFactory.createTable() output_ws_wrap1 = StaticWorkspaceWrapper("Output1", output_ws) parameter_ws_wrap1 = StaticWorkspaceWrapper("Parameter1", table_workspace) covariance_ws_wrap1 = StaticWorkspaceWrapper("Covariance1", table_workspace) output_ws_wrap2 = StaticWorkspaceWrapper("Output2", output_ws) parameter_ws_wrap2 = StaticWorkspaceWrapper("Parameter2", table_workspace) covariance_ws_wrap2 = StaticWorkspaceWrapper("Covariance2", table_workspace) fit1 = FitInformation(["Input1"], "GausOsc", [output_ws_wrap1], parameter_ws_wrap1, covariance_ws_wrap1) fit2 = FitInformation(["Input2"], "GausOsc", [output_ws_wrap2], parameter_ws_wrap2, covariance_ws_wrap2) self.fitting_context.active_fit_history = [fit1, fit2] self.fitting_context.remove_workspace_by_name("Parameter1") self.assertEqual(self.fitting_context.active_fit_history[0], fit2) self.assertEqual(self.fitting_context.all_latest_fits()[0], fit2)
def test_logs_from_workspace_without_logs_returns_emtpy_list(self): fake_ws = create_test_workspace() fit = FitInformation(mock.MagicMock(), 'func1', [StaticWorkspaceWrapper(fake_ws.name(), fake_ws)], mock.MagicMock(), mock.MagicMock()) allowed_logs = fit.log_names() self.assertEqual(0, len(allowed_logs))
def test_string_log_value_from_fit_with_single_workspace(self): single_value_logs = [('sv_1', '5')] fake1 = create_test_workspace(ws_name='fake1', string_value_logs=single_value_logs) fit = FitInformation(mock.MagicMock(), 'func1', [StaticWorkspaceWrapper(fake1.name(), fake1)], mock.MagicMock(), mock.MagicMock()) self.assertEqual(float(single_value_logs[0][1]), fit.log_value(single_value_logs[0][0]))
def test_has_log_returns_true_if_all_workspaces_have_the_log(self): time_series_logs = (('ts_1', (1., )), ('ts_2', (3., ))) fake1 = create_test_workspace(ws_name='fake1', time_series_logs=time_series_logs) fake2 = create_test_workspace(ws_name='fake2', time_series_logs=time_series_logs) fit = FitInformation(mock.MagicMock(), 'func1', [ StaticWorkspaceWrapper(fake1.name(), fake1), StaticWorkspaceWrapper(fake2.name(), fake2) ], mock.MagicMock(), mock.MagicMock()) self.assertTrue(fit.has_log('ts_1'))
def test_log_names_uses_filter_fn(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) fit = FitInformation(mock.MagicMock(), 'func1', [StaticWorkspaceWrapper(fake1.name(), fake1)], mock.MagicMock(), mock.MagicMock()) log_names = fit.log_names(lambda log: log.name == 'ts_1') self.assertEqual(1, len(log_names)) self.assertEqual(time_series_logs[0][0], log_names[0])
def test_time_series_log_value_from_fit_with_single_workspace_uses_time_average( self): time_series_logs = \ [('ts_1', (("2000-05-01T12:00:00", 5.), ("2000-05-01T12:00:10", 20.), ("2000-05-01T12:05:00", 30.)))] fake1 = create_test_workspace('fake1', time_series_logs) fit = FitInformation(mock.MagicMock(), 'func1', [StaticWorkspaceWrapper(fake1.name(), fake1)], mock.MagicMock(), mock.MagicMock()) time_average = (10 * 5 + 290 * 20) / 300. self.assertAlmostEqual(time_average, fit.log_value('ts_1'), places=6)
def test_has_log_returns_false_if_all_workspaces_do_not_have_log(self): time_series_logs = [('ts_1', (1., ))] fake1 = create_test_workspace(ws_name='fake1', time_series_logs=time_series_logs) fake2 = create_test_workspace(ws_name='fake2') fit = FitInformation(mock.MagicMock(), 'func1', [ StaticWorkspaceWrapper(fake1.name(), fake1), StaticWorkspaceWrapper(fake2.name(), fake2) ], mock.MagicMock(), mock.MagicMock()) self.assertFalse( fit.has_log('ts_1'), msg='All input workspaces should have the requested log')
def test_logs_for_single_workspace_return_all_time_series_logs(self): time_series_logs = (('ts_1', (1., )), ('ts_2', (3., ))) single_value_logs = (('sv_1', 'val1'), ('sv_2', 'val2')) fake_ws = create_test_workspace(time_series_logs=time_series_logs) fit = FitInformation(mock.MagicMock(), 'func1', [StaticWorkspaceWrapper(fake_ws.name(), fake_ws)], mock.MagicMock(), mock.MagicMock()) log_names = fit.log_names() for name, _ in time_series_logs: self.assertTrue(name in log_names, msg="{} not found in log list".format(name)) for name, _ in single_value_logs: self.assertFalse(name in log_names, msg="{} found in log list".format(name))
def test_parameters_are_readonly(self): fit_info = FitInformation(mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), mock.MagicMock()) self.assertRaises(AttributeError, setattr, fit_info, "parameters", mock.MagicMock())
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_global_parameters_are_captured(self): fit_information_object = FitInformation(mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), ['A']) self.assertEqual(['A'], fit_information_object.parameters.global_parameters)
def test_empty_global_parameters_if_none_specified(self): fit_information_object = FitInformation(mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), mock.MagicMock()) self.assertEqual([], fit_information_object.parameters.global_parameters)
def test_log_names_from_list_of_workspaces_gives_combined_set(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:]) fit = FitInformation(mock.MagicMock(), 'func1', [ StaticWorkspaceWrapper(fake1.name(), fake1), StaticWorkspaceWrapper(fake2.name(), fake2) ], mock.MagicMock(), mock.MagicMock()) log_names = fit.log_names() self.assertEqual(len(time_series_logs), len(log_names)) for name, _ in time_series_logs: self.assertTrue(name in log_names, msg="{} not found in log list".format(name))
def test_time_series_log_value_from_fit_with_multiple_workspaces_uses_average_of_time_average( self): time_series_logs1 = \ [('ts_1', (("2000-05-01T12:00:00", 5.), ("2000-05-01T12:00:10", 20.), ("2000-05-01T12:05:00", 30.)))] fake1 = create_test_workspace('fake1', time_series_logs1) time_series_logs2 = \ [('ts_1', (("2000-05-01T12:00:30", 10.), ("2000-05-01T12:01:45", 30.), ("2000-05-01T12:05:00", 40.)))] fake2 = create_test_workspace('fake2', time_series_logs2) fit = FitInformation(mock.MagicMock(), 'func1', [ StaticWorkspaceWrapper(fake1.name(), fake1), StaticWorkspaceWrapper(fake2.name(), fake2) ], mock.MagicMock(), mock.MagicMock()) time_average1 = (10 * 5 + 290 * 20) / 300. time_average2 = (75 * 10 + 195 * 30) / 270. all_average = 0.5 * (time_average1 + time_average2) self.assertAlmostEqual(all_average, fit.log_value('ts_1'), places=6)
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_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_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_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 create_test_fits(input_workspaces, function_name, parameters, output_workspace_names=None, global_parameters=None): """ Create a list of fits :param input_workspaces: The input workspaces :param function_name: The name of the function :param parameters: The parameters list :param output_workspace_names: A list of workspace names :param global_parameters: An optional list of tied parameters :return: A list of Fits """ workspace = WorkspaceFactory.create("Workspace2D", NVectors=3, YLength=5, XLength=5) output_workspace_names = output_workspace_names if output_workspace_names is not None else [ StaticWorkspaceWrapper('test-output-ws', workspace) ] # Convert parameters to fit table-like structure fit_table = [{ 'Name': name, 'Value': value, 'Error': error } for name, (value, error) in parameters.items()] fits = [] for name in input_workspaces: parameter_workspace = mock.NonCallableMagicMock() parameter_workspace.workspace.__iter__.return_value = fit_table parameter_workspace.workspace_name = name + '_Parameters' fits.append( FitInformation([name], function_name, output_workspace_names, parameter_workspace, mock.Mock(), global_parameters)) return fits
def test_equality_with_globals(self): fit_info = FitInformation(mock.MagicMock(), 'MuonGuassOsc', mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), ['A']) self.assertEqual(fit_info, fit_info)
def test_inequality_with_globals(self): fit_info1 = FitInformation(mock.MagicMock(), 'MuonGuassOsc', mock.MagicMock(), mock.MagicMock(), ['A']) fit_info2 = FitInformation(mock.MagicMock(), 'MuonGuassOsc', mock.MagicMock(), mock.MagicMock(), ['B']) self.assertNotEqual(fit_info1, fit_info2)