| 1 |
.. raw:: html |
| 2 |
|
| 3 |
<style> .red {color:red} </style> |
| 4 |
<style> .blue {color:blue} </style> |
| 5 |
<style> .green {color:green} </style> |
| 6 |
<style> .cyan {color:cyan} </style> |
| 7 |
<style> .magenta {color:magenta} </style> |
| 8 |
<style> .orange {color:orange} </style> |
| 9 |
<style> .brown {color:brown} </style> |
| 10 |
|
| 11 |
.. role:: red |
| 12 |
.. role:: blue |
| 13 |
.. role:: green |
| 14 |
.. role:: cyan |
| 15 |
.. role:: magenta |
| 16 |
.. role:: orange |
| 17 |
.. role:: brown |
| 18 |
|
| 19 |
.. _examples: |
| 20 |
|
| 21 |
-------- |
| 22 |
Examples |
| 23 |
-------- |
| 24 |
|
| 25 |
Working with MDF |
| 26 |
================ |
| 27 |
|
| 28 |
.. code-block:: python |
| 29 |
|
| 30 |
from asammdf import MDF, Signal |
| 31 |
import numpy as np |
| 32 |
|
| 33 |
|
| 34 |
# create 3 Signal objects |
| 35 |
|
| 36 |
timestamps = np.array([0.1, 0.2, 0.3, 0.4, 0.5], dtype=np.float32) |
| 37 |
|
| 38 |
# unit8 |
| 39 |
s_uint8 = Signal(samples=np.array([0, 1, 2, 3, 4], dtype=np.uint8), |
| 40 |
timestamps=timestamps, |
| 41 |
name='Uint8_Signal', |
| 42 |
unit='u1') |
| 43 |
# int32 |
| 44 |
s_int32 = Signal(samples=np.array([-20, -10, 0, 10, 20], dtype=np.int32), |
| 45 |
timestamps=timestamps, |
| 46 |
name='Int32_Signal', |
| 47 |
unit='i4') |
| 48 |
|
| 49 |
# float64 |
| 50 |
s_float64 = Signal(samples=np.array([-20, -10, 0, 10, 20], dtype=np.float64), |
| 51 |
timestamps=timestamps, |
| 52 |
name='Float64_Signal', |
| 53 |
unit='f8') |
| 54 |
|
| 55 |
# create empty MDf version 4.00 file |
| 56 |
with MDF(version='4.10') as mdf4: |
| 57 |
|
| 58 |
# append the 3 signals to the new file |
| 59 |
signals = [s_uint8, s_int32, s_float64] |
| 60 |
mdf4.append(signals, comment='Created by Python') |
| 61 |
|
| 62 |
# save new file |
| 63 |
mdf4.save('my_new_file.mf4', overwrite=True) |
| 64 |
|
| 65 |
# convert new file to mdf version 3.10 |
| 66 |
mdf3 = mdf4.convert(version='3.10') |
| 67 |
print(mdf3.version) |
| 68 |
|
| 69 |
# get the float signal |
| 70 |
sig = mdf3.get('Float64_Signal') |
| 71 |
print(sig) |
| 72 |
|
| 73 |
# cut measurement from 0.3s to end of measurement |
| 74 |
mdf4_cut = mdf4.cut(start=0.3) |
| 75 |
mdf4_cut.get('Float64_Signal').plot() |
| 76 |
|
| 77 |
# cut measurement from start of measurement to 0.4s |
| 78 |
mdf4_cut = mdf4.cut(stop=0.45) |
| 79 |
mdf4_cut.get('Float64_Signal').plot() |
| 80 |
|
| 81 |
# filter some signals from the file |
| 82 |
mdf4 = mdf4.filter(['Int32_Signal', 'Uint8_Signal']) |
| 83 |
|
| 84 |
# save using zipped transpose deflate blocks |
| 85 |
mdf4.save('out.mf4', compression=2, overwrite=True) |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
Working with Signal |
| 91 |
=================== |
| 92 |
|
| 93 |
.. code-block:: python |
| 94 |
|
| 95 |
from asammdf import Signal |
| 96 |
import numpy as np |
| 97 |
|
| 98 |
|
| 99 |
# create 3 Signal objects with different time stamps |
| 100 |
|
| 101 |
# unit8 with 100ms time raster |
| 102 |
timestamps = np.array([0.1 * t for t in range(5)], dtype=np.float32) |
| 103 |
s_uint8 = Signal(samples=np.array([t for t in range(5)], dtype=np.uint8), |
| 104 |
timestamps=timestamps, |
| 105 |
name='Uint8_Signal', |
| 106 |
unit='u1') |
| 107 |
|
| 108 |
# int32 with 50ms time raster |
| 109 |
timestamps = np.array([0.05 * t for t in range(10)], dtype=np.float32) |
| 110 |
s_int32 = Signal(samples=np.array(list(range(-500, 500, 100)), dtype=np.int32), |
| 111 |
timestamps=timestamps, |
| 112 |
name='Int32_Signal', |
| 113 |
unit='i4') |
| 114 |
|
| 115 |
# float64 with 300ms time raster |
| 116 |
timestamps = np.array([0.3 * t for t in range(3)], dtype=np.float32) |
| 117 |
s_float64 = Signal(samples=np.array(list(range(2000, -1000, -1000)), dtype=np.int32), |
| 118 |
timestamps=timestamps, |
| 119 |
name='Float64_Signal', |
| 120 |
unit='f8') |
| 121 |
|
| 122 |
# map signals |
| 123 |
xs = np.linspace(-1, 1, 50) |
| 124 |
ys = np.linspace(-1, 1, 50) |
| 125 |
X, Y = np.meshgrid(xs, ys) |
| 126 |
vals = np.linspace(0, 180. / np.pi, 100) |
| 127 |
phi = np.ones((len(vals), 50, 50), dtype=np.float64) |
| 128 |
for i, val in enumerate(vals): |
| 129 |
phi[i] *= val |
| 130 |
R = 1 - np.sqrt(X**2 + Y**2) |
| 131 |
samples = np.cos(2 * np.pi * X + phi) * R |
| 132 |
|
| 133 |
timestamps = np.arange(0, 2, 0.02) |
| 134 |
|
| 135 |
s_map = Signal(samples=samples, |
| 136 |
timestamps=timestamps, |
| 137 |
name='Variable Map Signal', |
| 138 |
unit='dB') |
| 139 |
s_map.plot() |
| 140 |
|
| 141 |
|
| 142 |
prod = s_float64 * s_uint8 |
| 143 |
prod.name = 'Uint8_Signal * Float64_Signal' |
| 144 |
prod.unit = '*' |
| 145 |
prod.plot() |
| 146 |
|
| 147 |
pow2 = s_uint8 ** 2 |
| 148 |
pow2.name = 'Uint8_Signal ^ 2' |
| 149 |
pow2.unit = 'u1^2' |
| 150 |
pow2.plot() |
| 151 |
|
| 152 |
allsum = s_uint8 + s_int32 + s_float64 |
| 153 |
allsum.name = 'Uint8_Signal + Int32_Signal + Float64_Signal' |
| 154 |
allsum.unit = '+' |
| 155 |
allsum.plot() |
| 156 |
|
| 157 |
# inplace operations |
| 158 |
prod *= -1 |
| 159 |
prod.name = '- Uint8_Signal * Float64_Signal' |
| 160 |
prod.plot() |
| 161 |
|
| 162 |
# cut signal |
| 163 |
s_int32.plot() |
| 164 |
cut_signal = s_int32.cut(start=0.2, stop=0.35) |
| 165 |
cut_signal.plot() |
| 166 |
|
| 167 |
|
| 168 |
MF4 demo file generator |
| 169 |
======================= |
| 170 |
|
| 171 |
.. code-block:: python |
| 172 |
|
| 173 |
from asammdf import MDF, SUPPORTED_VERSIONS, Signal |
| 174 |
import numpy as np |
| 175 |
|
| 176 |
cycles = 100 |
| 177 |
sigs = [] |
| 178 |
|
| 179 |
mdf = MDF() |
| 180 |
|
| 181 |
t = np.arange(cycles, dtype=np.float64) |
| 182 |
|
| 183 |
# no conversion |
| 184 |
sig = Signal( |
| 185 |
np.ones(cycles, dtype=np.uint64), |
| 186 |
t, |
| 187 |
name='Channel_no_conversion', |
| 188 |
unit='s', |
| 189 |
conversion=None, |
| 190 |
comment='Unsigned 64 bit channel {}', |
| 191 |
) |
| 192 |
sigs.append(sig) |
| 193 |
|
| 194 |
# linear |
| 195 |
conversion = { |
| 196 |
'a': 2, |
| 197 |
'b': -0.5, |
| 198 |
} |
| 199 |
sig = Signal( |
| 200 |
np.ones(cycles, dtype=np.int64), |
| 201 |
t, |
| 202 |
name='Channel_linear_conversion', |
| 203 |
unit='Nm', |
| 204 |
conversion=conversion, |
| 205 |
comment='Signed 64bit channel with linear conversion', |
| 206 |
) |
| 207 |
sigs.append(sig) |
| 208 |
|
| 209 |
|
| 210 |
# algebraic |
| 211 |
conversion = { |
| 212 |
'formula': '2 * sin(X)', |
| 213 |
} |
| 214 |
sig = Signal( |
| 215 |
np.arange(cycles, dtype=np.int32) / 100.0, |
| 216 |
t, |
| 217 |
name='Channel_algebraic', |
| 218 |
unit='eV', |
| 219 |
conversion=conversion, |
| 220 |
comment='Sinus channel with algebraic conversion', |
| 221 |
) |
| 222 |
sigs.append(sig) |
| 223 |
|
| 224 |
# rational |
| 225 |
conversion = { |
| 226 |
'P1': 0, |
| 227 |
'P2': 4, |
| 228 |
'P3': -0.5, |
| 229 |
'P4': 0, |
| 230 |
'P5': 0, |
| 231 |
'P6': 1, |
| 232 |
} |
| 233 |
sig = Signal( |
| 234 |
np.ones(cycles, dtype=np.int64), |
| 235 |
t, |
| 236 |
name='Channel_rational_conversion', |
| 237 |
unit='Nm', |
| 238 |
conversion=conversion, |
| 239 |
comment='Channel with rational conversion', |
| 240 |
) |
| 241 |
sigs.append(sig) |
| 242 |
|
| 243 |
# string channel |
| 244 |
sig = [ |
| 245 |
'String channel sample {}'.format(j).encode('ascii') |
| 246 |
for j in range(cycles) |
| 247 |
] |
| 248 |
sig = Signal( |
| 249 |
np.array(sig), |
| 250 |
t, |
| 251 |
name='Channel_string', |
| 252 |
comment='String channel', |
| 253 |
encoding='latin-1', |
| 254 |
) |
| 255 |
sigs.append(sig) |
| 256 |
|
| 257 |
# byte array |
| 258 |
ones = np.ones(cycles, dtype=np.dtype('(8,)u1')) |
| 259 |
sig = Signal( |
| 260 |
ones*111, |
| 261 |
t, |
| 262 |
name='Channel_bytearay', |
| 263 |
comment='Byte array channel', |
| 264 |
) |
| 265 |
sigs.append(sig) |
| 266 |
|
| 267 |
# tabular |
| 268 |
vals = 20 |
| 269 |
conversion = { |
| 270 |
'raw_{}'.format(i): i |
| 271 |
for i in range(vals) |
| 272 |
} |
| 273 |
conversion.update( |
| 274 |
{ |
| 275 |
'phys_{}'.format(i): -i |
| 276 |
for i in range(vals) |
| 277 |
} |
| 278 |
) |
| 279 |
sig = Signal( |
| 280 |
np.arange(cycles, dtype=np.uint32) % 20, |
| 281 |
t, |
| 282 |
name='Channel_tabular', |
| 283 |
unit='-', |
| 284 |
conversion=conversion, |
| 285 |
comment='Tabular channel', |
| 286 |
) |
| 287 |
sigs.append(sig) |
| 288 |
|
| 289 |
# value to text |
| 290 |
vals = 20 |
| 291 |
conversion = { |
| 292 |
'val_{}'.format(i): i |
| 293 |
for i in range(vals) |
| 294 |
} |
| 295 |
conversion.update( |
| 296 |
{ |
| 297 |
'text_{}'.format(i): 'key_{}'.format(i).encode('ascii') |
| 298 |
for i in range(vals) |
| 299 |
} |
| 300 |
) |
| 301 |
conversion['default'] = b'default key' |
| 302 |
sig = Signal( |
| 303 |
np.arange(cycles, dtype=np.uint32) % 30, |
| 304 |
t, |
| 305 |
name='Channel_value_to_text', |
| 306 |
conversion=conversion, |
| 307 |
comment='Value to text channel', |
| 308 |
) |
| 309 |
sigs.append(sig) |
| 310 |
|
| 311 |
# tabular with range |
| 312 |
vals = 20 |
| 313 |
conversion = { |
| 314 |
'lower_{}'.format(i): i * 10 |
| 315 |
for i in range(vals) |
| 316 |
} |
| 317 |
conversion.update( |
| 318 |
{ |
| 319 |
'upper_{}'.format(i): (i + 1) * 10 |
| 320 |
for i in range(vals) |
| 321 |
} |
| 322 |
) |
| 323 |
conversion.update( |
| 324 |
{ |
| 325 |
'phys_{}'.format(i): i |
| 326 |
for i in range(vals) |
| 327 |
} |
| 328 |
) |
| 329 |
conversion['default'] = -1 |
| 330 |
sig = Signal( |
| 331 |
2 * np.arange(cycles, dtype=np.float64), |
| 332 |
t, |
| 333 |
name='Channel_value_range_to_value', |
| 334 |
unit='order', |
| 335 |
conversion=conversion, |
| 336 |
comment='Value range to value channel', |
| 337 |
) |
| 338 |
sigs.append(sig) |
| 339 |
|
| 340 |
# value range to text |
| 341 |
vals = 20 |
| 342 |
conversion = { |
| 343 |
'lower_{}'.format(i): i * 10 |
| 344 |
for i in range(vals) |
| 345 |
} |
| 346 |
conversion.update( |
| 347 |
{ |
| 348 |
'upper_{}'.format(i): (i + 1) * 10 |
| 349 |
for i in range(vals) |
| 350 |
} |
| 351 |
) |
| 352 |
conversion.update( |
| 353 |
{ |
| 354 |
'text_{}'.format(i): 'Level {}'.format(i) |
| 355 |
for i in range(vals) |
| 356 |
} |
| 357 |
) |
| 358 |
conversion['default'] = b'Unknown level' |
| 359 |
sig = Signal( |
| 360 |
6 * np.arange(cycles, dtype=np.uint64) % 240, |
| 361 |
t, |
| 362 |
name='Channel_value_range_to_text', |
| 363 |
conversion=conversion, |
| 364 |
comment='Value range to text channel', |
| 365 |
) |
| 366 |
sigs.append(sig) |
| 367 |
|
| 368 |
|
| 369 |
mdf.append(sigs, comment='single dimensional channels', common_timebase=True) |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
sigs = [] |
| 374 |
|
| 375 |
# lookup tabel with axis |
| 376 |
samples = [ |
| 377 |
np.ones((cycles, 2, 3), dtype=np.uint64) * 1, |
| 378 |
np.ones((cycles, 2), dtype=np.uint64) * 2, |
| 379 |
np.ones((cycles, 3), dtype=np.uint64) * 3, |
| 380 |
] |
| 381 |
|
| 382 |
types = [ |
| 383 |
('Channel_lookup_with_axis', '(2, 3)<u8'), |
| 384 |
('channel_axis_1', '(2, )<u8'), |
| 385 |
('channel_axis_2', '(3, )<u8'), |
| 386 |
] |
| 387 |
|
| 388 |
sig = Signal( |
| 389 |
np.rec.fromarrays(samples, dtype=np.dtype(types)), |
| 390 |
t, |
| 391 |
name='Channel_lookup_with_axis', |
| 392 |
unit='A', |
| 393 |
comment='Array channel with axis', |
| 394 |
) |
| 395 |
sigs.append(sig) |
| 396 |
|
| 397 |
# lookup tabel with default axis |
| 398 |
samples = [ |
| 399 |
np.ones((cycles, 2, 3), dtype=np.uint64) * 4, |
| 400 |
] |
| 401 |
|
| 402 |
types = [ |
| 403 |
('Channel_lookup_with_default_axis', '(2, 3)<u8'), |
| 404 |
] |
| 405 |
|
| 406 |
sig = Signal( |
| 407 |
np.rec.fromarrays(samples, dtype=np.dtype(types)), |
| 408 |
t, |
| 409 |
name='Channel_lookup_with_default_axis', |
| 410 |
unit='mA', |
| 411 |
comment='Array channel with default axis', |
| 412 |
) |
| 413 |
sigs.append(sig) |
| 414 |
|
| 415 |
# structure channel composition |
| 416 |
samples = [ |
| 417 |
np.ones(cycles, dtype=np.uint8) * 10, |
| 418 |
np.ones(cycles, dtype=np.uint16) * 20, |
| 419 |
np.ones(cycles, dtype=np.uint32) * 30, |
| 420 |
np.ones(cycles, dtype=np.uint64) * 40, |
| 421 |
np.ones(cycles, dtype=np.int8) * -10, |
| 422 |
np.ones(cycles, dtype=np.int16) * -20, |
| 423 |
np.ones(cycles, dtype=np.int32) * -30, |
| 424 |
np.ones(cycles, dtype=np.int64) * -40, |
| 425 |
] |
| 426 |
|
| 427 |
types = [ |
| 428 |
('struct_channel_0', np.uint8), |
| 429 |
('struct_channel_1', np.uint16), |
| 430 |
('struct_channel_2', np.uint32), |
| 431 |
('struct_channel_3', np.uint64), |
| 432 |
('struct_channel_4', np.int8), |
| 433 |
('struct_channel_5', np.int16), |
| 434 |
('struct_channel_6', np.int32), |
| 435 |
('struct_channel_7', np.int64), |
| 436 |
] |
| 437 |
|
| 438 |
sig = Signal( |
| 439 |
np.rec.fromarrays(samples, dtype=np.dtype(types)), |
| 440 |
t, |
| 441 |
name='Channel_structure_composition', |
| 442 |
comment='Structure channel composition', |
| 443 |
) |
| 444 |
sigs.append(sig) |
| 445 |
|
| 446 |
|
| 447 |
# nested structures |
| 448 |
l4_arr = [ |
| 449 |
np.ones(cycles, dtype=np.float64) * 41, |
| 450 |
np.ones(cycles, dtype=np.float64) * 42, |
| 451 |
np.ones(cycles, dtype=np.float64) * 43, |
| 452 |
np.ones(cycles, dtype=np.float64) * 44, |
| 453 |
] |
| 454 |
|
| 455 |
types = [ |
| 456 |
('level41', np.float64), |
| 457 |
('level42', np.float64), |
| 458 |
('level43', np.float64), |
| 459 |
('level44', np.float64), |
| 460 |
] |
| 461 |
|
| 462 |
l4_arr = np.rec.fromarrays(l4_arr, dtype=types) |
| 463 |
|
| 464 |
l3_arr = [ |
| 465 |
l4_arr, |
| 466 |
l4_arr, |
| 467 |
l4_arr, |
| 468 |
] |
| 469 |
|
| 470 |
types = [ |
| 471 |
('level31', l4_arr.dtype), |
| 472 |
('level32', l4_arr.dtype), |
| 473 |
('level33', l4_arr.dtype), |
| 474 |
] |
| 475 |
|
| 476 |
l3_arr = np.rec.fromarrays(l3_arr, dtype=types) |
| 477 |
|
| 478 |
|
| 479 |
l2_arr = [ |
| 480 |
l3_arr, |
| 481 |
l3_arr, |
| 482 |
] |
| 483 |
|
| 484 |
types = [ |
| 485 |
('level21', l3_arr.dtype), |
| 486 |
('level22', l3_arr.dtype), |
| 487 |
] |
| 488 |
|
| 489 |
l2_arr = np.rec.fromarrays(l2_arr, dtype=types) |
| 490 |
|
| 491 |
|
| 492 |
l1_arr = [ |
| 493 |
l2_arr, |
| 494 |
] |
| 495 |
|
| 496 |
types = [ |
| 497 |
('level11', l2_arr.dtype), |
| 498 |
] |
| 499 |
|
| 500 |
l1_arr = np.rec.fromarrays(l1_arr, dtype=types) |
| 501 |
|
| 502 |
|
| 503 |
sigs.append( |
| 504 |
Signal( |
| 505 |
l1_arr, |
| 506 |
t, |
| 507 |
name='Nested_structures', |
| 508 |
) |
| 509 |
) |
| 510 |
|
| 511 |
mdf.append(sigs, comment='arrays', common_timebase=True) |
| 512 |
|
| 513 |
mdf.save('demo.mf4', overwrite=True) |
| 514 |
|
| 515 |
|
| 516 |
|