Newer
Older
from __future__ import (absolute_import, division, print_function)
import stresstesting
import mantid.simpleapi as mantid
from mantid import config
from isis_powder import Polaris
DIRS = config['datasearch.directories'].split(';')
inst_name = "POLARIS"
# Relative to system data folder
working_folder_name = "ISIS_Powder"
# Relative to working folder
input_folder_name = "input"
output_folder_name = "output"
# Relative to input folder
calibration_folder_name = os.path.join("calibration", inst_name.lower())
calibration_map_rel_path = os.path.join("yaml_files", "polaris_system_test_mapping.yaml")
spline_rel_path = os.path.join("17_1", "VanSplined_98532_cycle_16_3_silicon_all_spectra.cal.nxs")
# Generate paths for the tests
# This implies DIRS[0] is the system test data folder
working_dir = os.path.join(DIRS[0], working_folder_name)
input_dir = os.path.join(working_dir, input_folder_name)
output_dir = os.path.join(working_dir, output_folder_name)
calibration_map_path = os.path.join(input_dir, calibration_map_rel_path)
calibration_dir = os.path.join(input_dir, calibration_folder_name)
spline_path = os.path.join(calibration_dir, spline_rel_path)
class CreateVanadiumTest(stresstesting.MantidStressTest):
existing_config = config['datasearch.directories']
def requiredFiles(self):
return _gen_required_files()
def runTest(self):
setup_mantid_paths()
self.calibration_results = run_vanadium_calibration()
def validate(self):
return calibration_validator(self.calibration_results)
def cleanup(self):
try:
_try_delete(output_dir)
_try_delete(spline_path)
finally:
mantid.mtd.clear()
config['datasearch.directories'] = self.existing_config
class FocusTest(stresstesting.MantidStressTest):
existing_config = config['datasearch.directories']
def requiredFiles(self):
return _gen_required_files()
def runTest(self):
# Gen vanadium calibration first
setup_mantid_paths()
self.focus_results = run_focus()
def validate(self):
return focus_validation(self.focus_results)
try:
_try_delete(spline_path)
_try_delete(output_dir)
finally:
config['datasearch.directories'] = self.existing_config
mantid.mtd.clear()
def _gen_required_files():
required_run_numbers = ["98531", "98532", # create_van : PDF mode
"98533"] # File to focus (Si)
# Generate file names of form "INSTxxxxx.nxs"
input_files = [os.path.join(input_dir, (inst_name + "000" + number + ".nxs")) for number in required_run_numbers]
input_files.append(calibration_map_path)
return input_files
def run_vanadium_calibration():
vanadium_run = 98532 # Choose arbitrary run in the cycle 17_1
pdf_inst_obj = setup_inst_object(mode="PDF")
# Run create vanadium twice to ensure we get two different output splines / files
pdf_inst_obj.create_vanadium(first_cycle_run_no=vanadium_run,
do_absorb_corrections=True, multiple_scattering=False)
# Check the spline looks good and was saved
if not os.path.exists(spline_path):
raise RuntimeError("Could not find output spline at the following path: " + spline_path)
splined_ws = mantid.Load(Filename=spline_path)
def run_focus():
run_number = 98533
sample_empty = 98532 # Use the vanadium empty again to make it obvious
sample_empty_scale = 0.5 # Set it to 50% scale
# Copy the required splined file into place first (instead of relying on generated one)
splined_file_name = "POLARIS00098532_splined.nxs"
original_splined_path = os.path.join(input_dir, splined_file_name)
shutil.copy(original_splined_path, spline_path)
inst_object = setup_inst_object(mode="PDF")
return inst_object.focus(run_number=run_number, input_mode="Individual", do_van_normalisation=True,
do_absorb_corrections=False, sample_empty=sample_empty,
sample_empty_scale=sample_empty_scale)
def calibration_validator(results):
# Get the name of the grouped workspace list
reference_file_name = "ISIS_Powder-POLARIS00098533_splined.nxs"
return _compare_ws(reference_file_name=reference_file_name, results=results)
def focus_validation(results):
reference_file_name = "ISIS_Powder-POLARIS98533_FocusSempty.nxs"
return _compare_ws(reference_file_name=reference_file_name, results=results)
def _compare_ws(reference_file_name, results):
ref_ws = mantid.Load(Filename=reference_file_name)
is_valid = True if len(results) > 0 else False
for ws, ref in zip(results, ref_ws):
if not (mantid.CompareWorkspaces(Workspace1=ws, Workspace2=ref)):
is_valid = False
print (ws.getName() + " was not equal to: " + ref.getName())
def setup_mantid_paths():
config['datasearch.directories'] += ";" + input_dir
def setup_inst_object(mode):
user_name = "Test"
inst_obj = Polaris(user_name=user_name, calibration_mapping_file=calibration_map_path,
calibration_directory=calibration_dir, output_directory=output_dir, mode=mode)
return inst_obj
def _try_delete(path):
try:
# Use this instead of os.remove as we could be passed a non-empty dir
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
except OSError as err:
print ("Could not delete output file at: ", path)