| 1 |
from pathlib import Path |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
import numpy.typing as npt |
| 5 |
from typing_extensions import Any |
| 6 |
|
| 7 |
from asammdf import MDF, Signal, SUPPORTED_VERSIONS |
| 8 |
import asammdf.blocks.v2_v3_blocks as v3b |
| 9 |
import asammdf.blocks.v2_v3_constants as v3c |
| 10 |
import asammdf.blocks.v4_blocks as v4b |
| 11 |
import asammdf.blocks.v4_constants as v4c |
| 12 |
|
| 13 |
SUPPORTED_VERSIONS = SUPPORTED_VERSIONS[1:] # type: ignore[misc] |
| 14 |
|
| 15 |
cycles = 500 |
| 16 |
channels_count = 20 |
| 17 |
array_channels_count = 20 |
| 18 |
|
| 19 |
|
| 20 |
def get_test_data(filename: str = "") -> Path: |
| 21 |
""" |
| 22 |
Utility functions needed by all test scripts. |
| 23 |
""" |
| 24 |
return Path(__file__).resolve().parent.joinpath("/data/", filename) |
| 25 |
|
| 26 |
|
| 27 |
def generate_test_file(tmpdir: str, version: str = "4.10") -> Path | None: |
| 28 |
mdf = MDF(version=version) |
| 29 |
|
| 30 |
if version <= "3.30": |
| 31 |
filename = Path(tmpdir) / f"big_test_{version}.mdf" |
| 32 |
else: |
| 33 |
filename = Path(tmpdir) / f"big_test_{version}.mf4" |
| 34 |
|
| 35 |
if filename.exists(): |
| 36 |
return filename |
| 37 |
|
| 38 |
t = np.arange(cycles, dtype=np.float64) |
| 39 |
|
| 40 |
cls = v4b.ChannelConversion if version >= "4.00" else v3b.ChannelConversion |
| 41 |
|
| 42 |
# no conversion |
| 43 |
sigs = [] |
| 44 |
for i in range(channels_count): |
| 45 |
sig = Signal( |
| 46 |
np.ones(cycles, dtype=np.uint64) * i, |
| 47 |
t, |
| 48 |
name=f"Channel_{i}", |
| 49 |
unit=f"unit_{i}", |
| 50 |
conversion=None, |
| 51 |
comment=f"Unsigned int 16bit channel {i}", |
| 52 |
) |
| 53 |
sigs.append(sig) |
| 54 |
mdf.append(sigs, common_timebase=True) |
| 55 |
|
| 56 |
# linear |
| 57 |
sigs = [] |
| 58 |
for i in range(channels_count): |
| 59 |
conversion: dict[str, Any] = { |
| 60 |
"conversion_type": (v4c.CONVERSION_TYPE_LIN if version >= "4.00" else v3c.CONVERSION_TYPE_LINEAR), |
| 61 |
"a": float(i), |
| 62 |
"b": -0.5, |
| 63 |
} |
| 64 |
sig = Signal( |
| 65 |
np.ones(cycles, dtype=np.int64), |
| 66 |
t, |
| 67 |
name=f"Channel_{i}", |
| 68 |
unit=f"unit_{i}", |
| 69 |
conversion=cls(**conversion), |
| 70 |
comment=f"Signed 16bit channel {i} with linear conversion", |
| 71 |
) |
| 72 |
sigs.append(sig) |
| 73 |
mdf.append(sigs, common_timebase=True) |
| 74 |
|
| 75 |
# algebraic |
| 76 |
sigs = [] |
| 77 |
for i in range(channels_count): |
| 78 |
conversion = { |
| 79 |
"conversion_type": (v4c.CONVERSION_TYPE_ALG if version >= "4.00" else v3c.CONVERSION_TYPE_FORMULA), |
| 80 |
"formula": f"{i} * sin(X)", |
| 81 |
} |
| 82 |
sig = Signal( |
| 83 |
np.arange(cycles, dtype=np.int32) / 100.0, |
| 84 |
t, |
| 85 |
name=f"Channel_{i}", |
| 86 |
unit=f"unit_{i}", |
| 87 |
conversion=cls(**conversion), |
| 88 |
comment=f"Sinus channel {i} with algebraic conversion", |
| 89 |
) |
| 90 |
sigs.append(sig) |
| 91 |
mdf.append(sigs, common_timebase=True) |
| 92 |
|
| 93 |
# rational |
| 94 |
sigs = [] |
| 95 |
for i in range(channels_count): |
| 96 |
conversion = { |
| 97 |
"conversion_type": (v4c.CONVERSION_TYPE_RAT if version >= "4.00" else v3c.CONVERSION_TYPE_RAT), |
| 98 |
"P1": 0, |
| 99 |
"P2": i, |
| 100 |
"P3": -0.5, |
| 101 |
"P4": 0, |
| 102 |
"P5": 0, |
| 103 |
"P6": 1, |
| 104 |
} |
| 105 |
sig = Signal( |
| 106 |
np.ones(cycles, dtype=np.int64), |
| 107 |
t, |
| 108 |
name=f"Channel_{i}", |
| 109 |
unit=f"unit_{i}", |
| 110 |
conversion=cls(**conversion), |
| 111 |
comment=f"Channel {i} with rational conversion", |
| 112 |
) |
| 113 |
sigs.append(sig) |
| 114 |
mdf.append(sigs, common_timebase=True) |
| 115 |
|
| 116 |
# string |
| 117 |
sigs = [] |
| 118 |
encoding = "latin-1" if version < "4.00" else "utf-8" |
| 119 |
for i in range(channels_count): |
| 120 |
strings = [f"Channel {i} sample {j}".encode(encoding) for j in range(cycles)] |
| 121 |
sig = Signal( |
| 122 |
np.array(strings), |
| 123 |
t, |
| 124 |
name=f"Channel_{i}", |
| 125 |
unit=f"unit_{i}", |
| 126 |
comment=f"String channel {i}", |
| 127 |
encoding=encoding, |
| 128 |
) |
| 129 |
sigs.append(sig) |
| 130 |
mdf.append(sigs, common_timebase=True) |
| 131 |
|
| 132 |
# byte array |
| 133 |
sigs = [] |
| 134 |
ones = np.ones(cycles, dtype=np.dtype("(8,)u1")) |
| 135 |
for i in range(channels_count): |
| 136 |
sig = Signal( |
| 137 |
ones * i, |
| 138 |
t, |
| 139 |
name=f"Channel_{i}", |
| 140 |
unit=f"unit_{i}", |
| 141 |
comment=f"Byte array channel {i}", |
| 142 |
) |
| 143 |
sigs.append(sig) |
| 144 |
mdf.append(sigs, common_timebase=True) |
| 145 |
|
| 146 |
# value to text |
| 147 |
sigs = [] |
| 148 |
ones = np.ones(cycles, dtype=np.uint64) |
| 149 |
conversion = { |
| 150 |
"raw": np.arange(255, dtype=np.float64), |
| 151 |
"phys": np.array([f"Value {i}".encode("ascii") for i in range(255)]), |
| 152 |
"conversion_type": (v4c.CONVERSION_TYPE_TABX if version >= "4.00" else v3c.CONVERSION_TYPE_TABX), |
| 153 |
"links_nr": 260, |
| 154 |
"ref_param_nr": 255, |
| 155 |
} |
| 156 |
|
| 157 |
for i in range(255): |
| 158 |
conversion[f"val_{i}"] = conversion[f"param_val_{i}"] = conversion["raw"][i] |
| 159 |
conversion[f"text_{i}"] = conversion["phys"][i] |
| 160 |
conversion[f"text_{255}"] = "Default" |
| 161 |
|
| 162 |
for i in range(channels_count): |
| 163 |
sig = Signal( |
| 164 |
ones * i, |
| 165 |
t, |
| 166 |
name=f"Channel_{i}", |
| 167 |
unit=f"unit_{i}", |
| 168 |
comment=f"Value to text channel {i}", |
| 169 |
conversion=cls(**conversion), |
| 170 |
) |
| 171 |
sigs.append(sig) |
| 172 |
mdf.append(sigs, common_timebase=True) |
| 173 |
|
| 174 |
name = mdf.save(filename, overwrite=True) |
| 175 |
mdf.close() |
| 176 |
|
| 177 |
return None |
| 178 |
|
| 179 |
|
| 180 |
def generate_arrays_test_file(tmpdir: str) -> Path | None: |
| 181 |
version = "4.10" |
| 182 |
mdf = MDF(version=version) |
| 183 |
filename = Path(tmpdir) / f"arrays_test_{version}.mf4" |
| 184 |
|
| 185 |
if filename.exists(): |
| 186 |
return filename |
| 187 |
|
| 188 |
t = np.arange(cycles, dtype=np.float64) |
| 189 |
|
| 190 |
# lookup tabel with axis |
| 191 |
sigs = [] |
| 192 |
for i in range(array_channels_count): |
| 193 |
samples: list[npt.NDArray[Any]] = [ |
| 194 |
np.ones((cycles, 2, 3), dtype=np.uint64) * i, |
| 195 |
np.ones((cycles, 2), dtype=np.uint64) * i, |
| 196 |
np.ones((cycles, 3), dtype=np.uint64) * i, |
| 197 |
] |
| 198 |
|
| 199 |
types = [ |
| 200 |
( |
| 201 |
f"Channel_{i}", |
| 202 |
np.dtype( |
| 203 |
"u8", |
| 204 |
metadata={ |
| 205 |
"axes": [ |
| 206 |
{ |
| 207 |
"type": "REF_AXIS", |
| 208 |
"size": 3, |
| 209 |
"conversion": None, |
| 210 |
"inverse_layout": False, |
| 211 |
"byte_offset_base": 8, |
| 212 |
"ref": (0, 0), |
| 213 |
"ref_name": f"channel_{i}_X_axis", |
| 214 |
"dtype_field_name": f"channel_{i}_X_axis", |
| 215 |
}, |
| 216 |
{ |
| 217 |
"type": "REF_AXIS", |
| 218 |
"size": 2, |
| 219 |
"conversion": None, |
| 220 |
"inverse_layout": False, |
| 221 |
"byte_offset_base": 8 * 3, |
| 222 |
"ref": (0, 0), |
| 223 |
"ref_name": f"channel_{i}_Y_axis", |
| 224 |
"dtype_field_name": f"channel_{i}_Y_axis", |
| 225 |
}, |
| 226 |
] |
| 227 |
}, |
| 228 |
), |
| 229 |
(2, 3), |
| 230 |
), |
| 231 |
( |
| 232 |
f"channel_{i}_Y_axis", |
| 233 |
np.dtype( |
| 234 |
"u8", |
| 235 |
metadata={ |
| 236 |
"axes": [ |
| 237 |
{ |
| 238 |
"type": "SCALE_AXIS", |
| 239 |
"size": 2, |
| 240 |
"conversion": None, |
| 241 |
"inverse_layout": False, |
| 242 |
"byte_offset_base": 8, |
| 243 |
"dtype_field_name": "", |
| 244 |
} |
| 245 |
] |
| 246 |
}, |
| 247 |
), |
| 248 |
(2,), |
| 249 |
), |
| 250 |
( |
| 251 |
f"channel_{i}_X_axis", |
| 252 |
np.dtype( |
| 253 |
"u8", |
| 254 |
metadata={ |
| 255 |
"axes": [ |
| 256 |
{ |
| 257 |
"type": "SCALE_AXIS", |
| 258 |
"size": 3, |
| 259 |
"conversion": None, |
| 260 |
"inverse_layout": False, |
| 261 |
"byte_offset_base": 8, |
| 262 |
"dtype_field_name": "", |
| 263 |
} |
| 264 |
] |
| 265 |
}, |
| 266 |
), |
| 267 |
(3,), |
| 268 |
), |
| 269 |
] |
| 270 |
|
| 271 |
sig = Signal( |
| 272 |
np.rec.fromarrays(samples, dtype=np.dtype(types)), |
| 273 |
t, |
| 274 |
name=f"Channel_{i}", |
| 275 |
unit=f"unit_{i}", |
| 276 |
conversion=None, |
| 277 |
comment=f"Array channel {i}", |
| 278 |
) |
| 279 |
sigs.append(sig) |
| 280 |
mdf.append(sigs, common_timebase=True) |
| 281 |
|
| 282 |
# lookup tabel with default axis |
| 283 |
sigs = [] |
| 284 |
for i in range(array_channels_count): |
| 285 |
samples = [np.ones((cycles, 2, 3), dtype=np.uint64) * i] |
| 286 |
|
| 287 |
types = [ |
| 288 |
( |
| 289 |
f"Channel_{i}", |
| 290 |
np.dtype( |
| 291 |
"u8", |
| 292 |
metadata={ |
| 293 |
"axes": [ |
| 294 |
{ |
| 295 |
"type": "NO_AXIS", |
| 296 |
"size": 3, |
| 297 |
"conversion": None, |
| 298 |
"inverse_layout": False, |
| 299 |
"byte_offset_base": 8, |
| 300 |
"dtype_field_name": "", |
| 301 |
}, |
| 302 |
{ |
| 303 |
"type": "NO_AXIS", |
| 304 |
"size": 2, |
| 305 |
"conversion": None, |
| 306 |
"inverse_layout": False, |
| 307 |
"byte_offset_base": 8 * 3, |
| 308 |
"dtype_field_name": "", |
| 309 |
}, |
| 310 |
] |
| 311 |
}, |
| 312 |
), |
| 313 |
(2, 3), |
| 314 |
) |
| 315 |
] |
| 316 |
|
| 317 |
sig = Signal( |
| 318 |
np.rec.fromarrays(samples, dtype=np.dtype(types)), |
| 319 |
t, |
| 320 |
name=f"Channel_{i}", |
| 321 |
unit=f"unit_{i}", |
| 322 |
conversion=None, |
| 323 |
comment=f"Array channel {i} with default axis", |
| 324 |
) |
| 325 |
sigs.append(sig) |
| 326 |
mdf.append(sigs, common_timebase=True) |
| 327 |
|
| 328 |
# structure channel composition |
| 329 |
sigs = [] |
| 330 |
for i in range(array_channels_count): |
| 331 |
samples = [ |
| 332 |
np.ones(cycles, dtype=np.uint8) * i, |
| 333 |
np.ones(cycles, dtype=np.uint16) * i, |
| 334 |
np.ones(cycles, dtype=np.uint32) * i, |
| 335 |
np.ones(cycles, dtype=np.uint64) * i, |
| 336 |
np.ones(cycles, dtype=np.int8) * i, |
| 337 |
np.ones(cycles, dtype=np.int16) * i, |
| 338 |
np.ones(cycles, dtype=np.int32) * i, |
| 339 |
np.ones(cycles, dtype=np.int64) * i, |
| 340 |
] |
| 341 |
|
| 342 |
types = [ |
| 343 |
(f"struct_{i}_channel_0", np.uint8), |
| 344 |
(f"struct_{i}_channel_1", np.uint16), |
| 345 |
(f"struct_{i}_channel_2", np.uint32), |
| 346 |
(f"struct_{i}_channel_3", np.uint64), |
| 347 |
(f"struct_{i}_channel_4", np.int8), |
| 348 |
(f"struct_{i}_channel_5", np.int16), |
| 349 |
(f"struct_{i}_channel_6", np.int32), |
| 350 |
(f"struct_{i}_channel_7", np.int64), |
| 351 |
] |
| 352 |
|
| 353 |
sig = Signal( |
| 354 |
np.rec.fromarrays(samples, dtype=np.dtype(types)), |
| 355 |
t, |
| 356 |
name=f"Channel_{i}", |
| 357 |
unit=f"unit_{i}", |
| 358 |
conversion=None, |
| 359 |
comment=f"Structure channel composition {i}", |
| 360 |
) |
| 361 |
sigs.append(sig) |
| 362 |
|
| 363 |
mdf.append(sigs, common_timebase=True) |
| 364 |
|
| 365 |
name = mdf.save(filename, overwrite=True) |
| 366 |
|
| 367 |
mdf.close() |
| 368 |
|
| 369 |
return None |
| 370 |
|
| 371 |
|
| 372 |
if __name__ == "__main__": |
| 373 |
# generate_test_file("3.30") |
| 374 |
# generate_test_file("4.10") |
| 375 |
generate_arrays_test_file(r"D:\TMP") |