{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4ebc0d16",
   "metadata": {},
   "source": [
    "# MRIO to SRIO\n",
    "\n",
    "This notebook transforms a Multi-Regional table into a Single-Regional table (MRIO-to-SRIO), working with a test IOT.\n",
    "The logic behind this transformation is represented in the figure below: after selecting the reference region of the SRIO (Reg1 in this case), MARIO will aggregate all imports of such region into a new factor of production and all the exports in a new consumption category.\n",
    "\n",
    "![shock_template](../../_static/images/MRIO_to_SRIO.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "840064ce",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:41:04.825427Z",
     "iopub.status.busy": "2026-04-26T22:41:04.825154Z",
     "iopub.status.idle": "2026-04-26T22:41:07.094661Z",
     "shell.execute_reply": "2026-04-26T22:41:07.094305Z"
    }
   },
   "outputs": [],
   "source": [
    "import mario"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a1968f17",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:41:07.097687Z",
     "iopub.status.busy": "2026-04-26T22:41:07.097397Z",
     "iopub.status.idle": "2026-04-26T22:41:07.204840Z",
     "shell.execute_reply": "2026-04-26T22:41:07.204510Z"
    }
   },
   "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": "f9551a60",
   "metadata": {},
   "source": [
    "## Inspect the starting regions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3436d8dd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:41:07.206968Z",
     "iopub.status.busy": "2026-04-26T22:41:07.206847Z",
     "iopub.status.idle": "2026-04-26T22:41:07.210013Z",
     "shell.execute_reply": "2026-04-26T22:41:07.209745Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Reg1', 'Reg2']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.regions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7101b61f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Taxes', 'Wages', 'Capital']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.factors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2bb1cdd8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Final demand']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.consumption_categories"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48913429",
   "metadata": {},
   "source": [
    "## Keep only `Reg1`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4be0a4c1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:41:07.211688Z",
     "iopub.status.busy": "2026-04-26T22:41:07.211610Z",
     "iopub.status.idle": "2026-04-26T22:41:07.319012Z",
     "shell.execute_reply": "2026-04-26T22:41:07.318743Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING All the scenarios will be deleted to build up the new baseline.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolving X for baseline.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: trying X via formula build_iot_X_from_Z_Y.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Resolver: resolved X via formula build_iot_X_from_Z_Y.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Transformation: baseline deleted from the database.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Transformation: New baseline added to the database\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['Reg1']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "single_region_db = db.to_single_region(\"Reg1\", inplace=False)\n",
    "single_region_db.regions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "733a4048",
   "metadata": {},
   "source": [
    "## Keep more than one region\n",
    "\n",
    "If you need to keep more than one region explicit, use `to_region_subset(...)` instead of `to_single_region(...)`.\n",
    "The selected regions remain endogenous with each other, while all the other regions are moved to exogenous imports and exports.\n",
    "\n",
    "For example, to keep `Reg1` and `Reg2` explicit:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15860782",
   "metadata": {},
   "outputs": [],
   "source": [
    "subset_db = db.to_region_subset([\"Reg1\", \"Reg2\"], inplace=False)\n",
    "subset_db.regions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "290983fb",
   "metadata": {},
   "source": [
    "## Inspect the new value-added and final-demand labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5d85c612",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:41:07.320591Z",
     "iopub.status.busy": "2026-04-26T22:41:07.320504Z",
     "iopub.status.idle": "2026-04-26T22:41:07.322536Z",
     "shell.execute_reply": "2026-04-26T22:41:07.322312Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Taxes', 'Wages', 'Capital', 'imports']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "single_region_db.factors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "99f68b8e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:41:07.324060Z",
     "iopub.status.busy": "2026-04-26T22:41:07.323955Z",
     "iopub.status.idle": "2026-04-26T22:41:07.325843Z",
     "shell.execute_reply": "2026-04-26T22:41:07.325613Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Final demand', 'Final Demand exports', 'Intermediate exports']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "single_region_db.consumption_categories"
   ]
  }
 ],
 "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
}
