forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SANSCentreFinderTest.py
282 lines (235 loc) · 10.7 KB
/
SANSCentreFinderTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
import unittest
from reducer_singleton import ReductionSingleton
import ISISCommandInterface as command_iface
import centre_finder as cf
from mantid.simpleapi import *
class SANSBeamCentrePositionUpdater(unittest.TestCase):
def test_that_find_ALL_produces_correct_increment(self):
# Arrange
fac = cf.BeamCentrePositionUpdaterFactory()
position_updater = fac.create_beam_centre_position_updater(cf.FindDirectionEnum.ALL)
x = 1.0
y = 2.0
x_step = 0.1
y_step = 0.2
# Act
x_new, y_new = position_updater.increment_position(x, y, x_step, y_step)
# Assert
x_expected = 1.1
y_expected = 2.2
self.assertEqual(x_expected, x_new, "The x value should have been incremented.")
self.assertEqual(y_expected, y_new, "The y value should have been incremented.")
def test_that_find_LEFTRIGHT_produces_correct_increment(self):
# Arrange
fac = cf.BeamCentrePositionUpdaterFactory()
position_updater = fac.create_beam_centre_position_updater(cf.FindDirectionEnum.LEFT_RIGHT)
x = 1.0
y = 2.0
x_step = 0.1
y_step = 0.2
# Act
x_new, y_new = position_updater.increment_position(x, y, x_step, y_step)
# Assert
x_expected = 1.1
y_expected = 2.0
self.assertEqual(x_expected, x_new, "The x value should have been incremented.")
self.assertEqual(y_expected, y_new, "The y value should have been incremented.")
def test_that_find_UPPDOWN_produces_correct_increment(self):
# Arrange
fac = cf.BeamCentrePositionUpdaterFactory()
position_updater = fac.create_beam_centre_position_updater(cf.FindDirectionEnum.UP_DOWN)
x = 1.0
y = 2.0
x_step = 0.1
y_step = 0.2
# Act
x_new, y_new = position_updater.increment_position(x, y, x_step, y_step)
# Assert
x_expected = 1.0
y_expected = 2.2
self.assertEqual(x_expected, x_new, "The x value should have been incremented.")
self.assertEqual(y_expected, y_new, "The y value should have been incremented.")
class TestPositionProvider(unittest.TestCase):
workspace_name = 'dummy_ws'
class MockSample(object):
'''
Mocking out the sample
'''
def __init__(self, ws_name):
super(TestPositionProvider.MockSample, self).__init__()
self.wksp_name = ws_name
def get_wksp_name(self):
return self.wksp_name
def _provide_reducer(self, is_larmor, is_new=True):
'''
Provide a reducer with either Larmor or non-Larmor. If we have Larmor,
then we want to be able to set the run number as well
'''
command_iface.Clean()
if is_larmor and is_new:
command_iface.LARMOR()
CreateSampleWorkspace(OutputWorkspace=self.workspace_name)
AddSampleLog(Workspace=self.workspace_name, LogName='run_number', LogText='3000', LogType='Number')
sample = self.MockSample(self.workspace_name)
ReductionSingleton()._sample_run = sample
return ReductionSingleton()
elif is_larmor and not is_new:
command_iface.LARMOR()
CreateSampleWorkspace(OutputWorkspace=self.workspace_name)
AddSampleLog(Workspace=self.workspace_name, LogName='run_number', LogText='1000', LogType='Number')
sample = self.MockSample(self.workspace_name)
ReductionSingleton()._sample_run = sample
return ReductionSingleton()
else:
command_iface.LOQ()
return ReductionSingleton()
def _clean_up(self, workspace_name):
if workspace_name in mtd.getObjectNames():
mtd.remove(workspace_name)
def test_that_XY_increment_provider_is_created_for_non_larmor(self):
# Arrange
increment_coord1 = 1
increment_coord2 = 2
tolerance = 200
is_larmor = False
reducer = self._provide_reducer(is_larmor)
# Act
factory = cf.PositionProviderFactory(increment_coord1=increment_coord1,
increment_coord2=increment_coord2,
tolerance=tolerance,
position_type=cf.FindDirectionEnum.ALL)
provider = factory.create_position_provider(reducer=reducer)
# Asssert
self.assertTrue(isinstance(provider, cf.PositionProviderXY), "Should create a XY increment provider")
# Clean up
self._clean_up(self.workspace_name)
def test_that_YAngle_increment_provider_is_created_for_larmor(self):
# Arrange
increment_coord1 = 1
increment_coord2 = 2
tolerance = 200
is_larmor = True
is_new = True
reducer = self._provide_reducer(is_larmor, is_new)
# Act
factory = cf.PositionProviderFactory(increment_coord1=increment_coord1,
increment_coord2=increment_coord2,
tolerance=tolerance,
position_type=cf.FindDirectionEnum.ALL)
provider = factory.create_position_provider(reducer=reducer)
# Asssert
self.assertTrue(isinstance(provider, cf.PositionProviderAngleY), "Should create a AngleY increment provider")
# Clean up
self._clean_up(self.workspace_name)
def test_that_XY_increment_provider_is_created_for_old_larmor(self):
# Arrange
increment_coord1 = 1
increment_coord2 = 2
tolerance = 200
is_larmor = True
is_new = False
reducer = self._provide_reducer(is_larmor, is_new)
# Act
factory = cf.PositionProviderFactory(increment_coord1=increment_coord1,
increment_coord2=increment_coord2,
tolerance=tolerance,
position_type=cf.FindDirectionEnum.ALL)
provider = factory.create_position_provider(reducer=reducer)
# Asssert
self.assertTrue(isinstance(provider, cf.PositionProviderXY), "Should create a XY increment provider")
# Clean up
self._clean_up(self.workspace_name)
def test_that_XY_increment_provider_halves_the_step(self):
# Arrange
increment_coord1 = 1
increment_coord2 = 2
tolerance = 200
provider = cf.PositionProviderXY(increment_coord1, increment_coord2, tolerance)
# Act and Assert
self.assertEqual(increment_coord1, provider.get_increment_coord1())
self.assertEqual(increment_coord2, provider.get_increment_coord2())
provider.half_and_reverse_increment_coord1()
provider.half_and_reverse_increment_coord2()
self.assertEqual(-increment_coord1 / 2.0, provider.get_increment_coord1())
self.assertEqual(-increment_coord2 / 2.0, provider.get_increment_coord2())
def test_that_AngleY_increment_provider_halves_the_step(self):
# Arrange
increment_coord1 = 1
increment_coord2 = 2
tolerance = 200
tolerance_angle = 33
bench_rotation = 1
coord1_scale_factor = 1
provider = cf.PositionProviderAngleY(increment_coord1, increment_coord2, tolerance, tolerance_angle,
bench_rotation, coord1_scale_factor)
# Act and Assert
self.assertEqual(increment_coord1, provider.get_increment_coord1())
self.assertEqual(increment_coord2, provider.get_increment_coord2())
provider.half_and_reverse_increment_coord1()
provider.half_and_reverse_increment_coord2()
self.assertEqual(-increment_coord1 / 2.0, provider.get_increment_coord1())
self.assertEqual(-increment_coord2 / 2.0, provider.get_increment_coord2())
def test_that_XY_increment_is_smaller_than_tolerance(self):
# Arrange
increment_coord1 = 1
increment_coord2 = 2
tolerance = 200
provider = cf.PositionProviderXY(increment_coord1, increment_coord2, tolerance)
# Act
is_smaller_coord1 = provider.is_coord1_increment_smaller_than_tolerance()
is_smaller_coord2 = provider.is_coord2_increment_smaller_than_tolerance()
# Assert
self.assertTrue(is_smaller_coord1)
self.assertTrue(is_smaller_coord2)
def test_that_XY_increment_is_larger_than_tolerance(self):
# Arrange
increment_coord1 = 1
increment_coord2 = 2
tolerance = 0.2
provider = cf.PositionProviderXY(increment_coord1, increment_coord2, tolerance)
# Act
is_smaller_coord1 = provider.is_coord1_increment_smaller_than_tolerance()
is_smaller_coord2 = provider.is_coord2_increment_smaller_than_tolerance()
# Assert
self.assertFalse(is_smaller_coord1)
self.assertFalse(is_smaller_coord2)
def test_that_AngleY_increment_is_smaller_than_tolerance(self):
# Arrange
increment_coord1 = 1
increment_coord2 = 2
tolerance = 100
tolerance_angle = 233
bench_rotation = 1
coord1_scale_factor = 1
provider = cf.PositionProviderAngleY(increment_coord1, increment_coord2, tolerance, tolerance_angle,
bench_rotation, coord1_scale_factor)
# Act
is_smaller_coord1 = provider.is_coord1_increment_smaller_than_tolerance()
is_smaller_coord2 = provider.is_coord2_increment_smaller_than_tolerance()
# Assert
self.assertTrue(is_smaller_coord1)
self.assertTrue(is_smaller_coord2)
def test_that_AngleY_increment_is_larger_than_tolerance(self):
# Arrange
increment_coord1 = 1
increment_coord2 = 2
tolerance = 0.2
tolerance_angle = 0.1
bench_rotation = 1
coord1_scale_factor = 1
provider = cf.PositionProviderAngleY(increment_coord1, increment_coord2, tolerance, tolerance_angle,
bench_rotation, coord1_scale_factor)
# Act
is_smaller_coord1 = provider.is_coord1_increment_smaller_than_tolerance()
is_smaller_coord2 = provider.is_coord2_increment_smaller_than_tolerance()
# Assert
self.assertFalse(is_smaller_coord1)
self.assertFalse(is_smaller_coord2)
if __name__ == "__main__":
unittest.main()