{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f97ec22d",
   "metadata": {},
   "source": [
    "# Shock analyses\n",
    "\n",
    "This workflow covers the standard way to apply a shock to a *database* by creating a new scenario via the dedicate Excel template.\n",
    "\n",
    "Core methods\n",
    "------------\n",
    "\n",
    "The two main entry points are:\n",
    "\n",
    "* `Database.get_shock_excel` to write the workbook template;\n",
    "* `Database.shock_calc` to read that workbook and store a shocked scenario.\n",
    "\n",
    "Typical flow\n",
    "------------\n",
    "\n",
    "The usual sequence is:\n",
    "\n",
    "* generate a template workbook from the current database\n",
    "* fill the rows you want to shock\n",
    "* apply the workbook into a named scenario"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Excel-supported shocks\n",
    "\n",
    "This is the standard workflow for supported shocks in MARIO: generate the workbook template, fill it, and load it back with `shock_calc(...)`. The examples below show the Excel route for both IOT and SUT tables.\n"
   ],
   "id": "excel-supported-shocks"
  },
  {
   "cell_type": "markdown",
   "id": "7b240ee9",
   "metadata": {},
   "source": [
    "___\n",
    "### IOT workflow\n",
    "\n",
    "Load the test IOT table embedded into MARIO "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "85e43231",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:40:48.984021Z",
     "iopub.status.busy": "2026-04-26T22:40:48.983700Z",
     "iopub.status.idle": "2026-04-26T22:40:51.214075Z",
     "shell.execute_reply": "2026-04-26T22:40:51.213691Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Parser: excel reading IOT flows from /Users/lorenzorinaldi/Documents/GitHub/MARIO/mario/test/tables/test_IOT_standard.xlsx.\n",
      "INFO Parser: state payload ready with 6 canonical blocks.\n",
      "INFO Parser: excel state ready for IOT.\n",
      "INFO Metadata: initialized.\n"
     ]
    }
   ],
   "source": [
    "import mario\n",
    "db_IOT = mario.load_test(\"IOT\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d415be91",
   "metadata": {},
   "source": [
    "#### Generate an empty shock template"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7dd30006",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:40:51.324821Z",
     "iopub.status.busy": "2026-04-26T22:40:51.324706Z",
     "iopub.status.idle": "2026-04-26T22:40:51.372567Z",
     "shell.execute_reply": "2026-04-26T22:40:51.372262Z"
    }
   },
   "outputs": [],
   "source": [
    "db_IOT.get_shock_excel(\n",
    "    path=\"/path/to/shock_IOT_template.xlsx\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "062d1c1e",
   "metadata": {},
   "source": [
    "#### Fill shock template\n",
    "\n",
    "Fill the shocked template to simulate a change in consumption of \"Industry\" outputs from both \"Reg1\" and \"Reg2\" by \"Services\" sector in \"Reg2\", which would cause also an increase in CO2 emissions of such sector. \n",
    "\n",
    "These changes affect only `z` and `e` matrices\n",
    "\n",
    "Being a simplified example, only \"Percentage\" change type are adopted, but also \"Absolute\" and \"Update\" changes could be used\n",
    "\n",
    "![shock_iot_template](../../_static/images/shock_IOT_template.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "611899dd",
   "metadata": {},
   "source": [
    "#### Apply a filled shock workbook\n",
    "\n",
    "For IOTs, the matrices that can be shocked are ``z``, ``v``, ``e`` and ``Y``.\n",
    "\n",
    "In our example, the filled workbook updates one `z` and `e` coefficient matrices. We use the ``scenario`` argument to define the output name of the new scenario. If omitted, MARIO creates a name such as ``shock 1`` automatically."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc4a7128",
   "metadata": {},
   "source": [
    "##### Download the packaged example workbooks\n",
    "\n",
    "The exact workbooks used in this notebook are available here:\n",
    "\n",
    "- [IOT shock template](../../_static/data/supporting_files/shock_IOT_template.xlsx)\n",
    "- [Filled IOT shock workbook](../../_static/data/supporting_files/shock_IOT_filled.xlsx)\n",
    "- [SUT shock template](../../_static/data/supporting_files/shock_SUT_template.xlsx)\n",
    "- [Filled SUT shock workbook](../../_static/data/supporting_files/shock_SUT_filled.xlsx)\n",
    "- [IOT cluster shock template](../../_static/data/supporting_files/shock_IOT_template_clusters.xlsx)\n",
    "- [Filled IOT cluster shock workbook](../../_static/data/supporting_files/shock_IOT_filled_clusters.xlsx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f136b81",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:40:51.374497Z",
     "iopub.status.busy": "2026-04-26T22:40:51.374390Z",
     "iopub.status.idle": "2026-04-26T22:40:51.612802Z",
     "shell.execute_reply": "2026-04-26T22:40:51.612510Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolving z for baseline.\n",
      "INFO Resolver: trying z via formula build_iot_z_from_Z_X.\n",
      "INFO Resolver: resolved z via formula build_iot_z_from_Z_X.\n",
      "INFO Resolver: resolving X for baseline.\n",
      "INFO Resolver: trying X via formula build_iot_X_from_Z_Y.\n",
      "INFO Resolver: resolved X via formula build_iot_X_from_Z_Y.\n",
      "INFO Resolver: resolving e for baseline.\n",
      "INFO Resolver: trying e via formula build_iot_e_from_E_X.\n",
      "INFO Resolver: resolved e via formula build_iot_e_from_E_X.\n",
      "INFO Resolver: resolving v for baseline.\n",
      "INFO Resolver: trying v via formula build_iot_v_from_V_X.\n",
      "INFO Resolver: resolved v via formula build_iot_v_from_V_X.\n",
      "INFO Shock: Shock implemented successfully.\n"
     ]
    }
   ],
   "source": [
    "db_IOT.shock_calc(\n",
    "    io=\"/path/to/shock_IOT_filled.xlsx\",\n",
    "    z=True,\n",
    "    e=True,\n",
    "    scenario=\"example shock\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a0391c3",
   "metadata": {},
   "source": [
    "You will notice the *database* now has 2 scenarios "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c779aed2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['baseline', 'example shock']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db_IOT.scenarios"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "052fc8f9",
   "metadata": {},
   "source": [
    "#### Inspect the shocked coefficient matrix\n",
    "\n",
    "The `query` method can also be used to check variations (absolute or relative) of matrices across scenarios. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "253407f2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:40:51.614685Z",
     "iopub.status.busy": "2026-04-26T22:40:51.614550Z",
     "iopub.status.idle": "2026-04-26T22:40:51.621493Z",
     "shell.execute_reply": "2026-04-26T22:40:51.621244Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Region</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Reg1</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Reg2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Level</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Sector</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Sector</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Item</th>\n",
       "      <th>Agriculture</th>\n",
       "      <th>Industry</th>\n",
       "      <th>Services</th>\n",
       "      <th>Agriculture</th>\n",
       "      <th>Industry</th>\n",
       "      <th>Services</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Region</th>\n",
       "      <th>Level</th>\n",
       "      <th>Item</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Reg1</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">Sector</th>\n",
       "      <th>Agriculture</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Reg2</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">Sector</th>\n",
       "      <th>Agriculture</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Region                           Reg1                          Reg2           \\\n",
       "Level                          Sector                        Sector            \n",
       "Item                      Agriculture Industry Services Agriculture Industry   \n",
       "Region Level  Item                                                             \n",
       "Reg1   Sector Agriculture         0.0      0.0      0.0         0.0      0.0   \n",
       "              Industry            0.0      0.0      0.0         0.0      0.0   \n",
       "              Services            0.0      0.0      0.0         0.0      0.0   \n",
       "Reg2   Sector Agriculture         0.0      0.0      0.0         0.0      0.0   \n",
       "              Industry            0.0      0.0      0.0         0.0      0.0   \n",
       "              Services            0.0      0.0      0.0         0.0      0.0   \n",
       "\n",
       "Region                              \n",
       "Level                               \n",
       "Item                      Services  \n",
       "Region Level  Item                  \n",
       "Reg1   Sector Agriculture      0.0  \n",
       "              Industry         0.2  \n",
       "              Services         0.0  \n",
       "Reg2   Sector Agriculture      0.0  \n",
       "              Industry         0.3  \n",
       "              Services         0.0  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db_IOT.query(\n",
    "    matrices=\"z\", \n",
    "    scenarios=\"example shock\", \n",
    "    base_scenario=\"baseline\", \n",
    "    type=\"relative\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea491f2d",
   "metadata": {},
   "source": [
    "`query(...)` is still the most direct way to inspect exact coefficients. When you want a faster visual scan of where the shock is concentrated, you can use `plot(...)` on the same matrix.\n",
    "\n",
    "The first plot below shows the relative variation of the shocked scenario against the baseline."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b7904721",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "coloraxis": "coloraxis",
         "histfunc": "sum",
         "hovertemplate": "Item_to=%{y}<br>sum of Value=%{z}<extra></extra>",
         "name": "",
         "type": "histogram2d",
         "x": [
          "Agriculture",
          "Industry",
          "Services"
         ],
         "xaxis": "x",
         "xbingroup": "x",
         "y": [
          "Agriculture",
          "Industry",
          "Services"
         ],
         "yaxis": "y",
         "ybingroup": "y",
         "z": {
          "bdata": "+y+JZ8h+wj8WLwFE8bPVP7wJmwNNN+I/",
          "dtype": "f8"
         }
        }
       ],
       "layout": {
        "coloraxis": {
         "colorbar": {
          "title": {
           "text": "sum of Value"
          }
         },
         "colorscale": [
          [
           0,
           "rgb(5,48,97)"
          ],
          [
           0.1,
           "rgb(33,102,172)"
          ],
          [
           0.2,
           "rgb(67,147,195)"
          ],
          [
           0.3,
           "rgb(146,197,222)"
          ],
          [
           0.4,
           "rgb(209,229,240)"
          ],
          [
           0.5,
           "rgb(247,247,247)"
          ],
          [
           0.6,
           "rgb(253,219,199)"
          ],
          [
           0.7,
           "rgb(244,165,130)"
          ],
          [
           0.8,
           "rgb(214,96,77)"
          ],
          [
           0.9,
           "rgb(178,24,43)"
          ],
          [
           1,
           "rgb(103,0,31)"
          ]
         ]
        },
        "font": {
         "family": "Verdana",
         "size": 15
        },
        "legend": {
         "tracegroupgap": 0
        },
        "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": "Intersectoral transaction flows vs baseline (relative)"
        },
        "xaxis": {
         "anchor": "y",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "Item_to"
         }
        },
        "yaxis": {
         "anchor": "x",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "Item_to"
         }
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "db_IOT.plot(\n",
    "    matrix=\"Z\",\n",
    "    scenarios=[\"example shock\"],\n",
    "    base_scenario=\"baseline\",\n",
    "    difference=\"relative\",\n",
    "    preset=\"heatmap\",\n",
    "    auto_open=False,\n",
    "    path=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a30fe3be",
   "metadata": {},
   "source": [
    "You can also compare the raw matrix values side by side across scenarios, for example by faceting on `Scenario`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d4016e4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "hovertemplate": "Sector_to=Agriculture<br>Scenario=baseline<br>Sector_from=%{x}<br>Value=%{y}<extra></extra>",
         "legendgroup": "Agriculture",
         "marker": {
          "color": "#3F8EFC",
          "pattern": {
           "shape": ""
          }
         },
         "name": "Agriculture",
         "orientation": "v",
         "showlegend": true,
         "textposition": "auto",
         "type": "bar",
         "x": [
          "Agriculture",
          "Industry",
          "Services"
         ],
         "xaxis": "x",
         "y": {
          "bdata": "VKiuK2JoLEFZYNSBXCAxQdES5sjR5DxB",
          "dtype": "f8"
         },
         "yaxis": "y"
        },
        {
         "hovertemplate": "Sector_to=Agriculture<br>Scenario=example shock<br>Sector_from=%{x}<br>Value=%{y}<extra></extra>",
         "legendgroup": "Agriculture",
         "marker": {
          "color": "#3F8EFC",
          "pattern": {
           "shape": ""
          }
         },
         "name": "Agriculture",
         "orientation": "v",
         "showlegend": false,
         "textposition": "auto",
         "type": "bar",
         "x": [
          "Agriculture",
          "Industry",
          "Services"
         ],
         "xaxis": "x2",
         "y": {
          "bdata": "yGw6Uy5tLEEZyUfvQCMxQQAyMvSy6TxB",
          "dtype": "f8"
         },
         "yaxis": "y2"
        },
        {
         "hovertemplate": "Sector_to=Industry<br>Scenario=baseline<br>Sector_from=%{x}<br>Value=%{y}<extra></extra>",
         "legendgroup": "Industry",
         "marker": {
          "color": "#BF9FC2",
          "pattern": {
           "shape": ""
          }
         },
         "name": "Industry",
         "orientation": "v",
         "showlegend": true,
         "textposition": "auto",
         "type": "bar",
         "x": [
          "Agriculture",
          "Industry",
          "Services"
         ],
         "xaxis": "x",
         "y": {
          "bdata": "UsTQvzlwVEF1hpWJEtV1QVw8CJqkkmJB",
          "dtype": "f8"
         },
         "yaxis": "y"
        },
        {
         "hovertemplate": "Sector_to=Industry<br>Scenario=example shock<br>Sector_from=%{x}<br>Value=%{y}<extra></extra>",
         "legendgroup": "Industry",
         "marker": {
          "color": "#BF9FC2",
          "pattern": {
           "shape": ""
          }
         },
         "name": "Industry",
         "orientation": "v",
         "showlegend": false,
         "textposition": "auto",
         "type": "bar",
         "x": [
          "Agriculture",
          "Industry",
          "Services"
         ],
         "xaxis": "x2",
         "y": {
          "bdata": "Lm5JCwBzVEEag4xGCdh1QZ29FBAqlWJB",
          "dtype": "f8"
         },
         "yaxis": "y2"
        },
        {
         "hovertemplate": "Sector_to=Services<br>Scenario=baseline<br>Sector_from=%{x}<br>Value=%{y}<extra></extra>",
         "legendgroup": "Services",
         "marker": {
          "color": "#79C2D6",
          "pattern": {
           "shape": ""
          }
         },
         "name": "Services",
         "orientation": "v",
         "showlegend": true,
         "textposition": "auto",
         "type": "bar",
         "x": [
          "Agriculture",
          "Industry",
          "Services"
         ],
         "xaxis": "x",
         "y": {
          "bdata": "r1wGT05oMEERhilmqVBkQR/QoMMtLn1B",
          "dtype": "f8"
         },
         "yaxis": "y"
        },
        {
         "hovertemplate": "Sector_to=Services<br>Scenario=example shock<br>Sector_from=%{x}<br>Value=%{y}<extra></extra>",
         "legendgroup": "Services",
         "marker": {
          "color": "#79C2D6",
          "pattern": {
           "shape": ""
          }
         },
         "name": "Services",
         "orientation": "v",
         "showlegend": false,
         "textposition": "auto",
         "type": "bar",
         "x": [
          "Agriculture",
          "Industry",
          "Services"
         ],
         "xaxis": "x2",
         "y": {
          "bdata": "Y2nLtMtoMEEijV+pRFFkQel3IMgML31B",
          "dtype": "f8"
         },
         "yaxis": "y2"
        }
       ],
       "layout": {
        "annotations": [
         {
          "font": {},
          "showarrow": false,
          "text": "Scenario=baseline",
          "x": 0.245,
          "xanchor": "center",
          "xref": "paper",
          "y": 1,
          "yanchor": "bottom",
          "yref": "paper"
         },
         {
          "font": {},
          "showarrow": false,
          "text": "Scenario=example shock",
          "x": 0.755,
          "xanchor": "center",
          "xref": "paper",
          "y": 1,
          "yanchor": "bottom",
          "yref": "paper"
         }
        ],
        "barmode": "relative",
        "font": {
         "family": "Verdana",
         "size": 15
        },
        "legend": {
         "title": {
          "text": "Sector_to"
         },
         "tracegroupgap": 0
        },
        "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": "Intersectoral transaction flows"
        },
        "xaxis": {
         "anchor": "y",
         "domain": [
          0,
          0.49
         ],
         "title": {
          "text": "Sector_from"
         }
        },
        "xaxis2": {
         "anchor": "y2",
         "domain": [
          0.51,
          1
         ],
         "matches": "x",
         "title": {
          "text": "Sector_from"
         }
        },
        "yaxis": {
         "anchor": "x",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "Value"
         }
        },
        "yaxis2": {
         "anchor": "x2",
         "domain": [
          0,
          1
         ],
         "matches": "y",
         "showticklabels": false
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "db_IOT.plot(\n",
    "    matrix=\"Z\",\n",
    "    scenarios=[\"baseline\", \"example shock\"],\n",
    "    preset=None,\n",
    "    kind=\"bar\",\n",
    "    x=\"Sector_from\",\n",
    "    color=\"Sector_to\",\n",
    "    facet_col=\"Scenario\",\n",
    "    filters={\"Region_from\": [\"Reg1\"], \"Region_to\": [\"Reg1\"]},\n",
    "    top_n=6,\n",
    "    auto_open=False,\n",
    "    path=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e68ecd0",
   "metadata": {},
   "source": [
    "___\n",
    "### SUT workflow\n",
    "\n",
    "Load the test SUT table embedded into MARIO "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "cfe00922",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Parser: excel reading SUT flows from /Users/lorenzorinaldi/Documents/GitHub/MARIO/mario/test/tables/test_SUT_standard.xlsx.\n",
      "INFO Parser: state payload ready with 10 canonical blocks.\n",
      "INFO Parser: excel state ready for SUT.\n",
      "INFO Metadata: initialized.\n"
     ]
    }
   ],
   "source": [
    "import mario\n",
    "db_SUT = mario.load_test(\"SUT\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1770cfa3",
   "metadata": {},
   "source": [
    "#### Generate an empty shock template"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49a990e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "db_SUT.get_shock_excel(\n",
    "    path=\"/path/to/shock_SUT_template.xlsx\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb4f35ff",
   "metadata": {},
   "source": [
    "#### Fill shock template\n",
    "\n",
    "To simulate a similar shock with respect to the IOT case, fill the shock template to simulate a change in consumption of \"Goods\" outputs from both \"Region 1\" and \"Region 2\" by \"Services\" activity in \"Region 2\", which would cause also an increase in CO2 emissions of such sector. \n",
    "\n",
    "These changes affect now the `u` (instead of `z`) and `ea` (instead of `e`) matrices.\n",
    "\n",
    "Again, only \"Percentage\" change type are adopted.\n",
    "\n",
    "![shock_sut_template](../../_static/images/shock_SUT_template.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c3fb30b",
   "metadata": {},
   "source": [
    "#### Apply a filled shock workbook\n",
    "\n",
    "For SUTs, the matrices that can be shocked are:\n",
    "\n",
    "* ``u`` and ``s`` instead of ``z``\n",
    "* ``va`` and ``vc`` instead of ``v``\n",
    "* ``ea`` and ``ec`` instead of ``e``\n",
    "* ``Ya`` and ``Yc`` instead of ``Y``\n",
    "\n",
    "In our example, the filled workbook updates one `u` and `ea` coefficient matrices. \n",
    "\n",
    "N.B. The template workbook will only generate non-fully empty matrices (e.g. in this example ``ec`` is fully empty)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32b024eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolving Y for baseline.\n",
      "INFO Resolver: trying Y via concat.\n",
      "INFO Resolver: resolved Y via concat.\n",
      "INFO Resolver: resolving Xa for baseline.\n",
      "INFO Resolver: trying Xa via formula build_sut_Xa_from_S_Ya.\n",
      "INFO Resolver: resolved Xa via formula build_sut_Xa_from_S_Ya.\n",
      "INFO Resolver: resolving Xc for baseline.\n",
      "INFO Resolver: trying Xc via formula build_sut_Xc_from_U_Yc.\n",
      "INFO Resolver: resolved Xc via formula build_sut_Xc_from_U_Yc.\n",
      "INFO Resolver: resolving v for baseline.\n",
      "INFO Resolver: trying v via concat.\n",
      "INFO Resolver: resolved v via concat.\n",
      "INFO Shock: Shock implemented successfully.\n"
     ]
    }
   ],
   "source": [
    "db_SUT.shock_calc(\n",
    "    io=\"/path/to/shock_SUT_filled.xlsx\",\n",
    "    z=True,\n",
    "    e=True,\n",
    "    scenario=\"example shock\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6c31182c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['baseline', 'example shock']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db_SUT.scenarios"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16b4123d",
   "metadata": {},
   "source": [
    "#### Inspect the shocked coefficient matrix\n",
    "\n",
    "Use again the `query` method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "32728bf6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolving u for example shock.\n",
      "INFO Resolver: trying u via extract.\n",
      "INFO Resolver: resolved u via extract.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Region</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Region 1</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Region 2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Level</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Activity</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Activity</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Item</th>\n",
       "      <th>Manufacturing</th>\n",
       "      <th>Services</th>\n",
       "      <th>Manufacturing</th>\n",
       "      <th>Services</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Region</th>\n",
       "      <th>Level</th>\n",
       "      <th>Item</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Region 1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">Commodity</th>\n",
       "      <th>Goods</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Region 2</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">Commodity</th>\n",
       "      <th>Goods</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Region                           Region 1               Region 2         \n",
       "Level                            Activity               Activity         \n",
       "Item                        Manufacturing Services Manufacturing Services\n",
       "Region   Level     Item                                                  \n",
       "Region 1 Commodity Goods              0.0      0.0           0.0      0.2\n",
       "                   Services           0.0      0.0           0.0      0.0\n",
       "Region 2 Commodity Goods              0.0      0.0           0.0      0.3\n",
       "                   Services           0.0      0.0           0.0      0.0"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db_SUT.query(\n",
    "    matrices=\"u\", \n",
    "    scenarios=\"example shock\", \n",
    "    base_scenario=\"baseline\", \n",
    "    type=\"relative\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c221c359",
   "metadata": {},
   "source": [
    "Again, keep `query(...)` as the standard workflow when you need the exact numbers. `plot(...)` is useful as a complementary step to identify patterns and hotspots before drilling back into the table.\n",
    "\n",
    "This example visualizes the relative change in the shocked `u` matrix compared with the baseline."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d571ef16",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolving U for example shock.\n",
      "INFO Resolver: trying U via extract.\n",
      "INFO Resolver: resolved U via extract.\n"
     ]
    },
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "coloraxis": "coloraxis",
         "histfunc": "sum",
         "hovertemplate": "Item_to=%{y}<br>sum of Value=%{z}<extra></extra>",
         "name": "",
         "type": "histogram2d",
         "x": [
          "Manufacturing",
          "Services"
         ],
         "xaxis": "x",
         "xbingroup": "x",
         "y": [
          "Manufacturing",
          "Services"
         ],
         "yaxis": "y",
         "ybingroup": "y",
         "z": {
          "bdata": "rMLmmsnD4j9IFTRk28zjPw==",
          "dtype": "f8"
         }
        }
       ],
       "layout": {
        "coloraxis": {
         "colorbar": {
          "title": {
           "text": "sum of Value"
          }
         },
         "colorscale": [
          [
           0,
           "rgb(5,48,97)"
          ],
          [
           0.1,
           "rgb(33,102,172)"
          ],
          [
           0.2,
           "rgb(67,147,195)"
          ],
          [
           0.3,
           "rgb(146,197,222)"
          ],
          [
           0.4,
           "rgb(209,229,240)"
          ],
          [
           0.5,
           "rgb(247,247,247)"
          ],
          [
           0.6,
           "rgb(253,219,199)"
          ],
          [
           0.7,
           "rgb(244,165,130)"
          ],
          [
           0.8,
           "rgb(214,96,77)"
          ],
          [
           0.9,
           "rgb(178,24,43)"
          ],
          [
           1,
           "rgb(103,0,31)"
          ]
         ]
        },
        "font": {
         "family": "Verdana",
         "size": 15
        },
        "legend": {
         "tracegroupgap": 0
        },
        "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": "Use transaction flows vs baseline (relative)"
        },
        "xaxis": {
         "anchor": "y",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "Item_to"
         }
        },
        "yaxis": {
         "anchor": "x",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "Item_to"
         }
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "db_SUT.plot(\n",
    "    matrix=\"U\",\n",
    "    scenarios=[\"example shock\"],\n",
    "    base_scenario=\"baseline\",\n",
    "    difference=\"relative\",\n",
    "    preset=\"heatmap\",\n",
    "    auto_open=False,\n",
    "    path=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a644537c",
   "metadata": {},
   "source": [
    "If you prefer to inspect absolute values instead of differences, plot both scenarios together and facet the result by `Scenario`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3d0c0dd5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "plotlyServerURL": "https://plot.ly"
       },
       "data": [
        {
         "hovertemplate": "Activity_to=Manufacturing<br>Scenario=baseline<br>Commodity_from=%{x}<br>Value=%{y}<extra></extra>",
         "legendgroup": "Manufacturing",
         "marker": {
          "color": "#3F8EFC",
          "pattern": {
           "shape": ""
          }
         },
         "name": "Manufacturing",
         "orientation": "v",
         "showlegend": true,
         "textposition": "auto",
         "type": "bar",
         "x": [
          "Goods",
          "Services"
         ],
         "xaxis": "x",
         "y": {
          "bdata": "eNMgYmRpU0K8hYn+efxSQg==",
          "dtype": "f8"
         },
         "yaxis": "y"
        },
        {
         "hovertemplate": "Activity_to=Manufacturing<br>Scenario=example shock<br>Commodity_from=%{x}<br>Value=%{y}<extra></extra>",
         "legendgroup": "Manufacturing",
         "marker": {
          "color": "#3F8EFC",
          "pattern": {
           "shape": ""
          }
         },
         "name": "Manufacturing",
         "orientation": "v",
         "showlegend": false,
         "textposition": "auto",
         "type": "bar",
         "x": [
          "Goods",
          "Services"
         ],
         "xaxis": "x2",
         "y": {
          "bdata": "HnvxOYwPVEKpcC+R/Z5TQg==",
          "dtype": "f8"
         },
         "yaxis": "y2"
        },
        {
         "hovertemplate": "Activity_to=Services<br>Scenario=baseline<br>Commodity_from=%{x}<br>Value=%{y}<extra></extra>",
         "legendgroup": "Services",
         "marker": {
          "color": "#BF9FC2",
          "pattern": {
           "shape": ""
          }
         },
         "name": "Services",
         "orientation": "v",
         "showlegend": true,
         "textposition": "auto",
         "type": "bar",
         "x": [
          "Goods",
          "Services"
         ],
         "xaxis": "x",
         "y": {
          "bdata": "HFssD39MQ0IOqiCqqhxlQg==",
          "dtype": "f8"
         },
         "yaxis": "y"
        },
        {
         "hovertemplate": "Activity_to=Services<br>Scenario=example shock<br>Commodity_from=%{x}<br>Value=%{y}<extra></extra>",
         "legendgroup": "Services",
         "marker": {
          "color": "#BF9FC2",
          "pattern": {
           "shape": ""
          }
         },
         "name": "Services",
         "orientation": "v",
         "showlegend": false,
         "textposition": "auto",
         "type": "bar",
         "x": [
          "Goods",
          "Services"
         ],
         "xaxis": "x2",
         "y": {
          "bdata": "+2T+hF14Q0JkzDhAqExlQg==",
          "dtype": "f8"
         },
         "yaxis": "y2"
        }
       ],
       "layout": {
        "annotations": [
         {
          "font": {},
          "showarrow": false,
          "text": "Scenario=baseline",
          "x": 0.245,
          "xanchor": "center",
          "xref": "paper",
          "y": 1,
          "yanchor": "bottom",
          "yref": "paper"
         },
         {
          "font": {},
          "showarrow": false,
          "text": "Scenario=example shock",
          "x": 0.755,
          "xanchor": "center",
          "xref": "paper",
          "y": 1,
          "yanchor": "bottom",
          "yref": "paper"
         }
        ],
        "barmode": "relative",
        "font": {
         "family": "Verdana",
         "size": 15
        },
        "legend": {
         "title": {
          "text": "Activity_to"
         },
         "tracegroupgap": 0
        },
        "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": "Use transaction flows"
        },
        "xaxis": {
         "anchor": "y",
         "domain": [
          0,
          0.49
         ],
         "title": {
          "text": "Commodity_from"
         }
        },
        "xaxis2": {
         "anchor": "y2",
         "domain": [
          0.51,
          1
         ],
         "matches": "x",
         "title": {
          "text": "Commodity_from"
         }
        },
        "yaxis": {
         "anchor": "x",
         "domain": [
          0,
          1
         ],
         "title": {
          "text": "Value"
         }
        },
        "yaxis2": {
         "anchor": "x2",
         "domain": [
          0,
          1
         ],
         "matches": "y",
         "showticklabels": false
        }
       }
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "db_SUT.plot(\n",
    "    matrix=\"U\",\n",
    "    scenarios=[\"baseline\", \"example shock\"],\n",
    "    preset=None,\n",
    "    kind=\"bar\",\n",
    "    x=\"Commodity_from\",\n",
    "    color=\"Activity_to\",\n",
    "    facet_col=\"Scenario\",\n",
    "    filters={\"Region_from\": [\"Region 1\"], \"Region_to\": [\"Region 1\"]},\n",
    "    top_n=6,\n",
    "    auto_open=False,\n",
    "    path=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Programmatic shocks\n",
    "\n",
    "The Excel workflow above is the standard way to prepare shocks, but the same scenario logic can also be used directly from Python when the shock is already scripted.\n",
    "\n",
    "The key methods are:\n",
    "\n",
    "* `clone_scenario(...)` to create a target scenario;\n",
    "* `reset_to_flows(...)` or `reset_to_coefficients(...)` to collapse that scenario to one consistent storage mode;\n",
    "* `update_scenarios(...)` to replace the selected matrices.\n",
    "\n",
    "### Why the reset matters\n",
    "\n",
    "`update_scenarios(...)` is a low-level storage operation. It only overwrites the matrices you pass. It does not create a new scenario, does not recompute dependent matrices, and does not clear already materialized derived matrices such as `X`, `V` or `E`.\n",
    "\n",
    "For that reason, manual edits should normally start from either `reset_to_flows(...)` or `reset_to_coefficients(...)`.\n",
    "\n",
    "* Use flow mode when you want to store flows explicitly.\n",
    "* Use coefficient mode when you want MARIO to propagate the shock through `X` and then rebuild dependent flows on demand.\n",
    "\n",
    "**Important.** Dotted access such as `db.Z` or `db.X` always refers to the baseline scenario. For non-baseline scenarios, use `query(..., scenarios=\"policy\")` or `get_block(..., scenario=\"policy\")`.\n",
    "\n",
    "Also make an explicit copy before editing: `Z = db.query(\"Z\", scenarios=\"policy\").copy()`. Without `.copy()`, in-place operations such as `*=` mutate the stored scenario matrix immediately.\n",
    "\n",
    "### Flow-side direct updates\n",
    "\n",
    "```python\n",
    "db.clone_scenario(\"baseline\", \"manual flow shock\")\n",
    "db.reset_to_flows(\"manual flow shock\")\n",
    "\n",
    "Z = db.query(\"Z\", scenarios=\"manual flow shock\").copy()\n",
    "Z.loc[:, :] *= 1.10\n",
    "\n",
    "db.update_scenarios(\"manual flow shock\", Z=Z)\n",
    "\n",
    "X = db.query(\"X\", scenarios=\"manual flow shock\")\n",
    "```\n",
    "\n",
    "In this setup `X` is rebuilt from the updated `Z` and stored `Y`. However, quantities based on other stored flow blocks keep their current values until you update those blocks too. For example, `GDP()` uses `V`, so replacing only `Z` in flow mode does not change GDP.\n",
    "\n",
    "### Coefficient-side direct updates\n",
    "\n",
    "```python\n",
    "db.clone_scenario(\"baseline\", \"manual coefficient shock\")\n",
    "db.reset_to_coefficients(\"manual coefficient shock\")\n",
    "\n",
    "z = db.query(\"z\", scenarios=\"manual coefficient shock\").copy()\n",
    "z.loc[:, :] *= 0.95\n",
    "\n",
    "db.update_scenarios(\"manual coefficient shock\", z=z)\n",
    "\n",
    "X = db.query(\"X\", scenarios=\"manual coefficient shock\")\n",
    "V = db.query(\"V\", scenarios=\"manual coefficient shock\")\n",
    "GDP = db.GDP(scenario=\"manual coefficient shock\")\n",
    "```\n",
    "\n",
    "\n",
    "This is usually the cleaner manual workflow when the shock is structural and should propagate through the model.\n"
   ],
   "id": "programmatic-shocks"
  },
  {
   "cell_type": "markdown",
   "id": "70ebd5b7",
   "metadata": {},
   "source": [
    "___\n",
    "## Clusters\n",
    "\n",
    "Shock templates and shock application both understand clusters.\n",
    "\n",
    "You can:\n",
    "\n",
    "* rely on default clusters already available on the database (e.g. \"all\" for *Regions*)\n",
    "* store reusable custom clusters on the database\n",
    "* pass ad hoc clusters directly to ``get_shock_excel(...)`` or ``shock_calc(...)``\n",
    "\n",
    "This is useful when a single workbook row should target multiple regions,\n",
    "sectors, activities, commodities, or final-demand categories at once.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "5dc00446",
   "metadata": {},
   "outputs": [],
   "source": [
    "clusters = {\n",
    "    'Sector': {\n",
    "        'Agr_Serv': ['Agriculture', 'Services']\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd206e27",
   "metadata": {},
   "outputs": [],
   "source": [
    "db_IOT.get_shock_excel(\n",
    "    path=\"/path/to/shock_IOT_template_clusters.xlsx\",\n",
    "    clusters=clusters\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dffdb748",
   "metadata": {},
   "source": [
    "When passing the ``clusters`` argument, you will see the clusters are allowed to be selected.  \n",
    "\n",
    "![shock_iot_clusters](../../_static/images/shock_IOT_clusters.png)\n",
    "\n",
    "Then with the ``shock_calc`` method, you can parse the template back."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "254562b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Shock: Shock implemented successfully.\n"
     ]
    }
   ],
   "source": [
    "db_IOT.shock_calc(\n",
    "    io=\"/path/to/shock_IOT_filled_clusters.xlsx\",\n",
    "    z=True,\n",
    "    e=True,\n",
    "    scenario=\"example shock clusters\",\n",
    "    clusters=clusters\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "487f5ddf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Region</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Reg1</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Reg2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Level</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Sector</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Sector</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Item</th>\n",
       "      <th>Agriculture</th>\n",
       "      <th>Industry</th>\n",
       "      <th>Services</th>\n",
       "      <th>Agriculture</th>\n",
       "      <th>Industry</th>\n",
       "      <th>Services</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Region</th>\n",
       "      <th>Level</th>\n",
       "      <th>Item</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Reg1</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">Sector</th>\n",
       "      <th>Agriculture</th>\n",
       "      <td>0.000179</td>\n",
       "      <td>0.000087</td>\n",
       "      <td>0.000077</td>\n",
       "      <td>0.009694</td>\n",
       "      <td>0.005164</td>\n",
       "      <td>0.074003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>0.000179</td>\n",
       "      <td>0.000087</td>\n",
       "      <td>0.000077</td>\n",
       "      <td>0.009694</td>\n",
       "      <td>0.005164</td>\n",
       "      <td>0.022860</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>0.000179</td>\n",
       "      <td>0.000087</td>\n",
       "      <td>0.000077</td>\n",
       "      <td>0.009694</td>\n",
       "      <td>0.005164</td>\n",
       "      <td>0.074003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Reg2</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">Sector</th>\n",
       "      <th>Agriculture</th>\n",
       "      <td>0.000179</td>\n",
       "      <td>0.000087</td>\n",
       "      <td>0.000077</td>\n",
       "      <td>0.009694</td>\n",
       "      <td>0.005164</td>\n",
       "      <td>0.074003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>0.000179</td>\n",
       "      <td>0.000087</td>\n",
       "      <td>0.000077</td>\n",
       "      <td>0.009694</td>\n",
       "      <td>0.005164</td>\n",
       "      <td>0.022860</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>0.000179</td>\n",
       "      <td>0.000087</td>\n",
       "      <td>0.000077</td>\n",
       "      <td>0.009694</td>\n",
       "      <td>0.005164</td>\n",
       "      <td>0.074003</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Region                           Reg1                            Reg2  \\\n",
       "Level                          Sector                          Sector   \n",
       "Item                      Agriculture  Industry  Services Agriculture   \n",
       "Region Level  Item                                                      \n",
       "Reg1   Sector Agriculture    0.000179  0.000087  0.000077    0.009694   \n",
       "              Industry       0.000179  0.000087  0.000077    0.009694   \n",
       "              Services       0.000179  0.000087  0.000077    0.009694   \n",
       "Reg2   Sector Agriculture    0.000179  0.000087  0.000077    0.009694   \n",
       "              Industry       0.000179  0.000087  0.000077    0.009694   \n",
       "              Services       0.000179  0.000087  0.000077    0.009694   \n",
       "\n",
       "Region                                         \n",
       "Level                                          \n",
       "Item                       Industry  Services  \n",
       "Region Level  Item                             \n",
       "Reg1   Sector Agriculture  0.005164  0.074003  \n",
       "              Industry     0.005164  0.022860  \n",
       "              Services     0.005164  0.074003  \n",
       "Reg2   Sector Agriculture  0.005164  0.074003  \n",
       "              Industry     0.005164  0.022860  \n",
       "              Services     0.005164  0.074003  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db_IOT.query(\n",
    "    matrices=\"Z\", \n",
    "    scenarios=\"example shock clusters\", \n",
    "    base_scenario=\"example shock\", \n",
    "    type=\"relative\"\n",
    ")"
   ]
  }
 ],
 "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
}
