-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_instrument_scripts.py
397 lines (326 loc) · 15 KB
/
test_instrument_scripts.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import os
import sys
import unittest
from genie_python import genie as g
from utilities import utilities
sys.path.append(os.path.join("C:\\", "Instrument", "scripts"))
def assert_tables(cls: unittest.TestCase, detector: str, wiring: str, spectra: str) -> None:
"""
Utility function for asserting that all of the tables are correct.
Args:
cls (unittest.TestCase): test class instance
detector (str): detector table file name
wiring (str): wiring table file name
spectra (str): spectra table file name
"""
cls.assertIn(detector, g.get_detector_table())
cls.assertIn(wiring, g.get_wiring_table())
cls.assertIn(spectra, g.get_spectra_table())
# Tests for the scanning instrument scripts. They are mainly testing
# the do_sans and do_trans methods and each of their parameters. For
# each instrument we are testing that it can go into sans mode, trans
# mode and switch between them. Testing each parameter is spread
# out over each instrument depending on what the simulation allows.
# All the tables for this are copies of _ibextest.dat renamed for
# the tables needed in the script.
# Test Sans2d more thorougher than other instruments as it is best
# simulated and functionality tested does not need repeated per
# instrument
class TestInstrumentScriptsSans2d(unittest.TestCase):
@classmethod
def setUpClass(cls):
g.set_instrument(None)
utilities.load_config_if_not_already_loaded("instrument_scripts_sans2d")
from instrument.sans2d.sans import Sans2d
cls.instr = Sans2d()
g.set_pv("CAEN:hv0:1:SIM", 1, is_local=True)
for i in range(10):
g.set_pv(f"CAEN:hv0:1:SIM:{i}:status", "On", is_local=True)
g.set_pv("MOT:SAMP:X:MTR.VMAX", 15, is_local=True)
g.set_pv("MOT:SAMP:X:MTR.VELO", 15, is_local=True)
g.set_pv("MOT:SAMP:Y:MTR.VMAX", 15, is_local=True)
g.set_pv("MOT:SAMP:Y:MTR.VELO", 15, is_local=True)
def test_WHEN_do_sans_is_called_instrument_is_in_sans_mode(self):
self.instr.do_sans()
utilities.assert_with_timeout(
lambda: self.assertEqual("OUT", g.get_pv("FINS_VAC:MONITOR3:STATUS:SP", is_local=True)),
timeout=30,
)
self.assertEqual("sans", self.instr.measurement_type)
assert_tables(
self,
"detector_gastubes_01.dat",
"wiring_gastubes_01_event.dat",
"spectrum_gastubes_01.dat",
)
def test_WHEN_do_trans_is_called_instrument_is_in_trans_mode(self):
self.instr.do_trans()
utilities.assert_with_timeout(
lambda: self.assertEqual("IN", g.get_pv("FINS_VAC:MONITOR3:STATUS:SP", is_local=True)),
timeout=30,
)
self.assertEqual("transmission", self.instr.measurement_type)
assert_tables(self, "detector_trans8.dat", "wiring_trans8.dat", "spectra_trans8.dat")
def test_WHEN_after_calling_do_trans_calling_do_sans_puts_instrument_is_in_sans_mode(self):
self.instr.do_trans()
self.instr.do_sans()
utilities.assert_with_timeout(
lambda: self.assertEqual("OUT", g.get_pv("FINS_VAC:MONITOR3:STATUS:SP", is_local=True)),
timeout=30,
)
self.assertEqual("sans", self.instr.measurement_type)
assert_tables(
self,
"detector_gastubes_01.dat",
"wiring_gastubes_01_event.dat",
"spectrum_gastubes_01.dat",
)
def test_WHEN_after_calling_do_sans_calling_do_trans_puts_instrument_is_in_trans_mode(self):
self.instr.do_sans()
self.instr.do_trans()
utilities.assert_with_timeout(
lambda: self.assertEqual("IN", g.get_pv("FINS_VAC:MONITOR3:STATUS:SP", is_local=True)),
timeout=30,
)
self.assertEqual("transmission", self.instr.measurement_type)
assert_tables(self, "detector_trans8.dat", "wiring_trans8.dat", "spectra_trans8.dat")
def test_WHEN_do_sans_large_is_called_instrument_is_in_sans_mode_with_correct_aperture(self):
self.instr.do_sans_large()
utilities.assert_with_timeout(
lambda: self.assertEqual("OUT", g.get_pv("FINS_VAC:MONITOR3:STATUS:SP", is_local=True)),
timeout=30,
)
self.assertEqual("sans", self.instr.measurement_type)
utilities.assert_with_timeout(
lambda: self.assertEqual("LARGE", g.get_pv("LKUP:SCRAPER:POSN:SP", is_local=True)),
timeout=30,
)
assert_tables(
self,
"detector_gastubes_01.dat",
"wiring_gastubes_01_event.dat",
"spectrum_gastubes_01.dat",
)
def test_WHEN_calling_do_sans_trans_with_all_valid_time_parameters_THEN_run_starts(self):
initial_run_num = g.get_runnumber()
self.instr.do_sans(title="TEST_RUN", time=10)
g.waitfor_runstate("SETUP", maxwaitsecs=30)
self.assertEqual(g.get_runstate(), "SETUP")
self.assertEqual(int(initial_run_num) + 1, int(g.get_runnumber()))
self.instr.do_sans(title="TEST_RUN", seconds=10)
g.waitfor_runstate("SETUP", maxwaitsecs=30)
self.assertEqual(g.get_runstate(), "SETUP")
self.assertEqual(int(initial_run_num) + 2, int(g.get_runnumber()))
self.instr.do_trans(title="TEST_RUN", uamps=1)
g.waitfor_runstate("SETUP", maxwaitsecs=30)
self.assertEqual(g.get_runstate(), "SETUP")
self.assertEqual(int(initial_run_num) + 3, int(g.get_runnumber()))
def test_WHEN_calling_do_sans_do_trans_sets_title_correctly(self):
self.instr.do_sans(title="SANS_TEST")
utilities.assert_with_timeout(
lambda: self.assertEqual(g.get_title(), "SANS_TEST_SANS"), timeout=30
)
self.instr.do_trans(title="TRANS_TEST")
utilities.assert_with_timeout(
lambda: self.assertEqual(g.get_title(), "TRANS_TEST_TRANS"), timeout=30
)
def test_WHEN_calling_do_sans_with_position_sets_position_correctly(self):
self.instr.do_sans(pos="BT")
utilities.assert_with_timeout(
lambda: self.assertEqual(self.instr.changer_pos, "BT"), timeout=30
)
with self.assertRaises(RuntimeError):
self.instr.do_sans(pos="BAD_POSITION")
utilities.assert_with_timeout(
lambda: self.assertEqual(self.instr.changer_pos, "BT"), timeout=30
)
def test_WHEN_calling_do_sans_with_thickness_set_thickness_correctly(self):
self.instr.do_sans(thickness=2.0)
self.assertEqual(2.0, g.get_sample_pars()["THICK"])
def test_WHEN_calling_do_sans_with_dls_sample_changer_sets_position_correctly(self):
self.instr.do_sans(pos="DLS2", dls_sample_changer=True)
utilities.assert_with_timeout(
lambda: self.assertEqual(self.instr.changer_pos_dls, "DLS2"), timeout=30
)
with self.assertRaises(RuntimeError):
self.instr.do_sans(pos="BAD_POSITION", dls_sample_changer=True)
utilities.assert_with_timeout(
lambda: self.assertEqual(self.instr.changer_pos_dls, "DLS2"), timeout=30
)
def test_WHEN_do_sans_with_aperture_sets_aperture_correctly(self):
self.instr.do_sans(aperture="LARGE")
utilities.assert_with_timeout(
lambda: self.assertEqual(
g.get_pv("LKUP:SCRAPER:POSN:SP", is_local=True).upper(), "LARGE"
),
timeout=30,
)
def test_WHEN_do_sans_with_period_sets_period_correctly(self):
self.instr.do_sans(period=1)
self.assertEqual(1, g.get_period())
# Test zoom but don't need to test parts of the instrument base class done in sans2d
class TestInstrumentScriptsZOOM(unittest.TestCase):
@classmethod
def setUpClass(cls):
g.set_instrument(None)
utilities.load_config_if_not_already_loaded("instrument_scripts_zoom")
from instrument.zoom.sans import Zoom
cls.instr = Zoom()
g.set_pv("CAEN:hv0:4:SIM", 1, is_local=True)
for i in range(8):
g.set_pv(f"CAEN:hv0:4:SIM:{i}:status", "On", is_local=True)
def test_WHEN_do_sans_is_called_instrument_is_in_sans_mode(self):
self.instr.do_sans()
utilities.assert_with_timeout(
lambda: self.assertEqual("EXTRACTED", g.get_pv("VACUUM:MONITOR:4", is_local=True)),
timeout=30,
)
self.assertEqual("sans", self.instr.measurement_type)
assert_tables(
self,
"detector_1det_1dae3card.dat",
"wiring1det_event_200218.dat",
"spec2det_280318_to_test_18_1.txt",
)
def test_WHEN_do_trans_is_called_instrument_is_in_trans_mode(self):
self.instr.do_trans()
utilities.assert_with_timeout(
lambda: self.assertEqual("INSERTED", g.get_pv("VACUUM:MONITOR:4", is_local=True)),
timeout=30,
)
self.assertEqual("transmission", self.instr.measurement_type)
assert_tables(
self,
"detector_8mon_1dae3card_00.dat",
"wiring_8mon_1dae3card_00_hist.dat",
"spectrum_8mon_1dae3card_00.dat",
)
def test_WHEN_after_calling_do_trans_calling_do_sans_puts_instrument_is_in_sans_mode(self):
self.instr.do_trans()
self.instr.do_sans()
utilities.assert_with_timeout(
lambda: self.assertEqual("EXTRACTED", g.get_pv("VACUUM:MONITOR:4", is_local=True)),
timeout=30,
)
self.assertEqual("sans", self.instr.measurement_type)
assert_tables(
self,
"detector_1det_1dae3card.dat",
"wiring1det_event_200218.dat",
"spec2det_280318_to_test_18_1.txt",
)
def test_WHEN_after_calling_do_sans_calling_do_trans_puts_instrument_is_in_trans_mode(self):
self.instr.do_sans()
self.instr.do_trans()
utilities.assert_with_timeout(
lambda: self.assertEqual("INSERTED", g.get_pv("VACUUM:MONITOR:4", is_local=True)),
timeout=30,
)
self.assertEqual("transmission", self.instr.measurement_type)
assert_tables(
self,
"detector_8mon_1dae3card_00.dat",
"wiring_8mon_1dae3card_00_hist.dat",
"spectrum_8mon_1dae3card_00.dat",
)
def test_WHEN_calling_do_sans_with_custom_daes_THEN_daes_are_set_correctly(self):
self.instr.do_sans(dae="histogram")
assert_tables(
self,
"detector_1det_1dae3card.dat",
"wiring1det_histogram_200218.dat",
"spec2det_130218.txt",
)
self.instr.do_trans()
self.instr.do_sans()
assert_tables(
self,
"detector_1det_1dae3card.dat",
"wiring1det_histogram_200218.dat",
"spec2det_130218.txt",
)
self.instr.set_default_dae(mode="event")
self.instr.do_sans()
assert_tables(
self,
"detector_1det_1dae3card.dat",
"wiring1det_event_200218.dat",
"spec2det_280318_to_test_18_1.txt",
)
class TestInstrumentScriptsLOQ(unittest.TestCase):
@classmethod
def setUpClass(cls):
g.set_instrument(None)
utilities.load_config_if_not_already_loaded("instrument_scripts_loq")
from instrument.loq.sans import LOQ
cls.instr = LOQ()
cls.instr.detector_lock(True)
g.set_pv("MOT:MTR0104.VMAX", 5, is_local=True)
g.set_pv("MOT:MTR0104.VELO", 5, is_local=True)
def test_WHEN_do_sans_is_called_instrument_is_in_sans_mode(self):
# No recsim for loq detector
self.instr.detector_lock(True)
self.instr.do_sans()
self.assertEqual("OUT", g.cget("Tx_Mon")["value"])
self.assertEqual("sans", self.instr.measurement_type)
assert_tables(self, "detector35576_M4.dat", "wiring35576_M4.dat", "spectra35576_M4.dat")
def test_WHEN_do_trans_is_called_instrument_is_in_trans_mode(self):
self.instr.detector_lock(True)
self.instr.do_trans()
self.assertEqual("IN", g.cget("Tx_Mon")["value"])
self.assertEqual("transmission", self.instr.measurement_type)
assert_tables(self, "detector8.dat", "wiring8.dat", "spectra8.dat")
def test_WHEN_after_calling_do_trans_calling_do_sans_puts_instrument_is_in_sans_mode(self):
self.instr.do_trans()
self.instr.do_sans()
self.assertEqual("OUT", g.cget("Tx_Mon")["value"])
self.assertEqual("sans", self.instr.measurement_type)
assert_tables(self, "detector35576_M4.dat", "wiring35576_M4.dat", "spectra35576_M4.dat")
def test_WHEN_after_calling_do_sans_calling_do_trans_puts_instrument_is_in_trans_mode(self):
self.instr.do_sans()
self.instr.do_trans()
self.assertEqual("IN", g.cget("Tx_Mon")["value"])
self.assertEqual("transmission", self.instr.measurement_type)
assert_tables(self, "detector8.dat", "wiring8.dat", "spectra8.dat")
def test_WHEN_calling_do_sans_with_a_custom_block_sets_block_correctly(self):
# Use monitor block in absence of other block
self.instr.do_sans(Tx_Mon="IN")
g.waitfor_move(blocks="Tx_Mon")
self.assertEqual("IN", g.cget("Tx_Mon")["value"])
## comment out until sesansroutines moved from setting to instrument scripts
# class TestInstrumentScriptsLarmor(unittest.TestCase):
# @classmethod
# def setUpClass(cls):
# g.set_instrument(None)
# utilities.load_config_if_not_already_loaded("instrument_scripts_larmor")
# from instrument.larmor.sans import Larmor
# cls.instr = Larmor()
# g.set_pv("CAEN:hv0:0:SIM", 1, is_local=True)
# for i in range(8, 12):
# g.set_pv(f"CAEN:hv0:0:SIM:{i}:status", "On", is_local=True)
# g.set_pv("MOT:MTR0602.VMAX", 5, is_local=True)
# g.set_pv("MOT:MTR0602.VELO", 5, is_local=True)
# g.set_pv("MOT:MTR0507.VMAX", 5, is_local=True)
# g.set_pv("MOT:MTR0507.VELO", 5, is_local=True)
# def test_WHEN_do_sans_is_called_instrument_is_in_sans_mode(self):
# self.instr.do_sans()
# self.assertEqual(200.0, g.cget("m4trans")["value"])
# self.assertEqual("sans", self.instr.measurement_type)
# assert_tables(self, "detector.dat", "wiring_dae3_event.dat", "spectra_1To1.dat")
# def test_WHEN_do_trans_is_called_instrument_is_in_trans_mode(self):
# self.instr.do_trans()
# self.assertEqual(0.0, g.cget("m4trans")["value"])
# self.assertEqual("transmission", self.instr.measurement_type)
# assert_tables(self, "detector_monitors_only.dat", "wiring_dae3_monitors_only.dat", "spectra_monitors_only.dat")
# def test_WHEN_after_calling_do_trans_calling_do_sans_puts_instrument_is_in_sans_mode(self):
# self.instr.do_trans()
# self.instr.do_sans()
# self.assertEqual(200.0, g.cget("m4trans")["value"])
# self.assertEqual("sans", self.instr.measurement_type)
# assert_tables(self, "detector.dat", "wiring_dae3_event.dat", "spectra_1To1.dat")
# def test_WHEN_after_calling_do_sans_calling_do_trans_puts_instrument_is_in_trans_mode(self):
# self.instr.do_sans()
# self.instr.do_trans()
# self.assertEqual(0.0, g.cget("m4trans")["value"])
# self.assertEqual("transmission", self.instr.measurement_type)
# assert_tables(self, "detector_monitors_only.dat", "wiring_dae3_monitors_only.dat", "spectra_monitors_only.dat")