Repository navigation
Expand file tree
/
Copy pathpreprocessing.py
More file actions
82 lines (63 loc) · 2.02 KB
/
Copy pathpreprocessing.py
File metadata and controls
82 lines (63 loc) · 2.02 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
import numpy as np
#%%
def nan_helper(y):
"""Helper to handle indices and logical indices of NaNs.
Input:
- y, 1d numpy array with possible NaNs
Output:
- nans, logical indices of NaNs
- index, a function, with signature indices= index(logical_indices),
to convert logical indices of NaNs to 'equivalent' indices
Example:
>>> # linear interpolation of NaNs
>>> nans, x= nan_helper(y)
>>> y[nans]= np.interp(x(nans), x(~nans), y[~nans])
"""
return np.isnan(y), lambda z: z.nonzero()[0]
#%%
def interpolate_nans(t, y, gap_dur=np.inf):
''' Replaces nans with interpolated values if the 'island' or nans/or gap
is shorter than 'gap_dur'
Args:
y - 1d numpy array,
gap_dur - duration of gap in ms
Returns:
y - interpolated array
'''
# Find index for nans where gaps are longer than 'gap_dur' samples
d = np.isnan(y)
# If there are no nans, return
if not np.any(d):
return y
# Find onsets and offsets of gaps
d = np.diff(np.concatenate((np.array([0]), d*1, np.array([0]))))
onsets = np.where(d==1)[0]
offsets = np.where(d==-1)[0]
# Decrease offsets come too late by -1
if np.any(offsets >= len(y)):
idx = np.where(offsets >= len(y))[0][0]
offsets[idx] = offsets[idx] - 1
dur = t[offsets] - t[onsets]
# If the gaps are longer than 'gaps', replace temporarily with other values
for i, on in enumerate(onsets):
if dur[i] > gap_dur:
y[onsets[i]:offsets[i]] = -1000
nans, x = nan_helper(y)
y[nans]= np.interp(x(nans), x(~nans), y[~nans])
# put nans back
y[y == -1000] = np.nan
return y
#%%
def nearest_odd_integer(x):
"""Returns nearest odd integer of input value.
Input:
- x, float or integer
Output:
- integer value that is odd
Example:
>>> nearest_odd_integer(6.25)
7
>>> nearest_odd_integer(5.99)
5
"""
return int(2*np.floor(x/2)+1)