Debian Codegraph @

examples/mf4_demo.py

raw
1 from typing import Any
2
3 import numpy as np
4 import numpy.typing as npt
5
6 from asammdf import MDF, Signal
7
8 cycles = 100
9 sigs = []
10
11 mdf = MDF()
12
13 t = np.arange(cycles, dtype=np.float64)
14
15 # no conversion
16 sig = Signal(
17 np.ones(cycles, dtype=np.uint64),
18 t,
19 name="Channel_no_conversion",
20 unit="s",
21 conversion=None,
22 comment="Unsigned 64 bit channel {}",
23 )
24 sigs.append(sig)
25
26 # linear
27 conversion: dict[str, object] = {"a": 2, "b": -0.5}
28 sig = Signal(
29 np.ones(cycles, dtype=np.int64),
30 t,
31 name="Channel_linear_conversion",
32 unit="Nm",
33 conversion=conversion,
34 comment="Signed 64bit channel with linear conversion",
35 )
36 sigs.append(sig)
37
38
39 # algebraic
40 conversion = {"formula": "2 * sin(X)"}
41 sig = Signal(
42 np.arange(cycles, dtype=np.int32) / 100.0,
43 t,
44 name="Channel_algebraic",
45 unit="eV",
46 conversion=conversion,
47 comment="Sinus channel with algebraic conversion",
48 )
49 sigs.append(sig)
50
51 # rational
52 conversion = {"P1": 0, "P2": 4, "P3": -0.5, "P4": 0, "P5": 0, "P6": 1}
53 sig = Signal(
54 np.ones(cycles, dtype=np.int64),
55 t,
56 name="Channel_rational_conversion",
57 unit="Nm",
58 conversion=conversion,
59 comment="Channel with rational conversion",
60 )
61 sigs.append(sig)
62
63 # string channel
64 strings = [f"String channel sample {j}".encode("ascii") for j in range(cycles)]
65 sig = Signal(
66 np.array(strings),
67 t,
68 name="Channel_string",
69 comment="String channel",
70 encoding="latin-1",
71 )
72 sigs.append(sig)
73
74 # byte array
75 ones = np.ones(cycles, dtype=np.dtype("(8,)u1"))
76 sig = Signal(ones * 111, t, name="Channel_bytearay", comment="Byte array channel")
77 sigs.append(sig)
78
79 # tabular
80 vals = 20
81 conversion = {f"raw_{i}": i for i in range(vals)}
82 conversion.update({f"phys_{i}": -i for i in range(vals)})
83 sig = Signal(
84 np.arange(cycles, dtype=np.uint32) % 20,
85 t,
86 name="Channel_tabular",
87 unit="-",
88 conversion=conversion,
89 comment="Tabular channel",
90 )
91 sigs.append(sig)
92
93 # value to text
94 vals = 20
95 conversion = {f"val_{i}": i for i in range(vals)}
96 conversion.update({f"text_{i}": f"key_{i}".encode("ascii") for i in range(vals)})
97 conversion["default"] = b"default key"
98 sig = Signal(
99 np.arange(cycles, dtype=np.uint32) % 30,
100 t,
101 name="Channel_value_to_text",
102 conversion=conversion,
103 comment="Value to text channel",
104 )
105 sigs.append(sig)
106
107 # tabular with range
108 vals = 20
109 conversion = {f"lower_{i}": i * 10 for i in range(vals)}
110 conversion.update({f"upper_{i}": (i + 1) * 10 for i in range(vals)})
111 conversion.update({f"phys_{i}": i for i in range(vals)})
112 conversion["default"] = -1
113 sig = Signal(
114 2 * np.arange(cycles, dtype=np.float64),
115 t,
116 name="Channel_value_range_to_value",
117 unit="order",
118 conversion=conversion,
119 comment="Value range to value channel",
120 )
121 sigs.append(sig)
122
123 # value range to text
124 vals = 20
125 conversion = {f"lower_{i}": i * 10 for i in range(vals)}
126 conversion.update({f"upper_{i}": (i + 1) * 10 - 5 for i in range(vals)})
127 conversion.update({f"text_{i}": f"Level {i}" for i in range(vals)})
128 conversion["default"] = b"Unknown level"
129 sig = Signal(
130 6 * np.arange(cycles, dtype=np.uint64) % 240,
131 t,
132 name="Channel_value_range_to_text",
133 conversion=conversion,
134 comment="Value range to text channel",
135 )
136 sigs.append(sig)
137
138
139 mdf.append(sigs, comment="single dimensional channels", common_timebase=True)
140
141
142 sigs = []
143
144 # lookup tabel with axis
145 samples: list[npt.NDArray[Any]] = [
146 np.ones((cycles, 2, 3), dtype=np.uint64) * 1,
147 np.ones((cycles, 2), dtype=np.uint64) * 2,
148 np.ones((cycles, 3), dtype=np.uint64) * 3,
149 ]
150
151 types: list[npt.DTypeLike] = [
152 ("Channel_lookup_with_axis", "(2, 3)<u8"),
153 ("channel_Y_axis", "(2, )<u8"),
154 ("channel_X_axis", "(3, )<u8"),
155 ]
156
157 sig = Signal(
158 np.rec.fromarrays(samples, dtype=np.dtype(types)),
159 t,
160 name="Channel_lookup_with_axis",
161 unit="A",
162 comment="Array channel with axis",
163 )
164 sigs.append(sig)
165
166 # lookup tabel with default axis
167 samples = [np.ones((cycles, 2, 3), dtype=np.uint64) * 4]
168
169 types = [("Channel_lookup_with_default_axis", "(2, 3)<u8")]
170
171 sig = Signal(
172 np.rec.fromarrays(samples, dtype=np.dtype(types)),
173 t,
174 name="Channel_lookup_with_default_axis",
175 unit="mA",
176 comment="Array channel with default axis",
177 )
178 sigs.append(sig)
179
180 # structure channel composition
181 samples = [
182 np.ones(cycles, dtype=np.uint8) * 10,
183 np.ones(cycles, dtype=np.uint16) * 20,
184 np.ones(cycles, dtype=np.uint32) * 30,
185 np.ones(cycles, dtype=np.uint64) * 40,
186 np.ones(cycles, dtype=np.int8) * -10,
187 np.ones(cycles, dtype=np.int16) * -20,
188 np.ones(cycles, dtype=np.int32) * -30,
189 np.ones(cycles, dtype=np.int64) * -40,
190 ]
191
192 types = [
193 ("struct_channel_0", np.uint8),
194 ("struct_channel_1", np.uint16),
195 ("struct_channel_2", np.uint32),
196 ("struct_channel_3", np.uint64),
197 ("struct_channel_4", np.int8),
198 ("struct_channel_5", np.int16),
199 ("struct_channel_6", np.int32),
200 ("struct_channel_7", np.int64),
201 ]
202
203 sig = Signal(
204 np.rec.fromarrays(samples, dtype=np.dtype(types)),
205 t,
206 name="Channel_structure_composition",
207 comment="Structure channel composition",
208 )
209 sigs.append(sig)
210
211
212 # nested structures
213 arrays = [
214 np.ones(cycles, dtype=np.float64) * 41,
215 np.ones(cycles, dtype=np.float64) * 42,
216 np.ones(cycles, dtype=np.float64) * 43,
217 np.ones(cycles, dtype=np.float64) * 44,
218 ]
219
220 types = [
221 ("level41", np.float64),
222 ("level42", np.float64),
223 ("level43", np.float64),
224 ("level44", np.float64),
225 ]
226
227 l4_arr = np.rec.fromarrays(arrays, dtype=types)
228
229 arrays = [
230 l4_arr,
231 l4_arr,
232 l4_arr,
233 ]
234
235 types = [
236 ("level31", l4_arr.dtype),
237 ("level32", l4_arr.dtype),
238 ("level33", l4_arr.dtype),
239 ]
240
241 l3_arr = np.rec.fromarrays(arrays, dtype=types)
242
243
244 arrays = [
245 l3_arr,
246 l3_arr,
247 ]
248
249 types = [("level21", l3_arr.dtype), ("level22", l3_arr.dtype)]
250
251 l2_arr = np.rec.fromarrays(arrays, dtype=types)
252
253
254 arrays = [l2_arr]
255
256 types = [("level11", l2_arr.dtype)]
257
258 l1_arr = np.rec.fromarrays(arrays, dtype=types)
259
260
261 sigs.append(Signal(l1_arr, t, name="Nested_structures"))
262
263 mdf.append(sigs, comment="arrays", common_timebase=True)
264
265 mdf.save("demo.mf4", overwrite=True)