-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
194 lines (167 loc) · 5.9 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
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
import os
import dash
import pandas as pd
from dash import dcc
from dash import html
import plotly.express as px
from dash.dependencies import Input, Output
import pymongo
# https://codepen.io/LeonGr/pen/yginI.js
# https://codepen.io/chriddyp/pen/bWLwgP.css
# https://codepen.io/ninjakx/pen/bGEpbXo.css
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
# env variables
client = pymongo.MongoClient(os.environ['MONGO_URI'])
database = str(os.environ.get('MONGO_DB', 'openfda'))
graph_refresh_interval = int(os.environ.get('GRAPH_REFRESH_INTERVAL', '30000'))
# database
db = client[database]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.Div([
html.H2('Patient reactions'),
dcc.Graph(id='live-update-graph1'),
dcc.Interval(
id='interval-component1',
interval=graph_refresh_interval,
n_intervals=0
)
]),
html.Div([
html.H2('Medical products'),
dcc.Graph(id='live-update-graph2'),
dcc.Interval(
id='interval-component2',
interval=graph_refresh_interval,
n_intervals=0
)
]),
html.Div([
html.H2('Reactions by month'),
dcc.Graph(id='live-update-graph3'),
dcc.Interval(
id='interval-component3',
interval=graph_refresh_interval,
n_intervals=0
)
]),
])
@app.callback(Output('live-update-graph1', 'figure'),
Input('interval-component1', 'n_intervals'))
def update_graph_live1(n):
pipeline = [
{"$unwind": "$patientReactions"},
{"$project": {"reaction": "$patientReactions"}},
{"$group": {"_id": "$reaction", "total": {"$sum": 1}}},
{"$project": {
"_id": 0,
"reaction": "$_id",
"total": 1
}},
{"$sort": {"total": -1}},
{"$limit": 20}
]
result = db.drugAdverseEvent.aggregate(pipeline, allowDiskUse=True)
df = pd.DataFrame(list(result))
fig = px.bar(df, y='total', x='reaction', text_auto='.4s', height=600)
fig.update_layout(
yaxis=dict(
title='Number of events',
titlefont_size=20,
tickfont_size=18
),
xaxis=dict(
title='Reaction',
titlefont_size=20,
tickfont_size=18
),
barmode='group',
bargap=0.15, # gap between bars of adjacent location coordinates.
bargroupgap=0.1 # gap between bars of the same location coordinate.
)
return fig
@app.callback(Output('live-update-graph2', 'figure'),
Input('interval-component2', 'n_intervals'))
def update_graph_live2(n):
pipeline = [
{"$unwind": "$medicinalProduct"},
{"$project": {"product": {"$toUpper": "$medicinalProduct"}}},
{"$group": {"_id": "$product", "total": {"$sum": 1}}},
{"$project": {
"_id": 0,
"product": {"$substr": ["$_id", 0, 20]},
"total": 1
}},
{"$sort": {"total": -1}},
{"$limit": 20}
]
result = db.drugAdverseEvent.aggregate(pipeline, allowDiskUse=True)
df = pd.DataFrame(list(result))
fig = px.bar(df, y='total', x='product', text_auto='.4s', height=600)
fig.update_layout(
yaxis=dict(
title='Number of events',
titlefont_size=20,
tickfont_size=18
),
xaxis=dict(
title='Name',
titlefont_size=20,
tickfont_size=18
),
barmode='group',
bargap=0.15, # gap between bars of adjacent location coordinates.
bargroupgap=0.1 # gap between bars of the same location coordinate.
)
return fig
@app.callback(Output('live-update-graph3', 'figure'),
Input('interval-component3', 'n_intervals'))
def update_graph_live3(n):
pipeline = [
{"$project": {"date": {"$dateFromString": {"dateString": "$receiveDate", "format": "%Y%m%d"}}}},
{"$group": {"_id": {"$month": "$date"}, "total": {"$sum": 1}}},
{"$project": {
"month": {
"$switch": {
"branches": [
{"case": { "$eq": ["$_id", 1]}, "then": "January"},
{"case": { "$eq": ["$_id", 2]}, "then": "February"},
{"case": { "$eq": ["$_id", 3]}, "then": "March"},
{"case": { "$eq": ["$_id", 4]}, "then": "April"},
{"case": { "$eq": ["$_id", 5]}, "then": "May"},
{"case": { "$eq": ["$_id", 6]}, "then": "June"},
{"case": { "$eq": ["$_id", 7]}, "then": "July"},
{"case": { "$eq": ["$_id", 8]}, "then": "August"},
{"case": { "$eq": ["$_id", 9]}, "then": "September"},
{"case": { "$eq": ["$_id", 10]}, "then": "October"},
{"case": { "$eq": ["$_id", 11]}, "then": "November"},
{"case": { "$eq": ["$_id", 12]}, "then": "December"}
], "default": "unknown"
}
},
"total": 1,
"_id": 0
}},
{"$sort": {"total": -1}}
]
result = db.drugAdverseEvent.aggregate(pipeline, allowDiskUse=True)
df = pd.DataFrame(list(result))
fig = px.bar(df, y='total', x='month', text_auto='.4s', height=600)
fig.update_layout(
yaxis=dict(
title='Number of events',
titlefont_size=20,
tickfont_size=18
),
xaxis=dict(
title='Month',
titlefont_size=20,
tickfont_size=18
),
barmode='group',
bargap=0.15, # gap between bars of adjacent location coordinates.
bargroupgap=0.1 # gap between bars of the same location coordinate.
)
return fig
if __name__ == '__main__':
app.run_server(debug=True, host='0.0.0.0')