-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecision_interface_santorini.py
More file actions
403 lines (297 loc) · 15.7 KB
/
Copy pathdecision_interface_santorini.py
File metadata and controls
403 lines (297 loc) · 15.7 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
from spyre import server
import pandas as pd
import urllib2
import json
import networkx
import random
import matplotlib
from matplotlib import pyplot as plt
from py4j.java_gateway import JavaGateway
import os
import decision_algorithm
import numpy as np
class DecisionInterfaceSantorini(server.App):
title = "Decision Algorithm Santorini"
inputs = [{ "type":'dropdown',
"label": 'Scenario',
"options" : [ {"label": "VR-EQ1-EQ2", "value":"scenario1_input.json"}],
"key": 'ticker',
"action_id": "update_data"},
{ "type":'slider',
"label": 'Dead',
"min" : 0,"max" : 10,"value" : 4,
"key": 'dead',
"action_id": 'plot'},
{ "type":'slider',
"label": 'Direct Cost',
"min" : 0,"max" : 10,"value" : 0,
"key": 'dir_cost',
"action_id": 'plot'},
{ "type":'slider',
"label": 'EM Management Cost',
"min" : 0,"max" : 10,"value" : 0,
"key": 'em_cost',
"action_id": 'plot'},
{ "type":'slider',
"label": 'Homeless',
"min" : 0,"max" : 10,"value" : 0,
"key": 'hom',
"action_id": 'plot'},
{ "type":'slider',
"label": 'Indirect Cost',
"min" : 0,"max" : 10,"value" : 0,
"key": 'ind_cost',
"action_id": 'plot'},
{ "type":'slider',
"label": 'Injured',
"min" : 0,"max" : 10,"value" : 0,
"key": 'inj',
"action_id": 'plot'},
{ "type":'slider',
"label": 'Sanitary Cost',
"min" : 0,"max" : 10,"value" : 0,
"key": 'san_cost',
"action_id": 'plot'}]
controls = [{ "type" : "hidden",
"id" : "update_data"}, { "type" : "hidden",
"id" : "plot"},
{"type" : "button",
"label" : "Update results",
"id" : "update_results"}
]
tabs = ["Data","Ranking","Classes", "Distributions", "Info"]
outputs = [ { "type" : "table",
"id" : "table_id",
"control_id" : "update_data",
"tab" : "Data",
"on_page_load" : True }, { "type" : "html",
"id" : "html3",
"tab" : "Info",
"on_page_load" : True },{ "type" : "html",
"id" : "html1",
"control_id" : "update_results",
"tab" : "Ranking",
"on_page_load" : True }, { "type" : "html",
"id" : "html2",
"control_id" : "update_results",
"tab" : "Classes",
"on_page_load" : True }, { "type" : "plot",
"id" : "plot1",
"control_id" : "update_results",
"tab" : "Distributions",
"on_page_load" : True }]
def getData(self, params): #reads the input data
ticker = params['ticker']
data_input = 'santorini/'+ticker
df = pd.read_json(data_input)
return df
def getDataOutput_Ranking(self,params): #read the output data
ticker = params['ticker']
weights = [params['dead'], params['dir_cost'], params['em_cost'], params['hom'], params['ind_cost'], params['inj'], params['san_cost']]
final_ranking, ranking_distribution = decision_algorithm.decision_ranking('santorini/scenario1_input.json',weights,['EVC_anteEQ1','EVC_anteEQ1_anteEQ2','No Mitigation'],
np.array([0, 50, 50, 2, 50, 2, 20]), np.array([2, 100, 100, 20, 100, 20, 200]), np.array([5, 5000, 5000, 100, 5000, 100, 2000]))
return (final_ranking, ranking_distribution)
def getDataOutput_Sorting(self,params): #read the output data
ticker = params['ticker']
weights = [params['dead'], params['dir_cost'], params['em_cost'], params['hom'], params['ind_cost'], params['inj'], params['san_cost']]
final_sorting, sorting_distribution = decision_algorithm.decision_sorting('santorini/scenario1_input.json',weights,['EVC_anteEQ1','EVC_anteEQ1_anteEQ2','No Mitigation'],
np.array([0, 50, 50, 2, 50, 2,20]),np.array([2, 100, 100, 20, 100, 20, 200]),np.array([5, 5000, 5000, 100, 5000, 100, 2000]),
np.array(([20, 3300000,2300000,2050,655000,60,120],
[30, 3600000,2500000,2070,700000,100,200],
[1000, 2000000000,180000000,2000008,15020000,3000,6000])))
return (final_sorting, sorting_distribution)
def html3(self,params):
description = """
<br> </br>
<h1> Description </h1>
<ul>
<font size = 5 > <li>This is a multicriteria algorithm to provide Decision Support in the choice of mitigation strategies in a simulated cascading effects scenario</li>
<li> <b> Scenario </b> allows to select a simulated cascading effect scenario.
<li> The numerical values on the left represent <b> weights </b> to measure the importance of the criterion for the decision maker</li>
<li> <b> Ranking </b> provides a relative judgement of the mitigation strategies</li>
<li> <b> Class assignment </b> compares mitigation strategies to predefined classes</li>
<li> <b> Distributions </b> represents the uncertainty of the results by perturbating simulated data and generating an array of possibile similar scenarios</li>
</ul>
<br> </br>
<h1> Mitigation strategies </h1>
<ul> <font size = 5>
<li> <b> EVC_anteEQ1: </b> evacuate all the population before first earthquake </li>
<li> <b> EVC_anteEQ1_anteEQ2: </b> evacuate tourists before EQ1 and the rest of the population after EQ2 </li>
<li> <b> No Mitigation: </b> do not evacuate the population </li>
</ul>
<br></br>
<h1> Criteria </h1>
<ul> <font size = 5>
<li> <b> Dead: </b> number of victims </li>
<li> <b> Direct Cost: </b> sum of direct costs </li>
<li> <b> EM Management Cost: </b> emergency management cost </li>
<li> <b> Homeless: </b> number of homeless people </li>
<li> <b> Indirect Cost: </b> sum of indirect costs </li>
<li> <b> Injured: </b> number of injured people </li>
<li> <b> Sanitary Cost: </b> sum of sanitary costs </li>
</ul>
"""
return description
def html2(self,params):
ticker = params['ticker']
final_sorting = self.getDataOutput_Sorting(params)[0] #read from getdataoutput
class_EVC_t0 = final_sorting['EVC_anteEQ1_anteEQ2']
class_EVC_t1 = final_sorting['EVC_anteEQ1']
class_NoMit = final_sorting['No Mitigation']
class_assignment = """<h1> <font size = 13> Class Assignment </font> </h1> <ul> <font size = 6><li>
Evacuation anteEQ1 anteEQ2 <i class="badge%s">%s</i> </li> <li> Evacuation anteEQ1 <i class="badge%s">%s</i> </li> <li>
No Mitigation <i class="badge%s">%s</i> </li> </font></ul> """%(class_EVC_t0,class_EVC_t0,class_EVC_t1,class_EVC_t1,class_NoMit,class_NoMit)
return class_assignment
def html1(self,params):
ticker = params['ticker']
final_ranking = self.getDataOutput_Ranking(params)[0] #read from getdataoutput
rank_EVCA = final_ranking['EVC_anteEQ1_anteEQ2']
rank_EVCA = int(rank_EVCA)
rank_EVCB = final_ranking['EVC_anteEQ1']
rank_EVCB = int(rank_EVCB)
rank_NoMit = final_ranking['No Mitigation']
rank_NoMit = int(rank_NoMit)
ranking = """<h1> <font size = 13> Ranking </font> </h1> <ul><font size = 6> <li> Evacuation anteEQ1 anteEQ2<i class="badge%s">%s</i> </li>
<li> Evacuation anteEQ1 <i class="badge%s">%s</i> </li> <li>
No Mitigation <i class="badge%s">%s</i> </li> </font></ul>
"""%(rank_EVCA,rank_EVCA,rank_EVCB,rank_EVCB,rank_NoMit,rank_NoMit)
return ranking
def plot1(self, params):
ticker = params['ticker']
ranking_distribution = self.getDataOutput_Ranking(params)[1] #read from getdataoutput
#print ranking_distribution
class_distribution = self.getDataOutput_Sorting(params)[1]
N = 3 #number of mitigation strategies
M = 3 #number of classes
values1 = ranking_distribution['EVC_anteEQ1_anteEQ2']
num_1 = int(values1[0])
num_2 = int(values1[1])
num_3 = int(values1[2])
values1 = [1 for i in range(num_1)] + [2 for i in range(num_2)] + [3 for i in range(num_3)]
values2 = ranking_distribution['EVC_anteEQ1']
num_1 = int(values2[0])
num_2 = int(values2[1])
num_3 = int(values2[2])
values2 = [1 for i in range(num_1)] + [2 for i in range(num_2)] + [3 for i in range(num_3)]
values4 = ranking_distribution['No Mitigation']
num_1 = int(values4[0])
num_2 = int(values4[1])
num_3 = int(values4[2])
values4 = [1 for i in range(num_1)] + [2 for i in range(num_2)] + [3 for i in range(num_3)]
class1 = class_distribution['EVC_anteEQ1_anteEQ2']
num_1 = int(class1[0])
num_2 = int(class1[1])
num_3 = int(class1[2])
class1 = ['a' for i in range(num_1)] + ['b' for i in range(num_2)] + ['c' for i in range(num_3)]
class1_num = [ord(i) -96 for i in class1] #turn letters to integer, 'a' -> 1, 'b' -> 2...
class2 = class_distribution['EVC_anteEQ1']
num_1 = int(class2[0])
num_2 = int(class2[1])
num_3 = int(class2[2])
class2 = ['a' for i in range(num_1)] + ['b' for i in range(num_2)] + ['c' for i in range(num_3)]
class2_num = [ord(i) -96 for i in class2] #turn letters to integer, 'a' -> 1, 'b' -> 2...
class4 = class_distribution['No Mitigation']
num_1 = int(class4[0])
num_2 = int(class4[1])
num_3 = int(class4[2])
class4 = ['a' for i in range(num_1)] + ['b' for i in range(num_2)] + ['c' for i in range(num_3)]
class4_num = [ord(i) -96 for i in class4] #turn letters to integer, 'a' -> 1, 'b' -> 2...
# These are the "Tableau 20" colors as RGB.
tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
(44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
(148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
(227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]
# Scale the RGB values to the [0, 1] range, which is the format matplotlib accepts.
for i in range(len(tableau20)):
r, g, b = tableau20[i]
tableau20[i] = (r / 255., g / 255., b / 255.)
fig = plt.figure(1,figsize=(15, 11), dpi = 1000)
ax1 = plt.subplot(231)
ax1.spines["top"].set_visible(False)
ax1.spines["bottom"].set_visible(False)
ax1.spines["right"].set_visible(False)
ax1.spines["left"].set_visible(False)
ax1.get_xaxis().tick_bottom()
ax1.get_yaxis().tick_left()
plt.ylim(0,150)
plt.xlim(0.5,3.5)
plt.ylabel("Rank Frequency", fontsize=20)
plt.xlabel('EVC_anteEQ1_anteEQ2', fontsize=20)
plt.yticks(range(0,len(class2)+1,20), fontsize=14)
plt.xticks(range(1,N+1), fontsize = 14)
plt.hist(values1, bins = range(1,N+2), color = tableau20[0], align = 'left', rwidth=0.9)
ax2 = plt.subplot(232)
ax2.spines["top"].set_visible(False)
ax2.spines["bottom"].set_visible(False)
ax2.spines["right"].set_visible(False)
ax2.spines["left"].set_visible(False)
ax2.get_xaxis().tick_bottom()
ax2.get_yaxis().tick_left()
plt.ylim(0,150)
plt.xlim(0.5,3.5)
plt.ylabel(" ", fontsize=18)
plt.xlabel('EVC_anteEQ1', fontsize=20)
plt.yticks(range(0,len(class2)+1,20), fontsize=14)
plt.xticks(range(1,N+1), fontsize = 14)
plt.hist(values2, bins = range(1,N+2), color = tableau20[0], align = 'left', rwidth=0.9)
ax4 = plt.subplot(233)
ax4.spines["top"].set_visible(False)
ax4.spines["bottom"].set_visible(False)
ax4.spines["right"].set_visible(False)
ax4.spines["left"].set_visible(False)
ax4.get_xaxis().tick_bottom()
ax4.get_yaxis().tick_left()
plt.ylim(0,150)
plt.xlim(0.5,3.5)
plt.ylabel(" ", fontsize=18)
plt.xlabel("No Mitigation", fontsize=20)
plt.yticks(range(0,len(class2)+1,20), fontsize=14)
plt.xticks(range(1,N+1), fontsize = 14)
plt.hist(values4, bins = range(1,N+2), color = tableau20[0], align = 'left', rwidth=0.9)
cx1 = plt.subplot(234)
cx1.spines["top"].set_visible(False)
cx1.spines["bottom"].set_visible(False)
cx1.spines["right"].set_visible(False)
cx1.spines["left"].set_visible(False)
cx1.get_xaxis().tick_bottom()
cx1.get_yaxis().tick_left()
plt.ylim(0,150)
plt.xlim(0.5,3.5)
plt.ylabel("Class Frequency", fontsize=20)
plt.xlabel("EVC_anteEQ1_anteEQ2", fontsize=20)
plt.yticks(range(0,len(class2)+1,20), fontsize=14)
plt.xticks(range(1,M+1), ('A','B','C'),fontsize = 14)
plt.hist(class1_num, bins = range(1,M+2), color = tableau20[15], align = 'left', rwidth=0.9)
cx2 = plt.subplot(235)
cx2.spines["top"].set_visible(False)
cx2.spines["bottom"].set_visible(False)
cx2.spines["right"].set_visible(False)
cx2.spines["left"].set_visible(False)
cx2.get_xaxis().tick_bottom()
cx2.get_yaxis().tick_left()
plt.ylim(0,150)
plt.xlim(0.5,3.5)
plt.ylabel(" ", fontsize=18)
plt.xlabel("EVC_anteEQ1", fontsize=20)
plt.yticks(range(0,len(class2)+1,20), fontsize=14)
plt.xticks(range(1,M+1), ('A','B','C'),fontsize = 14)
plt.hist(class2_num, bins = range(1,M+2), color = tableau20[15], align = 'left', rwidth=0.9)
cx4 = plt.subplot(236)
cx4.spines["top"].set_visible(False)
cx4.spines["bottom"].set_visible(False)
cx4.spines["right"].set_visible(False)
cx4.spines["left"].set_visible(False)
cx4.get_xaxis().tick_bottom()
cx4.get_yaxis().tick_left()
plt.ylim(0,150)
plt.xlim(0.5,3.5)
plt.ylabel(" ", fontsize=18)
plt.xlabel("No Mitigation", fontsize=20)
plt.yticks(range(0,len(class2)+1,20), fontsize=14)
plt.xticks(range(1,M+1), ('A','B','C'),fontsize = 14)
plt.hist(class4_num, bins = range(1,M+2), color = tableau20[15], align = 'left', rwidth=0.9)
return fig
if __name__ == '__main__':
app = DecisionInterfaceSantorini()
app.launch(port = 80, host = '192.168.30.22')