def _add_simultaneous_fit_results_to_ADS_and_context( self, input_workspace_names: list, parameter_table, output_group_workspace, covariance_matrix, global_parameters: list) -> None: """Adds the results of a simultaneous fit to the ADS and fitting context.""" function_name = self.fitting_context.function_name output_workspace_wraps, directory = self._create_output_workspace_wraps( input_workspace_names, function_name, output_group_workspace) parameter_table_name, _ = create_parameter_table_name( input_workspace_names[0] + "+ ...", function_name) covariance_matrix_name, _ = create_covariance_matrix_name( input_workspace_names[0] + "+ ...", function_name) self._add_workspace_to_ADS(parameter_table, parameter_table_name, directory) self._add_workspace_to_ADS(covariance_matrix, covariance_matrix_name, directory) parameter_workspace_wrap = StaticWorkspaceWrapper( parameter_table_name, retrieve_ws(parameter_table_name)) covariance_workspace_wrap = StaticWorkspaceWrapper( covariance_matrix_name, retrieve_ws(covariance_matrix_name)) self._add_fit_to_context(input_workspace_names, output_workspace_wraps, parameter_workspace_wrap, covariance_workspace_wrap, global_parameters)
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_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_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_that_current_normalised_covariance_matrix_will_return_a_statix_workspace_wrapper_when_a_fit_exists(self): ws = CreateEmptyTableWorkspace() wrapper = StaticWorkspaceWrapper("CovarianceMatrix", ws) self.model._get_normalised_covariance_matrix_for = mock.Mock(return_value=wrapper) covariance_wrapper = self.model.current_normalised_covariance_matrix() self.assertEqual(covariance_wrapper, wrapper)
def test_that_show_normalised_covariance_matrix_will_not_raise_an_error( self): ws = CreateEmptyTableWorkspace() wrapper = StaticWorkspaceWrapper("CovarianceMatrix", ws) self.view.show_normalised_covariance_matrix(wrapper.workspace, wrapper.workspace_name)
def test_create_results_table_with_logs_selected(self): workspace = WorkspaceFactory.create("Workspace2D", NVectors=3, YLength=5, XLength=5) workspace.mutableRun().addProperty("sample_temp", 50, True) workspace.mutableRun().addProperty("sample_magn_field", 2, True) _, model = create_test_model(('ws1',), 'func1', self.parameters, [StaticWorkspaceWrapper('ws1', workspace)], self.logs) selected_results = [('ws1', 0)] table = model.create_results_table(self.log_names, selected_results) expected_cols = ['workspace_name'] + self.log_names + [ 'f0.Height', 'f0.HeightError', 'f0.PeakCentre', 'f0.PeakCentreError', 'f0.Sigma', 'f0.SigmaError', 'f1.Height', 'f1.HeightError', 'f1.PeakCentre', 'f1.PeakCentreError', 'f1.Sigma', 'f1.SigmaError', 'Cost function value' ] expected_types = (TableColumnType.NoType, TableColumnType.X, TableColumnType.X, TableColumnType.Y, TableColumnType.YErr, TableColumnType.Y, TableColumnType.YErr, TableColumnType.Y, TableColumnType.YErr, TableColumnType.Y, TableColumnType.YErr, TableColumnType.Y, TableColumnType.YErr, TableColumnType.Y, TableColumnType.YErr, TableColumnType.Y) avg_log_values = 50., 2.0 expected_content = [ ('ws1_Parameters', avg_log_values[0], avg_log_values[1], self.f0_height[0], self.f0_height[1], self.f0_centre[0], self.f0_centre[1], self.f0_sigma[0], self.f0_sigma[1], self.f1_height[0], self.f1_height[1], self.f1_centre[0], self.f1_centre[1], self.f1_sigma[0], self.f1_sigma[1], self.cost_function[0]) ] self._assert_table_matches_expected(zip(expected_cols, expected_types), expected_content, table, model.results_table_name())
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_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_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_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_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_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_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_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 _create_output_workspace_wraps_for_a_multi_domain_fit( self, input_workspace_names: list, output_group_workspace, output_group_name: str) -> list: """Returns a list of StaticWorkspaceWrapper objects containing the fitted output workspaces for many domains.""" self._add_workspace_to_ADS(output_group_workspace, output_group_name, "") output_workspace_names = self._rename_members_of_fitted_workspace_group( input_workspace_names, output_group_name) return [ StaticWorkspaceWrapper(workspace_name, retrieve_ws(workspace_name)) for workspace_name in output_workspace_names ]
def test_create_results_table_with_logs_missing_from_some_workspaces_raises(self): workspace = WorkspaceFactory.create("Workspace2D", NVectors=3, YLength=5, XLength=5) parameters = OrderedDict([('f0.Height', (100, 0.1))]) logs = [('log1', (1., 2.)), ('log2', (3., 4.)), ('log3', (4., 5.)), ('log4', (5., 6.))] fits_logs1 = create_test_fits(('ws1',), 'func1', parameters, output_workspace_names=[StaticWorkspaceWrapper('test-ws1-ws', workspace)]) add_logs(fits_logs1[0].input_workspaces[0], logs[:2]) fits_logs2 = create_test_fits(('ws2',), 'func1', parameters, output_workspace_names=[StaticWorkspaceWrapper('test-ws2-ws', workspace)]) add_logs(fits_logs2[0].input_workspaces[0], logs[2:]) fitting_context = TFAsymmetryFittingContext() fitting_context.fit_list = fits_logs1 + fits_logs2 model = ResultsTabModel(fitting_context, ResultsContext()) selected_results = [('ws1', 0), ('ws2', 1)] selected_logs = ['log1', 'log3'] self.assertRaises(IndexError, model.create_results_table, selected_logs, selected_results)
def test_that_handle_covariance_matrix_clicked_calls_the_expected_functions( self): ws = CreateEmptyTableWorkspace() wrapper = StaticWorkspaceWrapper("CovarianceMatrix", ws) self.model.current_normalised_covariance_matrix = mock.Mock( return_value=wrapper) self.presenter.handle_covariance_matrix_clicked() self.model.current_normalised_covariance_matrix.assert_called_once_with( ) self.view.show_normalised_covariance_matrix.assert_called_once_with( wrapper.workspace, wrapper.workspace_name)
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_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 _create_output_workspace_wraps(self, input_workspace_names: list, function_name: str, output_group_workspace) -> tuple: """Returns a list of StaticWorkspaceWrapper objects containing the fitted output workspaces""" if self.fitting_context.number_of_datasets > 1: output_group_name, directory = create_multi_domain_fitted_workspace_name( input_workspace_names[0], function_name) output_workspace_wraps = self._create_output_workspace_wraps_for_a_multi_domain_fit( input_workspace_names, output_group_workspace, output_group_name) else: output_workspace_name, directory = create_fitted_workspace_name( input_workspace_names[0], function_name) self._add_workspace_to_ADS(output_group_workspace, output_workspace_name, directory) output_workspace_wraps = [ StaticWorkspaceWrapper(output_workspace_name, retrieve_ws(output_workspace_name)) ] return output_workspace_wraps, directory
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_that_has_normalised_covariance_matrix_returns_true_when_there_is_not_a_covariance_matrix(self): ws = CreateEmptyTableWorkspace() wrapper = StaticWorkspaceWrapper("CovarianceMatrix", ws) self.model._get_normalised_covariance_matrix_for = mock.Mock(return_value=wrapper) self.assertTrue(self.model.has_normalised_covariance_matrix())