-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_unpack_condition_args.py
executable file
·86 lines (73 loc) · 2.5 KB
/
test_unpack_condition_args.py
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
import numpy as np
def _unpack_condition_args(interval, x, peaks):
"""
Parse condition arguments for `find_peaks`.
Parameters
----------
interval : number or ndarray or sequence
Either a number or ndarray or a 2-element sequence of the former. The
first value is always interpreted as `imin` and the second, if supplied,
as `imax`.
x : ndarray
The signal with `peaks`.
peaks : ndarray
An array with indices used to reduce `imin` and / or `imax` if those are
arrays.
Returns
-------
imin, imax : number or ndarray or None
Minimal and maximal value in `argument`.
Raises
------
ValueError :
If interval border is given as array and its size does not match the size
of `x`.
Notes
-----
.. versionadded:: 1.1.0
"""
try:
imin, imax = interval
except (TypeError, ValueError):
imin, imax = (interval, None)
# Reduce arrays if arrays
if isinstance(imin, np.ndarray):
if imin.size != x.size:
raise ValueError('array size of lower interval border must match x')
imin = imin[peaks]
if isinstance(imax, np.ndarray):
if imax.size != x.size:
raise ValueError('array size of upper interval border must match x')
imax = imax[peaks]
return imin, imax
def read_lines_in_groups(filename, group_size=3):
try:
with open(filename, 'r') as file:
group = []
for line in file:
group.append(line.strip()) # Remove any extra whitespace/newlines
if len(group) == group_size:
yield group
group = [] # Reset the group for the next set of lines
# # Yield any remaining lines if they're fewer than the group size
# if group:
# yield group
except Exception as e:
print(f"Error reading file: {e}")
grs = read_lines_in_groups("input.txt")
# assume x is a number
for gr in grs:
interval = list(map(float, gr[0].split()))
x = list(map(float, gr[1].split()))
peaks = list(map(int, gr[2].split()))
if len(interval) > len(x):
interval = np.array(interval)
interval = interval.reshape((2, -1))
elif len(interval) == 1:
interval = interval[0]
out = _unpack_condition_args(interval, np.array(x), np.array(peaks))
if isinstance(out[0], np.ndarray):
print(out[0].tolist())
print(out[1].tolist())
else:
print(out)