From 70d36f92d14f25f11bbdda21d2dfdc22e36c9b77 Mon Sep 17 00:00:00 2001
From: David Fairbrother <DavidFair@users.noreply.github.com>
Date: Tue, 14 Mar 2017 14:37:37 +0000
Subject: [PATCH] Re #19113 Added unit test for crop_in_tof

---
 scripts/Diffraction/isis_powder/gem.py        |  4 +-
 scripts/Diffraction/isis_powder/pearl.py      |  2 +-
 scripts/Diffraction/isis_powder/polaris.py    |  6 +-
 .../isis_powder/routines/common.py            |  2 +-
 scripts/test/ISISPowderCommonTest.py          | 58 +++++++++++++++++--
 5 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/scripts/Diffraction/isis_powder/gem.py b/scripts/Diffraction/isis_powder/gem.py
index be6894c7dad..e98c0de52bf 100644
--- a/scripts/Diffraction/isis_powder/gem.py
+++ b/scripts/Diffraction/isis_powder/gem.py
@@ -53,14 +53,14 @@ class Gem(AbstractInst):
                                                      multiple_scattering=self._inst_settings.multiple_scattering)
 
     def _crop_banks_to_user_tof(self, focused_banks):
-        return common.crop_banks_in_tof(focused_banks, self._inst_settings.focused_cropping_values)
+        return common.crop_banks_using_crop_list(focused_banks, self._inst_settings.focused_cropping_values)
 
     def _crop_raw_to_expected_tof_range(self, ws_to_crop):
         raw_cropping_values = self._inst_settings.raw_tof_cropping_values
         return common.crop_in_tof(ws_to_crop, raw_cropping_values[0], raw_cropping_values[1])
 
     def _crop_van_to_expected_tof_range(self, van_ws_to_crop):
-        return common.crop_banks_in_tof(van_ws_to_crop, self._inst_settings.vanadium_cropping_values)
+        return common.crop_banks_using_crop_list(van_ws_to_crop, self._inst_settings.vanadium_cropping_values)
 
     def _get_sample_empty(self):
         sample_empty = self._inst_settings.sample_empty
diff --git a/scripts/Diffraction/isis_powder/pearl.py b/scripts/Diffraction/isis_powder/pearl.py
index af5ce47fec1..4b33c9dd598 100644
--- a/scripts/Diffraction/isis_powder/pearl.py
+++ b/scripts/Diffraction/isis_powder/pearl.py
@@ -109,7 +109,7 @@ class Pearl(AbstractInst):
         return grouped_d_spacing, None
 
     def _crop_banks_to_user_tof(self, focused_banks):
-        return common.crop_banks_in_tof(focused_banks, self._inst_settings.tof_cropping_values)
+        return common.crop_banks_using_crop_list(focused_banks, self._inst_settings.tof_cropping_values)
 
     def _crop_raw_to_expected_tof_range(self, ws_to_crop):
         out_ws = common.crop_in_tof(ws_to_crop=ws_to_crop, x_min=self._inst_settings.raw_data_crop_vals[0],
diff --git a/scripts/Diffraction/isis_powder/polaris.py b/scripts/Diffraction/isis_powder/polaris.py
index b1d32f94e1a..eede4233c75 100644
--- a/scripts/Diffraction/isis_powder/polaris.py
+++ b/scripts/Diffraction/isis_powder/polaris.py
@@ -47,7 +47,7 @@ class Polaris(AbstractInst):
         return True
 
     def _crop_banks_to_user_tof(self, focused_banks):
-        return common.crop_banks_in_tof(focused_banks, self._inst_settings.focused_cropping_values)
+        return common.crop_banks_using_crop_list(focused_banks, self._inst_settings.focused_cropping_values)
 
     def _crop_raw_to_expected_tof_range(self, ws_to_crop):
         cropped_ws = common.crop_in_tof(ws_to_crop=ws_to_crop, x_min=self._inst_settings.raw_data_crop_values[0],
@@ -55,8 +55,8 @@ class Polaris(AbstractInst):
         return cropped_ws
 
     def _crop_van_to_expected_tof_range(self, van_ws_to_crop):
-        cropped_ws = common.crop_banks_in_tof(bank_list=van_ws_to_crop,
-                                              crop_values_list=self._inst_settings.van_crop_values)
+        cropped_ws = common.crop_banks_using_crop_list(bank_list=van_ws_to_crop,
+                                                       crop_values_list=self._inst_settings.van_crop_values)
         return cropped_ws
 
     def _generate_auto_vanadium_calibration(self, run_details):
diff --git a/scripts/Diffraction/isis_powder/routines/common.py b/scripts/Diffraction/isis_powder/routines/common.py
index 2072fda7567..32032fc99b9 100644
--- a/scripts/Diffraction/isis_powder/routines/common.py
+++ b/scripts/Diffraction/isis_powder/routines/common.py
@@ -22,7 +22,7 @@ def cal_map_dictionary_key_helper(dictionary, key, append_to_error_message=None)
     return dictionary_key_helper(dictionary=dictionary, key=key, throws=True, exception_msg=err_message)
 
 
-def crop_banks_in_tof(bank_list, crop_values_list):
+def crop_banks_using_crop_list(bank_list, crop_values_list):
     """
     Crops each bank by the specified tuple values from a list of tuples in TOF. The number
     of tuples must match the number of banks to crop. A list of [(100,200), (150,250)] would crop
diff --git a/scripts/test/ISISPowderCommonTest.py b/scripts/test/ISISPowderCommonTest.py
index dc663daa6b0..1538e0fcbdf 100644
--- a/scripts/test/ISISPowderCommonTest.py
+++ b/scripts/test/ISISPowderCommonTest.py
@@ -32,7 +32,7 @@ class ISISPowderCommonTest(unittest.TestCase):
         # Check that it correctly returns the key value where it exists
         self.assertEqual(common.cal_map_dictionary_key_helper(dictionary=dict_with_key, key=correct_key_name), 123)
 
-    def test_crop_banks_in_tof(self):
+    def test_crop_banks_using_crop_list(self):
         bank_list = []
         cropping_value = (0, 1000)  # Crop to 0-1000 microseconds for unit tests
         cropping_value_list = []
@@ -46,24 +46,70 @@ class ISISPowderCommonTest(unittest.TestCase):
 
         # Check a list of WS and single cropping value is detected
         with self.assertRaisesRegexp(ValueError, "The cropping values were not in a list type"):
-            common.crop_banks_in_tof(bank_list=bank_list, crop_values_list=cropping_value)
+            common.crop_banks_using_crop_list(bank_list=bank_list, crop_values_list=cropping_value)
 
         # Check a list of cropping values and a single workspace is detected
         with self.assertRaisesRegexp(RuntimeError, "Attempting to use list based cropping"):
-            common.crop_banks_in_tof(bank_list=bank_list[0], crop_values_list=cropping_value_list)
+            common.crop_banks_using_crop_list(bank_list=bank_list[0], crop_values_list=cropping_value_list)
 
         # What about a mismatch between the number of cropping values and workspaces
         with self.assertRaisesRegexp(RuntimeError, "The number of TOF cropping values does not match"):
-            common.crop_banks_in_tof(bank_list=bank_list[1:], crop_values_list=cropping_value_list)
+            common.crop_banks_using_crop_list(bank_list=bank_list[1:], crop_values_list=cropping_value_list)
 
         # Check we can crop a single workspace from the list
-        cropped_single_ws_list = common.crop_banks_in_tof(bank_list=[bank_list[0]], crop_values_list=[cropping_value])
+        cropped_single_ws_list = common.crop_banks_using_crop_list(bank_list=[bank_list[0]], crop_values_list=[cropping_value])
         self.assertEqual(cropped_single_ws_list[0].getNumberBins(), expected_number_of_bins)
+        mantid.DeleteWorkspace(Workspace=cropped_single_ws_list[0])
 
         # Check we can crop a whole list
-        cropped_ws_list = common.crop_banks_in_tof(bank_list=bank_list[1:], crop_values_list=cropping_value_list[1:])
+        cropped_ws_list = common.crop_banks_using_crop_list(bank_list=bank_list[1:], crop_values_list=cropping_value_list[1:])
         for ws in cropped_ws_list[1:]:
             self.assertEqual(ws.getNumberBins(), expected_number_of_bins)
+            mantid.DeleteWorkspace(Workspace=ws)
+
+    def test_crop_in_tof(self):
+        ws_list = []
+        x_min = 100
+        x_max = 500  # Crop to 0-500 microseconds for unit tests
+        expected_number_of_bins = x_max - x_min
+
+        for i in range(0, 3):
+            out_name = "crop_banks_in_tof-" + str(i)
+            ws_list.append(mantid.CreateSampleWorkspace(OutputWorkspace=out_name, XMin=0, XMax=20000, BinWidth=1))
+
+        # Crop a single workspace in TOF
+        tof_single_ws = common.crop_in_tof(ws_to_crop=ws_list[0], x_min=x_min, x_max=x_max)
+        self.assertEqual(tof_single_ws.getNumberBins(), expected_number_of_bins)
+        mantid.DeleteWorkspace(tof_single_ws)
+
+        # Crop a list of workspaces in TOF
+        cropped_ws_list = common.crop_in_tof(ws_to_crop=ws_list[1:], x_min=x_min, x_max=x_max)
+        for ws in cropped_ws_list:
+            self.assertEqual(ws.getNumberBins(), expected_number_of_bins)
+            mantid.DeleteWorkspace(ws)
+
+    def test_crop_in_tof_coverts_units(self):
+        # Checks that crop_in_tof converts to TOF before cropping
+        ws_list = []
+        x_min = 100
+        x_max = 500  # Crop to 0-500 microseconds for unit tests
+        expected_number_of_bins = 20000  # Hard code number of expected bins for dSpacing
+
+        for i in range(0, 3):
+            out_name = "crop_banks_in_dSpacing-" + str(i)
+            ws_list.append(mantid.CreateSampleWorkspace(OutputWorkspace=out_name, XMin=0, XMax=20000, BinWidth=1,
+                                                        XUnit="dSpacing"))
+
+        # Crop a single workspace from d_spacing and check the number of bins
+        tof_single_ws = common.crop_in_tof(ws_to_crop=ws_list[0], x_min=x_min, x_max=x_max)
+        self.assertEqual(tof_single_ws.getNumberBins(), expected_number_of_bins)
+        mantid.DeleteWorkspace(tof_single_ws)
+
+        # Crop a list of workspaces in dSpacing
+        cropped_ws_list = common.crop_in_tof(ws_to_crop=ws_list[1:], x_min=x_min, x_max=x_max)
+        for ws in cropped_ws_list:
+            self.assertEqual(ws.getNumberBins(), expected_number_of_bins)
+            mantid.DeleteWorkspace(ws)
 
 if __name__ == "__main__":
     unittest.main()
-- 
GitLab