Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-12 08:24:55

0001 """
0002 Workflow execution commands for AID2E CLI.
0003 
0004 Commands for running optimization workflows and managing execution lifecycle:
0005 - optimize: Run optimization from configuration (current placeholder)
0006 - run: Execute full workflow (planned)
0007 - resume: Restart from checkpoint (planned)
0008 - stop: Halt running optimization (planned)
0009 - status: Check optimization progress (planned)
0010 - clean: Remove temporary files (planned)
0011 """
0012 
0013 import sys
0014 from typing import Optional
0015 
0016 import click
0017 
0018 from aid2e.utilities.configurations import load_config
0019 
0020 from aid2e.utilities.workflows.toy_evaluator import run_epic_b0_toy_optimization
0021 
0022 @click.command(name="optimize")
0023 @click.argument("config_file", type=click.Path(exists=True))
0024 @click.option("--validate-only", is_flag=True, help="Validate config but do not run")
0025 @click.option("-v", "--verbosity", count=True, help="Increase verbosity (can be used multiple times)")
0026 @click.option("--log", "log_file", type=click.Path(dir_okay=False), help="Path to log file")
0027 def optimize(config_file: str, validate_only: bool, verbosity: int, log_file: Optional[str]):
0028     """
0029     Run optimization based on configuration file.
0030     
0031     CONFIG_FILE: Path to the YAML configuration file.
0032     
0033     Example:
0034         aid2e optimize optimization.yml
0035         aid2e optimize optimization.yml --validate-only
0036         aid2e optimize optimization.yml -vv --log output.log
0037     """
0038     try:
0039         if verbosity > 0:
0040             click.echo(f"Loading configuration from: {config_file}")
0041         
0042         config = load_config(config_file)
0043         
0044         if validate_only:
0045             click.echo(click.style("✓ Configuration validated; skipping execution.", fg="green"))
0046             return
0047         
0048         # Display optimization info
0049         click.echo(click.style(f"Running optimization: {config.optimizer.name}", fg="cyan", bold=True))
0050         click.echo(f"  Algorithm: {config.optimizer.name} ({config.optimizer.type})")
0051         click.echo(f"  Iterations: {config.optimizer.parameters.get('n_iterations', 'N/A')}")
0052         click.echo(f"  Verbosity: {verbosity}")
0053         if log_file:
0054             click.echo(f"  Log file: {log_file}")
0055         click.echo()
0056         
0057         # TODO: Implement actual optimization execution
0058         # This will involve:
0059         # 1. Instantiate optimizer from config.optimizer
0060         # 2. Setup problem evaluator from config.problem
0061         # 3. Run optimization loop
0062         # 4. Save results to config.problem.output_location
0063         
0064         # click.echo(click.style("Note: Optimizer execution not yet implemented.", fg="yellow"))
0065         # click.echo("The configuration has been validated and is ready for optimization.")
0066 
0067         # === Addressed TODO for B0 (toy model placeholder as objective function, to be replaced by Geant4 simulations later)
0068         if config.problem.problem_type == "EPIC_B0":
0069             run_epic_b0_toy_optimization(config, verbosity)
0070             return
0071 
0072     except Exception as e:
0073         click.echo(click.style(f"✗ Error: {e}", fg="red"), err=True)
0074         if verbosity > 1:
0075             import traceback
0076             traceback.print_exc()
0077         sys.exit(1)
0078 
0079 
0080 # Future commands (placeholders for documentation)
0081 
0082 def run_command():
0083     """
0084     Execute complete optimization workflow (PLANNED).
0085     
0086     Will support:
0087     - Full orchestration: config → problem → optimizer → execution → results
0088     - Checkpoint/resume support
0089     - Output directory override
0090     - Dry-run mode
0091     
0092     Usage:
0093         aid2e run config.yml
0094         aid2e run config.yml --dry-run
0095         aid2e run config.yml --output results/experiment_1/
0096         aid2e run config.yml --resume checkpoint.json
0097     """
0098     pass
0099 
0100 
0101 def resume_command():
0102     """
0103     Resume optimization from checkpoint (PLANNED).
0104     
0105     Will support:
0106     - Restart interrupted optimization from saved state
0107     - Continue from specific iteration
0108     - Merge results with previous runs
0109     
0110     Usage:
0111         aid2e resume checkpoint.json
0112         aid2e resume checkpoint.json --iterations 50
0113     """
0114     pass
0115 
0116 
0117 def stop_command():
0118     """
0119     Gracefully halt running optimization (PLANNED).
0120     
0121     Will support:
0122     - Stop by run ID or process ID
0123     - Save checkpoint before stopping
0124     - Force stop option
0125     
0126     Usage:
0127         aid2e stop <run_id>
0128         aid2e stop <run_id> --force
0129         aid2e stop --all
0130     """
0131     pass
0132 
0133 
0134 def status_command():
0135     """
0136     Check progress of optimizations (PLANNED).
0137     
0138     Will support:
0139     - Show active runs
0140     - Display iteration progress
0141     - Show current best objectives
0142     - List completed runs
0143     
0144     Usage:
0145         aid2e status
0146         aid2e status <run_id>
0147         aid2e status --all
0148     """
0149     pass
0150 
0151 
0152 def clean_command():
0153     """
0154     Remove temporary and intermediate files (PLANNED).
0155     
0156     Will support:
0157     - Clean work directories
0158     - Remove old checkpoints
0159     - Clear cache files
0160     - Dry-run to preview deletions
0161     
0162     Usage:
0163         aid2e clean <output_dir>
0164         aid2e clean <output_dir> --dry-run
0165         aid2e clean <output_dir> --keep-checkpoints
0166     """
0167     pass