forked from ccolg/InterPOLO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
169 lines (138 loc) · 4.92 KB
/
app.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
import csv
import json
import ast
from flask import Flask, Response, render_template, request, redirect, url_for, jsonify, make_response
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
app = Flask(__name__)
#setting up an application whose name is app with Flask
data = []
with open('gtd_regression2.csv') as file:
for row in csv.DictReader(file):
data.append(row)
file.close()
GTD = ast.literal_eval(json.dumps(data))
GTD_attributes = [column_key for column_key in GTD[0].keys()]
countries = []
for row in GTD:
countries.append(row['country'])
#GTD_attributes = [1, 2, 3]
ML_techniques = ['Regression']
country = sorted(list(set(countries)))
@app.route('/')
@app.route('/index.html')
#both connect to explore page
def main():
return render_template('index.html', attributes = GTD_attributes, techniques = ML_techniques, countries = country)
@app.route('/explore', methods = ['GET', 'POST'])
def drop_down_box_options():
selections = []
ML_techniques = None
country = None
if request.method == "POST":
selections = request.json['GTD_attributes']
ML_techniques = request.json['ML_techniques']
country = request.json['country']
print (selections, ML_techniques, country)
#gives list of selected attributes in unicode
# use them for determing the columns to extract (done below but commented out for testing). DON'T DO ANALYTICS HERE. Check the next app.route Flask call
#GTD_temp_analysis = []
#for j in selections:
# temp = []
# for i in GTD:
# temp.append(i[j])
# GTD_temp_analysis.append(temp)
#creating database by collecting all row values for the selected attribute/column. Store the final dictionary/array in result
#df = pd.DataFrame(GTD_temp_analysis, columns=GTD_attributes)
result = selections
#for j in selections:
# values = []
# for k in GTD:
# values.append(k[j])
# result.append(values)
return redirect(url_for('Analytics',data = result, technique = ML_techniques, country = country)) #going back to explore page
@app.route('/Analytics', methods = ['GET', 'POST'])
def Analytics():
selected_data = request.args.get('data')
selected_technique = request.args.get('technique')
selected_country = request.args.get('country')
print (selected_data, selected_technique, country)
GTD_final = technique(selected_data, selected_technique, selected_country)
print (GTD_final)
# call the functions needed to do the necessary analysis and store the functional values in the array/dictionary GTD_final, which will be JSONified and passed to d3
# print GTD_final
result = jsonify(GTD_final)
# print type(GTD_final) #GTD_final is dict
# print type(result) #result is <class 'flask.wrappers.Response'>
return Response(json.dumps(GTD_final), mimetype = 'application/json')
def list(data): #dummy function just for execution check
clean_data = json.loads(str(data).replace("\'", '"')) #converting from unicode to string/number/dict
# print type(clean_data) #clean_data is dict
# print clean_data
array = []
header = []
for i, values in clean_data.items():
print(header.append(i))
array.append(values)
# print type(result) #result is dict
# print result
#result = {"key": "........""}
return [header, array]
def Regression(data, country):
table = []
for row in GTD:
entry = []
for key, value in row.items():
if (key == 'latitude') or (key == 'longitude') or (key == 'nkill') or (key == 'nwound'):
entry.append(float(value))
else:
entry.append(value)
table.append(entry)
df = pd.DataFrame(table, columns = GTD[0].keys())
df = df[df['country'] == country]
df['casualty'] = df['nkill'] + df['nwound']
X = df[data]
y = df['casualty']
Xy = pd.concat([X,y], axis = 1).dropna(how = 'any')
# Note the difference in argument order
model = smf.ols('casualty ~ {}'.format(data), data = Xy).fit()
# Print out the statistics
output = '{}'.format(model.params)
return {'key': output}
def technique(data, technique, country):
options = {
#'sum': sum(data)
'Regression': Regression(data, country)
# 'SVM': SVM(data),
# 'LR': LR(data),
# 'PCA': PCA(data)
}
# uncomment to test other techniques
# can consider using if else if ladder if implementation gets slow
return options[technique]
# def SVM(data):
# return
@app.route('/data')
def get_data():
# data_file = open('gtd_regression2.csv')
# print data_file
# csv_file = csv.reader(data_file)
# print csv_file
# response = make_response(csv_file)
# response.headers["Content-Disposition"] = "attachment; filename=result.csv"
# return response
with open('globalterrorismdb_0617dist.csv') as file:
csv = file.read()
return Response(csv,mimetype="text/csv")
@app.route('/analysis.html')
def Analysis():
return render_template('analysis.html')
@app.route('/dataset.html')
def Dataset():
return render_template('dataset.html')
@app.route('/about.html')
def About():
return render_template('about.html')
if __name__ == '__main__':
app.run(host = 'localhost', port = 8000, debug = True)