-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.py
161 lines (133 loc) · 5.21 KB
/
server.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import os
from glob import glob
import matplotlib
import numpy as np
import pandas as pd
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import io
import tornado.ioloop
import tornado.web
from geo import ItmToWGS84
coordinates_converter = ItmToWGS84()
CHART_SCALE = 10
AGE_BINS = [1, 4, 14, 99]
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
class MarkersHandler(tornado.web.RequestHandler):
def get(self):
data = {"markers": app.markers}
output = json.dumps(data, ensure_ascii=False).encode("utf-8")
self.write(output)
class TimelineHandler(tornado.web.RequestHandler):
def get(self):
city1 = self.get_argument("city1")
city2 = self.get_argument("city2")
image = create_plot(city1, city2)
self.set_header('Content-type', 'image/png')
self.set_header('Content-length', len(image))
self.write(image)
def create_plot(city1, city2):
plt.figure()
years1 = create_plot_one_city(city1, "#FC5200")
years2 = create_plot_one_city(city2, "#FEB700")
plt.axvline(0)
years = np.concatenate((years1, years2))
plt.xticks([int(np.nanmin(years)), int(np.nanmax(years))])
plt.xlabel("")
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['top'].set_visible(False)
plt.gca().ticklabel_format(useOffset=False)
plt.gca().yaxis.set_major_locator(plt.NullLocator())
memdata = io.BytesIO()
plt.savefig(memdata, format='png')
image = memdata.getvalue()
return image
def create_plot_one_city(city, color):
df = app.df_acc
df = df[df.SEMEL_YISHUV == int(city)]
if df.empty:
return [np.nan]
years = df.SHNAT_TEUNA
df = df.groupby("SHNAT_TEUNA").size()
df.plot(color=color)
return years
def load_markers():
print "Creating markers...",
df_cities = pd.read_csv("static/data/cities.csv", encoding="cp1255")
app.df_acc = pd.concat(pd.read_csv(filename, encoding="cp1255") for filename in
glob("static/data/lms/Accidents Type 3/*/*AccData.csv"))
df_inv = pd.concat(pd.read_csv(filename, encoding="cp1255") for filename in
glob("static/data/lms/Accidents Type 3/*/*InvData.csv"))
app.df_acc = app.df_acc[app.df_acc.SEMEL_YISHUV > 0]
# Severity counts per city
groups = app.df_acc.groupby(["SEMEL_YISHUV", "HUMRAT_TEUNA"], as_index=False)
df_size = groups.size()
df_size_total = app.df_acc.groupby("SEMEL_YISHUV", as_index=False).size()
max_size = df_size_total.max()
df_markers = groups.mean()
df_markers = pd.merge(df_markers, df_cities, left_on="SEMEL_YISHUV", right_on="SEMEL")
df_markers = df_markers[pd.notnull(df_markers.X) & pd.notnull(df_markers.Y) & (df_size_total > 1)]
# Involved individuals counts
df_involved = pd.merge(app.df_acc, df_inv,
left_on=["pk_teuna_fikt", "sug_tik"],
right_on=["pk_teuna_fikt", "SUG_TIK"])
df_involved = df_involved.groupby(["SEMEL_YISHUV",
np.digitize(df_involved.KVUZA_GIL, bins=AGE_BINS)],
as_index=False).size()
app.markers = []
for index, row in df_markers.iterrows():
lng, lat = coordinates_converter.convert(row.X, row.Y)
size = count_to_size(df_size_total[row.SEMEL_YISHUV], max_size)
severity_count = df_size[row.SEMEL_YISHUV]
light = severity_count.get(3, 1)
severe = severity_count.get(1, 0) + severity_count.get(2, 0)
normalizer = max(light, severe)
color = max(0, int(200 - 2000 * float(severe) / light))
age_count = df_involved[row.SEMEL_YISHUV]
involved = age_count.sum()
young = age_count.get(1, 0)
middle = age_count.get(2, 0)
old = age_count.get(3, 0)
# 4 (that is 99 before binning) is unknown age
app.markers.append({
"lat": lat,
"lng": lng,
"title": row.NAME,
"size": size,
"color": color,
"total": df_size_total[row.SEMEL_YISHUV],
"light": light,
"severe": severe,
"light_size": CHART_SCALE * count_to_size(light, normalizer),
"severe_size": CHART_SCALE * count_to_size(severe, normalizer),
"involved_count": involved,
"young_count": young,
"middle_count": middle,
"old_count": old,
"id": row.SEMEL_YISHUV,
})
app.markers.sort(key=lambda marker: marker["title"])
print "%d done" % len(app.markers)
def count_to_size(count, max_size):
return 30 * np.log(1.25 + count / float(max_size)) if count else 0
def make_app():
return tornado.web.Application(
[
(r"/", MainHandler),
(r"/markers", MarkersHandler),
(r"/timeline.png", TimelineHandler),
],
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
debug=True,
)
if __name__ == "__main__":
app = make_app()
load_markers()
app.listen(8000)
tornado.ioloop.IOLoop.current().start()