Skip to content
Snippets Groups Projects
Commit d2536035 authored by Conor Finn's avatar Conor Finn
Browse files

RE #27012 Make fetching pull from file system

parent 47e2371d
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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)
......
......@@ -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",
......
......@@ -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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment