-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfitcurve.py
331 lines (295 loc) · 13.4 KB
/
fitcurve.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import numpy as np
import pandas as pd
import random
import time
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
from mpl_toolkits.mplot3d import Axes3D
DEBUG = False
DEBUG_WIND_NUMBER = 3
###################
# Read raw dataset
###################
print("Reading dataset...")
raw_df = pd.read_csv("./data/dataset.csv") # Load Dataset
param_df = pd.read_csv("./data/parameters.csv", index_col="风机编号").T # Load turbine parameters
raw_df["ts"] = raw_df["Time"].apply(lambda x: int(time.mktime(time.strptime(x, "%Y/%m/%d %H:%M"))))
raw_df["label"] = 0
raw_df["selected"] = 0
raw_df_index = raw_df.index
##############
# Parameters
##############
pass
# 对每个风机设置不同的参数
recur_param_df = pd.read_csv("./data/recur_param.csv", index_col="风机编号")
##################################################################################
# 0. Preliminary Elimination with simple rules
# Remove points whose windspeed, rotorspeed or power < 0.
# Remove points whose power > 0 but windspeed not in [切入风速, 切出风速] range.
##################################################################################
print("Preliminary Elimination...")
df = raw_df
raw_df.loc[df["Power"]<0, "label"] = 1
raw_df.loc[df["WindSpeed"]<0, "label"] = 1
raw_df.loc[df["RotorSpeed"]<0, "label"] = 1
# # 下面这些好像不太行,但是符合论文中的物理规则?
# for wind_number, sub_df in df.groupby("WindNumber"):
# print(" Wind Number:", wind_number)
# cut_in_windspeed, cut_out_windspeed = param_df.loc["切入风速", wind_number], param_df.loc["切出风速", wind_number]
# power_abnormal_condition = (df["Power"] > 0) & ((df["WindSpeed"] < cut_in_windspeed) | (df["WindSpeed"] > cut_out_windspeed))
# raw_df.loc[power_abnormal_condition, "label"] = 1
# 对每个风机的特别预处理
for wind_number, sub_df in df.groupby("WindNumber"):
if DEBUG and not wind_number == DEBUG_WIND_NUMBER:
continue
pass
print(raw_df.loc[raw_df_index, "label"].value_counts())
#######################################################################################
# 1. The Elimination of Horizontal Sparse Outliers Using Quartile Method
# For each wind turbind:
# Divide wind power values into some equal intervals.
# The quartile method is applied to the wind speed dataset in each power interval.
# The wind speed data beyond [Fl, Fu] are eliminated from the dataset.
# IQR = P3−P1 (0.75 and 0.25 percentile points)
# [Fl,Fu] = [P1 −1.5*IQR, P3 +1.5*IQR]
#######################################################################################
print("Horizontal Eliminating...")
df = raw_df[raw_df["label"]==0]
for wind_number, sub_df in df.groupby("WindNumber"):
if DEBUG and not wind_number == DEBUG_WIND_NUMBER:
continue
print(" Wind Number:", wind_number)
rated_power = param_df.loc["额定功率", wind_number]
try:
pw_interval_width_ratio = recur_param_df.loc[wind_number, "pw_interval_width_ratio"]
horizontal_low_tolarance = recur_param_df.loc[wind_number, "horizontal_low_tolarance"]
horizontal_high_tolarance = recur_param_df.loc[wind_number, "horizontal_high_tolarance"]
except:
pw_interval_width_ratio = 0.0125
horizontal_low_tolarance = 1.5
horizontal_high_tolarance = 1.5
pw_interval_width = pw_interval_width_ratio * rated_power
sub_df.loc[:, "pw_interval"] = sub_df["Power"].apply(lambda x: x // pw_interval_width)
for pw_interval, interval_df in sub_df.groupby("pw_interval"):
p1, p3 = interval_df["WindSpeed"].quantile(0.25), interval_df["WindSpeed"].quantile(0.75)
iqr = p3 - p1
fl, fu = p1 - horizontal_low_tolarance*iqr, p3 + horizontal_high_tolarance*iqr
bad_interval_index = (interval_df["WindSpeed"] < fl) | (interval_df["WindSpeed"] > fu)
sparse_outlier_index = interval_df[bad_interval_index].index
raw_df.loc[sparse_outlier_index, "label"] = 1
print(raw_df.loc[raw_df_index, "label"].value_counts())
#############################################################################################################################
# 2. The Elimination of Vertical Sparse Outliers Using Quartile Method
# For each wind turbine:
# Divide wind speed values into a number of equal intervals.
# The quartile method is applied to the wind power dataset in each wind speed interval.
# Attention: Only the wind power data above Fu are eliminated from the dataset while the data below Fl are not considered.
# #############################################################################################################################
print("Vertical Eliminating...")
df = raw_df[raw_df["label"]==0]
for wind_number, sub_df in df.groupby("WindNumber"):
if DEBUG and not wind_number == DEBUG_WIND_NUMBER:
continue
print(" Wind Number:", wind_number)
try:
ws_interval_width = recur_param_df.loc[wind_number, "ws_interval_width"]
vertical_tolarance = recur_param_df.loc[wind_number, "vertical_tolarance"]
except:
ws_interval_width = 0.5 # m/s
vertical_tolarance = 1.5
sub_df.loc[:, "ws_interval"] = sub_df["WindSpeed"].apply(lambda x: x // ws_interval_width)
for ws_interval, interval_df in sub_df.groupby("ws_interval"):
p1, p3 = interval_df["Power"].quantile(0.25), interval_df["Power"].quantile(0.75)
iqr = p3 - p1
fl, fu = p1 - 1.5*iqr, p3 + vertical_tolarance*iqr
bad_interval_index = interval_df["Power"] > fu
sparse_outlier_index = interval_df[bad_interval_index].index
raw_df.loc[sparse_outlier_index, "label"] = 1
print(raw_df.loc[raw_df_index, "label"].value_counts())
####################################################################################################################
# 3. The Elimination according to timestamp
# For each wind turbine:
# Select points which looks good.
####################################################################################################################
print("Timestamp check...")
df = raw_df[raw_df["label"]==0]
for wind_number, sub_df in df.groupby("WindNumber"):
if DEBUG and not wind_number == DEBUG_WIND_NUMBER:
continue
print(" Wind Number:", wind_number)
timestamp = sub_df["Time"].apply(lambda x: int(time.mktime(time.strptime(x, "%Y/%m/%d %H:%M"))))
if wind_number == 3:
selected_condition = (sub_df["ts"] > 1.528 * 10**9) & (sub_df["ts"] < 1.535 * 10**9)
selected_index = sub_df[selected_condition].index
raw_df.loc[selected_index, "selected"] = 1
continue
threshold = sub_df["ts"].quantile(0.90)
selected_condition = sub_df["ts"] > threshold
selected_index = sub_df[selected_condition].index
raw_df.loc[selected_index, "selected"] = 1
print(raw_df.loc[raw_df_index, "label"].value_counts())
###########
# 5. Fit poly curves
################
print("Fitting curves...")
curves = []
df = raw_df[raw_df["selected"]==1]
for wind_number, sub_df in df.groupby("WindNumber"):
print(" Wind Number:", wind_number)
fig, axs = plt.subplots(1, 3)
fig.set_size_inches(40, 20)
fig.suptitle("WindNumber: " + str(wind_number))
axs[0].set_title("W&P")
axs[0].set_xlabel("WindSpeed")
axs[0].set_ylabel("Power")
axs[0].grid()
if(wind_number == 11 or wind_number == 12):
z0 = np.polyfit(sub_df["WindSpeed"], sub_df["Power"],9)
else:
z0 = np.polyfit(sub_df["WindSpeed"], sub_df["Power"],5)
p0 = np.poly1d(z0)
curves.append(p0)
ax0 = axs[0].scatter(sub_df["WindSpeed"], sub_df["Power"])
x = np.linspace(2,14, 50)
y =p0(x)
axs[0].plot(x,y,color='red')
#####################################
# 6. Eliminate according to curves
#####################################
df = raw_df[raw_df['label'] == 0]
for wind_number, sub_df in df.groupby("WindNumber"):
print(" Wind Number:", wind_number)
# delete points below the line1: (10.7, 1500) -> (12.3, 2000)
outlier_condition11 = sub_df["Power"] < curves[wind_number-1](sub_df["WindSpeed"] - delta)
outlier_condition12 = sub_df["Power"] > curves[wind_number-1](sub_df["WindSpeed"] + delta)
outlier_condition1 = (outlier_condition11 | outlier_condition12)
outlier_index = sub_df[outlier_condition1].index
raw_df.loc[outlier_index, "label"] = 1
####################################################################################################################
# 7. Specialized processing for each wind turbine...
# For each wind turbine:
# Divide wind speed values into a number of equal intervals.
# The DBSCAN clustering method is applied to the wind power dataset in each wind speed interval.
# The topmost cluster with largest average power value is the normal data, while other clusters are eliminated.
####################################################################################################################
print("Specialize for each wind turbine...")
raw_df["diff"] = 0
df = raw_df.loc[raw_df_index]
df = df[df["label"]==0]
for wind_number, sub_df in df.groupby("WindNumber"):
if DEBUG and not wind_number == DEBUG_WIND_NUMBER:
continue
pass
print(raw_df.loc[raw_df_index, "label"].value_counts())
################################################
# Plot the results and save the submission file
################################################
raw_df = raw_df.loc[raw_df_index]
print("Plotting 3D scatter...")
# 画三维散点图(结果图)
for wind_number, sub_df in raw_df.groupby("WindNumber"):
if DEBUG and not wind_number == DEBUG_WIND_NUMBER:
continue
print(" Wind Number:", wind_number)
fig = plt.figure()
fig.set_size_inches(30, 30, 30)
ax = Axes3D(fig)
ax.set_title("Color stands for label")
ax0 = ax.scatter(sub_df["WindSpeed"], sub_df["RotorSpeed"], sub_df["Power"], c=sub_df["selected"])
ax.set_xlabel("WindSpeed")
ax.set_ylabel("RotorSpeed")
ax.set_zlabel("Power")
fig.colorbar(ax0)
plt.savefig("./figures/fitcurve/" + str(wind_number) + "_selected_scatter.jpg")
plt.close()
# 画维度两两组合的函数关系(结果图)
print("Plotting 2D scatter...")
# df = raw_df[raw_df["label"]==0]
df = raw_df
for wind_number, sub_df in df.groupby("WindNumber"):
if DEBUG and not wind_number == DEBUG_WIND_NUMBER:
continue
print(" Wind Number:", wind_number)
fig, axs = plt.subplots(1, 3)
fig.set_size_inches(40, 20)
fig.suptitle("WindNumber: " + str(wind_number))
axs[0].set_title("W&P")
axs[0].set_xlabel("WindSpeed")
axs[0].set_ylabel("Power")
axs[0].set_xlim(0, 25)
axs[0].set_xticks(np.linspace(0, 25, 26))
axs[0].set_ylim(-100, 2300)
axs[0].set_yticks(np.linspace(0, 2500, 26))
ax0 = axs[0].scatter(sub_df["WindSpeed"], sub_df["Power"], c=sub_df["selected"])
fig.colorbar(ax0, ax=axs[0])
axs[1].set_title("W&R")
axs[1].set_xlabel("WindSpeed")
axs[1].set_ylabel("RotorSpeed")
ax1 = axs[1].scatter(sub_df["WindSpeed"], sub_df["RotorSpeed"], c=sub_df["selected"])
fig.colorbar(ax1, ax=axs[1])
axs[2].set_xlabel("RotorSpeed")
axs[2].set_ylabel("Power")
axs[2].set_title("R&P")
ax2 = axs[2].scatter(sub_df["RotorSpeed"], sub_df["Power"], c=sub_df["selected"])
fig.colorbar(ax2, ax=axs[2])
plt.savefig("./figures/fitcurve/" + str(wind_number) + "_selected_dim_relation.jpg")
plt.close()
# 画三维散点图
print("Plotting 3D raw scatter...")
for wind_number, sub_df in raw_df.groupby("WindNumber"):
color = sub_df["ts"]
if not DEBUG:
break
if DEBUG and not wind_number == DEBUG_WIND_NUMBER:
continue
print(" Wind Number:", wind_number)
fig = plt.figure()
fig.set_size_inches(30, 30, 30)
ax = Axes3D(fig)
ax.set_title("Color stands for label")
ax0 = ax.scatter(sub_df["WindSpeed"], sub_df["RotorSpeed"], sub_df["Power"], c=color)
ax.set_xlabel("WindSpeed")
ax.set_ylabel("RotorSpeed")
ax.set_zlabel("Power")
fig.colorbar(ax0)
plt.savefig("./figures/fitcurve/" + str(wind_number) + "_raw_scatter.jpg")
# plt.show()
plt.close()
# 画维度两两组合的函数关系
print("Plotting 2D raw scatter...")
df = raw_df
for wind_number, sub_df in df.groupby("WindNumber"):
color = sub_df["ts"]
if not DEBUG:
break
if DEBUG and not wind_number == DEBUG_WIND_NUMBER:
continue
print(" Wind Number:", wind_number)
fig, axs = plt.subplots(1, 3)
fig.set_size_inches(40, 20)
fig.suptitle("WindNumber: " + str(wind_number))
axs[0].set_title("W&P")
axs[0].set_xlabel("WindSpeed")
axs[0].set_ylabel("Power")
axs[0].set_xlim(0, 25)
axs[0].set_xticks(np.linspace(0, 25, 26))
axs[0].set_ylim(-100, 2300)
axs[0].set_yticks(np.linspace(0, 2500, 26))
ax0 = axs[0].scatter(sub_df["WindSpeed"], sub_df["Power"], c=color)
fig.colorbar(ax0, ax=axs[0])
axs[1].set_title("W&R")
axs[1].set_xlabel("WindSpeed")
axs[1].set_ylabel("RotorSpeed")
ax1 = axs[1].scatter(sub_df["WindSpeed"], sub_df["RotorSpeed"], c=color)
fig.colorbar(ax1, ax=axs[1])
axs[2].set_xlabel("RotorSpeed")
axs[2].set_ylabel("Power")
axs[2].set_title("R&P")
ax2 = axs[2].scatter(sub_df["RotorSpeed"], sub_df["Power"], c=color)
fig.colorbar(ax2, ax=axs[2])
plt.savefig("./figures/fitcurve/" + str(wind_number) + "_raw_dim_relation.jpg")
plt.close()
if not DEBUG:
submission_df = raw_df[["WindNumber", "Time", "label", "selected"]]
submission_df.to_csv("./results/result.csv", index=False)