Skip to content
Snippets Groups Projects
Commit 2f52a110 authored by Dan Nixon's avatar Dan Nixon
Browse files

Add unit tests and validation

Refs #6971
parent 36428ba2
No related branches found
No related tags found
No related merge requests found
...@@ -59,31 +59,20 @@ class TOSCABankCorrection(DataProcessorAlgorithm): ...@@ -59,31 +59,20 @@ class TOSCABankCorrection(DataProcessorAlgorithm):
doc='Scale factor for the second bank (histogram 1)') doc='Scale factor for the second bank (histogram 1)')
def _validate_range(self, name): def validateInputs(self):
""" self._get_properties()
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):
issues = dict() issues = dict()
# Validate search range # Validate search range
search_range_valid = self._validate_range('SearchRange') if len(self._search_range) != 2:
if search_range_valid != '': issues['SearchRange'] = 'Search range must have two values'
issues['SearchRange'] = search_range_valid 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 return issues
...@@ -133,7 +122,7 @@ class TOSCABankCorrection(DataProcessorAlgorithm): ...@@ -133,7 +122,7 @@ class TOSCABankCorrection(DataProcessorAlgorithm):
try: try:
self._peak_position = float(self.getPropertyValue('PeakPosition')) self._peak_position = float(self.getPropertyValue('PeakPosition'))
except: except ValueError:
self._peak_position = None self._peak_position = None
...@@ -187,8 +176,12 @@ class TOSCABankCorrection(DataProcessorAlgorithm): ...@@ -187,8 +176,12 @@ class TOSCABankCorrection(DataProcessorAlgorithm):
DeleteWorkspace('__bank_1_peaks') DeleteWorkspace('__bank_1_peaks')
DeleteWorkspace('__bank_2_peaks') DeleteWorkspace('__bank_2_peaks')
selected_peak = matching_peaks[0] if len(matching_peaks) > 0:
logger.debug('Found matching peak: %s' % (str(selected_peak))) 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 return selected_peak
......
...@@ -115,7 +115,7 @@ class TimeSlice(PythonAlgorithm): ...@@ -115,7 +115,7 @@ class TimeSlice(PythonAlgorithm):
return '' return ''
def validateInput(self): def validateInputs(self):
issues = dict() issues = dict()
issues['SpectraRange'] = self._validate_range('SpectraRange') issues['SpectraRange'] = self._validate_range('SpectraRange')
......
...@@ -55,6 +55,7 @@ set ( TEST_PY_FILES ...@@ -55,6 +55,7 @@ set ( TEST_PY_FILES
UpdatePeakParameterTableValueTest.py UpdatePeakParameterTableValueTest.py
SANSSubtractTest.py SANSSubtractTest.py
TimeSliceTest.py TimeSliceTest.py
TOSCABankCorrectionTest.py
TransformToIqtTest.py TransformToIqtTest.py
ExportSampleLogsToCSVFileTest.py ExportSampleLogsToCSVFileTest.py
ExportExperimentLogTest.py ExportExperimentLogTest.py
......
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()
360b5639a909d42b099693b8d7a99b45
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment