{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2ea02860",
   "metadata": {},
   "source": [
    "# Compute matrices\n",
    "\n",
    "This notebook shows how MARIO calculates matrices that are not stored yet in the packaged test tables, first on the IOT fixture and then on the SUT fixture."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a9da775b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:16.234742Z",
     "iopub.status.busy": "2026-05-01T06:51:16.234567Z",
     "iopub.status.idle": "2026-05-01T06:51:18.601490Z",
     "shell.execute_reply": "2026-05-01T06:51:18.601177Z"
    }
   },
   "outputs": [],
   "source": [
    "import mario\n",
    "\n",
    "mario.set_log_verbosity(\"info\")  # you can set the log verbosity as \"debug\", \"info\", \"warning\", \"error\", or \"critical\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "30cbd996",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:18.604031Z",
     "iopub.status.busy": "2026-05-01T06:51:18.603806Z",
     "iopub.status.idle": "2026-05-01T06:51:18.684536Z",
     "shell.execute_reply": "2026-05-01T06:51:18.684237Z"
    }
   },
   "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": [
    "db = mario.load_test(\"IOT\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48900e70",
   "metadata": {},
   "source": [
    "## See what is already available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "71502593",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:18.686147Z",
     "iopub.status.busy": "2026-05-01T06:51:18.686045Z",
     "iopub.status.idle": "2026-05-01T06:51:18.689235Z",
     "shell.execute_reply": "2026-05-01T06:51:18.688982Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['E', 'EY', 'V', 'VY', 'Y', 'Z'])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.matrices[\"baseline\"].keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8429df58",
   "metadata": {},
   "source": [
    "## Calculate selected matrices\n",
    "\n",
    "Use `calc_all(...)` to calculate a list of matrices. MARIO resolves whatever dependencies are needed internally, but by default it materializes only the requested target matrices."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f8172a1a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:18.690777Z",
     "iopub.status.busy": "2026-05-01T06:51:18.690664Z",
     "iopub.status.idle": "2026-05-01T06:51:18.981336Z",
     "shell.execute_reply": "2026-05-01T06:51:18.980874Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "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 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 f for baseline.\n",
      "INFO Resolver: trying f via formula build_iot_f_from_e_w (compute_method=auto, runtime=inverse).\n",
      "INFO Resolver: resolved w via formula build_iot_w_from_z (compute_method=auto, runtime=inverse).\n",
      "INFO Resolver: resolved f via formula build_iot_f_from_e_w (compute_method=auto, runtime=inverse).\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "dict_keys(['E', 'EY', 'V', 'VY', 'Y', 'Z', 'X', 'z', 'f'])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.calc_all([\"X\", \"z\", \"f\"])\n",
    "db.matrices[\"baseline\"].keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bec51176",
   "metadata": {},
   "source": [
    "## Inspect the calculated results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "67b31578",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:18.983684Z",
     "iopub.status.busy": "2026-05-01T06:51:18.983563Z",
     "iopub.status.idle": "2026-05-01T06:51:19.028170Z",
     "shell.execute_reply": "2026-05-01T06:51:19.027675Z"
    }
   },
   "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 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></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=\"3\" valign=\"top\">Reg1</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">Sector</th>\n",
       "      <th>Agriculture</th>\n",
       "      <td>9.972169e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>5.311550e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>1.044952e+08</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>7.984966e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>1.195683e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>2.418662e+06</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             production\n",
       "Region Level  Item                     \n",
       "Reg1   Sector Agriculture  9.972169e+06\n",
       "              Industry     5.311550e+07\n",
       "              Services     1.044952e+08\n",
       "Reg2   Sector Agriculture  7.984966e+04\n",
       "              Industry     1.195683e+06\n",
       "              Services     2.418662e+06"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c69b70a7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:19.030690Z",
     "iopub.status.busy": "2026-05-01T06:51:19.030501Z",
     "iopub.status.idle": "2026-05-01T06:51:19.069688Z",
     "shell.execute_reply": "2026-05-01T06:51:19.068988Z"
    }
   },
   "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.093346</td>\n",
       "      <td>0.100871</td>\n",
       "      <td>0.010290</td>\n",
       "      <td>0.029593</td>\n",
       "      <td>0.042325</td>\n",
       "      <td>0.004276</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>0.112553</td>\n",
       "      <td>0.431001</td>\n",
       "      <td>0.101928</td>\n",
       "      <td>0.017704</td>\n",
       "      <td>0.112831</td>\n",
       "      <td>0.012555</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>0.189887</td>\n",
       "      <td>0.183327</td>\n",
       "      <td>0.292816</td>\n",
       "      <td>0.016302</td>\n",
       "      <td>0.023262</td>\n",
       "      <td>0.024197</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.000042</td>\n",
       "      <td>0.000049</td>\n",
       "      <td>0.000008</td>\n",
       "      <td>0.037102</td>\n",
       "      <td>0.025621</td>\n",
       "      <td>0.003292</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>0.000691</td>\n",
       "      <td>0.002514</td>\n",
       "      <td>0.000627</td>\n",
       "      <td>0.121941</td>\n",
       "      <td>0.240282</td>\n",
       "      <td>0.066618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>0.000159</td>\n",
       "      <td>0.000293</td>\n",
       "      <td>0.000368</td>\n",
       "      <td>0.208897</td>\n",
       "      <td>0.261236</td>\n",
       "      <td>0.298549</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.093346  0.100871  0.010290    0.029593   \n",
       "              Industry       0.112553  0.431001  0.101928    0.017704   \n",
       "              Services       0.189887  0.183327  0.292816    0.016302   \n",
       "Reg2   Sector Agriculture    0.000042  0.000049  0.000008    0.037102   \n",
       "              Industry       0.000691  0.002514  0.000627    0.121941   \n",
       "              Services       0.000159  0.000293  0.000368    0.208897   \n",
       "\n",
       "Region                                         \n",
       "Level                                          \n",
       "Item                       Industry  Services  \n",
       "Region Level  Item                             \n",
       "Reg1   Sector Agriculture  0.042325  0.004276  \n",
       "              Industry     0.112831  0.012555  \n",
       "              Services     0.023262  0.024197  \n",
       "Reg2   Sector Agriculture  0.025621  0.003292  \n",
       "              Industry     0.240282  0.066618  \n",
       "              Services     0.261236  0.298549  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2893c02c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:19.072308Z",
     "iopub.status.busy": "2026-05-01T06:51:19.072059Z",
     "iopub.status.idle": "2026-05-01T06:51:19.121543Z",
     "shell.execute_reply": "2026-05-01T06:51:19.120983Z"
    }
   },
   "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>0.205834</td>\n",
       "      <td>0.071561</td>\n",
       "      <td>0.041096</td>\n",
       "      <td>0.026342</td>\n",
       "      <td>0.034874</td>\n",
       "      <td>0.018308</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CO2</th>\n",
       "      <td>508912.512077</td>\n",
       "      <td>492572.973155</td>\n",
       "      <td>332919.806550</td>\n",
       "      <td>392045.986026</td>\n",
       "      <td>228374.179654</td>\n",
       "      <td>145915.355204</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.205834       0.071561       0.041096       0.026342   \n",
       "CO2         508912.512077  492572.973155  332919.806550  392045.986026   \n",
       "\n",
       "Region                                    \n",
       "Level                                     \n",
       "Item             Industry       Services  \n",
       "Item                                      \n",
       "Employment       0.034874       0.018308  \n",
       "CO2         228374.179654  145915.355204  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.f"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a54c3dc8",
   "metadata": {},
   "source": [
    "## Calculate selected matrices in other scenarios\n",
    "\n",
    "By default, the matrices will calculated in the \"baseline\" scenario"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d7c92532",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:19.124510Z",
     "iopub.status.busy": "2026-05-01T06:51:19.124260Z",
     "iopub.status.idle": "2026-05-01T06:51:19.432148Z",
     "shell.execute_reply": "2026-05-01T06:51:19.431842Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name = IOT test (standard)\n",
       "table = IOT\n",
       "scenarios = ['baseline', 'new_scenario']\n",
       "Factor of production = 3\n",
       "Satellite account = 2\n",
       "Consumption category = 1\n",
       "Region = 2\n",
       "Sector = 3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.clone_scenario(\"baseline\", \"new_scenario\")\n",
    "\n",
    "db"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3042b206",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:19.433811Z",
     "iopub.status.busy": "2026-05-01T06:51:19.433668Z",
     "iopub.status.idle": "2026-05-01T06:51:19.576886Z",
     "shell.execute_reply": "2026-05-01T06:51:19.576552Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolving p for new_scenario.\n",
      "INFO Resolver: trying p via formula build_iot_p_from_v_w (compute_method=auto, runtime=inverse).\n",
      "INFO Resolver: resolved w via formula build_iot_w_from_z (compute_method=auto, runtime=inverse).\n",
      "INFO Resolver: resolved p via formula build_iot_p_from_v_w (compute_method=auto, runtime=inverse).\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></th>\n",
       "      <th>price index</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=\"3\" valign=\"top\">Reg1</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">Sector</th>\n",
       "      <th>Agriculture</th>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>1.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>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           price index\n",
       "Region Level  Item                    \n",
       "Reg1   Sector Agriculture          1.0\n",
       "              Industry             1.0\n",
       "              Services             1.0\n",
       "Reg2   Sector Agriculture          1.0\n",
       "              Industry             1.0\n",
       "              Services             1.0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.query(\"p\",\"new_scenario\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c021e55e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:19.578817Z",
     "iopub.status.busy": "2026-05-01T06:51:19.578702Z",
     "iopub.status.idle": "2026-05-01T06:51:19.581321Z",
     "shell.execute_reply": "2026-05-01T06:51:19.581027Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['E', 'EY', 'V', 'VY', 'Y', 'Z', 'X', 'z', 'f'])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.matrices['baseline'].keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d4cc1da3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:19.582872Z",
     "iopub.status.busy": "2026-05-01T06:51:19.582783Z",
     "iopub.status.idle": "2026-05-01T06:51:19.585066Z",
     "shell.execute_reply": "2026-05-01T06:51:19.584812Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['E', 'EY', 'V', 'VY', 'Y', 'Z', 'X', 'z', 'f', 'p'])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.matrices['new_scenario'].keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e93a522",
   "metadata": {},
   "source": [
    "## Compute selected matrices on a SUT\n",
    "\n",
    "SUT workflows use the same `calc_all(...)` interface, but the resolved matrix names include split and unified activity/commodity blocks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8de4e65e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:19.586777Z",
     "iopub.status.busy": "2026-05-01T06:51:19.586643Z",
     "iopub.status.idle": "2026-05-01T06:51:19.632818Z",
     "shell.execute_reply": "2026-05-01T06:51:19.632542Z"
    }
   },
   "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": [
    "sut = mario.load_test(\"SUT\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "20a70fb3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:19.634384Z",
     "iopub.status.busy": "2026-05-01T06:51:19.634291Z",
     "iopub.status.idle": "2026-05-01T06:51:19.636651Z",
     "shell.execute_reply": "2026-05-01T06:51:19.636374Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['EY', 'Ea', 'Ec', 'S', 'U', 'VY', 'Va', 'Vc', 'Ya', 'Yc'])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sut.matrices[\"baseline\"].keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a1b589bb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:19.638324Z",
     "iopub.status.busy": "2026-05-01T06:51:19.638193Z",
     "iopub.status.idle": "2026-05-01T06:51:20.129079Z",
     "shell.execute_reply": "2026-05-01T06:51:20.128501Z"
    }
   },
   "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",
      "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",
      "INFO Resolver: resolving gac for baseline.\n",
      "INFO Resolver: trying gac via extract.\n",
      "INFO Resolver: trying gac via formula build_sut_gac_from_gaa_bs.\n",
      "INFO Resolver: resolved gac via formula build_sut_gac_from_gaa_bs.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "dict_keys(['EY', 'Ea', 'Ec', 'S', 'U', 'VY', 'Va', 'Vc', 'Ya', 'Yc', 'Xc', 'u', 'gac'])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sut.calc_all([\"Xc\", \"u\", \"gac\"])\n",
    "sut.matrices[\"baseline\"].keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13dd9af8",
   "metadata": {},
   "source": [
    "## Inspect the calculated SUT results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "785a0cfc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:20.132048Z",
     "iopub.status.busy": "2026-05-01T06:51:20.131770Z",
     "iopub.status.idle": "2026-05-01T06:51:20.171737Z",
     "shell.execute_reply": "2026-05-01T06:51:20.171328Z"
    }
   },
   "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 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": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sut.Xc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7dbeb2a9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:20.173644Z",
     "iopub.status.busy": "2026-05-01T06:51:20.173516Z",
     "iopub.status.idle": "2026-05-01T06:51:20.205916Z",
     "shell.execute_reply": "2026-05-01T06:51:20.205421Z"
    }
   },
   "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>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": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sut.u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f7f6fefc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-05-01T06:51:20.207889Z",
     "iopub.status.busy": "2026-05-01T06:51:20.207745Z",
     "iopub.status.idle": "2026-05-01T06:51:20.239433Z",
     "shell.execute_reply": "2026-05-01T06:51:20.239108Z"
    }
   },
   "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\">Commodity</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Commodity</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Item</th>\n",
       "      <th>Goods</th>\n",
       "      <th>Services</th>\n",
       "      <th>Goods</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\">Activity</th>\n",
       "      <th>Manufacturing</th>\n",
       "      <td>1.355147</td>\n",
       "      <td>0.290279</td>\n",
       "      <td>0.337316</td>\n",
       "      <td>0.198263</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>0.313276</td>\n",
       "      <td>1.443459</td>\n",
       "      <td>0.098479</td>\n",
       "      <td>0.080553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Region 2</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">Activity</th>\n",
       "      <th>Manufacturing</th>\n",
       "      <td>0.009015</td>\n",
       "      <td>0.004155</td>\n",
       "      <td>2.015638</td>\n",
       "      <td>0.550454</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>0.002194</td>\n",
       "      <td>0.001910</td>\n",
       "      <td>0.329334</td>\n",
       "      <td>1.495274</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Region                           Region 1            Region 2          \n",
       "Level                           Commodity           Commodity          \n",
       "Item                                Goods  Services     Goods  Services\n",
       "Region   Level    Item                                                 \n",
       "Region 1 Activity Manufacturing  1.355147  0.290279  0.337316  0.198263\n",
       "                  Services       0.313276  1.443459  0.098479  0.080553\n",
       "Region 2 Activity Manufacturing  0.009015  0.004155  2.015638  0.550454\n",
       "                  Services       0.002194  0.001910  0.329334  1.495274"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sut.gac"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f5f4b55",
   "metadata": {},
   "source": [
    "## Control the calculation runtime\n",
    "\n",
    "By default, `calc_all(...)`, `resolve(...)`, `resolve_many(...)`, and dotted access such as `db.f` use the automatic runtime.\n",
    "\n",
    "Conceptually, MARIO can compute many derived targets in two different ways:\n",
    "\n",
    "- `inverse`: build the explicit inverse/intermediate path first, then reuse it downstream.\n",
    "- `solve`: avoid materializing the full inverse and compute the requested target directly through linear solves.\n",
    "- `auto`: the default choice. MARIO decides between the two depending on the requested target, the size of the system, and the estimated memory cost of materializing the inverse.\n",
    "\n",
    "In the public API this behavior is controlled through `compute_options`, rather than through a dedicated `compute_method=` argument. Most users can simply keep the default automatic behavior."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2d587abc",
   "metadata": {},
   "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",
      "INFO Resolver: resolving f for baseline.\n",
      "INFO Resolver: trying f via formula build_iot_f_from_e_w (compute_method=auto, runtime=inverse).\n",
      "INFO Resolver: resolved w via formula build_iot_w_from_z (compute_method=auto, runtime=inverse).\n",
      "INFO Resolver: resolved f via formula build_iot_f_from_e_w (compute_method=auto, runtime=inverse).\n",
      "INFO Resolver: resolving f for baseline.\n",
      "INFO Resolver: trying f via formula build_iot_f_from_e_w (compute_method=inverse, runtime=inverse).\n",
      "INFO Resolver: resolved w via formula build_iot_w_from_z (compute_method=inverse, runtime=inverse).\n",
      "INFO Resolver: resolved f via formula build_iot_f_from_e_w (compute_method=inverse, runtime=inverse).\n",
      "INFO Resolver: resolving f for baseline.\n",
      "INFO Resolver: trying f via formula build_iot_f_from_e_z (compute_method=solve, runtime=solve).\n",
      "INFO Resolver: resolved f via formula build_iot_f_from_e_z (compute_method=solve, runtime=solve).\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.205834</td>\n",
       "      <td>0.071561</td>\n",
       "      <td>0.041096</td>\n",
       "      <td>0.026342</td>\n",
       "      <td>0.034874</td>\n",
       "      <td>0.018308</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CO2</th>\n",
       "      <td>508912.512077</td>\n",
       "      <td>492572.973155</td>\n",
       "      <td>332919.806550</td>\n",
       "      <td>392045.986026</td>\n",
       "      <td>228374.179654</td>\n",
       "      <td>145915.355204</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.205834       0.071561       0.041096       0.026342   \n",
       "CO2         508912.512077  492572.973155  332919.806550  392045.986026   \n",
       "\n",
       "Region                                    \n",
       "Level                                     \n",
       "Item             Industry       Services  \n",
       "Item                                      \n",
       "Employment       0.034874       0.018308  \n",
       "CO2         228374.179654  145915.355204  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iot = mario.load_test(\"IOT\")\n",
    "\n",
    "# automatic choice (default)\n",
    "iot.resolve(\"f\")\n",
    "\n",
    "# prefer the explicit inverse-based path\n",
    "iot.resolve(\n",
    "    \"f\",\n",
    "    force_rewrite=True,\n",
    "    compute_options=mario.ComputeOptions(\n",
    "        backend_override=\"dense_inverse\",\n",
    "    ),\n",
    ")\n",
    "\n",
    "# prefer direct target solves\n",
    "iot.resolve(\n",
    "    \"f\",\n",
    "    force_rewrite=True,\n",
    "    compute_options=mario.ComputeOptions(\n",
    "        planning_override=\"prefer_direct_targets\",\n",
    "    ),\n",
    ")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4b0d991",
   "metadata": {},
   "source": [
    "The same pattern works for SUT targets such as `pc`, `fc`, `Xc`, `wcc`, and the other split activity/commodity blocks. If you need lower-level control of the solve backend, advanced options such as `backend_override=\"sparse_direct\"` and `backend_override=\"sparse_iterative\"` are also available, but in most workflows `auto` is the recommended choice."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9508c35b",
   "metadata": {},
   "source": [
    "## Compare `auto`, `inverse`, and `solve` with large database\n",
    "\n",
    "The example below mirrors the same benchmarking pattern you can use on larger real-world databases. On the small packaged test fixture, absolute timings are not especially meaningful so an example with GLORIA is performed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0d3bc700",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "auto times: [105.73984454094898, 106.24737650004681, 127.93017966602929]\n",
      "auto median: 106.24737650004681\n",
      "inverse-only times: [454.232233250048, 432.189996042056, 405.2793996250257]\n",
      "inverse-only median: 432.189996042056\n",
      "solve-only times: [99.94249587494414, 95.362556417007, 106.08363883302081]\n",
      "solve-only median: 99.94249587494414\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import numpy as np\n",
    "mario.set_log_verbosity(\"critical\")\n",
    "\n",
    "target = \"pc\" # target matrix to resolve (e.g: pc = price index vector by commodity)\n",
    "repeats = 3 # number of times to repeat each pipeline for comparing median timing\n",
    "\n",
    "benchmark_db = mario.parse_gloria(\n",
    "    path = '/Users/lorenzorinaldi/Library/CloudStorage/OneDrive-SharedLibraries-PolitecnicodiMilano/DENG-SESAM - Documenti/c-Research/a-Datasets/_Input Output Databases/GLORIA',\n",
    "    year = 2023,\n",
    "    )\n",
    "\n",
    "auto_options = mario.ComputeOptions()\n",
    "inverse_options = mario.ComputeOptions(backend_override=\"dense_inverse\")\n",
    "solve_options = mario.ComputeOptions(planning_override=\"prefer_direct_targets\")\n",
    "\n",
    "pipelines = [\n",
    "    (\"auto\", auto_options),\n",
    "    (\"inverse-only\", inverse_options),\n",
    "    (\"solve-only\", solve_options),\n",
    "]\n",
    "\n",
    "results = {}\n",
    "\n",
    "for label, options in pipelines:\n",
    "    times = []\n",
    "    resolved = None\n",
    "\n",
    "    for _ in range(repeats):\n",
    "        candidate = benchmark_db.copy()\n",
    "        t0 = time.perf_counter()\n",
    "        resolved = candidate.resolve(target, compute_options=options)\n",
    "        times.append(time.perf_counter() - t0)\n",
    "\n",
    "    results[label] = {\n",
    "        \"times\": times,\n",
    "        \"median\": float(np.median(times)),\n",
    "        \"value\": resolved,\n",
    "    }\n",
    "\n",
    "    print(label, \"times:\", times)\n",
    "    print(label, \"median:\", results[label][\"median\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "78dcc7cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "relative error auto vs inverse: 1.0673892003248946e-08\n",
      "relative error solve vs inverse: 1.0673892003248946e-08\n",
      "inverse/auto speed ratio: 4.067770991426462\n",
      "inverse/solve speed ratio: 4.324386661134078\n"
     ]
    }
   ],
   "source": [
    "inverse_values = results[\"inverse-only\"][\"value\"].to_numpy(dtype=float)\n",
    "auto_values = results[\"auto\"][\"value\"].to_numpy(dtype=float)\n",
    "solve_values = results[\"solve-only\"][\"value\"].to_numpy(dtype=float)\n",
    "\n",
    "auto_relative_error = np.linalg.norm(inverse_values - auto_values) / np.linalg.norm(inverse_values)\n",
    "solve_relative_error = np.linalg.norm(inverse_values - solve_values) / np.linalg.norm(inverse_values)\n",
    "\n",
    "print(\"relative error auto vs inverse:\", auto_relative_error)\n",
    "print(\"relative error solve vs inverse:\", solve_relative_error)\n",
    "print(\"inverse/auto speed ratio:\", results[\"inverse-only\"][\"median\"] / results[\"auto\"][\"median\"])\n",
    "print(\"inverse/solve speed ratio:\", results[\"inverse-only\"][\"median\"] / results[\"solve-only\"][\"median\"])\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
}
