-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFaultRecovery.py
136 lines (88 loc) · 4.82 KB
/
FaultRecovery.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
"""
CityPulse fault recovery component
Created on Thu Jul 30 08:49:16 2015
@author: [email protected] [email protected]
"""
import numpy as np
from sklearn.neighbors import KNeighborsRegressor
class FaultRecovery:
#default constructor
def __init__(self):
#configuration params of the component
self.SAMPLE_SIZE = 5
self.NUMBER_OF_NEIGHBORS = 5
self.REFERENCE_DATASET_MAXIMUM_SIZE = 100;
#the reference dataset params
self.referenceDatasetX = np.empty([0,self.SAMPLE_SIZE-1])
self.referenceDatasetY = np.empty([1,0])
self.referenceDatasetNumberOfRows = 0
#sample is a buffer vector where are kept the last SAMPLE_SIZE reported measurements
self.sample = np.empty(self.SAMPLE_SIZE)*np.NaN
#the prediction model
self.model = KNeighborsRegressor(n_neighbors=self.NUMBER_OF_NEIGHBORS, weights='distance', p=2, metric='minkowski')
#value keeping the previous reported measurement
self.previousMeasurement = None
def addValidMeasurement(self,measurement):
"""
method used for reporting a new valid measurement
Parameters
----------
measurement: an integer of double value representig the latest measurement
"""
#valid only for the first time when there is no previous measurement
if self.previousMeasurement is None:
self.previousMeasurement = measurement
#the model keeps track of the measurements variation (differences from onea measurement to the other) not the actual values
valueToAdd = measurement - self.previousMeasurement
#add the new value in the sample
for k in range(self.SAMPLE_SIZE-1):
self.sample[k] = self.sample[k+1]
self.sample[self.SAMPLE_SIZE-1] = valueToAdd
#check if the current sample is potentialy valid to be added to the reference dataset (sample does not contain missing values)
if (sum(np.isnan(self.sample)) == 0):
self.referenceDatasetNumberOfRows = self.referenceDatasetNumberOfRows + 1
self.referenceDatasetX = np.vstack((self.referenceDatasetX,self.sample[0:-1]))
self.referenceDatasetY = np.append(self.referenceDatasetY,self.sample[-1])
if(self.referenceDatasetNumberOfRows > self.REFERENCE_DATASET_MAXIMUM_SIZE):
self.referenceDatasetX = np.delete( self.referenceDatasetX, 0, 0)
self.referenceDatasetY = np.delete( self.referenceDatasetY, 0, 0)
self.referenceDatasetNumberOfRows = self.referenceDatasetNumberOfRows - 1
def reportInvalidMeasurement(self):
"""
method used for reporting an invalid measurement
"""
#it adds an estimation to the sample of each and every invalid report if it is posible (the prediction model works) otherwhise np.nan
pred = self.getEstimation()
for k in range(self.SAMPLE_SIZE-1):
self.sample[k] = self.sample[k+1]
if (pred is None):
self.sample[self.SAMPLE_SIZE-1] = np.nan
else:
self.sample[self.SAMPLE_SIZE-1] = pred
def getEstimation(self):
"""
method used for optaining the estimations
Returns a double value representing the estimation
"""
if((self.referenceDatasetNumberOfRows>=self.NUMBER_OF_NEIGHBORS) & (sum(np.isnan(self.sample)) == 0)):
self.model.fit(self.referenceDatasetX,self.referenceDatasetY)
pred = self.model.predict(self.sample[0:-1]) + self.previousMeasurement
return pred
return None
def isReady(self):
"""
checks if the reference data set contains enough entries intorde to generate a prediction
Returns a boolean value
"""
return (self.referenceDatasetNumberOfRows >= self.NUMBER_OF_NEIGHBORS)
def loadTrainingSet(self, reference_X,reference_Y):
"""
this method can be used to load reference data set if needed.
"""
self.referenceDatasetX = reference_X
self.referenceDatasetY = reference_Y
self.referenceDatasetNumberOfRows = len(self.referenceDatasetY)
x_train = self.referenceDatasetX
y_train = self.referenceDatasetY
self.model.fit(x_train,y_train )