ReflectometryQuickAuxiliaryTest.py 5.31 KB
Newer Older
1
import unittest
2
import numpy
3
from mantid.simpleapi import *
4
from isis_reflectometry import quick
5
6

class ReflectometryQuickAuxiliaryTest(unittest.TestCase):
7

8
    __wsName = None
9
10


11
12
13
14
    def __init__(self, methodName='runTest'):
        super(ReflectometryQuickAuxiliaryTest, self).__init__(methodName)
        self.__wsName = "TestWorkspace"
        LoadISISNexus(Filename='POLREF00004699', OutputWorkspace=self.__wsName)
15

16
17
    def __del__(self):
        DeleteWorkspace(mtd[self.__wsName])
18
19


20
    def test_cleanup(self):
21
        numObjectsOriginal = len(mtd.getObjectNames())
22
23
        todump =CreateSingleValuedWorkspace(OutputWorkspace='_toremove', DataValue=1, ErrorValue=1)
        tokeep =CreateSingleValuedWorkspace(OutputWorkspace='tokeep', DataValue=1, ErrorValue=1)
24
        self.assertEqual(numObjectsOriginal+2, len(mtd.getObjectNames()))
25
26
27
        # Should remove workspaces starting with _
        quick.cleanup()
        cleaned_object_names = mtd.getObjectNames()
28
        self.assertEqual(numObjectsOriginal+1, len(cleaned_object_names))
29
        self.assertEqual(True, ('tokeep' in cleaned_object_names))
30

31
        DeleteWorkspace(tokeep)
32

33
    def test_groupGet_instrument(self):
34

35
        expectedInstrument = "POLREF"
36

37
        # Test with group workspace as input
38
        instrument = quick.groupGet(self.__wsName, 'inst')
39
        self.assertEquals(expectedInstrument, instrument.getName(), "Did not fetch the instrument from ws group")
40

41
        # Test with single workspace as input
42
        instrument = quick.groupGet(mtd[self.__wsName][0].name(), 'inst')
43
        self.assertEquals(expectedInstrument, instrument.getName(), "Did not fetch the instrument from ws")
44
45


46
    def test_groupGet_histogram_count(self):
47
        expectedNHistograms = mtd[self.__wsName][0].getNumberHistograms()
48

49
        # Test with group workspace as input
50
        nHistograms = quick.groupGet(self.__wsName, 'wksp')
51
        self.assertEquals(expectedNHistograms, nHistograms, "Did not fetch the n histograms from ws group")
52

53
        # Test with single workspace as input
54
        nHistograms = quick.groupGet(mtd[self.__wsName][0].name(), 'wksp')
55
        self.assertEquals(expectedNHistograms, nHistograms, "Did not fetch the n histograms from ws")
56
57


58
    def test_groupGet_log_single_value(self):
59

60
        expectedNPeriods = 2
61

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

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

70
    def test_groupGet_multi_value_log(self):
71

72
        # Expected start theta, taken from the last value of the time series log.
73
74
        expectedStartTheta = 0.4903

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

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

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

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

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

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

97
        corrected = correction.apply(test_ws)
98

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

101
102
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
103

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

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

110
        corrected = correction.apply(test_ws)
111

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

114
115
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
116

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

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

123
        corrected = correction.apply(test_ws)
124

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

127
128
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
129
130


131
if __name__ == '__main__':
132
    unittest.main()