-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroomalyzer.py
290 lines (250 loc) · 7.27 KB
/
roomalyzer.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
279
280
281
282
283
284
285
286
287
288
289
290
"""
Roomalyzer is a Python module for visualizing conditions in your room.
"""
import tomllib
from pathlib import Path
import numpy as np
import pandas as pd
import requests
import plotly.graph_objects as go
class Roomalyzer:
"""Roomalyzer class"""
data = pd.DataFrame()
dehum_log = pd.DataFrame()
@property
def temp(self):
"""Column name for data access
Returns
-------
str
Column name
"""
return "temperature"
@property
def hum(self):
"""Column name for data access
Returns
-------
str
Column name
"""
return "humidity"
@property
def date(self):
"""Column name for data access
Returns
-------
str
Column name
"""
return "date"
__consts = {}
def __init__(self):
with open("constants.toml", "rb") as file:
self.__consts = tomllib.load(file)
@property
def summary(self):
"""Data summary - global max, min, mean
Returns
-------
pd.DataFrame
Dataframe with calculated summary
"""
return (
self.data.agg(
{self.temp: ["min", "max", "mean"], self.hum: ["min", "max", "mean"]}
)
.round(2)
.reset_index()
)
def read_thingspeak(self, url: str):
"""Reads data from a ThingSpeak server
Parameters
----------
url : str
URL to ThingSpeak server
"""
df = (
pd.json_normalize(requests.get(url, timeout=1000).json()["feeds"])
.replace("NAN", np.nan)
.dropna()
)
names = {"created_at": self.date, "field1": self.temp, "field2": self.hum}
df = df.rename(columns=names)
df[self.date] = pd.to_datetime(df[self.date])
for item in [self.temp, self.hum]:
df[item] = pd.to_numeric(df[item])
df = df[df[self.temp] != 0]
df = df[df[self.hum] != 0]
self.data = df
def read_dehumidifier_log(self, path: str, sep=","):
"""Reads file with dehumidifier on and off time, in csv format.
Parameters
----------
path : Path
Path to .csv file
sep : str, optional
csv file separator, by default ","
"""
try:
df = pd.read_csv(Path(path), sep=sep)
df["Date"] = pd.to_datetime(df["Date"])
on = df[df["State"] == "on"]["Date"].rename("on").reset_index()
off = df[df["State"] == "off"]["Date"].rename("off").reset_index()
df = pd.concat([on, off], axis=1)
self.dehum_log = df
except FileNotFoundError:
print("No dehumidifier log found. Omitting")
def dehumidifier_state_to_bool(self):
"""Converts dehumidifier state from 'on' to 1 and 'off' to 0"""
df = self.dehum_log.copy()
df["State_b"] = np.where(df["State"] == "on", 1, 0)
self.dehum_log = df
def calc_average_vals(self, frequency: str, offset: str):
"""Calculates average values
Parameters
----------
frequency : str
Can be day, month, week
offset : str
Shift regarding chosen frequency
Returns
-------
pd.DataFrame
Dataframe with averaged values
"""
return (
self.data.groupby(pd.Grouper(key=self.date, freq=frequency, offset=offset))
.mean()
.reset_index()
.join(
self.data.groupby(
pd.Grouper(key=self.date, freq=frequency, offset=offset) # type: ignore
) # type: ignore
.agg("std")
.reset_index(),
rsuffix="_std",
)
.dropna()
)
def check_humidity_levels(self):
"""Checks if humidity levels are off limits, adds a new column
Returns
-------
None
"""
df = self.data.copy()
def conditions(val):
if val >= self.__consts["humidity_levels"]["high"]:
return 1
if val <= self.__consts["humidity_levels"]["low"]:
return -1
return 0
func = np.vectorize(conditions)
df["hum_lvl"] = func(df[self.hum])
self.data = df
def add_subplot_chart(
self,
figure: go.Figure,
y_data: pd.Series,
row: int,
col: int,
x_data=None,
name=None,
errorbars=None,
):
"""Adds a subplot chart
Parameters
----------
figure : go.Figure
Figure to attach to
y_data : pd.Series
Data to put on Y axis
row : int
Row in subplot grid
col : int
Column in subplot grid
x_data : pd.Series, optional
Data to put on X axis, by default date and time
name : str, optional
Name of the chart, by default None
errorbars : pd.Series, optional
Data to create error bars, by default None
Returns
-------
go.Figure
Figure
"""
if x_data is None:
x_data = self.data[self.date]
if name is None:
name = y_data.name
figure.append_trace(
go.Scatter(
x=x_data,
y=y_data,
name=name,
error_y={"type": "data", "array": errorbars},
),
row=row,
col=col,
)
return figure
def create_chart(self, y_data: pd.Series, x_data=None, name=None):
"""Create an indepentend chart
Parameters
----------
y_data : pd.Series
Data to put on Y axis
x_data : pd.Series, optional
Data to put on X axis, by default None
name : str, optional
Name of the chart, by default None
Returns
-------
go.Figure
Figure with chart
"""
if x_data is None:
x_data = self.data[self.date]
fig = go.Figure(data=go.Scatter(x=x_data, y=y_data, name=name))
return fig
def add_chart(
self,
figure: go.Figure,
y_data: pd.Series,
x_data=None,
name=None,
errorbars=None,
):
"""Append chart to an existing figure
Parameters
----------
figure : go.Figure
Figure to append
y_data : pd.Series
Data to put on Y axis
x_data : pd.Series, optional
Data to put on X axis, by default None
name : str, optional
Name of the chart, by default None
errorbars : pd.Series, optional
Data to create error bars, by default None
Returns
-------
go.Figure
Figure
"""
if x_data is None:
x_data = self.data[self.date]
figure = figure.add_trace(
go.Scatter(
go.Scatter(
x=x_data,
y=y_data,
name=name,
error_y={"type": "data", "array": errorbars},
)
)
)
return figure