-
Notifications
You must be signed in to change notification settings - Fork 5
/
interface.py
271 lines (235 loc) · 9.44 KB
/
interface.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
"""Structure and main execution of the GUI. All the functionalities of the
wacrashviz package will be initiated through this interface. The final map
gets output based on user selections."""
import tkinter as tk
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
from crash4viz import mapping_funcs
from crash4viz import mapping
from crash4viz import mlpredict
def generate_ml(year):
"""
generate statistical analysis plots in the outputs folder
"""
merged_data = pd.read_csv(mapping_funcs.DATA_DIR + '/{}.csv'.format(year))
mlpredict.month_plot(merged_data)
mlpredict.weekday_plot(merged_data)
mlpredict.weather_plot(merged_data)
mlpredict.road_plot(merged_data)
mlpredict.light_plot(merged_data)
mlpredict.ml_prediction(merged_data)
year_plot_list = []
for years in range(2013, 2018):
year_plot_list.append(
pd.read_csv(mapping_funcs.DATA_DIR + '/{}.csv'.format(years)).shape[0]
)
mlpredict.year_plot(year_plot_list)
def set_map_options(dropdown, var):
"""
Set the different options for types of maps that the user can see
based on their selections in the previous drop-down menus.
"""
dropdown.configure(state='normal')
menu = dropdown['menu']
menu.delete(0, 'end')
options = ['Basic road-map',
'Cluster map',
'Layers by year map',
'Cluster & Layer map']
for name in options:
menu.add_command(label=name, command=lambda name=name: var.set(name))
def enable_next_dropdown(dropdown):
"""
Enables next dropdown
"""
dropdown.configure(state='normal') # enable drop-down
class MainApp(tk.Tk):
"""
The drop-down options described in this class dynamically update based
on user selections.
"""
def __init__(self, parent):
tk.Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
"""Initiate the interface with certain features that will update based
on user selections."""
self.title('WA Crash Feature Mapper')
self.minsize(700, 700)
self.selection0 = tk.StringVar()
self.selection0.set('Select year to view')
options0 = ['2013', '2014', '2015', '2016', '2017']
self.drop0 = tk.OptionMenu(self, self.selection0, *options0)
self.drop0.pack()
# self.button0
tk.Button(
self,
text='Save year selection',
command=lambda: enable_next_dropdown(self.drop1)
).pack()
self.selection1 = tk.StringVar()
self.selection1.set('Select county to view')
options1 = ['Adams',
'Asotin',
'Benton',
'Chelan',
'Clallam',
'Clark',
'Columbia',
'Cowlitz',
'Douglas',
'Ferry',
'Franklin',
'Garfield',
'Grant',
'Grays Harbor',
'Island',
'Jefferson',
'King',
'Kitsap',
'Kittitas',
'Klickitat',
'Lewis',
'Lincoln',
'Mason',
'Okanogan',
'Pacific',
'Pend Oreille',
'Pierce',
'San Juan',
'Skagit',
'Skamania',
'Snohomish',
'Spokane',
'Stevens',
'Thurston',
'Wahkiakum',
'Walla Walla',
'Whatcom',
'Whitman',
'Yakima']
self.drop1 = tk.OptionMenu(self, self.selection1, *options1)
self.drop1.configure(state='disabled')
self.drop1.pack()
# self.button1
tk.Button(
self,
text='Save county selection',
command=lambda: enable_next_dropdown(self.drop2)
).pack()
self.selection2 = tk.StringVar()
self.selection2.set('Select group feature to view')
options2 = [
'Weather',
'Surface Condition',
'Lighting Condition',
'Day of the week']
self.drop2 = tk.OptionMenu(self, self.selection2, *options2)
self.drop2.configure(state='disabled')
self.drop2.pack()
# self.button2 =
tk.Button(
self,
text='Save group selection',
command=lambda: self.set_options_init(self.drop3, self.selection3)
).pack()
self.selection3 = tk.StringVar()
self.selection3.set('Select subgroup feature to view')
options3 = 'Select subgroup to view'
self.drop3 = tk.OptionMenu(self, self.selection3, options3)
self.drop3.configure(state='disabled')
self.drop3.pack()
# self.button3 =
tk.Button(
self,
text='Save subgroup selection',
command=lambda: set_map_options(self.drop4, self.selection4)
).pack()
self.selection4 = tk.StringVar()
self.selection4.set('Select type of map to view')
options4 = ['Select type of map to view']
self.drop4 = tk.OptionMenu(self, self.selection4, *options4)
self.drop4.configure(state='disabled')
self.drop4.pack()
# show the final map based on selections
# self.button4 =
tk.Button(
self,
text='Show map', command=self.show_map
).pack()
# self.button5 =
tk.Button(
self,
text='Generate ML reports',
command=lambda: generate_ml(self.selection0.get())
).pack()
def set_options_init(self, dropdown, var):
"""
Change the options in the third drop-down menu based on the user's
selection in the second drop-down menu.
"""
vars_dict = mapping_funcs.VARS_DICT
subgroups_dict = mapping_funcs.SUBGROUPS_DICT
if self.selection2.get() in vars_dict.keys():
dropdown.configure(state='normal') # enable drop-down
menu = dropdown['menu']
menu.delete(0, 'end')
options = subgroups_dict[self.selection2.get()].values()
for name in options:
# Add menu items.
menu.add_command(label=name, command=lambda name=name: var.set(name))
def show_map(self):
"""Output the map that was chosen for the features that were selected,
output text to inidicate where the final interactable html map is saved
for the user."""
vars_dict = mapping_funcs.VARS_DICT
subgroups_dict = mapping_funcs.R_SUBGROUPS_DICT
county_dict = mapping_funcs.R_COUNTY_DICT
year = self.selection0.get()
county = county_dict[self.selection1.get()]
county_name = mapping_funcs.COUNTY_DICT[county]
grp_feature = vars_dict[self.selection2.get()]
subgrp_feature = subgroups_dict[self.selection2.get()][self.selection3.get()]
my_map = mapping.Maps()
data = mapping_funcs.read_dataframe(year)
dataframe = data[data.COUNTY == county]
group_df = dataframe.groupby(grp_feature)
subgrp_df = group_df.apply(lambda g: g[g['weather'] == subgrp_feature])
grp_dict = mapping_funcs.GRP_DICT
group = grp_dict[grp_feature]
if self.selection4.get() == 'Basic road-map':
my_map.basic_map(county_name, county, group, dataframe, subgrp_df)
if subgrp_df.shape[0] == 0:
my_text = f'No data for the provided {group} selection, \
an empty map was generated into the "outputs" folder'
else:
my_text = 'Basic map for {}, under {} conditions saved in "outputs" folder'.format(
self.selection3.get(), self.selection2.get())
tk.Label(ROOT, text=my_text).pack()
if self.selection4.get() == 'Cluster map':
my_map.plot_folium_filtered_clusters(
county_name, county, group, dataframe, subgrp_df)
if subgrp_df.shape[0] == 0:
my_text = f'No data for the provided {group} selection,\
an empty map was generated into the "outputs" folder'
else:
my_text = 'Cluster map for {}, under {} conditions saved in \
"outputs" folder'.format(self.selection3.get(), self.selection2.get())
tk.Label(ROOT, text=my_text).pack()
if self.selection4.get() == 'Layers by year map':
my_map.plot_folium_filtered_layers(
group, county_name, county, grp_feature, subgrp_feature)
my_text = 'Layer map for {}, under {} conditions saved in "outputs" folder'.format(
self.selection3.get(), self.selection2.get())
tk.Label(ROOT, text=my_text).pack()
if self.selection4.get() == 'Cluster & Layer map':
my_map.plot_folium_filtered_clusters_layers(
county_name, county, group, grp_feature, subgrp_feature)
my_text = 'Cluster & layer map for {}, under {} conditions saved \
in "outputs" folder'.format(self.selection3.get(), self.selection2.get())
tk.Label(ROOT, text=my_text).pack()
if __name__ == '__main__':
ROOT = MainApp(None)
ROOT.mainloop()