-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_import_ashrae.py
369 lines (296 loc) · 14.5 KB
/
data_import_ashrae.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
import numpy as np
import pandas as pd
class DataImportAshrae():
"""
class provides different methods to import BDG2 data
for experiments with Discord Detectors
"""
def __init__(self):
"""
method initializes df_all_data
"""
self.df_all_data = None
def get_meter_data( self, energy_type_list: list, site_list: list,
verbose=False):
"""
method returns a sorted NxM dataframe with M buildings and N
rows with hourly timestamp as indices
Keyword arguments:
energy_type_list -- List with the requested meter types
site_list -- List with the requested site ids
verbose -- enable debugging printing (default False)
Returns:
df_result -- dataframe (NxM) (N = #timestamps, M = #buildings)
with readings at corresponding times at corresponding buildings
"""
df_site_meter = self._prepare_and_filter_raw_data( energy_type_list,
site_list)
filter_col = ['meter_reading']
df_result = self._build_desired_df( df_site_meter,
filter_col,
addBuildingID= True,
addDiscordID= False)
return df_result
def get_daily_profiles( self, meter_type=0, site_list=[]):
"""
method returns a sorted Nx24 dataframe with M buildings and 24
features (hourly readings) and hourly timestamp as indices
Keyword arguments:
meter_type --Integer with the requested meter types
site_list -- List with the requested site ids
Returns:
df_result -- dataframe (NxM) (N = #buildings x days, 24 = hourly
readings)
"""
df_site_meter = DataImportAshrae().get_meter_data([meter_type], site_list)
building_list = df_site_meter.columns
og_idx = df_site_meter.reset_index()["timestamp"].copy()
df_site_meter = df_site_meter.reset_index(drop=True)
day_list = []
bdg_list = []
idx_list = []
for bid in building_list:
for i in range(0, df_site_meter.shape[0], 24):
day = df_site_meter.loc[i:i+23, bid]
day_list.append(day)
idx_list.append(og_idx[i])
bdg_list.append(bid)
df_all = pd.DataFrame(np.stack(day_list, axis=0), index=idx_list)
df_all['building_id'] = bdg_list
# remove days with nan values
df_all = df_all.dropna(axis=0, how='any')
assert df_all.isnull().values.any() == False
return df_all
def get_labeled_meter_data( self, energy_type_list: list, site_list: list,
verbose=False):
"""
method returns a sorted Nx(2*M) dataframe with M buildings,
M corresponding labels (is_discord?) and N rows with
hourly timestamp as indices
Keyword arguments:
energy_type_list -- List with the requested meter types
site_list -- List with the requested site ids
verbose -- enable debugging printing (default False)
Returns:
df_result -- dataframe (Nx(2*M)) (N = #timestamps, M = #buildings)
with readings at corresponding times at corresponding buildings
and a label that describes if the reading is a discord
"""
df_site_meter = self._prepare_and_filter_raw_data( energy_type_list,
site_list)
filter_col = ['meter_reading', 'is_discord']
df_result = self._build_desired_df( df_site_meter,
filter_col,
addBuildingID= True,
addDiscordID= True)
return df_result
def get_label_data(self, energy_type_list, site_list, verbose=False):
"""
method returns a sorted NxM dataframe with M buildings and
N rows with hourly timestamp as indices
Keyword arguments:
energy_type_list -- List with the requested meter types
site_list -- List with the requested site ids
verbose -- enable debugging printing (default False)
Returns:
df_result -- dataframe (NxM) (N = #timestamps, M = #buildings)
with labels (is_discord?) at corresponding times
at corresponding buildings
"""
df_site_meter = self._prepare_and_filter_raw_data( energy_type_list,
site_list)
filter_col = ['is_discord']
df_result = self._build_desired_df( df_site_meter,
filter_col,
addBuildingID= False,
addDiscordID= True)
return df_result
def get_meta_data(self, verbose= False):
"""
method returns a dataframe with metadata from the BDG2 dataset
Keyword arguments:
verbose -- enable debugging printing (default False)
Returns:
df_result -- metadata from BDG2
"""
if self.df_all_data is None:
self.df_all_data = self._get_raw_data().copy()
df_result = self.df_all_data.copy()
df_result = df_result.filter([ 'site_id',
'building_id',
'primary_use',
'square_feet',
'year_built',
'floor_count',],
axis=1)
df_result = df_result.drop_duplicates(subset=[ 'site_id',
'building_id',])
return df_result
def get_timestamps(self, verbose= False):
"""
Method returns a dataframe with all reading times
Keyword arguments:
verbose -- enable debugging printing (default False)
Returns:
df_result -- dataframe with all reading times
"""
if self.df_all_data is None:
self.df_all_data = self._get_raw_data().copy()
df_result = self.df_all_data.copy()
df_result = df_result.filter(['timestamp'], axis=1)
df_result = df_result.drop_duplicates()
return df_result
def get_timestamps_buildings(self, resolution='H'):
"""
TODO
"""
assert (resolution in ['H', 'D']), ('Make sure that the '
'resolution is either "H" '
'(hourly) or "D" (daily)')
if self.df_all_data is None:
self.df_all_data = self._get_raw_data().copy()
df_result = self.df_all_data.copy()
df_result = df_result.filter(['timestamp',
'building_id',
'meter',
], axis=1)
if resolution == 'D':
df_result['date'] = df_result['timestamp'].dt.date
df_result = df_result.drop(['timestamp'], axis=1)
df_result = df_result.drop_duplicates()
df_result = df_result.rename(columns={'date': 'timestamp'})
df_result['timestamp'] = pd.to_datetime(df_result['timestamp'])
assert (True not in df_result.duplicated().unique()), (
'Something went wrong. At this point, duplicates must'
'no longer appear in the dataframe df_result!')
return df_result
def get_vacation_data(self, site_id:int, verbose=False):
"""
TODO
"""
excel_vacations = pd.ExcelFile(r'data/holidays/Great Energy Predictor III Schedule Data Collection.xlsx')
dict_site_sheet = {
1:{'id':1,
'name':'site1',
'sheet':'University College London',
},
2:{'id':2,
'name':'site2',
'sheet':'Arizona State',
},
4:{'id':4,
'name':'site4',
'sheet':'University of California Berkel',
},
14:{'id':14,
'name':'site14',
'sheet':'Princeton University',
},
15:{'id':15,
'name':'site15',
'sheet':'Cornell',
},
}
list_available_sites = list(dict_site_sheet.keys())
# check if the request is legal
assert site_id in list_available_sites, "Only vacation data for sites 1, 2, 4, 14 and 15 can be exported"
# select necessary data
df_vacations = pd.read_excel(excel_vacations, dict_site_sheet[site_id]['sheet'])
list_columns = list(df_vacations.columns)
list_columns.remove('Date')
# calculate is_normal and is_discord column
df_vacations['Label 1'] = df_vacations['Label 1'].astype('category')
df_vacations['is_normal'] = pd.get_dummies(df_vacations,
columns=['Label 1']
)['Label 1_Regular']
assert (df_vacations['is_normal'].sum()
== df_vacations[df_vacations['Label 1'] == 'Regular'].shape[0])
df_vacations['is_discord'] = df_vacations['is_normal'].replace({0:1, 1:0})
# clean up dataframe
df_vacations = df_vacations.drop(list_columns, axis=1)
df_vacations = df_vacations.drop('is_normal', axis=1)
# filtering the necessary data
df_vacations = df_vacations.set_index('Date')
df_vacations = df_vacations.loc['2016-01-01':'2016-12-31']
return df_vacations
def _get_raw_data(self, verbose= False):
assert self.df_all_data is None, "The data has already been loaded"
# prepare base data
#(ashrae energy predictor + winning solution s data)
str_path_prefix = 'data/ashrae-energy-prediction/'
df_meters = pd.read_csv(str_path_prefix + 'train.csv',
parse_dates= True)
df_weather = pd.read_csv( str_path_prefix + 'weather_train.csv',
parse_dates= True)
df_metadata = pd.read_csv( str_path_prefix + 'building_metadata.csv',
parse_dates= True)
df_discord_labels = pd.read_csv('data/outliers/bad_meter_readings.csv',
dtype=
{'is_bad_meter_reading': np.int64})
df_discord_labels = df_discord_labels.rename(
columns={'is_bad_meter_reading': 'is_discord'})
# merging the files
df_all_data = df_meters.merge( df_metadata,
on= 'building_id', how= 'left')
df_all_data = df_all_data.merge(df_weather,
on= ['site_id', 'timestamp'],
how= 'left')
df_all_data = df_all_data.merge(df_discord_labels, left_index=True,
right_index=True, how='outer')
df_all_data.timestamp = pd.to_datetime(df_all_data.timestamp)
return df_all_data
def _filter_sites(self, df_base, site_list, verbose=False):
df_reduced = df_base[(df_base['site_id'].isin(site_list))]
return df_reduced
def _filter_energy_type(self, df_base, energy_type_list, verbose=False):
df_reduced = df_base[(df_base['meter'].isin(energy_type_list))]
return df_reduced
def _correction_power(self, df_base, verbose=False):
assert not df_base[(df_base['meter'] == 0)
& (df_base['site_id'] == 0)].empty,(
"Nothing needs to be corrected in this section of the data")
# from the rank-1 solution:
# https://github.com/buds-lab/ashrae-great-energy-predictor-3-solution-analysis/blob/master/solutions/rank-1/scripts/02_preprocess_data.py
df_base[(df_base['meter']== 0)
& (df_base['site_id']== 0)].meter_reading.mul(0.2931)
return df_base
def _prepare_and_filter_raw_data( self, energy_type_list: list,
site_list: list):
if self.df_all_data is None:
self.df_all_data = self._get_raw_data()
df_site_meter = self._filter_sites(self.df_all_data.copy(), site_list)
df_site_meter = self._filter_energy_type(df_site_meter, energy_type_list)
if (0 in energy_type_list) and (0 in site_list):
df_site_meter = self._correction_power(df_site_meter)
return df_site_meter
def _build_desired_df( self, df_site_meter, filter_col: list,
addBuildingID=False, addDiscordID=False):
df_timestamps = pd.DataFrame(
{'timestamp': self.df_all_data.timestamp.unique()})
collector_list = [df_timestamps.set_index('timestamp')]
# restructure
for building in df_site_meter.building_id.unique():
# filter all data for one building
curr_building = df_site_meter[(df_site_meter['building_id']
== building)]
# ensure that all dates are taken into account
curr_building = curr_building.merge(df_timestamps, how='outer',
on='timestamp')
# selection of the wanted data and assignment of suitable names
curr_building = curr_building.filter( ['timestamp'] + filter_col,
axis=1)
if addBuildingID:
curr_building = curr_building.rename(
columns={'meter_reading': building})
if addDiscordID:
curr_building['is_discord'] = curr_building['is_discord'].fillna(1)
curr_building['is_discord'] = curr_building['is_discord'].astype('int64')
curr_building = curr_building.rename(
columns={'is_discord':
('is_discord_' + str(building))})
curr_building = curr_building.set_index('timestamp')
# appending all buildings
collector_list.append(curr_building)
df_result = pd.concat(collector_list, axis=1)
df_result = df_result.sort_index()
return df_result