def test_logs_from_workspace_without_logs_returns_emtpy_list(self): fake_ws = create_test_workspace() fit = FitInformation(mock.MagicMock(), 'func1', fake_ws.name(), fake_ws.name()) allowed_logs = fit.log_names() self.assertEqual(0, len(allowed_logs))
def test_handle_fit_completed_adds_appropriate_fits_to_plot(self): self.model.plotted_workspaces = self.workspace_list self.model.plotted_workspaces_inverse_binning = {} fit_information = FitInformation( mock.MagicMock(), 'GaussOsc', ['MUSR62260; Group; bottom; Asymmetry; MA'], ['MUSR62260; Group; bottom; Asymmetry; MA; Fitted;']) self.context.fitting_context.fit_list.__getitem__.return_value = fit_information self.context.fitting_context.number_of_fits = 1 self.presenter.handle_fit_completed(fit_information) self.assertEqual(self.model.add_workspace_to_plot.call_count, 2) # check fit and diff workspaces plotted self.model.add_workspace_to_plot.assert_any_call( self.view.get_axes()[0], 'MUSR62260; Group; bottom; Asymmetry; MA; Fitted;', [1], errors=False, plot_kwargs=mock.ANY) self.model.add_workspace_to_plot.assert_called_with( self.view.get_axes()[0], 'MUSR62260; Group; bottom; Asymmetry; MA; Fitted;', [2], errors=False, plot_kwargs=mock.ANY)
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 """ output_workspace_names = output_workspace_names if output_workspace_names is not None else [ 'test-output-ws' ] # 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(parameter_workspace, function_name, name, output_workspace_names, global_parameters)) return fits
def test_context_constructor_accepts_fit_list(self): fit_list = [ FitInformation(mock.MagicMock(), 'MuonGuassOsc', mock.MagicMock(), mock.MagicMock()) ] context = FittingContext(fit_list) self.assertEqual(fit_list, context.fit_list)
def test_global_parameters_are_captured(self): fit_information_object = FitInformation(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()) self.assertEqual([], fit_information_object.parameters.global_parameters)
def create_simulatenous_fit_over_groups(self, workspace_list): output_workspaces = [ws + '; ' + 'Fitted;' for ws in workspace_list] fit_information = FitInformation(mock.MagicMock(), 'GaussOsc', workspace_list, output_workspaces) return fit_information
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', [fake1.name()], [fake1.name()]) self.assertEqual(float(single_value_logs[0][1]), fit.log_value(single_value_logs[0][0]))
def test_items_can_be_added_to_fitting_context(self): fit_information_object = FitInformation(mock.MagicMock(), 'MuonGuassOsc', mock.MagicMock(), mock.MagicMock()) self.fitting_context.add_fit(fit_information_object) self.assertEqual(fit_information_object, self.fitting_context.fit_list[0])
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:]) self.fitting_context.add_fit( FitInformation(mock.MagicMock(), 'func1', fake1.name(), fake1.name())) self.fitting_context.add_fit( FitInformation(mock.MagicMock(), 'func1', fake2.name(), fake2.name())) 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', [fake1.name(), fake2.name()], [fake1.name(), fake2.name()]) 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', fake1.name(), fake1.name()) 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_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(fit_params._parameter_workspace, mock.MagicMock(), mock.MagicMock(), mock.MagicMock()) self.assertRaises(AttributeError, setattr, fit_info, "parameters", fit_params)
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', [fake1.name(), fake2.name()], [fake1.name(), fake2.name()]) self.assertFalse( fit.has_log('ts_1'), msg='All input workspaces should have the requested log')
def test_log_names_respects_filter(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:]) self.fitting_context.add_fit( FitInformation(mock.MagicMock(), 'func1', fake1.name(), fake1.name())) self.fitting_context.add_fit( FitInformation(mock.MagicMock(), 'func1', fake2.name(), fake2.name())) required_logs = ('ts_2', 'ts_4') log_names = self.fitting_context.log_names( filter_fn=lambda log: log.name in required_logs) self.assertEqual(len(required_logs), len(log_names)) for name in required_logs: self.assertTrue(name in log_names, msg="{} not found in log list".format(name))
def test_handle_plot_selected_fits_correctly_calls_model(self): fit = FitInformation(mock.MagicMock(), 'GaussOsc', ['MUSR62260; Group; bottom; Asymmetry; MA'], ['MUSR62260; Group; bottom; Asymmetry; MA; Fitted']) self.model.get_fit_workspace_and_indices.return_value = [["MUSR62260; Group; bottom; Asymmetry; MA; Fitted"], [1]] fit_information = FitPlotInformation(input_workspaces=["MUSR62260; Group; bottom; Asymmetry; MA"], fit=fit) self.view.is_plot_diff.return_value = False self.presenter.handle_plot_selected_fits([fit_information]) self.model.get_fit_workspace_and_indices.assert_called_once_with(fit,False)
def test_get_fit_workspaces_to_plot_returns_correctly(self): fit = FitInformation( mock.MagicMock(), 'GaussOsc', ['MUSR62260; Group; bottom; Asymmetry; MA'], ['MUSR62260; Group; bottom; Asymmetry; MA; Fitted']) expected_workspaces = [ 'MUSR62260; Group; bottom; Asymmetry; MA; Fitted' ] * 2 expected_indices = [1, 2] workspaces, indices = self.model.get_fit_workspace_and_indices(fit) self.assertEqual(workspaces, expected_workspaces) self.assertEqual(expected_indices, indices)
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', fake_ws.name(), fake_ws.name()) 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_can_add_fits_without_first_creating_fit_information_objects(self): parameter_workspace = mock.MagicMock() input_workspace = mock.MagicMock() output_workspace_names = mock.MagicMock() fit_function_name = 'MuonGuassOsc' fit_information_object = FitInformation( parameter_workspace, fit_function_name, input_workspace, output_workspace_names) self.fitting_context.add_fit_from_values( parameter_workspace, fit_function_name, input_workspace, output_workspace_names) self.assertEqual(fit_information_object, self.fitting_context.fit_list[0])
def test_handle_plot_selected_fits_correctly_calls_plot(self): fit = FitInformation(mock.MagicMock(), 'GaussOsc', ['MUSR62260; Group; bottom; Asymmetry; MA'], ['MUSR62260; Group; bottom; Asymmetry; MA; Fitted']) self.model.get_fit_workspace_and_indices.return_value = [["MUSR62260; Group; bottom; Asymmetry; MA; Fitted"], [1]] fit_information = FitPlotInformation(input_workspaces=["MUSR62260; Group; bottom; Asymmetry; MA"], fit=fit) expected_workspace_list = ["MUSR62260; Group; bottom; Asymmetry; MA"] + \ ['MUSR62260; Group; bottom; Asymmetry; MA; Fitted'] expected_indices = [0] + [1] self.presenter.handle_plot_selected_fits([fit_information]) self.figure_presenter.plot_workspaces.assert_called_once_with(expected_workspace_list, expected_indices, hold_on=False, autoscale=False)
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', [fake1.name()], [fake1.name()], ) time_average = (10 * 5 + 290 * 20) / 300. self.assertAlmostEqual(time_average, fit.log_value('ts_1'), places=6)
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', [fake1.name(), fake2.name()], [fake1.name(), fake2.name()]) 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_handle_fit_completed_adds_appropriate_fits_to_plot(self): self.model.plotted_workspaces = self.workspace_list self.model.plotted_workspaces_inverse_binning = [] fit_information = FitInformation(mock.MagicMock(), 'GaussOsc', ['MUSR62260; Group; bottom; Asymmetry; MA'], ['MUSR62260; Group; bottom; Asymmetry; MA; Fitted;']) self.context.fitting_context.fit_list.__getitem__.return_value = fit_information self.context.fitting_context.number_of_fits = 1 self.presenter.handle_fit_completed() self.assertEqual(self.model.add_workspace_to_plot.call_count, 2) self.model.add_workspace_to_plot.assert_any_call('MUSR62260; Group; bottom; Asymmetry; MA; Fitted;', 2, 'MUSR62260; Group; bottom; Asymmetry; MA; Fitted;: Fit') self.model.add_workspace_to_plot.assert_called_with('MUSR62260; Group; bottom; Asymmetry; MA; Fitted;', 3, 'MUSR62260; Group; bottom; Asymmetry; MA; Fitted;: Diff')
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', [fake1.name(), fake2.name()], [fake1.name(), fake2.name()]) 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): fit_info = FitInformation(mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), mock.MagicMock()) self.assertRaises(AttributeError, setattr, fit_info, "parameters", mock.MagicMock())
def test_inequality_with_globals(self): fit_info1 = FitInformation(mock.MagicMock(), 'MuonGuassOsc', mock.MagicMock(), ['A']) fit_info2 = FitInformation(mock.MagicMock(), 'MuonGuassOsc', mock.MagicMock(), ['B']) self.assertNotEqual(fit_info1, fit_info2)
def test_len_gives_length_of_fit_list(self): self.assertEqual(0, len(self.fitting_context)) self.fitting_context.add_fit( FitInformation(mock.MagicMock(), 'MuonGuassOsc', mock.MagicMock(), mock.MagicMock())) self.assertEqual(1, len(self.fitting_context))
def test_equality_with_globals(self): fit_info = FitInformation(mock.MagicMock(), 'MuonGuassOsc', mock.MagicMock(), mock.MagicMock(), ['A']) self.assertEqual(fit_info, fit_info)