From fc9d895d10838b96d892f4b877547a0b25828279 Mon Sep 17 00:00:00 2001
From: David Fairbrother <DavidFair@users.noreply.github.com>
Date: Wed, 7 Jun 2017 12:06:25 +0100
Subject: [PATCH] Re #19794 Added system test for POLARIS

---
 .../ISIS_Powder/input/POLARIS00098531.nxs.md5 |   1 +
 .../ISIS_Powder/input/POLARIS00098532.nxs.md5 |   1 +
 .../input/POLARIS00098532_splined.nxs.md5     |   1 +
 .../ISIS_Powder/input/POLARIS00098533.nxs.md5 |   1 +
 .../cycle_16_3_silicon_all_spectra.cal.md5    |   1 +
 ...grouping_file_with_essential_masks.cal.md5 |   1 +
 .../calibration/polaris/VanaPeaks.dat.md5     |   1 +
 .../polaris_system_test_mapping.yaml.md5      |   1 +
 .../tests/analysis/ISIS_PowderPolarisTest.py  | 174 +++++++++++-------
 ...SIS_Powder-POLARIS00098533_splined.nxs.md5 |   1 +
 ...IS_Powder-POLARIS98533_FocusSempty.nxs.md5 |   1 +
 11 files changed, 119 insertions(+), 65 deletions(-)
 create mode 100644 Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098531.nxs.md5
 create mode 100644 Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098532.nxs.md5
 create mode 100644 Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098532_splined.nxs.md5
 create mode 100644 Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098533.nxs.md5
 create mode 100644 Testing/Data/SystemTest/ISIS_Powder/input/calibration/polaris/17_1/cycle_16_3_silicon_all_spectra.cal.md5
 create mode 100644 Testing/Data/SystemTest/ISIS_Powder/input/calibration/polaris/Master_copy_of_grouping_file_with_essential_masks.cal.md5
 create mode 100644 Testing/Data/SystemTest/ISIS_Powder/input/calibration/polaris/VanaPeaks.dat.md5
 create mode 100644 Testing/Data/SystemTest/ISIS_Powder/input/yaml_files/polaris_system_test_mapping.yaml.md5
 create mode 100644 Testing/SystemTests/tests/analysis/reference/ISIS_Powder-POLARIS00098533_splined.nxs.md5
 create mode 100644 Testing/SystemTests/tests/analysis/reference/ISIS_Powder-POLARIS98533_FocusSempty.nxs.md5

diff --git a/Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098531.nxs.md5 b/Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098531.nxs.md5
new file mode 100644
index 00000000000..7aee7d3b74e
--- /dev/null
+++ b/Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098531.nxs.md5
@@ -0,0 +1 @@
+f01af83ae099c374e3fdd87b98069db5
diff --git a/Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098532.nxs.md5 b/Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098532.nxs.md5
new file mode 100644
index 00000000000..1537c387fda
--- /dev/null
+++ b/Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098532.nxs.md5
@@ -0,0 +1 @@
+84cf90b7704c016162deb5def8dec28a
diff --git a/Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098532_splined.nxs.md5 b/Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098532_splined.nxs.md5
new file mode 100644
index 00000000000..cef22098790
--- /dev/null
+++ b/Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098532_splined.nxs.md5
@@ -0,0 +1 @@
+7db7340850fda919a385ea698d582d2f
diff --git a/Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098533.nxs.md5 b/Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098533.nxs.md5
new file mode 100644
index 00000000000..1d2a657d6fa
--- /dev/null
+++ b/Testing/Data/SystemTest/ISIS_Powder/input/POLARIS00098533.nxs.md5
@@ -0,0 +1 @@
+78bc44bd3272771196c78a1861498af0
diff --git a/Testing/Data/SystemTest/ISIS_Powder/input/calibration/polaris/17_1/cycle_16_3_silicon_all_spectra.cal.md5 b/Testing/Data/SystemTest/ISIS_Powder/input/calibration/polaris/17_1/cycle_16_3_silicon_all_spectra.cal.md5
new file mode 100644
index 00000000000..27ec9c3e517
--- /dev/null
+++ b/Testing/Data/SystemTest/ISIS_Powder/input/calibration/polaris/17_1/cycle_16_3_silicon_all_spectra.cal.md5
@@ -0,0 +1 @@
+6894e550f8f114666cf4c0a60046d18b
diff --git a/Testing/Data/SystemTest/ISIS_Powder/input/calibration/polaris/Master_copy_of_grouping_file_with_essential_masks.cal.md5 b/Testing/Data/SystemTest/ISIS_Powder/input/calibration/polaris/Master_copy_of_grouping_file_with_essential_masks.cal.md5
new file mode 100644
index 00000000000..34294ada8e9
--- /dev/null
+++ b/Testing/Data/SystemTest/ISIS_Powder/input/calibration/polaris/Master_copy_of_grouping_file_with_essential_masks.cal.md5
@@ -0,0 +1 @@
+c2fa198cf2c4a1cdf86350cafba365fe
diff --git a/Testing/Data/SystemTest/ISIS_Powder/input/calibration/polaris/VanaPeaks.dat.md5 b/Testing/Data/SystemTest/ISIS_Powder/input/calibration/polaris/VanaPeaks.dat.md5
new file mode 100644
index 00000000000..eae8b9ef2c0
--- /dev/null
+++ b/Testing/Data/SystemTest/ISIS_Powder/input/calibration/polaris/VanaPeaks.dat.md5
@@ -0,0 +1 @@
+af21fb9fb11c2477808c073a501734d4
diff --git a/Testing/Data/SystemTest/ISIS_Powder/input/yaml_files/polaris_system_test_mapping.yaml.md5 b/Testing/Data/SystemTest/ISIS_Powder/input/yaml_files/polaris_system_test_mapping.yaml.md5
new file mode 100644
index 00000000000..fe7f0d61037
--- /dev/null
+++ b/Testing/Data/SystemTest/ISIS_Powder/input/yaml_files/polaris_system_test_mapping.yaml.md5
@@ -0,0 +1 @@
+4e0273d28eb1bdd7fdc80e877b9f7f38
diff --git a/Testing/SystemTests/tests/analysis/ISIS_PowderPolarisTest.py b/Testing/SystemTests/tests/analysis/ISIS_PowderPolarisTest.py
index 4e25f3a3ed8..46ec3d41957 100644
--- a/Testing/SystemTests/tests/analysis/ISIS_PowderPolarisTest.py
+++ b/Testing/SystemTests/tests/analysis/ISIS_PowderPolarisTest.py
@@ -2,16 +2,43 @@ from __future__ import (absolute_import, division, print_function)
 
 import os
 import stresstesting
+import shutil
 
 import mantid.simpleapi as mantid
 from mantid import config
 
-from isis_powder import polaris
+from isis_powder import Polaris
 
 DIRS = config['datasearch.directories'].split(';')
 
+# Setup various path details
 
-class VanadiumCalibrationTest(stresstesting.MantidStressTest):
+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):
 
     calibration_results = None
     existing_config = config['datasearch.directories']
@@ -20,15 +47,19 @@ class VanadiumCalibrationTest(stresstesting.MantidStressTest):
         return _gen_required_files()
 
     def runTest(self):
-        self.calibration_results = _run_vanadium_calibration()
+        setup_mantid_paths()
+        self.calibration_results = run_vanadium_calibration()
 
     def validate(self):
-        return _calibration_validation(self, self.calibration_results)
+        return calibration_validator(self.calibration_results)
 
     def cleanup(self):
-        # TODO clean up reference files properly
-        config['datasearch.directories'] = self.existing_config
-        # _clean_up()
+        try:
+            _try_delete(output_dir)
+            _try_delete(spline_path)
+        finally:
+            mantid.mtd.clear()
+            config['datasearch.directories'] = self.existing_config
 
 
 class FocusTest(stresstesting.MantidStressTest):
@@ -41,94 +72,107 @@ class FocusTest(stresstesting.MantidStressTest):
 
     def runTest(self):
         # Gen vanadium calibration first
-        _run_vanadium_calibration()
-        self.focus_results = _run_focus()
+        setup_mantid_paths()
+        self.focus_results = run_focus()
 
-    def validation(self):
-        return _focus_validation(self, self.focus_results)
+    def validate(self):
+        return focus_validation(self.focus_results)
 
     def cleanup(self):
-        config['datasearch.directories'] = self.existing_config
-        # TODO
+        try:
+            _try_delete(spline_path)
+            _try_delete(output_dir)
+        finally:
+            config['datasearch.directories'] = self.existing_config
+            mantid.mtd.clear()
 
 
 def _gen_required_files():
-    input_files = ["POLARIS/POL78338.raw",
-                   "POLARIS/POL78339.raw",
-                   "POLARIS/POL79514.raw"]
+    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 = 95598
+def run_vanadium_calibration():
+    vanadium_run = 98532  # Choose arbitrary run in the cycle 17_1
 
-    polaris_obj = setup_polaris_instrument()
-    # Try it without an output name
+    pdf_inst_obj = setup_inst_object(mode="PDF")
 
-    return polaris_obj.create_calibration_vanadium(run_in_range=vanadium_run, do_absorb_corrections=True,
-                                                   generate_absorb_corrections=True)
+    # 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 = 95599
-    polaris_obj = setup_polaris_instrument()
-    return polaris_obj.focus(run_number=run_number, input_mode="Individual", do_van_normalisation=True)
+    return splined_ws
 
 
-def _calibration_validation(cls, results):
-    _validation_setup(cls)
-    results_name = results[0].name()
-    reference_file_name = "ISIS_Powder-POLARIS78338_Van_Cal.nxs"
-    return results_name, reference_file_name
+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"
 
-def _focus_validation(cls, results):
-    _validation_setup(cls)
+    original_splined_path = os.path.join(input_dir, splined_file_name)
+    shutil.copy(original_splined_path, spline_path)
 
-    reference_file_name = "POLARIS_PowderFocus79514.nxs"
-    focus_output_name = "Focus_results"
-    mantid.GroupWorkspaces(InputWorkspaces=results, OutputWorkspace=focus_output_name)
+    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)
 
-    return focus_output_name, reference_file_name
 
+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 _validation_setup(cls):
-    cls.disableChecking.append('Instrument')
-    cls.disableChecking.append('Sample')
-    cls.disableChecking.append('SpectraMap')
 
+def focus_validation(results):
+    reference_file_name = "ISIS_Powder-POLARIS98533_FocusSempty.nxs"
+    return _compare_ws(reference_file_name=reference_file_name, results=results)
 
-def _clean_up():
-    output_file_path = _get_calibration_dir() + _get_calibration_output_name()
-    try:
-        os.remove(output_file_path)
-    except OSError:
-        print ("Could not delete output file at: ", output_file_path)
 
+def _compare_ws(reference_file_name, results):
+    ref_ws = mantid.Load(Filename=reference_file_name)
 
-def setup_polaris_instrument():
-    user_name = "Test"
-    calibration_mapping_file_name = "polaris_calibration.yaml"
+    is_valid = True if len(results) > 0 else False
 
-    calibration_dir = _get_calibration_dir()
-    calibration_mapping_path = os.path.join(calibration_dir, calibration_mapping_file_name)
-    output_dir = _get_output_dir()
+    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())
 
-    path_to_add = os.path.join(DIRS[0], "POLARIS")
-    config['datasearch.directories'] += ";" + path_to_add
-    polaris_obj = polaris.Polaris(user_name=user_name, chopper_on=True, apply_solid_angle=False,
-                                  calibration_directory=calibration_dir, output_directory=output_dir,
-                                  calibration_mapping_file=calibration_mapping_path)
-    return polaris_obj
+    return is_valid
 
 
-def _get_calibration_output_name():
-    return "system_test_polaris_van_cal.nxs"
+def setup_mantid_paths():
+    config['datasearch.directories'] += ";" + input_dir
 
 
-def _get_output_dir():
-    return os.path.join(DIRS[0], "POLARIS/DataOut")
+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 _get_calibration_dir():
-    return os.path.join(DIRS[0], "POLARIS/Calibration")
+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)
diff --git a/Testing/SystemTests/tests/analysis/reference/ISIS_Powder-POLARIS00098533_splined.nxs.md5 b/Testing/SystemTests/tests/analysis/reference/ISIS_Powder-POLARIS00098533_splined.nxs.md5
new file mode 100644
index 00000000000..cef22098790
--- /dev/null
+++ b/Testing/SystemTests/tests/analysis/reference/ISIS_Powder-POLARIS00098533_splined.nxs.md5
@@ -0,0 +1 @@
+7db7340850fda919a385ea698d582d2f
diff --git a/Testing/SystemTests/tests/analysis/reference/ISIS_Powder-POLARIS98533_FocusSempty.nxs.md5 b/Testing/SystemTests/tests/analysis/reference/ISIS_Powder-POLARIS98533_FocusSempty.nxs.md5
new file mode 100644
index 00000000000..3c514fbac25
--- /dev/null
+++ b/Testing/SystemTests/tests/analysis/reference/ISIS_Powder-POLARIS98533_FocusSempty.nxs.md5
@@ -0,0 +1 @@
+fce76485d46b2d6fc49c3b37f0a92a77
-- 
GitLab