#!/usr/bin/env python3 """ 配置管理模块 """ import yaml from pathlib import Path DEFAULT_CONFIG_YAML = """ simulation: num_events: 8 # 生成的事件数量 output_file: "detector_output" # 去掉扩展名,会根据格式自动添加 output_format: "all" # 可选: binary, text, debug, all packing_mode: "separate" # 打包模式: separate(单独打包), combined(整体打包) event_generation_mode: "pulse" # 事件生成模式: random(随机), fixed(固定), pulse(脉冲) detectors: detector1: name: "detector1" sample_space_size: 5000 allow_replacement: true energy_distribution: type: "normal" mean: 1000.0 std: 200.0 timestamp_distribution: type: "exponential" scale: 50.0 detector2: name: "detector2" sample_space_size: 4000 allow_replacement: true energy_distribution: type: "normal" mean: 800.0 std: 150.0 timestamp_distribution: type: "uniform" low: 0.0 high: 200.0 detector3: name: "detector3" sample_space_size: 6000 allow_replacement: true energy_distribution: type: "gamma" shape: 2.0 scale: 500.0 timestamp_distribution: type: "normal" mean: 100.0 std: 30.0 sampling: min_signals_per_detector: 1 # 每个探测器最少信号数(随机模式使用) max_signals_per_detector: 10 # 每个探测器最多信号数(随机模式使用) require_signal: true # 固定事件模式配置 fixed_events: num_signals_per_event: 5 # 每个事件的信号数量 energy_levels: # 各探测器能量水平 (keV) detector1: [1000, 2000, 3000, 4000, 5000] detector2: [800, 1600, 2400, 3200, 4000] detector3: [500, 1000, 1500, 2000, 2500] timestamps: [10, 50, 100, 200, 300] # 时间戳序列 (us) repeat_pattern: true # 是否重复使用模式 # 脉冲信号模式配置 pulse_signals: pulse_count: 100 # 脉冲数量 pulse_interval: 2 # 脉冲间隔 (us) pulse_width: 1 # 脉冲宽度 (信号数量) energy_levels: # 各探测器能量水平 (keV) detector1: 1500 detector2: 1200 detector3: 900 jitter: 0 # 时间抖动 (us) energy_noise: 0 # 能量噪声 (keV) # 硬件配置 hardware: ip: "192.168.1.100" port: 8080 data_source: "generated" send_mode: "single" loop_count: 1 interval: 100 """ class ConfigManager: """配置管理器""" @staticmethod def load_config(config_file: str = None) -> dict: """ 加载配置 参数: config_file: 配置文件路径 返回: 配置字典 """ if config_file and Path(config_file).exists(): with open(config_file, 'r', encoding='utf-8') as f: return yaml.safe_load(f) else: # 使用默认配置 return yaml.safe_load(DEFAULT_CONFIG_YAML) @staticmethod def save_config(config: dict, config_file: str) -> bool: """ 保存配置 参数: config: 配置字典 config_file: 配置文件路径 返回: 是否保存成功 """ try: with open(config_file, 'w', encoding='utf-8') as f: yaml.dump(config, f, default_flow_style=False, allow_unicode=True) return True except Exception as e: print(f"保存配置失败: {e}") return False @staticmethod def get_default_config() -> dict: """ 获取默认配置 返回: 默认配置字典 """ return yaml.safe_load(DEFAULT_CONFIG_YAML)