From 7b14b7fef2dc9cbc018b0afcd926c9747db381e3 Mon Sep 17 00:00:00 2001 From: Conor Finn <conor.finn@stfc.ac.uk> Date: Tue, 3 Dec 2019 16:24:31 +0000 Subject: [PATCH] RE #26848 Make loading calibration load vanadium Loading the vanadium to be used later on makes more sense to do at this stage than at the start of the focusing step. --- .../tabs/calibration/model.py | 7 +++- .../tabs/common/vanadium_corrections.py | 4 ++ .../tabs/focus/model.py | 11 +++-- .../tabs/focus/presenter.py | 12 +++--- .../tabs/focus/test/test_focus_model.py | 40 +++++++++---------- .../tabs/focus/test/test_focus_presenter.py | 12 ++++-- 6 files changed, 48 insertions(+), 38 deletions(-) diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/model.py b/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/model.py index 9d4ca4737c2..d042c71e16f 100644 --- a/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/model.py +++ b/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/model.py @@ -80,6 +80,7 @@ class CalibrationModel(object): except RuntimeError: logger.error("Invalid file selected: ", file_path) return + vanadium_corrections.fetch_correction_workspaces(van_no, instrument) return instrument, van_no, ceria_no @staticmethod @@ -248,7 +249,11 @@ class CalibrationModel(object): # If formatted correctly the line should be in the format INS bank ICONS difc difa tzero elements = line.split() bank = elements[1] - params_table.append([int(bank) - 1, float(elements[3]), float(elements[4]), float(elements[5])]) + params_table.append( + [int(bank) - 1, + float(elements[3]), + float(elements[4]), + float(elements[5])]) if run_numbers == "": raise RuntimeError("Invalid file format.") diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/common/vanadium_corrections.py b/scripts/Engineering/gui/engineering_diffraction/tabs/common/vanadium_corrections.py index 243edf12e22..d0a2914e942 100644 --- a/scripts/Engineering/gui/engineering_diffraction/tabs/common/vanadium_corrections.py +++ b/scripts/Engineering/gui/engineering_diffraction/tabs/common/vanadium_corrections.py @@ -48,6 +48,10 @@ def fetch_correction_workspaces(vanadium_path, instrument): return integ_workspace, curves_workspace +def check_workspaces_exist(): + return Ads.doesExist(CURVES_WORKSPACE_NAME) and Ads.doesExist(INTEGRATED_WORKSPACE_NAME) + + def _calculate_vanadium_correction(vanadium_path): """ Runs the vanadium correction algorithm. diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/model.py b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/model.py index 878c79155bb..f41d7f526c4 100644 --- a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/model.py +++ b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/model.py @@ -18,7 +18,7 @@ FOCUSED_OUTPUT_WORKSPACE_NAME = "engggui_focusing_output_ws_bank_" class FocusModel(object): - def focus_run(self, sample_path, banks, plot_output, instrument, rb_number, current_calib): + def focus_run(self, sample_path, banks, plot_output, instrument, rb_number): """ Focus some data using the current calibration. :param sample_path: The path to the data to be focused. @@ -26,13 +26,12 @@ class FocusModel(object): :param plot_output: True if the output should be plotted. :param instrument: The instrument that the data came from. :param rb_number: The experiment number, used to create directories. Can be None - :param current_calib: The current calibration loaded in the calibration tab. """ - vanadium_path = current_calib["vanadium_path"] - if vanadium_path is None: + if not Ads.doesExist(vanadium_corrections.INTEGRATED_WORKSPACE_NAME) and not Ads.doesExist( + vanadium_corrections.CURVES_WORKSPACE_NAME): return - integration_workspace, curves_workspace = vanadium_corrections.fetch_correction_workspaces( - vanadium_path, instrument) + integration_workspace = Ads.retrieve(vanadium_corrections.INTEGRATED_WORKSPACE_NAME) + curves_workspace = Ads.retrieve(vanadium_corrections.CURVES_WORKSPACE_NAME) sample_workspace = self._load_focus_sample_run(sample_path) for name in banks: output_workspace_name = FOCUSED_OUTPUT_WORKSPACE_NAME + str(name) diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/presenter.py b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/presenter.py index de3d38777cf..2305db60871 100644 --- a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/presenter.py +++ b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/presenter.py @@ -10,6 +10,7 @@ from __future__ import (absolute_import, division, print_function) from qtpy.QtWidgets import QMessageBox from Engineering.gui.engineering_diffraction.tabs.common import INSTRUMENT_DICT +from Engineering.gui.engineering_diffraction.tabs.common.vanadium_corrections import check_workspaces_exist from mantidqt.utils.asynchronous import AsyncTask from mantidqt.utils.observer_pattern import Observer from mantid.simpleapi import logger @@ -46,11 +47,10 @@ class FocusPresenter(object): :param plot_output: True if the output should be plotted. :param rb_num: The rb_number from the main window (often an experiment id) """ - self.worker = AsyncTask( - self.model.focus_run, - (focus_path, banks, plot_output, self.instrument, rb_num, self.current_calibration), - error_cb=self._on_worker_error, - finished_cb=self.emit_enable_button_signal) + self.worker = AsyncTask(self.model.focus_run, + (focus_path, banks, plot_output, self.instrument, rb_num), + error_cb=self._on_worker_error, + finished_cb=self.emit_enable_button_signal) self.set_focus_controls_enabled(False) self.worker.start() @@ -70,7 +70,7 @@ class FocusPresenter(object): """ if not self.view.get_focus_valid(): return False - if self.current_calibration["vanadium_path"] is None: + if self.current_calibration["vanadium_path"] is None or not check_workspaces_exist(): self._create_error_message( "Load a calibration from the Calibration tab before focusing.") return False diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_focus_model.py b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_focus_model.py index aa8174464a1..ff406bb710d 100644 --- a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_focus_model.py +++ b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_focus_model.py @@ -25,60 +25,56 @@ class FocusModelTest(unittest.TestCase): "ceria_path": "this_is_mocked_out_too" } - @patch(file_path + ".vanadium_corrections.fetch_correction_workspaces") - def test_fails_on_invalid_sample_number(self, fetch_van): - fetch_van.return_value = ("mocked_integ", "mocked_curves") - self.assertRaises(ValueError, self.model.focus_run, "FAIL", ["1", "2"], False, "ENGINX", - "0", self.current_calibration) - fetch_van.assert_called_with("/mocked/out/anyway", "ENGINX") - - @patch(file_path + ".vanadium_corrections.fetch_correction_workspaces") - def test_focus_cancelled_when_calibration_is_not_set(self, fetch_van): - fetch_van.return_value = ("mocked_integ", "mocked_curves") - blank_calibration = {"vanadium_path": None, "ceria_path": None} - self.model.focus_run("305761", ["1", "2"], False, "ENGINX", "0", blank_calibration) - self.assertEqual(fetch_van.call_count, 0) + @patch(file_path + ".FocusModel._load_focus_sample_run") + @patch(file_path + ".vanadium_corrections.Ads.doesExist") + def test_focus_cancelled_if_van_wsp_missing(self, ads_exist, load): + ads_exist.return_value = False + self.model.focus_run("307593", ["1", "2"], False, "ENGINX", "0") + self.assertEqual(load.call_count, 0) + @patch(file_path + ".Ads") @patch(file_path + ".FocusModel._save_focused_output_files_as_nexus") @patch(file_path + ".FocusModel._run_focus") @patch(file_path + ".FocusModel._load_focus_sample_run") - @patch(file_path + ".vanadium_corrections.fetch_correction_workspaces") - def test_focus_run_for_each_bank(self, fetch_van, load_focus, run_focus, output): - fetch_van.return_value = ("mocked_integ", "mocked_curves") + def test_focus_run_for_each_bank(self, load_focus, run_focus, output, ads): + ads.retrieve.return_value = "test_wsp" banks = ["1", "2"] load_focus.return_value = "mocked_sample" - self.model.focus_run("305761", banks, False, "ENGINX", "0", self.current_calibration) + self.model.focus_run("305761", banks, False, "ENGINX", "0") self.assertEqual(len(banks), run_focus.call_count) run_focus.assert_called_with("mocked_sample", model.FOCUSED_OUTPUT_WORKSPACE_NAME + banks[-1], - "mocked_integ", "mocked_curves", banks[-1]) + "test_wsp", "test_wsp", banks[-1]) + @patch(file_path + ".Ads") @patch(file_path + ".FocusModel._save_focused_output_files_as_nexus") @patch(file_path + ".FocusModel._plot_focused_workspace") @patch(file_path + ".FocusModel._run_focus") @patch(file_path + ".FocusModel._load_focus_sample_run") @patch(file_path + ".vanadium_corrections.fetch_correction_workspaces") - def test_focus_plotted_when_checked(self, fetch_van, load_focus, run_focus, plot_focus, output): + def test_focus_plotted_when_checked(self, fetch_van, load_focus, run_focus, plot_focus, output, ads): + ads.doesExist.return_value = True fetch_van.return_value = ("mocked_integ", "mocked_curves") banks = ["1", "2"] load_focus.return_value = "mocked_sample" - self.model.focus_run("305761", banks, True, "ENGINX", "0", self.current_calibration) + self.model.focus_run("305761", banks, True, "ENGINX", "0") self.assertEqual(len(banks), plot_focus.call_count) + @patch(file_path + ".Ads") @patch(file_path + ".FocusModel._save_focused_output_files_as_nexus") @patch(file_path + ".FocusModel._plot_focused_workspace") @patch(file_path + ".FocusModel._run_focus") @patch(file_path + ".FocusModel._load_focus_sample_run") @patch(file_path + ".vanadium_corrections.fetch_correction_workspaces") def test_focus_not_plotted_when_not_checked(self, fetch_van, load_focus, run_focus, plot_focus, - output): + output, ads): fetch_van.return_value = ("mocked_integ", "mocked_curves") banks = ["1", "2"] load_focus.return_value = "mocked_sample" - self.model.focus_run("305761", banks, False, "ENGINX", "0", self.current_calibration) + self.model.focus_run("305761", banks, False, "ENGINX", "0") self.assertEqual(0, plot_focus.call_count) @patch(file_path + ".SaveNexus") diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_focus_presenter.py b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_focus_presenter.py index dc5579cb042..cd083931c24 100644 --- a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_focus_presenter.py +++ b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_focus_presenter.py @@ -22,8 +22,9 @@ class FocusPresenterTest(unittest.TestCase): self.model = mock.create_autospec(model.FocusModel) self.presenter = presenter.FocusPresenter(self.model, self.view) + @patch(tab_path + ".presenter.check_workspaces_exist") @patch(tab_path + ".presenter.FocusPresenter.start_focus_worker") - def test_worker_started_with_correct_params(self, worker): + def test_worker_started_with_correct_params(self, worker, wsp_exists): self.presenter.current_calibration = { "vanadium_path": "Fake/Path", "ceria_path": "Fake/Path" @@ -33,6 +34,7 @@ class FocusPresenterTest(unittest.TestCase): self.view.get_south_bank.return_value = True self.view.get_plot_output.return_value = True self.view.is_searching.return_value = False + wsp_exists.return_value = True self.presenter.on_focus_clicked() worker.assert_called_with("305738", ["South"], True, None) @@ -106,26 +108,30 @@ class FocusPresenterTest(unittest.TestCase): create_error.assert_called_with( "Load a calibration from the Calibration tab before focusing.") + @patch(tab_path + ".presenter.check_workspaces_exist") @patch(tab_path + ".presenter.FocusPresenter._create_error_message") - def test_validate_while_searching(self, create_error): + def test_validate_while_searching(self, create_error, wsp_check): self.presenter.current_calibration = { "vanadium_path": "Fake/File/Path", "ceria_path": "Fake/Path" } self.view.is_searching.return_value = True + wsp_check.return_value = True banks = ["North", "South"] self.assertEqual(False, self.presenter._validate(banks)) self.assertEqual(0, create_error.call_count) + @patch(tab_path + ".presenter.check_workspaces_exist") @patch(tab_path + ".presenter.FocusPresenter._create_error_message") - def test_validate_with_no_banks_selected(self, create_error): + def test_validate_with_no_banks_selected(self, create_error, wsp_check): self.presenter.current_calibration = { "vanadium_path": "Fake/Path", "ceria_path": "Fake/Path" } self.view.is_searching.return_value = False banks = [] + wsp_check.return_value = True self.presenter._validate(banks) create_error.assert_called_with("Please select at least one bank.") -- GitLab