This repository was archived by the owner on Aug 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_analysis.py
More file actions
419 lines (370 loc) · 17 KB
/
Copy pathsession_analysis.py
File metadata and controls
419 lines (370 loc) · 17 KB
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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# time:2021/6/16
# EVSense: Xudong Wang, Guoming Tang
# Start this file in 20210615
# Update in 20210618, adjust the figure size.
import os
import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def EV_session_analysis(df, resident_id, time_period, saved_path, verbose=False, label_bins_num=3, label_num_std=10):
"""
Start Package this analysis in 20210616
Input the df with the index is the localminute(in Pandas DataFrame.datetime format)
Auto-saved the EV-sessons analysis at the target dictionary path.
df: Input data Frame
saved_path: string, path
return the EV session analysis dataframe and relabel the EV charging event.
"""
print("=" * 50)
print(f"EV Charging sessions analysis for resident {resident_id}")
# figure 1 EV_session_meter_histogram
plt.figure()
n, bins, patches = plt.hist(df['car1'], bins="doane") # default is bins="auto"
plt.title(f"resident {resident_id} EV Meter records (per min) Histogram")
plt.xlabel("Power: Kwh")
plt.ylabel('Frequency')
plt.savefig(saved_path + f'{resident_id}_EV_session_meter_histogram.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
# Analysis the distribution and relabel it.
diff = np.diff(bins)[0]
print('The value interval for histgram bins is', diff)
print(f"Current histogram bins for determinant EV charging session label is {label_bins_num}")
# Using the bin to relabel the EV charging sessions.
top_bins_idx = np.argsort(n)[-label_bins_num:][::-1]
thresh = np.sort([bins[top_bins_idx[0]], bins[top_bins_idx[1]], bins[top_bins_idx[2]]])
thresh_0, thresh_1, thresh_2 = thresh
# pts_0 = df['car1'][(df['car1'] >= thresh_0) & (df['car1'] <= (thresh_0 + diff))] #Bottom bin
pts_0 = df['car1'][
(df['car1'] >= thresh_1) & (df['car1'] <= (thresh_1 + 2 * diff))] # Middle bin + another
pts_1 = df['car1'][
(df['car1'] >= thresh_2) & (df['car1'] <= (thresh_2 + diff))] # Top bin
mean_0 = np.mean(pts_0)
mean_1 = np.mean(pts_1)
std_0 = np.std(pts_0)
std_1 = np.std(pts_1)
print(f"OFF: mean:{mean_0} Kw, std:{std_0}")
print(f"ON: mean:{mean_1} Kw, std:{std_1}")
print(f"Using {label_bins_num} bins threshold:", thresh_0, thresh_1, thresh_2)
print(f"Current {label_num_std} times std of the OFF state (Upper bound) to determine the EV charging sessions:",
mean_0 + label_num_std * std_0)
# relabel the car label
df['label'] = np.ones(len(df))
df['label'].loc[(df['car1'] <= (mean_0 + label_num_std * std_0))] = 0
# figure 2+ EV_session_visualization
if time_period:
start = pd.Timestamp(datetime.datetime.strptime(time_period['start'], '%Y-%m-%d'))
end = pd.Timestamp(datetime.datetime.strptime(time_period['end'], '%Y-%m-%d'))
df.sort_index(inplace=True)
temp = df.loc[start:end]
fig, ax1 = plt.subplots(figsize=(15, 5))
ax2 = ax1.twinx()
ax1.plot(temp['label'], 'r-')
ax2.plot(temp['car1'], 'b--')
plt.title(f'resident: {resident_id} EV charging sessions visualization')
ax1.set_ylabel('Label for EV Charging Event', color='r')
ax2.set_ylabel('Car', color='b')
plt.savefig(saved_path + f'{resident_id}_EV_charging_sessions_visualization.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
plt.figure(figsize=(14, 7))
df['car1'].loc['2018-03-20':'2018-03-21'].plot(figsize=(14, 7), style=['-'])
(df['car1'] * df['label']).loc['2018-03-20':'2018-03-21'].plot(figsize=(14, 7), style=['ro'])
# plt.title(f"EV charging session for resident {resident_id} with label (Red dots)")
plt.ylabel('Kw')
plt.savefig(saved_path + f'{resident_id}_EV_charging_sessions_visualization_demo.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
else:
fig, ax1 = plt.subplots(figsize=(15, 5))
ax2 = ax1.twinx()
ax1.plot(df['label'], 'r-')
ax2.plot(df['car1'], 'b--')
plt.title(f'resident: {resident_id} EV Charging sessions visualization')
ax1.set_ylabel('Label for EV Charging Event', color='r')
ax2.set_ylabel('Car', color='b')
plt.savefig(saved_path + f'{resident_id}_EV_Charging_sessions_visualization.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
# figure 3 EV Charging_agg_vs_car curve
plt.figure(figsize=(18, 5))
df.filter(['aggregate', 'car1']).plot()
plt.title(f'resident {resident_id} Aggregate Power Vs EV charging Power')
plt.ylabel('Kw')
plt.savefig(saved_path + f'{resident_id}_EV_charging_agg_vs_car.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
# Analysis the Detail EV session and return the EV session
print("=" * 50)
ses_df = get_EV_charging_session(df, resident_id, saved_path, verbose, duration_fig_num=15)
# Analysis the start and end charging session time.
plt.figure(figsize=(6, 6))
ax = ses_df['startTime'].dt.tz_localize('UTC').dt.tz_convert("America/Chicago").dt.hour.plot.hist(bins=24,
title=f"Histogram of startTime Hour of Day for resident {resident_id}")
ax.set_xlabel("Hour of Day")
plt.savefig(saved_path + f'{resident_id}_EV_charging_startTime_Histogram.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
plt.figure(figsize=(6, 6))
ax = ses_df['endTime'].dt.tz_localize('UTC').dt.tz_convert("America/Chicago").dt.hour.plot.hist(bins=24,
title=f"Histogram of endTime Hour of Day for resident {resident_id}")
ax.set_xlabel("Hour of Day")
plt.savefig(saved_path + f'{resident_id}_EV_charging_endTime_Histogram.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
# Start vs End time
from sklearn.cluster import DBSCAN
X = [ses_df['startTime'].dt.tz_localize('UTC').dt.tz_convert("America/Chicago").dt.hour + ses_df[
'startTime'].dt.minute * 1 / 60,
ses_df['endTime'].dt.tz_localize('UTC').dt.tz_convert("America/Chicago").dt.hour + ses_df[
'endTime'].dt.minute * 1 / 60]
X = np.array(X).T
model = DBSCAN(eps=2, min_samples=int(np.round(0.1 * X.shape[0])), random_state=0)
y = model.fit_predict(X)
# print(y)
plt.figure(figsize=(6, 6))
COLOR = ['green', 'red', 'blue', 'black', 'gray']
for idx in range(len(y)):
plt.scatter(X[idx, 0], X[idx, 1], color=COLOR[y[idx]])
plt.xlabel("startTime Hour of Day")
plt.ylabel("endTime Hour of Day")
plt.title(f"startTime vs. endTime (Hour of Day) for resident {resident_id}")
plt.plot([0, 24], [0, 24], 'k-')
plt.savefig(saved_path + f'{resident_id}_EV_charging_start_vs_endTime.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
# Histogram of energy per charge
plt.figure(figsize=(6, 6))
ax = ses_df['energy'].plot.hist(bins=24, title=f"Histogram of Energy per Charge for resident {resident_id}")
ax.set_xlabel("Energy per Charge (kWh)")
plt.savefig(saved_path + f'{resident_id}_EV_charging_energy_percharge.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
# Histogram of the energy for month and season
plt.figure(figsize=(6, 6))
monthCounter = [sum(ses_df['startTime'].dt.tz_localize('UTC').dt.tz_convert("America/Chicago").dt.month == m) for m
in range(1, 13)]
plt.bar(range(1, 13), monthCounter)
plt.xlabel('Month')
plt.ylabel('Frequency')
plt.title(f'Histogram of Energy monthly Charge for resident {resident_id}')
plt.savefig(saved_path + f'{resident_id}_EV_charging_frequency_monthly.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
plt.figure(figsize=(6, 6))
seasonCounter = [sum(monthCounter[0:3]), sum(monthCounter[3:6]), sum(monthCounter[6:9]), sum(monthCounter[9:])]
plt.bar(["Winter", "Spring", "Summer", "Autumn"], seasonCounter)
plt.ylabel('Frequency')
plt.title(f'Histogram of Energy seasonal Charge for resident {resident_id}')
plt.savefig(saved_path + f'{resident_id}_EV_charging_frequency_seasonal.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
plt.figure(figsize=(6, 6))
dayOfWeekCounter = [
sum(ses_df['startTime'].dt.tz_localize('UTC').dt.tz_convert("America/Chicago").dt.dayofweek == dow) / 52 for dow
in range(0, 7)]
plt.bar(["M", "Tu", "W", "Th", "F", "Sa", "Su"], dayOfWeekCounter)
plt.ylabel('Frequency')
plt.title(f'Histogram of Energy week Charge for resident {resident_id}')
plt.savefig(saved_path + f'{resident_id}_EV_charging_frequency_weekly.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
plt.figure(figsize=(14, 7))
ses_df.boxplot(column='energy', by='startHour', figsize=(14, 7))
plt.savefig(saved_path + f'{resident_id}_EV_charging_energybystartHour.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
return ses_df
def get_EV_charging_session(df, resident_id, saved_path, verbose=False, duration_fig_num=15):
ses_df = pd.DataFrame(columns=['power', 'startTime', 'endTime',
'duration', 'peakPower', 'tailDuration', 'energy'])
sessions = []
startTime = []
endTime = []
duration = []
start_idx = df.index[0]
end_idx = df.index[0]
prev_state = df['label'][0]
for idx, row in df.iterrows():
state = row['label']
if state != prev_state:
if state == 1:
start_idx = idx - pd.Timedelta('1 min')
df['label'].loc[start_idx] = 1
else:
end_idx = idx
sessions.append(df['car1'].loc[start_idx:end_idx])
startTime.append(start_idx)
endTime.append(end_idx)
duration.append(end_idx - start_idx)
start_idx = end_idx
prev_state = state
ses_df['power'] = sessions
ses_df['startTime'] = startTime
ses_df['endTime'] = endTime
ses_df['duration'] = duration
for idx, row in ses_df.iterrows():
ses_df['peakPower'].loc[idx] = np.max(row['power'])
ses_df['energy'].loc[idx] = np.trapz(row['power'],
row['power'].index.astype(np.int64) / 10 ** 9) / 3.6e3 # [kWh]
ses_df_orig = ses_df.copy()
print("Plot the preliminary figure of the EV charging sessions.")
plt.figure()
ses_df['duration'].dt.total_seconds().div(60).astype(int).plot.hist(bins=50, title=f"{resident_id}: duration")
plt.savefig(saved_path + f'{resident_id}_EV_charging_sessions_duration_origin.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
plt.figure()
ses_df['energy'].plot.hist(bins=50, title=f"{resident_id}: energy")
plt.savefig(saved_path + f'{resident_id}_EV_charging_sessions_energy_origin.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
plt.figure()
ses_df['peakPower'].plot.hist(bins=50, title=f"{resident_id}: peakPower")
plt.savefig(saved_path + f'{resident_id}_EV_charging_sessions_peakPower_origin.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
# Remove some outliers session.
print("Plot the figures of the EV charging sessions after remove outliers.")
ses_df = ses_df_orig.copy()
prevLen = len(ses_df_orig)
ses_df = ses_df[ses_df['peakPower'] > 1.2]
print("{} sessions removed ({}%), {} remaining".format(prevLen - len(ses_df.index),
np.round((prevLen - len(ses_df.index)) / prevLen, 2),
len(ses_df.index)))
prevLen = len(ses_df)
ses_df = ses_df[ses_df['energy'] >= 0.1]
print("{} sessions removed ({}%), {} remaining".format(prevLen - len(ses_df.index),
np.round((prevLen - len(ses_df.index)) / prevLen, 2),
len(ses_df.index)))
prevLen = len(ses_df)
ses_df = ses_df[ses_df['duration'].dt.total_seconds() >= 120]
print("{} sessions removed ({}%), {} remaining".format(prevLen - len(ses_df.index),
np.round((prevLen - len(ses_df.index)) / prevLen, 2),
len(ses_df.index)))
plt.figure()
ses_df['duration'].dt.total_seconds().div(60).astype(int).plot.hist(bins=50, title=f"{resident_id}: duration")
plt.savefig(saved_path + f'{resident_id}_EV_charging_sessions_duration_cleaned.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
plt.figure()
ses_df['energy'].plot.hist(bins=50, title=f"{resident_id}: energy")
plt.savefig(saved_path + f'{resident_id}_EV_charging_sessions_energy_cleaned.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
plt.figure()
ses_df['peakPower'].plot.hist(bins=50, title=f"{resident_id}: peakPower")
plt.savefig(saved_path + f'{resident_id}_EV_charging_sessions_peakPower_cleaned.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
# Analysis the EV charging session.
window = 5
ses_df['tailBin'] = None
for idx, row in ses_df.iterrows():
convolve = np.convolve(row['power'], np.ones(window) * 1 / window, mode='valid')
diff = np.diff(convolve)
thresh_idxs = np.where(diff < -1e-1)[0]
thresh_idxs = thresh_idxs[np.where(thresh_idxs > np.round(len(row['power']) * 0.1))[0]]
if len(thresh_idxs) == 0:
print(diff)
tailStart = int(np.round(len(row['power']) * 0.9))
else:
tailStart = thresh_idxs[0]
tailDuration = len(row['power']) - tailStart
ses_df['tailDuration'].loc[idx] = tailDuration
try:
tail = row['power'].values[tailStart:]
numBins = 5
binWidth = tail[0] / numBins
tailBin = [0]
for B in reversed(range(1, numBins)):
tailBin.append(np.where(tail >= B * binWidth)[0][-1] / tailDuration)
tailBin.append(1)
ses_df['tailBin'].loc[idx] = tailBin
except IndexError as e:
print("IndexError Details : " + str(e))
pass
continue
# Plot the EV Charging sessions tailDuration
plt.figure()
ses_df['tailDuration'].plot.hist(bins=50)
plt.title(f"{resident_id}: Charging session's tailDuration")
plt.savefig(saved_path + f'{resident_id}_EV_charging_sessions_tailDuration.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
plt.figure()
ses_df['tailDuration'][ses_df['tailDuration'] < 150].plot.hist(bins=50)
plt.title(f"{resident_id}: Charging session's tailDuration")
plt.savefig(saved_path + f'{resident_id}_EV_charging_sessions_tailDuration_cleaned.pdf', format='pdf')
if verbose:
plt.show()
plt.close()
# plot the tail duration for the EV
lim = duration_fig_num
for idx, row in ses_df[ses_df['tailDuration'] < 8].iterrows():
if lim <= 0:
break
try:
tail = row['power'][len(row['power']) - int(row['tailDuration']):]
plt.figure()
ses_df['power'].loc[idx].plot(
title="duration={}, tailDuration={}".format(row['duration'], row['tailDuration']))
tail.plot(style='r.-')
plt.savefig(saved_path + f'{resident_id}_EV_charging_sessions_tailDuration_Short_duration_{lim}.pdf',
format='pdf')
if verbose:
plt.show()
plt.close()
except IndexError as e:
print("IndexError Details : " + str(e))
pass
lim -= 1
continue
lim = duration_fig_num
for idx, row in ses_df[ses_df['tailDuration'] >= 8].iterrows():
if lim <= 0:
break
try:
tail = row['power'][len(row['power']) - int(row['tailDuration']):]
plt.figure()
ses_df['power'].loc[idx].plot(
title="duration={}, tailDuration={}".format(row['duration'], row['tailDuration']))
tail.plot(style='r.-')
plt.savefig(saved_path + f'{resident_id}_EV_charging_sessions_tailDuration_Long_duration_{lim}.pdf',
format='pdf')
if verbose:
plt.show()
plt.close()
except IndexError as e:
print("IndexError Details : " + str(e))
pass
lim -= 1
continue
ses_df['startHour'] = ses_df['startTime'].dt.tz_localize('UTC').dt.tz_convert("America/Chicago").dt.hour
ses_df['endHour'] = ses_df['endTime'].dt.tz_localize('UTC').dt.tz_convert("America/Chicago").dt.hour
ses_df.to_csv(saved_path + f'{resident_id}_EV_charging_sessions_analysis.csv', encoding='utf-8', index=False)
# print("Saved Done!")
return ses_df