Spaces:
Sleeping
Sleeping
Jonas
commited on
Commit
·
2262f59
1
Parent(s):
5565cf6
Add error handling and placeholder charts in app.py; implement create_placeholder_chart in plotting.py
Browse files- app.py +16 -5
- plotting.py +27 -0
app.py
CHANGED
|
@@ -10,7 +10,8 @@ from plotting import (
|
|
| 10 |
create_bar_chart,
|
| 11 |
create_outcome_chart,
|
| 12 |
create_time_series_chart,
|
| 13 |
-
create_pie_chart
|
|
|
|
| 14 |
)
|
| 15 |
import pandas as pd
|
| 16 |
|
|
@@ -106,8 +107,12 @@ def top_adverse_events_tool(drug_name: str, patient_sex: str = "all", min_age: i
|
|
| 106 |
age_range = (min_age, max_age)
|
| 107 |
|
| 108 |
data = get_top_adverse_events(drug_name, patient_sex=sex_code, age_range=age_range)
|
| 109 |
-
chart = create_bar_chart(data, drug_name)
|
| 110 |
text_summary = format_top_events_results(data, drug_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
return chart, text_summary
|
| 112 |
|
| 113 |
def serious_outcomes_tool(drug_name: str):
|
|
@@ -124,7 +129,7 @@ def serious_outcomes_tool(drug_name: str):
|
|
| 124 |
|
| 125 |
if "error" in data or not data.get("results"):
|
| 126 |
text_summary = format_serious_outcomes_results(data, drug_name)
|
| 127 |
-
return
|
| 128 |
|
| 129 |
chart = create_outcome_chart(data, drug_name)
|
| 130 |
text_summary = format_serious_outcomes_results(data, drug_name)
|
|
@@ -158,6 +163,10 @@ def time_series_tool(drug_name: str, event_name: str, aggregation: str):
|
|
| 158 |
"""
|
| 159 |
agg_code = 'Y' if aggregation == 'Yearly' else 'Q'
|
| 160 |
data = get_time_series_data(drug_name, event_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
chart = create_time_series_chart(data, drug_name, event_name, time_aggregation=agg_code)
|
| 162 |
return chart
|
| 163 |
|
|
@@ -169,11 +178,13 @@ def report_source_tool(drug_name: str):
|
|
| 169 |
drug_name (str): The name of the drug.
|
| 170 |
|
| 171 |
Returns:
|
| 172 |
-
A Plotly figure
|
| 173 |
"""
|
| 174 |
data = get_report_source_data(drug_name)
|
|
|
|
| 175 |
if not data or not data.get("results"):
|
| 176 |
-
return f"No report source data found for '{drug_name}'.
|
|
|
|
| 177 |
chart = create_pie_chart(data, drug_name)
|
| 178 |
return chart
|
| 179 |
|
|
|
|
| 10 |
create_bar_chart,
|
| 11 |
create_outcome_chart,
|
| 12 |
create_time_series_chart,
|
| 13 |
+
create_pie_chart,
|
| 14 |
+
create_placeholder_chart
|
| 15 |
)
|
| 16 |
import pandas as pd
|
| 17 |
|
|
|
|
| 107 |
age_range = (min_age, max_age)
|
| 108 |
|
| 109 |
data = get_top_adverse_events(drug_name, patient_sex=sex_code, age_range=age_range)
|
|
|
|
| 110 |
text_summary = format_top_events_results(data, drug_name)
|
| 111 |
+
|
| 112 |
+
if "error" in data or not data.get("results"):
|
| 113 |
+
return None, text_summary
|
| 114 |
+
|
| 115 |
+
chart = create_bar_chart(data, drug_name)
|
| 116 |
return chart, text_summary
|
| 117 |
|
| 118 |
def serious_outcomes_tool(drug_name: str):
|
|
|
|
| 129 |
|
| 130 |
if "error" in data or not data.get("results"):
|
| 131 |
text_summary = format_serious_outcomes_results(data, drug_name)
|
| 132 |
+
return None, text_summary
|
| 133 |
|
| 134 |
chart = create_outcome_chart(data, drug_name)
|
| 135 |
text_summary = format_serious_outcomes_results(data, drug_name)
|
|
|
|
| 163 |
"""
|
| 164 |
agg_code = 'Y' if aggregation == 'Yearly' else 'Q'
|
| 165 |
data = get_time_series_data(drug_name, event_name)
|
| 166 |
+
|
| 167 |
+
if "error" in data or not data.get("results"):
|
| 168 |
+
return create_placeholder_chart(f"No time-series data found for '{drug_name}' and '{event_name}'.")
|
| 169 |
+
|
| 170 |
chart = create_time_series_chart(data, drug_name, event_name, time_aggregation=agg_code)
|
| 171 |
return chart
|
| 172 |
|
|
|
|
| 178 |
drug_name (str): The name of the drug.
|
| 179 |
|
| 180 |
Returns:
|
| 181 |
+
A Plotly figure.
|
| 182 |
"""
|
| 183 |
data = get_report_source_data(drug_name)
|
| 184 |
+
|
| 185 |
if not data or not data.get("results"):
|
| 186 |
+
return create_placeholder_chart(f"No report source data found for '{drug_name}'.")
|
| 187 |
+
|
| 188 |
chart = create_pie_chart(data, drug_name)
|
| 189 |
return chart
|
| 190 |
|
plotting.py
CHANGED
|
@@ -1,5 +1,32 @@
|
|
| 1 |
import plotly.graph_objects as go
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def create_bar_chart(data: dict, drug_name: str):
|
| 5 |
"""
|
|
|
|
| 1 |
import plotly.graph_objects as go
|
| 2 |
import pandas as pd
|
| 3 |
+
from typing import Dict, Any, Optional
|
| 4 |
+
|
| 5 |
+
def create_placeholder_chart(message: str) -> go.Figure:
|
| 6 |
+
"""
|
| 7 |
+
Creates a placeholder chart with a text message.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
message (str): The message to display on the chart.
|
| 11 |
+
|
| 12 |
+
Returns:
|
| 13 |
+
go.Figure: A Plotly figure object with the message.
|
| 14 |
+
"""
|
| 15 |
+
fig = go.Figure()
|
| 16 |
+
fig.add_annotation(
|
| 17 |
+
text=message,
|
| 18 |
+
xref="paper",
|
| 19 |
+
yref="paper",
|
| 20 |
+
showarrow=False,
|
| 21 |
+
font=dict(size=16)
|
| 22 |
+
)
|
| 23 |
+
fig.update_layout(
|
| 24 |
+
xaxis=dict(showgrid=False, zeroline=False, visible=False),
|
| 25 |
+
yaxis=dict(showgrid=False, zeroline=False, visible=False),
|
| 26 |
+
plot_bgcolor="rgba(0,0,0,0)",
|
| 27 |
+
paper_bgcolor="rgba(0,0,0,0)",
|
| 28 |
+
)
|
| 29 |
+
return fig
|
| 30 |
|
| 31 |
def create_bar_chart(data: dict, drug_name: str):
|
| 32 |
"""
|