Spaces:
Sleeping
Sleeping
File size: 19,643 Bytes
5d5bc80 0f6df01 4435d63 22cab1f bca8228 4435d63 88e2993 3bb596b 88e2993 4435d63 6fc0a4d 52413b9 257cd77 235cff6 26805b1 235cff6 257cd77 35eeba4 257cd77 2774625 ace1d29 3618400 ace1d29 257cd77 26805b1 257cd77 2a2a793 257cd77 9d5ea5c 257cd77 d729459 5274a68 357c7d7 5274a68 d729459 5274a68 305b4fb 5274a68 dbbb4e0 5274a68 257cd77 d729459 257cd77 2774625 a344de1 897e0e1 a344de1 3e91553 2774625 3e91553 2774625 e1cd830 2774625 e1cd830 2774625 6fc0a4d bca8228 29cb0aa 72c17f7 29cb0aa 72c17f7 29cb0aa 72c17f7 29cb0aa 72c17f7 29cb0aa c2ffdce bdae620 c2ffdce 29cb0aa c2ffdce 02ec192 29cb0aa 0f6df01 6fc0a4d 257cd77 6fc0a4d a907f6b fe19722 257cd77 6fc0a4d a907f6b 6fc0a4d 257cd77 6fc0a4d 0f6df01 0e6c4c9 d729459 bca8228 29cb0aa 26805b1 255f724 26805b1 b3fbd66 26805b1 88752c7 26805b1 1445d8e 29cb0aa 1445d8e a907f6b 1445d8e 6fc0a4d 257cd77 6fc0a4d 976d8d5 6fc0a4d 1d917dd 6fc0a4d 1d917dd 6fc0a4d 1d917dd 6fc0a4d 1d917dd 6fc0a4d 0f6df01 6fc0a4d f87790d a985384 d73cf3d fd89ace 6fc0a4d 0f6df01 6fc0a4d 257cd77 5d5bc80 6fc0a4d 0e6c4c9 b3fbd66 0e6c4c9 e377201 0f3621a 88cdcd8 0e6c4c9 0f3621a db06eb6 e377201 0f6df01 34f85e7 0e6c4c9 34f85e7 2774625 6fc0a4d 257cd77 2774625 5274a68 6fc0a4d 4435d63 6fc0a4d 257cd77 bca8228 29cb0aa bca8228 be84ec3 bca8228 f390488 257cd77 fe19722 a907f6b fe19722 a907f6b 31deb20 fe19722 a907f6b fe19722 a907f6b 0f6df01 4bfc55c fe19722 a907f6b fe19722 6fc0a4d 11ac7d8 a985384 2774625 5edd7e8 6fc0a4d fe19722 38199ab 257cd77 6fc0a4d 0f6df01 6fc0a4d 0f3621a 6fc0a4d bca8228 0f6df01 22cab1f 0f3621a 6fc0a4d 4435d63 6fc0a4d |
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
import warnings
warnings.filterwarnings("ignore")
import io
import os
import time
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.simplefilter(action='ignore', category=RuntimeWarning)
import pandas as pd
import csv
import ast
from tqdm import tqdm
from operator import itemgetter
import numpy as np
import re
import datetime
import html
from joblib import Parallel, delayed
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
#plt.style.use('seaborn-paper')
import holoviews as hv
from holoviews import opts, dim
from bokeh.sampledata.les_mis import data
from bokeh.io import show
from bokeh.sampledata.les_mis import data
import panel as pn
#pn.extension(design='material')
import bokeh
from math import pi
from bokeh.palettes import Category20c
from bokeh.palettes import Category10
from bokeh.plotting import figure, show
from bokeh.transform import cumsum
from bokeh.resources import INLINE
from holoviews.operation.timeseries import rolling, rolling_outlier_std
hv.extension('bokeh')
## LOAD DATASETS
data = './data'
def read_freq_map(filename):
df = pd.read_csv(os.path.join(data,filename), sep=' ')
#df = df.head(10)
if 'Unnamed: 0' in df.columns:
df = df.drop('Unnamed: 0', axis=1)
column_0 = df.columns[0]
column_1 = df.columns[1]
freqmap = dict(zip(df[column_0], df[column_1]))
return freqmap
def read_ont_freq_dataframe(filename):
df = pd.read_csv(os.path.join(data,filename), sep=' ')
#print(df)
if 'Unnamed: 0' in df.columns:
df = df.drop('Unnamed: 0', axis=1)
column_0 = df.columns[0]
column_1 = df.columns[1]
freqmap = dict(zip(df[column_0], df[column_1]))
return freqmap
entityTypesFreqMap = read_freq_map('entityTypes.tsv')
relationTypesFreqMap = read_freq_map('relationTypes.tsv')
topDrugEntities = read_freq_map('topDrugs.tsv')
#print(topDrugEntities)
topConditionEntities = read_freq_map('topConditions.tsv')
topDrugOnts_df = pd.read_csv(os.path.join(data,'topDrugOntologies.tsv'), sep='\t')
topConditionOnts_df = pd.read_csv(os.path.join(data,'topConditionOntologies.tsv'), sep='\t')
grouping_filtered = pd.read_csv(os.path.join(data, 'drugReviewsCausal_relations.tsv'), sep=" ")
################################# CREATE CHARTS ############################
def create_type_bar_charts(entRelsButton, **kwargs):
if entRelsButton=='Entity':
dictionary = entityTypesFreqMap
return hv.Bars(dictionary, hv.Dimension('Entity Types'), 'Frequency').opts( framewise=True, xrotation=45,width=1200, height=600)
elif entRelsButton=='Relation':
dictionary = relationTypesFreqMap
return hv.Bars(dictionary, hv.Dimension('Relation Types'), 'Frequency').opts(framewise=True, xrotation=45,width=1200, height=600)
def create_type_pie_charts(entRelsButton, **kwargs):
if entRelsButton == 'Entity':
dictionary = entityTypesFreqMap
data = pd.Series(dictionary).reset_index(name='Frequency').rename(columns={'index': 'Entity'})
data['angle'] = data['Frequency']/data['Frequency'].sum() * 2*pi
data['color'] = Category10[3][:2]
p = figure(height=350, title="Pie Chart", toolbar_location=None,
tools="hover", tooltips="@Entity: @Frequency", x_range=(-0.5, 1.0))
p.wedge(x=0, y=1, radius=0.4,start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
line_color="white", fill_color='color', legend_field='Entity', source=data)
p.axis.axis_label = None
p.axis.visible = False
p.grid.grid_line_color = None
return p
elif entRelsButton == 'Relation':
dictionary = relationTypesFreqMap
data = pd.Series(dictionary).reset_index(name='Frequency').rename(columns={'index': 'Relation'})
data['angle'] = data['Frequency']/data['Frequency'].sum() * 2*pi
data['color'] = Category20c[len(dictionary)]
p = figure(height=350, title="Pie Chart", toolbar_location=None,
tools="hover", tooltips="@Relation: @Frequency", x_range=(-0.5, 1.0))
p.wedge(x=0, y=1, radius=0.4,start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
line_color="white", fill_color='color', legend_field='Relation', source=data)
p.axis.axis_label = None
p.axis.visible = False
p.grid.grid_line_color = None
return p
def create_ent_bar_charts(ents, **kwargs):
# Create button widgets for each label
drug_buttons = []
condition_buttons = []
sorted_drugs = sorted(topDrugEntities.items(), key=lambda x: x[1], reverse=True)
sorted_conditions = sorted(topConditionEntities.items(), key=lambda x: x[1], reverse=True)
for i, (drg,count) in enumerate(sorted_drugs):
button = pn.widgets.Button(name=drg, width=150)
## Open the associated URL in a new tab when button is clicked
button.js_on_click(code=f'window.open("https://api-vast.jrc.service.ec.europa.eu/describe/?url=http://causaldrugskg.org/causaldrugskg/resource/{drg}", "_blank");')
drug_buttons.append(button)
for i, (cnd,count) in enumerate(sorted_conditions):
button = pn.widgets.Button(name=cnd, width=150)
## Open the associated URL in a new tab when button is clicked
button.js_on_click(
code=f'window.open("https://api-vast.jrc.service.ec.europa.eu/describe/?url=http://causaldrugskg.org/causaldrugskg/resource/{cnd}", "_blank");')
condition_buttons.append(button)
# Stack the buttons vertically (or wrap in a GridBox for nicer layout)
drug_button_column = pn.Column(*drug_buttons, sizing_mode='stretch_width')
condition_button_column = pn.Column(*condition_buttons, sizing_mode='stretch_width')
if ents=='Drug':
bars = hv.Bars(sorted_drugs, hv.Dimension('Drug Entities'), 'Frequency').opts(framewise=True, xrotation=45,width=1200, height=600, fontsize={'xticks': 18, 'xlabel':18, 'ylabel':16})
# Combine everything into a Panel layout
layout = pn.Row(bars, drug_button_column)
return layout
elif ents=='Condition':
bars = hv.Bars(sorted_conditions, hv.Dimension('Condition Entities'), 'Frequency').opts(framewise=True, xrotation=45,width=1200, height=600, fontsize={'xticks': 18, 'xlabel':18, 'ylabel':16})
layout = pn.Row(bars, condition_button_column)
return layout
def create_ontology_bar_charts(ents, **kwargs):
if ents=='Drug':
df = pd.DataFrame({
'Drug_Ontologies': [ont.split('/')[-1] for ont in topDrugOnts_df['ontology']],
'Frequency': list(topDrugOnts_df['count']),
'url': list(topDrugOnts_df['ontology_url']) # using full keys as hyperlinks
})
drug_ontolgy_buttons = []
for i,row in df.iterrows():
button = pn.widgets.Button(name=row['Drug_Ontologies'], width=150)
## Open the associated URL in a new tab when button is clicked
url = row["url"]
button.js_on_click(
code=f'window.open("{url}", "_blank");')
drug_ontolgy_buttons.append(button)
drug_ontology_column = pn.Column(*drug_ontolgy_buttons, sizing_mode='stretch_width')
# Create bar chart with label as x-axis
bars = hv.Bars(df, kdims=['Drug_Ontologies'], vdims=['Frequency'])
bars.opts(
framewise=True,
tools=['hover'],
width=1200,
height=600,
show_legend=True,
xrotation=45,
xlabel='Drug_Ontologies',
ylabel='Frequency',
hover_tooltips=[
("Drug_Ontologies", "@Drug_Ontologies"),
("Frequency", "@Frequency")
]
)
#links_panel = pn.Column(*[pn.pane.Markdown(f"[{row.Drug_Ontologies}]({row.url})", width=400) for _, row in df.iterrows()],name='Links')
layout = pn.Row(bars, drug_ontology_column)
return layout
elif ents=='Condition':
df = pd.DataFrame({
'Condition_Ontologies': [ont.split('/')[-1] for ont in topConditionOnts_df['ontology']],
'Frequency': list(topConditionOnts_df['count']),
'url': list(topConditionOnts_df['ontology_url']) # using full keys as hyperlinks
})
condition_ontolgy_buttons = []
for i, row in df.iterrows():
button = pn.widgets.Button(name=row['Condition_Ontologies'], width=150)
## Open the associated URL in a new tab when button is clicked
url = row["url"]
button.js_on_click(
code=f'window.open("{url}", "_blank");')
condition_ontolgy_buttons.append(button)
condition_ontology_column = pn.Column(*condition_ontolgy_buttons, sizing_mode='stretch_width')
# Create bar chart with label as x-axis
bars = hv.Bars(df, kdims=['Condition_Ontologies'], vdims=['Frequency'])
bars.opts(
framewise=True,
tools=['hover'],
width=1200,
height=600,
show_legend=True,
xrotation=45,
xlabel='Condition_Ontologies',
ylabel='Frequency',
hover_tooltips=[
("Condition Ontologies", "@Condition Ontologies"),
("Frequency", "@Frequency") ])
#links_panel = pn.Column(*[pn.pane.Markdown(f"[{row.Condition_Ontologies}]({row.url})", width=400) for _, row in df.iterrows()],name='Links')
layout = pn.Row(bars, condition_ontology_column)
return layout
############################# WIDGETS & CALLBACK ###########################################
def filter_data0(df, min_value):
filtered_df = df[df['value'] >= min_value]
return filtered_df
def plot_chord(df,min_value):
filtered_df = filter_data0(df, min_value)
# Create a Holoviews Dataset for nodes
nodes = hv.Dataset(filtered_df, 'index')
nodes.data.head()
chord = hv.Chord(filtered_df, ['source', 'target'], ['value'])
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))
def chordify_triples(rel_grouping, min_val):
# Define range for minimum value slider
min_value_range = rel_grouping['value'].unique()
min_value_range.sort()
min_value_range = min_value_range[min_value_range > min_val]
# Define HoloMap with minimum value and attribute as key dimensions
holomap = hv.HoloMap({min_value: plot_chord(rel_grouping, min_value)
for min_value in min_value_range},
kdims=['Show triples with support greater than']
)
return holomap
# https://tabler-icons.io/
button1 = pn.widgets.Button(name="Introduction", button_type="warning", icon="file-info", styles={"width": "100%"})
button2 = pn.widgets.Button(name="Top Key Entities", button_type="warning", icon="chart-bar", styles={"width": "100%"})
button3 = pn.widgets.Button(name="Entity/Relation Types", button_type="warning", icon="chart-histogram", styles={"width": "100%"})
button4 = pn.widgets.Button(name="Ontology Coverage", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"})
#button5 = pn.widgets.Button(name="Causal Relation Chord Diagrams", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"})
markdown_button_style = """
<div style="background-color: #f0f0f0; /* Matches 'warning' button type */
color: white;
font-weight: bold;
padding: 8px 12px;
border-radius: 4px;
text-align: center;
width: 100%;
">
Causal Relation Chord Diagrams
</div>
"""
#button5 = pn.pane.Markdown(markdown_button_style, width_policy="max")
button5 = pn.pane.Markdown("<div style='background-color:#f7c045; color: black; padding:8px 12px; font-weight: bold; border:1px solid #ccc; " "border-radius:8px; text-align:center; width:100%; white-space: nowrap;'>Causal Relation Chord Diagrams</div>", width=225, height=30, margin=(9, 9))
# Define child buttons
child_button_1 = pn.widgets.Button(name="Cause", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"})
child_button_2 = pn.widgets.Button(name="Enable", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"})
child_button_3 = pn.widgets.Button(name="Prevent", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"})
child_button_4 = pn.widgets.Button(name="Hinder", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"})
child_button_5 = pn.widgets.Button(name="Other", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"})
# Layout: dendrogram-style using vertical + indent
tree_layout = pn.Column(
button5,
pn.Row(pn.Spacer(width=70), # indent
pn.Column(child_button_1, child_button_2,child_button_3,child_button_4, child_button_5))
)
entRelsButton = pn.widgets.RadioButtonGroup(name='### Select', options=['Entity','Relation'], value = 'Entity' )
entTypeButton = pn.widgets.RadioButtonGroup(name='### Select Entity Type', options=list(entityTypesFreqMap.keys()), value='Drug')
#relationTypeButton = pn.widgets.RadioButtonGroup(options=list(relationTypesFreqMap.keys()), value='Cause', name='Select Causal Relation')
# Define the callback function to update the HoloMap
#def update_holomap(event):
# initial_holomap.object = filter_triples(event.new)
# Create the initial HoloMap
#initial_holomap = filter_triples(relationTypeButton.value)
# Bind the callback function to the value change event of the RadioButton widget
#relationTypeButton.param.watch(update_holomap, 'value')
def show_page(page_key):
main_area.clear()
main_area.append(mapping[page_key])
button1.on_click(lambda event: show_page("Page1"))
button2.on_click(lambda event: show_page("Page2"))
button3.on_click(lambda event: show_page("Page3"))
button4.on_click(lambda event: show_page("Page4"))
#button5.on_click(lambda event: show_page("Page5"))
child_button_1.on_click(lambda event: show_page("Page5a"))
child_button_2.on_click(lambda event: show_page("Page5b"))
child_button_3.on_click(lambda event: show_page("Page5c"))
child_button_4.on_click(lambda event: show_page("Page5d"))
child_button_5.on_click(lambda event: show_page("Page5e"))
### CREATE PAGE LAYOUTS
def CreatePage1():
return pn.Column(pn.pane.Markdown("""
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.
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.
---------------------------
## Top Key Entities
Bar plots representing the occurence counts of the top 30 Drug and Condition entities in the KG, where occurrence means the entity is either the Subject or Object of an extracted triple in the KG.
Clicking on the entity name in the right legend redirects to the corresponding entry in the Virtuoso Faceted Browser endpoint of the KG
## Entities/Relation Types
Bar plots of the Entity and Relation type counts.
## Ontology Coverage
Bar plots representing the linking of KG entities to standard Biomedical ontologies. Bar heights indicate the number of Drug/Condition entities linked to the corresponding ontology.
Linking is performed using the Bioportal API (https://bioportal.bioontology.org/)
Clicking on the ontology name on the right legend redirects to the ontology entry page.
## Causal Relations Chord Diagrams
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.
""", width=800), align="center")
def CreatePage2():
return pn.Column(
pn.pane.Markdown("## Top 30 Entities "),
entTypeButton,
pn.bind(create_ent_bar_charts, entTypeButton),
align="center", )
def CreatePage3():
return pn.Column(
pn.pane.Markdown("## Entity/Relation Types "),
entRelsButton,
pn.bind(create_type_pie_charts, entRelsButton),
align="center",
)
def CreatePage4():
return pn.Column(
pn.pane.Markdown("## Bio-Medical Ontology Coverage "),
entTypeButton,
pn.bind(create_ontology_bar_charts, entTypeButton),
align="center", )
def CreatePage5():
return pn.Column(
pn.pane.Markdown("## Causal Relation Chord Diagrams"),
chordify_triples(grouping_filtered),
align="center", )
def CreatePage5a():
rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Cause']
return pn.Column(
pn.pane.Markdown("## Relation Chord Diagram: Cause"),
chordify_triples(rel_grouping,10),
align="center", )
def CreatePage5b():
rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Enable']
return pn.Column(
pn.pane.Markdown("## Relation Chord Diagram: Enable"),
chordify_triples(rel_grouping,4),
align="center", )
def CreatePage5c():
rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Prevent']
return pn.Column(
pn.pane.Markdown("## Relation Chord Diagram: Prevent"),
chordify_triples(rel_grouping,50),
align="center", )
def CreatePage5d():
rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Hinder']
return pn.Column(
pn.pane.Markdown("## Relation Chord Diagram: Hinder"),
chordify_triples(rel_grouping,10),
align="center", )
def CreatePage5e():
rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Other']
return pn.Column(
pn.pane.Markdown("## Relation Chord Diagram: Other"),
chordify_triples(rel_grouping,10),
align="center", )
mapping = {
"Page1": CreatePage1(),
"Page2": CreatePage2(),
"Page3": CreatePage3(),
"Page4": CreatePage4(),
#"Page5": CreatePage5(),
"Page5a": CreatePage5a(),
"Page5b": CreatePage5b(),
"Page5c": CreatePage5c(),
"Page5d": CreatePage5d(),
"Page5e": CreatePage5e(),
}
#################### SIDEBAR LAYOUT ##########################
sidebar = pn.Column(pn.pane.Markdown("## Panels"), button1,button2,button3,
button4,tree_layout,
styles={"width": "100%", "padding": "15px"})
#################### MAIN AREA LAYOUT ##########################
main_area = pn.Column(mapping["Page1"], styles={"width":"100%"})
###################### APP LAYOUT ##############################
template = pn.template.BootstrapTemplate(
title=" CausalDrugsKG_Dashboard ",
sidebar=[sidebar],
main=[main_area],
#header_background="black",
#site="Charting the Landscape of Digital Health",
#theme=pn.template.DarkTheme,
sidebar_width=270, ## Default is 330
busy_indicator=pn.indicators.BooleanStatus(value=True),
)
### DEPLOY APP
# Serve the Panel app
template.servable() |