Spaces:
Sleeping
Sleeping
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 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_ent_bar_charts(ents, **kwargs): | |
# Create button widgets for each label | |
drug_buttons = [] | |
condition_buttons = [] | |
for i, drg in enumerate(list(topDrugEntities.keys())): | |
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 in enumerate(list(topConditionEntities.keys())): | |
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': | |
dictionary = topDrugEntities | |
bars = hv.Bars(dictionary, 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': | |
dictionary = topConditionEntities | |
bars = hv.Bars(dictionary, 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_bar_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() |