4141 build_checkpoint_signature ,
4242 checkpoint_signature_mismatches ,
4343)
44- from policyengine_us_data .calibration .calibration_utils import (
45- create_target_groups ,
44+ from policyengine_us_data .calibration_package .payload import (
45+ CalibrationPackagePayload ,
46+ CalibrationPackageReader ,
47+ CalibrationPackageWriter ,
4648)
4749from policyengine_us_data .calibration_package .specs import (
4850 DEFAULT_TARGET_CONFIG_PATH as DEFAULT_TARGET_CONFIG_RELATIVE_PATH ,
@@ -680,20 +682,16 @@ def save_calibration_package(
680682 cd_geoid: CD GEOID array from geography assignment.
681683 block_geoid: Block GEOID array from geography assignment.
682684 """
683- import pickle
684-
685- package = {
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- Path (path ).parent .mkdir (parents = True , exist_ok = True )
695- with open (path , "wb" ) as f :
696- 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 )
697695 logger .info ("Calibration package saved to %s" , path )
698696
699697
@@ -706,16 +704,14 @@ def load_calibration_package(path: str) -> dict:
706704 Returns:
707705 Dict with X_sparse, targets_df, target_names, metadata.
708706 """
709- import pickle
710-
711- with open (path , "rb" ) as f :
712- package = pickle .load (f )
707+ payload = CalibrationPackageReader (package_path = Path (path )).read ()
708+ package = payload .to_mapping ()
713709 logger .info (
714710 "Loaded package: %d targets, %d records" ,
715- package [ " X_sparse" ] .shape [0 ],
716- package [ " X_sparse" ] .shape [1 ],
711+ payload . X_sparse .shape [0 ],
712+ payload . X_sparse .shape [1 ],
717713 )
718- meta = package . get ( " metadata" , {})
714+ meta = payload . metadata
719715 print_package_provenance (meta )
720716 check_package_staleness (meta )
721717 return package
@@ -1732,15 +1728,15 @@ def run_calibration(
17321728
17331729 initial_weights = compute_initial_weights (X_sparse , targets_df )
17341730 if package_output_path :
1735- package_payload = {
1736- " X_sparse" : X_sparse ,
1737- " targets_df" : targets_df ,
1738- " target_names" : target_names ,
1739- " metadata" : metadata ,
1740- " initial_weights" : initial_weights ,
1741- " cd_geoid" : geography .cd_geoid ,
1742- " block_geoid" : geography .block_geoid ,
1743- }
1731+ package_payload = CalibrationPackagePayload (
1732+ X_sparse = X_sparse ,
1733+ targets_df = targets_df ,
1734+ target_names = target_names ,
1735+ metadata = metadata ,
1736+ initial_weights = initial_weights ,
1737+ cd_geoid = geography .cd_geoid ,
1738+ block_geoid = geography .block_geoid ,
1739+ )
17441740 save_calibration_package (
17451741 package_output_path ,
17461742 X_sparse ,
0 commit comments