diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/model.py b/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/model.py index 75bc5735c66b2628039c38a4ddacb909b8037f45..b323a9779322fd4212b962749acc7a508a32bccc 100644 --- a/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/model.py +++ b/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/model.py @@ -11,7 +11,7 @@ from os import path, makedirs from mantid.api import AnalysisDataService as Ads from mantid.kernel import logger -from mantid.simpleapi import Load, EnggVanadiumCorrections, EnggCalibrate, DeleteWorkspace, CloneWorkspace, \ +from mantid.simpleapi import Load, EnggCalibrate, DeleteWorkspace, CloneWorkspace, \ CreateWorkspace, AppendSpectra, CreateEmptyTableWorkspace from mantidqt.plotting.functions import plot from Engineering.EnggUtils import write_ENGINX_GSAS_iparam_file diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/test/test_model.py b/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/test/test_model.py index 281c19b4d81e0bee1a8688b25d1528351c625f3f..fab9c2b1d4058c766bb3c1590f9e287d46952994 100644 --- a/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/test/test_model.py +++ b/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/test/test_model.py @@ -18,6 +18,7 @@ INTEGRATED_WORKSPACE_NAME = "engggui_vanadium_integration" CURVES_WORKSPACE_NAME = "engggui_vanadium_curves" INPUT_WORKSPACE_NAME = "engggui_vanadium_ws" class_path = 'Engineering.gui.engineering_diffraction.tabs.calibration.model.CalibrationModel' +file_path = 'Engineering.gui.engineering_diffraction.tabs.calibration.model' class CalibrationModelTest(unittest.TestCase): @@ -34,18 +35,18 @@ class CalibrationModelTest(unittest.TestCase): @patch(class_path + '.create_output_files') @patch(class_path + '.run_calibration') @patch(class_path + '.load_ceria') - @patch(class_path + '.calculate_vanadium_correction') - def test_EnggVanadiumCorrections_algorithm_is_called(self, alg, load_ceria, calib, output_files, + @patch(file_path + '.vanadium_corrections.fetch_correction_workspaces') + def test_EnggVanadiumCorrections_algorithm_is_called(self, van, load_ceria, calib, output_files, update_table): + van.return_value = ("A", "B") self.model.create_new_calibration(VANADIUM_NUMBER, CERIUM_NUMBER, False, "ENGINX") - alg.assert_called_once() + van.assert_called_once() @patch(class_path + '.update_calibration_params_table') @patch(class_path + '.create_output_files') @patch(class_path + '.load_ceria') - @patch(class_path + '.calculate_vanadium_correction') @patch(class_path + '.run_calibration') - def test_EnggCalibrate_algorithm_is_called(self, calibrate_alg, vanadium_alg, load_ceria, + def test_fetch_vanadium_is_called(self, calibrate_alg, load_ceria, output_files, update_table): self.model.create_new_calibration(VANADIUM_NUMBER, CERIUM_NUMBER, False, "ENGINX") self.assertEqual(calibrate_alg.call_count, 1) @@ -53,12 +54,13 @@ class CalibrationModelTest(unittest.TestCase): @patch(class_path + '.update_calibration_params_table') @patch(class_path + '.create_output_files') @patch(class_path + '.load_ceria') - @patch(class_path + '.calculate_vanadium_correction') + @patch(file_path + '.vanadium_corrections.fetch_correction_workspaces') @patch(class_path + '._plot_vanadium_curves') @patch(class_path + '._plot_difc_zero') @patch(class_path + '.run_calibration') def test_plotting_check(self, calib, plot_difc_zero, plot_van, van, ceria, output_files, update_table): + van.return_value = ("A", "B") self.model.create_new_calibration(VANADIUM_NUMBER, CERIUM_NUMBER, False, "ENGINX") plot_van.assert_not_called() plot_difc_zero.assert_not_called() @@ -69,12 +71,13 @@ class CalibrationModelTest(unittest.TestCase): @patch(class_path + '.update_calibration_params_table') @patch(class_path + '.create_output_files') @patch(class_path + '.load_ceria') - @patch(class_path + '.calculate_vanadium_correction') + @patch(file_path + '.vanadium_corrections.fetch_correction_workspaces') @patch(class_path + '._plot_vanadium_curves') @patch(class_path + '._plot_difc_zero') @patch(class_path + '.run_calibration') def test_present_RB_number_results_in_user_output_files(self, calib, plot_difc_zero, plot_van, van, ceria, output_files, update_table): + van.return_value = ("A", "B") self.model.create_new_calibration(VANADIUM_NUMBER, CERIUM_NUMBER, False, @@ -85,23 +88,25 @@ class CalibrationModelTest(unittest.TestCase): @patch(class_path + '.update_calibration_params_table') @patch(class_path + '.create_output_files') @patch(class_path + '.load_ceria') - @patch(class_path + '.calculate_vanadium_correction') + @patch(file_path + '.vanadium_corrections.fetch_correction_workspaces') @patch(class_path + '._plot_vanadium_curves') @patch(class_path + '._plot_difc_zero') @patch(class_path + '.run_calibration') def test_absent_run_number_results_in_no_user_output_files(self, calib, plot_difc_zero, plot_van, van, ceria, output_files, update_table): + van.return_value = ("A", "B") self.model.create_new_calibration(VANADIUM_NUMBER, CERIUM_NUMBER, False, "ENGINX") self.assertEqual(output_files.call_count, 1) @patch(class_path + '.update_calibration_params_table') @patch(class_path + '.create_output_files') @patch(class_path + '.load_ceria') - @patch(class_path + '.calculate_vanadium_correction') + @patch(file_path + '.vanadium_corrections.fetch_correction_workspaces') @patch(class_path + '.run_calibration') def test_calibration_params_table_is_updated(self, calibrate_alg, vanadium_alg, load_ceria, output_files, update_table): + vanadium_alg.return_value = ("A", "B") self.model.create_new_calibration(VANADIUM_NUMBER, CERIUM_NUMBER, False, "ENGINX") self.assertEqual(calibrate_alg.call_count, 1) diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/common/test/__init__.py b/scripts/Engineering/gui/engineering_diffraction/tabs/common/test/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/common/test/test_vanadium_corrections.py b/scripts/Engineering/gui/engineering_diffraction/tabs/common/test/test_vanadium_corrections.py index f46ae5a5db40ff43b8faecda6a1b2d305ebffdf9..2f6ba9e4986b5adf67d43d380f58806cfc0ce4e3 100644 --- a/scripts/Engineering/gui/engineering_diffraction/tabs/common/test/test_vanadium_corrections.py +++ b/scripts/Engineering/gui/engineering_diffraction/tabs/common/test/test_vanadium_corrections.py @@ -62,7 +62,9 @@ class VanadiumCorrectionsTest(unittest.TestCase): @patch(dir_path + ".vanadium_corrections.makedirs") def test_file_path_generation(self, makedirs): vanadium_run_number = "1234" - rmtree(path.join(path.expanduser("~"), "Engineering_Mantid")) + engineering_path = path.join(path.expanduser("~"), "Engineering_Mantid") + if path.exists(engineering_path): + rmtree(engineering_path) output = vanadium_corrections._generate_saved_workspace_file_paths(vanadium_run_number) self.assertEqual(output, (path.join(path.expanduser("~"), "Engineering_Mantid", "Vanadium_Runs", 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 5c479608e6e7f43ed93e034a5f642c797dc5eaed..243edf12e221d56844b88dac7e193cb9e778972c 100644 --- a/scripts/Engineering/gui/engineering_diffraction/tabs/common/vanadium_corrections.py +++ b/scripts/Engineering/gui/engineering_diffraction/tabs/common/vanadium_corrections.py @@ -19,10 +19,35 @@ VANADIUM_INPUT_WORKSPACE_NAME = "engggui_vanadium_ws" CURVES_WORKSPACE_NAME = "engggui_vanadium_curves" INTEGRATED_WORKSPACE_NAME = "engggui_vanadium_integration" +VANADIUM_DIRECTORY_NAME = "Vanadium_Runs" + SAVED_FILE_CURVE_SUFFIX = "_precalculated_vanadium_run_bank_curves.nxs" SAVED_FILE_INTEG_SUFFIX = "_precalculated_vanadium_run_integration.nxs" +def fetch_correction_workspaces(vanadium_path, instrument): + """ + Fetch workspaces from the file system or create new ones. + :param vanadium_path: The path to the requested vanadium run raw data. + :param instrument: The instrument the data came from. + :return: The resultant integration and curves workspaces. + """ + vanadium_number = path_handling.get_run_number_from_path(vanadium_path, instrument) + integ_path, curves_path = _generate_saved_workspace_file_paths(vanadium_number) + if path.exists(curves_path) and path.exists(integ_path): # Check if the cached files exist. + try: + integ_workspace = Load(Filename=integ_path, OutputWorkspace=INTEGRATED_WORKSPACE_NAME) + curves_workspace = Load(Filename=curves_path, OutputWorkspace=CURVES_WORKSPACE_NAME) + return integ_workspace, curves_workspace + except RuntimeError as e: + logger.error( + "Problem loading existing vanadium calculations. Creating new cached files. Description: " + + str(e)) + integ_workspace, curves_workspace = _calculate_vanadium_correction(vanadium_path) + _save_correction_files(integ_workspace, integ_path, curves_workspace, curves_path) + return integ_workspace, curves_workspace + + def _calculate_vanadium_correction(vanadium_path): """ Runs the vanadium correction algorithm. @@ -45,32 +70,33 @@ def _calculate_vanadium_correction(vanadium_path): return integrated_workspace, curves_workspace -def fetch_correction_workspaces(vanadium_path, instrument): - vanadium_number = path_handling.get_run_number_from_path(vanadium_path, instrument) - integ_path, curves_path = _generate_saved_workspace_file_paths(vanadium_number) - if path.exists(curves_path) and path.exists(integ_path): # Check if the cached files exist. - try: - integ_workspace = Load(Filename=integ_path, OutputWorkspace=INTEGRATED_WORKSPACE_NAME) - curves_workspace = Load(Filename=curves_path, OutputWorkspace=CURVES_WORKSPACE_NAME) - return integ_workspace, curves_workspace - except RuntimeError as e: - logger.error( - "Problem loading existing vanadium calculations. Creating new cached files. Description: " - + str(e)) - integ_workspace, curves_workspace = _calculate_vanadium_correction(vanadium_path) - _save_correction_files(integ_workspace, integ_path, curves_workspace, curves_path) - return integ_workspace, curves_workspace - - def _save_correction_files(integration_workspace, integration_path, curves_workspace, curves_path): - SaveNexus(InputWorkspace=integration_workspace, Filename=integration_path) - SaveNexus(InputWorkspace=curves_workspace, Filename=curves_path) + """ + Attempt to save the created workspaces to the filesystem. + :param integration_workspace: The workspace for the vanadium integration. + :param integration_path: The path to save the integration workspace to. + :param curves_workspace: The workspace for the vanadium curves. + :param curves_path: The path to save the curves workspace to. + """ + try: + SaveNexus(InputWorkspace=integration_workspace, Filename=integration_path) + SaveNexus(InputWorkspace=curves_workspace, Filename=curves_path) + except RuntimeError as e: # If the files cannot be saved, continue with the execution of the algorithm anyway. + logger.error( + "Vanadium Correction files could not be saved to the filesystem. Description: " + + str(e)) + return def _generate_saved_workspace_file_paths(vanadium_number): + """ + Generate file paths based on a vanadium run number. + :param vanadium_number: The number of the vanadium run. + :return: The full path to the file. + """ integrated_filename = vanadium_number + SAVED_FILE_INTEG_SUFFIX curves_filename = vanadium_number + SAVED_FILE_CURVE_SUFFIX - vanadium_dir = path.join(path_handling.OUT_FILES_ROOT_DIR, "Vanadium_Runs") + vanadium_dir = path.join(path_handling.OUT_FILES_ROOT_DIR, VANADIUM_DIRECTORY_NAME) if not path.exists(vanadium_dir): makedirs(vanadium_dir) return path.join(vanadium_dir, integrated_filename), path.join(vanadium_dir, curves_filename)