-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasserguete.py
executable file
·237 lines (195 loc) · 7.75 KB
/
wasserguete.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Wasserguete.py (c) 2014 pschwede, licensed under GPL 3.0
"""
import gc
import csv
import sys
import glob
import numpy as np
import pprint
import pickle
import colorsys
import datetime
import etframes
import itertools
from os.path import realpath, join
from matplotlib import pyplot
from collections import defaultdict
DELIM = "\t"
def color_palette(n, nmax=255):
"""
# tango
return ["#edd400",
"#f57900",
"#c17d11",
"#73d216",
"#3465a4",
"#75507b",
"#cc0000",
"#d3d7cf"]
# solarized
yellow = (181./255, 137./255, 0./255)
orange = (203./255, 75./255, 22./255)
red = (220./255, 50./255, 47./255)
magenta = (211./255, 54./255, 130./255)
violet = (108./255,113./255,196./255)
blue = ( 38./255,139./255,210./255)
cyan = ( 42./255,161./255,152./255)
green = (133./255,153./255, 0./255)
return [red, orange, yellow, violet, cyan, blue, green][n % 7]
"""
# rainbow
return colorsys.hsv_to_rgb(0.66 * n / nmax, .8, .8)
def get_substrate_key(cvs_dir, substrate):
with open(join(cvs_dir, "ZENTRAL_ZTPARAM.csv"), "r") as z:
reader = csv.reader(z, delimiter=DELIM)
for row in reader:
if substrate in row:
return row[0]
def get_rows(cvs_dir, key):
rows = []
for csv_file in glob.glob("%s/*.csv" % cvs_dir):
if "ZENTRAL_ZTPARAM.csv" in csv_file:
continue
with open(csv_file, "r") as f:
dictreader = csv.DictReader(f, delimiter=DELIM)
for k in dictreader:
if "PRAEFIX" not in k:
break
if key in k.values():
rows.append(k)
return rows
def extract_info(substances):
rows = defaultdict(dict)
for subs in substances:
rows[subs] = {}
for cvs_dir in glob.glob("data/csv/*"):
try:
subs_key = get_substrate_key(cvs_dir, subs)
if not subs_key:
continue
subs_rows = get_rows(cvs_dir, subs_key)
positives = 0
for row in subs_rows:
if row["PRAEFIX"] == "nn":
#continue
value = 0.0
else:
value = float(row["ERGEBNIS"])
try:
time = datetime.datetime.strptime("%s %s" % (row["PDATUM"].split(" ")[0], row["PZEIT"].split(" ")[1]), "%x %X")
except IndexError, e:
time = datetime.datetime.strptime(row["PDATUM"], "%x %X")
try:
pos = row["MKZ_"]
except KeyError:
pos = row["MKZ"]
dic = {"datetime": time, "value": value}
try:
rows[subs][pos].append(dic)
except KeyError:
rows[subs][pos] = [dic]
positives += 1 if row["PRAEFIX"] == "nn" else 0
except IOError, e:
print "Error: %s" % (e, cvs_dir)
return rows
def timelines(info, years):
for j, substance in enumerate(info):
tuples = [row for row in itertools.chain(*info[substance].values())]
tuples = sorted(tuples, key=lambda x: x["datetime"])
x = np.array([i["datetime"] for i in tuples])
y = np.array([i["value"] for i in tuples])
fig, ax = pyplot.subplots()
ax.scatter(x, y, color="none", linewidth=1, edgecolor="black")
# ax.scatter(x, y, color="none", linewidth=1, edgecolor=color_palette(j, len(info)))
etframes.add_dot_dash_plot(ax, ys=y)
pyplot.title(u"%s im Oberfl\u00E4chenwasser (\u00B5g/l)" % substance)
fig.autofmt_xdate()
fig.set_size_inches(16, 9)
fig.savefig("%s.png" % substance, format="png", dpi=100, bbox_inches="tight")
gc.collect()
def get_years(info):
years = set([])
for substance in enumerate(info):
years |= set([x["datetime"].year for x in tuples[substance]])
years = sorted(list(years))
return years
def positives_per_year(info, years=None):
fig, ax = pyplot.subplots()
tuples = {}
for i, substance in enumerate(info):
tuples[substance] = [row for row in itertools.chain(*info[substance].values())]
tuples[substance] = sorted(tuples[substance], key=lambda x: x["datetime"])
if not years:
years = get_years(info)
bar_width = 0.9 / len(years)
xpos = np.arange(len(years))
for i, substance in enumerate(info):
values = []
pct = []
for j, year in enumerate(years):
# a bar for each substance
positives = 0
yearly = 0
all_ = 0
for t in tuples[substance]:
if t["datetime"].year == year:
all_ += 1
yearly += t["value"]
if t["value"] > 0:
positives += 1
pct.append(float(positives) * 100 / all_ if all_ else 0)
values.append(float(yearly) / all_ if all_ else 0)
ax.bar(xpos + (bar_width * i), pct, width=bar_width, color=color_palette(i, len(years)), linewidth=0.01, label=substance)
#ax.bar(xpos + (bar_width * i), values, width=bar_width, color=color_palette(i, len(years)), linewidth=0.01, label=substance)
leg = ax.legend(loc="best", fancybox=True)
leg.get_frame().set_alpha(0.5)
ax.set_ylabel("Anteil in %")
ax.set_xlabel("Jahr")
pyplot.xticks(xpos + bar_width * len(years) / 2, years)
pyplot.title(u"Positivbefunde Oberfl\u00E4chenwasser pro Jahr in Sachsen")
fig.set_size_inches(16, 9)
fig.savefig("positives_per_year.png", format="png", dpi=100, bbox_inches="tight")
def punchcard(info, years=None):
if not years:
years = get_years(info)
for year in years:
fig, ax = pyplot.subplots()
ax.set_ylim(24, 0)
ax.set_yticks(range(0,24))
ax.set_xlim(0, 365)
for i, substance in enumerate(info):
pos = lambda x: x > 0
neg = lambda x: x <= 0
for side in [pos, neg]:
datapoints = [r for r in itertools.chain(*info[substance].values()) if r["datetime"].year == year and side(r["value"])]
day_of_year = [r["datetime"].timetuple().tm_yday for r in datapoints]
hour_of_day = [r["datetime"].hour + float(i) / len(info) for r in datapoints]
col = color_palette(i, len(info))
if side is pos:
ax.scatter(x=day_of_year, y=hour_of_day, s=10, color=col, edgecolor=col, linewidth=0.5, label=substance)
else:
ax.scatter(x=day_of_year, y=hour_of_day, s=10, color="w", edgecolor=col, alpha=0.5, linewidth=0.25)
pyplot.title(u"Oberfl\u00E4chenwasser Messungen in Sachsen %s" % year)
leg = ax.legend(loc="best", fancybox=True)
leg.get_frame().set_alpha(0.5)
ax.set_ylabel("Stunde")
ax.set_xlabel("Tag im Jahr")
fig.set_size_inches(16,9)
fig.savefig("punchcard_%s.png" % year, format="png", dpi=100, bbox_inches="tight")
gc.collect()
if __name__ == "__main__":
substances = ["Imidacloprid", "Clothianidin", "Propoxur", "Glyphosate", "Propioconazol", "Tebuconazol", "Thiamethoxam"]
try:
with open("data/info.pkl", "r") as f:
info = pickle.load(f)
except:
info = extract_info(substances)
gc.collect()
with open("data/info.pkl", "w") as f:
pickle.dump(info, f)
years = [2007, 2008, 2009, 2010, 2011, 2012, 2013]
timelines(info, years)
positives_per_year(info, years)
punchcard(info, years)