ReflectometryQuickAuxiliaryTest.py 5.75 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
        config.setString("default.facility", "ISIS")
24
        LoadISISNexus(Filename='POLREF00004699', OutputWorkspace=self.__wsName)
25
        config.setString("default.facility", "NONE")
26

27
    def tearDown(self):
28
        DeleteWorkspace(mtd[self.__wsName])
29

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

41
        DeleteWorkspace(tokeep)
42

43
44
    def test_groupGet_instrument(self):
        expectedInstrument = "POLREF"
45

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

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

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

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

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

65
66
    def test_groupGet_log_single_value(self):
        expectedNPeriods = 2
67

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

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

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

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

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

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

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

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

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

102
        corrected = correction.apply(test_ws)
103

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

106
107
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
108

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

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

115
        corrected = correction.apply(test_ws)
116

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

119
120
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
121

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

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

128
        corrected = correction.apply(test_ws)
129

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

132
133
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
134
135


136
if __name__ == '__main__':
137
    unittest.main()