Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.17 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Sep 11 11:55:08 2017
  4.  
  5. @author: thanvaf
  6. """
  7.  
  8. ### Libraries ###
  9. from influxdb import InfluxDBClient
  10. import pandas as pd
  11. from datetime import datetime, timedelta
  12. import numpy as np
  13. import matplotlib.pyplot as plt
  14. from itertools import islice
  15. import time
  16. import math
  17. from pytz import all_timezones
  18. import glob, os
  19. from collections import Counter
  20. from sklearn.model_selection import GridSearchCV
  21. from sklearn.tree import DecisionTreeRegressor
  22. from sklearn.svm import SVR
  23. from sklearn.metrics import f1_score, precision_score, recall_score, classification_report
  24.  
  25. #import statsmodels.api as sm
  26. #from statsmodels.sandbox.regression.predstd import wls_prediction_std
  27.  
  28. ### Modules ###
  29. def roundTime(dt=None, roundTo=60):
  30. """Round a datetime object to any time laps in seconds
  31. dt : datetime.datetime object, default now.
  32. roundTo : Closest number of seconds to round to, default 1 minute.
  33. Author: Thierry Husson 2012 - Use it as you want but don't blame me.
  34. """
  35. if dt == None : dt = datetime.datetime.now()
  36. seconds = (dt.replace(tzinfo=None) - dt.min).seconds
  37. rounding = (seconds+roundTo/2) // roundTo * roundTo
  38. return dt + timedelta(0,rounding-seconds,-dt.microsecond)
  39.  
  40. def datetime_range(start, end, delta):
  41. current = start
  42. while current < end:
  43. yield current
  44. current += delta
  45. #==============================================================================
  46. # Database parameters
  47. #==============================================================================
  48. localhost = 'smarthome.iti.gr'
  49. port = 8086
  50. username = 'root'
  51. password = 'root'
  52. databaseN = 'e32dc40831f441bc94f0f3830b1954a0'
  53. assignmentToken0 = [['76080e05-a7c1-46fe-a6c8-28349165c996','mx:W_S'],['67ade86d-deea-40a9-84e9-d16c968fedc9','mx:W_L'],['7d4e8313-f362-481d-b030-1865c2c02b27','mx:consumption'],
  54. ['e9210e17-a691-4bab-b878-69a278c185b9','mx:consumption'],['5b7387c0-b8a6-48be-a74d-7e497e6bdaa4','mx:consumption']]
  55. #measurement = 'mx:consumption'
  56. time = 'time'
  57. #==============================================================================
  58. # Dates parameters
  59. #==============================================================================
  60. N = 10
  61. currentDate=datetime.utcnow()
  62. #endDate=(currentDate - timedelta(days = 1)).strftime('%Y-%m-%dT23:59:59.999Z')
  63. endDate=(currentDate).strftime('%Y-%m-%dT23:59:59.999Z')
  64. startDate=(currentDate - timedelta(days = N)).strftime('%Y-%m-%dT00:00:00.000Z')
  65. #==============================================================================
  66. # Retrieve data from database based on specified parameters
  67. #==============================================================================
  68. for i in range(0,5):
  69. client = InfluxDBClient(host = localhost, port = 8086, username = 'root', password = 'root', database = databaseN)
  70. q = ("""SELECT "{0}","{1}" FROM events WHERE assignment = '{2}' AND time>='{3}' AND time<='{4}'""".format(time, assignmentToken0[i][1], assignmentToken0[i][0],startDate,endDate))
  71. #q = ("""SELECT "{0}" FROM events WHERE assignment = '{1}' AND time>='{2}T07:00:00Z' AND time<='{3}T13:51:00Z'""".format(measurement,assignmentToken1,start_date,end_date))
  72. df = pd.DataFrame(client.query(q, chunked=True).get_points())
  73. size = df.shape
  74. #==============================================================================
  75. # Time series of interest
  76. #==============================================================================
  77. if i == 0:
  78. # Groud Floor Total
  79. gftTimeSeries = df[assignmentToken0[i][1]].astype(float)
  80. gftTimeStamp = df[time]
  81. elif i == 1:
  82. # Oven
  83. oTimeSeries = df[assignmentToken0[i][1]].astype(float)
  84. oTimeStamp = df[time]
  85. elif i == 2:
  86. # Fridge
  87. fTimeSeries = df[assignmentToken0[i][1]].astype(float)
  88. fTimeStamp = df[time]
  89. elif i == 3:
  90. # Dish Washer
  91. dwTimeSeries = df[assignmentToken0[i][1]].astype(float)
  92. dwTimeStamp = df[time]
  93. elif i == 4:
  94. # Cooking Hood
  95. chTimeSeries = df[assignmentToken0[i][1]].astype(float)
  96. chTimeStamp = df[time]
  97.  
  98.  
  99.  
  100. # =============================================================================
  101. # A C T I V I T Y M O N I T O R I N G
  102. # =============================================================================
  103.  
  104.  
  105.  
  106. # =============================================================================
  107. # Change the timestamp to the Europe/Athens timezone
  108. # =============================================================================
  109. # Convert the timestamp into a pandas dataframe
  110. groundfl_date = pd.to_datetime(gftTimeStamp).dt.tz_localize('UTC').dt.tz_convert('Europe/Athens')
  111. oven_date = pd.to_datetime(oTimeStamp).dt.tz_localize('UTC').dt.tz_convert('Europe/Athens')
  112. fridge_date = pd.to_datetime(fTimeStamp).dt.tz_localize('UTC').dt.tz_convert('Europe/Athens')
  113. dishwasher_date = pd.to_datetime(dwTimeStamp).dt.tz_localize('UTC').dt.tz_convert('Europe/Athens')
  114. cooking_hood_date = pd.to_datetime(chTimeStamp).dt.tz_localize('UTC').dt.tz_convert('Europe/Athens')
  115.  
  116.  
  117. ground_floor = pd.concat([groundfl_date, gftTimeSeries], axis=1)
  118. oven = pd.concat([oven_date, oTimeSeries], axis=1)
  119. fridge = pd.concat([fridge_date, fTimeSeries], axis=1)
  120. dishwasher = pd.concat([dishwasher_date, dwTimeSeries], axis=1)
  121. cooking_hood = pd.concat([cooking_hood_date, chTimeSeries], axis=1)
  122.  
  123. # Convert pandas into a txt file with tab seperator
  124. ground_floor.to_csv(r'C:\Users\anasvaf\Desktop\Energy_SmartHome\total_consumption.txt', header=None, index=None, sep='\t')
  125. oven.to_csv(r'C:\Users\anasvaf\Desktop\Energy_SmartHome\oven_consumption.txt', header=None, index=None, sep='\t')
  126. fridge.to_csv(r'C:\Users\anasvaf\Desktop\Energy_SmartHome\fridge_consumption.txt', header=None, index=None, sep='\t')
  127. dishwasher.to_csv(r'C:\Users\anasvaf\Desktop\Energy_SmartHome\dishwasher_consumption.txt', header=None, index=None, sep='\t')
  128. cooking_hood.to_csv(r'C:\Users\anasvaf\Desktop\Energy_SmartHome\cooking_hood_consumption.txt', header=None, index=None, sep='\t')
  129.  
  130. # =============================================================================
  131. # Select particular series for processing
  132. # =============================================================================
  133. # Total power consumption
  134. ts = pd.read_csv("total_consumption.txt", header=None, sep='\t')
  135. ts['time'] = ts[0]
  136. tmpstmp = np.array(ts.time)
  137. ts['mx:W_S'] = ts[1]
  138. ts.drop(ts.columns[:2], axis=1,inplace=True)
  139.  
  140. # Oven power consumption
  141. ts1 = pd.read_csv("oven_consumption.txt", header=None, sep='\t')
  142. ts1['time'] = ts1[0]
  143. tmpstmp1 = np.array(ts1.time)
  144. ts1['mx:W_L'] = ts1[1]
  145. ts1.drop(ts1.columns[:2], axis=1,inplace=True)
  146.  
  147. # Fridge power consumption
  148. ts2 = pd.read_csv("fridge_consumption.txt", header=None, sep='\t')
  149. ts2['time'] = ts2[0]
  150. tmpstmp2 = np.array(ts2.time)
  151. ts2['mx:consumption'] = ts2[1]
  152. ts2.drop(ts2.columns[:2], axis=1,inplace=True)
  153.  
  154. # Dishwasher power consumption
  155. ts3 = pd.read_csv("dishwasher_consumption.txt", header=None, sep='\t')
  156. ts3['time'] = ts3[0]
  157. tmpstmp3 = np.array(ts3.time)
  158. ts3['mx:consumption'] = ts3[1]
  159. ts3.drop(ts3.columns[:2], axis=1,inplace=True)
  160.  
  161. # Cooking hood power consumption
  162. ts4 = pd.read_csv("cooking_hood_consumption.txt", header=None, sep='\t')
  163. ts4['time'] = ts4[0]
  164. tmpstmp4 = np.array(ts4.time)
  165. ts4['mx:consumption'] = ts4[1]
  166. ts4.drop(ts4.columns[:2], axis=1,inplace=True)
  167.  
  168.  
  169. # =============================================================================
  170. # Process for the time
  171. # =============================================================================
  172. ts['time'] = pd.to_datetime(ts['time'])
  173. ts.set_index(keys='time', inplace=True)
  174. ts.info() # display info for the time series
  175.  
  176. ts1['time'] = pd.to_datetime(ts1['time'])
  177. ts1.set_index(keys='time', inplace=True)
  178. ts1.info() # display info for the time series
  179.  
  180. ts2['time'] = pd.to_datetime(ts2['time'])
  181. ts2.set_index(keys='time', inplace=True)
  182. ts2.info() # display info for the time series
  183.  
  184. ts3['time'] = pd.to_datetime(ts3['time'])
  185. ts3.set_index(keys='time', inplace=True)
  186. ts3.info() # display info for the time series
  187.  
  188. ts4['time'] = pd.to_datetime(ts4['time'])
  189. ts4.set_index(keys='time', inplace=True)
  190. ts4.info() # display info for the time series
  191.  
  192. # rename 2nd column to energy_consumed. DO NOT FORGET TO CHANGE THE LABEL FOR EACH TOKEN (depends on the appliance)
  193. ts.rename(columns={'mx:W_S':'energy_consumed'}, inplace=True) # ground floor total
  194. ts1.rename(columns={'mx:W_L':'energy_consumed'}, inplace=True) # oven
  195. ts2.rename(columns={'mx:consumption':'energy_consumed'}, inplace=True) # fridge
  196. ts3.rename(columns={'mx:consumption':'energy_consumed'}, inplace=True) # dishwasher
  197. ts4.rename(columns={'mx:consumption':'energy_consumed'}, inplace=True) # cooking hood
  198.  
  199. # =============================================================================
  200. # Plot power consumptions for one day
  201. # =============================================================================
  202. # Total ground floor
  203. ts_day1 = ts[datetime(2017, 9, 5):datetime(2017, 9, 6)]
  204. ts_day1.plot(figsize=(10,8), color='r')
  205. plt.xlabel('Time (s)')
  206. plt.ylabel('Energy Consumed (Watts)')
  207. plt.title('Total Ground Floor Energy Consumed vs. Time')
  208.  
  209. # Oven
  210. ts1_day1 = ts1[datetime(2017, 9, 5):datetime(2017, 9, 6)]
  211. ts1_day1.plot(figsize=(10,8), color='r')
  212. plt.xlabel('Time (s)')
  213. plt.ylabel('Energy Consumed (Watts)')
  214. plt.title('Oven Energy Consumed vs. Time')
  215.  
  216. # Fridge
  217. ts2_day1 = ts2[datetime(2017, 9, 5):datetime(2017, 9, 6)]
  218. ts2_day1.plot(figsize=(10,8), color='r')
  219. plt.xlabel('Time (s)')
  220. plt.ylabel('Energy Consumed (Watts)')
  221. plt.title('Fridge Energy Consumed vs. Time')
  222.  
  223. # Dishwasher
  224. ts3_day1 = ts3[datetime(2017, 9, 5):datetime(2017, 9, 6)]
  225. ts3_day1.plot(figsize=(10,8), color='r')
  226. plt.xlabel('Time (s)')
  227. plt.ylabel('Energy Consumed (Watts)')
  228. plt.title('Dishwasher Energy Consumed vs. Time')
  229.  
  230. # Cooking hood (plotting for the September 11 since there is no data before)
  231. ts4_day1 = ts4[datetime(2017, 9, 11):datetime(2017, 9, 12)]
  232. ts4_day1.plot(figsize=(10,8), color='r')
  233. plt.xlabel('Time (s)')
  234. plt.ylabel('Energy Consumed (Watts)')
  235. plt.title('Cooking Hood Energy Consumed vs. Time')
  236.  
  237. # =============================================================================
  238. # Plot power consumption for the first nine days
  239. # =============================================================================
  240. # Total ground floor
  241. ts_week1 = ts[datetime(2017, 9, 5):datetime(2017, 9, 14)]
  242. ts_week1.plot(figsize=(10,8), color='r')
  243. plt.xlabel('Time (s)')
  244. plt.ylabel('Energy Consumed (Watts)')
  245. plt.title('Total Ground Floor Energy Consumed vs. Time')
  246.  
  247. # Oven
  248. ts1_week1 = ts1[datetime(2017, 9, 5):datetime(2017, 9, 14)]
  249. ts1_week1.plot(figsize=(10,8), color='r')
  250. plt.xlabel('Time (s)')
  251. plt.ylabel('Energy Consumed (Watts)')
  252. plt.title('Oven Energy Consumed vs. Time')
  253.  
  254. # Fridge
  255. ts2_week1 = ts2[datetime(2017, 9, 5):datetime(2017, 9, 14)]
  256. ts2_week1.plot(figsize=(10,8), color='r')
  257. plt.xlabel('Time (s)')
  258. plt.ylabel('Energy Consumed (Watts)')
  259. plt.title('Fridge Energy Consumed vs. Time')
  260.  
  261. # Dishwasher
  262. ts3_week1 = ts3[datetime(2017, 9, 5):datetime(2017, 9, 14)]
  263. ts3_week1.plot(figsize=(10,8), color='r')
  264. plt.xlabel('Time (s)')
  265. plt.ylabel('Energy Consumed (Watts)')
  266. plt.title('Dishwasher Energy Consumed vs. Time')
  267.  
  268. # Cooking hood
  269. ts4_week1 = ts4[datetime(2017, 9, 5):datetime(2017, 9, 14)]
  270. ts4_week1.plot(figsize=(10,8), color='r')
  271. plt.xlabel('Time (s)')
  272. plt.ylabel('Energy Consumed (Watts)')
  273. plt.title('Cooking Hood Energy Consumed vs. Time')
  274.  
  275. # =============================================================================
  276. # Power consumption difference (daily)
  277. # =============================================================================
  278. delta_ts_day1 = ts_day1.diff(1)[1:]
  279. delta_ts_day1.plot(figsize=(10,8), color='r')
  280. plt.xlabel('Time')
  281. plt.ylabel('Energy Consumed (Watts)')
  282. plt.title('Total Ground Floor Energy Consumed vs. Time')
  283.  
  284. delta_ts1_day1 = ts1_day1.diff(1)[1:]
  285. delta_ts1_day1.plot(figsize=(10,8), color='r')
  286. plt.xlabel('Time')
  287. plt.ylabel('Energy Consumed (Watts)')
  288. plt.title('Oven Energy Consumed vs. Time')
  289.  
  290. delta_ts2_day1 = ts2_day1.diff(1)[1:]
  291. delta_ts2_day1.plot(figsize=(10,8), color='r')
  292. plt.xlabel('Time')
  293. plt.ylabel('Energy Consumed (Watts)')
  294. plt.title('Fridge Energy Consumed vs. Time')
  295.  
  296. delta_ts3_day1 = ts3_day1.diff(1)[1:]
  297. delta_ts3_day1.plot(figsize=(10,8), color='r')
  298. plt.xlabel('Time')
  299. plt.ylabel('Energy Consumed (Watts)')
  300. plt.title('Dishwasher Energy Consumed vs. Time')
  301.  
  302. delta_ts4_day1 = ts4_day1.diff(1)[1:]
  303. delta_ts4_day1.plot(figsize=(10,8), color='r')
  304. plt.xlabel('Time')
  305. plt.ylabel('Energy Consumed (Watts)')
  306. plt.title('Cooking Hood Energy Consumed vs. Time')
  307.  
  308.  
  309. # =============================================================================
  310. # Define a sliding window for data processing with 50% overlap
  311. # =============================================================================
  312. def window(seq, n=2):
  313. it = iter(seq)
  314. result = tuple(islice(it, n))
  315. if len(result) == n:
  316. yield result
  317. for elem in it:
  318. result = result[1:] + (elem,)
  319. yield result
  320.  
  321. # =============================================================================
  322. # Create a matrix for the energy_consumed
  323. # =============================================================================
  324. matrix = []
  325.  
  326. for ts_i in window(ts.energy_consumed, 30):
  327. matrix.append(ts_i)
  328.  
  329. matrix = np.array(matrix)
  330. matrix.shape
  331.  
  332. # =============================================================================
  333. # Create a matrix for the dates
  334. # =============================================================================
  335. dates_matrix = []
  336.  
  337. for time in window(ts.index, 30):
  338. dates_matrix.append(time)
  339.  
  340. dates_matrix = np.array(dates_matrix)
  341. dates_matrix.shape
  342.  
  343. ## =============================================================================
  344. ## Define the labels for disaggregation (based on the total consumption)
  345. ## =============================================================================
  346. #devices = ['fridge', 'cooking hood', 'oven', 'dishwasher', 'unknown']
  347. #labels = []
  348. #for ts_i in matrix:
  349. # delta_ts_i = np.diff(ts_i)
  350. # if all(ts_i < 1000): #fridge
  351. # label = 1
  352. # elif all((ts_i > 1001) & (ts_i < 1500)): #Oven
  353. # label = 2
  354. # elif any((delta_ts_i <= -1500) | (delta_ts_i >= 1500)) and all(ts_i < 3000): #dishwasher
  355. # label = 3
  356. # elif any(ts_i > 2000): #cooking hood
  357. # label = 4
  358. # else: #Other Appliances (Unknown)
  359. # label = 5
  360. # labels.append(label)
  361. #labels = np.array(labels)
  362. #
  363. #
  364. #
  365. ## Count instances for each label
  366. #d = Counter(labels)
  367. #print (d)
  368.  
  369. # =============================================================================
  370. # Normalize the data
  371. # =============================================================================
  372. # Z-normalization
  373. def znormalization(ts):
  374. mus = ts.mean(axis = 0)
  375. stds = ts.std(axis = 0)
  376. return (ts - mus) / stds
  377.  
  378. zzzz = znormalization(ts)
  379.  
  380. LABELS = [
  381. "COOKING",
  382. "WASHING DISHES",
  383. "USING THE FRIDGE"
  384. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement