-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotting.py
261 lines (198 loc) · 10.1 KB
/
plotting.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 5 11:37:01 2019
@author: burr
"""
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from fileutils import getfilelist, readfile, adctoamp, adctovolt, ch0calib, ch1calib,appliancelist
import os
appliancelistforplots = ['No Load', 'Incandescent Light Bulb','Electric Kettle','Fan - Level 1','Fan - Level 2','Fan - Level 3','Microwave Oven','Microwave Oven','Microwave Oven','Laptop','Computer Monitor','Fluorescent Light','Smartphone-Charger', 'HP Idle', 'HP 1 Thread', 'HP 2 Threads', 'HP 3 Threads', 'Samsung Idle', 'Samsung 1 Thread', 'Samsung 2 Threads', 'Samsung 3 Threads', 'Samsung 4 Threads', 'Samsung Playing Video']
experimentlist = ['1_WithoutSwitchingEvents', '2_WithSwitchingEvents', '4_LaptopStates',
'3_MultipleDevices_Order00', '3_MultipleDevices_Order01', '3_MultipleDevices_Order02', '3_MultipleDevices_Order10', '3_MultipleDevices_Order11']
def plotforthesis(path):
timestamp_arr, ch0_arr, ch1_arr, marker_arr, label_arr, filename = readfile(path)
fig, (ax1, ax2) = plt.subplots(ncols=2, sharex=True, figsize = (10, 4))
fig.suptitle(appliancelistforplots[appliancelist.index(os.path.basename(filename).split('.')[0].split('_')[-1])])
ax1.set_xlim([0,0.04])
ax1.set_xticks([0, 0.02, 0.04])
ax1.set_title ('Voltage')
ax1.set_ylabel('[V]')
ax1.set_xlabel('Time [s]')
ax1.plot(timestamp_arr, (ch0_arr+ch0calib)*adctovolt, linewidth = 1, marker = '')
ax2.set_xticks([0, 0.02, 0.04])
ax2.set_title ('Current')
ax2.set_ylabel('[A]')
ax2.set_xlabel('Time [s]')
ax2.plot(timestamp_arr, (ch1_arr+ch1calib)*adctoamp, linewidth = 1)
current = (ch1_arr+ch1calib)*adctoamp
voltage = (ch0_arr+ch0calib)*adctovolt
fs = 10000
fig, (ax1, ax2) = plt.subplots(ncols=2, sharex=True, figsize = (10, 4))
fig.suptitle(appliancelistforplots[appliancelist.index(os.path.basename(filename).split('.')[0].split('_')[-1])])
ax1.set_title('Voltage Spectrum')
ax1.set_ylabel('Amplitude [dB]')
ax1.set_ylim((-40, 60))
ax1.set_xlim((0,1000))
ax2.set_title('Current Spectrum')
ax2.set_ylabel('Amplitude [dB]')
ax2.set_ylim((-90, 30))
plt.xlabel('Frequency [Hz]')
currentrfft = np.fft.rfft(current*np.hanning(len(current)))
currentrfft = currentrfft/len(currentrfft)*2
voltagerfft = np.fft.rfft(voltage*np.hanning(len(voltage)))
voltagerfft = voltagerfft/len(voltagerfft)*2
rfftfreqs = np.fft.rfftfreq(len(current), 1/fs)
ax1.plot(rfftfreqs, 20*np.log10(np.abs(voltagerfft)))
ax2.plot(rfftfreqs, 20*np.log10(np.abs(currentrfft)))
def plotdata(timestamp_arr, ch0_arr, ch1_arr, marker_arr, label_arr, filename):
fig, (axis1, axis2, axis3, axis4) = plt.subplots(nrows=4, sharex=True, figsize=(14,8))
# plt.ion()
fig.suptitle(filename)
axis1.set_title("Voltage")
axis1.set_ylabel('[V]')
#axis1.set_ylim([-400, 400])
axis2.set_title("Current")
axis2.set_ylabel('[A]')
#axis2.set_ylim([-10, 10])
axis3.set_title("Power")
axis3.set_ylabel('[W]')
axis1.plot(timestamp_arr, (ch0_arr+ch0calib)*adctovolt, linewidth = 1, marker = '')
axis2.plot(timestamp_arr, (ch1_arr+ch1calib)*adctoamp, linewidth = 1)
power = pd.Series(np.multiply(((ch0_arr+ch0calib)*adctovolt),((ch1_arr+ch1calib)*adctoamp)))
axis3.plot(timestamp_arr, power, linewidth = 1)
axis3.plot(timestamp_arr, power.rolling(window=int(10000/50)).mean(), linewidth = 1)
if (len(label_arr)>0):
axis2.plot(timestamp_arr,label_arr, linewidth=1)
axis2.set_title("Current and Label")
axis4.set_title("Marker")
axis4.set_ylim([-1.5, 1.5])
axis4.plot(timestamp_arr, marker_arr, linewidth = 1)
plt.pause(0.001)
plt.draw()
plt.show()
def plotfile(path):
timestamp_arr, ch0_arr, ch1_arr, marker_arr, label_arr, filename = readfile(path)
plotdata(timestamp_arr, ch0_arr, ch1_arr, marker_arr, label_arr, filename)
def plotappliance(experiment, appliance):
filelist, dirlist, namelist = getfilelist('labelled/')
for file in filelist:
if ((experiment in file) and (appliance in file)):
plotfile(file)
def plotspectogram(path, title=1):
timestamp_arr, ch0_arr, ch1_arr, marker_arr, label_arr, filename = readfile(path)
plt.ioff()
fig, ax1 = plt.subplots(nrows=1)
if (title):
plt.suptitle(appliancelistforplots[appliancelist.index(os.path.basename(filename).split('.')[0].split('_')[-1])])
current = (ch1_arr+ch1calib)*adctoamp
fs = 10000
windowsize = 1000
Pxx, freqs, bins, im = ax1.specgram(current, NFFT=windowsize,Fs=fs, mode='magnitude', scale='dB' , vmin=-130, vmax = 20)#, cmap=plt.cm.gist_heat)
ax1.set_xlabel('Time [s]')
ax1.set_ylabel('Frequency [Hz]')
fig.colorbar(im).set_label('Intensity [dB]')
scenario = os.path.basename(os.path.dirname(os.path.dirname(filename)))
scenarioinstance = os.path.basename(os.path.dirname(filename))
saveto = os.path.join('plots', 'spectrograms', scenario, scenarioinstance)
if not os.path.isdir(saveto):
os.makedirs(saveto)
plt.savefig(os.path.join(saveto, os.path.basename(filename).split('.')[0])+'.svg')
plt.close()
plt.ion()
def plotrfft(path, title=1):
plt.ioff()
timestamp_arr, ch0_arr, ch1_arr, marker_arr, label_arr, filename = readfile(path)
fig, (ax1) = plt.subplots(nrows=1)
if (title):
fig.suptitle(appliancelistforplots[appliancelist.index(os.path.basename(filename).split('.')[0].split('_')[-1])])
current = (ch1_arr+ch1calib)*adctoamp
fs = 10000
ax1.set_ylabel('Amplitude [dB]')
ax1.set_ylim((-120, 20))
plt.xlabel('Frequency [Hz]')
currentrfft = np.fft.rfft(current)
currentrfft = currentrfft/len(currentrfft)
rfftfreqs = np.fft.rfftfreq(len(current), 1/fs)
ax1.plot(rfftfreqs, 20*np.log10(np.abs(currentrfft)))
scenario = os.path.basename(os.path.dirname(os.path.dirname(filename)))
scenarioinstance = os.path.basename(os.path.dirname(filename))
saveto = os.path.join('plots', 'spectra', scenario, scenarioinstance)
if not os.path.isdir(saveto):
os.makedirs(saveto)
plt.savefig(os.path.join(saveto, os.path.basename(filename).split('.')[0])+'.svg')#, dpi=500)
plt.close()
plt.ion()
def plotappliancespectograms(experiment, appliance):
plt.close('all')
filelist, dirlist, namelist = getfilelist()
for file in filelist:
if ((experiment in file) and (appliance in file)):
if not ('order' in file):
plotspectogram(file)
else:
plotspectogram(file,title=0)
def plotappliancerffts(experiment, appliance):
plt.close('all')
filelist, dirlist, namelist = getfilelist()
for file in filelist:
if ((experiment in file) and (appliance in file)):
if not ('order' in file):
plotrfft(file)
else:
plotrfft(file,title=0)
def plotpowerandlabel(path, title=1):
filelist, dirlist, namelist = getfilelist(path)
plt.ioff()
for file in filelist:
timestamp_arr, ch0_arr, ch1_arr, marker_arr, label_arr, filename = readfile(file)
fig, (axis1, axis2) = plt.subplots(nrows=2, sharex=True)
if (title):
fig.suptitle(appliancelistforplots[appliancelist.index(os.path.basename(filename).split('.')[0].split('_')[-1])])
axis1.set_title("Power")
axis1.set_ylabel('[W]')
axis2.set_title("Label")
axis2.set_xlabel('Time [s]')
power = pd.Series(np.multiply(((ch0_arr+ch0calib)*adctovolt),((ch1_arr+ch1calib)*adctoamp)))
# axis1.plot(timestamp_arr, power, linewidth = 1)
axis1.plot(timestamp_arr, power.rolling(window=int(10000/50)).mean(), linewidth = 1, color='orange')
# axis1.set_xlim([0.1,0.3])
axis1.set_ylim([-50, 1500])
axis2.set_ylim([-1,23])
axis2.plot(timestamp_arr, label_arr, color='g')
scenario = os.path.basename(os.path.dirname(os.path.dirname(filename)))
scenarioinstance = os.path.basename(os.path.dirname(filename))
saveto = os.path.join('plots', 'powertrace', scenario, scenarioinstance)
if not os.path.isdir(saveto):
os.makedirs(saveto)
plt.savefig(os.path.join(saveto, os.path.basename(filename).split('.')[0])+'.svg')#, dpi=500)
plt.close()
plt.ion()
if __name__ == '__main__':
# for appliance in appliancelist:
# plotappliancerffts('1_WithoutSwitchingEvents', appliance)
# plotappliancerffts('2_WithSwitchingEvents', appliance)
# plotappliancerffts('4_LaptopStates', appliance)
# for appliance in appliancelist:
# plotappliancespectograms('1_WithoutSwitchingEvents', appliance)
# plotappliancespectograms('2_WithSwitchingEvents', appliance)
# plotappliancespectograms('4_LaptopStates', appliance)
# plotappliancespectograms('5_MassiveSwitching', appliance)
# plotappliancespectograms('3_MultipleDevices_Order00', 'order')
# plotappliancespectograms('3_MultipleDevices_Order01', 'order')
# plotappliancespectograms('3_MultipleDevices_Order02', 'order')
# plotappliancespectograms('3_MultipleDevices_Order10', 'order')
# plotappliancespectograms('3_MultipleDevices_Order11', 'order')
# plotpowerandlabel('labelled/1_WithoutSwitchingEvents/0108_1100')
# plotpowerandlabel('labelled/2_WithSwitchingEvents/0108_1200')
# plotpowerandlabel('labelled/4_LaptopStates/1508_1800')
# plotpowerandlabel('labelled/3_MultipleDevices_Order00', title=0)
# plotpowerandlabel('labelled/3_MultipleDevices_Order01', title=0)
# plotpowerandlabel('labelled/3_MultipleDevices_Order02', title=0)
# plotpowerandlabel('labelled/3_MultipleDevices_Order10', title=0)
# plotpowerandlabel('labelled/3_MultipleDevices_Order11', title=0)
# plotpowerandlabel('labelled/3_MultipleDevices_Order11', title=0)
# plotpowerandlabel('labelled/5_MassiveSwitching')
pass