-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsensor_maintenance.py
519 lines (421 loc) · 21.4 KB
/
sensor_maintenance.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
import logging
import numpy as np
import os
import glob
import pandas as pd
from functools import partial
from gluonts.dataset.util import to_pandas
from tactis.gluon.dataset import get_dataset
from ..base import UnivariateCRPSTask
from ..config import DATA_STORAGE_PATH
from ..utils import get_random_window_univar, datetime_to_str
from . import WeightCluster
get_dataset = partial(get_dataset, path=DATA_STORAGE_PATH)
from cik_benchmark.data.pems import (
load_traffic_series,
get_traffic_prediction_length,
get_traffic_history_factor,
)
class SensorPeriodicMaintenanceTask(UnivariateCRPSTask):
"""
A task where the history contains misleading information due to periodic
sensor maintenance. The maintenance periods should not be reflected in
the forecast.
"""
_context_sources = UnivariateCRPSTask._context_sources + ["c_cov"]
_skills = UnivariateCRPSTask._skills + ["instruction following"]
__version__ = "0.0.2" # Modification will trigger re-caching
def random_instance(self):
datasets = ["traffic"]
# Select a random dataset
dataset_name = self.random.choice(datasets)
if dataset_name == "electricity_hourly":
dataset = get_dataset(dataset_name, regenerate=False)
assert len(dataset.train) == len(
dataset.test
), "Train and test sets must contain the same number of time series"
# Get the dataset metadata
metadata = dataset.metadata
# Select a random time series
ts_index = self.random.choice(len(dataset.train))
full_series = to_pandas(list(dataset.test)[ts_index])
# Select a random window
window = get_random_window_univar(
full_series,
prediction_length=metadata.prediction_length,
history_factor=self.random.randint(3, 7),
random=self.random,
)
# Extract the history and future series
history_series = window.iloc[: -metadata.prediction_length]
future_series = window.iloc[-metadata.prediction_length :]
# Duration: between 2 and 6 hours
duration = self.random.randint(2, 7)
start_hour = self.random.randint(
0, 24 - (duration + 1)
) # +1 so the drop doesn't come at the end of the history window
maintenance_start_date = history_series.index[start_hour]
maintenance_end_date = history_series.index[start_hour + duration]
# Make the hour strings
maintenance_start_hour = f"{maintenance_start_date.hour:02d}:00"
maintenance_end_hour = f"{(maintenance_end_date.hour):02d}:00"
# Add the maintenance period to the prediction window
history_series.index = history_series.index.to_timestamp()
history_series.loc[
history_series.between_time(
maintenance_start_hour, maintenance_end_hour
).index
] = 0
# Convert history index to timestamp for consistency
future_series.index = future_series.index.to_timestamp()
background = f"This series represents electricity consumption recordings captured by a meter. "
background += f"The sensor was offline for maintenance every day between {maintenance_start_hour} and {maintenance_end_hour}, which resulted in zero readings. Assume that the sensor will not be in maintenance in the future."
elif dataset_name == "traffic":
prediction_length = get_traffic_prediction_length()
max_attempts = 100
for _ in range(max_attempts):
target = "Occupancy (%)"
full_series = load_traffic_series(target=target, random=self.random)
try:
# Select a random window
window = get_random_window_univar(
full_series,
prediction_length=prediction_length,
history_factor=get_traffic_history_factor(),
random=self.random,
max_attempts=1, # Handle the attempts in this method instead
)
break
except ValueError:
# This exception is thrown if get_random_window_univar did not select a valid window
pass
else:
raise ValueError(
f"Could not find a valid window after {max_attempts} attempts"
)
# Extract the history and future series
history_series = window.iloc[:-prediction_length]
future_series = window.iloc[-prediction_length:]
# Duration: between 2 and 6 hours
duration = self.random.randint(2, 7)
start_hour = self.random.randint(
0, 24 - (duration + 1)
) # +1 so the drop doesn't come at the end of the history window
maintenance_start_date = history_series.index[start_hour]
maintenance_end_date = history_series.index[start_hour + duration]
# Make the hour strings
maintenance_start_hour = f"{maintenance_start_date.hour:02d}:00"
maintenance_end_hour = f"{(maintenance_end_date.hour):02d}:00"
history_series.loc[
history_series.between_time(
maintenance_start_hour, maintenance_end_hour
).index
] = 0
background = f"This series represents the occupancy rate (%) captured by a highway sensor. "
background += f"The sensor was offline for maintenance every day between {maintenance_start_hour} and {maintenance_end_hour}, which resulted in zero readings. Assume that the sensor will not be in maintenance in the future."
else:
raise NotImplementedError(f"Dataset {dataset_name} is not supported.")
# Instantiate the class variables
self.past_time = history_series.to_frame()
self.future_time = future_series.to_frame()
self.background = background
# ROI parameters to add focus to the times where there would have been maintenance in the prediction region
maintenance_hours_in_pred = [
future_series.index.get_loc(x)
for x in future_series.between_time(
maintenance_start_hour, maintenance_end_hour
).index
]
self.region_of_interest = maintenance_hours_in_pred
class SensorTrendAccumulationTask(UnivariateCRPSTask):
"""
A task where the history contains misleading information due to the
measurement sensor accumulating a trend over time due to a calibration
issue. The trend should not be reflected in the forecast.
"""
_context_sources = UnivariateCRPSTask._context_sources + ["c_cov"]
_skills = UnivariateCRPSTask._skills + ["instruction following", "reasoning: math"]
__version__ = "0.0.3" # Modification will trigger re-caching
def get_series(
self,
dataset_name: str = "traffic",
target=None, # 'Speed (mph)' or 'Occupancy (%)'
):
if dataset_name == "traffic":
if target is None:
target = "Occupancy (%)"
series = load_traffic_series(target=target, random=self.random)
else:
raise NotImplementedError(f"Dataset {dataset_name} is not supported.")
return series
def get_prediction_length(self, dataset_name: str = "traffic"):
if dataset_name == "traffic":
return get_traffic_prediction_length()
else:
raise NotImplementedError(f"Dataset {dataset_name} is not supported.")
def get_history_factor(self, dataset_name: str = "traffic"):
if dataset_name == "traffic":
return get_traffic_history_factor()
else:
raise NotImplementedError(f"Dataset {dataset_name} is not supported.")
def random_instance(self):
datasets = ["traffic"]
dataset_name = self.random.choice(datasets)
prediction_length = self.get_prediction_length(dataset_name=dataset_name)
max_attempts = 100
for _ in range(max_attempts):
full_series = self.get_series(dataset_name=dataset_name)
try:
# Select a random window
window = get_random_window_univar(
full_series,
prediction_length=prediction_length,
history_factor=self.get_history_factor(dataset_name=dataset_name),
random=self.random,
max_attempts=1, # Handle the attempts in this method instead
)
break
except ValueError:
# This exception is thrown if get_random_window_univar did not select a valid window
pass
else:
raise ValueError(
f"Could not find a valid window after {max_attempts} attempts"
)
# Extract the history and future series
history_series = window.iloc[:-prediction_length]
future_series = window.iloc[-prediction_length:]
if dataset_name == "traffic":
# Sample a starting point in the first half of the history's index
history_series.index = pd.to_datetime(history_series.index)
start_point = self.random.choice(
history_series.index[: len(history_series) // 2]
)
n_points_slope = len(history_series) - history_series.index.get_loc(
start_point
)
# Design an artificial additive trend
# XXX: We make sure that the maximum increase to any value in the series if
# of 1.25 to 2 times the absolute mean value of the series. This ensures a
# significant trend without making the series explode.
mean = np.abs(history_series.mean())
factor = (
1.25 + self.random.rand() * 0.75
) # Random factor between 1.25 and 2
# XXX: Assumes a constant frequency
trend = np.linspace(0, factor * mean, n_points_slope + 1)[
1:
] # Start at non-zero value
# Add trend to the series
history_series.loc[start_point:] = history_series.loc[
start_point:
] + np.float32(trend)
# Convert future index to timestamp for consistency
future_series.index = pd.to_datetime(future_series.index)
background = f"This series represents the occupancy rate (%) captured by a highway sensor. "
background += (
f"The sensor had a calibration problem starting from {datetime_to_str(start_point)} "
+ f"which resulted in an additive trend in the series that increases by {trend[1] - trend[0]:.4f} at every hour. "
+ f"At timestep {future_series.index[0]}, the sensor was repaired and this additive trend will disappear."
)
else:
raise NotImplementedError(f"Dataset {dataset_name} is not supported.")
# Instantiate the class variables
self.past_time = history_series.to_frame()
self.future_time = future_series.to_frame()
self.background = background
# No RoI need to be defined as the full prediction window is important
class SensorSpikeTask(UnivariateCRPSTask):
"""
A task where the history contains misleading information due to the
measurement sensor having random spikes due to an unexpected glitch.
This should not affect the forecast.
# TODO: Support more spikes: in which case single-timesteps spikes would be trivial; but it is non-trivial to handle multi-length spikes
"""
_context_sources = UnivariateCRPSTask._context_sources + ["c_cov"]
_skills = UnivariateCRPSTask._skills + ["instruction following"]
__version__ = "0.0.3 " # Modification will trigger re-caching
def get_series(
self,
dataset_name: str = "traffic",
target=None, # 'Speed (mph)' or 'Occupancy (%)'
):
if dataset_name == "traffic":
if target is None:
target = "Occupancy (%)"
series = load_traffic_series(target=target, random=self.random)
else:
raise NotImplementedError(f"Dataset {dataset_name} is not supported.")
return series
def get_prediction_length(self, dataset_name: str = "traffic"):
if dataset_name == "traffic":
return get_traffic_prediction_length()
else:
raise NotImplementedError(f"Dataset {dataset_name} is not supported.")
def get_history_factor(self, dataset_name: str = "traffic"):
if dataset_name == "traffic":
return get_traffic_history_factor()
else:
raise NotImplementedError(f"Dataset {dataset_name} is not supported.")
def random_instance(self):
datasets = ["traffic"]
dataset_name = self.random.choice(datasets)
prediction_length = self.get_prediction_length(dataset_name=dataset_name)
max_attempts = 100
for _ in range(max_attempts):
full_series = self.get_series(dataset_name=dataset_name)
try:
# Select a random window
window = get_random_window_univar(
full_series,
prediction_length=prediction_length,
history_factor=self.get_history_factor(dataset_name=dataset_name),
random=self.random,
max_attempts=1, # Handle the attempts in this method instead
)
break
except ValueError:
# This exception is thrown if get_random_window_univar did not select a valid window
pass
else:
raise ValueError(
f"Could not find a valid window after {max_attempts} attempts"
)
# Extract the history and future series
history_series = window.iloc[:-prediction_length]
future_series = window.iloc[-prediction_length:]
if dataset_name == "traffic":
# Sample a starting point in the first half of the history's index
history_series.index = pd.to_datetime(history_series.index)
spike_duration = self.random.choice(
[1, 2, 3]
) # Arbitrarily picked from 1,2,3
# Arbitrary way to select a start date: sort the values of future_series (excluding the last spike_duration+1 points), pick it from the largest 5 values
spike_start_point = self.random.choice(
np.argsort(history_series.values[: -(spike_duration + 1)])[-5:][::-1]
)
spike_start_date = history_series.index[spike_start_point]
spike_type = self.random.choice([-1, 1]) # Upward spike or downward spike
spike_magnitude = (
self.random.choice([2, 3]) * history_series.max()
) # Arbitrarily set to twice or thrice the max value in the time series
# Add spike to the data
history_series.iloc[
spike_start_point : spike_start_point + spike_duration
] = (spike_type * spike_magnitude)
# Convert future index to timestamp for consistency
future_series.index = pd.to_datetime(future_series.index)
background = f"This series represents the occupancy rate (%) captured by a highway sensor."
background += f" The sensor experienced an unexpected glitch resulting in a spike starting from {datetime_to_str(spike_start_date)} for {spike_duration} {'hour' if spike_duration == 1 else 'hours'}. Assume that the sensor will not have this glitch in the future."
else:
raise NotImplementedError(f"Dataset {dataset_name} is not supported.")
# Instantiate the class variables
self.past_time = history_series.to_frame()
self.future_time = future_series.to_frame()
self.background = background
# No region of interest since the only the history is modified
class SensorMaintenanceInPredictionTask(UnivariateCRPSTask):
"""
A task where the prediction part contains zero readings for a period due to maintenance.
The maintenance periods should be reflected in the forecast.
"""
_context_sources = UnivariateCRPSTask._context_sources + ["c_cov", "c_f"]
_skills = UnivariateCRPSTask._skills + ["instruction following"]
__version__ = "0.0.3" # Modification will trigger re-caching
def random_instance(self):
# TODO: This task can use all datasets where the notion of a "sensor" is meaningful
datasets = ["traffic"]
dataset_name = self.random.choice(datasets)
if dataset_name == "electricity_hourly":
# Select a random dataset
dataset = get_dataset(dataset_name, regenerate=False)
assert len(dataset.train) == len(
dataset.test
), "Train and test sets must contain the same number of time series"
prediction_length = dataset.metadata.prediction_length
# Select a random time series
ts_index = self.random.choice(len(dataset.train))
full_series = to_pandas(list(dataset.test)[ts_index])
# Select a random window
window = get_random_window_univar(
full_series,
prediction_length=prediction_length,
history_factor=self.random.randint(3, 7),
random=self.random,
)
# Extract the history and future series
history_series = window.iloc[:-prediction_length]
future_series = window.iloc[-prediction_length:]
# Duration: between 2 and 6 hours
duration = self.random.randint(2, 7)
start_hour = self.random.randint(0, prediction_length - duration)
maintenance_start_date = future_series.index[start_hour]
maintenance_end_date = future_series.index[start_hour + duration]
# Add the maintenance period to the prediction window
future_series.index = future_series.index.to_timestamp()
future_series.iloc[start_hour : start_hour + duration] = 0
# Convert history index to timestamp for consistency
history_series.index = history_series.index.to_timestamp()
background = f"This series represents electricity consumption recordings captured by a meter."
scenario = f"Consider that the meter will be offline for maintenance between {datetime_to_str(maintenance_start_date)} and {datetime_to_str(maintenance_end_date)}, which results in zero readings."
elif dataset_name == "traffic":
prediction_length = get_traffic_prediction_length()
max_attempts = 100
for _ in range(max_attempts):
target = "Occupancy (%)"
full_series = load_traffic_series(target=target, random=self.random)
try:
# Select a random window
window = get_random_window_univar(
full_series,
prediction_length=prediction_length,
history_factor=get_traffic_history_factor(),
random=self.random,
max_attempts=1, # Handle the attempts in this method instead
)
break
except ValueError:
# This exception is thrown if get_random_window_univar did not select a valid window
pass
else:
raise ValueError(
f"Could not find a valid window after {max_attempts} attempts"
)
# Extract the history and future series
history_series = window.iloc[:-prediction_length]
future_series = window.iloc[-prediction_length:]
# Duration: between 2 and 6 hours
duration = self.random.randint(2, 7)
start_hour = self.random.randint(0, prediction_length - duration)
maintenance_start_date = future_series.index[start_hour]
maintenance_end_date = future_series.index[start_hour + duration]
# Add the maintenance period to the prediction window
future_series.iloc[start_hour : start_hour + duration] = 0
background = f"This series represents the occupancy rate (%) captured by a highway sensor."
scenario = f"Consider that the meter will be offline for maintenance between {datetime_to_str(maintenance_start_date)} and {datetime_to_str(maintenance_end_date)}, which results in zero readings."
else:
raise NotImplementedError(f"Dataset {dataset_name} is not supported.")
# Instantiate the class variables
self.past_time = history_series.to_frame()
self.future_time = future_series.to_frame()
self.background = background
self.scenario = scenario
# ROI metric parameters
self.region_of_interest = slice(start_hour, start_hour + duration)
__TASKS__ = [
SensorMaintenanceInPredictionTask,
SensorPeriodicMaintenanceTask,
SensorTrendAccumulationTask,
SensorSpikeTask,
]
__CLUSTERS__ = [
WeightCluster(
weight=1,
tasks=[
SensorMaintenanceInPredictionTask,
SensorPeriodicMaintenanceTask,
SensorTrendAccumulationTask,
SensorSpikeTask,
],
),
]