-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
84 lines (72 loc) · 2.82 KB
/
utils.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
# Author: Mark Gee
# Platform: keras
# Utility functions for ILearner
from keras.callbacks import Callback
from pykalman import KalmanFilter
import numpy as np
from keras import backend as K
# Reset states callback for stateful LSTMs
class ResetStatesCallback(Callback):
def __init__(self, n_sequences):
self.counter = 0
self.n_sequences = n_sequences
def on_batch_begin(self, batch, logs={}):
if self.counter % self.n_sequences == 0:
self.model.reset_states()
self.counter += 1
# Moving average filter
def moving_average_filter(data, window):
N = window
cumsum, filtered_data = [0], []
for i, x in enumerate(data, 1):
cumsum.append(cumsum[i-1] + x)
if i>=N:
moving_ave = (cumsum[i] - cumsum[i-N])/N
filtered_data.append(moving_ave)
return np.array(filtered_data)
# Kalman filter
def kalman_filter(data, error):
data=np.ma.masked_less(data,-900)
Transition_Matrix=[[1,0,1,0],[0,1,0,1],[0,0,1,0],[0,0,0,1]]
Observation_Matrix=[[1,0,0,0],[0,1,0,0]]
xinit=data[0,0]
yinit=data[0,1]
vxinit=data[1,0]-data[0,0]
vyinit=data[1,1]-data[0,1]
initstate=[xinit,yinit,vxinit,vyinit]
initcovariance=error*np.eye(4)
transistionCov=error*np.eye(4)
observationCov=error*np.eye(2)
kf=KalmanFilter(transition_matrices=Transition_Matrix,
observation_matrices =Observation_Matrix,
initial_state_mean=initstate,
initial_state_covariance=initcovariance,
transition_covariance=transistionCov,
observation_covariance=observationCov)
(filtered_state_means, _) = kf.filter(data)
return np.array(filtered_state_means)
# F1 Metric
def f1(y_true, y_pred):
def recall(y_true, y_pred):
"""Recall metric.
Only computes a batch-wise average of recall.
Computes the recall, a metric for multi-label classification of
how many relevant items are selected.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def precision(y_true, y_pred):
"""Precision metric.
Only computes a batch-wise average of precision.
Computes the precision, a metric for multi-label classification of
how many selected items are relevant.
"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
precision = precision(y_true, y_pred)
recall = recall(y_true, y_pred)
return 2*((precision*recall)/(precision+recall+K.epsilon()))