-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_process.py
More file actions
39 lines (33 loc) · 1.36 KB
/
data_process.py
File metadata and controls
39 lines (33 loc) · 1.36 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
import numpy as np
def find_nearest(array, value, func=np.real, getValue=False):
idx = (np.abs(func(array) - func(value))).argmin()
if getValue:
return array[idx]
else: return idx
def closest_argmin(A, B):
"""
stolen from https://stackoverflow.com/questions/45349561/find-nearest-indices-for-one-array-against-all-values-in-another-array-python
"""
sidx_B = B.argsort()
sorted_B = B[sidx_B]
sorted_idx = np.searchsorted(sorted_B, A)
sorted_idx[sorted_idx == B.size] = B.size - 1
mask = (sorted_idx > 0) & ((np.abs(A - sorted_B[sorted_idx - 1]) < np.abs(A - sorted_B[sorted_idx])) )
return sidx_B[sorted_idx-mask]
def get_regex_find_digits():
return '([-+]?[0-9]+[.]?[0-9]*([eE][-+]?[0-9]+)?)'
def reduceComplexArrayBounds(array, realLimits, imagLimits):
slc = np.logical_and.reduce(
(
np.real(array) < realLimits[1],
np.real(array) > realLimits[0],
np.imag(array) < imagLimits[1],
np.imag(array) > imagLimits[0]
)
)
return array[slc], slc
def list_of_lists_to_array(liste, pad_value=None, dtype=float):
"""
https://stackoverflow.com/questions/43146266/convert-list-of-lists-with-different-lengths-to-a-numpy-array
"""
return np.array([[*i, *[pad_value]*(len(max(liste, key=len)) - len(i))] for i in list(liste)], dtype=dtype)