Jonas
commited on
Commit
·
3455d98
1
Parent(s):
2262f59
Refactor result formatting in app.py to return DataFrames and headers; update interface outputs to use DataFrames and Markdown for better presentation.
Browse files- app.py +42 -57
- openfda_client.py +3 -3
app.py
CHANGED
|
@@ -17,48 +17,6 @@ import pandas as pd
|
|
| 17 |
|
| 18 |
# --- Formatting Functions ---
|
| 19 |
|
| 20 |
-
def format_top_events_results(data: dict, drug_name: str) -> str:
|
| 21 |
-
"""Formats the results for the top adverse events tool."""
|
| 22 |
-
if "error" in data:
|
| 23 |
-
return f"An error occurred: {data['error']}"
|
| 24 |
-
|
| 25 |
-
if "results" not in data or not data["results"]:
|
| 26 |
-
return f"No adverse event data found for '{drug_name}'. The drug may not be in the database or it might be misspelled."
|
| 27 |
-
|
| 28 |
-
header = f"Top Adverse Events for '{drug_name.title()}'\n"
|
| 29 |
-
header += "Source: FDA FAERS via OpenFDA\n"
|
| 30 |
-
header += "Disclaimer: Spontaneous reports do not prove causation. Consult a healthcare professional.\n"
|
| 31 |
-
header += "---------------------------------------------------\n"
|
| 32 |
-
|
| 33 |
-
try:
|
| 34 |
-
df = pd.DataFrame(data["results"])
|
| 35 |
-
df = df.rename(columns={"term": "Adverse Event", "count": "Report Count"})
|
| 36 |
-
result_string = df.to_string(index=False)
|
| 37 |
-
return header + result_string
|
| 38 |
-
except Exception as e:
|
| 39 |
-
return f"An error occurred while formatting the data: {e}"
|
| 40 |
-
|
| 41 |
-
def format_serious_outcomes_results(data: dict, drug_name: str) -> str:
|
| 42 |
-
"""Formats the results for the serious outcomes tool."""
|
| 43 |
-
if "error" in data:
|
| 44 |
-
return f"An error occurred: {data['error']}"
|
| 45 |
-
|
| 46 |
-
if "results" not in data or not data["results"]:
|
| 47 |
-
return f"No serious outcome data found for '{drug_name}'. The drug may not be in the database or it might be misspelled."
|
| 48 |
-
|
| 49 |
-
header = f"Top Serious Outcomes for '{drug_name.title()}'\n"
|
| 50 |
-
header += "Source: FDA FAERS via OpenFDA\n"
|
| 51 |
-
header += "Disclaimer: Spontaneous reports do not prove causation. Consult a healthcare professional.\n"
|
| 52 |
-
header += "---------------------------------------------------\n"
|
| 53 |
-
|
| 54 |
-
try:
|
| 55 |
-
df = pd.DataFrame(data["results"])
|
| 56 |
-
df = df.rename(columns={"term": "Serious Outcome", "count": "Report Count"})
|
| 57 |
-
result_string = df.to_string(index=False)
|
| 58 |
-
return header + result_string
|
| 59 |
-
except Exception as e:
|
| 60 |
-
return f"An error occurred while formatting the data: {e}"
|
| 61 |
-
|
| 62 |
def format_pair_frequency_results(data: dict, drug_name: str, event_name: str) -> str:
|
| 63 |
"""Formats the results for the drug-event pair frequency tool."""
|
| 64 |
if "error" in data:
|
|
@@ -87,7 +45,7 @@ def top_adverse_events_tool(drug_name: str, patient_sex: str = "all", min_age: i
|
|
| 87 |
max_age (int): The maximum age for the filter.
|
| 88 |
|
| 89 |
Returns:
|
| 90 |
-
tuple: A Plotly figure
|
| 91 |
"""
|
| 92 |
if patient_sex is None:
|
| 93 |
patient_sex = "all"
|
|
@@ -107,13 +65,26 @@ def top_adverse_events_tool(drug_name: str, patient_sex: str = "all", min_age: i
|
|
| 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 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
chart = create_bar_chart(data, drug_name)
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
def serious_outcomes_tool(drug_name: str):
|
| 119 |
"""
|
|
@@ -123,17 +94,29 @@ def serious_outcomes_tool(drug_name: str):
|
|
| 123 |
drug_name (str): The name of the drug to search for.
|
| 124 |
|
| 125 |
Returns:
|
| 126 |
-
tuple: A Plotly figure
|
| 127 |
"""
|
| 128 |
data = get_serious_outcomes(drug_name)
|
| 129 |
|
| 130 |
-
if "error" in data
|
| 131 |
-
|
| 132 |
-
return
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
chart = create_outcome_chart(data, drug_name)
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
def drug_event_stats_tool(drug_name: str, event_name: str):
|
| 139 |
"""
|
|
@@ -217,7 +200,8 @@ interface1 = gr.Interface(
|
|
| 217 |
],
|
| 218 |
outputs=[
|
| 219 |
gr.Plot(label="Top Adverse Events Chart"),
|
| 220 |
-
gr.
|
|
|
|
| 221 |
],
|
| 222 |
title="Top Adverse Events by Drug",
|
| 223 |
description="Find the most frequently reported adverse events for a specific medication.",
|
|
@@ -234,7 +218,8 @@ interface3 = gr.Interface(
|
|
| 234 |
],
|
| 235 |
outputs=[
|
| 236 |
gr.Plot(label="Top Serious Outcomes Chart"),
|
| 237 |
-
gr.
|
|
|
|
| 238 |
],
|
| 239 |
title="Serious Outcome Analysis",
|
| 240 |
description="Find the most frequently reported serious outcomes (e.g., hospitalization, death) for a specific medication.",
|
|
|
|
| 17 |
|
| 18 |
# --- Formatting Functions ---
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def format_pair_frequency_results(data: dict, drug_name: str, event_name: str) -> str:
|
| 21 |
"""Formats the results for the drug-event pair frequency tool."""
|
| 22 |
if "error" in data:
|
|
|
|
| 45 |
max_age (int): The maximum age for the filter.
|
| 46 |
|
| 47 |
Returns:
|
| 48 |
+
tuple: A Plotly figure, a Pandas DataFrame, and a summary string.
|
| 49 |
"""
|
| 50 |
if patient_sex is None:
|
| 51 |
patient_sex = "all"
|
|
|
|
| 65 |
age_range = (min_age, max_age)
|
| 66 |
|
| 67 |
data = get_top_adverse_events(drug_name, patient_sex=sex_code, age_range=age_range)
|
| 68 |
+
|
| 69 |
+
if "error" in data:
|
| 70 |
+
error_message = f"An error occurred: {data['error']}"
|
| 71 |
+
return create_placeholder_chart(error_message), pd.DataFrame(), error_message
|
| 72 |
+
|
| 73 |
+
if "results" not in data or not data["results"]:
|
| 74 |
+
message = f"No adverse event data found for '{drug_name}'. The drug may not be in the database or it might be misspelled."
|
| 75 |
+
return create_placeholder_chart(message), pd.DataFrame(), message
|
| 76 |
|
| 77 |
chart = create_bar_chart(data, drug_name)
|
| 78 |
+
|
| 79 |
+
df = pd.DataFrame(data["results"])
|
| 80 |
+
df = df.rename(columns={"term": "Adverse Event", "count": "Report Count"})
|
| 81 |
+
|
| 82 |
+
header = (
|
| 83 |
+
f"### Top Adverse Events for '{drug_name.title()}'\n"
|
| 84 |
+
"**Source**: FDA FAERS via OpenFDA\n"
|
| 85 |
+
"**Disclaimer**: Spontaneous reports do not prove causation. Consult a healthcare professional."
|
| 86 |
+
)
|
| 87 |
+
return chart, df, header
|
| 88 |
|
| 89 |
def serious_outcomes_tool(drug_name: str):
|
| 90 |
"""
|
|
|
|
| 94 |
drug_name (str): The name of the drug to search for.
|
| 95 |
|
| 96 |
Returns:
|
| 97 |
+
tuple: A Plotly figure, a Pandas DataFrame, and a summary string.
|
| 98 |
"""
|
| 99 |
data = get_serious_outcomes(drug_name)
|
| 100 |
|
| 101 |
+
if "error" in data:
|
| 102 |
+
error_message = f"An error occurred: {data['error']}"
|
| 103 |
+
return create_placeholder_chart(error_message), pd.DataFrame(), error_message
|
| 104 |
+
|
| 105 |
+
if "results" not in data or not data["results"]:
|
| 106 |
+
message = f"No serious outcome data found for '{drug_name}'. The drug may not be in the database or it might be misspelled."
|
| 107 |
+
return create_placeholder_chart(message), pd.DataFrame(), message
|
| 108 |
+
|
| 109 |
chart = create_outcome_chart(data, drug_name)
|
| 110 |
+
|
| 111 |
+
df = pd.DataFrame(data["results"])
|
| 112 |
+
df = df.rename(columns={"term": "Serious Outcome", "count": "Report Count"})
|
| 113 |
+
|
| 114 |
+
header = (
|
| 115 |
+
f"### Top Serious Outcomes for '{drug_name.title()}'\n"
|
| 116 |
+
"**Source**: FDA FAERS via OpenFDA\n"
|
| 117 |
+
"**Disclaimer**: Spontaneous reports do not prove causation. Consult a healthcare professional."
|
| 118 |
+
)
|
| 119 |
+
return chart, df, header
|
| 120 |
|
| 121 |
def drug_event_stats_tool(drug_name: str, event_name: str):
|
| 122 |
"""
|
|
|
|
| 200 |
],
|
| 201 |
outputs=[
|
| 202 |
gr.Plot(label="Top Adverse Events Chart"),
|
| 203 |
+
gr.DataFrame(label="Top Adverse Events", interactive=False),
|
| 204 |
+
gr.Markdown()
|
| 205 |
],
|
| 206 |
title="Top Adverse Events by Drug",
|
| 207 |
description="Find the most frequently reported adverse events for a specific medication.",
|
|
|
|
| 218 |
],
|
| 219 |
outputs=[
|
| 220 |
gr.Plot(label="Top Serious Outcomes Chart"),
|
| 221 |
+
gr.DataFrame(label="Top Serious Outcomes", interactive=False),
|
| 222 |
+
gr.Markdown()
|
| 223 |
],
|
| 224 |
title="Serious Outcome Analysis",
|
| 225 |
description="Find the most frequently reported serious outcomes (e.g., hospitalization, death) for a specific medication.",
|
openfda_client.py
CHANGED
|
@@ -248,7 +248,7 @@ def get_serious_outcomes(drug_name: str, limit: int = 10) -> dict:
|
|
| 248 |
|
| 249 |
query = (
|
| 250 |
f'search=patient.drug.medicinalproduct:"{drug_name_processed}"'
|
| 251 |
-
f'&count=
|
| 252 |
)
|
| 253 |
|
| 254 |
try:
|
|
@@ -301,7 +301,7 @@ def get_time_series_data(drug_name: str, event_name: str) -> dict:
|
|
| 301 |
query = (
|
| 302 |
f'search=patient.drug.medicinalproduct:"{drug_name_processed}"'
|
| 303 |
f'+AND+patient.reaction.reactionmeddrapt:"{event_name_processed}"'
|
| 304 |
-
f'&count=
|
| 305 |
)
|
| 306 |
|
| 307 |
try:
|
|
@@ -345,7 +345,7 @@ def get_report_source_data(drug_name: str) -> dict:
|
|
| 345 |
|
| 346 |
query = (
|
| 347 |
f'search=patient.drug.medicinalproduct:"{drug_name_processed}"'
|
| 348 |
-
f'&count=
|
| 349 |
)
|
| 350 |
|
| 351 |
try:
|
|
|
|
| 248 |
|
| 249 |
query = (
|
| 250 |
f'search=patient.drug.medicinalproduct:"{drug_name_processed}"'
|
| 251 |
+
f'&count=reactionoutcome.exact&limit={limit}'
|
| 252 |
)
|
| 253 |
|
| 254 |
try:
|
|
|
|
| 301 |
query = (
|
| 302 |
f'search=patient.drug.medicinalproduct:"{drug_name_processed}"'
|
| 303 |
f'+AND+patient.reaction.reactionmeddrapt:"{event_name_processed}"'
|
| 304 |
+
f'&count=receiptdate'
|
| 305 |
)
|
| 306 |
|
| 307 |
try:
|
|
|
|
| 345 |
|
| 346 |
query = (
|
| 347 |
f'search=patient.drug.medicinalproduct:"{drug_name_processed}"'
|
| 348 |
+
f'&count=qualification.exact&limit=5'
|
| 349 |
)
|
| 350 |
|
| 351 |
try:
|