forked from Yueeeeeeee/BERT4NILM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
348 lines (279 loc) · 13.6 KB
/
dataset.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
from abc import *
from config import *
from dataloader import *
import numpy as np
import pandas as pd
from pathlib import Path
from collections import defaultdict
import torch.utils.data as data_utils
class AbstractDataset(metaclass=ABCMeta):
def __init__(self, args, stats=None):
self.house_indicies = args.house_indicies
self.appliance_names = args.appliance_names
self.normalize = args.normalize
self.sampling = args.sampling
self.cutoff = [args.cutoff[i]
for i in ['aggregate'] + self.appliance_names]
self.threshold = [args.threshold[i] for i in self.appliance_names]
self.min_on = [args.min_on[i] for i in self.appliance_names]
self.min_off = [args.min_off[i] for i in self.appliance_names]
self.val_size = args.validation_size
self.window_size = args.window_size
self.window_stride = args.window_stride
self.x, self.y = self.load_data()
self.status = self.compute_status(self.y)
print('Appliance:', self.appliance_names)
print('Sum of ons:', np.sum(self.status, axis=0))
print('Total length:', self.status.shape[0])
if stats is None:
self.x_mean = np.mean(self.x, axis=0)
self.x_std = np.std(self.x, axis=0)
else:
self.x_mean, self.x_std = stats
self.x = (self.x - self.x_mean) / self.x_std
@classmethod
@abstractmethod
def code(cls):
pass
@classmethod
def raw_code(cls):
return cls.code()
@abstractmethod
def load_data(self):
pass
def get_data(self):
return self.x, self.y, self.status
def get_original_data(self):
x_org = self.x * self.x_std + self.x_mean
return x_org, self.y, self.status
def get_mean_std(self):
return self.x_mean, self.x_std
def compute_status(self, data):
status = np.zeros(data.shape)
if len(data.squeeze().shape) == 1:
columns = 1
else:
columns = data.squeeze().shape[-1]
if not self.threshold:
self.threshold = [10 for i in range(columns)]
if not self.min_on:
self.min_on = [1 for i in range(columns)]
if not self.min_off:
self.min_off = [1 for i in range(columns)]
for i in range(columns):
initial_status = data[:, i] >= self.threshold[i]
status_diff = np.diff(initial_status)
events_idx = status_diff.nonzero()
events_idx = np.array(events_idx).squeeze()
events_idx += 1
if initial_status[0]:
events_idx = np.insert(events_idx, 0, 0)
if initial_status[-1]:
events_idx = np.insert(
events_idx, events_idx.size, initial_status.size)
events_idx = events_idx.reshape((-1, 2))
on_events = events_idx[:, 0].copy()
off_events = events_idx[:, 1].copy()
assert len(on_events) == len(off_events)
if len(on_events) > 0:
off_duration = on_events[1:] - off_events[:-1]
off_duration = np.insert(off_duration, 0, 1000)
on_events = on_events[off_duration > self.min_off[i]]
off_events = off_events[np.roll(
off_duration, -1) > self.min_off[i]]
on_duration = off_events - on_events
on_events = on_events[on_duration >= self.min_on[i]]
off_events = off_events[on_duration >= self.min_on[i]]
assert len(on_events) == len(off_events)
temp_status = data[:, i].copy()
temp_status[:] = 0
for on, off in zip(on_events, off_events):
temp_status[on: off] = 1
status[:, i] = temp_status
return status
def get_status(self):
return self.status
def get_datasets(self):
val_end = int(self.val_size * len(self.x))
val = NILMDataset(self.x[:val_end], self.y[:val_end], self.status[:val_end],
self.window_size, self.window_size)
train = NILMDataset(self.x[val_end:], self.y[val_end:], self.status[val_end:],
self.window_size, self.window_stride)
return train, val
def get_bert_datasets(self, mask_prob=0.25):
val_end = int(self.val_size * len(self.x))
val = NILMDataset(self.x[:val_end], self.y[:val_end], self.status[:val_end],
self.window_size, self.window_size)
train = BERTDataset(self.x[val_end:], self.y[val_end:], self.status[val_end:],
self.window_size, self.window_stride, mask_prob=mask_prob)
return train, val
def _get_rawdata_root_path(self):
return Path(RAW_DATASET_ROOT_FOLDER)
def _get_folder_path(self):
root = self._get_rawdata_root_path()
return root.joinpath(self.raw_code())
class REDD_LF_Dataset(AbstractDataset):
@classmethod
def code(cls):
return 'redd_lf'
@classmethod
def _if_data_exists(self):
folder = Path(RAW_DATASET_ROOT_FOLDER).joinpath(self.code())
first_file = folder.joinpath('house_1', 'channel_1.dat')
if first_file.is_file():
return True
return False
def load_data(self):
for appliance in self.appliance_names:
assert appliance in ['dishwasher',
'refrigerator', 'microwave', 'washer_dryer']
for house_id in self.house_indicies:
assert house_id in [1, 2, 3, 4, 5, 6]
if not self.cutoff:
self.cutoff = [6000] * (len(self.appliance_names) + 1)
if not self._if_data_exists():
print('Please download, unzip and move data into',
self._get_folder_path())
raise FileNotFoundError
else:
directory = self._get_folder_path()
for house_id in self.house_indicies:
house_folder = directory.joinpath('house_' + str(house_id))
house_label = pd.read_csv(house_folder.joinpath(
'labels.dat'), sep=' ', header=None)
main_1 = pd.read_csv(house_folder.joinpath(
'channel_1.dat'), sep=' ', header=None)
main_2 = pd.read_csv(house_folder.joinpath(
'channel_2.dat'), sep=' ', header=None)
house_data = pd.merge(main_1, main_2, how='inner', on=0)
house_data.iloc[:, 1] = house_data.iloc[:,
1] + house_data.iloc[:, 2]
house_data = house_data.iloc[:, 0: 2]
appliance_list = house_label.iloc[:, 1].values
app_index_dict = defaultdict(list)
for appliance in self.appliance_names:
data_found = False
for i in range(len(appliance_list)):
if appliance_list[i] == appliance:
app_index_dict[appliance].append(i + 1)
data_found = True
if not data_found:
app_index_dict[appliance].append(-1)
if np.sum(list(app_index_dict.values())) == -len(self.appliance_names):
self.house_indicies.remove(house_id)
continue
for appliance in self.appliance_names:
if app_index_dict[appliance][0] == -1:
temp_values = house_data.copy().iloc[:, 1]
temp_values[:] = 0
temp_data = house_data.copy().iloc[:, :2]
temp_data.iloc[:, 1] = temp_values
else:
temp_data = pd.read_csv(house_folder.joinpath(
'channel_' + str(app_index_dict[appliance][0]) + '.dat'), sep=' ', header=None)
if len(app_index_dict[appliance]) > 1:
for idx in app_index_dict[appliance][1:]:
temp_data_ = pd.read_csv(house_folder.joinpath(
'channel_' + str(idx) + '.dat'), sep=' ', header=None)
temp_data = pd.merge(
temp_data, temp_data_, how='inner', on=0)
temp_data.iloc[:, 1] = temp_data.iloc[:,
1] + temp_data.iloc[:, 2]
temp_data = temp_data.iloc[:, 0: 2]
house_data = pd.merge(
house_data, temp_data, how='inner', on=0)
house_data.iloc[:, 0] = pd.to_datetime(
house_data.iloc[:, 0], unit='s')
house_data.columns = ['time', 'aggregate'] + \
[i for i in self.appliance_names]
house_data = house_data.set_index('time')
house_data = house_data.resample(self.sampling).mean().fillna(
method='ffill', limit=30)
if house_id == self.house_indicies[0]:
entire_data = house_data
else:
entire_data = entire_data.append(
house_data, ignore_index=True)
entire_data = entire_data.dropna().copy()
entire_data = entire_data[entire_data['aggregate'] > 0]
entire_data[entire_data < 5] = 0
entire_data = entire_data.clip(
[0] * len(entire_data.columns), self.cutoff, axis=1)
return entire_data.values[:, 0], entire_data.values[:, 1:]
class UK_DALE_Dataset(AbstractDataset):
@classmethod
def code(cls):
return 'uk_dale'
@classmethod
def _if_data_exists(self):
folder = Path(RAW_DATASET_ROOT_FOLDER).joinpath(self.code())
first_file = folder.joinpath('house_1', 'channel_1.dat')
if first_file.is_file():
return True
return False
def load_data(self):
for appliance in self.appliance_names:
assert appliance in ['dishwasher', 'fridge',
'microwave', 'washing_machine', 'kettle']
for house_id in self.house_indicies:
assert house_id in [1, 2, 3, 4, 5]
if not self.cutoff:
self.cutoff = [6000] * (len(self.appliance_names) + 1)
if not self._if_data_exists():
print('Please download, unzip and move data into',
self._get_folder_path())
raise FileNotFoundError
else:
directory = self._get_folder_path()
for house_id in self.house_indicies:
house_folder = directory.joinpath('house_' + str(house_id))
house_label = pd.read_csv(house_folder.joinpath(
'labels.dat'), sep=' ', header=None)
house_data = pd.read_csv(house_folder.joinpath(
'channel_1.dat'), sep=' ', header=None)
house_data.iloc[:, 0] = pd.to_datetime(
house_data.iloc[:, 0], unit='s')
house_data.columns = ['time', 'aggregate']
house_data = house_data.set_index('time')
house_data = house_data.resample(self.sampling).mean().fillna(
method='ffill', limit=30)
appliance_list = house_label.iloc[:, 1].values
app_index_dict = defaultdict(list)
for appliance in self.appliance_names:
data_found = False
for i in range(len(appliance_list)):
if appliance_list[i] == appliance:
app_index_dict[appliance].append(i + 1)
data_found = True
if not data_found:
app_index_dict[appliance].append(-1)
if np.sum(list(app_index_dict.values())) == -len(self.appliance_names):
self.house_indicies.remove(house_id)
continue
for appliance in self.appliance_names:
if app_index_dict[appliance][0] == -1:
house_data.insert(len(house_data.columns), appliance, np.zeros(len(house_data)))
else:
temp_data = pd.read_csv(house_folder.joinpath(
'channel_' + str(app_index_dict[appliance][0]) + '.dat'), sep=' ', header=None)
temp_data.iloc[:, 0] = pd.to_datetime(
temp_data.iloc[:, 0], unit='s')
temp_data.columns = ['time', appliance]
temp_data = temp_data.set_index('time')
temp_data = temp_data.resample(self.sampling).mean().fillna(
method='ffill', limit=30)
house_data = pd.merge(
house_data, temp_data, how='inner', on='time')
if house_id == self.house_indicies[0]:
entire_data = house_data
if len(self.house_indicies) == 1:
entire_data = entire_data.reset_index(drop=True)
else:
entire_data = entire_data.append(
house_data, ignore_index=True)
entire_data = entire_data.dropna().copy()
entire_data = entire_data[entire_data['aggregate'] > 0]
entire_data[entire_data < 5] = 0
entire_data = entire_data.clip(
[0] * len(entire_data.columns), self.cutoff, axis=1)
return entire_data.values[:, 0], entire_data.values[:, 1:]