-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfixtures.py
236 lines (181 loc) · 7.52 KB
/
fixtures.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
"""
Common fragments/… for unit tests.
"""
import numpy
from ndscan.experiment import *
class AddOneFragment(ExpFragment):
def build_fragment(self):
self.setattr_param("value", FloatParam, "Value to return", 0.0)
self.setattr_result("result", FloatChannel)
self.num_prepare_calls = 0
self.num_host_setup_calls = 0
self.num_device_setup_calls = 0
self.num_host_cleanup_calls = 0
self.num_device_cleanup_calls = 0
def prepare(self):
self.num_prepare_calls += 1
def host_setup(self):
self.num_host_setup_calls += 1
def device_setup(self):
self.num_device_setup_calls += 1
def host_cleanup(self):
self.num_host_cleanup_calls += 1
def device_cleanup(self):
self.num_device_cleanup_calls += 1
def run_once(self):
self.result.push(self.value.get() + 1)
def get_default_analyses(self):
# Nonsensical fit spec to exercise serialisation code.
return [
OnlineFit(
"lorentzian",
{
"x": self.value,
"y": self.result
},
constants={"y0": 1.0},
initial_values={"fwhm": 2.0},
)
]
class ReboundAddOneFragment(ExpFragment):
def build_fragment(self):
self.setattr_fragment("add_one", AddOneFragment)
self.setattr_param_rebind("value", self.add_one, unit="ms")
def run_once(self):
self.add_one.run_once()
class ReboundReboundAddOneFragment(ExpFragment):
def build_fragment(self):
self.setattr_fragment("rebound_add_one", ReboundAddOneFragment)
self.setattr_param_rebind("value", self.rebound_add_one)
def run_once(self):
self.rebound_add_one.run_once()
class MultiReboundAddOneFragment(ExpFragment):
def build_fragment(self):
self.setattr_fragment("first", AddOneFragment)
self.setattr_fragment("second", AddOneFragment)
self.setattr_param_like("value", self.first)
self.first.bind_param("value", self.value)
self.second.bind_param("value", self.value)
def run_once(self):
self.first.run_once()
self.second.run_once()
class AddOneCustomAnalysisFragment(AddOneFragment):
def get_default_analyses(self):
return [CustomAnalysis({self.value}, self._analyze)]
def _analyze(self, axis_values, result_values):
return [
Annotation("location", {self.value: numpy.mean(axis_values[self.value])}),
Annotation("location",
{self.result: numpy.mean(result_values[self.result])})
]
class TrivialKernelFragment(ExpFragment):
def build_fragment(self):
pass
@kernel
def device_setup(self):
pass
@kernel
def run_once(self):
pass
class TransitoryErrorFragment(ExpFragment):
"""Fails device_setup() and run_once() a configurable number of times with a
transitory error before succeeding.
"""
def build_fragment(self):
self.num_device_setup_to_fail = 0
self.num_device_setup_to_restart_fail = 0
self.num_run_once_to_fail = 0
self.num_run_once_to_restart_fail = 0
self.setattr_result("result", IntChannel)
def device_setup(self):
if self.num_device_setup_to_restart_fail > 0:
self.num_device_setup_to_restart_fail -= 1
raise RestartKernelTransitoryError
if self.num_device_setup_to_fail > 0:
self.num_device_setup_to_fail -= 1
raise TransitoryError
def run_once(self):
if self.num_run_once_to_restart_fail > 0:
self.num_run_once_to_restart_fail -= 1
raise RestartKernelTransitoryError
if self.num_run_once_to_fail > 0:
self.num_run_once_to_fail -= 1
raise TransitoryError
self.result.push(42)
class MultiPointTransitoryErrorFragment(TransitoryErrorFragment):
"""TransitoryErrorFragment that resets counters after run_once() has completed
successfully (for testing scan behaviour).
"""
def build_fragment(self,
num_device_setup_to_fail=0,
num_device_setup_to_restart_fail=0,
num_run_once_to_fail=0,
num_run_once_to_restart_fail=0):
super().build_fragment()
self.orig_num_device_setup_to_fail = num_device_setup_to_fail
self.orig_num_device_setup_to_restart_fail = num_device_setup_to_restart_fail
self.orig_num_run_once_to_fail = num_run_once_to_fail
self.orig_num_run_once_to_restart_fail = num_run_once_to_restart_fail
self.reset_counters()
def reset_counters(self):
self.num_device_setup_to_fail = self.orig_num_device_setup_to_fail
self.num_device_setup_to_restart_fail = \
self.orig_num_device_setup_to_restart_fail
self.num_run_once_to_fail = self.orig_num_run_once_to_fail
self.num_run_once_to_restart_fail = self.orig_num_run_once_to_restart_fail
def run_once(self):
super().run_once()
self.reset_counters()
class RequestTerminationFragment(ExpFragment):
"""To test handling of TerminationRequested exceptions without having to implement
actual termination requests in a mock scheduler, raises TerminationRequested as
soon as it is run.
"""
def build_fragment(self):
pass
def run_once(self):
raise TerminationRequested
class TwoAnalysisFragment(ExpFragment):
def build_fragment(self):
self.setattr_param("a", FloatParam, "a", 0.0)
self.setattr_param("b", FloatParam, "b", 0.0)
def get_default_analyses(self):
def make_analyse(channel_name):
def analyse(coords, values, results):
# Explicitly specify the channel name instead of just taking the only
# value to make sure the names work as expected from the user side.
results[channel_name].push(42.0)
return []
return analyse
return [
CustomAnalysis([self.a], make_analyse("result_a"),
[FloatChannel("result_a")]),
CustomAnalysis([self.b], make_analyse("result_b"),
[FloatChannel("result_b")])
]
class AddOneAggregate(AggregateExpFragment):
def build_fragment(self) -> None:
self.setattr_fragment("a", AddOneFragment)
self.setattr_fragment("b", AddOneFragment)
self.setattr_result("sum", FloatChannel)
def push_sum():
self.sum.push(self.a.value.get() + 1 + self.b.value.get() + 1)
return super().build_fragment([self.a, self.b, push_sum])
class TrivialKernelAggregate(AggregateExpFragment):
def build_fragment(self) -> None:
self.setattr_fragment("a", TrivialKernelFragment)
self.setattr_fragment("b", TrivialKernelFragment)
return super().build_fragment([self.a, self.b])
class TwoAnalysisAggregate(AggregateExpFragment):
def build_fragment(self) -> None:
self.setattr_fragment("first", TwoAnalysisFragment)
self.setattr_fragment("second", TwoAnalysisFragment)
self.setattr_param_rebind("a", self.first)
self.second.bind_param("a", self.a)
return super().build_fragment([self.first, self.second])
class ReadParamDefault(ExpFragment):
def build_fragment(self):
self.setattr_param("foo", IntParam, "Foo", default="dataset('foo', 0)")
self.setattr_result("value", IntChannel)
def run_once(self) -> None:
self.value.push(self.foo.get())