/
application.py
198 lines (151 loc) · 6.26 KB
/
application.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
import flask
from flask import render_template, request
import os
import json
import collections
from sqlalchemy import create_engine
import itertools
import networkx as nx
app = flask.Flask(__name__)
@app.route("/")
def summary():
data1=open("topdocs.json").read()
data2=open("network.json").read()
cities = open("cities.json").read()
with open("../App/classdict.json") as tweetfile:
classdict = json.loads(tweetfile.read())
classdict= collections.OrderedDict(sorted(classdict.items()))
for key, value in classdict.iteritems():
classdict[key] = collections.OrderedDict(sorted(value.items()))
return render_template('layout.html', data1=data1, data2=data2, cities=cities, classdict=classdict)
@app.route("/networkapi", methods=['POST'])
def getnetworkdata():
engine = create_engine('mysql+pymysql://root:@localhost:3306/physicianReferral', encoding="utf-8")
connection = engine.connect()
vardict = flask.request.json
print vardict
if vardict['spec1'] == "General":
vardict['spec1'] = "N/A"
if vardict['spec2'] == "General":
vardict['spec2'] = "N/A"
# TODO: don't always default to NYC
vardict['city'] = vardict.get('city', 'New York, NY').split(",")[0]
#Build in exception for when putting in "all" as inputs
linenr = {'spec1':[12, 20, 28], 'spec2':[41, 49, 57], 'class1':[11, 19, 27], 'class2':[40, 48, 56]}
#Get weighted edges with NPI numbers
fd = open('sqlscript.sql', 'r')
sqlFile = fd.read()
fd.close()
sqlsplit = sqlFile.split("\n")
skiplines = [linenr[item] for item in [key for key, value in vardict.iteritems() if value in "All"]]
import itertools
skiplines = list(itertools.chain(*skiplines))
sqlFile = " ".join([line for i, line in enumerate(sqlsplit) if i not in skiplines])
sqlcommand = ['"' + vardict[word] + '"' if word in vardict.keys() else word for word in sqlFile.split()]
sql = " ".join(sqlcommand)
cmd = connection.execute(sql)
wedges = {(line[0], line[1]):line[3] for line in cmd}
print "length of wedges:" + str(len(wedges))
print "made weighted edges ..."
#Get all doctor information including names and taxonomy codes
fd = open('sqlscript2.sql', 'r')
sqlFile = fd.read()
fd.close()
sqlsplit = sqlFile.split("\n")
linenr2 = {'spec1':[11, 27, 43], 'spec2':[17, 33, 49], 'class1':[10, 26, 42], 'class2':[16, 32, 48]}
allist = [key for key, value in vardict.iteritems() if value in "All"]
skiplines = [linenr2[item] for item in [key for key, value in vardict.iteritems() if value in "All"]]
import itertools
skiplines = list(itertools.chain(*skiplines))
sqlFile = " ".join([line for i, line in enumerate(sqlsplit) if i not in skiplines])
sqlcommand = ['"' + vardict[word] + '"' if word in vardict.keys() else word for word in sqlFile.split()]
sql = " ".join(sqlcommand)
cmd = connection.execute(sql)
doctors = {line[0]:(line[1], line[2], line[3], line[4], line[5], line[6], line[7], line[8], line[9]) for line in cmd}
print "length of doctors:" + str(len(doctors))
print "made doctors list..."
#Get taxonomy code mapping
sql = "SELECT Code, Type, Classification, Specialization from taxonomy;"
cmd = connection.execute(sql)
taxonomy = {line[0]:(line[1], line[2], line[3]) for line in cmd}
print "made taxonomy ..."
#Create nodes for inserting into networkx
import itertools
uniqueNPI = set(itertools.chain.from_iterable(wedges.keys()))
dnames = dict([(key, str(value[0]) + ", " + str(value[1])) for key, value in doctors.iteritems() if key in uniqueNPI])
dnames2 = dict([(str(value[0]) + ", " + str(value[1]), key) for key, value in doctors.iteritems() if key in uniqueNPI])
nodes = dnames.values()
#Create edges for inserting into networkx
edges = [(dnames[key[0]], dnames[key[1]], value) for key, value in wedges.iteritems()]
#Create networkx objects
G1=nx.Graph()
G1.add_nodes_from(nodes)
G1.add_weighted_edges_from(edges)
graphs = list(nx.connected_component_subgraphs(G1))
subgroups = {}
for i in range(len(graphs)):
for node in graphs[i].nodes():
subgroups[node] = i
#creating referring docs list
referringdocs = [key[0] for key in wedges.keys()]
referreddocs = [key[1] for key in wedges.keys()]
#make file for network graph
nodelist = []
for node in G1.nodes():
newdict = {}
newdict['name'] = node
if dnames2[node] in referringdocs:
newdict['type'] = 'referring'
elif dnames2[node] in referreddocs:
newdict['type'] = 'referred'
newdict['group'] = subgroups[node]
nodelist.append(newdict)
namenumber = {}
for i, node in enumerate(G1.nodes()):
namenumber[node] = i
linklist = []
for i, edge in enumerate(G1.edges()):
newdict = {}
newdict['source'] = namenumber[edge[0]]
newdict['target'] = namenumber[edge[1]]
linklist.append(newdict)
network = {}
network['nodes'] = nodelist
network['links'] = linklist
#make file for top doctors
G = nx.DiGraph()
G.add_nodes_from(nodes)
G.add_weighted_edges_from(edges)
try:
centrality = nx.eigenvector_centrality(G, max_iter=10000)
except nx.NetworkXError:
centrality = nx.degree_centrality(G)
eigenlist = [(node,centrality[node]) for node in centrality]
topdoctors = sorted(eigenlist, key = lambda tup: -tup[1])
topdocjson = []
for i, doctor in enumerate(topdoctors):
newdict = {}
newdict['rank'] = i + 1
newdict['name'] = doctor[0]
newdict['NPI'] = dnames2[doctor[0]]
newdict['add'] = doctors[dnames2[doctor[0]]][5] + ", " + doctors[dnames2[doctor[0]]][6] + ", "+ doctors[dnames2[doctor[0]]][7] + ", "+ doctors[dnames2[doctor[0]]][8]
newdict['url'] = "https://www.google.com/search?q=" + "dr+" + doctor[0].split(", ")[0] + "+" + doctor[0].split(", ")[1]
specialty = [item for item in [doctors[dnames2[doctor[0]]][2], doctors[dnames2[doctor[0]]][3],doctors[dnames2[doctor[0]]][4]] if item is not None]
spec = []
for item in specialty:
if taxonomy[item][2] != "N/A":
spec.append(taxonomy[item][1] + "/ " + taxonomy[item][2])
elif taxonomy[item][2] == "N/A":
spec.append(taxonomy[item][1])
newdict['specialty'] = spec
topdocjson.append(newdict)
top={}
top['output'] = topdocjson
output={}
output['network'] = network
output['topdocs'] = top
return flask.jsonify(output)
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
app.debug = True
app.run(host='0.0.0.0', port=port)