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

class ReflectometryQuickAuxiliaryTest(unittest.TestCase):
13

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

42
        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


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

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

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


65
    def test_groupGet_log_single_value(self):
66

67
        expectedNPeriods = 2
68

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

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

77
    def test_groupGet_multi_value_log(self):
78

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

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

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

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

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

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

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

104
        corrected = correction.apply(test_ws)
105

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

108
109
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
110

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

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

117
        corrected = correction.apply(test_ws)
118

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

121
122
        DeleteWorkspace(test_ws)
        DeleteWorkspace(corrected)
123

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

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

130
        corrected = correction.apply(test_ws)
131

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

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


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