{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "15ce0d92",
   "metadata": {},
   "source": [
    "# Basic inspections\n",
    "\n",
    "This notebook starts from the packaged MARIO IOT test table and then repeats the same inspection pattern on the packaged SUT fixture."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8a2e1caf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:50:59.285177Z",
     "iopub.status.busy": "2026-05-01T06:50:59.284818Z",
     "iopub.status.idle": "2026-05-01T06:51:01.636335Z",
     "shell.execute_reply": "2026-05-01T06:51:01.635924Z"
    }
   },
   "outputs": [],
   "source": [
    "import mario"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38e7caad",
   "metadata": {},
   "source": [
    "## Load the test table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aca7505c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:01.638686Z",
     "iopub.status.busy": "2026-05-01T06:51:01.638402Z",
     "iopub.status.idle": "2026-05-01T06:51:01.994285Z",
     "shell.execute_reply": "2026-05-01T06:51:01.993957Z"
    }
   },
   "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"
     ]
    },
    {
     "data": {
      "text/plain": [
       "name = IOT test (standard)\n",
       "table = IOT\n",
       "scenarios = ['baseline']\n",
       "Factor of production = 3\n",
       "Satellite account = 2\n",
       "Consumption category = 1\n",
       "Region = 2\n",
       "Sector = 3"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db = mario.load_test(\"IOT\")\n",
    "db"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b83d559e",
   "metadata": {},
   "source": [
    "## Check scenarios and sets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5d06b268",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:01.996268Z",
     "iopub.status.busy": "2026-05-01T06:51:01.996039Z",
     "iopub.status.idle": "2026-05-01T06:51:01.998338Z",
     "shell.execute_reply": "2026-05-01T06:51:01.998114Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['baseline']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.scenarios"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "96a5e125",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:01.999786Z",
     "iopub.status.busy": "2026-05-01T06:51:01.999679Z",
     "iopub.status.idle": "2026-05-01T06:51:02.001533Z",
     "shell.execute_reply": "2026-05-01T06:51:02.001297Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Factor of production',\n",
       " 'Satellite account',\n",
       " 'Consumption category',\n",
       " 'Region',\n",
       " 'Sector']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.sets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3b5f7242",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.002966Z",
     "iopub.status.busy": "2026-05-01T06:51:02.002857Z",
     "iopub.status.idle": "2026-05-01T06:51:02.004973Z",
     "shell.execute_reply": "2026-05-01T06:51:02.004761Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Factor of production': ['Taxes', 'Wages', 'Capital'],\n",
       " 'Satellite account': ['Employment', 'CO2'],\n",
       " 'Consumption category': ['Final demand'],\n",
       " 'Region': ['Reg1', 'Reg2'],\n",
       " 'Sector': ['Agriculture', 'Services', 'Industry']}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.get_index(\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bd04e124",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.006304Z",
     "iopub.status.busy": "2026-05-01T06:51:02.006198Z",
     "iopub.status.idle": "2026-05-01T06:51:02.008118Z",
     "shell.execute_reply": "2026-05-01T06:51:02.007895Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Agriculture', 'Services', 'Industry']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.Sector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "212c7f0a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.009449Z",
     "iopub.status.busy": "2026-05-01T06:51:02.009350Z",
     "iopub.status.idle": "2026-05-01T06:51:02.023925Z",
     "shell.execute_reply": "2026-05-01T06:51:02.023702Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Agriculture', 'Services', 'Industry']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.sectors # sets name are now not case-sensitive or singular-plural sensitive"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fb6010b",
   "metadata": {},
   "source": [
    "## Search labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b3f4a696",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.025513Z",
     "iopub.status.busy": "2026-05-01T06:51:02.025426Z",
     "iopub.status.idle": "2026-05-01T06:51:02.027609Z",
     "shell.execute_reply": "2026-05-01T06:51:02.027364Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Sector': ['Agriculture']}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.search(\"Agr\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64cfa903",
   "metadata": {},
   "source": [
    "## Check stored matrices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "264dda95",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.029097Z",
     "iopub.status.busy": "2026-05-01T06:51:02.029018Z",
     "iopub.status.idle": "2026-05-01T06:51:02.031061Z",
     "shell.execute_reply": "2026-05-01T06:51:02.030852Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['E', 'EY', 'V', 'VY', 'Y', 'Z'])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.list_matrices(scenario='baseline')  # change baseline to the name of the scenario you want to inspect"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a840a951",
   "metadata": {},
   "source": [
    "## Query for a matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8e160055",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.032526Z",
     "iopub.status.busy": "2026-05-01T06:51:02.032425Z",
     "iopub.status.idle": "2026-05-01T06:51:02.063662Z",
     "shell.execute_reply": "2026-05-01T06:51:02.063430Z"
    }
   },
   "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>9.308651e+05</td>\n",
       "      <td>5.357799e+06</td>\n",
       "      <td>1.075278e+06</td>\n",
       "      <td>2363.029548</td>\n",
       "      <td>50607.806861</td>\n",
       "      <td>10341.916110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>1.122397e+06</td>\n",
       "      <td>2.289284e+07</td>\n",
       "      <td>1.065096e+07</td>\n",
       "      <td>1413.697219</td>\n",
       "      <td>134910.657543</td>\n",
       "      <td>30367.209709</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>1.893586e+06</td>\n",
       "      <td>9.737509e+06</td>\n",
       "      <td>3.059785e+07</td>\n",
       "      <td>1301.725200</td>\n",
       "      <td>27813.857216</td>\n",
       "      <td>58525.384254</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>4.168587e+02</td>\n",
       "      <td>2.596041e+03</td>\n",
       "      <td>8.430071e+02</td>\n",
       "      <td>2962.557738</td>\n",
       "      <td>30635.045696</td>\n",
       "      <td>7961.643900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>6.890987e+03</td>\n",
       "      <td>1.335193e+05</td>\n",
       "      <td>6.555829e+04</td>\n",
       "      <td>9736.931972</td>\n",
       "      <td>287300.673355</td>\n",
       "      <td>161126.940614</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>1.590090e+03</td>\n",
       "      <td>1.555045e+04</td>\n",
       "      <td>3.843707e+04</td>\n",
       "      <td>16680.335207</td>\n",
       "      <td>312355.775242</td>\n",
       "      <td>722089.042972</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Region                             Reg1                              \\\n",
       "Level                            Sector                               \n",
       "Item                        Agriculture      Industry      Services   \n",
       "Region Level  Item                                                    \n",
       "Reg1   Sector Agriculture  9.308651e+05  5.357799e+06  1.075278e+06   \n",
       "              Industry     1.122397e+06  2.289284e+07  1.065096e+07   \n",
       "              Services     1.893586e+06  9.737509e+06  3.059785e+07   \n",
       "Reg2   Sector Agriculture  4.168587e+02  2.596041e+03  8.430071e+02   \n",
       "              Industry     6.890987e+03  1.335193e+05  6.555829e+04   \n",
       "              Services     1.590090e+03  1.555045e+04  3.843707e+04   \n",
       "\n",
       "Region                             Reg2                                \n",
       "Level                            Sector                                \n",
       "Item                        Agriculture       Industry       Services  \n",
       "Region Level  Item                                                     \n",
       "Reg1   Sector Agriculture   2363.029548   50607.806861   10341.916110  \n",
       "              Industry      1413.697219  134910.657543   30367.209709  \n",
       "              Services      1301.725200   27813.857216   58525.384254  \n",
       "Reg2   Sector Agriculture   2962.557738   30635.045696    7961.643900  \n",
       "              Industry      9736.931972  287300.673355  161126.940614  \n",
       "              Services     16680.335207  312355.775242  722089.042972  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.Z"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e8566a0",
   "metadata": {},
   "source": [
    "If you want to query for multiple matrices in given scenarios use the \"query\" method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b305fc02",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.065125Z",
     "iopub.status.busy": "2026-05-01T06:51:02.065030Z",
     "iopub.status.idle": "2026-05-01T06:51:02.069773Z",
     "shell.execute_reply": "2026-05-01T06:51:02.069555Z"
    }
   },
   "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>Region</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Reg1</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Reg2</th>\n",
       "    </tr>\n",
       "    <tr>\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>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>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>Employment</th>\n",
       "      <td>1.702588e+06</td>\n",
       "      <td>6.547713e+05</td>\n",
       "      <td>2.050334e+06</td>\n",
       "      <td>7.393224e+02</td>\n",
       "      <td>3.939633e+03</td>\n",
       "      <td>1.852450e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CO2</th>\n",
       "      <td>3.415989e+12</td>\n",
       "      <td>8.884627e+12</td>\n",
       "      <td>1.878737e+13</td>\n",
       "      <td>2.315340e+10</td>\n",
       "      <td>4.839515e+10</td>\n",
       "      <td>1.679321e+11</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",
       "Item                                                                 \n",
       "Employment  1.702588e+06  6.547713e+05  2.050334e+06  7.393224e+02   \n",
       "CO2         3.415989e+12  8.884627e+12  1.878737e+13  2.315340e+10   \n",
       "\n",
       "Region                                  \n",
       "Level                                   \n",
       "Item            Industry      Services  \n",
       "Item                                    \n",
       "Employment  3.939633e+03  1.852450e+04  \n",
       "CO2         4.839515e+10  1.679321e+11  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.query(\n",
    "    matrices=[\"E\"], # provide a matrix of list of matrices  \n",
    "    scenarios=\"baseline\" # provide a scenario or list of scenarios (baseline is default)\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fb9a107",
   "metadata": {},
   "source": [
    "The \"query\" method will also calculate matrices in case they are missing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "67e0e34c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.071377Z",
     "iopub.status.busy": "2026-05-01T06:51:02.071292Z",
     "iopub.status.idle": "2026-05-01T06:51:02.209655Z",
     "shell.execute_reply": "2026-05-01T06:51:02.209323Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "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"
     ]
    },
    {
     "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>Region</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Reg1</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Reg2</th>\n",
       "    </tr>\n",
       "    <tr>\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>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>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>Employment</th>\n",
       "      <td>0.170734</td>\n",
       "      <td>0.012327</td>\n",
       "      <td>0.019621</td>\n",
       "      <td>0.009259</td>\n",
       "      <td>0.003295</td>\n",
       "      <td>0.007659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CO2</th>\n",
       "      <td>342552.270192</td>\n",
       "      <td>167269.943085</td>\n",
       "      <td>179791.790027</td>\n",
       "      <td>289962.466193</td>\n",
       "      <td>40474.898330</td>\n",
       "      <td>69431.794242</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",
       "Item                                                                     \n",
       "Employment       0.170734       0.012327       0.019621       0.009259   \n",
       "CO2         342552.270192  167269.943085  179791.790027  289962.466193   \n",
       "\n",
       "Region                                  \n",
       "Level                                   \n",
       "Item            Industry      Services  \n",
       "Item                                    \n",
       "Employment      0.003295      0.007659  \n",
       "CO2         40474.898330  69431.794242  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.query(\"e\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66675e9b",
   "metadata": {},
   "source": [
    "## Repeat the same checks on a SUT\n",
    "\n",
    "The same inspection methods work on SUT databases, but you will see `Activity` and `Commodity` sets plus split blocks such as `U`, `S`, `Ya`, and `Yc`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "585902d5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.211569Z",
     "iopub.status.busy": "2026-05-01T06:51:02.211428Z",
     "iopub.status.idle": "2026-05-01T06:51:02.519764Z",
     "shell.execute_reply": "2026-05-01T06:51:02.519279Z"
    }
   },
   "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"
     ]
    },
    {
     "data": {
      "text/plain": [
       "name = SUT test (standard)\n",
       "table = SUT\n",
       "tech_assumption = industry-based\n",
       "scenarios = ['baseline']\n",
       "Activity = 2\n",
       "Commodity = 2\n",
       "Factor of production = 3\n",
       "Satellite account = 2\n",
       "Consumption category = 1\n",
       "Region = 2"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sut = mario.load_test(\"SUT\")\n",
    "sut"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "72fea064",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.521735Z",
     "iopub.status.busy": "2026-05-01T06:51:02.521595Z",
     "iopub.status.idle": "2026-05-01T06:51:02.523959Z",
     "shell.execute_reply": "2026-05-01T06:51:02.523719Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Activity',\n",
       " 'Commodity',\n",
       " 'Factor of production',\n",
       " 'Satellite account',\n",
       " 'Consumption category',\n",
       " 'Region']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sut.sets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4d1a6ac2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.525574Z",
     "iopub.status.busy": "2026-05-01T06:51:02.525464Z",
     "iopub.status.idle": "2026-05-01T06:51:02.527557Z",
     "shell.execute_reply": "2026-05-01T06:51:02.527309Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Activity': ['Manufacturing', 'Services'],\n",
       " 'Commodity': ['Goods', 'Services'],\n",
       " 'Factor of production': ['Taxes', 'Wages', 'Capital'],\n",
       " 'Satellite account': ['Employment', 'CO2'],\n",
       " 'Consumption category': ['Final demand'],\n",
       " 'Region': ['Region 1', 'Region 2']}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sut.get_index(\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0104a999",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.529208Z",
     "iopub.status.busy": "2026-05-01T06:51:02.529011Z",
     "iopub.status.idle": "2026-05-01T06:51:02.531463Z",
     "shell.execute_reply": "2026-05-01T06:51:02.531210Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Activity': ['Services'], 'Commodity': ['Services']}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sut.search(\"Serv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9f1eed2",
   "metadata": {},
   "source": [
    "## Check stored SUT matrices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2a7db3e1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.532992Z",
     "iopub.status.busy": "2026-05-01T06:51:02.532887Z",
     "iopub.status.idle": "2026-05-01T06:51:02.534963Z",
     "shell.execute_reply": "2026-05-01T06:51:02.534671Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['EY', 'Ea', 'Ec', 'S', 'U', 'VY', 'Va', 'Vc', 'Ya', 'Yc'])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sut.matrices[\"baseline\"].keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "14f2d373",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.536609Z",
     "iopub.status.busy": "2026-05-01T06:51:02.536503Z",
     "iopub.status.idle": "2026-05-01T06:51:02.566658Z",
     "shell.execute_reply": "2026-05-01T06:51:02.566293Z"
    }
   },
   "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=\"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>3.334903e+11</td>\n",
       "      <td>1.657755e+11</td>\n",
       "      <td>1.487965e+11</td>\n",
       "      <td>6.895332e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>3.261811e+11</td>\n",
       "      <td>7.254021e+11</td>\n",
       "      <td>1.176727e+10</td>\n",
       "      <td>3.588505e+10</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>1.922623e+11</td>\n",
       "      <td>4.099986e+10</td>\n",
       "      <td>3.038628e+13</td>\n",
       "      <td>1.157115e+13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>2.614852e+10</td>\n",
       "      <td>5.823465e+10</td>\n",
       "      <td>1.154871e+13</td>\n",
       "      <td>3.075294e+13</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   \n",
       "Region   Level     Item                                                 \n",
       "Region 1 Commodity Goods     3.334903e+11  1.657755e+11  1.487965e+11   \n",
       "                   Services  3.261811e+11  7.254021e+11  1.176727e+10   \n",
       "Region 2 Commodity Goods     1.922623e+11  4.099986e+10  3.038628e+13   \n",
       "                   Services  2.614852e+10  5.823465e+10  1.154871e+13   \n",
       "\n",
       "Region                                     \n",
       "Level                                      \n",
       "Item                             Services  \n",
       "Region   Level     Item                    \n",
       "Region 1 Commodity Goods     6.895332e+10  \n",
       "                   Services  3.588505e+10  \n",
       "Region 2 Commodity Goods     1.157115e+13  \n",
       "                   Services  3.075294e+13  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sut.U"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01cd42bc",
   "metadata": {},
   "source": [
    "## Query derived SUT matrices\n",
    "\n",
    "`query(...)` behaves the same way on SUT databases and can materialize missing split or unified blocks on demand."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "35d82629",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.568379Z",
     "iopub.status.busy": "2026-05-01T06:51:02.568261Z",
     "iopub.status.idle": "2026-05-01T06:51:02.685432Z",
     "shell.execute_reply": "2026-05-01T06:51:02.685121Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolving u for baseline.\n",
      "INFO Resolver: trying u via extract.\n",
      "INFO Resolver: trying u via formula build_sut_u_from_U_Xa.\n",
      "INFO Resolver: resolved u via formula build_sut_u_from_U_Xa.\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.261452</td>\n",
       "      <td>0.068540</td>\n",
       "      <td>0.002359</td>\n",
       "      <td>0.000660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>0.255721</td>\n",
       "      <td>0.299919</td>\n",
       "      <td>0.000187</td>\n",
       "      <td>0.000343</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.150731</td>\n",
       "      <td>0.016951</td>\n",
       "      <td>0.481652</td>\n",
       "      <td>0.110734</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>0.020500</td>\n",
       "      <td>0.024077</td>\n",
       "      <td>0.183058</td>\n",
       "      <td>0.294300</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.261452  0.068540      0.002359  0.000660\n",
       "                   Services      0.255721  0.299919      0.000187  0.000343\n",
       "Region 2 Commodity Goods         0.150731  0.016951      0.481652  0.110734\n",
       "                   Services      0.020500  0.024077      0.183058  0.294300"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sut.query(\"u\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fe5c32a7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:02.687441Z",
     "iopub.status.busy": "2026-05-01T06:51:02.687298Z",
     "iopub.status.idle": "2026-05-01T06:51:02.793139Z",
     "shell.execute_reply": "2026-05-01T06:51:02.792871Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "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"
     ]
    },
    {
     "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 th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Item</th>\n",
       "      <th>production</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Region</th>\n",
       "      <th>Level</th>\n",
       "      <th>Item</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>1.311430e+12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>2.382765e+12</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>6.302777e+13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>1.045551e+14</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Item                           production\n",
       "Region   Level     Item                  \n",
       "Region 1 Commodity Goods     1.311430e+12\n",
       "                   Services  2.382765e+12\n",
       "Region 2 Commodity Goods     6.302777e+13\n",
       "                   Services  1.045551e+14"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sut.query(\"Xc\")"
   ]
  }
 ],
 "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
}
