-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpret_example.py
More file actions
266 lines (186 loc) · 6.89 KB
/
interpret_example.py
File metadata and controls
266 lines (186 loc) · 6.89 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""
Minimal interpreter for linear and GLM model outputs.
This script demonstrates the core idea of the project:
Take a model summary, detect key statistical elements,
and retrieve the relevant explanations from the knowledge base.
This version does not use LLMs.
Everything is grounded in explicit statistical knowledge.
"""
from pathlib import Path
import re
from typing import List, Dict
from retrieval.logging_config import setup_logging
logger = setup_logging("interpret.linear")
from retrieval.search import semantic_search
# =========================
# Paths
# =========================
PROJECT_ROOT = Path(__file__).resolve().parent
EXAMPLES_PATH = PROJECT_ROOT / "examples"
"""
Remove markdown formatting so explanations can be printed cleanly.
This keeps output readable and avoids leaking formatting artifacts
from the KB files.
"""
def clean_markdown(text):
cleaned_lines = []
for line in text.splitlines():
stripped = line.strip()
# remove linhas vazias
if not stripped:
continue
# remove títulos markdown mesmo com "-"
if stripped.startswith("#"):
continue
if stripped.startswith("- #"):
continue
if stripped.startswith("##"):
continue
if stripped.startswith("###"):
continue
# remove "-" inicial mas mantém conteúdo
if stripped.startswith("- "):
stripped = stripped[2:]
cleaned_lines.append(stripped)
return "\n".join(cleaned_lines)
"""
Inspect model output and detect statistical concepts present.
This is a heuristic parser, not a full statistical parser.
The goal is simply to identify which explanations should be retrieved.
"""
def extract_statistical_signals(text: str) -> Dict[str, List[str]]:
#Extracts interpretable statistical signals from model output.
signals = {
"coefficients": [],
"model_metrics": []
}
# Detect coefficients (very simple heuristic)
coef_pattern = re.compile(r"(-?\d+\.\d+)")
negative_found = False
for line in text.splitlines():
if coef_pattern.search(line):
if "-" in line:
negative_found = True
if negative_found:
signals["coefficients"].append(
"interpretation of coefficients in linear regression model"
)
# Detect AIC / BIC
if "AIC" in text:
signals["model_metrics"].append("AIC interpretation in linear regression model")
if "BIC" in text:
signals["model_metrics"].append("BIC interpretation in linear regression model")
# Detect p-values
if "Pr(>|t|)" in text or "Pr(>|z|)" in text or "p-value" in text:
signals["inference"] = ["interpretation of p-values in linear regression model"]
# Detect standard errors
if "Std. Error" in text or "Std Error" in text:
signals.setdefault("inference", []).append(
"interpretation of standard errors in regression"
)
# Detect log-lik
if "log-lik" in text or "loglik)" in text or "logLik" in text:
signals["inference"] = ["interpretation of log likelihood in linear regression model"]
return signals
"""
Remove duplicate retrieval results and limit their size.
Retrieval systems sometimes return overlapping chunks.
This keeps output concise.
"""
def unique_and_trim(texts, max_chars=400):
seen = set()
cleaned = []
for t in texts:
t = t.strip()
if t not in seen:
seen.add(t)
cleaned.append(t[:max_chars])
return cleaned
"""
Main interpretation pipeline.
This function:
- detects statistical signals
- retrieves relevant explanations
- prints a structured report
It does not modify the model output itself.
It explains it.
"""
def interpret_model_output(text: str) -> None:
if not isinstance(text, str) or not text.strip():
raise ValueError("Invalid model output")
print("=== INTERPRETATION REPORT ===\n")
logger.info("Interpreting model output (%d chars)", len(text))
signals = extract_statistical_signals(text)
logger.info("Detected signals: %s", list(signals.keys()))
# --- Coefficients ---
if signals["coefficients"]:
print("## Coefficient Interpretation\n")
for signal in signals["coefficients"]:
logger.debug("Searching KB for signal: %s", signal)
try:
results = semantic_search(signal, k=2)
except Exception as e:
logger.error("Search failed for signal '%s': %s", signal, e)
continue
results = unique_and_trim(results)
results = results[:1]
for r in results:
formatted = clean_markdown(r)
paragraphs = formatted.split("\n")
for p in paragraphs:
if p.strip():
print(" ", p)
print()
print()
if "inference" in signals:
print("## Statistical Inference\n")
for q in signals["inference"]:
logger.debug("Searching KB for signal: %s", signal)
try:
results = semantic_search(q, k=2)
except Exception as e:
logger.error("Search failed for signal '%s': %s", signal, e)
continue
results = unique_and_trim(results)
results = results[:1]
for r in results:
formatted = clean_markdown(r)
paragraphs = formatted.split("\n")
for p in paragraphs:
if p.strip():
print(" ", p)
print()
print()
# --- Model Metrics ---
if signals["model_metrics"]:
print("## Model Fit and Selection\n")
for signal in signals["model_metrics"]:
logger.debug("Searching KB for signal: %s", signal)
try:
results = semantic_search(signal, k=3)
except Exception as e:
logger.error("Search failed for signal '%s': %s", signal, e)
continue
results = unique_and_trim(results)
results = results[:1]
for r in results:
formatted = clean_markdown(r)
paragraphs = formatted.split("\n")
for p in paragraphs:
if p.strip():
print(" ", p)
print()
print()
print("=== END OF REPORT ===")
# =========================
# CLI entry point
# Allows running the interpreter directly on example files.
# =========================
if __name__ == "__main__":
example_file = EXAMPLES_PATH / "glm_output_R.txt"
if not example_file.exists():
raise FileNotFoundError(
f"Example file not found: {example_file}"
)
model_output = example_file.read_text(encoding="utf-8")
interpret_model_output(model_output)