{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "jqrbl-5KPRjH" }, "source": [ "#Workshop2: Waste IO\n", "Data:WIOdata2.xlsx" ] }, { "cell_type": "markdown", "metadata": { "id": "4-6WYbWVPYX4" }, "source": [ "Preparation for libraries." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "iGeRZ458OLgm" }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import numpy.linalg as la" ] }, { "cell_type": "markdown", "metadata": { "id": "OuLt8JIvPdbR" }, "source": [ "Data input & labels" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Gak8CHAMPgzJ" }, "outputs": [], "source": [ "# Load data\n", "WIOdata = pd.read_excel('WIOdata2.xlsx','WIOdata2', header=0,index_col=0)\n", "S = pd.read_excel('WIOdata2.xlsx','S', header=0,index_col=0).to_numpy()\n", "\n", "# Labels\n", "l_col = tuple(WIOdata.columns)\n", "l_row = tuple(WIOdata.index)\n", "n_x, n_t, n_y, n_w, n_v, n_f = 81, 10, 8, 99, 8, 7 # Sector, treatment, final demand, waste, value added, emission items\n", "# n_y does not include Exports\n", "\n", "n_xw = n_x + n_w\n", "n_xt = n_x + n_t\n", "n_xy = n_x + n_y\n", "n_xty = n_x + n_y + n_t\n", "\n", "l_x = l_col[ : n_x] # List of products\n", "l_t = l_col[n_x : n_xt] # List of treatment\n", "l_y = l_col[n_xt : n_xty] # List of domestic final demand\n", "l_y_all = l_col[n_xt : n_xty + 1] # List of final demand with Exports\n", "l_w = l_row[n_x : n_xw] # List of waste\n", "l_xt = l_x + l_t # List of production and treatment\n", "l_xw = l_x + l_w # List of production and waste\n", "l_f = l_row[n_xw + n_w + n_v:] # Emissions\n", "\n", "n_y_all = len(l_y_all)\n", "\n", "# Converting Pd.DatFrame to Numpy arrays\n", "Data = WIOdata.to_numpy()\n", "\n", "# Gross output\n", "x_ = Data[l_row.index(\"Gross output\"),:n_x]" ] }, { "cell_type": "markdown", "metadata": { "id": "ZirhedOZPwBk" }, "source": [ "Waste output and net waste flow" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "clsrjPAVRA_b" }, "outputs": [], "source": [ "# Gross output of waste\n", "w_out = Data[n_x:n_x+n_w,:].sum(axis=1)\n", "\n", "# Net waste flow\n", "W_net = Data[n_x : n_xw, :] - Data[n_xw : n_xw + n_w, : ]\n", "w_net = W_net.sum(axis=1)\n", "\n", "# Direct generation from final demand\n", "W_y = Data[n_x : n_xw , n_xt : n_xty]\n", "\n", "# Net waste flow converted to treatment demand\n", "SW_ = S.T @ W_net\n", "\n", "# Treatment acitivities\n", "t_ = SW_.sum(axis=1)" ] }, { "cell_type": "markdown", "metadata": { "id": "zdQ7VRAbRGQb" }, "source": [ "Production and treatment activities" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "4iOAmrCuRHQT" }, "outputs": [], "source": [ "# Combined flow matrix of production and treatment\n", "XSW_ = np.vstack(( Data[ :n_x, : ], SW_))\n", "\n", "# Combined activities (production and treatment)\n", "xt_ = np.concatenate((x_, t_))\n", "\n", "# Final demand for treatment\n", "Y_t = XSW_[ n_x: , n_xt : n_xty ]\n", "\n", "# Final demand for products and treatment\n", "Y_xt = np.vstack(( Data[ :n_x, n_xt: n_xty], Y_t ))\n", "\n", "#%% Waste and emission coefficients\n", "# Waste output coefficients\n", "G_out = Data[ n_x : n_xw , : n_xt ] / xt_\n", "\n", "# Waste input coeffficients\n", "G_in = Data[ n_xw : n_xw + n_w , : n_xt ] / xt_\n", "\n", "# Net waste output coeffcieints\n", "G = G_out - G_in\n", "\n", "# Emission coefficients\n", "F = Data[n_xw + n_w + n_v:, :n_xt] / xt_" ] }, { "cell_type": "markdown", "metadata": { "id": "zYyyW7tBRMnF" }, "source": [ "Input coefficients matrix" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "E2J-r7KpROx1" }, "outputs": [], "source": [ "# A matrix (input coefficients matrix)\n", "A_xt = XSW_[:, :n_xt] / xt_\n", "\n", "# Domestic ratio adjustment\n", "# Imports and exports\n", "im_ = Data[:n_x, l_col.index(\"Imports\")]\n", "ex_ = Data[:n_x, l_col.index(\"Exports\")]\n", "\n", "# Domestic ratio: d = 1 for treatment\n", "d_ = np.ones(n_xt)\n", "d_[:n_x] = 1 + im_ / (x_ - ex_ - im_)\n", "\n", "# Adjusted A matrix for imports\n", "A_xt_d = A_xt\n", "A_xt_d = np.diag(d_) @ A_xt\n", "\n", "# Final demand for domestic products and treatment\n", "# Adjusting final demand (except Exports) for imports\n", "Y_xt_d = np.zeros(( n_xt, n_y_all ))\n", "Y_xt_d[ :, :n_y ] = Y_xt[ :, :n_y ]\n", "Y_xt_d[ :, :n_y ] = np.diag(d_) @ Y_xt[ :, :n_y ]\n", "# Adding Exports as the last column element\n", "Y_xt_d[:n_x,n_y] = ex_\n", "\n", "# The row sum of final demand for domestic products and treatment\n", "y_xt_d = Y_xt_d.sum(axis=1)" ] }, { "cell_type": "markdown", "metadata": { "id": "igFAMFQKRTUN" }, "source": [ "Leontief inverse and calculation of activities" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "FeaDaLv2RVCX" }, "outputs": [], "source": [ "# Leontief inverse\n", "L = la.inv( np.eye( n_xt ) - A_xt_d )\n", "XT_y = L @ Y_xt_d\n", "xt_y = XT_y.sum(axis=1)\n", "\n", "# Consistency check\n", "epsilon = 0.00001\n", "indices = np.where(np.abs(xt_ - xt_y) > epsilon)[0]\n", "print(\"Inconsistent indices for XT_y:\", indices)" ] }, { "cell_type": "markdown", "metadata": { "id": "rmRhEnAwRZN2" }, "source": [ "Waste footptint" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "id": "GYIVWPQbRbwf" }, "outputs": [], "source": [ "# Waste footprint by final demand categories\n", "WF_y = pd.DataFrame(data = G @ XT_y, index=l_w, columns=l_y_all)\n", "\n", "# Waste footprint by final demand for products and treatment\n", "WF_Y = pd.DataFrame(data = G @ L @ np.diag(y_xt_d), index=l_w, columns=l_xt)\n", "\n", "# Emissions induced by final demand categories\n", "E_y = pd.DataFrame(data = F @ XT_y, index=l_f, columns=l_y_all)\n", "\n", "# Emissions induced by final demand for products and treatment\n", "E_Y = pd.DataFrame(data = F @ L @ np.diag(y_xt_d), index=l_f, columns=l_xt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Outputs to XLSX Files" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "WF_y.to_excel('WF_y_category.xlsx', engine='openpyxl')\n", "\n", "WF_Y.to_excel('WF_y_products.xlsx', engine='openpyxl')\n", "\n", "E_y.to_excel('E_y_category.xlsx', engine='openpyxl')\n", "\n", "E_Y.to_excel('E_y_products.xlsx', engine='openpyxl')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.7" } }, "nbformat": 4, "nbformat_minor": 4 }