From 2f52a110d84444e6d9e42933a9fc782c99c10a59 Mon Sep 17 00:00:00 2001 From: Dan Nixon <dan@dan-nixon.com> Date: Tue, 7 Apr 2015 16:58:58 +0100 Subject: [PATCH] Add unit tests and validation Refs #6971 --- .../WorkflowAlgorithms/TOSCABankCorrection.py | 43 +++---- .../WorkflowAlgorithms/TimeSlice.py | 2 +- .../python/plugins/algorithms/CMakeLists.txt | 1 + .../algorithms/TOSCABankCorrectionTest.py | 118 ++++++++++++++++++ .../UnitTest/TSC14007_graphite002_red.nxs.md5 | 1 + 5 files changed, 139 insertions(+), 26 deletions(-) create mode 100644 Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/TOSCABankCorrectionTest.py create mode 100644 Code/Mantid/Testing/Data/UnitTest/TSC14007_graphite002_red.nxs.md5 diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/TOSCABankCorrection.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/TOSCABankCorrection.py index 33e611bef86..498867d9255 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/TOSCABankCorrection.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/TOSCABankCorrection.py @@ -59,31 +59,20 @@ class TOSCABankCorrection(DataProcessorAlgorithm): doc='Scale factor for the second bank (histogram 1)') - def _validate_range(self, name): - """ - Validates a range property - - @param name Name of the property - """ - - range_prop = self.getProperty(name).value - - if len(range_prop) != 2: - return 'Range must have two values' - - if range_prop[0] > range_prop[1]: - return 'Range must be in format "low,high"' - - return '' - - - def validateInput(self): + def validateInputs(self): + self._get_properties() issues = dict() # Validate search range - search_range_valid = self._validate_range('SearchRange') - if search_range_valid != '': - issues['SearchRange'] = search_range_valid + if len(self._search_range) != 2: + issues['SearchRange'] = 'Search range must have two values' + elif self._search_range[0] > self._search_range[1]: + issues['SearchRange'] = 'Search range must be in format "low,high"' + + # Ensure manual peak position is inside search range + if 'SearchRange' not in issues and self._peak_position is not None: + if self._peak_position < self._search_range[0] or self._peak_position > self._search_range[1]: + issues['PeakPosition'] = 'Peak position must be inside SearchRange' return issues @@ -133,7 +122,7 @@ class TOSCABankCorrection(DataProcessorAlgorithm): try: self._peak_position = float(self.getPropertyValue('PeakPosition')) - except: + except ValueError: self._peak_position = None @@ -187,8 +176,12 @@ class TOSCABankCorrection(DataProcessorAlgorithm): DeleteWorkspace('__bank_1_peaks') DeleteWorkspace('__bank_2_peaks') - selected_peak = matching_peaks[0] - logger.debug('Found matching peak: %s' % (str(selected_peak))) + if len(matching_peaks) > 0: + selected_peak = matching_peaks[0] + logger.debug('Found matching peak: %s' % (str(selected_peak))) + else: + selected_peak = None + logger.warning('No peak found') return selected_peak diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/TimeSlice.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/TimeSlice.py index d318ac36ca3..37d251ba070 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/TimeSlice.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/WorkflowAlgorithms/TimeSlice.py @@ -115,7 +115,7 @@ class TimeSlice(PythonAlgorithm): return '' - def validateInput(self): + def validateInputs(self): issues = dict() issues['SpectraRange'] = self._validate_range('SpectraRange') diff --git a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt index 77b67b9edc8..4bc9cec31dc 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt +++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/CMakeLists.txt @@ -55,6 +55,7 @@ set ( TEST_PY_FILES UpdatePeakParameterTableValueTest.py SANSSubtractTest.py TimeSliceTest.py + TOSCABankCorrectionTest.py TransformToIqtTest.py ExportSampleLogsToCSVFileTest.py ExportExperimentLogTest.py diff --git a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/TOSCABankCorrectionTest.py b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/TOSCABankCorrectionTest.py new file mode 100644 index 00000000000..32330e62e1d --- /dev/null +++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/TOSCABankCorrectionTest.py @@ -0,0 +1,118 @@ +import unittest +from mantid.simpleapi import * +from mantid.api import * + + +class TOSCABankCorrectionTest(unittest.TestCase): + + def setUp(self): + """ + Loads sample workspace. + """ + + self._original = '__TOSCABankCorrectionTest_original' + + Load(Filename='TSC14007_graphite002_red.nxs', + OutputWorkspace=self._original) + + + def tearDown(self): + """ + Removes workspaces. + """ + + DeleteWorkspace(self._original) + + + def test_automatic_peak_selection(self): + """ + Tests automatically finding a peak in the default range. + """ + + corrected_reduction, peak_position, scale_factor_1, scale_factor_2 = \ + TOSCABankCorrection(InputWorkspace=self._original) + + self.assertAlmostEqual(peak_position, 1077.47222328) + self.assertAlmostEqual(scale_factor_1, 1.0059271) + self.assertAlmostEqual(scale_factor_2, 0.9941423) + + + def test_automatic_peak_in_range(self): + """ + Tests automatically finding a peak in a given range. + """ + + corrected_reduction, peak_position, scale_factor_1, scale_factor_2 = \ + TOSCABankCorrection(InputWorkspace=self._original, + SearchRange=[200, 800]) + + self.assertAlmostEqual(peak_position, 713.20080359) + self.assertAlmostEqual(scale_factor_1, 1.006076146) + self.assertAlmostEqual(scale_factor_2, 0.993996806) + + + def test_manual_peak_selection(self): + """ + Tests using a peak provided by the user. + """ + + corrected_reduction, peak_position, scale_factor_1, scale_factor_2 = \ + TOSCABankCorrection(InputWorkspace=self._original, + PeakPosition='715') + + self.assertAlmostEqual(peak_position, 713.4430671) + self.assertAlmostEqual(scale_factor_1, 1.00611439) + self.assertAlmostEqual(scale_factor_2, 0.99395947) + + + def test_manual_peak_not_found(self): + """ + Tests error handling when a peak cannot be found using a manual peak position. + """ + + self.assertRaises(RuntimeError, + TOSCABankCorrection, + InputWorkspace=self._original, + OutputWorkspace='__TOSCABankCorrectionTest_output', + PeakPosition='900') + + + def test_validation_search_range_order(self): + """ + Tests validation to ensure low and high values are entered in correct order. + """ + + self.assertRaises(RuntimeError, + TOSCABankCorrection, + InputWorkspace=self._original, + OutputWorkspace='__TOSCABankCorrectionTest_output', + SearchRange=[500, 50]) + + + def test_validation_search_range_count(self): + """ + Tests validation to ensure two values exist values are entered in correct order. + """ + + self.assertRaises(RuntimeError, + TOSCABankCorrection, + InputWorkspace=self._original, + OutputWorkspace='__TOSCABankCorrectionTest_output', + SearchRange=[500]) + + + def test_validation_peak_position_in_search_range(self): + """ + Tests validation to ensure that the PeakPosition falls inside SearchRange. + """ + + self.assertRaises(RuntimeError, + TOSCABankCorrection, + InputWorkspace=self._original, + OutputWorkspace='__TOSCABankCorrectionTest_output', + SearchRange=[200, 2000], + PeakPosition='2500') + + +if __name__ == '__main__': + unittest.main() diff --git a/Code/Mantid/Testing/Data/UnitTest/TSC14007_graphite002_red.nxs.md5 b/Code/Mantid/Testing/Data/UnitTest/TSC14007_graphite002_red.nxs.md5 new file mode 100644 index 00000000000..4d60a5e8378 --- /dev/null +++ b/Code/Mantid/Testing/Data/UnitTest/TSC14007_graphite002_red.nxs.md5 @@ -0,0 +1 @@ +360b5639a909d42b099693b8d7a99b45 -- GitLab