MIP → SAM in one call

This example takes a tiny 3×3 MIP fixture and runs the full equilibria.sam_tools.run_mip_to_sam() pipeline. Each step is recorded with balance statistics; the final SAM is balanced via iterated GRAS.

Run the pipeline

from pathlib import Path

import equilibria
from equilibria.sam_tools import run_mip_to_sam

# Locate the test fixture relative to the installed package.
PKG_ROOT = Path(equilibria.__file__).resolve().parents[2]
FIXTURE = PKG_ROOT / "tests" / "sam_tools" / "fixtures" / "simple_mip.xlsx"

result = run_mip_to_sam(FIXTURE)

Inspect the recorded steps

Every transformation logs its name and the post-step balance.

import pandas as pd

steps_df = pd.DataFrame(
    [
        {
            "step": s["step"],
            "total": s["balance"]["total"],
            "max_row_col_diff": s["balance"]["max_row_col_abs_diff"],
        }
        for s in result.steps
    ]
)
print(steps_df.to_string(index=False))
                 step  total  max_row_col_diff
   initial_validation  570.0      1.300000e+02
     mip_balance_gras  570.0      1.300000e+02
        normalize_mip  570.0      1.500000e+02
      disaggregate_va  570.0      1.500000e+02
        factor_income  700.0      1.500000e+02
household_expenditure  700.0      1.500000e+02
           government  742.0      1.580000e+02
          row_account  742.0      1.580000e+02
          make_matrix 1153.0      3.302500e+01
           investment 1214.0      2.100000e+01
       create_x_block 1214.0      2.100000e+01
      convert_exports 1214.0      2.100000e+01
          balance_ras 1214.0      7.827339e-10
     final_validation 1214.0      7.827339e-10

Look at the resulting SAM

df = result.sam.to_dataframe()
print("Shape:", df.shape)
print("Row categories:", sorted({cat for cat, _ in result.sam.row_keys}))
print("Column categories:", sorted({cat for cat, _ in result.sam.col_keys}))
Shape: (24, 24)
Row categories: ['AG', 'FD', 'I', 'IMP', 'J', 'K', 'L', 'OTH', 'VA', 'X']
Column categories: ['AG', 'FD', 'I', 'IMP', 'J', 'K', 'L', 'OTH', 'VA', 'X']

Total running time of the script: (0 minutes 0.837 seconds)

Gallery generated by Sphinx-Gallery