11from dataclasses import dataclass
22from typing import List
3- from kgpipe .evaluation .aspects .reference import ReferenceEvaluator , ReferenceConfig
4-
3+ from kgpipe .common import KgPipe , Data , DataFormat , KG
4+ from pathlib import Path
5+ from kgpipe .common .models import KgPipePlan
6+ from kgpipe .evaluation .aspects .reference import (
7+ ReferenceEvaluator , ReferenceConfig ,
8+ ER_EntityMatchMetric , ER_RelationMatchMetric ,
9+ TE_ExpectedEntityLinkMetric , TE_ExpectedRelationLinkMetric
10+ )
11+ import os
512@dataclass
613class BinaryClassifier :
714 tp : int
@@ -21,20 +28,117 @@ class ThresholdSensitivityResult:
2128 threshold : float
2229 result : BinaryClassifier
2330
31+ benchdata = Path ("/home/marvin/phd/kgpipe/experiments/moviekg/data/datasets/film_10k/" )
32+ seed_path = benchdata / "split_0/kg/seed/data.nt"
33+ rdf_path = benchdata / "split_1/sources/rdf/data.nt"
34+ result_dir_path = Path (f"data/moviekg/threshold_sensitivity/" )
2435
36+ # reference_evaluator = ReferenceEvaluator()
2537
26- reference_evaluator = ReferenceEvaluator ()
38+ def run_paris_pipeline (pipeline_name : str , threshold : float ) -> List [ThresholdSensitivityResult ]:
39+ from kgpipe_tasks .tasks import paris_entity_matching , paris_exchange
2740
28- def paris_threshold_sensitivity (pipeline_name : str , threshold : float ) -> List [ThresholdSensitivityResult ]:
29- ReferenceConfig (
30- ENTITY_MATCH_THRESHOLD = threshold
31- RELATION_MATCH_THRESHOLD = threshold
32- ) # TODO get config from dataset
33- # kg = KG(path=Path(f"data/moviekg/paris/{pipeline_name}.nt"))
34- # reference_kg = KG(path=Path("data/moviekg/paris/reference.nt"))
35- # result = reference_evaluator.evaluate(kg, reference_kg)
36- # return result
37- pass
41+ pipe_result_dir_path = result_dir_path / f"{ pipeline_name } "
42+ pipeline = KgPipe (
43+ name = "paris pipeline" ,
44+ tasks = [paris_entity_matching , paris_exchange ],
45+ seed = Data (path = seed_path , format = DataFormat .RDF_NTRIPLES ),
46+ data_dir = pipe_result_dir_path / "tmp"
47+ )
48+ plan = pipeline .build (
49+ source = Data (path = rdf_path , format = DataFormat .RDF_NTRIPLES ),
50+ result = Data (path = pipe_result_dir_path / "result.json" , format = DataFormat .ER_JSON )
51+ )
52+
53+ os .makedirs (pipe_result_dir_path , exist_ok = True )
54+
55+ with open (pipe_result_dir_path / "exec-plan.json" , "w" ) as f :
56+ f .write (plan .model_dump_json (indent = 4 ))
57+
58+ pipeline .run ()
59+
60+ def paris_er_threshold_sensitivity (pipeline_name : str , threshold : float ) -> List [ThresholdSensitivityResult ]:
61+ config = ReferenceConfig (
62+ name = "paris config" ,
63+ ENTITY_MATCH_THRESHOLD = threshold ,
64+ RELATION_MATCH_THRESHOLD = threshold ,
65+ GT_MATCHES = benchdata / "split_1/sources/rdf/meta/verified_matches.csv" ,
66+ GT_MATCHES_TARGET_DATASET = "split_0/kg/seed"
67+ )
68+
69+ plan = KgPipePlan .model_validate_json (open (result_dir_path / f"{ pipeline_name } " / "exec-plan.json" ).read ())
70+
71+ kg = KG (id = "paris" , name = "paris" , path = Path (f"data/moviekg/paris/{ pipeline_name } .nt" ), format = DataFormat .RDF_NTRIPLES , plan = plan )
72+
73+ metric_result = ER_EntityMatchMetric ().compute (kg , config = config )
74+ # print(metric_result)
75+
76+ return metric_result
77+
78+ def paris_om_threshold_sensitivity (pipeline_name : str , threshold : float ) -> List [ThresholdSensitivityResult ]:
79+ config = ReferenceConfig (
80+ name = "paris config" ,
81+ ENTITY_MATCH_THRESHOLD = threshold ,
82+ RELATION_MATCH_THRESHOLD = threshold ,
83+ GT_MATCHES = benchdata / "split_1/sources/rdf/meta/verified_matches.csv" ,
84+ GT_MATCHES_TARGET_DATASET = "split_0/kg/seed"
85+ )
86+
87+ plan = KgPipePlan .model_validate_json (open (result_dir_path / f"{ pipeline_name } " / "exec-plan.json" ).read ())
88+
89+ kg = KG (id = "paris" , name = "paris" , path = Path (f"data/moviekg/paris/{ pipeline_name } .nt" ), format = DataFormat .RDF_NTRIPLES , plan = plan )
90+
91+ metric_result = ER_RelationMatchMetric ().compute (kg , config = config )
92+ # print(metric_result)
93+
94+ return metric_result
95+
96+ def test_paris ():
97+ # run_paris_pipeline("paris", 0.99)
98+ range_of_thresholds = [0.0 , 0.001 , 0.01 , 0.05 , 0.1 , 0.2 , 0.3 , 0.4 , 0.5 , 0.6 , 0.7 , 0.8 , 0.9 , 0.95 , 0.99 , 0.999 , 1.0 ]
99+
100+ er_results = []
101+ for threshold in range_of_thresholds :
102+ result = paris_er_threshold_sensitivity ("paris" , threshold )
103+ er_results .append ([threshold , result .normalized_score , result .details ])
104+
105+ print ()
106+ print ("ER Results:" )
107+ for r in er_results :
108+ print (r [0 ], r [1 ], r [2 ])
109+
110+ om_results = []
111+ for threshold in range_of_thresholds :
112+ result = paris_om_threshold_sensitivity ("paris" , threshold )
113+ om_results .append ([threshold , result .normalized_score , result .details ])
114+
115+ print ("OM Results:" )
116+ for r in om_results :
117+ print (r [0 ], r [1 ], r [2 ])
118+
119+ # def paris_threshold_sensitivity(pipeline_name: str, threshold: float) -> List[ThresholdSensitivityResult]:
120+ # result = run_paris_pipeline(pipeline_name, threshold)
121+
122+ # pipeline.run(
123+ # input=[Data(path=Path(f"data/moviekg/paris/{pipeline_name}.nt"), format=DataFormat.RDF_NTRIPLES)],
124+ # output=[Data(path=Path(f"data/moviekg/paris/{pipeline_name}.paris_csv"), format=DataFormat.PARIS_CSV)]
125+ # )
126+
127+ # config = ReferenceConfig(
128+ # name="paris config",
129+ # ENTITY_MATCH_THRESHOLD=threshold,
130+ # RELATION_MATCH_THRESHOLD=threshold
131+ # )
132+
133+
134+
135+
136+ # # TODO get config from dataset
137+ # # kg = KG(path=Path(f"data/moviekg/paris/{pipeline_name}.nt"))
138+ # # reference_kg = KG(path=Path("data/moviekg/paris/reference.nt"))
139+ # # result = reference_evaluator.evaluate(kg, reference_kg)
140+ # # return result
141+ # pass
38142
39143def jedai_threshold_sensitivity (pipeline_name : str , threshold : float ) -> List [ThresholdSensitivityResult ]:
40144 pass
0 commit comments