This repository was archived by the owner on Nov 28, 2021. It is now read-only.
forked from atlefren/pytilt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
executable file
·158 lines (127 loc) · 5.46 KB
/
plot.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
#!/usr/bin/env python3
import matplotlib
matplotlib.use('Agg')
import argparse
import imghdr
import os
import smtplib
import time
import datetime
from email.message import EmailMessage
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates
from subprocess import Popen, PIPE
FIGSIZE = (15, 6)
# https://stackoverflow.com/questions/4931376/generating-matplotlib-graphs-without-a-running-x-server
# https://matplotlib.org/gallery/text_labels_and_annotations/date.html
# https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots
# https://matplotlib.org/api/dates_api.html#matplotlib.dates.MonthLocator
# https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot
# https://matplotlib.org/tutorials/introductory/pyplot.html
def meanr(x):
# ignore NaN (blank fields in the CSV
return round(np.nanmean(x), 1)
def medianr(x):
# ignore NaN (blank fields in the CSV
return round(np.nanmedian(x), 1)
def get_data(options):
data = pd.read_csv(options.data_file, names=['color', 'epoch', 'iso', 'sg', 'c', 'f', 'n'],
index_col='epoch')
data['time'] = pd.to_datetime(data['iso'])
data['date'] = data['time'].dt.date
data['c'] = round(data['c'], 1)
# aggregated by date
columns = [min, meanr, medianr, max]
date_data = data.groupby('date').agg({'sg': columns,
'c': columns}).rename(columns={'meanr': 'mean', 'medianr': 'mdn'})
return data, date_data
def make_plots(options, data, data_by_date):
output_dir = '/tmp/hydrometer-plots-%i' % int(time.time())
os.mkdir(output_dir)
f0 = os.path.join(output_dir, 'density.png')
f1 = os.path.join(output_dir, 'temperature.png')
f2 = os.path.join(output_dir, 'density_date.png')
f3 = os.path.join(output_dir, 'temperature_date.png')
date_html = data_by_date.to_html()
days_locator = dates.DayLocator(interval=1)
days_format = dates.DateFormatter('%d')
plt.ioff()
fig0, ax0 = plt.subplots(figsize=FIGSIZE)
ax0.xaxis.set_major_locator(days_locator)
ax0.xaxis.set_major_formatter(days_format)
ax0.format_xdata = days_format
ax0.grid(True, which='both')
ax0.plot(data['time'], data['sg'])
plt.savefig(f0, dpi=200)
fig1, ax1 = plt.subplots(figsize=FIGSIZE)
ax1.xaxis.set_major_locator(days_locator)
ax1.xaxis.set_major_formatter(days_format)
ax1.format_xdata = days_format
ax1.grid(True, which='both')
ax1.plot(data['time'], data['c'])
plt.savefig(f1, dpi=200)
fig2, ax2 = plt.subplots(figsize=FIGSIZE)
ax2.xaxis.set_major_locator(days_locator)
ax2.xaxis.set_major_formatter(days_format)
ax2.format_xdata = days_format
ax2.grid(True, which='both')
ax2.plot(data_by_date.index, data_by_date['sg'])
plt.savefig(f2, dpi=200)
fig3, ax3 = plt.subplots(figsize=FIGSIZE)
ax3.xaxis.set_major_locator(days_locator)
ax3.xaxis.set_major_formatter(days_format)
ax3.format_xdata = days_format
ax3.grid(True, which='both')
ax3.plot(data_by_date.index, data_by_date['c'])
plt.savefig(f3, dpi=200)
return date_html, (f0, f1, f2, f3)
def send_mail(message, options):
# https://stackoverflow.com/questions/73781/sending-mail-via-sendmail-from-python
if options.mail_command:
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
p.communicate(message.as_bytes())
else:
with smtplib.SMTP('localhost') as s:
s.send_message(mail)
return
oparser = argparse.ArgumentParser(description="Plotter for temperature and humidity log",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
oparser.add_argument("-d", dest="data_file",
required=True,
metavar="CSV",
help="CSV input file")
oparser.add_argument("-m", dest="mail",
action='append',
metavar='[email protected]',
help="send mail to this address")
oparser.add_argument("-M", dest='mail_command',
action='store_true',
default=False,
help="use mail command instead of localhost SMTP")
oparser.add_argument("-f", dest="from_mail",
default='[email protected]',
metavar='[email protected]',
help="send mail from this address")
options = oparser.parse_args()
data, data_by_date = get_data(options)
html, plot_files = make_plots(options, data, data_by_date)
if options.mail:
mail = EmailMessage()
mail.set_charset('utf-8')
mail['To'] = ', '.join(options.mail)
mail['From'] = options.from_mail
mail['Subject'] = 'Hydrometer %s' % datetime.datetime.now().strftime('%a %H:%M')
# https://stackoverflow.com/questions/56711321/addng-attachment-to-an-emailmessage-raises-typeerror-set-text-content-got-an
# accepts a maintype argument if the content is bytes, but not if the content is str
mail.add_attachment(html.encode('utf-8'), disposition='inline',
maintype='text', subtype='html')
# https://docs.python.org/3/library/email.examples.html
for file in plot_files:
with open(file, 'rb') as fp:
img_data = fp.read()
mail.add_attachment(img_data, disposition='inline',
maintype='image',
subtype=imghdr.what(None, img_data))
send_mail(mail, options)