mario.Database.calc_trades#
db.calc_trades(...) aggregates one sector or commodity into a
region-by-region trade matrix.
When item is omitted, MARIO aggregates all sectors or commodities together,
but only when they share one common unit of measure. Hybrid or otherwise
heterogeneous unit layouts still require an explicit item.
For IOT tables it uses Z for intermediate trade and Y for final demand.
For SUT tables it uses U for intermediate trade and Yc for final
demand when the table follows the Isard format.
For Chenery-Moses SUT tables, commodity trade is aggregated from S instead.
Because that trade is stored as one supply-side flow matrix, MARIO currently
supports only the aggregate case with intermediate=True, final=True and
aggregate=True.
When both intermediate=True and final=True, the default is
aggregate=True, so the returned dataframe is the sum of intermediate and
final trade. Use aggregate=False to keep the two components separate under a
column MultiIndex.
Use total=True to add row and column totals.
Use clusters=... to aggregate the Region dimension before totals and
plotting. This accepts Region aggregation presets such as 'continent' and
explicit mappings such as {'EU': ['FR', 'DE', 'IT']}.
Use clusters_direction='origin' to aggregate only the origin Regions,
'destination' to aggregate only the destination Regions, or 'both' to
aggregate both axes. The default is 'both'.
Use scenario='all' to calculate the same trade matrix for every available
scenario on the database. In that case db.calc_trades(...) returns a
dictionary {scenario: dataframe} instead of a single dataframe.
Use show_plot=True to also build and display a region-by-region heatmap
while still returning the trade matrix.
Use exclude_domestic_from_plot=True to hide same-label origin-destination
pairs from that heatmap without changing the returned trade matrix.
When more than one scenario is plotted together, MARIO builds one animated heatmap with a scenario slider.
Use save_plot=... to write that heatmap to an HTML file. The older
path=... argument is still accepted as a backward-compatible alias. When
save_plot is provided, MARIO saves the plot without displaying it. When the
path ends in .html or .htm, MARIO writes HTML; for image extensions
such as .png or .svg, it uses Plotly image export.
Use title=... to override the heatmap title. The heatmap colorbar uses the
unit of the selected item instead of the generic sum of Value label.
- Database.calc_trades(item=None, scenario='baseline', clusters=None, clusters_direction='both', intermediate=True, final=True, aggregate=True, total=False, show_plot=False, exclude_domestic_from_plot=False, save_plot=None, path=False, auto_open=False, title=None)#
Calculate region-by-region trade matrices for one item.
- Parameters:
item – Sector or commodity label to aggregate by region. When omitted, MARIO aggregates all sectors or commodities only if they share one common unit of measure.
scenario – Scenario to analyse.
clusters – Optional Region aggregation preset or mapping applied before totals and plotting. This accepts the same Region aggregation payloads used by MARIO’s region aggregation helpers, including preset strings such as
"continent"and explicit{"Group": [regions...]}mappings.clusters_direction – Select which axis should receive Region aggregation when
clustersis provided. Use"origin"to aggregate only rows,"destination"to aggregate only columns, or"both"for the default square aggregation.intermediate – When
True, include intermediate transactions. This usesZfor IOT tables andUfor SUT tables.final – When
True, include final-demand transactions. This usesYfor IOT tables andYcfor SUT tables.aggregate – When
Trueand bothintermediateandfinalare enabled, return their sum as one trade matrix. Useaggregate=Falseto keep the two components separate.total – When
True, add aTotalrow and aTotalcolumn.show_plot – When
True, build and display a region-by-region heatmap for the calculated trades.exclude_domestic_from_plot – When
True, omit same-label origin-destination pairs from the heatmap only. The returned trade matrix is not modified.save_plot – Optional HTML output path for the heatmap. When provided, MARIO writes the plot to this path.
path – Backward-compatible alias for
save_plot. Ignored when neithershow_plotnorsave_plotare requested. PassFalseto skip HTML export.auto_open – When
True, automatically open the saved heatmap HTML.title – Optional title for the heatmap. When omitted, MARIO uses
"Trades for {item}".
- Returns:
Region-by-region trade matrix for one scenario, or a dictionary
{scenario: matrix}when more than one scenario is requested. When bothintermediateandfinalare requested, the default is to return their aggregate sum. Useaggregate=Falseto get a dataframe with column MultiIndex top-level labelsIntermediateandFinal. When more than one scenario is plotted together, MARIO builds one animated heatmap with a scenario slider.- Return type:
pandas.DataFrame | dict