Repository navigation
Expand file tree
/
Copy pathdummy_module.py
More file actions
677 lines (492 loc) · 15.7 KB
/
Copy pathdummy_module.py
File metadata and controls
677 lines (492 loc) · 15.7 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
from typing import TypedDict
import numpy as np
import qrcode
from scipy import ndimage, signal
class Calibration(TypedDict):
gain: float
offset: float
class MeasurementConfig(TypedDict):
calibration: Calibration
enabled: bool
label: str
class MeasurementResult(TypedDict):
success: bool
message: str
adjusted_value: float
class ArrayConfig(TypedDict):
values: np.ndarray[tuple[int, int], np.dtype[np.float64]]
factor: float
class ChannelLimits(TypedDict):
minimum: float
maximum: float
class ChannelConfig(TypedDict):
limits: ChannelLimits
name: str
class DeviceConfig(TypedDict):
channel: ChannelConfig
enabled: bool
class SensorCalibration(TypedDict):
gain: float
offset: float
class SensorConfig(TypedDict):
calibration: SensorCalibration
serial_number: str
class AcquisitionConfig(TypedDict):
sensor: SensorConfig
sample_rate: float
class ExperimentConfig(TypedDict):
acquisition: AcquisitionConfig
operator: str
def add_integers(a: int, b: int) -> int:
"""
Add two integers.
Args:
a: First integer
b: Second integer
Returns:
Sum of both integers
"""
return a + b
def divide_values(a: float, b: float) -> float:
"""
Divide two floating point numbers.
Args:
a: Numerator
b: Denominator
Returns:
Division result
"""
return a / b
def add_numpy_float32_values(a: np.float32, b: np.float32) -> np.float32:
"""
Add two NumPy 32-bit floating point scalar values.
Args:
a: First NumPy 32-bit floating point value
b: Second NumPy 32-bit floating point value
Returns:
Sum of both NumPy 32-bit floating point values
"""
return a + b
def increment_numpy_u8_value(value: np.uint8) -> np.uint8:
"""
Increment a NumPy unsigned 8-bit integer scalar value.
Args:
value: Input NumPy unsigned 8-bit integer value
Returns:
Incremented NumPy unsigned 8-bit integer value
"""
return value + 1
def invert_flag(flag: bool) -> bool:
"""
Invert a boolean value.
Args:
flag: Input boolean
Returns:
Inverted boolean
"""
return not flag
def create_greeting(name: str) -> str:
"""
Create a greeting string.
Args:
name: Person name
Returns:
Greeting message
"""
return f"Hello {name}"
def increment_u8_value(value: int) -> int:
"""
Increment an unsigned byte value.
Args:
value: Input unsigned byte value
Returns:
Incremented unsigned byte value
"""
return value + 1
def scale_float_list(values: list[float], factor: float) -> list[float]:
"""
Scale a list of floating point values.
Args:
values: Input float list
factor: Scale factor
Returns:
Scaled float list
"""
return [value * factor for value in values]
def increment_integer_list(values: list[int]) -> list[int]:
"""
Increment every integer in a list.
Args:
values: Input integer list
Returns:
Incremented integer list
"""
return [value + 1 for value in values]
def invert_boolean_list(values: list[bool]) -> list[bool]:
"""
Invert every boolean in a list.
Args:
values: Input boolean list
Returns:
Inverted boolean list
"""
return [not value for value in values]
def append_string_list(values: list[str], suffix: str) -> list[str]:
"""
Append text to every string in a list.
Args:
values: Input string list
suffix: Text to append
Returns:
String list with appended text
"""
return [value + suffix for value in values]
def increment_u8_list(values: list[int]) -> list[int]:
"""
Increment every unsigned byte value in a list.
Args:
values: Input unsigned byte value list
Returns:
Incremented unsigned byte value list
"""
return [value + 1 for value in values]
def summarize_measurement(
value: float,
valid: bool,
) -> tuple[float, bool]:
"""
Create a tuple result.
Args:
value: Measurement value
valid: Measurement validity
Returns:
Tuple containing value and validity
"""
return value, valid
def scale_numpy_array(
array_in: np.ndarray[tuple[int, int], np.dtype[np.float64]],
factor: float,
) -> np.ndarray[tuple[int, int], np.dtype[np.float64]]:
"""
Scale a NumPy floating point array by a factor.
Args:
array_in: Input NumPy floating point array
factor: Scale factor
Returns:
Scaled NumPy floating point array
"""
return array_in * factor
def scale_numpy_integer_array(
array_in: np.ndarray[tuple[int], np.dtype[np.int64]],
factor: int,
) -> np.ndarray[tuple[int], np.dtype[np.int64]]:
"""
Scale a NumPy integer array by a factor.
Args:
array_in: Input NumPy integer array
factor: Scale factor
Returns:
Scaled NumPy integer array
"""
return array_in * factor
def scale_numpy_int8_array(
array_in: np.ndarray[tuple[int], np.dtype[np.int8]],
factor: int,
) -> np.ndarray[tuple[int], np.dtype[np.int8]]:
"""
Scale a NumPy 8-bit integer array by a factor.
Args:
array_in: Input NumPy 8-bit integer array
factor: Scale factor
Returns:
Scaled NumPy 8-bit integer array
"""
return array_in * factor
def scale_numpy_int16_array(
array_in: np.ndarray[tuple[int], np.dtype[np.int16]],
factor: int,
) -> np.ndarray[tuple[int], np.dtype[np.int16]]:
"""
Scale a NumPy 16-bit integer array by a factor.
Args:
array_in: Input NumPy 16-bit integer array
factor: Scale factor
Returns:
Scaled NumPy 16-bit integer array
"""
return array_in * factor
def scale_numpy_int32_array(
array_in: np.ndarray[tuple[int], np.dtype[np.int32]],
factor: int,
) -> np.ndarray[tuple[int], np.dtype[np.int32]]:
"""
Scale a NumPy 32-bit integer array by a factor.
Args:
array_in: Input NumPy 32-bit integer array
factor: Scale factor
Returns:
Scaled NumPy 32-bit integer array
"""
return array_in * factor
def scale_numpy_uint8_array(
array_in: np.ndarray[tuple[int], np.dtype[np.uint8]],
factor: int,
) -> np.ndarray[tuple[int], np.dtype[np.uint8]]:
"""
Scale a NumPy unsigned 8-bit integer array by a factor.
Args:
array_in: Input NumPy unsigned 8-bit integer array
factor: Scale factor
Returns:
Scaled NumPy unsigned 8-bit integer array
"""
return array_in * factor
def scale_numpy_uint16_array(
array_in: np.ndarray[tuple[int], np.dtype[np.uint16]],
factor: int,
) -> np.ndarray[tuple[int], np.dtype[np.uint16]]:
"""
Scale a NumPy unsigned 16-bit integer array by a factor.
Args:
array_in: Input NumPy unsigned 16-bit integer array
factor: Scale factor
Returns:
Scaled NumPy unsigned 16-bit integer array
"""
return array_in * factor
def scale_numpy_uint32_array(
array_in: np.ndarray[tuple[int], np.dtype[np.uint32]],
factor: int,
) -> np.ndarray[tuple[int], np.dtype[np.uint32]]:
"""
Scale a NumPy unsigned 32-bit integer array by a factor.
Args:
array_in: Input NumPy unsigned 32-bit integer array
factor: Scale factor
Returns:
Scaled NumPy unsigned 32-bit integer array
"""
return array_in * factor
def scale_numpy_uint64_array(
array_in: np.ndarray[tuple[int], np.dtype[np.uint64]],
factor: int,
) -> np.ndarray[tuple[int], np.dtype[np.uint64]]:
"""
Scale a NumPy unsigned 64-bit integer array by a factor.
Args:
array_in: Input NumPy unsigned 64-bit integer array
factor: Scale factor
Returns:
Scaled NumPy unsigned 64-bit integer array
"""
return array_in * factor
def scale_numpy_float32_array(
array_in: np.ndarray[tuple[int], np.dtype[np.float32]],
factor: float,
) -> np.ndarray[tuple[int], np.dtype[np.float32]]:
"""
Scale a NumPy 32-bit floating point array by a factor.
Args:
array_in: Input NumPy 32-bit floating point array
factor: Scale factor
Returns:
Scaled NumPy 32-bit floating point array
"""
return array_in * factor
def invert_numpy_boolean_array(
array_in: np.ndarray[tuple[int, int, int], np.dtype[np.bool_]],
) -> np.ndarray[tuple[int, int, int], np.dtype[np.bool_]]:
"""
Invert a NumPy boolean array.
Args:
array_in: Input NumPy boolean array
Returns:
Inverted NumPy boolean array
"""
return np.logical_not(array_in)
def append_numpy_string_array(
array_in: np.ndarray[tuple[int], np.dtype[np.str_]],
suffix: str,
) -> np.ndarray[tuple[int], np.dtype[np.str_]]:
"""
Append text to each element in a NumPy string array.
Args:
array_in: Input NumPy string array
suffix: Text to append
Returns:
NumPy string array with appended text
"""
return np.char.add(array_in, suffix)
def increment_numpy_u8_array(
array_in: np.ndarray[tuple[int, int, int], np.dtype[np.uint8]],
) -> np.ndarray[tuple[int, int, int], np.dtype[np.uint8]]:
"""
Increment each element in a NumPy unsigned 8-bit integer array.
Args:
array_in: Input NumPy unsigned 8-bit integer array
Returns:
Incremented NumPy unsigned 8-bit integer array
"""
return array_in + 1
def process_measurement_config(config: MeasurementConfig) -> MeasurementResult:
"""
Process a nested measurement configuration dictionary.
Args:
config: Measurement configuration
Returns:
Measurement result dictionary
"""
calibration = config["calibration"]
adjusted_value = calibration["gain"] + calibration["offset"]
success = config["enabled"] and adjusted_value > 0
return {
"success": success,
"message": config["label"],
"adjusted_value": adjusted_value,
}
def process_measurement_configs(
configs: list[MeasurementConfig],
) -> list[MeasurementResult]:
"""
Process a list of nested measurement configuration dictionaries.
Args:
configs: Measurement configuration dictionaries
Returns:
Measurement result dictionaries
"""
return [process_measurement_config(config) for config in configs]
def scale_array_from_config(config: ArrayConfig) -> ArrayConfig:
"""
Scale an array stored in a dictionary.
Args:
config: Array configuration dictionary
Returns:
Array configuration dictionary with scaled values
"""
return {
"values": config["values"] * config["factor"],
"factor": config["factor"],
}
def process_device_config(config: DeviceConfig) -> MeasurementResult:
"""
Process a device configuration with two nested dictionary levels.
Args:
config: Device configuration
Returns:
Measurement result dictionary
"""
limits = config["channel"]["limits"]
valid_limits = limits["minimum"] < limits["maximum"]
return {
"success": config["enabled"] and valid_limits,
"message": config["channel"]["name"],
"adjusted_value": limits["maximum"] - limits["minimum"],
}
def process_experiment_config(config: ExperimentConfig) -> MeasurementResult:
"""
Process an experiment configuration with three nested dictionary levels.
Args:
config: Experiment configuration
Returns:
Measurement result dictionary
"""
calibration = config["acquisition"]["sensor"]["calibration"]
adjusted_value = calibration["gain"] + calibration["offset"]
return {
"success": config["acquisition"]["sample_rate"] > 0,
"message": config["operator"],
"adjusted_value": adjusted_value,
}
def mixed_example(
count: int,
threshold: float,
enabled: bool,
label: str,
values: list[float],
) -> tuple[bool, str]:
"""
Complex example function using multiple supported types.
Args:
count: Number of iterations
threshold: Threshold value
enabled: Enable flag
label: Text label
values: Numeric values
Returns:
Tuple containing status and message
"""
total = sum(values)
success = enabled and total > threshold and count > 0
message = f"Label={label}, Total={total}"
return success, message
def smooth_detrended_signal(
values: np.ndarray[tuple[int], np.dtype[np.float64]],
window_length: int,
polynomial_order: int,
) -> np.ndarray[tuple[int], np.dtype[np.float64]]:
"""Remove a linear trend, then smooth a one-dimensional float signal."""
detrended = signal.detrend(values)
return signal.savgol_filter(detrended, window_length, polynomial_order)
def replace_integer_spikes(
values: np.ndarray[tuple[int], np.dtype[np.int64]],
window_size: int,
threshold: int,
) -> np.ndarray[tuple[int], np.dtype[np.int64]]:
"""Replace one-dimensional integer outliers with their local median."""
baseline = ndimage.median_filter(values, size=window_size)
return np.where(np.abs(values - baseline) > threshold, baseline, values)
def sharpen_float_grid(
values: np.ndarray[tuple[int, int], np.dtype[np.float64]],
sigma: float,
strength: float,
) -> np.ndarray[tuple[int, int], np.dtype[np.float64]]:
"""Smooth a two-dimensional float grid, then enhance its edges."""
smoothed = ndimage.gaussian_filter(values, sigma=sigma)
edges = ndimage.laplace(smoothed)
return smoothed - strength * edges
def close_integer_grid(
values: np.ndarray[tuple[int, int], np.dtype[np.int64]],
window_size: int,
) -> np.ndarray[tuple[int, int], np.dtype[np.int64]]:
"""Fill small low-valued gaps in a two-dimensional integer grid."""
expanded = ndimage.maximum_filter(values, size=window_size)
return ndimage.minimum_filter(expanded, size=window_size)
def create_url_qr_code(
url: str,
) -> np.ndarray[tuple[int, int], np.dtype[np.uint8]]:
"""Encode a URL as a 2D array of QR modules (1 for dark, 0 for light)."""
qr = qrcode.QRCode(border=4)
qr.add_data(url)
qr.make(fit=True)
return np.array(qr.get_matrix(), dtype=np.uint8)
# Scalar coverage for every supported NumPy data type.
def invert_numpy_boolean_value(value: np.bool_) -> np.bool_:
"""Invert a NumPy boolean scalar."""
return np.bool_(not value)
def increment_numpy_int8_value(value: np.int8) -> np.int8:
"""Increment a NumPy signed 8-bit integer scalar."""
return np.int8(value + 1)
def increment_numpy_int16_value(value: np.int16) -> np.int16:
"""Increment a NumPy signed 16-bit integer scalar."""
return np.int16(value + 1)
def increment_numpy_int32_value(value: np.int32) -> np.int32:
"""Increment a NumPy signed 32-bit integer scalar."""
return np.int32(value + 1)
def increment_numpy_int64_value(value: np.int64) -> np.int64:
"""Increment a NumPy signed 64-bit integer scalar."""
return np.int64(value + 1)
def increment_numpy_uint16_value(value: np.uint16) -> np.uint16:
"""Increment a NumPy unsigned 16-bit integer scalar."""
return np.uint16(value + 1)
def increment_numpy_uint32_value(value: np.uint32) -> np.uint32:
"""Increment a NumPy unsigned 32-bit integer scalar."""
return np.uint32(value + 1)
def increment_numpy_uint64_value(value: np.uint64) -> np.uint64:
"""Increment a NumPy unsigned 64-bit integer scalar."""
return np.uint64(value + 1)
def increment_numpy_float64_value(value: np.float64) -> np.float64:
"""Increment a NumPy 64-bit floating-point scalar."""
return np.float64(value + 1)
def append_numpy_string_value(value: np.str_) -> np.str_:
"""Append text to a NumPy string scalar."""
return np.str_(value + "_processed")