Install the optional cvxlab package in the same Python environment as MARIO. Provide the target model’s model_settings.xlsx and map its sets and exogenous data tables to MARIO coordinates and matrices.

The worked example uses these SUT settings. Save the workbook beside the notebook. It declares ten SUT input tables and a small feasibility problem for checking workbook loading.

import mario

source = mario.load_test("SUT")
db = source.to_single_region(
    source.regions[0], imports_by="commodity",
    import_representation="separate", inplace=False,
)
report = db.to_cvxlab(
    model_settings="cvxlab_sut_settings.xlsx",
    path="cvxlab_example",
    scenarios="baseline",
    mapping={
        "sets": {
            "Region_from": "region_from",
            "Region_to": "region_to",
            "Activity_from": "activity_from",
            "Activity_to": "activity_to",
            "Commodity_from": "commodity_from",
            "Commodity_to": "commodity_to",
            "Consumption category_to": "consumption",
            "Factor of production_from": "factor",
            "Satellite account_from": "extension",
        },
        "matrices": {
            "S": "SUPPLY", "U": "USE", "Yc": "FINAL_DEMAND",
            "Va": "VALUE_ADDED", "VY": "VALUE_ADDED_FINAL_DEMAND",
            "IMP_a": "IMPORTS_USE", "IMP_c": "IMPORTS_SUPPLY",
            "IMP_Y": "IMPORTS_FINAL_DEMAND",
            "Ea": "EMISSIONS", "EY": "EMISSIONS_FINAL_DEMAND",
        },
    },
)

Result · A model directory containing model_settings.xlsx, sets.xlsx and input_data/input_data.xlsx. Each input table is a sheet with the coordinates and value column generated by CVXLab.

Define the mapping#

Set keys identify MARIO flat-export coordinates. _from identifies a row axis and _to a column axis. Destination names identify sets in the CVXLab settings. Bare MARIO set names apply to either populated side, and a list can map one source classification to multiple destination sets. copy_from aliases use their source sheet in sets.xlsx. The exported settings order source sets before their aliases so that CVXLab can load the workbook directly.

Matrix keys identify MARIO matrices. Destination names identify CVXLab exogenous data tables in structure_variables. Use the table key defined there. The transferred columns follow the table’s coordinate declarations. Values use the units of the selected MARIO matrices. See the API reference for complete arguments.

Every source axis with multiple labels needs a mapping. A singleton source axis can be omitted. An additional destination coordinate with one defined member can be filled automatically. CVXLab’s set filters determine the input rows to populate. unmatched_coordinates reports source rows outside those rows.

Native IMP_a, IMP_c and IMP_Y use the same matrix interface as Va and VY. For an IOT, map Sector_from and Sector_to and select the IOT matrices, including IMP and IMP_Y when imports are separate. See Separate import accounts for extraction and import detail.

Populate one table from several matrices#

Assign the same destination to several matrices to populate disjoint blocks of a CVXLab table. For example, a model can declare use_0 and imports_0 over products and accounts, and extensions_0 over extensions and accounts. Map activities and final demand to the shared account set, and factors and environmental flows to the shared extension set.

shared_mapping = {
    "sets": {
        "Commodity_from": "products",
        "Activity_to": "accounts",
        "Consumption category_to": "accounts",
        "Factor of production_from": "extensions",
        "Satellite account_from": "extensions",
    },
    "matrices": {
        "U": "use_0", "Yc": "use_0",
        "IMP_a": "imports_0", "IMP_Y": "imports_0",
        "Va": "extensions_0", "VY": "extensions_0",
        "Ea": "extensions_0", "EY": "extensions_0",
    },
}
report = db.to_cvxlab(
    model_settings="combined_model_settings.xlsx",
    path="combined_model",
    mapping=shared_mapping,
)

The model settings for this example must declare those shared tables and sets. CVXLab generates their coordinates, then MARIO writes each source block into its matching cells. Overlapping source coordinates cause an explicit error before destination files are updated. A source whose extra coordinates are pending is listed in pending_matrices while other complete blocks can be transferred. written_cells counts the cells written to each target table.

Update existing workbooks#

Repeated calls append new mapped set members and preserve existing attributes. Mapped input cells are updated by their coordinate labels. Other scenarios, additional input data and user sheets are preserved. The method adds missing set sheets and input tables using the structures generated by CVXLab.

Exports are prepared in a temporary directory. A failed file replacement rolls back the updated destination files. A concurrent export to the same directory, or an external edit during preparation, causes an explicit error.

Complete additional inputs#

Inspect the returned report after exporting.

report["written_cells"]
report["pending_sets"]
report["missing_attributes"]
report["pending_matrices"]
report["unmapped_tables"]
report["unmatched_coordinates"]
report["missing_values"]
report["extra_input_rows"]

pending_sets lists sets needing members. missing_attributes identifies unfilled declared set attributes. pending_matrices lists additional set coordinates needed for mapped matrices. unmapped_tables identifies inputs to supply separately, and missing_values counts empty cells in the processed input tables. extra_input_rows identifies existing records outside the current CVXLab coordinates or filters for review. A warning directs attention to outstanding data.

Fill the additional information in the generated workbooks and call the method again. The method can populate the matrices whose coordinates are complete while providing templates for the remaining inputs.

For several scenarios, pass their names to scenarios and map Scenario to the appropriate CVXLab scenario set. Each input table must include that coordinate in its settings.

mapping["sets"]["Scenario"] = "scenario"
report = db.to_cvxlab(
    model_settings="scenario_model_settings.xlsx",
    path="scenario_model",
    scenarios=["baseline", "policy"],
    mapping=mapping,
)

To export the native MARIO names and layouts, use Export a database. The CVXLab method prepares single-workbook Excel inputs using the structure of the supplied settings.