Skip to content

Commit 288d00a

Browse files
feat: benchmark setup (merge)
Merge pull request #23 from DaneshjouLab/shlok/feat/benchmark-setup
2 parents 4999b8b + ab00b8a commit 288d00a

3 files changed

Lines changed: 77 additions & 2 deletions

File tree

src/benchmark/__init__.py

Whitespace-only changes.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from typing import List
2+
from src.utils import get_pmcid_annotation
3+
4+
5+
class AnnotationBenchmark:
6+
def __init__(self):
7+
pass
8+
9+
def get_var_drug_ann_score(self, var_drug_ann: List[dict]):
10+
return 1.0
11+
12+
def get_var_pheno_ann_score(self, var_pheno_ann: List[dict]):
13+
return 1.0
14+
15+
def get_var_fa_ann_score(self, var_fa_ann: List[dict]):
16+
return 1.0
17+
18+
def get_study_parameters_score(self, study_parameters: List[dict]):
19+
return 1.0
20+
21+
def calculate_total_score(
22+
self,
23+
var_drug_ann: List[dict],
24+
var_pheno_ann: List[dict],
25+
var_fa_ann: List[dict],
26+
study_parameters: List[dict],
27+
):
28+
# Return average of all scores
29+
scores = [
30+
self.get_var_drug_ann_score(var_drug_ann),
31+
self.get_var_pheno_ann_score(var_pheno_ann),
32+
self.get_var_fa_ann_score(var_fa_ann),
33+
self.get_study_parameters_score(study_parameters),
34+
]
35+
return sum(scores) / len(scores)
36+
37+
def run(self, pmcid: str):
38+
pmcid_annotation = get_pmcid_annotation(pmcid)
39+
40+
var_drug_ann = pmcid_annotation.get("varDrugAnn", [])
41+
var_pheno_ann = pmcid_annotation.get("varPhenoAnn", [])
42+
var_fa_ann = pmcid_annotation.get("varFaAnn", [])
43+
study_parameters = pmcid_annotation.get("studyParameters", [])
44+
45+
total_score = self.calculate_total_score(
46+
var_drug_ann, var_pheno_ann, var_fa_ann, study_parameters
47+
)
48+
print(f"Score for pmcid {pmcid}: {total_score}")
49+
return total_score
50+
51+
def run_all(self):
52+
benchmark_pmcids = []
53+
with open("persistent_data/benchmark_pmcids.txt", "r") as f:
54+
benchmark_pmcids = f.read().splitlines()
55+
scores = []
56+
for pmcid in benchmark_pmcids:
57+
scores.append(self.run(pmcid))
58+
59+
overall_score = sum(scores) / len(scores)
60+
print(f"Average score: {overall_score}")
61+
return overall_score
62+
63+
64+
if __name__ == "__main__":
65+
benchmark = AnnotationBenchmark()
66+
benchmark.run_all()

src/utils.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@
55
from termcolor import colored
66
from src.article_parser import MarkdownParser
77
from pydantic import BaseModel, ValidationError
8+
from pathlib import Path
89

910
_true_variant_cache: Optional[dict] = None
1011

1112

13+
def get_pmcid_annotation(
14+
pmcid: str, annotations_by_pmcid: Path = Path("data/annotations_by_pmcid.json")
15+
) -> dict:
16+
with open(annotations_by_pmcid, "r") as f:
17+
annotations_by_pmcid = json.load(f)
18+
return annotations_by_pmcid.get(pmcid, {})
19+
20+
1221
def extractVariantsRegex(text):
1322
# Note, seems to extract a ton of variants, not just the ones that are being studied
1423
# Think it might only be applicable to rsIDs
@@ -79,7 +88,7 @@ def compare_lists(
7988
return true_positives, true_negatives, false_positives, false_negatives
8089

8190

82-
def get_true_variants(pmcid: str) -> List[str]:
91+
def get_true_variants(pmcid: str, annotations_by_pmcid: Path) -> List[str]:
8392
"""
8493
Get the actual annotated variants for a given PMCID.
8594
Uses module-level caching to load the JSON file only once.
@@ -88,7 +97,7 @@ def get_true_variants(pmcid: str) -> List[str]:
8897

8998
if _true_variant_cache is None:
9099
try:
91-
with open("data/benchmark/true_variant_list.json", "r") as f:
100+
with open(annotations_by_pmcid, "r") as f:
92101
_true_variant_cache = json.load(f)
93102
except FileNotFoundError:
94103
logger.error(

0 commit comments

Comments
 (0)