4444from policyengine_us_data .calibration .calibration_utils import (
4545 create_target_groups ,
4646)
47+ from policyengine_us_data .calibration_package .payload import (
48+ CalibrationPackagePayload ,
49+ CalibrationPackageReader ,
50+ CalibrationPackageWriter ,
51+ )
4752from policyengine_us_data .calibration_package .specs import (
4853 DEFAULT_TARGET_CONFIG_PATH as DEFAULT_TARGET_CONFIG_RELATIVE_PATH ,
4954 TargetConfigIdentity ,
@@ -677,20 +682,16 @@ def save_calibration_package(
677682 cd_geoid: CD GEOID array from geography assignment.
678683 block_geoid: Block GEOID array from geography assignment.
679684 """
680- import pickle
681-
682- package = {
683- "X_sparse" : X_sparse ,
684- "targets_df" : targets_df ,
685- "target_names" : target_names ,
686- "metadata" : metadata ,
687- "initial_weights" : initial_weights ,
688- "cd_geoid" : cd_geoid ,
689- "block_geoid" : block_geoid ,
690- }
691- Path (path ).parent .mkdir (parents = True , exist_ok = True )
692- with open (path , "wb" ) as f :
693- pickle .dump (package , f , protocol = pickle .HIGHEST_PROTOCOL )
685+ payload = CalibrationPackagePayload (
686+ X_sparse = X_sparse ,
687+ targets_df = targets_df ,
688+ target_names = target_names ,
689+ metadata = metadata ,
690+ initial_weights = initial_weights ,
691+ cd_geoid = cd_geoid ,
692+ block_geoid = block_geoid ,
693+ )
694+ CalibrationPackageWriter (package_path = Path (path )).write (payload )
694695 logger .info ("Calibration package saved to %s" , path )
695696
696697
@@ -703,16 +704,14 @@ def load_calibration_package(path: str) -> dict:
703704 Returns:
704705 Dict with X_sparse, targets_df, target_names, metadata.
705706 """
706- import pickle
707-
708- with open (path , "rb" ) as f :
709- package = pickle .load (f )
707+ payload = CalibrationPackageReader (package_path = Path (path )).read ()
708+ package = payload .to_mapping ()
710709 logger .info (
711710 "Loaded package: %d targets, %d records" ,
712- package [ " X_sparse" ] .shape [0 ],
713- package [ " X_sparse" ] .shape [1 ],
711+ payload . X_sparse .shape [0 ],
712+ payload . X_sparse .shape [1 ],
714713 )
715- meta = package . get ( " metadata" , {})
714+ meta = payload . metadata
716715 print_package_provenance (meta )
717716 check_package_staleness (meta )
718717 return package
@@ -1727,15 +1726,15 @@ def run_calibration(
17271726
17281727 initial_weights = compute_initial_weights (X_sparse , targets_df )
17291728 if package_output_path :
1730- package_payload = {
1731- " X_sparse" : X_sparse ,
1732- " targets_df" : targets_df ,
1733- " target_names" : target_names ,
1734- " metadata" : metadata ,
1735- " initial_weights" : initial_weights ,
1736- " cd_geoid" : geography .cd_geoid ,
1737- " block_geoid" : geography .block_geoid ,
1738- }
1729+ package_payload = CalibrationPackagePayload (
1730+ X_sparse = X_sparse ,
1731+ targets_df = targets_df ,
1732+ target_names = target_names ,
1733+ metadata = metadata ,
1734+ initial_weights = initial_weights ,
1735+ cd_geoid = geography .cd_geoid ,
1736+ block_geoid = geography .block_geoid ,
1737+ )
17391738 save_calibration_package (
17401739 package_output_path ,
17411740 X_sparse ,
0 commit comments