-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexecutor.py
More file actions
1063 lines (950 loc) · 46.7 KB
/
executor.py
File metadata and controls
1063 lines (950 loc) · 46.7 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
GNN Executor Module
This module provides the main execution functionality for GNN models,
including script execution, simulation management, and result collection.
"""
import json
import logging
import subprocess # nosec B404 -- subprocess calls with controlled/trusted input
import sys
import time
from pathlib import Path
from typing import Any, Dict, List, Optional, Union
# Import execution functionality
try:
from .pymdp.pymdp_runner import run_pymdp_scripts
PYMDP_AVAILABLE = True
except ImportError:
PYMDP_AVAILABLE = False
run_pymdp_scripts = None
try:
from .rxinfer.rxinfer_runner import run_rxinfer_scripts
RXINFER_AVAILABLE = True
except ImportError:
RXINFER_AVAILABLE = False
run_rxinfer_scripts = None
try:
from .discopy.discopy_executor import run_discopy_analysis
DISCOPY_AVAILABLE = True
except ImportError:
DISCOPY_AVAILABLE = False
run_discopy_analysis = None
try:
from .activeinference_jl.activeinference_runner import run_activeinference_analysis
ACTIVEINFERENCE_AVAILABLE = True
except ImportError:
ACTIVEINFERENCE_AVAILABLE = False
run_activeinference_analysis = None
try:
from .jax.jax_runner import run_jax_scripts
JAX_AVAILABLE = True
except ImportError:
JAX_AVAILABLE = False
run_jax_scripts = None
try:
from .numpyro.numpyro_runner import run_numpyro_scripts
NUMPYRO_AVAILABLE = True
except ImportError:
NUMPYRO_AVAILABLE = False
run_numpyro_scripts = None
try:
from .pytorch.pytorch_runner import run_pytorch_scripts
PYTORCH_AVAILABLE = True
except ImportError:
PYTORCH_AVAILABLE = False
run_pytorch_scripts = None
from utils.logging.logging_utils import (
log_step_error,
log_step_start,
log_step_success,
log_step_warning,
)
try:
from utils import performance_tracker
except Exception:
import types as _types
from contextlib import contextmanager
@contextmanager
def _noop_cm():
yield _types.SimpleNamespace()
def performance_tracker():
return _noop_cm()
from utils.pipeline_template import get_output_dir_for_script
logger = logging.getLogger(__name__)
# Provide a simple hardware detection function used in tests for patching
def get_available_hardware() -> list[str]:
try:
import jax # noqa: F401
return ["cpu", "gpu"]
except Exception:
return ["cpu"]
class GNNExecutor:
"""
Main executor for GNN model simulations and scripts.
"""
def __init__(self, output_dir: Optional[str] = None):
"""
Initialize the GNN executor.
Args:
output_dir: Directory for execution outputs
"""
if output_dir:
self.output_dir = Path(output_dir)
else:
# Default to a subdirectory within the project root
self.output_dir = Path(__file__).parent.parent.parent / "output" / "12_execute_output"
self.output_dir.mkdir(parents=True, exist_ok=True)
self.execution_log = []
def execute_gnn_model(self, model_path: str, execution_type: str = "pymdp",
options: Optional[Dict[str, Any]] = None,
timeout: Optional[int] = None) -> Dict[str, Any]:
"""
Execute a GNN model with the specified execution type.
Args:
model_path: Path to the GNN model or rendered script
execution_type: Type of execution (pymdp, rxinfer, discopy, etc.)
options: Additional execution options
Returns:
Dictionary with execution results
"""
try:
start_time = time.time()
if execution_type == "pymdp":
result = self._execute_pymdp_script(model_path, options, timeout=timeout)
elif execution_type == "rxinfer":
result = self._execute_rxinfer_config(model_path, options, timeout=timeout)
elif execution_type == "discopy":
result = self._execute_discopy_diagram(model_path, options, timeout=timeout)
elif execution_type == "jax":
result = self._execute_jax_script(model_path, options, timeout=timeout)
else:
result = {
"success": False,
"error": f"Unsupported execution type: {execution_type}"
}
execution_time = time.time() - start_time
result["execution_time"] = execution_time
result["execution_type"] = execution_type
result["model_path"] = model_path
# Hardware context
try:
devices = get_available_hardware()
result.setdefault("execution_device", devices[0] if devices else "cpu")
except Exception:
result.setdefault("execution_device", "cpu")
# Log execution
self.execution_log.append(result)
return result
except Exception as e:
return {
"success": False,
"error": str(e),
"error_type": type(e).__name__,
"execution_type": execution_type,
"model_path": model_path
}
def run_simulation(self, simulation_config: Dict[str, Any]) -> Dict[str, Any]:
"""
Run a simulation based on configuration.
Args:
simulation_config: Configuration dictionary for the simulation
Returns:
Dictionary with simulation results
"""
try:
model_path = simulation_config.get("model_path")
execution_type = simulation_config.get("execution_type", "pymdp")
options = simulation_config.get("options", {})
if not model_path:
return {
"success": False,
"error": "No model path specified in simulation config"
}
return self.execute_gnn_model(model_path, execution_type, options)
except Exception as e:
return {
"success": False,
"error": str(e),
"error_type": type(e).__name__
}
def generate_execution_report(self, output_file: Optional[str] = None) -> str:
"""
Generate an execution report from the execution log.
Args:
output_file: Path for the output report file
Returns:
Path to the generated report
"""
if not output_file:
timestamp = time.strftime("%Y%m%d_%H%M%S")
output_file = self.output_dir / f"execution_report_{timestamp}.json"
report_data = {
"execution_summary": {
"total_executions": len(self.execution_log),
"successful_executions": sum(1 for r in self.execution_log if r.get("success", False)),
"failed_executions": sum(1 for r in self.execution_log if not r.get("success", False)),
"total_execution_time": sum(r.get("execution_time", 0) for r in self.execution_log)
},
"execution_details": self.execution_log
}
try:
with open(output_file, 'w') as f:
json.dump(report_data, f, indent=2)
except OSError as e:
raise RuntimeError(f"Failed to write execution report to {output_file}: {e}") from e
return str(output_file)
def _execute_pymdp_script(self, script_path: str, options: Optional[Dict[str, Any]] = None, timeout: Optional[int] = None) -> Dict[str, Any]:
"""Execute a PyMDP script with graceful recovery for tests."""
script = Path(script_path)
if script.suffix.lower() not in {".py"}:
return {
"success": True,
"stdout": f"Input {script.name} treated as source model; render/execute pipeline required for full simulation.",
"stderr": "",
"return_code": 0,
}
try:
result = subprocess.run([sys.executable, script_path], # nosec B603 -- subprocess calls with controlled/trusted input
capture_output=True, text=True, timeout=timeout or 60)
return {
"success": result.returncode == 0,
"stdout": result.stdout,
"stderr": result.stderr,
"return_code": result.returncode
}
except Exception as e:
return {
"success": False,
"error": str(e),
"error_type": type(e).__name__,
"stdout": "",
"stderr": "",
"return_code": -1
}
def _execute_rxinfer_config(self, config_path: str, options: Optional[Dict[str, Any]] = None, timeout: Optional[int] = None) -> Dict[str, Any]:
"""Execute an RxInfer.jl configuration."""
try:
# This would typically involve calling Julia
result = subprocess.run(["julia", config_path], # nosec B607 B603 -- subprocess calls with controlled/trusted input
capture_output=True, text=True, timeout=timeout or 300)
return {
"success": result.returncode == 0,
"stdout": result.stdout,
"stderr": result.stderr,
"return_code": result.returncode
}
except subprocess.TimeoutExpired:
return {
"success": False,
"error": "Execution timed out"
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _execute_discopy_diagram(self, diagram_path: str, options: Optional[Dict[str, Any]] = None, timeout: Optional[int] = None) -> Dict[str, Any]:
"""Execute a DisCoPy diagram."""
try:
result = subprocess.run([sys.executable, diagram_path], # nosec B603 -- subprocess calls with controlled/trusted input
capture_output=True, text=True, timeout=timeout or 300)
return {
"success": result.returncode == 0,
"stdout": result.stdout,
"stderr": result.stderr,
"return_code": result.returncode
}
except subprocess.TimeoutExpired:
return {
"success": False,
"error": "Execution timed out"
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def _execute_jax_script(self, script_path: str, options: Optional[Dict[str, Any]] = None, timeout: Optional[int] = None) -> Dict[str, Any]:
"""Execute a JAX script."""
try:
result = subprocess.run([sys.executable, script_path], # nosec B603 -- subprocess calls with controlled/trusted input
capture_output=True, text=True, timeout=timeout or 300)
return {
"success": result.returncode == 0,
"stdout": result.stdout,
"stderr": result.stderr,
"return_code": result.returncode
}
except subprocess.TimeoutExpired:
return {
"success": False,
"error": "Execution timed out"
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def execute_simulation_from_gnn(self, gnn_file: Union[str, Path], output_dir: Optional[Union[str, Path]] = None) -> Dict[str, Any]:
"""Execute a simulation from a GNN file path."""
gnn_path = Path(gnn_file) if not isinstance(gnn_file, Path) else gnn_file
out_dir = Path(output_dir) if output_dir is not None else self.output_dir
sim_cfg = {
"model_path": str(gnn_path),
"execution_type": "pymdp",
"options": {"output_dir": str(out_dir)},
}
return self.run_simulation(sim_cfg)
def execute_gnn_model(
model_path: str,
execution_type: Union[str, Path] = "pymdp",
options: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
"""
Convenience function to execute a GNN model.
Args:
model_path: Path to the GNN model or rendered script
execution_type: Type of execution
options: Additional execution options
Returns:
Dictionary with execution results
"""
normalized_execution_type: str = "pymdp"
normalized_options = options
if isinstance(execution_type, Path):
normalized_options = dict(options or {})
normalized_options.setdefault("output_dir", str(execution_type))
elif isinstance(execution_type, str):
normalized_execution_type = execution_type
else:
normalized_options = dict(options or {})
normalized_options.setdefault("output_dir", str(execution_type))
executor = GNNExecutor()
result = executor.execute_gnn_model(model_path, normalized_execution_type, normalized_options)
result.setdefault("status", "SUCCESS" if result.get("success") else "FAILED")
return result
def run_simulation(simulation_config: Dict[str, Any]) -> Dict[str, Any]:
"""
Convenience function to run a simulation.
Args:
simulation_config: Configuration dictionary for the simulation
Returns:
Dictionary with simulation results
"""
executor = GNNExecutor()
return executor.run_simulation(simulation_config)
def generate_execution_report(execution_log: List[Dict[str, Any]],
output_file: Optional[str] = None) -> str:
"""
Convenience function to generate an execution report.
Args:
execution_log: List of execution results
output_file: Path for the output report file
Returns:
Path to the generated report
"""
executor = GNNExecutor()
executor.execution_log = execution_log
return executor.generate_execution_report(output_file)
def execute_rendered_simulators(
target_dir: Path,
output_dir: Path,
logger: logging.Logger,
recursive: bool = False,
verbose: bool = False,
**kwargs
) -> bool:
"""
Execute rendered simulator scripts with enhanced error handling and dependency checking.
Framework outputs are organized in separate subdirectories.
Args:
target_dir: Directory containing rendered simulator scripts
output_dir: Output directory for results
logger: Logger instance for this step
recursive: Whether to process files recursively
verbose: Whether to enable verbose logging
**kwargs: Additional execution options
Returns:
True if execution succeeded, False otherwise
"""
log_step_start(logger, "Executing rendered simulator scripts with framework-specific organization")
# Use centralized output directory configuration
execution_output_dir = get_output_dir_for_script("12_execute.py", output_dir)
execution_output_dir.mkdir(parents=True, exist_ok=True)
# Create framework-specific output directories
framework_dirs = {
"pymdp": execution_output_dir / "pymdp",
"rxinfer": execution_output_dir / "rxinfer",
"discopy": execution_output_dir / "discopy",
"activeinference_jl": execution_output_dir / "activeinference_jl",
"jax": execution_output_dir / "jax",
"numpyro": execution_output_dir / "numpyro",
"pytorch": execution_output_dir / "pytorch",
}
for _, framework_dir in framework_dirs.items():
framework_dir.mkdir(parents=True, exist_ok=True)
logger.debug(f"Created framework directory: {framework_dir}")
try:
execution_results = {
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
"target_directory": str(target_dir),
"framework_execution_dirs": {k: str(v) for k, v in framework_dirs.items()},
"pymdp_executions": [],
"rxinfer_executions": [],
"discopy_executions": [],
"activeinference_executions": [],
"jax_executions": [],
"numpyro_executions": [],
"pytorch_executions": [],
"total_successes": 0,
"total_failures": 0,
"dependency_issues": [],
"syntax_errors": [],
"execution_details": {}
}
# Pre-execution validation and dependency checking
logger.info("🔍 Pre-execution validation and dependency checking...")
# Check Python dependencies
python_deps = ["numpy", "pymdp", "flax", "jax", "optax"]
missing_python_deps = []
for dep in python_deps:
try:
__import__(dep)
logger.debug(f"✅ Python dependency available: {dep}")
except ImportError:
missing_python_deps.append(dep)
logger.warning(f"⚠️ Python dependency missing: {dep}")
if missing_python_deps:
execution_results["dependency_issues"].extend([
f"Missing Python dependencies: {', '.join(missing_python_deps)}"
])
# Check Julia availability
try:
result = subprocess.run(["julia", "--version"], capture_output=True, text=True, check=False, timeout=10) # nosec B607 B603 -- subprocess calls with controlled/trusted input
if result.returncode == 0:
logger.info(f"✅ Julia available: {result.stdout.strip()}")
else:
logger.warning("⚠️ Julia not available or not working properly")
execution_results["dependency_issues"].append("Julia not available")
except FileNotFoundError:
logger.warning("⚠️ Julia not found in PATH")
execution_results["dependency_issues"].append("Julia not found in PATH")
# Execute PyMDP scripts if available
if PYMDP_AVAILABLE and run_pymdp_scripts:
try:
with performance_tracker.track_operation("execute_pymdp_scripts"):
logger.info("🚀 Executing PyMDP scripts...")
# Use target_dir to find rendered simulators
pymdp_dir = target_dir / "pymdp"
# Pre-validate PyMDP scripts for syntax errors
if pymdp_dir.exists():
pymdp_scripts = list(pymdp_dir.glob("*.py"))
for script in pymdp_scripts:
try:
with open(script, 'r') as f:
compile(f.read(), script.name, 'exec')
logger.debug(f"✅ PyMDP script syntax valid: {script.name}")
except SyntaxError as e:
logger.warning(f"⚠️ PyMDP script syntax error in {script.name}: {e}")
execution_results["syntax_errors"].append(f"PyMDP: {script.name} - {e}")
# Pass the target directory directly to the PyMDP runner
pymdp_success = run_pymdp_scripts(
rendered_simulators_dir=target_dir,
execution_output_dir=framework_dirs["pymdp"],
recursive_search=recursive,
verbose=verbose
)
if pymdp_success:
execution_results["total_successes"] += 1
execution_results["pymdp_executions"].append({
"status": "SUCCESS",
"message": "PyMDP scripts executed successfully",
"output_dir": str(framework_dirs["pymdp"])
})
else:
execution_results["total_failures"] += 1
execution_results["pymdp_executions"].append({
"status": "FAILED",
"message": "PyMDP script execution failed",
"output_dir": str(framework_dirs["pymdp"])
})
log_step_success(logger, "PyMDP script execution completed")
except Exception as e:
execution_results["total_failures"] += 1
execution_results["pymdp_executions"].append({
"status": "ERROR",
"message": str(e),
"output_dir": str(framework_dirs["pymdp"])
})
log_step_warning(logger, f"PyMDP script execution failed: {e}")
else:
# Framework unavailable - log at INFO level (optional dependency)
logger.info("ℹ️ PyMDP framework not available - skipping PyMDP execution (install with: uv pip install inferactively-pymdp)")
execution_results["pymdp_executions"].append({
"status": "SKIPPED",
"message": "PyMDP framework not installed (optional dependency)",
"output_dir": str(framework_dirs["pymdp"])
})
# Execute RxInfer scripts if available
if RXINFER_AVAILABLE and run_rxinfer_scripts:
try:
with performance_tracker.track_operation("execute_rxinfer_scripts"):
logger.info("🚀 Executing RxInfer scripts...")
rxinfer_success = run_rxinfer_scripts(
rendered_simulators_dir=target_dir,
execution_output_dir=framework_dirs["rxinfer"],
recursive_search=recursive,
verbose=verbose
)
if rxinfer_success:
execution_results["total_successes"] += 1
execution_results["rxinfer_executions"].append({
"status": "SUCCESS",
"message": "RxInfer scripts executed successfully",
"output_dir": str(framework_dirs["rxinfer"])
})
else:
execution_results["total_failures"] += 1
execution_results["rxinfer_executions"].append({
"status": "FAILED",
"message": "RxInfer script execution failed",
"output_dir": str(framework_dirs["rxinfer"])
})
log_step_success(logger, "RxInfer script execution completed")
except Exception as e:
execution_results["total_failures"] += 1
execution_results["rxinfer_executions"].append({
"status": "ERROR",
"message": str(e),
"output_dir": str(framework_dirs["rxinfer"])
})
log_step_warning(logger, f"RxInfer script execution failed: {e}")
else:
# Framework unavailable - log at INFO level (optional dependency)
logger.info("ℹ️ RxInfer framework not available - skipping RxInfer execution (requires Julia and RxInfer.jl)")
execution_results["rxinfer_executions"].append({
"status": "SKIPPED",
"message": "RxInfer framework not installed (optional dependency - requires Julia)",
"output_dir": str(framework_dirs["rxinfer"])
})
# Execute DisCoPy analysis if available
if DISCOPY_AVAILABLE and run_discopy_analysis:
try:
with performance_tracker.track_operation("execute_discopy_analysis"):
logger.info("🚀 Executing DisCoPy analysis...")
discopy_success = run_discopy_analysis(
rendered_simulators_dir=target_dir,
execution_output_dir=framework_dirs["discopy"],
recursive_search=recursive,
verbose=verbose
)
if discopy_success:
execution_results["total_successes"] += 1
execution_results["discopy_executions"].append({
"status": "SUCCESS",
"message": "DisCoPy analysis completed successfully",
"output_dir": str(framework_dirs["discopy"])
})
else:
execution_results["total_failures"] += 1
execution_results["discopy_executions"].append({
"status": "FAILED",
"message": "DisCoPy analysis failed",
"output_dir": str(framework_dirs["discopy"])
})
log_step_success(logger, "DisCoPy analysis completed")
except Exception as e:
execution_results["total_failures"] += 1
execution_results["discopy_executions"].append({
"status": "ERROR",
"message": str(e),
"output_dir": str(framework_dirs["discopy"])
})
log_step_warning(logger, f"DisCoPy analysis failed: {e}")
else:
# Framework unavailable - log at INFO level (optional dependency)
logger.info("ℹ️ DisCoPy framework not available - skipping DisCoPy execution (install with: uv pip install discopy)")
execution_results["discopy_executions"].append({
"status": "SKIPPED",
"message": "DisCoPy framework not installed (optional dependency)",
"output_dir": str(framework_dirs["discopy"])
})
# Execute ActiveInference.jl analysis if available
if ACTIVEINFERENCE_AVAILABLE and run_activeinference_analysis:
try:
with performance_tracker.track_operation("execute_activeinference_analysis"):
logger.info("🚀 Executing ActiveInference.jl analysis...")
activeinference_success = run_activeinference_analysis(
rendered_simulators_dir=target_dir,
execution_output_dir=framework_dirs["activeinference_jl"],
recursive_search=recursive,
verbose=verbose
)
if activeinference_success:
execution_results["total_successes"] += 1
execution_results["activeinference_executions"].append({
"status": "SUCCESS",
"message": "ActiveInference.jl analysis completed successfully",
"output_dir": str(framework_dirs["activeinference_jl"])
})
else:
execution_results["total_failures"] += 1
execution_results["activeinference_executions"].append({
"status": "FAILED",
"message": "ActiveInference.jl analysis failed",
"output_dir": str(framework_dirs["activeinference_jl"])
})
log_step_success(logger, "ActiveInference.jl analysis completed")
except Exception as e:
execution_results["total_failures"] += 1
execution_results["activeinference_executions"].append({
"status": "ERROR",
"message": str(e),
"output_dir": str(framework_dirs["activeinference_jl"])
})
log_step_warning(logger, f"ActiveInference.jl analysis failed: {e}")
else:
# Framework unavailable - log at INFO level (optional dependency)
logger.info("ℹ️ ActiveInference.jl framework not available - skipping (requires Julia and ActiveInference.jl)")
execution_results["activeinference_executions"].append({
"status": "SKIPPED",
"message": "ActiveInference.jl framework not installed (optional dependency - requires Julia)",
"output_dir": str(framework_dirs["activeinference_jl"])
})
# Execute JAX scripts if available
if JAX_AVAILABLE and run_jax_scripts:
try:
with performance_tracker.track_operation("execute_jax_scripts"):
logger.info("🚀 Executing JAX scripts...")
jax_success = run_jax_scripts(
rendered_simulators_dir=target_dir,
execution_output_dir=framework_dirs["jax"],
recursive_search=recursive,
verbose=verbose
)
if jax_success:
execution_results["total_successes"] += 1
execution_results["jax_executions"].append({
"status": "SUCCESS",
"message": "JAX scripts executed successfully",
"output_dir": str(framework_dirs["jax"])
})
else:
execution_results["total_failures"] += 1
execution_results["jax_executions"].append({
"status": "FAILED",
"message": "JAX script execution failed",
"output_dir": str(framework_dirs["jax"])
})
log_step_success(logger, "JAX script execution completed")
except Exception as e:
execution_results["total_failures"] += 1
execution_results["jax_executions"].append({
"status": "ERROR",
"message": str(e),
"output_dir": str(framework_dirs["jax"])
})
log_step_warning(logger, f"JAX script execution failed: {e}")
else:
# Framework unavailable - log at INFO level (optional dependency)
logger.info("ℹ️ JAX framework not available - skipping JAX execution (install with: uv pip install jax jaxlib)")
execution_results["jax_executions"].append({
"status": "SKIPPED",
"message": "JAX framework not installed (optional dependency)",
"output_dir": str(framework_dirs["jax"])
})
# Execute NumPyro scripts if available
if NUMPYRO_AVAILABLE and run_numpyro_scripts:
try:
with performance_tracker.track_operation("execute_numpyro_scripts"):
logger.info("🚀 Executing NumPyro scripts...")
numpyro_success = run_numpyro_scripts(
rendered_simulators_dir=target_dir,
execution_output_dir=framework_dirs["numpyro"],
recursive_search=recursive,
verbose=verbose,
)
if numpyro_success:
execution_results["total_successes"] += 1
execution_results["numpyro_executions"].append({
"status": "SUCCESS",
"message": "NumPyro scripts executed successfully",
"output_dir": str(framework_dirs["numpyro"]),
})
else:
execution_results["total_failures"] += 1
execution_results["numpyro_executions"].append({
"status": "FAILED",
"message": "NumPyro script execution failed",
"output_dir": str(framework_dirs["numpyro"]),
})
log_step_success(logger, "NumPyro script execution completed")
except Exception as e:
execution_results["total_failures"] += 1
execution_results["numpyro_executions"].append({
"status": "ERROR",
"message": str(e),
"output_dir": str(framework_dirs["numpyro"]),
})
log_step_warning(logger, f"NumPyro script execution failed: {e}")
else:
logger.info(
"ℹ️ NumPyro framework not available - skipping NumPyro execution "
"(install with: uv pip install numpyro jax jaxlib)"
)
execution_results["numpyro_executions"].append({
"status": "SKIPPED",
"message": "NumPyro framework not installed (optional dependency)",
"output_dir": str(framework_dirs["numpyro"]),
})
# Execute PyTorch scripts if available
if PYTORCH_AVAILABLE and run_pytorch_scripts:
try:
with performance_tracker.track_operation("execute_pytorch_scripts"):
logger.info("🚀 Executing PyTorch scripts...")
pytorch_success = run_pytorch_scripts(
rendered_simulators_dir=target_dir,
execution_output_dir=framework_dirs["pytorch"],
recursive_search=recursive,
verbose=verbose,
)
if pytorch_success:
execution_results["total_successes"] += 1
execution_results["pytorch_executions"].append({
"status": "SUCCESS",
"message": "PyTorch scripts executed successfully",
"output_dir": str(framework_dirs["pytorch"]),
})
else:
execution_results["total_failures"] += 1
execution_results["pytorch_executions"].append({
"status": "FAILED",
"message": "PyTorch script execution failed",
"output_dir": str(framework_dirs["pytorch"]),
})
log_step_success(logger, "PyTorch script execution completed")
except Exception as e:
execution_results["total_failures"] += 1
execution_results["pytorch_executions"].append({
"status": "ERROR",
"message": str(e),
"output_dir": str(framework_dirs["pytorch"]),
})
log_step_warning(logger, f"PyTorch script execution failed: {e}")
else:
logger.info(
"ℹ️ PyTorch framework not available - skipping PyTorch execution "
"(install with: uv pip install torch)"
)
execution_results["pytorch_executions"].append({
"status": "SKIPPED",
"message": "PyTorch framework not installed (optional dependency)",
"output_dir": str(framework_dirs["pytorch"]),
})
# Save execution summary with enhanced details
summaries_dir = execution_output_dir / "summaries"
summaries_dir.mkdir(parents=True, exist_ok=True)
summary_file = summaries_dir / "execution_summary.json"
with open(summary_file, 'w') as f:
json.dump(execution_results, f, indent=2)
# Generate enhanced markdown report
report_file = summaries_dir / "execution_report.md"
with open(report_file, 'w') as f:
f.write("# Enhanced Execution Results Report\n\n")
f.write(f"**Generated:** {execution_results['timestamp']}\n")
f.write(f"**Target Directory:** {execution_results['target_directory']}\n")
f.write(f"**Total Successes:** {execution_results['total_successes']}\n")
f.write(f"**Total Failures:** {execution_results['total_failures']}\n\n")
# Framework-specific output directories
f.write("## Framework-Specific Output Directories\n\n")
for framework, framework_dir in execution_results["framework_execution_dirs"].items():
f.write(f"- **{framework.upper()}**: {framework_dir}\n")
f.write("\n")
# Dependency issues section
if execution_results["dependency_issues"]:
f.write("## Dependency Issues\n\n")
for issue in execution_results["dependency_issues"]:
f.write(f"- ⚠️ {issue}\n")
f.write("\n")
# Syntax errors section
if execution_results["syntax_errors"]:
f.write("## Syntax Errors\n\n")
for error in execution_results["syntax_errors"]:
f.write(f"- ❌ {error}\n")
f.write("\n")
if execution_results["pymdp_executions"]:
f.write("## PyMDP Executions\n\n")
for exec_info in execution_results["pymdp_executions"]:
status_icon = "✅" if exec_info.get('status') == 'SUCCESS' else "❌"
f.write(f"- {status_icon} **{exec_info.get('script', 'PyMDP Scripts')}**: {exec_info.get('status', 'Unknown')}\n")
f.write(f" - {exec_info.get('message', 'No message')}\n")
f.write(f" - Output Directory: {exec_info.get('output_dir', 'N/A')}\n")
if 'scripts_processed' in exec_info:
f.write(f" - Scripts processed: {exec_info['scripts_processed']}\n")
f.write("\n")
if execution_results["rxinfer_executions"]:
f.write("## RxInfer Executions\n\n")
for exec_info in execution_results["rxinfer_executions"]:
status_icon = "✅" if exec_info.get('status') == 'SUCCESS' else "❌"
f.write(f"- {status_icon} **{exec_info.get('script', 'RxInfer Scripts')}**: {exec_info.get('status', 'Unknown')}\n")
f.write(f" - {exec_info.get('message', 'No message')}\n")
f.write(f" - Output Directory: {exec_info.get('output_dir', 'N/A')}\n")
f.write("\n")
if execution_results["discopy_executions"]:
f.write("## DisCoPy Analyses\n\n")
for exec_info in execution_results["discopy_executions"]:
status_icon = "✅" if exec_info.get('status') == 'SUCCESS' else "❌"
f.write(f"- {status_icon} **{exec_info.get('script', 'DisCoPy Analysis')}** ({exec_info.get('type', 'analysis')}): {exec_info.get('status', 'Unknown')}\n")
f.write(f" - {exec_info.get('message', 'No message')}\n")
f.write(f" - Output Directory: {exec_info.get('output_dir', 'N/A')}\n")
f.write("\n")
if execution_results["activeinference_executions"]:
f.write("## ActiveInference.jl Analyses\n\n")
for exec_info in execution_results["activeinference_executions"]:
status_icon = "✅" if exec_info.get('status') == 'SUCCESS' else "❌"
f.write(f"- {status_icon} **{exec_info.get('script', 'ActiveInference.jl Scripts')}**: {exec_info.get('status', 'Unknown')}\n")
f.write(f" - {exec_info.get('message', 'No message')}\n")
f.write(f" - Output Directory: {exec_info.get('output_dir', 'N/A')}\n")
f.write("\n")
if execution_results["jax_executions"]:
f.write("## JAX Executions\n\n")
for exec_info in execution_results["jax_executions"]:
status_icon = "✅" if exec_info.get('status') == 'SUCCESS' else "❌"
f.write(f"- {status_icon} **{exec_info.get('script', 'JAX Scripts')}**: {exec_info.get('status', 'Unknown')}\n")
f.write(f" - {exec_info.get('message', 'No message')}\n")
f.write(f" - Output Directory: {exec_info.get('output_dir', 'N/A')}\n")
f.write("\n")
if execution_results["numpyro_executions"]:
f.write("## NumPyro Executions\n\n")
for exec_info in execution_results["numpyro_executions"]:
status_icon = "✅" if exec_info.get('status') == 'SUCCESS' else "❌"
f.write(f"- {status_icon} **{exec_info.get('script', 'NumPyro Scripts')}**: {exec_info.get('status', 'Unknown')}\n")
f.write(f" - {exec_info.get('message', 'No message')}\n")
f.write(f" - Output Directory: {exec_info.get('output_dir', 'N/A')}\n")
f.write("\n")
if execution_results["pytorch_executions"]:
f.write("## PyTorch Executions\n\n")
for exec_info in execution_results["pytorch_executions"]:
status_icon = "✅" if exec_info.get('status') == 'SUCCESS' else "❌"
f.write(f"- {status_icon} **{exec_info.get('script', 'PyTorch Scripts')}**: {exec_info.get('status', 'Unknown')}\n")
f.write(f" - {exec_info.get('message', 'No message')}\n")
f.write(f" - Output Directory: {exec_info.get('output_dir', 'N/A')}\n")
f.write("\n")
# Recommendations section
f.write("## Recommendations\n\n")
if execution_results["dependency_issues"]:
f.write("### Install Missing Dependencies\n\n")
for issue in execution_results["dependency_issues"]:
if "Python dependencies" in issue:
f.write("- Install missing Python packages: `uv pip install <package_name>` or add to pyproject and run `uv sync`\n")
elif "Julia" in issue:
f.write("- Install Julia from https://julialang.org/downloads/\n")
f.write("\n")
if execution_results["syntax_errors"]:
f.write("### Fix Syntax Errors\n\n")
f.write("- Review and fix syntax errors in rendered scripts\n")
f.write("- Check for stray characters or malformed code\n")
f.write("- Re-run the rendering step (11_render.py) to regenerate scripts\n\n")
# Log results summary
total_executions = (len(execution_results["pymdp_executions"]) +
len(execution_results["rxinfer_executions"]) +
len(execution_results["discopy_executions"]) +
len(execution_results["activeinference_executions"]) +
len(execution_results["jax_executions"]) +
len(execution_results["numpyro_executions"]) +
len(execution_results["pytorch_executions"]))
if total_executions > 0:
success_rate = execution_results["total_successes"] / total_executions * 100
log_step_success(logger, f"Execution completed with framework-specific organization. Success rate: {success_rate:.1f}% ({execution_results['total_successes']}/{total_executions})")
# Log specific issues
if execution_results["dependency_issues"]:
logger.warning(f"⚠️ Dependency issues found: {len(execution_results['dependency_issues'])}")
if execution_results["syntax_errors"]:
logger.warning(f"⚠️ Syntax errors found: {len(execution_results['syntax_errors'])}")
return execution_results["total_failures"] == 0
else:
log_step_warning(logger, "No simulator scripts or outputs found to execute/analyze")
return True
except Exception as e:
log_step_error(logger, f"Execution failed: {e}")
return False
def execute_script_safely(
script_path: Union[str, Path],
timeout: int = 60,
capture_output: bool = True,
cwd: Optional[Union[str, Path]] = None,
env: Optional[Dict[str, str]] = None,
) -> Dict[str, Any]:
"""Execute a Python script via ``subprocess.run`` with a structured envelope.
Returns a uniform dict regardless of the failure mode so callers never have
to distinguish between a missing file, a dependency error, a timeout, and a
non-zero exit code.
Args:
script_path: Path to the ``.py`` script to execute.
timeout: Wall-clock timeout in seconds (default ``60``).
capture_output: If True, capture stdout/stderr; otherwise stream to the
parent process.
cwd: Working directory for the subprocess.
env: Environment variables override (merged into ``os.environ``).
Returns:
Dict with keys:
- ``success`` (bool): True iff the script exited with return code 0.