-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathspecialized_parameters.py
More file actions
246 lines (204 loc) · 8.69 KB
/
Copy pathspecialized_parameters.py
File metadata and controls
246 lines (204 loc) · 8.69 KB
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
"""
Module for specialized parameters. The :mod:`qcodes.instrument.parameter`
module provides generic parameters for different generic cases. This module
provides useful/convenient specializations of such generic parameters.
"""
from __future__ import annotations
import warnings
from time import perf_counter
from typing import TYPE_CHECKING, Any, ClassVar, Generic, Literal
from qcodes.parameters.parameter_base import InstrumentTypeVar_co
from qcodes.utils import QCoDeSDeprecationWarning
from qcodes.validators import Strings, Validator
from .parameter import Parameter
if TYPE_CHECKING:
from collections.abc import Callable
from qcodes.instrument import Instrument
class ElapsedTimeParameter(Parameter):
"""
Parameter to measure elapsed time. Measures wall clock time since the
last reset of the instance's clock. The clock is reset upon creation of the
instance. The constructor passes kwargs along to the Parameter constructor.
Args:
name: The local name of the parameter. See the documentation of
:class:`qcodes.parameters.Parameter` for more details.
"""
_DEPRECATED_POSITIONAL_ARGS: ClassVar[tuple[str, ...]] = ("label",)
def __init__(
self, name: str, *args: Any, label: str = "Elapsed time", **kwargs: Any
):
if args:
# TODO: After QCoDeS 0.57 remove the args argument and delete this code block.
# we hardcode the class since mypy does not support __class__ and
# self / self.__class__ / type(self) in class bodies does not give
# exactly this class but the type of a subclass
positional_names = ElapsedTimeParameter._DEPRECATED_POSITIONAL_ARGS
if len(args) > len(positional_names):
raise TypeError(
f"{type(self).__name__}.__init__() takes at most "
f"{len(positional_names) + 2} positional arguments "
f"({len(args) + 2} given)"
)
_defaults: dict[str, Any] = {"label": "Elapsed time"}
_kwarg_vals: dict[str, Any] = {"label": label}
for i in range(len(args)):
arg_name = positional_names[i]
if _kwarg_vals[arg_name] != _defaults[arg_name]:
raise TypeError(
f"{type(self).__name__}.__init__() got multiple "
f"values for argument '{arg_name}'"
)
positional_arg_names = positional_names[: len(args)]
names_str = ", ".join(f"'{n}'" for n in positional_arg_names)
warnings.warn(
f"Passing {names_str} as positional argument(s) to "
f"{type(self).__name__} is deprecated. "
f"Please pass them as keyword arguments.",
QCoDeSDeprecationWarning,
stacklevel=2,
)
_pos = dict(zip(positional_names, args))
label = _pos.get("label", label)
hardcoded_kwargs = ["unit", "get_cmd", "set_cmd"]
for hck in hardcoded_kwargs:
if hck in kwargs:
raise ValueError(f'Can not set "{hck}" for an ElapsedTimeParameter.')
super().__init__(name=name, label=label, unit="s", set_cmd=False, **kwargs)
self._t0: float = perf_counter()
def get_raw(self) -> float:
return perf_counter() - self.t0
def reset_clock(self) -> None:
self._t0 = perf_counter()
@property
def t0(self) -> float:
return self._t0
class InstrumentRefParameter(
Parameter[str, InstrumentTypeVar_co], Generic[InstrumentTypeVar_co]
):
"""
An instrument reference parameter.
This parameter is useful when one needs a reference to another instrument
from within an instrument, e.g., when creating a meta instrument that
sets parameters on instruments it contains.
Args:
name: The name of the parameter that one wants to add.
instrument: The "parent" instrument this
parameter is attached to, if any.
initial_value: Starting value, may be None even if None does not
pass the validator. None is only allowed as an initial value
and cannot be set after initiation.
**kwargs: Passed to InstrumentRefParameter parent class
"""
_DEPRECATED_POSITIONAL_ARGS: ClassVar[tuple[str, ...]] = (
"instrument",
"label",
"unit",
"get_cmd",
"set_cmd",
"initial_value",
"max_val_age",
"vals",
"docstring",
)
def __init__(
self,
name: str,
*args: Any,
instrument: InstrumentTypeVar_co = None,
label: str | None = None,
unit: str | None = None,
get_cmd: str | Callable[..., Any] | Literal[False] | None = None,
set_cmd: str | Callable[..., Any] | Literal[False] | None = None,
initial_value: str | None = None,
max_val_age: float | None = None,
vals: Validator[Any] | None = None,
docstring: str | None = None,
**kwargs: Any,
) -> None:
if args:
# TODO: After QCoDeS 0.57 remove the args argument and delete this code block.
# we hardcode the class since mypy does not support __class__ and
# self / self.__class__ / type(self) in class bodies does not give
# exactly this class but the type of a subclass
positional_names = InstrumentRefParameter._DEPRECATED_POSITIONAL_ARGS
if len(args) > len(positional_names):
raise TypeError(
f"{type(self).__name__}.__init__() takes at most "
f"{len(positional_names) + 2} positional arguments "
f"({len(args) + 2} given)"
)
_defaults: dict[str, Any] = {
"instrument": None,
"label": None,
"unit": None,
"get_cmd": None,
"set_cmd": None,
"initial_value": None,
"max_val_age": None,
"vals": None,
"docstring": None,
}
_kwarg_vals: dict[str, Any] = {
"instrument": instrument,
"label": label,
"unit": unit,
"get_cmd": get_cmd,
"set_cmd": set_cmd,
"initial_value": initial_value,
"max_val_age": max_val_age,
"vals": vals,
"docstring": docstring,
}
for i in range(len(args)):
arg_name = positional_names[i]
if _kwarg_vals[arg_name] is not _defaults[arg_name]:
raise TypeError(
f"{type(self).__name__}.__init__() got multiple "
f"values for argument '{arg_name}'"
)
positional_arg_names = positional_names[: len(args)]
names_str = ", ".join(f"'{n}'" for n in positional_arg_names)
warnings.warn(
f"Passing {names_str} as positional argument(s) to "
f"{type(self).__name__} is deprecated. "
f"Please pass them as keyword arguments.",
QCoDeSDeprecationWarning,
stacklevel=2,
)
_pos = dict(zip(positional_names, args))
instrument = _pos.get("instrument", instrument)
label = _pos.get("label", label)
unit = _pos.get("unit", unit)
get_cmd = _pos.get("get_cmd", get_cmd)
set_cmd = _pos.get("set_cmd", set_cmd)
initial_value = _pos.get("initial_value", initial_value)
max_val_age = _pos.get("max_val_age", max_val_age)
vals = _pos.get("vals", vals)
docstring = _pos.get("docstring", docstring)
if vals is None:
vals = Strings()
if set_cmd is not None:
raise RuntimeError("InstrumentRefParameter does not support set_cmd.")
super().__init__(
name,
instrument=instrument,
label=label,
unit=unit,
get_cmd=get_cmd,
set_cmd=set_cmd,
initial_value=initial_value,
max_val_age=max_val_age,
vals=vals,
docstring=docstring,
**kwargs,
)
def get_instr(self) -> Instrument | None:
"""
Returns the instance of the instrument with the name equal to the
value of this parameter.
"""
# lazy import to avoid circular import
# since Instrument module depends on parameer module
from qcodes.instrument import Instrument # noqa: PLC0415
ref_instrument_name = self.get()
return Instrument.find_instrument(ref_instrument_name)