-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSFA.py
74 lines (58 loc) · 2.58 KB
/
SFA.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
import matplotlib.pyplot as plt
from utils import *
import warnings
warnings.filterwarnings("ignore")
plt.rcParams['font.sans-serif']=['simhei']
plt.rcParams['axes.unicode_minus']=False
if __name__ == "__main__":
"""
.dat file is the input data
data_train is the training data under normal condition
data_test_normal is the test data under normal condition
data_test_fault is the test data of the fault conditions
"""
# input data can be changed by yourself
data_train = np.load('./data/data_train.npy')
data_test_normal = np.load('./data/data_test_normal.npy')
data_test_fault = np.load('./data/data_test_fault.npy')
# Model Initialization
model = SFA()
# Model training
model.fit(data_original=data_train)
# test data Transformation
feature_normal_train, _, _ = model.transform(data_train)
feature_normal, values_normal, vector_normal = model.transform(data_test_normal)
feature_fault, values_fault, vector_fault = model.transform(data_test_fault)
# Difference of test data
diff_normal = np.diff(data_test_normal,axis=0)
diff_fault = np.diff(data_test_fault, axis=0)
diff_normal = np.array(diff_normal)
diff_fault = np.array(diff_fault)
# Statistics of test data
Te_train = np.diag(np.dot(feature_normal_train[:, :6], feature_normal_train[:, :6].transpose()))
Td_train = np.diag(np.dot(feature_normal_train[:, 6:], feature_normal_train[:, 6:].transpose()))
Te_normal = np.diag(np.dot(feature_normal[:,:6],feature_normal[:,:6].transpose()))
Td_normal = np.diag(np.dot(feature_normal[:,6:],feature_normal[:,6:].transpose()))
Te_fault = np.diag(np.dot(feature_fault[:,:6],feature_fault[:,:6].transpose()))
Td_fault = np.diag(np.dot(feature_fault[:,6:],feature_fault[:,6:].transpose()))
# Calculating Statistical Control Limits Using KDE Nonparametric Estimation
confidence = 0.95
plt.figure()
sns.kdeplot(Td_train, cumulative=False, bw=((input.max() - input.min()) / 1000))
plt.show()
Td_kzx = find_kde(Td_train, confidence)
Te_kzx = find_kde(Te_train, confidence)
# Splice the normal working condition of the test data with the fault
Td_test = np.concatenate([Td_normal,Td_fault],axis=0)
Te_test = np.concatenate([Te_normal,Te_fault],axis=0)
# Visualization of statistical monitoring
plt.suptitle("Test statistics and control limits")
plt.subplot(2,1,1)
plt.plot(Td_test)
plt.ylabel('Td_test')
plt.axhline(Td_kzx, c='r')
plt.subplot(2,1,2)
plt.plot(Te_test)
plt.ylabel('Te_test')
plt.axhline(Te_kzx, c='r')
plt.show()