-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMAPSS_PlotFunctions.py
More file actions
182 lines (140 loc) · 7.3 KB
/
CMAPSS_PlotFunctions.py
File metadata and controls
182 lines (140 loc) · 7.3 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import torch
import pandas as pd
import matplotlib.pyplot as plt
import CMAPSS_Dataloader
def train_actual_predicted(read_path, sub_dataset, window_size, max_life, train_output, alpha_grid, alpha_low, alpha_high, _COLORS):
train_data = CMAPSS_Dataloader.load_dataset(read_path, sub_dataset, max_life, train=True, test=False, finaltest=False)
train_data_gp = train_data.groupby('engine_id', sort=False)
train_data_reduce = []
for engine_id, data in train_data_gp:
data = data.iloc[window_size-1:,:]
train_data_reduce.append(data)
train_data = pd.concat(train_data_reduce, ignore_index=True)
train_data['Predicted RUL'] = train_output
train_rul_group = train_data.groupby('engine_id', sort = False)
max_plots=[]
for engine_id, data in train_rul_group:
actual_rul = data[['cycle', 'RUL']]
actual_rul = actual_rul.set_index('cycle')
predicted_rul = data[['cycle','Predicted RUL']]
predicted_rul = predicted_rul.set_index('cycle')
fig=plt.figure(figsize=(7,7))
ax=plt.subplot(1,1,1)
plt.grid(b=True, which="both", axis="both", alpha=alpha_grid)
plt.xlabel("Time (Cycle)", fontsize = 24)
plt.ylabel("RUL", fontsize = 24)
plt.xlim(data['cycle'].min(), data['cycle'].max())
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)
plt.plot(predicted_rul, color=_COLORS[0], alpha=alpha_high, linestyle="solid",linewidth=4, label='Predicted RUL')
plt.plot(actual_rul, color=_COLORS[1], alpha=alpha_high, linestyle="dashed", linewidth=4, label='Actual RUL')
ax.set_title('FD'+sub_dataset +" Train Engine Unit-" + str(engine_id), fontsize = 26)
ax.legend(loc="upper right", fontsize = 16)
max_plots.append(engine_id)
if len(max_plots) >= 1:
break
fig.tight_layout()
return None
def test_actual_predicted(read_path, sub_dataset, window_size, max_life, test_output, alpha_grid, alpha_low, alpha_high, _COLORS):
test_data = CMAPSS_Dataloader.load_dataset(read_path, sub_dataset, max_life, train=False, test=True, finaltest=False)
test_data_gp = test_data.groupby('engine_id', sort=False)
test_data_reduce = []
for engine_id, data in test_data_gp:
data = data.iloc[window_size-1:,:]
test_data_reduce.append(data)
test_data = pd.concat(test_data_reduce, ignore_index=True)
test_data['Predicted RUL'] = test_output
test_rul_group = test_data.groupby('engine_id', sort = False)
max_plots=[]
for engine_id, data in test_rul_group:
actual_rul = data[['cycle', 'RUL']]
actual_rul = actual_rul.set_index('cycle')
predicted_rul = data[['cycle','Predicted RUL']]
predicted_rul = predicted_rul.set_index('cycle')
fig=plt.figure(figsize=(7,7))
ax=plt.subplot(1,1,1)
plt.grid(b=True, which="both", axis="both", alpha=alpha_grid)
plt.xlabel("Time (Cycle)", fontsize = 24)
plt.ylabel("RUL", fontsize = 24)
plt.xlim(data['cycle'].min(), data['cycle'].max())
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)
plt.plot(predicted_rul, color=_COLORS[0], alpha=alpha_high, linestyle="solid",linewidth=4, label='Predicted RUL')
plt.plot(actual_rul, color=_COLORS[1], alpha=alpha_high, linestyle="dashed", linewidth=4, label='Actual RUL')
ax.set_title('FD'+sub_dataset +" Test Engine Unit-" + str(engine_id), fontsize = 26)
ax.legend(loc="upper right", fontsize = 16)
max_plots.append(engine_id)
if len(max_plots) >= 1:
break
fig.tight_layout()
return None
def loss_plot(sub_dataset, train_loss_epoch, test_loss_epoch, alpha_grid, alpha_low, alpha_high, _COLORS):
fig=plt.figure(figsize=(7,7))
plt.grid(b=True, which="both", axis="both", alpha=alpha_grid)
plt.xlabel('Epochs', fontsize=24)
plt.ylabel('RMSE', fontsize=24)
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)
plt.plot(train_loss_epoch, color=_COLORS[0], alpha=alpha_low, linestyle="solid", label='Train Loss')
plt.plot(test_loss_epoch, color=_COLORS[1], alpha=alpha_low, linestyle="solid", label='Test Loss')
fig.tight_layout()
return None
def score_func(pred_rul, actual_rul):
Si = []
for i in range(0,pred_rul.shape[0]):
di = pred_rul[i] - actual_rul[i]
if di < 0:
inter = torch.exp((-di)/13) - 1
else:
inter = torch.exp(di/10) - 1
Si.append(inter)
s = torch.sum(torch.stack(Si))
return s
def fianltest_actual_vs_predicted(read_path, sub_dataset, window_size, max_life, finaltest_output, alpha_grid,
alpha_low, alpha_high, _COLORS):
finaltest_data = CMAPSS_Dataloader.load_dataset(read_path, sub_dataset,max_life, train=False, test=False, finaltest=True)
finaltest_data_groupped = finaltest_data.groupby('engine_id', sort = False)
finaltest_data_reduced = []
for engine_id, data in finaltest_data_groupped:
data = data.iloc[window_size-1 : , :]
finaltest_data_reduced.append(data)
finaltest_data = pd.concat(finaltest_data_reduced, ignore_index=True)
finaltest_data['Predicted RUL'] = finaltest_output
finaltest_rul_group = finaltest_data.groupby('engine_id', sort = False)
if sub_dataset == "001":
plot_ids=[24]
if sub_dataset == "002":
plot_ids=[5]
if sub_dataset == "003":
plot_ids=[3]
if sub_dataset == "004":
plot_ids=[32]
for engine_id, data in finaltest_rul_group:
if engine_id in plot_ids:
actual_rul = data[['cycle', 'RUL']]
actual_rul = actual_rul.set_index('cycle')
predicted_rul = data[['cycle','Predicted RUL']]
predicted_rul = predicted_rul.set_index('cycle')
fig=plt.figure(figsize=(12,5))
ax=plt.subplot(1,1,1)
plt.grid(b=True, which="both", axis="both", alpha=alpha_grid)
plt.xlabel("Time (Cycle)", fontsize=24)
plt.ylabel("RUL",fontsize=24)
plt.xlim(data['cycle'].min(), data['cycle'].max())
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)
plt.plot(predicted_rul, color=_COLORS[0], alpha=alpha_high, linestyle="solid", linewidth=4, label='Predicted RUL')
plt.plot(actual_rul, color=_COLORS[1], alpha=alpha_high, linestyle="dashed", linewidth=4, label='Actual RUL')
ax.set_title('FD'+sub_dataset+" Final Test Engine Unit-" + str(engine_id), fontsize=26)
ax.legend(loc="upper right", fontsize = 16)
fig.tight_layout()
rul_score = []
for engine_id, data in finaltest_rul_group:
last_rul = data["Predicted RUL"].iloc[-1]
rul_score.append(last_rul)
true_rul = pd.read_csv(read_path + "RUL_FD"+sub_dataset + ".csv")
ture_rul = torch.FloatTensor(true_rul['RUL'].values)
rul_socre = torch.FloatTensor(rul_score)
score = score_func(rul_socre, ture_rul)
print("The Final Score is:", score.item())
return score