-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
255 lines (210 loc) · 8.18 KB
/
app.py
File metadata and controls
255 lines (210 loc) · 8.18 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
#import pandas as pd
from flask import Flask, jsonify, request, make_response
import json
import pickle
import scipy.spatial.distance as distance
import json
import flask_cors
import pandas as pd
religious_affil = {
-1: 'Not reported',
-2: 'Not applicable',
22: 'American Evangelical Lutheran Church',
24: 'African Methodist Episcopal Zion Church',
27: 'Assemblies of God Church',
28: 'Brethren Church',
30: 'Roman Catholic',
33: 'Wisconsin Evangelical Lutheran Synod',
34: 'Christ and Missionary Alliance Church',
35: 'Christian Reformed Church',
36: 'Evangelical Congregational Church',
37: 'Evangelical Covenant Church of America',
38: 'Evangelical Free Church of America',
39: 'Evangelical Lutheran Church',
40: 'International United Pentecostal Church',
41: 'Free Will Baptist Church',
42: 'Interdenominational',
43: 'Mennonite Brethren Church',
44: 'Moravian Church',
45: 'North American Baptist',
47: 'Pentecostal Holiness Church',
48: 'Christian Churches and Churches of Christ',
49: 'Reformed Church in America',
50: 'Episcopal Church, Reformed',
51: 'African Methodist Episcopal',
52: 'American Baptist',
53: 'American Lutheran',
54: 'Baptist',
55: 'Christian Methodist Episcopal',
57: 'Church of God',
58: 'Church of Brethren',
59: 'Church of the Nazarene',
60: 'Cumberland Presbyterian',
61: 'Christian Church (Disciples of Christ)',
64: 'Free Methodist',
65: 'Friends',
66: 'Presbyterian Church (USA)',
67: 'Lutheran Church in America',
68: 'Lutheran Church - Missouri Synod',
69: 'Mennonite Church',
71: 'United Methodist',
73: 'Protestant Episcopal',
74: 'Churches of Christ',
75: 'Southern Baptist',
76: 'United Church of Christ',
77: 'Protestant, not specified',
78: 'Multiple Protestant Denomination',
79: 'Other Protestant',
80: 'Jewish',
81: 'Reformed Presbyterian Church',
84: 'United Brethren Church',
87: 'Missionary Church Inc',
88: 'Undenominational',
89: 'Wesleyan',
91: 'Greek Orthodox',
92: 'Russian Orthodox',
93: 'Unitarian Universalist',
94: 'Latter Day Saints (Mormon Church)',
95: 'Seventh Day Adventists',
97: 'The Presbyterian Church in America',
99: 'Other',
100: 'Original Free Will Baptist',
101:'Ecumenical Christian',
102:'Evangelical Christian',
103:'Presbyterian',
105:'General Baptist',
106:'Muslim',
107: 'Plymouth Brethren',
}
# create app
app = Flask(__name__, static_folder='static')
#cors = flask_cors.CORS(app, resources={r"/api/*": {"origins": "*"}})
cors = flask_cors.CORS(app)
#app.config['CORS_HEADERS'] = 'Content-Type'
# load "model" data
df_final = pickle.load(open('static/df_final_names.pkl', 'rb'))
df_final = df_final.loc[:,~df_final.columns.duplicated()]
df_scaled = pickle.load(open('static/scaled_df.pkl', 'rb'))
with open('static/card_info_google.txt') as f:
card_dict = json.load(f)
with open('static/states.txt') as f:
states_dict = json.load(f)
# routes
@app.route('/model/', methods=['POST', 'OPTIONS'])
@flask_cors.cross_origin()
def predict():
if request.method == "OPTIONS": # CORS preflight
return _build_cors_prelight_response()
# get data
data = request.get_json()[0]
colleges = []
if data.get('dream'): colleges.append(data['dream'])
if data.get('target'): colleges.append(data['target'])
if data.get('safety'): colleges.append(data['safety'])
#colleges = [data['dream'], data['target'], data['safety']]
tops = []
closest_list = []
ids = []
for i, college in enumerate(colleges):
college_id = get_index(college)
# get scaled data for this college
test_college = df_scaled.iloc[[college_id]]
ids.append(college_id)
# get scaled distance from every other college (manhattan or minkowski seem best)
ary = distance.cdist(df_scaled, test_college, metric='cityblock')
# make a df with distances to manipulate for this school
results = df_final.copy() # different results
results['dist'] = ary
# sort them so we can examine top matches
closest = results.sort_values(by='dist')
closest = list(closest['INSTNM'])[1:8]
closest_list += closest
tops = tops + [closest[0]]
# now check out a combo of all three schools
mean_school = list(df_scaled.iloc[ids].mean())
combo_school = pd.DataFrame([mean_school], columns=df_scaled.columns)
ary = distance.cdist(df_scaled, combo_school, metric='cityblock')
results = df_final.copy()
results['dist'] = ary
closest = results.sort_values(by='dist')
closest = list(closest['INSTNM'])[1:10]
closest_list += closest
tops = closest[:1] + tops
tops = [x for x in tops if x not in colleges]
result = dupes(closest_list, colleges)
tops = [x for x in tops if x not in result]
result = result + tops # duplicates first, then top results starting with dream school #1
output = {}
output['results'] = []
for college in result:
i = get_index(college)
stats = df_final.iloc[i]
control = 'Public'
if stats['CONTROL'] == 1:
control = 'Public'
else:
control = 'Private'
rel_n = stats['RELAFFIL']
if rel_n not in ['No data available']:
religion = religious_affil[int(rel_n)]
else:
religion = 0
school = {
'schoolname': stats['INSTNM'],
'url': stats['INSTURL'],
'city': stats['CITY'],
'stabbr': stats['STABBR'],
'state': states_dict[stats['STABBR']],
'student_pop': stats['UGDS'],
'control': control,
'avg_tuition': stats['COSTT4_A'],
'admission_rate': stats['ADM_RATE'],
'avg_ACT': stats['ACTCMMID'], # not in the df_final_names unfortunately. Need to redo
'avg_SAT': stats['SAT_AVG'],
'HBCU': stats['HBCU'],
'WOMENONLY': stats['WOMENONLY'],
'MENONLY': stats['MENONLY'],
#'percent_match': results[results['INSTNM']==college]['dist']
}
image = card_dict[college].get('image')
desc = card_dict[college].get('description')
if image: school['image'] = image
if desc: school['description'] = desc
if religion: school['religious_affil'] = religion
output['results'].append(school)
#output['results'] = sorted(output['results'], lambda x: x['student_pop'])
#output['results'] = sorted(output['results'], key=lambda x: (x.get('avg_SAT'), x.get('avg_ACT')), reverse=True)
#cors_response = corsify_response(jsonify(output))
return jsonify(output)
@app.route('/colleges/', methods=['GET'])
def colleges():
with open('static/colleges.txt') as json_file:
data = json.load(json_file)
return data
def dupes(all, original):
# returns list of schools found in closest matches for multiple input schools
# all is a list of schools that were matches
# original is a list of the schools that were original inputs from user
all = [x for x in all if x not in original] # dump any that are part of original list (there is better way to do this)
my_counts = sorted([[x, all.count(x)] for x in set(all)], key=lambda x: x[1]) # get sorted list by number of times they occur in list
dupes = [x[0] for x in my_counts if x[1]>1]
return dupes
def get_index(college):
college_id = df_final[df_final['INSTNM'] == college] # test it out
college_id = college_id.index.to_list()[0]
return int(college_id)
def _build_cors_prelight_response():
response = make_response()
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add("Access-Control-Allow-Headers", "*")
response.headers.add("Access-Control-Allow-Methods", "*")
return response
# def corsify_response(response):
# #response.headers.add("Access-Control-Allow-Origin", "*")
# return response
# A welcome message to test our server
@app.route('/')
def index():
return "<h1>College rec server</h1>"
if __name__ == '__main__':
app.run(debug=True)