ReflectometryQuickAuxiliaryTest.py 5.71 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
from mantid.simpleapi import *
12
from mantid.kernel import config
13

14

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

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

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

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

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

39
        DeleteWorkspace(tokeep)
40

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

100
        corrected = correction.apply(test_ws)
101

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

104
105
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
106

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

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

113
        corrected = correction.apply(test_ws)
114

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

117
118
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
119

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

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

126
        corrected = correction.apply(test_ws)
127

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

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


134
if __name__ == '__main__':
135
    config.setFacility("ISIS")
136
    unittest.main()
137
    config.setFacility("NONE")