-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathanomaly_screening.py
560 lines (442 loc) · 18.6 KB
/
anomaly_screening.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
"""
Greg Schivley
Functions to screen demand timeseries data for anomalies.
Adopted from Tyler Ruggles
https://github.com/truggles/EIA_Cleaned_Hourly_Electricity_Demand_Code
"""
import pandas as pd
import numpy as np
def add_rolling_dem(df, short_hour_window):
df["rollingDem"] = (
df["demand_MW"]
.rolling(short_hour_window * 2, min_periods=1, center=True)
.median()
)
return df
def add_rolling_dem_long(df, nDays):
df["rollingDemLong"] = (
df["demand_MW"].rolling(nDays * 24 * 2, min_periods=1, center=True).median()
)
return df
def add_demand_minus_rolling_dem(df):
diff = df["demand_MW"] - df["rollingDem"]
df = df.assign(dem_minus_rolling=diff)
return df
def add_demand_rel_diff_wrt_hourly(df):
diff = df["demand_MW"] / (df["rollingDem"] * df["hourly_median_dem_dev"])
df = df.assign(dem_rel_diff_wrt_hourly=diff)
diff2 = df["demand_MW"] / (df["rollingDemLong"] * df["hourly_median_dem_dev"])
df = df.assign(dem_rel_diff_wrt_hourly_long=diff2)
return df
def add_delta_demand_rel_diff_wrt_hourly(df):
diff3 = df["dem_rel_diff_wrt_hourly"].diff()
df = df.assign(dem_rel_diff_wrt_hourly_delta_pre=diff3)
diff4 = df["dem_rel_diff_wrt_hourly"].diff(periods=-1)
df = df.assign(dem_rel_diff_wrt_hourly_delta_post=diff4)
return df
# This is a global value so does not need to be added to the df
def calculate_relative_demand_difference_IQR(df):
iqr_relative_deltas = np.nanpercentile(
df["dem_rel_diff_wrt_hourly_delta_pre"], 75
) - np.nanpercentile(df["dem_rel_diff_wrt_hourly_delta_pre"], 25)
return iqr_relative_deltas
def add_demand_minus_rolling_dem_iqr(df, iqr_hours):
rolling_window = df["dem_minus_rolling"].rolling(
iqr_hours * 2, min_periods=1, center=True
)
df["dem_minus_rolling_IQR"] = rolling_window.quantile(
0.75
) - rolling_window.quantile(0.25)
return df
def add_hourly_median_dem_deviations(df, nDays):
# Create a df to hold all values to take nanmedian later
vals_dem_minus_rolling = df["dem_minus_rolling"]
# Loop over nDays days on each side
for i in range(-nDays, nDays + 1):
# Already initialized with zero value
if i == 0:
continue
vals_dem_minus_rolling = pd.concat(
[vals_dem_minus_rolling, df.shift(periods=i * 24)["dem_minus_rolling"]],
axis=1,
)
df["vals_dem_minus_rolling"] = vals_dem_minus_rolling.median(axis=1, skipna=True)
# 1+vals to make it a scale factor
return df.assign(
hourly_median_dem_dev=1.0 + df["vals_dem_minus_rolling"] / df["rollingDemLong"]
)
def add_deltas(df):
diff = df["demand_MW"].diff()
df = df.assign(delta_pre=diff)
diff = df["demand_MW"].diff(periods=-1)
df = df.assign(delta_post=diff)
return df
def add_rolling_delta_iqr(df, iqr_hours):
rolling_window = df["delta_pre"].rolling(iqr_hours * 2, min_periods=1, center=True)
df["delta_rolling_IQR"] = rolling_window.quantile(0.75) - rolling_window.quantile(
0.25
)
return df
def add_categories(df):
df["category"] = np.where(df["demand_MW"].isna(), "MISSING", "OKAY")
return df
def filter_neg_and_zeros(df):
df["category"] = np.where(df["demand_MW"] <= 0.0, "NEG_OR_ZERO", df["category"])
filtered = np.where(df["demand_MW"] <= 0.0, df["demand_MW"], np.nan)
df["negAndZeroFiltered"] = filtered
df["demand_MW"] = df["demand_MW"].mask(df["demand_MW"] <= 0.0)
return df
def filter_extrem_demand(df, multiplier):
med = np.nanmedian(df["demand_MW"])
filtered = df["demand_MW"].where(df["demand_MW"] < med * multiplier)
df["globalDemandFiltered"] = np.where(
df["demand_MW"] != filtered, df["demand_MW"], np.nan
)
df["category"] = df["category"].mask(
((df["demand_MW"] != filtered) & (df["demand_MW"].notna())), other="GLOBAL_DEM"
)
df["demand_MW"] = filtered
return df
def filter_global_plus_minus_one(df):
globalDemPlusMinusFiltered = [np.nan for _ in df.index]
for idx in df.index:
if df.loc[idx, "category"] == "GLOBAL_DEM":
if df.loc[idx - 1, "category"] == "OKAY":
df.loc[idx - 1, "category"] = "GLOBAL_DEM_PLUS_MINUS"
globalDemPlusMinusFiltered[idx - 1] = df.loc[idx - 1, "demand_MW"]
df.loc[idx - 1, "demand_MW"] = np.nan
if df.loc[idx + 1, "category"] == "OKAY":
df.loc[idx + 1, "category"] = "GLOBAL_DEM_PLUS_MINUS"
globalDemPlusMinusFiltered[idx + 1] = df.loc[idx + 1, "demand_MW"]
df.loc[idx + 1, "demand_MW"] = np.nan
df["globalDemPlusMinusFiltered"] = globalDemPlusMinusFiltered
return df
def filter_local_demand(df, multiplier_up, multiplier_down):
# Filter in two steps to provide different labels for the categories
filtered = df["demand_MW"].where(
(
df["demand_MW"]
< df["rollingDem"] * df["hourly_median_dem_dev"]
+ multiplier_up * df["dem_minus_rolling_IQR"]
)
)
df["localDemandFilteredUp"] = np.where(
df["demand_MW"] != filtered, df["demand_MW"], np.nan
)
df["category"] = df["category"].mask(
((df["demand_MW"] != filtered) & (df["demand_MW"].notna())),
other="LOCAL_DEM_UP",
)
df["demand_MW"] = filtered
filtered = df["demand_MW"].where(
(
df["demand_MW"]
> df["rollingDem"] * df["hourly_median_dem_dev"]
- multiplier_down * df["dem_minus_rolling_IQR"]
)
)
df["localDemandFilteredDown"] = np.where(
df["demand_MW"] != filtered, df["demand_MW"], np.nan
)
df["category"] = df["category"].mask(
((df["demand_MW"] != filtered) & (df["demand_MW"].notna())),
other="LOCAL_DEM_DOWN",
)
df["demand_MW"] = filtered
return df
# Filter on a multiplier of the IQR and set
# the associated 'demand_MW' value to NAN.
# Only consider "double deltas", hours with
# large deltas on both sides
def filter_deltas(df, multiplier):
filtered = df["demand_MW"].mask(
(
(df["delta_pre"] > df["delta_rolling_IQR"] * multiplier)
& (df["delta_post"] > df["delta_rolling_IQR"] * multiplier)
)
| (
(df["delta_pre"] < -1.0 * df["delta_rolling_IQR"] * multiplier)
& (df["delta_post"] < -1.0 * df["delta_rolling_IQR"] * multiplier)
)
)
df["deltaFiltered"] = np.where(df["demand_MW"] != filtered, df["demand_MW"], np.nan)
df["category"] = df["category"].mask(
((df["demand_MW"] != filtered) & (df["demand_MW"].notna())), other="DELTA"
)
df["demand_MW"] = filtered
return df
# March through all hours recording previous "good"
# demand value and its index. Calculate deltas between
# this value and next "good" hour. If delta is LARGE
# mark NAN.
# Go forwards once, then backwards once to get all options.
def filter_single_sided_deltas(df, multiplier, rel_multiplier, iqr_relative_deltas):
# Go through forwards first, then reverse
prev_good_index = np.nan
deltaSingleFiltered = []
for idx in df.index:
deltaSingleFiltered.append(np.nan)
if np.isnan(df.loc[idx, "demand_MW"]):
continue
# Initialize first good entry, this will never be flagged
if np.isnan(prev_good_index):
prev_good_index = idx
# Check deltas demand and relative wrt hourly adjustment
prev_good_delta_dem = abs(
df.loc[prev_good_index, "demand_MW"] - df.loc[idx, "demand_MW"]
)
prev_good_delta_dem_rel_diff_wrt_hourly = abs(
df.loc[prev_good_index, "dem_rel_diff_wrt_hourly"]
- df.loc[idx, "dem_rel_diff_wrt_hourly"]
)
# delta_rolling_IQR is over 5 days on each side so should be
# similar regardless of which hours' we use. If delta is
# large, mark this hour anomalous
if (prev_good_delta_dem > df.loc[idx, "delta_rolling_IQR"] * multiplier) and (
prev_good_delta_dem_rel_diff_wrt_hourly
> rel_multiplier * iqr_relative_deltas
):
# If the previous "good" value was farther from expected values, then consider current hour good
# and the previous hour will be caught on the way back through the reverse direction.
# The max deviation from the rolling 4 day dem and the rolling 10 day dem is taken
# to help catch cases where a large deviation pulls the rolling 4 day dem to center
# on its values. i.e. SCL 2016 Dec 15.
prev_max = max(
abs(1.0 - df.loc[prev_good_index, "dem_rel_diff_wrt_hourly"]),
abs(1.0 - df.loc[prev_good_index, "dem_rel_diff_wrt_hourly_long"]),
)
current_max = max(
abs(1.0 - df.loc[idx, "dem_rel_diff_wrt_hourly"]),
abs(1.0 - df.loc[idx, "dem_rel_diff_wrt_hourly_long"]),
)
if abs(current_max) < abs(prev_max):
prev_good_index = idx
# else, continue to filter this hour
else:
deltaSingleFiltered[-1] = df.loc[idx, "demand_MW"]
df.loc[idx, "demand_MW"] = np.nan
df.loc[idx, "category"] = "SINGLE_DELTA"
else:
prev_good_index = idx
df["deltaSingleFilteredFwd"] = deltaSingleFiltered
### Go through reversed, ~ copy of above code ###
prev_good_index = np.nan
deltaSingleFiltered = []
for idx in reversed(df.index):
deltaSingleFiltered.append(np.nan)
if np.isnan(df.loc[idx, "demand_MW"]):
continue
# Initialize first good entry, this will never be flagged
if np.isnan(prev_good_index):
prev_good_index = idx
# Check deltas demand and relative wrt hourly adjustment
prev_good_delta_dem = abs(
df.loc[prev_good_index, "demand_MW"] - df.loc[idx, "demand_MW"]
)
prev_good_delta_dem_rel_diff_wrt_hourly = abs(
df.loc[prev_good_index, "dem_rel_diff_wrt_hourly"]
- df.loc[idx, "dem_rel_diff_wrt_hourly"]
)
# delta_rolling_IQR is over 5 days on each side so should be
# similar regardless of which hours' we use. If delta is
# large, mark this hour anomalous
if (prev_good_delta_dem > df.loc[idx, "delta_rolling_IQR"] * multiplier) and (
prev_good_delta_dem_rel_diff_wrt_hourly
> rel_multiplier * iqr_relative_deltas
):
deltaSingleFiltered[-1] = df.loc[idx, "demand_MW"]
df.loc[idx, "demand_MW"] = np.nan
df.loc[idx, "category"] = "SINGLE_DELTA"
else:
prev_good_index = idx
to_app = [val for val in reversed(deltaSingleFiltered)]
df["deltaSingleFilteredBkw"] = to_app
return df
def filter_runs(df):
d1 = df["demand_MW"].diff(periods=1)
d2 = df["demand_MW"].diff(periods=2)
# cannot compare a dtyped [float64] array with a scalar of type [bool]
filtered = df["demand_MW"].mask((d1 == 0) & (d2 == 0))
df["runFiltered"] = np.where(df["demand_MW"] != filtered, df["demand_MW"], np.nan)
df["demand_MW"] = filtered
df["category"] = np.where(
df["runFiltered"].notna(), "IDENTICAL_RUN", df["category"]
)
return df
def filter_anomalous_regions(df, width, anomalous_pct):
percent_good_data_cnt = [0.0 for _ in df.index]
percent_good_data_pre = [0.0 for _ in df.index]
percent_good_data_post = [0.0 for _ in df.index]
df["len_good_data"] = [0 for _ in df.index]
data_quality_cnt = []
data_quality_short = []
start_good_data = np.nan
end_good_data = np.nan
for idx in df.index:
# Remove the oldest item in the list
if len(data_quality_short) > width:
data_quality_short.pop(0)
if len(data_quality_cnt) > 2 * width:
data_quality_cnt.pop(0)
# Add new item and don't count MISSING as 'bad' data
if df.loc[idx, "category"] == "OKAY" or df.loc[idx, "category"] == "MISSING":
data_quality_cnt.append(1)
data_quality_short.append(1)
# Track length of good data chunks
if np.isnan(start_good_data):
start_good_data = idx
end_good_data = idx
else:
data_quality_cnt.append(0)
data_quality_short.append(0)
# Fill in length of good data chunk
if not (np.isnan(start_good_data) or np.isnan(end_good_data)):
len_good = end_good_data - start_good_data + 1
df.loc[start_good_data:end_good_data, "len_good_data"] = len_good
start_good_data = np.nan
end_good_data = np.nan
# centered measurements have length 2 * width
if len(data_quality_cnt) > 2 * width:
percent_good_data_cnt[idx - width] = np.mean(data_quality_cnt)
# left and right / pre and post measurements have length = width + 1
if len(data_quality_short) > width:
percent_good_data_pre[idx] = np.mean(data_quality_short)
percent_good_data_post[idx - width] = np.mean(data_quality_short)
anomalousRegionsFiltered = [np.nan for _ in df.index]
for idx in df.index:
if percent_good_data_cnt[idx] <= anomalous_pct:
for j in range(idx - width, idx + width):
if j < 1 or j >= len(df.index):
continue
if df.loc[j, "category"] == "OKAY":
# If this is the start or end of continuous good data, don't filter
if (
percent_good_data_pre[j] == 1.0
or percent_good_data_post[j] == 1.0
):
continue
if df.loc[j, "len_good_data"] > width:
continue
df.loc[j, "category"] = "ANOMALOUS_REGION"
anomalousRegionsFiltered[j] = df.loc[j, "demand_MW"]
df.loc[j, "demand_MW"] = np.nan
df["anomalousRegionsFiltered"] = anomalousRegionsFiltered
return df
def screen_timeseries(
name,
df,
short_hour_window,
iqr_hours,
nDays,
global_dem_cut,
local_dem_cut_up,
local_dem_cut_down,
delta_multiplier,
delta_single_multiplier,
rel_multiplier,
anomalous_regions_width,
anomalous_pct,
):
# Add category labels to track which algo screens an hourly value
df = add_categories(df)
# Mark missing and empty values
df = df.assign(missing=df["demand_MW"].isna())
# ---------------------------------------------
# Screening Step 1
# ---------------------------------------------
# Set all negative and zero values to NAN
# (negative or zero filter)
df = filter_neg_and_zeros(df)
# Set last demand values in runs of 3+ to NAN
# (identical run filter)
df = filter_runs(df)
# Global demand filter on 10x the median value
# (global demand filter)
df = filter_extrem_demand(df, global_dem_cut)
# Filter +/- 1 hour from any global deman filtered hours
# (global demand plus/minus 1 hour filter)
df = filter_global_plus_minus_one(df)
# ---------------------------------------------
# Calculate demand characteristics for Step 2
# ---------------------------------------------
# 48 hour moving median (M_{t,48hr})
df = add_rolling_dem(df, short_hour_window)
# 480 hour moving median (M_{t,480hr})
df = add_rolling_dem_long(df, nDays)
# demand minus moving median (Delta(d_{t},M_{t,48hr}))
df = add_demand_minus_rolling_dem(df)
# IQR values of relative deviations from diurnal cycle template (IQR_{dem,t})
df = add_demand_minus_rolling_dem_iqr(df, iqr_hours)
# demand deltas (delta(d_{t-1},d_{t}))
df = add_deltas(df)
# IQR values of demand deltas (IQR_{delta,t})
df = add_rolling_delta_iqr(df, iqr_hours)
# normalized hourly demand template (h_{t,diurnal})
df = add_hourly_median_dem_deviations(df, nDays)
# Demand deviation from hourly diurnal template (r_{t})
# This adds both the short and long moving medians
df = add_demand_rel_diff_wrt_hourly(df)
# Hour-to-hour differences between hourly diurnal templates
# (delta(r_{t-1},r_{t}))
# This adds differences for both the short and long moving medians
df = add_delta_demand_rel_diff_wrt_hourly(df)
# Calculate the global IQR for the hour-to-hour differences
# between hourly diurnal templates (IQR_{r})
# This is a global value and is not added to the dataframe
iqr_relative_deltas = calculate_relative_demand_difference_IQR(df)
# ---------------------------------------------
# Screening Step 2
# ---------------------------------------------
# (local demand filter)
df = filter_local_demand(df, local_dem_cut_up, local_dem_cut_down)
# (double-sided delta filter)
df = filter_deltas(df, delta_multiplier)
# (single-sided delta filter)
df = filter_single_sided_deltas(
df, delta_single_multiplier, rel_multiplier, iqr_relative_deltas
)
# (anomalous regions filter)
df = filter_anomalous_regions(df, anomalous_regions_width, anomalous_pct)
return name, df
# # Save as csv for easy viewing and pickle for computatinal ease
# print(f"Saving parquet ./data/screened/{region}-screened.parquet")
# # pickle_file = open(f'./data/pickle_{region}.pkl', 'wb')
# screened_path = input_path / "screened"
# screened_path.mkdir(parents=True, exist_ok=True)
# df.to_parquet(screened_path / f"{region}-screened.parquet")
# # pickle.dump(df, pickle_file)
# # pickle_file.close()
# df.to_csv(screened_path / f"{region}-screened.csv", index=False)
def make_anomaly_summary(df_dict):
type_count = {}
type_count["name"] = []
type_count["median_demand"] = []
TYPEs = [
"OKAY",
"MISSING",
"NEG_OR_ZERO",
"GLOBAL_DEM",
"GLOBAL_DEM_PLUS_MINUS",
"LOCAL_DEM_UP",
"LOCAL_DEM_DOWN",
"DELTA",
"SINGLE_DELTA",
"IDENTICAL_RUN",
"ANOMALOUS_REGION",
]
for t in TYPEs:
type_count[t] = []
for name, df in df_dict.items():
type_count["name"].append(name)
median_demand = df["demand_MW"].median()
if median_demand > 0:
median_demand = round(median_demand, 2)
type_count["median_demand"].append(median_demand)
for TYPE in TYPEs:
s = (df["category"] == TYPE).sum()
type_count[TYPE].append(s)
anomaly_summary = pd.DataFrame(type_count).sort_values("name")
anomaly_summary["pct_OKAY"] = anomaly_summary["OKAY"] / anomaly_summary[TYPEs].sum(
axis=1
)
return anomaly_summary