ReflectometryQuickAuxiliaryTest.py 5.63 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
import numpy
9
from mantid.simpleapi import *
10
from isis_reflectometry import quick
11

12

13
class ReflectometryQuickAuxiliaryTest(unittest.TestCase):
14

15
    __wsName = None
16
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
31
        todump =CreateSingleValuedWorkspace(OutputWorkspace='_toremove', DataValue=1, ErrorValue=1)
        tokeep =CreateSingleValuedWorkspace(OutputWorkspace='tokeep', DataValue=1, ErrorValue=1)
32
        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
    def test_groupGet_instrument(self):
42

43
        expectedInstrument = "POLREF"
44

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

49
        # Test with single workspace as input
50
        instrument = quick.groupGet(mtd[self.__wsName][0].name(), 'inst')
51
        self.assertEqual(expectedInstrument, instrument.getName(), "Did not fetch the instrument from ws")
52
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):
67

68
        expectedNPeriods = 2
69

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

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

78
    def test_groupGet_multi_value_log(self):
79

80
        # Expected start theta, taken from the last value of the time series log.
81
82
        expectedStartTheta = 0.4903

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

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

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

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

99
100
    def test_exponential_correction_strategy(self):
        test_ws =  CreateWorkspace(UnitX="TOF", DataX=[0,1,2,3], DataY=[1,1,1], NSpec=1)
101

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

105
        corrected = correction.apply(test_ws)
106

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

109
110
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
111

112
113
    def test_polynomial_correction_strategy(self):
        test_ws =  CreateWorkspace(UnitX="TOF", DataX=[0,1,2,3], DataY=[1,1,1], NSpec=1)
114

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

118
        corrected = correction.apply(test_ws)
119

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

122
123
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
124

125
126
    def test_null_correction_strategy(self):
        test_ws = CreateWorkspace(UnitX="TOF", DataX=[0,1,2,3], DataY=[1,1,1], NSpec=1)
127

128
129
        correction = quick.NullCorrectionStrategy() # Should have no effect.
        self.assertTrue(isinstance(correction, quick.CorrectionStrategy), msg="Should be of type Correction")
130

131
        corrected = correction.apply(test_ws)
132

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

135
136
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
137
138


139
if __name__ == '__main__':
140
    unittest.main()