-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
278 lines (191 loc) · 9.73 KB
/
test.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
from __future__ import print_function, division
import numpy as np
import pandas as pd
from os import *
from os.path import *
import nilmtk
from nilmtk import DataSet, TimeFrame, MeterGroup, HDFDataStore
from nilmtk.disaggregate import CombinatorialOptimisation
from nilmtk.utils import print_dict
from nilmtk.metrics import f1_score
from nilmtk.datastore import Key
from nilmtk.measurement import LEVEL_NAMES
from nilmtk.utils import check_directory_exists
from nilm_metadata import *
from inspect import currentframe, getfile, getsourcefile
from sys import getfilesystemencoding
import cProfile
import time
import warnings
warnings.filterwarnings("ignore")
def test_all_datasets(directory):
print("Testing all data sets started at: {}".format(time.now()))
print("-"*60)
check_directory_exists(directory)
datasets = [f for f in listdir(directory) if isfile(join(directory, f)) and
'.h5' in f and '.swp' not in f]
for dataset in datasets:
test_single_dataset(dataset)
def test_single_dataset(dataset):
print("Testing all function of {} dataset started at: {}".format(dataset, time.now()))
print("-"*60)
test_all_buildings(dataset)
test_metadata_dataset(dataset)
def test_all_buildings(dataset):
buildings = dataset.buildings
for building in buildings:
test_single_building(building)
def test_single_building(building):
test_single_building_metadata(building)
elec = building.elec
test_single_meter_group(elec)
def test_single_building_metadata(building):
try:
print(building.metadata)
except:
# log it..
try:
print(building.describe)
except:
# log it
def test_single_meter_group(elec):
return None
def test_metadata_dataset(dataset):
try:
print(dataset.metadata)
except Exception as e:
# Do something....maybe log it somewhere
def test_all_elecmeters(metergroup):
return None
def test_elecmeter(elecmeter):
return None
def test_all_meter_groups():
return None
def test_single_mete
"""
files = [f for f in listdir(path_to_directory) if isfile(join(path_to_directory, f)) and
'.h5' in f and '.swp' not in f]
files.sort()
print ("Datasets collected and sorted. Processing...")
try:
for i, file in enumerate(files):
current_file=DataSet(join(path_to_directory, file))
print ("Printing metadata for current file...done.")
print_dict(current_file.metadata)
print (" Loading file # ", i, " : ", file, ". Please wait.")
for building_number in range(1, len(current_file.buildings)+1):
#Examine metadata for a single house
elec=current_file.buildings[building_number].elec
print ("The dataset being processed is : ", elec.dataset())
print ("Metadata for current file: ")
print_dict(current_file.buildings[building_number].metadata)
print ("Appliance label information: ", elec.appliance_label())
#print (elec.appliances)
print ("Appliances:- ")
for i in elec.appliances:
print (i)
print ("Examining sub-metered appliances...")
print ("Collecting stats on meters...Done.")
print (elec._collect_stats_on_all_meters)
print ("Timeframe: ", elec.get_timeframe())
print ("Available power AC types: ", elec.available_power_ac_types())
print ("Clearing cache...done.")
elec.clear_cache()
print ("Testing if there are meters from multiple buildings. Result returned by method: ", elec.contains_meters_from_multiple_buildings())
# TODO: Find a better way to test the correlation function
# print ("Testing the correlation function. ", elec.correlation(elec))
print ("List of disabled meters: ", elec.disabled_meters)
print ("Trying to determine the dominant appliance: ")
try:
elec.dominant_appliance()
except RuntimeError:
print ('''More than one dominant appliance in MeterGroup! (The dominant appliance per meter should be manually specified in the metadata. If it isn't and if there are multiple appliances for a meter then NILMTK assumes all appliances on that meter are dominant. NILMTK can't automatically distinguish between multiple appliances on the same meter (at least, not without using NILM!))''')
pass
print ("Dropout rate: ", elec.dropout_rate())
try:
print ("Calculating energy per meter:")
print (elec.energy_per_meter())
print ("Calculating total entropy")
print (elec.entropy())
print ("Calculating entropy per meter: ")
print (elec.entropy_per_meter())
except ValueError:
print ("ValueError: Total size of array must remain unchanged.")
pass
print ("Calculating fraction per meter.")
print (elec.fraction_per_meter())
#print ("Average energy per period: ", elec.average_energy_per_period())
print ("Executing functions...")
lis=[]
func=""
'''for function in dir(elec):
try:
start=time.time()
if ("__" not in function or "dataframe_of_meters" not in function):
func=getattr(elec, function)
print ("Currently executing ", function, ". Please wait...")
print (func())
# print ("cProfile stats - printed")
# cProfile.run("func")
end=time.time()
print ("Time taken for the entire process : ", (end - start))
except AttributeError:
print ("Attribute error occured. ")
except TypeError:
lis.append(function)
print ("Warning: TypeError")
pass'''
print ("Plotting wiring hierarchy of meters....")
elec.draw_wiring_graph()
## DISAGGREGATION STARTS HERE
appliance_type="unknown"
#TODO : appliance_type should cycle through all appliances and check for each of them. For this, use a list.
selected_appliance=nilmtk.global_meter_group.select_using_appliances(type=appliance_type)
appliance_restricted = MeterGroup(selected_appliance.meters)
if ((appliance_restricted.proportion_of_upstream_total_per_meter()) is not None):
proportion_per_appliance = appliance_restricted.proportion_of_upstream_total_per_meter()
proportion_per_appliance.plot(kind='bar');
plt.title('Appliance energy as proportion of total building energy');
plt.ylabel('Proportion');
plt.xlabel('Appliance (<appliance instance>, <building instance>, <dataset name>)');
selected_appliance.select(building=building_number).total_energy()
selected_appliance.select(building=1).plot();
appliance_restricted = MeterGroup(selected_appliance.meters)
daily_energy = pd.DataFrame([meter.average_energy_per_period(offset_alias='D')
for meter in appliance_restricted.meters])
daily_energy.plot(kind='hist');
plt.title('Histogram of daily energy');
plt.xlabel('energy (kWh)');
plt.ylabel('Occurences');
plt.legend().set_visible(False)
current_file.store.window=TimeFrame(start='2012-04-01 00:00:00-05:00', end='2012-04-02 00:00:00-05:00')
#elec.plot();
fraction = elec.submeters().fraction_per_meter().dropna()
labels = elec.get_appliance_labels(fraction.index)
plt.figure(figsize=(8,8))
fraction.plot(kind='pie', labels=labels);
elec.select_using_appliances(category='heating')
elec.select_using_appliances(category='single-phase induction motor')
co = CombinatorialOptimisation()
co.train(elec)
for model in co.model:
print_dict(model)
disag_filename = join(data_dir, 'ampds-disag.h5')
output = HDFDataStore(disag_filename, 'w')
co.disaggregate(elec.mains(), output)
output.close()
disag = DataSet(disag_filename)
disag_elec = disag.buildings[building_number].elec
f1 = f1_score(disag_elec, elec)
f1.index = disag_elec.get_appliance_labels(f1.index)
f1.plot(kind='bar')
plt.xlabel('appliance');
plt.ylabel('f-score');
disag_elec.plot()
disag.store.close()
except AttributeError:
print ("AttributeError occured while executing. This means that the value returned by proportion_per_appliance = appliance_restricted.proportion_of_upstream_total_per_meter() is None")
pass
test_all('/Users/rishi/Documents/Master_folder/IIITD/5th_semester/Independent_Project/NILMTK_datasets')
#test_all('/Users/rishi/Documents/Master_folder/IIITD/5th_semester/Independent_Project/nilmtk/data/Location_of_h5')
"""