-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpost_simulation.py
More file actions
423 lines (377 loc) · 18.8 KB
/
post_simulation.py
File metadata and controls
423 lines (377 loc) · 18.8 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env python3
"""
Post-Simulation Analysis Module
This module provides generic post-simulation analysis methods that work across
all frameworks (PyMDP, RxInfer.jl, ActiveInference.jl, JAX, DisCoPy, PyTorch, NumPyro).
Implementation is split across sub-modules for maintainability:
- trace_analysis: Framework-agnostic trace, free energy, policy, state analysis
- framework_extractors: Framework-specific data extraction (extract_*_data)
- math_utils: Active Inference statistical functions (entropy, KL, VFE, EFE)
- visualizations: All plotting, animation, and dashboard generation
This file is the public facade for the analysis sub-modules listed above.
"""
from typing import Any
__all__: list[Any] = [
# trace_analysis
"analyze_simulation_traces",
"analyze_free_energy",
"analyze_policy_convergence",
"analyze_state_distributions",
"compare_framework_results",
# framework_extractors
"extract_pymdp_data",
"extract_rxinfer_data",
"extract_activeinference_jl_data",
"extract_jax_data",
"extract_discopy_data",
# math_utils
"compute_shannon_entropy",
"compute_kl_divergence",
"compute_variational_free_energy",
"compute_expected_free_energy",
"compute_information_gain",
"analyze_active_inference_metrics",
# visualizations
"plot_belief_evolution",
"animate_belief_evolution",
"visualize_all_framework_outputs",
"generate_belief_heatmaps",
"generate_action_analysis",
"generate_free_energy_plots",
"generate_observation_analysis",
"generate_cross_framework_comparison",
"generate_unified_framework_dashboard",
# local
"analyze_execution_results",
]
import json
import logging
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, Optional
import numpy as np
logger = logging.getLogger(__name__)
# Re-export from trace_analysis
# Re-export from framework_extractors
from .framework_extractors import (
extract_activeinference_jl_data,
extract_discopy_data,
extract_jax_data,
extract_pymdp_data,
extract_rxinfer_data,
)
# Re-export from math_utils
from .math_utils import (
analyze_active_inference_metrics,
compute_expected_free_energy,
compute_information_gain,
compute_kl_divergence,
compute_shannon_entropy,
compute_variational_free_energy,
)
from .trace_analysis import (
analyze_free_energy,
analyze_policy_convergence,
analyze_simulation_traces,
analyze_state_distributions,
compare_framework_results,
)
# Re-export from visualizations
from .visualizations import (
animate_belief_evolution,
generate_action_analysis,
generate_belief_heatmaps,
generate_cross_framework_comparison,
generate_free_energy_plots,
generate_observation_analysis,
generate_unified_framework_dashboard,
plot_belief_evolution,
visualize_all_framework_outputs,
)
def analyze_execution_results(
execution_results_dir: Path,
model_name: Optional[str] = None,
allowed_frameworks: Optional[set[str]] = None,
) -> Dict[str, Any]:
"""
Analyze execution results from the current execution scope.
Args:
execution_results_dir: Directory containing execution results
model_name: Optional model name filter
allowed_frameworks: Optional normalized framework names to include
Returns:
Dictionary with comprehensive analysis results
"""
try:
analysis_results: dict[str, Any] = {
"timestamp": datetime.now().isoformat(),
"execution_results_dir": str(execution_results_dir),
"framework_results": {},
"cross_framework_comparison": {},
}
# Find all result JSON files
result_files = list(execution_results_dir.rglob("*_results.json"))
if not result_files:
logger.warning(
f"No execution result files found in {execution_results_dir}"
)
return analysis_results
# Group by framework
framework_data: dict[Any, Any] = {}
for result_file in result_files:
try:
with open(result_file, "r") as f:
result_data = json.load(f)
framework = result_data.get("framework", "unknown")
# Normalize framework name to lowercase for consistent grouping
# Simulation JSONs may use "PyMDP"/"JAX" while execution logs use "pymdp"/"jax"
framework = framework.lower().replace(".", "_").replace(" ", "_")
if allowed_frameworks and framework not in allowed_frameworks:
continue
file_model_name = result_data.get("model_name", "unknown")
# Filter by model name if specified — use normalized comparison
# because GNN file stems (e.g. "simple_mdp") differ from
# display names in JSONs (e.g. "Simple MDP Agent", "GNNModel")
if model_name and file_model_name != model_name:
file_slug = (
file_model_name.lower().replace(" ", "_").replace("-", "_")
)
param_slug = model_name.lower().replace(" ", "_").replace("-", "_")
if param_slug not in file_slug and file_slug not in param_slug:
continue
if framework not in framework_data:
framework_data[framework] = []
framework_data[framework].append(result_data)
except Exception as e:
logger.warning(f"Failed to load result file {result_file}: {e}")
# Analyze each framework's results
for framework, results in framework_data.items():
try:
framework_analysis: dict[str, Any] = {
"framework": framework,
"result_count": len(results),
"analyses": [],
}
for result in results:
# Extract framework-specific data
try:
if framework == "pymdp":
extracted = extract_pymdp_data(result)
elif framework == "rxinfer":
extracted = extract_rxinfer_data(result)
elif framework == "activeinference_jl":
extracted = extract_activeinference_jl_data(result)
elif framework == "jax":
extracted = extract_jax_data(result)
elif framework == "discopy":
extracted = extract_discopy_data(result)
else:
extracted = result.get("simulation_data", {}) or {}
# Non-PyMDP frameworks still have heterogeneous outputs.
# PyMDP is strict and handled by extract_pymdp_data().
if (
framework != "pymdp"
and isinstance(extracted, dict)
and not extracted.get("beliefs")
and not extracted.get("observations")
):
implementation_dir = result.get("implementation_directory")
if implementation_dir:
try:
impl_path = Path(implementation_dir)
sim_data_dir = impl_path / "simulation_data"
if sim_data_dir.exists():
results_files = list(
sim_data_dir.glob("*.json")
)
for results_file in results_files:
try:
with open(results_file, "r") as f:
file_data = json.load(f)
if isinstance(file_data, dict):
if (
"beliefs" in file_data
and not extracted.get(
"beliefs"
)
):
extracted["beliefs"] = (
file_data["beliefs"]
)
if (
"actions" in file_data
and not extracted.get(
"actions"
)
):
extracted["actions"] = (
file_data["actions"]
)
if (
"observations" in file_data
and not extracted.get(
"observations"
)
):
extracted[
"observations"
] = file_data[
"observations"
]
except Exception as e:
logger.debug(
f"Failed to parse JSON file: {e}"
)
except Exception as e:
logger.debug(
f"Error reading files for {framework}: {e}"
)
# Run generic analyses
model_name_for_analysis = result.get("model_name", "unknown")
if isinstance(extracted, dict):
if framework != "pymdp" and not extracted.get(
"free_energy"
):
efe = None
if result.get("efe_history"):
efe = result["efe_history"]
elif isinstance(result.get("metrics"), dict) and result[
"metrics"
].get("expected_free_energy"):
efe = result["metrics"]["expected_free_energy"]
elif isinstance(
result.get("simulation_trace"), dict
) and result["simulation_trace"].get("efe_history"):
efe = result["simulation_trace"]["efe_history"]
if efe:
extracted["free_energy"] = efe
logger.debug(
f"Recovery: extracted EFE for {framework} from result data ({len(efe)} entries)"
)
if framework != "pymdp" and not extracted.get("beliefs"):
beliefs = result.get("beliefs") or result.get(
"simulation_trace", {}
).get("beliefs")
if beliefs:
extracted["beliefs"] = beliefs
if framework != "pymdp" and not extracted.get("actions"):
actions = result.get("actions") or result.get(
"simulation_trace", {}
).get("actions")
if actions:
extracted["actions"] = actions
if framework != "pymdp" and not extracted.get(
"observations"
):
obs = result.get("observations") or result.get(
"simulation_trace", {}
).get("observations")
if obs:
extracted["observations"] = obs
if extracted.get("free_energy"):
fe_analysis = analyze_free_energy(
extracted["free_energy"],
framework,
model_name_for_analysis,
)
framework_analysis["analyses"].append(fe_analysis)
if extracted.get("traces"):
trace_analysis = analyze_simulation_traces(
extracted["traces"],
framework,
model_name_for_analysis,
)
framework_analysis["analyses"].append(trace_analysis)
if extracted.get("policy"):
policy_analysis = analyze_policy_convergence(
extracted["policy"],
framework,
model_name_for_analysis,
)
framework_analysis["analyses"].append(policy_analysis)
except Exception as e:
logger.warning(f"Error analyzing result for {framework}: {e}")
framework_analysis["analyses"].append({"error": str(e)})
# Validate serializability
def safe_json_default(obj: Any) -> Any:
if isinstance(obj, Path):
return str(obj)
if isinstance(obj, (np.integer, int)):
return int(obj)
if isinstance(obj, (np.floating, float)):
return float(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
if isinstance(obj, (set, frozenset)):
return list(obj)
if hasattr(obj, "__dict__"):
return str(obj)
return str(obj)
try:
json.dumps(framework_analysis, default=safe_json_default)
analysis_results["framework_results"][framework] = (
framework_analysis
)
except Exception as e:
logger.error(
f"Circular reference or serialization error in {framework} analysis: {e}"
)
analysis_results["framework_results"][framework] = {
"framework": framework,
"error": f"Serialization failed: {e}",
}
except Exception as e:
logger.error(f"Failed to analyze framework {framework}: {e}")
analysis_results["framework_results"][framework] = {"error": str(e)}
# Cross-framework comparison
if len(framework_data) > 1:
comparison_input: dict[Any, Any] = {}
for framework, results in framework_data.items():
if results:
comparison_input[framework] = results[0]
comparison = compare_framework_results(
comparison_input, model_name or "unknown"
)
analysis_results["cross_framework_comparison"] = comparison
# Trigger visualizations and animations for the model results
if model_name:
results_dir = execution_results_dir.parent / "analysis_results"
results_dir.mkdir(parents=True, exist_ok=True)
for framework, data in analysis_results["framework_results"].items():
for _, analysis in enumerate(data.get("analyses", [])):
if "beliefs" in analysis or (
isinstance(analysis, dict)
and "simulation_data" in analysis
and "beliefs" in analysis["simulation_data"]
):
beliefs = analysis.get("beliefs") or analysis[
"simulation_data"
].get("beliefs")
if beliefs:
plot_file = (
results_dir / f"{model_name}_{framework}_beliefs.png"
)
plot_belief_evolution(
beliefs,
plot_file,
title=f"Beliefs - {model_name} ({framework})",
)
anim_file = (
results_dir / f"{model_name}_{framework}_beliefs.gif"
)
try:
animate_belief_evolution(
beliefs,
anim_file,
title=f"Evolution - {model_name} ({framework})",
)
except Exception as e:
logger.warning(f"Animation failed for {framework}: {e}")
analysis["plots"] = analysis.get("plots", [])
analysis["plots"].extend([str(plot_file), str(anim_file)])
return analysis_results
except Exception as e:
logger.error(f"Error analyzing execution results: {e}")
import traceback
logger.debug(traceback.format_exc())
return {"timestamp": datetime.now().isoformat(), "error": str(e)}