zavavan commited on
Commit
3618400
·
verified ·
1 Parent(s): 3a51ebf

Update dashboard.py

Browse files
Files changed (1) hide show
  1. dashboard.py +2 -347
dashboard.py CHANGED
@@ -8,351 +8,6 @@ import warnings
8
  warnings.simplefilter(action='ignore', category=FutureWarning)
9
  warnings.simplefilter(action='ignore', category=RuntimeWarning)
10
  import pandas as pd
11
- import warnings
12
- warnings.filterwarnings("ignore")
13
- import io
14
- import os
15
- import time
16
- import warnings
17
- warnings.simplefilter(action='ignore', category=FutureWarning)
18
- warnings.simplefilter(action='ignore', category=RuntimeWarning)
19
- import pandas as pd
20
- import csv
21
- import ast
22
- from tqdm import tqdm
23
- from operator import itemgetter
24
- import numpy as np
25
- import re
26
- import datetime
27
- import html
28
- from joblib import Parallel, delayed
29
- import matplotlib.pyplot as plt
30
- import matplotlib.dates as mdates
31
- #plt.style.use('seaborn-paper')
32
- import holoviews as hv
33
- from holoviews import opts, dim
34
- from bokeh.sampledata.les_mis import data
35
- from bokeh.io import show
36
- from bokeh.sampledata.les_mis import data
37
- import panel as pn
38
- #pn.extension(design='material')
39
-
40
- import bokeh
41
- from bokeh.resources import INLINE
42
- from holoviews.operation.timeseries import rolling, rolling_outlier_std
43
- hv.extension('bokeh')
44
-
45
- ## LOAD DATASETS
46
-
47
- data = './data'
48
-
49
-
50
- def read_freq_map(filename):
51
- df = pd.read_csv(os.path.join(data,filename), sep=' ')
52
- print(df)
53
- if 'Unnamed: 0' in df.columns:
54
- df = df.drop('Unnamed: 0', axis=1)
55
- column_0 = df.columns[0]
56
- column_1 = df.columns[1]
57
- freqmap = dict(zip(df[column_0], df[column_1]))
58
- return freqmap
59
-
60
- def read_ont_freq_dataframe(filename):
61
- df = pd.read_csv(os.path.join(data,filename), sep=' ')
62
- print(df)
63
- if 'Unnamed: 0' in df.columns:
64
- df = df.drop('Unnamed: 0', axis=1)
65
- column_0 = df.columns[0]
66
- column_1 = df.columns[1]
67
- freqmap = dict(zip(df[column_0], df[column_1]))
68
- return freqmap
69
-
70
-
71
- entityTypesFreqMap = read_freq_map('entityTypes.tsv')
72
- relationTypesFreqMap = read_freq_map('relationTypes.tsv')
73
- topDrugEntities = read_freq_map('topDrugs.tsv')
74
- topConditionEntities = read_freq_map('topConditions.tsv')
75
- topDrugOnts_df = pd.read_csv(os.path.join(data,'topDrugOntologies.tsv'), sep='\t')
76
- topConditionOnts_df = pd.read_csv(os.path.join(data,'topConditionOntologies.tsv'), sep='\t')
77
-
78
-
79
- grouping_filtered = pd.read_csv(os.path.join(data, 'drugReviewsCausal_relations.tsv'), sep=" ")
80
-
81
-
82
-
83
- ################################# CREATE CHARTS ############################
84
- def create_type_bar_charts(entRelsButton, **kwargs):
85
- if entRelsButton=='Entity':
86
- dictionary = entityTypesFreqMap
87
- return hv.Bars(dictionary, hv.Dimension('Entity Types'), 'Frequency').opts( framewise=True, xrotation=45,width=1200, height=600)
88
- elif entRelsButton=='Relation':
89
- dictionary = relationTypesFreqMap
90
- return hv.Bars(dictionary, hv.Dimension('Relation Types'), 'Frequency').opts(framewise=True, xrotation=45,width=1200, height=600)
91
-
92
-
93
- def create_ent_bar_charts(ents, **kwargs):
94
- if ents=='Drug':
95
- dictionary = topDrugEntities
96
- return hv.Bars(dictionary, hv.Dimension('Drug Entities'), 'Frequency').opts( framewise=True, xrotation=45,width=1200, height=600)
97
- elif ents=='Condition':
98
- dictionary = topConditionEntities
99
- return hv.Bars(dictionary, hv.Dimension('Condition Entities'), 'Frequency').opts(framewise=True, xrotation=45,width=1200, height=600)
100
-
101
- def create_ontology_bar_charts(ents, **kwargs):
102
- if ents=='Drug':
103
- df = pd.DataFrame({
104
- 'Drug_Ontologies': [ont.split('/')[-1] for ont in topDrugOnts_df['ontology']],
105
- 'Frequency': list(topDrugOnts_df['count']),
106
- 'url': list(topDrugOnts_df['ontology_url']) # using full keys as hyperlinks
107
- })
108
- # Create bar chart with label as x-axis
109
- bars = hv.Bars(df, kdims=['Drug_Ontologies'], vdims=['Frequency'])
110
- bars.opts(
111
- framewise=True,
112
- tools=['hover'],
113
- width=1200,
114
- height=600,
115
- show_legend=True,
116
- xrotation=45,
117
- xlabel='Drug_Ontologies',
118
- ylabel='Frequency',
119
- hover_tooltips=[
120
- ("Drug_Ontologies", "@Drug_Ontologies"),
121
- ("Frequency", "@Frequency")
122
- ]
123
- )
124
- links_panel = pn.Column(
125
- *[pn.pane.Markdown(f"[{row.Drug_Ontologies}]({row.url})", width=400) for _, row in df.iterrows()],
126
- name='Links'
127
- )
128
- layout = pn.Row(bars, links_panel)
129
- return layout
130
- elif ents=='Condition':
131
- df = pd.DataFrame({
132
- 'Condition_Ontologies': [ont.split('/')[-1] for ont in topConditionOnts_df['ontology']],
133
- 'Frequency': list(topConditionOnts_df['count']),
134
- 'url': list(topConditionOnts_df['ontology_url']) # using full keys as hyperlinks
135
- })
136
- # Create bar chart with label as x-axis
137
- bars = hv.Bars(df, kdims=['Condition_Ontologies'], vdims=['Frequency'])
138
- bars.opts(
139
- framewise=True,
140
- tools=['hover'],
141
- width=1200,
142
- height=600,
143
- show_legend=True,
144
- xrotation=45,
145
- xlabel='Condition_Ontologies',
146
- ylabel='Frequency',
147
- hover_tooltips=[
148
- ("Condition Ontologies", "@Condition Ontologies"),
149
- ("Frequency", "@Frequency") ])
150
- links_panel = pn.Column(
151
- *[pn.pane.Markdown(f"[{row.Condition_Ontologies}]({row.url})", width=400) for _, row in df.iterrows()],
152
- name='Links')
153
- layout = pn.Row(bars, links_panel)
154
- return layout
155
-
156
- ############################# WIDGETS & CALLBACK ###########################################
157
-
158
- def filter_data0(df, min_value):
159
- filtered_df = df[df['value'] >= min_value]
160
- return filtered_df
161
-
162
-
163
- def plot_chord(df,min_value):
164
- filtered_df = filter_data0(df, min_value)
165
- # Create a Holoviews Dataset for nodes
166
- nodes = hv.Dataset(filtered_df, 'index')
167
- nodes.data.head()
168
- chord = hv.Chord(filtered_df, ['source', 'target'], ['value'])
169
- return chord.opts(opts.Chord(cmap='Category20', edge_cmap='Category20', label_text_color="white", node_color = hv.dim('index').str(), edge_color = hv.dim('source').str(), labels = 'index', tools=['hover'], width=800, height=800))
170
-
171
-
172
- def chordify_triples(rel_grouping):
173
- # Define range for minimum value slider
174
- min_value_range = rel_grouping['value'].unique()
175
- min_value_range.sort()
176
-
177
- # Define HoloMap with minimum value and attribute as key dimensions
178
- holomap = hv.HoloMap({min_value: plot_chord(rel_grouping, min_value)
179
- for min_value in min_value_range},
180
- kdims=['Show triples with support greater than']
181
- )
182
- return holomap
183
-
184
-
185
- # https://tabler-icons.io/
186
- button1 = pn.widgets.Button(name="Introduction", button_type="warning", icon="file-info", styles={"width": "100%"})
187
- button2 = pn.widgets.Button(name="Entity/Relation Types:", button_type="warning", icon="chart-histogram", styles={"width": "100%"})
188
- button3 = pn.widgets.Button(name="Top Key Entities", button_type="warning", icon="chart-bar", styles={"width": "100%"})
189
- button4 = pn.widgets.Button(name="Ontology Coverage", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"})
190
- button5 = pn.widgets.Button(name="Causal Relation Chord Diagrams", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"})
191
- # Define child buttons
192
- child_button_1 = pn.widgets.Button(name="Cause", button_type="warning")
193
- child_button_2 = pn.widgets.Button(name="Enable", button_type="warning")
194
- child_button_3 = pn.widgets.Button(name="Prevent", button_type="warning")
195
- child_button_4 = pn.widgets.Button(name="Hinder", button_type="warning")
196
- child_button_5 = pn.widgets.Button(name="Other", button_type="warning")
197
- # Layout: dendrogram-style using vertical + indent
198
- tree_layout = pn.Column(
199
- button5,
200
- pn.Row(pn.Spacer(width=40), # indent
201
- pn.Column(child_button_1, child_button_2,child_button_3,child_button_4, child_button_5))
202
- )
203
-
204
- entRelsButton = pn.widgets.RadioButtonGroup(name='### Select', options=['Entity','Relation'], value = 'Entity' )
205
-
206
- entTypeButton = pn.widgets.RadioButtonGroup(name='### Select Entity Type', options=list(entityTypesFreqMap.keys()), value='Drug')
207
-
208
- #relationTypeButton = pn.widgets.RadioButtonGroup(options=list(relationTypesFreqMap.keys()), value='Cause', name='Select Causal Relation')
209
-
210
- # Define the callback function to update the HoloMap
211
- #def update_holomap(event):
212
- # initial_holomap.object = filter_triples(event.new)
213
-
214
-
215
- # Create the initial HoloMap
216
- #initial_holomap = filter_triples(relationTypeButton.value)
217
-
218
- # Bind the callback function to the value change event of the RadioButton widget
219
- #relationTypeButton.param.watch(update_holomap, 'value')
220
-
221
-
222
- def show_page(page_key):
223
- main_area.clear()
224
- main_area.append(mapping[page_key])
225
-
226
- button1.on_click(lambda event: show_page("Page1"))
227
- button2.on_click(lambda event: show_page("Page2"))
228
- button3.on_click(lambda event: show_page("Page3"))
229
- button4.on_click(lambda event: show_page("Page4"))
230
- button5.on_click(lambda event: show_page("Page5"))
231
- child_button_1.on_click(lambda event: show_page("Page5a"))
232
- child_button_2.on_click(lambda event: show_page("Page5b"))
233
- child_button_3.on_click(lambda event: show_page("Page5c"))
234
- child_button_4.on_click(lambda event: show_page("Page5d"))
235
- child_button_5.on_click(lambda event: show_page("Page5e"))
236
-
237
-
238
- ### CREATE PAGE LAYOUTS
239
-
240
- def CreatePage1():
241
- return pn.Column(pn.pane.Markdown("""
242
-
243
- This is a dashboard for exploring a causal relation knowledge graph automatically extracted from a collection of drug reviews. The source data consists of around 19200 reviews from the **Drug Reviews (Druglib.com)** dataset (https://archive.ics.uci.edu/dataset/461/drug+review+dataset+druglib+com) containing patient reviews on specific drugs along with related conditions, crawled from online pharmaceutical review sites.
244
- The causal relations represented in the KG are defined by the **MIMICause** schema (https://huggingface.co/datasets/pensieves/mimicause). The underlying CausalDrugsKG graph is available in Turtle and RDF serialization format in the European Data portal: https://data.jrc.ec.europa.eu/dataset/acebeb4e-9789-4b5c-97ec-292ce14e75d0.
245
-
246
-
247
- ---------------------------
248
-
249
- ## Entities/Relation Types
250
- Bar plots of the Entity and Relation type counts.
251
-
252
- ## Top Key Entities
253
- Bar plots representing the occurence counts of the top 30 Entities in the KG, where occurrence means the entity is either the Subject or Object of an extracted triple in the KG.
254
-
255
- ## Causal Relations Chord Diagrams
256
- Entity Chord Diagrams represent the most frequently connected entity pairs within the KG through chord illustrations, serving as both Subjects and Objects of predicative triples. The size of the chords corresponds to the support of the depicted relations.
257
- """, width=800), align="center")
258
-
259
- def CreatePage2():
260
- return pn.Column(
261
- pn.pane.Markdown("## Entity/Relation Types "),
262
- entRelsButton,
263
- pn.bind(create_type_bar_charts, entRelsButton),
264
- align="center",
265
- )
266
-
267
- def CreatePage3():
268
- return pn.Column(
269
- pn.pane.Markdown("## Top Entities "),
270
- entTypeButton,
271
- pn.bind(create_ent_bar_charts, entTypeButton),
272
- align="center", )
273
- def CreatePage4():
274
- return pn.Column(
275
- pn.pane.Markdown("## Bio Ontology Coverage "),
276
- entTypeButton,
277
- pn.bind(create_ontology_bar_charts, entTypeButton),
278
- align="center", )
279
- def CreatePage5():
280
- return pn.Column(
281
- pn.pane.Markdown("## Causal Relation Chord Diagrams"),
282
- chordify_triples(grouping_filtered),
283
- align="center", )
284
-
285
- def CreatePage5a():
286
- rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Cause']
287
- return pn.Column(
288
- pn.pane.Markdown("## Cause Relation Chord Diagrams"),
289
- chordify_triples(rel_grouping),
290
- align="center", )
291
-
292
- def CreatePage5b():
293
- rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Enable']
294
- return pn.Column(
295
- pn.pane.Markdown("## Enable Relation Chord Diagrams"),
296
- chordify_triples(rel_grouping),
297
- align="center", )
298
-
299
- def CreatePage5c():
300
- rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Prevent']
301
- return pn.Column(
302
- pn.pane.Markdown("## Prevent Relation Chord Diagrams"),
303
- chordify_triples(rel_grouping),
304
- align="center", )
305
-
306
- def CreatePage5d():
307
- rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Hinder']
308
- return pn.Column(
309
- pn.pane.Markdown("## Hinder Relation Chord Diagrams"),
310
- chordify_triples(rel_grouping),
311
- align="center", )
312
-
313
- def CreatePage5e():
314
- rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Other']
315
- return pn.Column(
316
- pn.pane.Markdown("## Causal Relation Chord Diagrams"),
317
- chordify_triples(rel_grouping),
318
- align="center", )
319
- mapping = {
320
- "Page1": CreatePage1(),
321
- "Page2": CreatePage2(),
322
- "Page3": CreatePage3(),
323
- "Page4": CreatePage4(),
324
- "Page5": CreatePage5(),
325
- "Page5a": CreatePage5a(),
326
- "Page5b": CreatePage5b(),
327
- "Page5c": CreatePage5c(),
328
- "Page5d": CreatePage5d(),
329
- "Page5e": CreatePage5e(),
330
- }
331
-
332
- #################### SIDEBAR LAYOUT ##########################
333
- sidebar = pn.Column(pn.pane.Markdown("## Panels"), button1,button2,button3,
334
- button4,tree_layout,
335
- styles={"width": "100%", "padding": "15px"})
336
-
337
- #################### MAIN AREA LAYOUT ##########################
338
- main_area = pn.Column(mapping["Page1"], styles={"width":"100%"})
339
-
340
- ###################### APP LAYOUT ##############################
341
- template = pn.template.BootstrapTemplate(
342
- title=" CausalDrugsKG_Dashboard ",
343
- sidebar=[sidebar],
344
- main=[main_area],
345
- #header_background="black",
346
- #site="Charting the Landscape of Digital Health",
347
- #theme=pn.template.DarkTheme,
348
- sidebar_width=270, ## Default is 330
349
- busy_indicator=pn.indicators.BooleanStatus(value=True),
350
- )
351
-
352
- ### DEPLOY APP
353
-
354
- # Serve the Panel app
355
- template.servable()
356
  import csv
357
  import ast
358
  from tqdm import tqdm
@@ -385,7 +40,7 @@ data = './data'
385
 
386
  def read_freq_map(filename):
387
  df = pd.read_csv(os.path.join(data,filename), sep=' ')
388
- print(df)
389
  if 'Unnamed: 0' in df.columns:
390
  df = df.drop('Unnamed: 0', axis=1)
391
  column_0 = df.columns[0]
@@ -395,7 +50,7 @@ def read_freq_map(filename):
395
 
396
  def read_ont_freq_dataframe(filename):
397
  df = pd.read_csv(os.path.join(data,filename), sep=' ')
398
- print(df)
399
  if 'Unnamed: 0' in df.columns:
400
  df = df.drop('Unnamed: 0', axis=1)
401
  column_0 = df.columns[0]
 
8
  warnings.simplefilter(action='ignore', category=FutureWarning)
9
  warnings.simplefilter(action='ignore', category=RuntimeWarning)
10
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  import csv
12
  import ast
13
  from tqdm import tqdm
 
40
 
41
  def read_freq_map(filename):
42
  df = pd.read_csv(os.path.join(data,filename), sep=' ')
43
+ #print(df)
44
  if 'Unnamed: 0' in df.columns:
45
  df = df.drop('Unnamed: 0', axis=1)
46
  column_0 = df.columns[0]
 
50
 
51
  def read_ont_freq_dataframe(filename):
52
  df = pd.read_csv(os.path.join(data,filename), sep=' ')
53
+ #print(df)
54
  if 'Unnamed: 0' in df.columns:
55
  df = df.drop('Unnamed: 0', axis=1)
56
  column_0 = df.columns[0]