Skip to content
Snippets Groups Projects
ISIS_PowderPolarisTest.py 7 KiB
Newer Older
from __future__ import (absolute_import, division, print_function)


import mantid.simpleapi as mantid
from mantid import config

from isis_powder import Polaris, SampleDetails

DIRS = config['datasearch.directories'].split(';')

# Setup various path details
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")
unsplined_van_rel_path = os.path.join("17_1", "Van_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)
unsplined_van_path = os.path.join(calibration_dir, unsplined_van_rel_path)


class CreateVanadiumTest(stresstesting.MantidStressTest):
    calibration_results = None
    existing_config = config['datasearch.directories']

    def requiredFiles(self):
        return _gen_required_files()

    def runTest(self):
        setup_mantid_paths()
        self.calibration_results = run_vanadium_calibration()
        splined_ws, unsplined_ws = self.calibration_results
        for ws in splined_ws+unsplined_ws:
            self.assertEqual(ws.sample().getMaterial().name(), 'V')
Elliot Oram's avatar
Elliot Oram committed
        return (unsplined_ws.getName(), "ISIS_Powder-POLARIS00098533_unsplined.nxs",
                splined_ws.getName(), "ISIS_Powder-POLARIS00098533_splined.nxs")
        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):
        for ws in self.focus_results:
            self.assertEqual(ws.sample().getMaterial().name(), 'Si')
        return self.focus_results.getName(), "ISIS_Powder-POLARIS98533_FocusSempty.nxs"
        try:
            _try_delete(spline_path)
            _try_delete(output_dir)
        finally:
            config['datasearch.directories'] = self.existing_config
            mantid.mtd.clear()
class TotalScatteringTest(stresstesting.MantidStressTest):

    def runTest(self):
        focused_ws = mantid.Load(Filename="ISIS_Powder-POLARIS98533_FocusSempty.nxs")  # Reference from focus output
        self.pdf_output = run_total_scattering(focused_ws, False)

    def validate(self):
        # Whilst total scattering is in development, the validation will avoid using reference files as they will have
        # to be updated very frequently. In the meantime, the expected peak in the PDF at ~4 Angstrom will be checked.
        # expected_peak_values = [bin_index, y_value] (each list item is a bank (1-5))
        expected_peak_values = [[7,  0.149],
                                [11, 0.285],
                                [23, 0.504],
                                [37, 0.382],
                                [52, 0.700]]
        for index, ws in enumerate(self.pdf_output):
            self.assertAlmostEqual(ws.dataY(0)[expected_peak_values[index][0]], expected_peak_values[index][1],
                                   places=3)


def run_total_scattering(focused_ws, merge_banks):
    pdf_inst_obj = setup_inst_object(mode="pdf")
    return pdf_inst_obj.create_total_scattering_pdf(focused_workspace=focused_ws,
                                                    merge_banks=merge_banks)
    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)
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)
    unsplined_ws = mantid.Load(Filename=unsplined_van_path)
    return splined_ws, unsplined_ws
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 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)

    sample_details = SampleDetails(height=4.0, radius=0.2985, center=[0, 0, 0], shape='cylinder')
    sample_details.set_material(chemical_formula='Si')
    inst_obj.set_sample_details(sample=sample_details)

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:
        print ("Could not delete output file at: ", path)