-
Notifications
You must be signed in to change notification settings - Fork 0
/
heater.py
185 lines (152 loc) · 5.23 KB
/
heater.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
# %%
import pandas as pd
import plotly.express as px
import statsmodels.formula.api as smf
from temperature.exterior import get_ext_df
PATH_TEMP = {'interior': 'netatmo/export/netatmo_temperature_2020-01-01_2022-01-23.csv',
'exterior': 'temperature/export/daily_export'}
PATH_GAZ = "gaz/data/gaz_conso.csv"
DATE_NEW_ROOF = '2021-07-18'
heater = pd.read_csv(
'netatmo/export/netatmo_agg_boiler_data_boileron_2020-01-01_2022-01-15.csv')
heater['date'] = pd.to_datetime(heater['date'])
heater.set_index('date', inplace=True)
# %%
def read_gaz(PATH_GAZ):
"""
Read gaz data.
"""
gaz = pd.read_csv(PATH_GAZ)
gaz['date'] = pd.to_datetime(gaz['date'], format='%d/%m/%Y')
# Smart meter only available from August 2020
gaz = gaz.query("date > '2020-08-01'")
return gaz
def read_temperature(path_temp):
"""
Read temperature data.
"""
int = pd.read_csv(path_temp['interior'])[['date', 'temperature']]
int['date'] = pd.to_datetime(int['date'])
ext = get_ext_df(path_temp['exterior'])
ext['datetime'] = pd.to_datetime(ext['datetime'])
int.set_index('date', inplace=True)
ext.set_index('datetime', inplace=True)
int_d = int.resample('D').mean().reset_index()
ext_d = ext.resample('D').mean().reset_index()
ext_d.rename(columns={'datetime': 'date',
'temp': 'temperature_ext'},
inplace=True)
df = pd.merge(int_d, ext_d, on='date', how='left')
return df
def stack_temperature(PATH_TEMP):
"""
Stack temperature data.
"""
df = read_temperature(PATH_TEMP)
df.set_index('date', inplace=True)
df = df.stack().reset_index()
df.columns = ['date', 'type', 'value']
return df
def _season(x):
"""
Seasonal function.
"""
if x.month in [1, 2, 3, 4, 10, 11, 12]:
return 'winter'
else:
return 'summer'
def merge_gaz_temperature(PATH_GAZ, PATH_TEMP, season='winter'):
"""
Merge gaz and temperature.
"""
gaz = read_gaz(PATH_GAZ)
temp = read_temperature(PATH_TEMP)
df = pd.merge(gaz, temp, on='date', how='left')
df['season'] = df['date'].apply(_season)
if season == 'winter':
df = df.query("season == 'winter'")
return df
def build_energy_model(PATH_GAZ, PATH_TEMP, season):
"""
Build energy model.
"""
df = merge_gaz_temperature(PATH_GAZ, PATH_TEMP, season)
df = df.query("date < '{}'".format(DATE_NEW_ROOF))
model = smf.ols(formula='energy ~ temperature + temperature_ext', data=df)
results = model.fit()
print(results.summary())
return results
def predict_on_set(PATH_GAZ, PATH_TEMP, season, type='test'):
model = build_energy_model(PATH_GAZ, PATH_TEMP, season)
df = merge_gaz_temperature(PATH_GAZ, PATH_TEMP, season)
if type == 'test':
df = df.query("date > '{}'".format(DATE_NEW_ROOF))
df['energy_predicted'] = model.predict(
df[['temperature', 'temperature_ext']])
return df
def plot_prediction(PATH_GAZ, PATH_TEMP, season, type='test'):
"""
Plot predicted energy.
"""
df = predict_on_set(PATH_GAZ, PATH_TEMP, season, type)
df.set_index('date', inplace=True)
df_stack = df[['energy', 'energy_predicted']].stack().reset_index()
df_stack.columns = ['date', 'type', 'value']
fig = px.line(df_stack, x='date', y='value', color='type')
fig.show()
def compute_energy_savings(PATH_GAZ, PATH_TEMP, season='winter', type='all'):
"""
What's the difference between the energy consumption and the energy predicted?
"""
df = predict_on_set(PATH_GAZ, PATH_TEMP, season, type)
df['diff'] = df['energy'] - df['energy_predicted']
savings_since_roof = df.query(
"date > '{}'".format(DATE_NEW_ROOF))['diff'].sum()
print("Savings since roof: {} kwh".format(savings_since_roof))
df['month_year'] = df['date'].apply(lambda x: x.strftime('%m-%Y'))
savings = df.groupby('month_year').agg({'diff': 'sum'}).reset_index()
savings['date'] = pd.to_datetime(savings['month_year'], format="%m-%Y")
savings.sort_values('date', inplace=True)
return savings
# %%
plot_prediction(PATH_GAZ, PATH_TEMP, season='winter', type='all')
# %%
# mod = smf.ols(formula='energy ~ boileron', data=merge_gaz_heater(heater, gaz))
# res = mod.fit()
# print(res.summary())
# # %%
# px.line(heater.resample('D').agg({'boileron': 'sum'}).reset_index(),
# x='date',
# y='boileron',
# )
# # %%
# px.scatter(merge_gaz_heater(heater, gaz),
# x='energy',
# y='boileron',
# hover_data=['date'])
# %%
def merge_gaz_heater(heater, gaz):
"""
Merge boiler and gaz datasets.
"""
heater_d = heater.resample('D').agg({'boileron': 'sum'}).reset_index()
df = pd.merge(gaz, heater_d, on='date', how='left')
return df
def stack_gaz_heater(heater, gaz):
"""
Merge boiler and gaz datasets.
"""
df = merge_gaz_heater(heater, gaz)
df.set_index('date', inplace=True)
df = df.stack().reset_index()
df.columns = ['date', 'type', 'value']
return df
def plot_gaz_heater(heater, gaz):
"""
Plot gaz and heater.
"""
df = stack_gaz_heater(heater, gaz)
fig = px.bar(df, x='date', y='value', facet_row='type', color='type')
fig.update_yaxes(matches=None)
fig.show()
# %%