-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest_experiment_subscan.py
287 lines (251 loc) · 10.6 KB
/
test_experiment_subscan.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
"""
Tests for subscan functionality.
"""
import json
from ndscan.experiment import *
from fixtures import (AddOneFragment, ReboundAddOneFragment,
AddOneCustomAnalysisFragment, TwoAnalysisFragment,
TwoAnalysisAggregate)
from mock_environment import ExpFragmentCase
class Scan1DFragment(ExpFragment):
def build_fragment(self, klass):
self.setattr_fragment("child", klass)
scan = setattr_subscan(self, "scan", self.child, [(self.child, "value")])
assert self.scan == scan
def run_once(self):
return self.scan.run([(self.child.value, LinearGenerator(0, 3, 4, False))],
ScanOptions(seed=1234))[:2]
class SubscanCase(ExpFragmentCase):
def test_1d_subscan_return(self):
parent = self.create(Scan1DFragment, AddOneFragment)
self._test_1d(parent, parent.child.result)
def test_1d_rebound_subscan_return(self):
parent = self.create(Scan1DFragment, ReboundAddOneFragment)
self._test_1d(parent, parent.child.add_one.result)
def _test_1d(self, parent, result_channel):
coords, values = parent.run_once()
expected_values = [float(n) for n in range(0, 4)]
expected_results = [v + 1 for v in expected_values]
self.assertEqual(coords, {parent.child.value: expected_values})
self.assertEqual(values, {result_channel: expected_results})
def test_1d_result_channels(self):
parent = self.create(Scan1DFragment, AddOneFragment)
results = run_fragment_once(parent)
expected_values = [float(n) for n in range(0, 4)]
expected_results = [v + 1 for v in expected_values]
self.assertEqual(results[parent.scan_axis_0], expected_values)
self.assertEqual(results[parent.scan_channel_result], expected_results)
spec = json.loads(results[parent.scan_spec])
self.assertEqual(spec["fragment_fqn"], "fixtures.AddOneFragment")
self.assertEqual(spec["seed"], 1234)
curve_annotation = {
"kind": "computed_curve",
"parameters": {
"function_name": "lorentzian",
"associated_channels": ["channel_result"]
},
"coordinates": {},
"data": {
"a": {
"analysis_name": "fit_lorentzian_channel_result",
"kind": "online_result",
"result_key": "a"
},
"fwhm": {
"analysis_name": "fit_lorentzian_channel_result",
"kind": "online_result",
"result_key": "fwhm"
},
"x0": {
"analysis_name": "fit_lorentzian_channel_result",
"kind": "online_result",
"result_key": "x0"
},
"y0": {
"analysis_name": "fit_lorentzian_channel_result",
"kind": "online_result",
"result_key": "y0"
}
}
}
location_annotation = {
"kind": "location",
"parameters": {
"associated_channels": ["channel_result"]
},
"coordinates": {
"axis_0": {
"analysis_name": "fit_lorentzian_channel_result",
"kind": "online_result",
"result_key": "x0"
}
},
"data": {
"axis_0_error": {
"analysis_name": "fit_lorentzian_channel_result",
"kind": "online_result",
"result_key": "x0_error"
}
}
}
self.assertEqual(spec["annotations"], [curve_annotation, location_annotation])
self.assertEqual(
spec["online_analyses"], {
"fit_lorentzian_channel_result": {
"constants": {
"y0": 1.0
},
"data": {
"y": "channel_result",
"x": "axis_0"
},
"fit_type": "lorentzian",
"initial_values": {
"fwhm": 2.0
},
"kind": "named_fit"
}
})
self.assertEqual(
spec["channels"], {
"result": {
"description": "",
"scale": 1.0,
"path": "child/result",
"type": "float",
"unit": ""
}
})
self.assertEqual(spec["axes"], [{
"min": 0,
"max": 3,
"path": "child",
"param": {
"description": "Value to return",
"default": "0.0",
"fqn": "fixtures.AddOneFragment.value",
"spec": {
"is_scannable": True,
"scale": 1.0,
"step": 0.1
},
"type": "float"
},
"increment": 1.0
}])
def test_1d_custom_analysis(self):
parent = self.create(Scan1DFragment, AddOneCustomAnalysisFragment)
results = run_fragment_once(parent)
annotations = json.loads(results[parent.scan_spec])["annotations"]
x_location = {
'coordinates': {
'axis_0': {
'kind': 'fixed',
'value': 1.5
}
},
'data': {},
'kind': 'location',
'parameters': {}
}
y_location = {
'coordinates': {
'channel_result': {
'kind': 'fixed',
'value': 2.5
}
},
'data': {},
'kind': 'location',
'parameters': {}
}
# FIXME: This should probably use fuzzy comparison for the floating point
# values.
self.assertEqual(annotations, [x_location, y_location])
def test_fragment_detach(self):
parent = self.create(Scan1DFragment, AddOneFragment)
run_fragment_once(parent)
# Make sure the setup and cleanup methods aren't also called during the parent
# fragment setup/cleanup (in addition to the subscan).
self.assertEqual(parent.child.num_host_setup_calls, 1)
self.assertEqual(parent.child.num_device_setup_calls, 4)
self.assertEqual(parent.child.num_device_cleanup_calls, 1)
self.assertEqual(parent.child.num_host_cleanup_calls, 1)
class RunSubscanTwiceFragment(ExpFragment):
def build_fragment(self):
self.setattr_fragment("child", AddOneFragment)
setattr_subscan(self,
"scan",
self.child, [(self.child, "value")],
expose_analysis_results=False)
def run_once(self):
r0 = self.scan.run([(self.child.value, LinearGenerator(0, 3, 4, False))])
r1 = self.scan.run([(self.child.value, LinearGenerator(4, 7, 4, False))])
return r0, r1
class RunSubscanTwiceCase(ExpFragmentCase):
def test_1d_subscan_twice(self):
parent = self.create(RunSubscanTwiceFragment)
results = parent.run_once()
for base, (coords, values, _) in zip([0, 4], results):
expected_values = [float(n) for n in range(base, base + 4)]
expected_results = [v + 1 for v in expected_values]
self.assertEqual(coords, {parent.child.value: expected_values})
self.assertEqual(values, {parent.child.result: expected_results})
class SubscanAnalysisFragment(ExpFragment):
def build_fragment(self,
declare_both_scannable=False,
always_execute_analyses=True):
self.always_execute_analyses = always_execute_analyses
self.setattr_fragment("child", TwoAnalysisFragment)
axes = [(self.child, "a")]
if declare_both_scannable:
axes.append((self.child, "b"))
setattr_subscan(self, "scan", self.child, axes)
self.had_result = False
def run_once(self):
_, _, analysis_results = self.scan.run(
[(self.child.a, LinearGenerator(0.0, 1.0, 3, True))],
execute_default_analyses=self.always_execute_analyses)
self.had_result = "result_a" in analysis_results
class AggregateSubscanAnalysisFragment(ExpFragment):
def build_fragment(self):
self.setattr_fragment("child", TwoAnalysisAggregate)
setattr_subscan(self, "scan", self.child, [(self.child, "a")])
self.had_all_results = False
def run_once(self):
_, _, analysis_results = self.scan.run([(self.child.a,
LinearGenerator(0.0, 1.0, 3, True))])
self.had_all_results = all(f"{n}_result_a" in analysis_results
for n in ("first", "second"))
class SubscanAnalysisCase(ExpFragmentCase):
def test_simple_filtering(self):
parent = self.create(SubscanAnalysisFragment)
results = run_fragment_once(parent)
spec = json.loads(results[parent.scan_spec])
self.assertEqual(spec["analysis_results"], {"result_a": "scan_result_a"})
self.assertEqual(results[parent.scan_result_a], 42.0)
self.assertTrue(parent.had_result)
def _test_subset_filtering(self, always_execute_analyses):
parent = self.create(SubscanAnalysisFragment,
declare_both_scannable=True,
always_execute_analyses=always_execute_analyses)
results = run_fragment_once(parent)
spec = json.loads(results[parent.scan_spec])
# Shouldn't have a result channel, since it wasn't statically known which
# axes would be scanned.
self.assertEqual(spec.get("analysis_results", {}), {})
# If requested, the analysis should have still been executed at run()-time,
# though.
self.assertEqual(parent.had_result, always_execute_analyses)
def test_subset_filtering(self):
self._test_subset_filtering(False)
def test_subset_filtering_2(self):
self._test_subset_filtering(True)
def test_aggregate(self):
# For simplicity, test AggregateExpFragment through an actual subscan instead of
# manually verifying the analysis result handling/…
parent = self.create(AggregateSubscanAnalysisFragment)
results = run_fragment_once(parent)
self.assertTrue(parent.had_all_results)
self.assertEqual(results[parent.scan_first_result_a], 42.0)
self.assertEqual(results[parent.scan_second_result_a], 42.0)