-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplainability_engine.py
More file actions
209 lines (177 loc) · 8.49 KB
/
Copy pathexplainability_engine.py
File metadata and controls
209 lines (177 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3
"""
explainability_engine.py — Justify every assistant recommendation with
evidence source, confidence score, supporting papers, alternative
interpretations, limitations, and an explainability statement.
Outputs
-------
outputs/explainability/explainability_report.md
"""
from __future__ import annotations
import argparse
import json
import logging
import re
from collections import defaultdict
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
import numpy as np
import pandas as pd
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(name)s: %(message)s")
log = logging.getLogger("explainability")
def load_kb(path: str = "knowledge_base.json") -> Dict:
p = Path(path)
return json.loads(p.read_text()) if p.exists() else {}
def load_evidence(path: str = "outputs/evidence/evidence_matrix.csv") -> pd.DataFrame:
p = Path(path)
return pd.read_csv(p) if p.exists() else pd.DataFrame()
def load_gaps(path: str = "outputs/reports/gap_confidence_scores.csv") -> pd.DataFrame:
p = Path(path)
return pd.read_csv(p) if p.exists() else pd.DataFrame()
def load_novelty(path: str = "outputs/reports/novelty_scores.csv") -> pd.DataFrame:
p = Path(path)
return pd.read_csv(p) if p.exists() else pd.DataFrame()
def explain_theme_detection(kb: Dict) -> List[Dict]:
"""Explain why each theme was identified with full evidence trace."""
explanations = []
for t in kb.get("themes", []):
theme = t.get("theme", "")
pc = t.get("paper_count", 0)
conf = t.get("confidence", 0)
papers = t.get("papers", [])
methods = 3 # from consensus metadata; heuristic
explanations.append({
"component": "theme_detection",
"theme": theme,
"evidence_source": "Multi-method consensus (NMF + hierarchical + fixed k-means)",
"confidence_score": conf,
"supporting_papers": papers[:5],
"alternative_interpretations": [
f"Different clustering parameters may yield different groupings",
f"Small corpus ({pc} papers) limits stability of theme structure",
],
"limitations": [
f"Only {pc} papers assigned to this theme",
f"Theme derived from abstract text only, not full text",
],
"explainability_statement": (
f"Theme '{theme}' was identified by all {methods} clustering methods "
f"with confidence {conf:.0%} across {pc} papers. "
f"The keywords {', '.join(t.get('keywords', [])[:3])} "
f"represent the core conceptual space."
),
})
return explanations
def explain_gap_detection(kb: Dict, gap_df: pd.DataFrame) -> List[Dict]:
explanations = []
for _, row in gap_df.iterrows():
theme = row.get("theme", "")
score = row.get("gap_confidence_score", 0.5)
verdict = row.get("verdict", "Uncertain")
explanations.append({
"component": "gap_detection",
"theme": theme,
"gap_statement": row.get("gap_statement", ""),
"evidence_source": "Evidence strength + citation poverty + method consensus + literature coverage",
"confidence_score": score,
"supporting_papers": str(row.get("supporting_papers", "")).split("; ")[:3],
"alternative_interpretations": [
"Expanding the corpus may reveal additional evidence that fills this gap",
"Gap may be an artefact of narrow search keywords",
],
"limitations": [
f"Gap validation is based on corpus of {kb.get('corpus_size', '?')} papers",
"Semantic similarity threshold may miss relevant papers",
],
"explainability_statement": (
f"Gap '{theme}' scored {score:.2f}/1.0 ({verdict}). "
f"Low paper count and weak evidence suggest this is "
f"{'a genuine' if verdict == 'Confirmed Gap' else 'an uncertain'} gap."
),
})
return explanations
def explain_novelty(novelty_df: pd.DataFrame) -> List[Dict]:
explanations = []
for _, row in novelty_df.iterrows():
theme = row.get("theme", "")
score = row.get("novelty_score", 0.5)
level = row.get("classification", "Incremental")
explanations.append({
"component": "novelty_scoring",
"theme": theme,
"evidence_source": "Topic saturation + publication density + citation density + emerging concepts + recency",
"confidence_score": score,
"supporting_papers": [],
"alternative_interpretations": [
"Different embedding models may yield different saturation estimates",
"Novelty is relative to the current corpus; adding papers may change classification",
],
"limitations": [
"Novelty scoring uses MiniLM embeddings which may miss domain-specific nuances",
"Emerging concept detection is keyword-based, not semantic",
],
"explainability_statement": (
f"Novelty for '{theme}' is {score:.2f} ({level}). "
f"{'High emerging concept score' if row.get('emerging_score', 0) > 0.5 else 'Moderate topic saturation'} "
f"drives this classification."
),
})
return explanations
def _generate_report(theme_explanations: List[Dict],
gap_explanations: List[Dict],
novelty_explanations: List[Dict]) -> str:
lines: List[str] = []
lines.append("# Explainability Report\n")
lines.append(f"- **Generated:** {datetime.now().strftime('%Y-%m-%d %H:%M')}\n")
lines.append("Every recommendation below includes: Evidence Source, Confidence Score, "
"Supporting Papers, Alternative Interpretations, Limitations, and Explainability Statement.\n")
for category, explanations in [
("Theme Detection", theme_explanations),
("Gap Detection", gap_explanations),
("Novelty Scoring", novelty_explanations),
]:
lines.append(f"## {category}\n")
for exp in explanations[:5]:
lines.append(f"### {exp.get('theme', 'N/A')}\n")
lines.append(f"- **Evidence Source:** {exp.get('evidence_source', '')}")
lines.append(f"- **Confidence Score:** {exp.get('confidence_score', '')}")
if exp.get("supporting_papers"):
sp = exp["supporting_papers"]
if isinstance(sp, list) and sp:
lines.append(f"- **Supporting Papers:** {sp[0][:80]}")
lines.append("- **Alternative Interpretations:**")
for alt in exp.get("alternative_interpretations", []):
lines.append(f" - {alt}")
lines.append("- **Limitations:**")
for lim in exp.get("limitations", []):
lines.append(f" - {lim}")
lines.append(f"- **Explainability Statement:** {exp.get('explainability_statement', '')}\n")
lines.append("---\n*Generated by explainability_engine.py*")
return "\n".join(lines)
def main() -> None:
parser = argparse.ArgumentParser(description="Explainability engine.")
parser.add_argument("--knowledge-base", type=str, default="knowledge_base.json")
parser.add_argument("--evidence", type=str, default="outputs/evidence/evidence_matrix.csv")
parser.add_argument("--gaps", type=str, default="outputs/reports/gap_confidence_scores.csv")
parser.add_argument("--novelty", type=str, default="outputs/reports/novelty_scores.csv")
parser.add_argument("--output-dir", type=str, default="outputs/explainability")
args = parser.parse_args()
kb = load_kb(args.knowledge_base)
gap_df = load_gaps(args.gaps)
novelty_df = load_novelty(args.novelty)
theme_exps = explain_theme_detection(kb)
gap_exps = explain_gap_detection(kb, gap_df)
novelty_exps = explain_novelty(novelty_df)
report = _generate_report(theme_exps, gap_exps, novelty_exps)
out_dir = Path(args.output_dir)
out_dir.mkdir(parents=True, exist_ok=True)
with open(out_dir / "explainability_report.md", "w") as f:
f.write(report)
log.info("Saved -> %s", out_dir / "explainability_report.md")
print(f"\n--- Explainability Engine Complete ---")
print(f" Theme explanations: {len(theme_exps)}")
print(f" Gap explanations: {len(gap_exps)}")
print()
if __name__ == "__main__":
main()