{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3346dcd0",
   "metadata": {},
   "source": [
    "# Structural path analysis\n",
    "\n",
    "This notebook shows how to use `db.calc_spa(...)` on EXIOBASE with MARIO.\n",
    "\n",
    "The case study focuses on the CO2 footprint of German final demand for motor vehicles in 2022. The target sector and indicator are fixed explicitly, so the notebook can focus on selecting and expanding one German final-demand bundle."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5c06aa0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import mario\n",
    "\n",
    "mario.set_palette(\"mario\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e907f6f",
   "metadata": {},
   "source": [
    "## 1. Configure the case study\n",
    "\n",
    "We reuse the same local EXIOBASE folder as the supply-chain notebook, but now the analytical question is narrower: which upstream paths dominate the CO2 footprint of German final demand for motor vehicles in 2022?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6fe434a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "FOLDER = r\"/Users/lorenzorinaldi/Library/CloudStorage/OneDrive-SharedLibraries-PolitecnicodiMilano/DENG-SESAM - Documenti/c-Research/a-Datasets/_Input Output Databases/EXIOBASE/3.10.1\"\n",
    "TABLE = \"IOT\"\n",
    "YEAR = 2022\n",
    "SYSTEM = \"pxp\"\n",
    "SECTOR = \"Motor vehicles, trailers and semi-trailers (34)\"\n",
    "INDICATOR = \"CO2 - combustion - air\"\n",
    "TARGET_REGION = \"DE\"\n",
    "BUNDLE_RANK = 0\n",
    "MAX_DEPTH = 10\n",
    "CUTOFF_SHARE = 0.001\n",
    "TOP_N = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf9f7c9a",
   "metadata": {},
   "source": [
    "## 2. Parse one EXIOBASE year\n",
    "\n",
    "One year is enough for one SPA run. After parsing, the database contains the matrices needed to identify the focal sector, select one final-demand bundle, and expand the dominant upstream paths."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "00fcef6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Parser: reading EXIOBASE IOT from /var/folders/7_/vg3_ld6n2pd73xyzt9dcdhlc0000gn/T/mario_exiobase_iot_zx6d1uct.\n",
      "INFO Parser: reading EXIOBASE IOT extensions from /var/folders/7_/vg3_ld6n2pd73xyzt9dcdhlc0000gn/T/mario_exiobase_iot_zx6d1uct.\n",
      "INFO Parser: using split extension layout with 7 extension directories.\n",
      "INFO Parser: EXIOBASE IOT parsed with 200 sectors, 9 value-added rows and 725 extension rows.\n",
      "INFO Metadata: initialized.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "name = EXIO_IOT_2022_pxp\n",
       "table = IOT\n",
       "scenarios = ['baseline']\n",
       "Factor of production = 9\n",
       "Satellite account = 725\n",
       "Consumption category = 7\n",
       "Region = 49\n",
       "Sector = 200"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db = mario.parse_exiobase(\n",
    "    path=f\"{FOLDER}/{TABLE}_{YEAR}_{SYSTEM}.zip\",\n",
    "    table=\"IOT\",\n",
    "    unit=\"Monetary\",\n",
    ")\n",
    "db"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f67d320c",
   "metadata": {},
   "source": [
    "## 3. Fixed target\n",
    "\n",
    "The target sector and indicator are set directly in the configuration cell above, so no extra label-resolution step is needed in the workflow."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8269b9e6",
   "metadata": {},
   "source": [
    "## 4. Inspect German final demand for the focal sector\n",
    "\n",
    "Before running SPA, it helps to see where the focal motor-vehicle sector is sold directly inside German final demand. The notebook ranks the German destination bundles and uses the largest one as the focal demand bundle for the next step."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b5c4fa2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Region  Level                 Item                                       \n",
       "DE      Consumption category  Final consumption expenditure by households    154583.411697\n",
       "                              Gross fixed capital formation                   59998.089082\n",
       "                              Changes in inventories                          -5815.191944\n",
       "Name: Direct final demand, dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "direct_final_demand = (\n",
    "    db.Y.loc[(slice(None), slice(None), SECTOR), :]\n",
    "    .loc[:, lambda frame: frame.columns.get_level_values(0) == TARGET_REGION]\n",
    "    .sum(axis=0)\n",
    "    .sort_values(ascending=False)\n",
    "    .rename(\"Direct final demand\")\n",
    ")\n",
    "direct_final_demand = direct_final_demand[direct_final_demand != 0]\n",
    "direct_final_demand.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "baae131a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolving f for baseline.\n",
      "INFO Resolver: trying f via formula build_iot_f_from_e_w (compute_method=auto, runtime=inverse).\n",
      "INFO Resolver: resolved f via formula build_iot_f_from_e_w (compute_method=auto, runtime=inverse).\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'Sector': 'Motor vehicles, trailers and semi-trailers (34)',\n",
       " 'Indicator': 'CO2 - combustion - air',\n",
       " 'Region': 'DE',\n",
       " 'Category': 'Final consumption expenditure by households',\n",
       " 'Direct final demand': 154583.4116965654,\n",
       " 'Total footprint': 37657332602.54765,\n",
       " 'Cutoff': 37657332.60254765}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if direct_final_demand.empty:\n",
    "    raise ValueError(f\"No direct final demand was found for {SECTOR} in {TARGET_REGION}.\")\n",
    "focus_bundle = direct_final_demand.index[BUNDLE_RANK]\n",
    "FOCUS_REGION = focus_bundle[0]\n",
    "FOCUS_CATEGORY = focus_bundle[-1]\n",
    "selected_bundle_demand = db.Y.loc[\n",
    "    (slice(None), slice(None), SECTOR),\n",
    "    focus_bundle,\n",
    "]\n",
    "selected_bundle_demand = selected_bundle_demand[selected_bundle_demand != 0]\n",
    "total_footprint = sum(\n",
    "    float(db.f.loc[INDICATOR, node]) * float(value)\n",
    "    for node, value in selected_bundle_demand.items()\n",
    ")\n",
    "CUTOFF = abs(total_footprint) * CUTOFF_SHARE\n",
    "{\n",
    "    \"Sector\": SECTOR,\n",
    "    \"Indicator\": INDICATOR,\n",
    "    \"Region\": FOCUS_REGION,\n",
    "    \"Category\": FOCUS_CATEGORY,\n",
    "    \"Direct final demand\": float(direct_final_demand.iloc[BUNDLE_RANK]),\n",
    "    \"Total footprint\": total_footprint,\n",
    "    \"Cutoff\": CUTOFF,\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c16b5963",
   "metadata": {},
   "source": [
    "## 5. Run structural path analysis with MARIO\n",
    "\n",
    "`db.calc_spa(...)` renders the built-in SPA view directly inside the notebook. Here the main chart is the integrated `plot=\"sankey\"` view, so MARIO displays the dominant structural paths for the selected German final-demand bundle as a network while still returning the ranked path table below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9a9c100b",
   "metadata": {},
   "outputs": [],
   "source": [
    "spa = db.calc_spa(\n",
    "    indicator=INDICATOR,\n",
    "    scenario=\"baseline\",\n",
    "    item=SECTOR,\n",
    "    final_demand_region=FOCUS_REGION,\n",
    "    final_demand_category=FOCUS_CATEGORY,\n",
    "    max_depth=MAX_DEPTH,\n",
    "    cutoff=CUTOFF,\n",
    "    top_n=TOP_N,\n",
    "    plot=\"sankey\",\n",
    "    show_plot=False,\n",
    "    title=f\"Top structural paths for {INDICATOR}\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "611e99be",
   "metadata": {},
   "source": [
    "![Structural path analysis](../../_static/images/spa_DE_motor_vehicles.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b189e6b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'indicator': 'CO2 - combustion - air',\n",
       " 'scenario': 'baseline',\n",
       " 'item': 'Motor vehicles, trailers and semi-trailers (34)',\n",
       " 'unit': 'kg',\n",
       " 'total_footprint': 37657332602.54765,\n",
       " 'reported_contribution': 5192567349.419048,\n",
       " 'coverage_share': 0.13788994043268354,\n",
       " 'max_depth': 10,\n",
       " 'cutoff': 37657332.60254765}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "spa.attrs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d920250",
   "metadata": {},
   "source": [
    "## 6. Summarize the path structure by depth\n",
    "\n",
    "After the Sankey overview, a second MARIO-native read is by upstream depth. This section uses the integrated `plot=\"depth\"` view of `db.calc_spa(...)` and removes the `top_n` cap, so the summary reflects all paths that survive the cutoff rather than only the ranked Sankey subset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a75e51a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "cliponaxis": false,
         "hovertemplate": "Depth=%{x}<br>Share of total=%{y}<br>Share label=%{text}<extra></extra>",
         "legendgroup": "0",
         "marker": {
          "color": "#3F8EFC",
          "pattern": {
           "shape": ""
          }
         },
         "name": "0",
         "orientation": "v",
         "showlegend": true,
         "text": [
          "3.1%"
         ],
         "textposition": "outside",
         "type": "bar",
         "x": [
          "0"
         ],
         "xaxis": "x",
         "y": {
          "bdata": "vw65RBGLnz8=",
          "dtype": "f8"
         },
         "yaxis": "y"
        },
        {
         "cliponaxis": false,
         "hovertemplate": "Depth=%{x}<br>Share of total=%{y}<br>Share label=%{text}<extra></extra>",
         "legendgroup": "1",
         "marker": {
          "color": "#BF9FC2",
          "pattern": {
           "shape": ""
          }
         },
         "name": "1",
         "orientation": "v",
         "showlegend": true,
         "text": [
          "13.2%"
         ],
         "textposition": "outside",
         "type": "bar",
         "x": [
          "1"
         ],
         "xaxis": "x",
         "y": {
          "bdata": "tTuP/qXewD8=",
          "dtype": "f8"
         },
         "yaxis": "y"
        },
        {
         "cliponaxis": false,
         "hovertemplate": "Depth=%{x}<br>Share of total=%{y}<br>Share label=%{text}<extra></extra>",
         "legendgroup": "2",
         "marker": {
          "color": "#79C2D6",
          "pattern": {
           "shape": ""
          }
         },
         "name": "2",
         "orientation": "v",
         "showlegend": true,
         "text": [
          "2.3%"
         ],
         "textposition": "outside",
         "type": "bar",
         "x": [
          "2"
         ],
         "xaxis": "x",
         "y": {
          "bdata": "Nfp7FFcImD8=",
          "dtype": "f8"
         },
         "yaxis": "y"
        }
       ],
       "layout": {
        "barmode": "relative",
        "font": {
         "family": "Verdana",
         "size": 15
        },
        "height": 420,
        "legend": {
         "title": {
          "text": "Depth"
         },
         "tracegroupgap": 0
        },
        "showlegend": false,
        "template": {
         "data": {
          "bar": [
           {
            "error_x": {
             "color": "#2a3f5f"
            },
            "error_y": {
             "color": "#2a3f5f"
            },
            "marker": {
             "line": {
              "color": "white",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "bar"
           }
          ],
          "barpolar": [
           {
            "marker": {
             "line": {
              "color": "white",
              "width": 0.5
             },
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "barpolar"
           }
          ],
          "carpet": [
           {
            "aaxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "#C8D4E3",
             "linecolor": "#C8D4E3",
             "minorgridcolor": "#C8D4E3",
             "startlinecolor": "#2a3f5f"
            },
            "baxis": {
             "endlinecolor": "#2a3f5f",
             "gridcolor": "#C8D4E3",
             "linecolor": "#C8D4E3",
             "minorgridcolor": "#C8D4E3",
             "startlinecolor": "#2a3f5f"
            },
            "type": "carpet"
           }
          ],
          "choropleth": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "choropleth"
           }
          ],
          "contour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "contour"
           }
          ],
          "contourcarpet": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "contourcarpet"
           }
          ],
          "heatmap": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "heatmap"
           }
          ],
          "histogram": [
           {
            "marker": {
             "pattern": {
              "fillmode": "overlay",
              "size": 10,
              "solidity": 0.2
             }
            },
            "type": "histogram"
           }
          ],
          "histogram2d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2d"
           }
          ],
          "histogram2dcontour": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "histogram2dcontour"
           }
          ],
          "mesh3d": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "type": "mesh3d"
           }
          ],
          "parcoords": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "parcoords"
           }
          ],
          "pie": [
           {
            "automargin": true,
            "type": "pie"
           }
          ],
          "scatter": [
           {
            "fillpattern": {
             "fillmode": "overlay",
             "size": 10,
             "solidity": 0.2
            },
            "type": "scatter"
           }
          ],
          "scatter3d": [
           {
            "line": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatter3d"
           }
          ],
          "scattercarpet": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattercarpet"
           }
          ],
          "scattergeo": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergeo"
           }
          ],
          "scattergl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattergl"
           }
          ],
          "scattermap": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermap"
           }
          ],
          "scattermapbox": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scattermapbox"
           }
          ],
          "scatterpolar": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolar"
           }
          ],
          "scatterpolargl": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterpolargl"
           }
          ],
          "scatterternary": [
           {
            "marker": {
             "colorbar": {
              "outlinewidth": 0,
              "ticks": ""
             }
            },
            "type": "scatterternary"
           }
          ],
          "surface": [
           {
            "colorbar": {
             "outlinewidth": 0,
             "ticks": ""
            },
            "colorscale": [
             [
              0,
              "#0d0887"
             ],
             [
              0.1111111111111111,
              "#46039f"
             ],
             [
              0.2222222222222222,
              "#7201a8"
             ],
             [
              0.3333333333333333,
              "#9c179e"
             ],
             [
              0.4444444444444444,
              "#bd3786"
             ],
             [
              0.5555555555555556,
              "#d8576b"
             ],
             [
              0.6666666666666666,
              "#ed7953"
             ],
             [
              0.7777777777777778,
              "#fb9f3a"
             ],
             [
              0.8888888888888888,
              "#fdca26"
             ],
             [
              1,
              "#f0f921"
             ]
            ],
            "type": "surface"
           }
          ],
          "table": [
           {
            "cells": {
             "fill": {
              "color": "#EBF0F8"
             },
             "line": {
              "color": "white"
             }
            },
            "header": {
             "fill": {
              "color": "#C8D4E3"
             },
             "line": {
              "color": "white"
             }
            },
            "type": "table"
           }
          ]
         },
         "layout": {
          "annotationdefaults": {
           "arrowcolor": "#2a3f5f",
           "arrowhead": 0,
           "arrowwidth": 1
          },
          "autotypenumbers": "strict",
          "coloraxis": {
           "colorbar": {
            "outlinewidth": 0,
            "ticks": ""
           }
          },
          "colorscale": {
           "diverging": [
            [
             0,
             "#8e0152"
            ],
            [
             0.1,
             "#c51b7d"
            ],
            [
             0.2,
             "#de77ae"
            ],
            [
             0.3,
             "#f1b6da"
            ],
            [
             0.4,
             "#fde0ef"
            ],
            [
             0.5,
             "#f7f7f7"
            ],
            [
             0.6,
             "#e6f5d0"
            ],
            [
             0.7,
             "#b8e186"
            ],
            [
             0.8,
             "#7fbc41"
            ],
            [
             0.9,
             "#4d9221"
            ],
            [
             1,
             "#276419"
            ]
           ],
           "sequential": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ],
           "sequentialminus": [
            [
             0,
             "#0d0887"
            ],
            [
             0.1111111111111111,
             "#46039f"
            ],
            [
             0.2222222222222222,
             "#7201a8"
            ],
            [
             0.3333333333333333,
             "#9c179e"
            ],
            [
             0.4444444444444444,
             "#bd3786"
            ],
            [
             0.5555555555555556,
             "#d8576b"
            ],
            [
             0.6666666666666666,
             "#ed7953"
            ],
            [
             0.7777777777777778,
             "#fb9f3a"
            ],
            [
             0.8888888888888888,
             "#fdca26"
            ],
            [
             1,
             "#f0f921"
            ]
           ]
          },
          "colorway": [
           "#636efa",
           "#EF553B",
           "#00cc96",
           "#ab63fa",
           "#FFA15A",
           "#19d3f3",
           "#FF6692",
           "#B6E880",
           "#FF97FF",
           "#FECB52"
          ],
          "font": {
           "color": "#2a3f5f"
          },
          "geo": {
           "bgcolor": "white",
           "lakecolor": "white",
           "landcolor": "white",
           "showlakes": true,
           "showland": true,
           "subunitcolor": "#C8D4E3"
          },
          "hoverlabel": {
           "align": "left"
          },
          "hovermode": "closest",
          "mapbox": {
           "style": "light"
          },
          "paper_bgcolor": "white",
          "plot_bgcolor": "white",
          "polar": {
           "angularaxis": {
            "gridcolor": "#EBF0F8",
            "linecolor": "#EBF0F8",
            "ticks": ""
           },
           "bgcolor": "white",
           "radialaxis": {
            "gridcolor": "#EBF0F8",
            "linecolor": "#EBF0F8",
            "ticks": ""
           }
          },
          "scene": {
           "xaxis": {
            "backgroundcolor": "white",
            "gridcolor": "#DFE8F3",
            "gridwidth": 2,
            "linecolor": "#EBF0F8",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "#EBF0F8"
           },
           "yaxis": {
            "backgroundcolor": "white",
            "gridcolor": "#DFE8F3",
            "gridwidth": 2,
            "linecolor": "#EBF0F8",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "#EBF0F8"
           },
           "zaxis": {
            "backgroundcolor": "white",
            "gridcolor": "#DFE8F3",
            "gridwidth": 2,
            "linecolor": "#EBF0F8",
            "showbackground": true,
            "ticks": "",
            "zerolinecolor": "#EBF0F8"
           }
          },
          "shapedefaults": {
           "line": {
            "color": "#2a3f5f"
           }
          },
          "ternary": {
           "aaxis": {
            "gridcolor": "#DFE8F3",
            "linecolor": "#A2B1C6",
            "ticks": ""
           },
           "baxis": {
            "gridcolor": "#DFE8F3",
            "linecolor": "#A2B1C6",
            "ticks": ""
           },
           "bgcolor": "white",
           "caxis": {
            "gridcolor": "#DFE8F3",
            "linecolor": "#A2B1C6",
            "ticks": ""
           }
          },
          "title": {
           "x": 0.05
          },
          "xaxis": {
           "automargin": true,
           "gridcolor": "#EBF0F8",
           "linecolor": "#EBF0F8",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "#EBF0F8",
           "zerolinewidth": 2
          },
          "yaxis": {
           "automargin": true,
           "gridcolor": "#EBF0F8",
           "linecolor": "#EBF0F8",
           "ticks": "",
           "title": {
            "standoff": 15
           },
           "zerolinecolor": "#EBF0F8",
           "zerolinewidth": 2
          }
         }
        },
        "title": {
         "text": "Footprint by SPA depth for CO2 - combustion - air"
        },
        "xaxis": {
         "anchor": "y",
         "categoryarray": [
          "0",
          "1",
          "2"
         ],
         "categoryorder": "array",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "Depth"
         }
        },
        "yaxis": {
         "anchor": "x",
         "domain": [
          0,
          1
         ],
         "tickformat": ".0%",
         "title": {
          "text": "Share of total footprint"
         }
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "spa_full = db.calc_spa(\n",
    "    indicator=INDICATOR,\n",
    "    scenario=\"baseline\",\n",
    "    item=SECTOR,\n",
    "    final_demand_region=FOCUS_REGION,\n",
    "    final_demand_category=FOCUS_CATEGORY,\n",
    "    max_depth=MAX_DEPTH,\n",
    "    cutoff=CUTOFF,\n",
    "    top_n=None,\n",
    "    plot=\"depth\",\n",
    "    show_plot=True,\n",
    "    title=f\"Footprint by SPA depth for {INDICATOR}\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec396a98",
   "metadata": {},
   "source": [
    "The same pattern can be reused with another indicator, another sector, or another EXIOBASE year. For this German motor-vehicles example, the most useful next variations are usually: comparing household versus investment demand, testing another manufacturing sector, or checking how the dominant CO2 paths shift over time."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mario",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
