forked from heatherbaier/wm-asu-caoe-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_helpers.py
352 lines (274 loc) · 13.7 KB
/
app_helpers.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
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
# import tensorflow as tf
from flask import request, jsonify, Response
# import torchvision.models as models
# from sklearn import preprocessing
from pandas import json_normalize
import geopandas as gpd
import pandas as pd
import numpy as np
# import torchvision
import importlib
import geojson
import flask
import json
import io
import os
# from model.utils import *
# from model.aggregator import *
# from model.encoder import *
from lstm_config import get_config
config, _ = get_config()
from lstm_utils import *
from lstm import *
print("WORKING DIRECTORY: ", os.getcwd())
# BASE_DIR = "./"
# BASE_DIR = "/home/caoemig/mysite"
# Path variables
GEOJSON_PATH = "./data/ipumns_simple_wgs_wdata8.geojson"
SHP_PATH = "./data/useforportal2.shp"
DATA_PATH = "./data/census2000_for_model.csv"
MODEL_DATA_PATH = "./data/data_for_portal_model.csv"
MONTH6_PATH = "./data/6month_counts_for_portal.csv"
MONTH12_PATH = "./data/12month_counts_for_portal.csv"
MIGRATION_PATH = "./data/migration_data.json"
CORR_TABLE_PATH = "./data/corr_table.csv"
MATCH_PATH = "./data/gB_IPUMS_match.csv"
IMPACT_PATH = "./data/fake_impact_subevent.csv"
IMPACT_PATH2 = "./data/impact.csv"
ALE_PATH = "./data/fake_ale.csv"
# ALE_PATH = "./data/merged_ale_v8.csv"
# ALE_INTERVALS_PATH = "./data/merged_ale_intervals_v8.json"
ALE_INTERVALS_PATH = "./data/fake_ale_intervals.json"
BORDER_STATIONS_PATH = "./data/border_stations7.geojson"
MODEL_6MONTH_PATH = "./trained_model/model_epoch979.torch"
MODEL_12MONTH_PATH = "./trained_model/model_epoch979.torch"
# BAD_IDS = ["105", "115", "122", "126", "147", "153", "1622", "1684", "2027", "2043", "104", "1630", "113", "640", "400", "1631", "2054", "1693", "152", "1608"]
MODEL_ERROR = 0.024487821
gdf = gpd.read_file(SHP_PATH)
gdf = gdf.dropna(subset = ["geometry"])
# graph_id_dict = dict(zip(gdf["shapeID"].to_list(), [str(i) for i in range(0, len(gdf))]))
munis_available = gdf["shapeID"].to_list()
# Read in spatial data
with open(GEOJSON_PATH) as f:
geodata_collection = geojson.load(f)
model_6month = LSTM_CPU(input_size = 586,
hidden_size = 32,
output_size = 1)
state_dict = torch.load(MODEL_6MONTH_PATH, map_location = torch.device('cpu'))["model_state_dict"]
weights = load_ddp_state(state_dict)
model_6month.load_state_dict(weights)
model_12month = LSTM_CPU(input_size = 586,
hidden_size = 32,
output_size = 1)
state_dict = torch.load(MODEL_12MONTH_PATH, map_location = torch.device('cpu'))["model_state_dict"]
weights = load_ddp_state(state_dict)
model_12month.load_state_dict(weights)
# def predict(graph, selected_muni_ref_dict, new_census_vals, selected_municipalities):
# x, adj_lists, y = [], {}, []
# a = 0
# for muni_id, dta in graph.items():
# if muni_id in selected_muni_ref_dict.values():
# # Grab the current x with both census & geographic features
# cur_x = dta["x"]
# # The first indcies of the current x are the goepgraphic features
# # that we want to preserve, so subset those out from the old cnesus data
# cur_x = cur_x[0:len(cur_x) - 202]
# # Now append the new census data to the geographic x features
# [cur_x.append(v) for v in new_census_vals[muni_id]]
# x.append(cur_x)
# else:
# x.append(dta["x"])
# y.append(dta["label"])
# adj_lists[str(a)] = dta["neighbors"]
# a += 1
# x = np.array(x)
# y = np.expand_dims(np.array(y), 1)
# agg = MeanAggregator(features = x, gcn = False)
# enc = Encoder(features = x, feature_dim = x.shape[1], embed_dim = 128, adj_lists = adj_lists, aggregator = agg)
# model = SupervisedGraphSage(num_classes = 1, enc = enc)
# model.load_state_dict(graph_checkpoint)
# predictions = []
# for muni in selected_municipalities:
# try:
# muni_ref = graph_id_dict[muni]
# input = [muni_ref]
# prediction = int(model.forward(input).item())
# predictions.append(prediction)
# except:
# predictions.append(0)
# print("PREDICTIONS: ", predictions)
# return predictions
def prep_dataframes(dta, request, selected_municipalities):
"""
Function to edit the baseline census data with the
user-edited changes and the interconnectedness changes
"""
print(dta.head())
#######################################################################
# Subset the data of the selected munis from the data frame & #
# grab the variables used in the census model #
#######################################################################
# with open("./us_vars.txt", "r") as f:
# vars = f.read().splitlines()
# vars = [i.strip('"",') for i in vars]
# vars = [i for i in vars if i in dta.columns] + ['muni_id']
# print(vars)
dta_selected = dta[dta['muni_id'].isin([int(i) for i in selected_municipalities])]
dta_dropped = dta[~dta['muni_id'].isin([int(i) for i in selected_municipalities])]
# dta_selected, dta_dropped = dta_selected[vars], dta_dropped[vars]
dta_selected, dta_dropped = dta_selected.fillna(0), dta_dropped.fillna(0)
#######################################################################
# Create the scaler to prep the data later for input into the model #
#######################################################################
print("DTA HEAD", dta_selected.head())
print("DTA HEAD", dta_selected.shape)
# X = dta[vars].drop(["sum_num_intmig", "GEO2_MX"], axis = 1).values
# mMScale = preprocessing.MinMaxScaler()
# scaler = mMScale.fit(X)
#######################################################################
# Parse the edited input variables and conver them to percent format #
#######################################################################
column_names, percent_changes = request.json['column_names'], request.json['percent_changes']
column_names = [i for i in column_names if i not in ['sum_num_intmig_button', 'perc_migrants_button', 'absolute_change_button', 'perc_change_button']]
percent_changes = [i for i in percent_changes if i not in ['sum_num_intmig', 'perc_migrants', 'absolute_change', 'perc_change']]
percent_changes = [(float(i) - 100) * .01 if i != '100' else 100 * .01 for i in percent_changes]
print("PERCENT CHANGES: ", percent_changes)
#######################################################################
# Open the var_map JSON, reverse the dictionary, then map each of #
# the column names back to their original names #
#######################################################################
with open("./var_map.json", "r") as f2:
var_names = json.load(f2)
reverse_var_names = dict([(value, key) for key, value in var_names.items()])
column_names = [reverse_var_names[i] if i in reverse_var_names.keys() else i for i in column_names]
#######################################################################
# INTERCONNECTEDNESS #
# 1) Identify the variables that have been edited by the user #
# 2) Identify the chagnes made by the user #
# 3) Read in the correlation table #
# 4) Subset the correlation table to the edited variables #
# 5) Sort the table in the order of the edited variables list #
# 6) Multiply each ro (aka each variable's) correlation with other #
# variables by the user-made change to that variable, calculate #
# the mean overall change to each variable and convert to #
# dictionary format #
#######################################################################
edited_variables = [column_names[i] for i in range(0, len(column_names)) if percent_changes[i] != 1.0]
edited_p_changes = [percent_changes[i] for i in range(0, len(column_names)) if percent_changes[i] != 1.0]
# corr_table = pd.read_csv(CORR_TABLE_PATH)
# corr_table = corr_table[corr_table['index'].isin(edited_variables)]
# corr_table = corr_table.set_index(['index']).reindex(edited_variables)#.reset_index()
# corr_dict = dict(corr_table.multiply(edited_p_changes, axis='rows').mean())
# print("EDITED VARIABLES & CHANGES: ", edited_variables, edited_p_changes)
# print(corr_dict)
# for var in range(0, len(edited_variables)):
# del corr_dict[edited_variables[var]]# = edited_p_changes[var]
# with open("./correlations.json", "w") as f:
# json.dump(corr_dict, f)
#######################################################################
# For each of the columns, If it's in the list of edited variables, #
# multiply it by the user-defined change. If it's not in the list #
# that means we're editing it by the correlated change, so grab that #
# from the corr dict we created above. #
#######################################################################
for i in range(0, len(column_names)):
if column_names[i] in edited_variables:
change_index = edited_variables.index(column_names[i])
change = dta_selected[column_names[i]] * edited_p_changes[change_index]
# print("CHANGING USER EDITED COLUMN NAME: ", column_names[i], " by ", np.mean(change))
dta_selected[column_names[i]] = dta_selected[column_names[i]] + change
elif (column_names[i] in edited_variables) and (column_names[i] != "GEO2_MX"):
# print(column_names[i], dta_selected.columns)
change = dta_selected[column_names[i]] * corr_dict[column_names[i]]
# print("CHANGING CORRELATED COLUMN NAME: ", column_names[i], " by ", np.mean(change))
dta_selected[column_names[i]] = dta_selected[column_names[i]] + change
dta_selected = dta_selected.fillna(0)
print(dta_selected)
#######################################################################
# Scale the selected data to the origianl scale from above #
#######################################################################
# X = dta_selected.drop(["sum_num_intmig", "GEO2_MX"], axis = 1).values
# X = scaler.transform(X)
# print("X SHAPE: ", X.shape)
return dta_selected, dta_dropped
# # Read in spatial data
# with open(GEOJSON_PATH) as f:
# geodata_collection = geojson.load(f)
# with open(BORDER_STATIONS_PATH) as bs:
# border_stations = geojson.load(bs)
def map_column_names(var_names, df):
for i in range(0, len(df.columns)):
if df.columns[i] in var_names.keys():
df = df.rename(columns = {df.columns[i]: var_names[df.columns[i]] })
return df
def get_column_lists(df, var_names, grouped_vars):
# e_vars = [i for i in grouped_vars['Economic'] if i in df.columns]
# econ = df[e_vars]
# econ = map_column_names(var_names, econ)
# econ = econ.columns
# print('here')
d_vars = [i for i in grouped_vars['Deomographic'] if i in df.columns]
demog = df[d_vars]
demog = map_column_names(var_names, demog)
demog = demog.columns
f_vars = [i for i in grouped_vars['Family'] if i in df.columns]
family = df[f_vars]
family = map_column_names(var_names, family)
family = family.columns
em_vars = [i for i in grouped_vars['Employment'] if i in df.columns]
employ = df[em_vars]
employ = map_column_names(var_names, employ)
employ = employ.columns
# print(employ)
# h_vars = [i for i in grouped_vars['Health'] if i in df.columns]
# health = df[h_vars]
# health = map_column_names(var_names, health)
# health = health.columns
edu_vars = [i for i in grouped_vars['Education'] if i in df.columns]
edu = df[edu_vars]
edu = map_column_names(var_names, edu)
edu = edu.columns
hh_vars = [i for i in grouped_vars['Household'] if i in df.columns]
hhold = df[hh_vars]
hhold = map_column_names(var_names, hhold)
hhold = hhold.columns
c_vars = [i for i in grouped_vars['Crime'] if i in df.columns]
crime = df[c_vars]
crime = map_column_names(var_names, crime)
crime = crime.columns
return demog, family, edu, employ, hhold, crime
def convert_to_pandas(geodata_collection, MATCH_PATH, DATA_PATH):
# Normalize the geoJSON as a pandas dataframe
df = json_normalize(geodata_collection["features"])
df = df.rename(columns = {"properties.shapeID": "shapeID"})
df["shapeID"] = df["shapeID"].astype(int)
# Read in the migration data
dta = pd.read_csv(DATA_PATH)
dta = dta.rename(columns = {"muni_id": "shapeID"})
print(df.columns)
print(dta.columns)
# Mix it all together
merged = pd.merge(df, dta, on = 'shapeID')
return merged
def convert_features_to_geojson(merged, column = 'sum_num_intmig'):
# Make lists of all of the features we want available to the Leaflet map
coords = merged['geometry.coordinates']
types = merged['geometry.type']
num_migrants = merged[column]
shapeIDs = merged['properties.shapeID']
shapeNames = merged['properties.ipumns_simple_wgs_wdata_geo2_mx1960_2015_ADMIN_NAME']
# For each of the polygons in the data frame, append it and it's data to a list of dicts to be sent as a JSON back to the Leaflet map
features = []
for i in range(0, len(merged)):
features.append({
"type": "Feature",
"geometry": {
"type": types[i],
"coordinates": coords[i]
},
"properties": {'num_migrants': num_migrants[i],
'shapeID': shapeIDs[i],
'shapeName': shapeNames[i]
}
})
return features