1+ import toml
2+ from pydantic import BaseModel , model_validator
3+ from typing import Optional
4+ from templates .calibration import CalibrationConfig , SimultaneousCalibrationConfig
5+
6+ CONFIG = None
7+
8+ class HHRebalancingModuleConfig (BaseModel ):
9+ control_table : str
10+ control_col : str
11+ geoid_col : str
12+
13+ class EmploymentModuleConfig (BaseModel ):
14+ simultaneous_calibration_config : Optional [SimultaneousCalibrationConfig ] = None
15+ enter_model_calibration_procedure : Optional [CalibrationConfig ] = None
16+ exit_model_calibration_procedure : Optional [CalibrationConfig ] = None
17+
18+ @model_validator (mode = "after" )
19+ def check_calibration_config_exclusivity (self ):
20+ sim_cal = self .simultaneous_calibration_config is not None
21+ enter_cal = self .enter_model_calibration_procedure is not None
22+ exit_cal = self .exit_model_calibration_procedure is not None
23+ if sim_cal and (enter_cal or exit_cal ):
24+ raise ValueError (f"Simultaneous calibration cannot be used at the same time as " + \
25+ f"individual model calibration. Simultaneous selected: { sim_cal } , " + \
26+ f"EnterModel selected: { enter_cal } , ExitModel selected: { exit_cal } " )
27+ return self
28+
29+ class HHReorgModuleConfig (BaseModel ):
30+ simultaneous_calibration_config : Optional [SimultaneousCalibrationConfig ] = None
31+ geoid_col : Optional [str ] = None
32+
33+ class MortalityModuleConfig (BaseModel ):
34+ calibration_procedure : Optional [CalibrationConfig ] = None
35+
36+ class BirthModuleConfig (BaseModel ):
37+ calibration_procedure : Optional [CalibrationConfig ] = None
38+
39+ class DEMOSConfig (BaseModel ):
40+ # Global config
41+ region_code : str
42+ forecast_year : int = 2020
43+ random_seed : int
44+ base_year : int
45+ output_fname : str = None
46+ calibrated_folder : str = "custom"
47+
48+ # Module-specific config
49+ employment_module_config : EmploymentModuleConfig
50+ mortality_module_config : MortalityModuleConfig
51+ birth_module_config : BirthModuleConfig
52+ hh_reorg_module_config : HHReorgModuleConfig
53+ hh_rebalancing_module_config : HHRebalancingModuleConfig
54+
55+ def __init__ (self , * args , ** kwargs ):
56+ super ().__init__ (* args , ** kwargs )
57+ if self .output_fname is None :
58+ self .output_fname = "data/model_data_{0}.h5" .format (self .forecast_year )
59+
60+
61+ def update (self , ** kwargs ):
62+ for key , value in kwargs .items ():
63+ setattr (self , key , value )
64+
65+ def load_config_file (dir : str ) -> DEMOSConfig :
66+ global CONFIG
67+ CONFIG = DEMOSConfig (** toml .load (dir ))
68+
69+ def set_config (params_dict : dict ):
70+ global CONFIG
71+ CONFIG .update (** vars (params_dict ))
72+
73+ def get_config ():
74+ global CONFIG
75+ return CONFIG
0 commit comments