ReflectometryQuickAuxiliaryTest.py 5.62 KB
Newer Older
1
2
3
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
#   NScD Oak Ridge National Laboratory, European Spallation Source,
#   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
# SPDX - License - Identifier: GPL - 3.0 +
7
import unittest
8

9
from isis_reflectometry import quick
10

11
12
from mantid.simpleapi import *

13

14
class ReflectometryQuickAuxiliaryTest(unittest.TestCase):
15
    __wsName = None
16

17
18
    def __init__(self, methodName='runTest'):
        self.__wsName = "TestWorkspace"
19
20
21
        super(ReflectometryQuickAuxiliaryTest, self).__init__(methodName)

    def setUp(self):
22
        LoadISISNexus(Filename='POLREF00004699', OutputWorkspace=self.__wsName)
23

24
    def tearDown(self):
25
        DeleteWorkspace(mtd[self.__wsName])
26

27
    def test_cleanup(self):
28
        numObjectsOriginal = len(mtd.getObjectNames())
29
        CreateSingleValuedWorkspace(OutputWorkspace='_toremove', DataValue=1, ErrorValue=1)
30
31
        tokeep = CreateSingleValuedWorkspace(OutputWorkspace='tokeep', DataValue=1, ErrorValue=1)
        self.assertEqual(numObjectsOriginal + 2, len(mtd.getObjectNames()))
32
33
34
        # Should remove workspaces starting with _
        quick.cleanup()
        cleaned_object_names = mtd.getObjectNames()
35
        self.assertEqual(numObjectsOriginal + 1, len(cleaned_object_names))
36
        self.assertEqual(True, ('tokeep' in cleaned_object_names))
37

38
        DeleteWorkspace(tokeep)
39

40
41
    def test_groupGet_instrument(self):
        expectedInstrument = "POLREF"
42

43
        # Test with group workspace as input
44
        instrument = quick.groupGet(self.__wsName, 'inst')
45
        self.assertEqual(expectedInstrument, instrument.getName(), "Did not fetch the instrument from ws group")
46

47
        # Test with single workspace as input
48
        instrument = quick.groupGet(mtd[self.__wsName][0].name(), 'inst')
49
        self.assertEqual(expectedInstrument, instrument.getName(), "Did not fetch the instrument from ws")
50

51
    def test_groupGet_histogram_count(self):
52
        expectedNHistograms = mtd[self.__wsName][0].getNumberHistograms()
53

54
        # Test with group workspace as input
55
        nHistograms = quick.groupGet(self.__wsName, 'wksp')
56
        self.assertEqual(expectedNHistograms, nHistograms, "Did not fetch the n histograms from ws group")
57

58
        # Test with single workspace as input
59
        nHistograms = quick.groupGet(mtd[self.__wsName][0].name(), 'wksp')
60
        self.assertEqual(expectedNHistograms, nHistograms, "Did not fetch the n histograms from ws")
61

62
63
    def test_groupGet_log_single_value(self):
        expectedNPeriods = 2
64

65
        # Test with group workspace as input
66
        nPeriods = quick.groupGet(self.__wsName, 'samp', 'nperiods')
67
        self.assertEqual(expectedNPeriods, nPeriods, "Did not fetch the number of periods from ws group")
68

69
        # Test with single workspace as input
70
        nPeriods = quick.groupGet(mtd[self.__wsName][0].name(), 'samp', 'nperiods')
71
        self.assertEqual(expectedNPeriods, nPeriods, "Did not fetch the number of periods from ws")
72

73
74
    def test_groupGet_multi_value_log(self):
        # Expected start theta, taken from the last value of the time series log.
75
76
        expectedStartTheta = 0.4903

77
        # Test with group workspace as input
78
        stheta = quick.groupGet(self.__wsName, 'samp', 'stheta')
79
        self.assertEqual(expectedStartTheta, round(float(stheta), 4))
80

81
        # Test with single workspace as input
82
        stheta = quick.groupGet(mtd[self.__wsName][0].name(), 'samp', 'stheta')
83
        self.assertEqual(expectedStartTheta, round(float(stheta), 4))
84

85
86
87
    def test_groupGet_unknown_log_error_code(self):
        errorCode = 0
        # Test with group workspace as input
88
        self.assertEqual(errorCode, quick.groupGet(self.__wsName, 'samp', 'MADE-UP-LOG-NAME'))
89

90
        # Test with group workspace as input
91
        self.assertEqual(errorCode, quick.groupGet(mtd[self.__wsName][0].name(), 'samp', 'MADE-UP-LOG-NAME'))
92

93
    def test_exponential_correction_strategy(self):
94
        test_ws = CreateWorkspace(UnitX="TOF", DataX=[0, 1, 2, 3], DataY=[1, 1, 1], NSpec=1)
95

96
        correction = quick.ExponentialCorrectionStrategy(1, 0)  # Should have no effect.
97
        self.assertTrue(isinstance(correction, quick.CorrectionStrategy), msg="Should be of type Correction")
98

99
        corrected = correction.apply(test_ws)
100

101
        self.assertTrue(all(test_ws.readY(0) == corrected.readY(0)), msg="Input and outputs should be identical")
102

103
104
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
105

106
    def test_polynomial_correction_strategy(self):
107
        test_ws = CreateWorkspace(UnitX="TOF", DataX=[0, 1, 2, 3], DataY=[1, 1, 1], NSpec=1)
108

109
        correction = quick.PolynomialCorrectionStrategy("1, 0")  # Should have no effect.
110
        self.assertTrue(isinstance(correction, quick.CorrectionStrategy), msg="Should be of type Correction")
111

112
        corrected = correction.apply(test_ws)
113

114
        self.assertTrue(all(test_ws.readY(0) == corrected.readY(0)), msg="Input and outputs should be identical")
115

116
117
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
118

119
    def test_null_correction_strategy(self):
120
        test_ws = CreateWorkspace(UnitX="TOF", DataX=[0, 1, 2, 3], DataY=[1, 1, 1], NSpec=1)
121

122
        correction = quick.NullCorrectionStrategy()  # Should have no effect.
123
        self.assertTrue(isinstance(correction, quick.CorrectionStrategy), msg="Should be of type Correction")
124

125
        corrected = correction.apply(test_ws)
126

127
        self.assertTrue(all(test_ws.readY(0) == corrected.readY(0)), msg="Input and outputs should be identical")
128

129
130
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
131
132


133
if __name__ == '__main__':
134
    unittest.main()