Spaces:
Sleeping
Sleeping
Jonas commited on
Commit ·
c6579d1
1
Parent(s): 8e004b8
fix last remaining bugs for adverse data and report sources (hopefully)
Browse files- openfda_client.py +7 -5
openfda_client.py
CHANGED
|
@@ -169,8 +169,8 @@ def get_top_adverse_events(drug_name: str, limit: int = 10, patient_sex: Optiona
|
|
| 169 |
return cache[cache_key]
|
| 170 |
|
| 171 |
query = (
|
| 172 |
-
f'search=
|
| 173 |
-
f'&count=patient.reaction.
|
| 174 |
)
|
| 175 |
|
| 176 |
try:
|
|
@@ -192,7 +192,7 @@ def get_top_adverse_events(drug_name: str, limit: int = 10, patient_sex: Optiona
|
|
| 192 |
|
| 193 |
except requests.exceptions.HTTPError as http_err:
|
| 194 |
if response.status_code == 404:
|
| 195 |
-
return {"error": f"No data found for
|
| 196 |
return {"error": f"HTTP error occurred: {http_err}"}
|
| 197 |
except requests.exceptions.RequestException as req_err:
|
| 198 |
return {"error": f"A network request error occurred: {req_err}"}
|
|
@@ -379,7 +379,7 @@ def get_report_source_data(drug_name: str) -> dict:
|
|
| 379 |
|
| 380 |
query = (
|
| 381 |
f'search=patient.drug.medicinalproduct:"{drug_name_processed}"'
|
| 382 |
-
f'&count=
|
| 383 |
)
|
| 384 |
|
| 385 |
try:
|
|
@@ -393,7 +393,9 @@ def get_report_source_data(drug_name: str) -> dict:
|
|
| 393 |
# Translate the qualification codes to human-readable terms
|
| 394 |
if "results" in data:
|
| 395 |
for item in data["results"]:
|
| 396 |
-
|
|
|
|
|
|
|
| 397 |
|
| 398 |
cache[cache_key] = data
|
| 399 |
return data
|
|
|
|
| 169 |
return cache[cache_key]
|
| 170 |
|
| 171 |
query = (
|
| 172 |
+
f'search={search_query}'
|
| 173 |
+
f'&count=patient.reaction.reactionmeddrapt.exact&limit={limit}'
|
| 174 |
)
|
| 175 |
|
| 176 |
try:
|
|
|
|
| 192 |
|
| 193 |
except requests.exceptions.HTTPError as http_err:
|
| 194 |
if response.status_code == 404:
|
| 195 |
+
return {"error": f"No data found for '{drug_name}' with the specified filters. The drug may not be in the database, or there may be no reports matching the filter criteria."}
|
| 196 |
return {"error": f"HTTP error occurred: {http_err}"}
|
| 197 |
except requests.exceptions.RequestException as req_err:
|
| 198 |
return {"error": f"A network request error occurred: {req_err}"}
|
|
|
|
| 379 |
|
| 380 |
query = (
|
| 381 |
f'search=patient.drug.medicinalproduct:"{drug_name_processed}"'
|
| 382 |
+
f'&count=primarysource.qualification'
|
| 383 |
)
|
| 384 |
|
| 385 |
try:
|
|
|
|
| 393 |
# Translate the qualification codes to human-readable terms
|
| 394 |
if "results" in data:
|
| 395 |
for item in data["results"]:
|
| 396 |
+
# The API returns numeric codes, ensure they are strings for mapping
|
| 397 |
+
term_str = str(item["term"])
|
| 398 |
+
item["term"] = QUALIFICATION_MAPPING.get(term_str, f"Unknown ({term_str})")
|
| 399 |
|
| 400 |
cache[cache_key] = data
|
| 401 |
return data
|