-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_cleaning.py
97 lines (65 loc) · 2.72 KB
/
data_cleaning.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
87
88
89
90
91
92
93
94
95
import numpy as np
import pandas as pd
def get_first_n_periods(x, k):
"""
Returns the first n periods of a worm's data.
Parameters:
x : list of numpy.Array
N x C x D matrix containing N worms, C time frames, and D features values
k : int
The number of periods to return
Returns:
x: numpy.Array
N x k x 900 x D numpy array representing the xy coordinates of the first k periods of each worm
"""
x_trimmed = [matrix[:900*k,[2,3]].reshape((-1,900,2)) for matrix in x]
x_trimmed = np.stack(x_trimmed, axis=0)
return x_trimmed
def remove_na(threshold_proportion, x, y):
"""
Removes worms with more than a certain proportion of missing data.
Parameters:
threshold_proportion : float
The proportion of missing data above which a worm is removed
x : numpy.Array
N x k x 900 x D numpy array representing the xy coordinates of the first k periods of each worm
y : numpy.Array
N x 1 array of labels
Returns:
x : numpy.Array
N_1 x k x 900 x D numpy array representing the xy coordinates of the first k periods of each worm
y : numpy.Array
N_1 x 1 array of labels
"""
total_size = x.shape[1]*x.shape[2]
mask = np.any(np.isnan(x).sum(axis=(1,2))/total_size < threshold_proportion,axis=1)
return x[mask], y[mask],mask
def remove_outliers(threshold, x, y):
"""
Removes worms with unreasonable lifespan values.
Parameters:
threshold : float
The threshold on the value in y below which a worm is removed
x : numpy.Array
N x k x 900 x D numpy array representing the xy coordinates of the first k periods of each worm
y : numpy.Array
N x 1 array of labels
Returns:
x : numpy.Array
N_1 x k x 900 x D numpy array representing the xy coordinates of the first k periods of each worm
y : numpy.Array
N_1 x 1 array of labels
"""
return x[y>=threshold], y[y>=threshold], y>=threshold
def fill_na_interpolation(x):
"""
Fills missing data using linear interpolation.
Parameters:
x : numpy.Array
N x k x 900 x D numpy array representing the xy coordinates of the first k periods of each worm
Returns:
x : numpy.Array
N x k x 900 x D numpy array representing the xy coordinates of the first k periods of each worm, with nan values filled by linear interpolation
"""
x_filled = np.array([np.apply_along_axis(lambda m: pd.Series(m).interpolate(method='linear').ffill().bfill().to_numpy(), axis=1, arr=matrix) for matrix in x])
return x_filled