{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# IODE and numpy\n", "\n", "IODE offers several ways to interact with numpy ndarray objects.\n", "\n", "
\n", "Using numpy ndarray objects is not recommended as there is no compatibility check between for the names and periods. The result is not guaranteed to be correct. This possibility is provided for speed reasons (when dealing with large subsets/databases).\n", "
\n", "\n", "Lets start with necessary imports and loading the sample data:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "from iode import (SAMPLE_DATA_DIR, comments, equations, identities, lists, scalars, \n", " tables, variables, Sample, NA)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading C:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data/fun.cmt\n", "317 objects loaded\n", "Loading C:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data/fun.eqs\n", "274 objects loaded\n", "Loading C:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data/fun.idt\n", "48 objects loaded\n", "Loading C:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data/fun.lst\n", "17 objects loaded\n", "Loading C:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data/fun.scl\n", "161 objects loaded\n", "Loading C:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data/fun.tbl\n", "46 objects loaded\n", "Loading C:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data/fun.var\n", "394 objects loaded\n" ] }, { "data": { "text/plain": [ "(317, 274, 48, 17, 161, 46, 394)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ---- load equations, identities, scalars and variables ----\n", "# Note: test binary and ASCII 'fun' files are located in the 'SAMPLE_DATA_DIR' \n", "# directory of the 'iode' package\n", "comments.load(f\"{SAMPLE_DATA_DIR}/fun.cmt\")\n", "equations.load(f\"{SAMPLE_DATA_DIR}/fun.eqs\")\n", "identities.load(f\"{SAMPLE_DATA_DIR}/fun.idt\")\n", "lists.load(f\"{SAMPLE_DATA_DIR}/fun.lst\")\n", "scalars.load(f\"{SAMPLE_DATA_DIR}/fun.scl\")\n", "tables.load(f\"{SAMPLE_DATA_DIR}/fun.tbl\")\n", "variables.load(f\"{SAMPLE_DATA_DIR}/fun.var\")\n", "\n", "# ---- print the number of objects present in the above workspaces ----\n", "len(comments), len(equations), len(identities), len(lists), len(scalars), len(tables), len(variables)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add one variable:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Workspace: Variables\n", "nb variables: 1\n", "filename: c:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data\\fun.var\n", "description: Modèle fun - Simulation 1 \n", "sample: 1960Y1:2015Y1\n", "mode: LEVEL\n", "\n", "name\t1960Y1\t1961Y1\t1962Y1\t1963Y1\t1964Y1\t1965Y1\t...\t2009Y1\t2010Y1\t2011Y1\t2012Y1\t2013Y1\t2014Y1\t2015Y1\n", "A3 \t na\t 1.00\t 2.00\t 3.00\t 4.00\t 5.00\t...\t 49.00\t 50.00\t 51.00\t 52.00\t 53.00\t 54.00\t na" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values = list(range(variables.nb_periods))\n", "values[0] = NA\n", "values[-1] = np.nan\n", "data = np.asarray(values)\n", "\n", "variables[\"A3\"] = data\n", "variables[\"A3\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Update one variable." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Update all values of a variable:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Workspace: Variables\n", "nb variables: 1\n", "filename: c:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data\\fun.var\n", "description: Modèle fun - Simulation 1 \n", "sample: 1960Y1:2015Y1\n", "mode: LEVEL\n", "\n", "name\t1960Y1\t1961Y1\t1962Y1\t1963Y1\t1964Y1\t1965Y1\t...\t2009Y1\t2010Y1\t2011Y1\t2012Y1\t2013Y1\t2014Y1\t2015Y1\n", "AOUC\t na\t 1.00\t 2.00\t 3.00\t 4.00\t 5.00\t...\t 49.00\t 50.00\t 51.00\t 52.00\t 53.00\t 54.00\t na" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values = list(range(variables.nb_periods))\n", "values[0] = NA\n", "values[-1] = np.nan\n", "data = np.asarray(values)\n", "\n", "variables[\"AOUC\"] = data\n", "variables[\"AOUC\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set the values for range of (contiguous) periods:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Workspace: Variables\n", "nb variables: 1\n", "filename: c:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data\\fun.var\n", "description: Modèle fun - Simulation 1 \n", "sample: 1991Y1:1995Y1\n", "mode: LEVEL\n", "\n", "name\t1991Y1\t1992Y1\t1993Y1\t1994Y1\t1995Y1\n", "ACAG\t 1.00\t na\t 3.00\t na\t 5.00" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# variable[t:t+x] = numpy array\n", "values = [1.0, NA, 3.0, np.nan, 5.0]\n", "data = np.asarray(values)\n", "\n", "variables[\"ACAG\", \"1991Y1:1995Y1\"] = data\n", "variables[\"ACAG\", \"1991Y1:1995Y1\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Update multiple variables at once" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 28.89 , 31.9 , 36.66 , 42.13 , 9.92 ],\n", " [ nan, -39.96 , -42.88 , -16.33 , -41.16 ],\n", " [ 1.023, nan, 1.046, nan, 1.064]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = [[28.89, 31.90, 36.66, 42.13, 9.92],\n", " [np.nan, -39.96, -42.88, -16.33, -41.16],\n", " [1.023, np.nan, 1.046, np.nan, 1.064]]\n", "data = np.asarray(data)\n", "data" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Workspace: Variables\n", "nb variables: 3\n", "filename: c:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data\\fun.var\n", "description: Modèle fun - Simulation 1 \n", "sample: 1991Y1:1995Y1\n", "mode: LEVEL\n", "\n", "name\t1991Y1\t1992Y1\t1993Y1\t1994Y1\t1995Y1\n", "ACAF\t 28.89\t 31.90\t 36.66\t 42.13\t 9.92\n", "ACAG\t na\t-39.96\t-42.88\t-16.33\t-41.16\n", "AOUC\t 1.02\t na\t 1.05\t na\t 1.06" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variables[\"ACAF, ACAG, AOUC\", \"1991Y1:1995Y1\"] = data\n", "variables[\"ACAF, ACAG, AOUC\", \"1991Y1:1995Y1\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arithmetic Operations On Variables With numpy ndarray objects\n", "\n", "IODE variables can be used in arithmetic operations with numpy arrays.\n", "\n", "Let's first reload the variables database to start from a clean state. \n", "Then we will select a subset of variables for the examples below:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading C:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data/fun.var\n", "394 objects loaded\n" ] }, { "data": { "text/plain": [ "Workspace: Variables\n", "nb variables: 5\n", "filename: c:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data\\fun.var\n", "description: Modèle fun - Simulation 1 \n", "sample: 1991Y1:1995Y1\n", "mode: LEVEL\n", "\n", " name\t1991Y1\t1992Y1\t1993Y1\t1994Y1\t1995Y1\n", "ACAF \t 26.24\t 30.16\t 34.66\t 8.16\t-13.13\n", "ACAG \t-30.93\t-40.29\t-43.16\t-16.03\t-41.85\n", "AOUC \t 1.02\t 1.03\t 1.03\t 1.05\t 1.05\n", "AOUC_\t 0.96\t 0.97\t 0.98\t 0.99\t 1.00\n", "AQC \t 1.06\t 1.11\t 1.15\t 1.16\t 1.16" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# reload variables to start from a clean state\n", "variables.load(f\"{SAMPLE_DATA_DIR}/fun.var\")\n", "\n", "# select a subset of variables for the examples below\n", "vars_subset = variables[\"A*\", \"1991Y1:1995Y1\"]\n", "vars_subset" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Workspace: Variables\n", "nb variables: 1\n", "filename: c:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data\\fun.var\n", "description: Modèle fun - Simulation 1 \n", "sample: 1991Y1:1995Y1\n", "mode: LEVEL\n", "\n", "name\t1991Y1\t1992Y1\t1993Y1\t1994Y1\t1995Y1\n", "ACAF\t 26.24\t 60.32\t103.99\t 32.64\t-65.65" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = np.array([1.0, 2.0, 3.0, 4.0, 5.0])\n", "\n", "# multiply the values of a single variable by \n", "# the values of a numpy 1D ndarray\n", "updated_ACAF = vars_subset[\"ACAF\"] * data\n", "updated_ACAF" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Workspace: Variables\n", "nb variables: 5\n", "filename: c:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data\\fun.var\n", "description: Modèle fun - Simulation 1 \n", "sample: 1995Y1:1995Y1\n", "mode: LEVEL\n", "\n", " name\t1995Y1\n", "ACAF \t-13.13\n", "ACAG \t-83.69\n", "AOUC \t 3.15\n", "AOUC_\t 3.98\n", "AQC \t 5.81" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# multiply the values of a subset corresponding to a single period \n", "# by the values of a numpy 1D ndarray\n", "vars_subset_1995Y1 = vars_subset[:, \"1995Y1\"] * data\n", "vars_subset_1995Y1" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Workspace: Variables\n", "nb variables: 5\n", "filename: c:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data\\fun.var\n", "description: Modèle fun - Simulation 1 \n", "sample: 1991Y1:1995Y1\n", "mode: LEVEL\n", "\n", " name\t 1991Y1\t 1992Y1\t 1993Y1\t 1994Y1\t 1995Y1\n", "ACAF \t 26.24\t 60.32\t 103.99\t 32.64\t -65.65\n", "ACAG \t-185.60\t-282.00\t-345.26\t-144.26\t-418.46\n", "AOUC \t 11.27\t 12.38\t 13.40\t 14.65\t 15.75\n", "AOUC_\t 15.43\t 16.56\t 17.62\t 18.80\t 19.91\n", "AQC \t 22.32\t 24.43\t 26.53\t 27.77\t 29.04" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# multiply the values of a subset of variables \n", "# by the values of a numpy 2D ndarray\n", "data = np.array([[1.0, 2.0, 3.0, 4.0, 5.0],\n", " [6.0, 7.0, 8.0, 9.0, 10.0],\n", " [11.0, 12.0, 13.0, 14.0, 15.0],\n", " [16.0, 17.0, 18.0, 19.0, 20.0],\n", " [21.0, 22.0, 23.0, 24.0, 25.0]])\n", "new_vars_subset = vars_subset * data\n", "new_vars_subset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import/Export IODE Variables workspace from/to numpy ndarray\n", "\n", "To import / export the content of the `Variables` workspace (or a subset of it) from/to a numpy ndarray object, use the [from_numpy](../_generated/iode.Variables.from_numpy.rst#iode.Variables.from_numpy) and [to_numpy](../_generated/iode.Variables.to_numpy.rst#iode.Variables.to_numpy) methods." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "394" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(variables)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Sample(\"1960Y1:2015Y1\")" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variables.sample" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "56" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variables.nb_periods" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Export to numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Export the whole Variables workspace to a numpy ndarray:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(394, 56)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# export the whole Variables workspace to a numpy ndarray (394 variables x 56 periods)\n", "data = variables.to_numpy()\n", "data.shape" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "442.26441085858613" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[5, 40]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "442.26441085858613" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variables.i[5, 40]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Export a subset of names:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['ACAF', 'ACAG', 'AOUC', 'AOUC_', 'AQC']" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# export a subset of names\n", "vars_subset = variables[\"A*\"]\n", "vars_subset.names\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Workspace: Variables\n", "nb variables: 5\n", "filename: c:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data\\fun.var\n", "description: Modèle fun - Simulation 1 \n", "sample: 1960Y1:2015Y1\n", "mode: LEVEL\n", "\n", " name\t1960Y1\t1961Y1\t1962Y1\t1963Y1\t1964Y1\t1965Y1\t...\t2009Y1\t2010Y1\t2011Y1\t2012Y1\t2013Y1\t2014Y1\t2015Y1\n", "ACAF \t na\t na\t na\t na\t na\t na\t...\t-37.46\t-37.83\t-44.54\t-55.56\t-68.89\t-83.34\t-96.41\n", "ACAG \t na\t na\t na\t na\t na\t na\t...\t 27.23\t 28.25\t 29.28\t 30.32\t 31.37\t 32.42\t 33.47\n", "AOUC \t na\t 0.25\t 0.25\t 0.26\t 0.28\t 0.29\t...\t 1.29\t 1.31\t 1.33\t 1.36\t 1.39\t 1.42\t 1.46\n", "AOUC_\t na\t na\t na\t na\t na\t na\t...\t 1.23\t 1.25\t 1.27\t 1.30\t 1.34\t 1.37\t 1.41\n", "AQC \t 0.22\t 0.22\t 0.22\t 0.23\t 0.24\t 0.25\t...\t 1.45\t 1.46\t 1.48\t 1.51\t 1.56\t 1.61\t 1.67" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vars_subset" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5, 56)" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = vars_subset.to_numpy()\n", "data.shape" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ nan, nan, nan, nan,\n", " nan, nan, nan, nan,\n", " nan, nan, 1.2130001 , 5.2020001 ,\n", " 9.184 , 8.0790005 , 11.332 , 13.518001 ,\n", " 15.784 , 16.544001 , 21.489 , 20.281 ,\n", " 21.277 , 32.417999 , 24.446999 , 27.025002 ,\n", " 24.504 , 27.560999 , 25.542 , 27.499001 ,\n", " 25.353001 , 17.165001 , 23.771 , 26.240999 ,\n", " 30.159 , 34.661999 , 8.1610022 , -13.130997 ,\n", " 32.171001 , 39.935799 , 29.645657 , 13.53040492,\n", " 10.04661079, 2.86792274, -0.92921251, -6.09156499,\n", " -14.58209446, -26.53878957, -28.98728798, -33.37842578,\n", " -38.40951778, -37.46350964, -37.82742883, -44.54479263,\n", " -55.55928982, -68.89465432, -83.34062511, -96.41041983])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# values of the 'ACAF' variable\n", "data[0]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.21753037, 0.21544869, 0.22228125, 0.22953896, 0.23653506,\n", " 0.24732406, 0.26255098, 0.26907021, 0.27206925, 0.27986595,\n", " 0.29396999, 0.31906503, 0.3426649 , 0.36655167, 0.42489415,\n", " 0.49478459, 0.53812659, 0.5841772 , 0.61441606, 0.64528418,\n", " 0.68947881, 0.73596764, 0.77532566, 0.82384807, 0.85829282,\n", " 0.90006256, 0.92794591, 0.93221092, 0.92874223, 0.9445076 ,\n", " 1. , 1.0628064 , 1.1102825 , 1.1532652 , 1.1571276 ,\n", " 1.1616869 , 1.1580297 , 1.201328 , 1.2031082 , 1.34296997,\n", " 1.33860286, 1.37918825, 1.40881647, 1.41970458, 1.40065206,\n", " 1.39697298, 1.39806354, 1.40791334, 1.42564488, 1.44633167,\n", " 1.46286837, 1.48227361, 1.51366598, 1.55803879, 1.61318117,\n", " 1.67429058])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# values of the 'AQC' variable\n", "data[-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Export a subset of names and periods:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Workspace: Variables\n", "nb variables: 5\n", "filename: c:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data\\fun.var\n", "description: Modèle fun - Simulation 1 \n", "sample: 2000Y1:2010Y1\n", "mode: LEVEL\n", "\n", " name\t2000Y1\t2001Y1\t2002Y1\t2003Y1\t2004Y1\t2005Y1\t2006Y1\t2007Y1\t2008Y1\t2009Y1\t2010Y1\n", "ACAF \t 10.05\t 2.87\t -0.93\t -6.09\t-14.58\t-26.54\t-28.99\t-33.38\t-38.41\t-37.46\t-37.83\n", "ACAG \t-41.53\t 18.94\t 19.98\t 21.02\t 22.07\t 23.11\t 24.13\t 25.16\t 26.19\t 27.23\t 28.25\n", "AOUC \t 1.12\t 1.14\t 1.16\t 1.17\t 1.17\t 1.18\t 1.20\t 1.22\t 1.26\t 1.29\t 1.31\n", "AOUC_\t 1.10\t 1.14\t 1.15\t 1.16\t 1.15\t 1.16\t 1.19\t 1.20\t 1.21\t 1.23\t 1.25\n", "AQC \t 1.34\t 1.38\t 1.41\t 1.42\t 1.40\t 1.40\t 1.40\t 1.41\t 1.43\t 1.45\t 1.46" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# export a subset of names and periods\n", "vars_subset = variables[\"A*\", \"2000Y1:2010Y1\"]\n", "vars_subset" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5, 11)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = vars_subset.to_numpy()\n", "data.shape" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 10.04661079, 2.86792274, -0.92921251, -6.09156499,\n", " -14.58209446, -26.53878957, -28.98728798, -33.37842578,\n", " -38.40951778, -37.46350964, -37.82742883],\n", " [-41.53478657, 18.93980114, 19.98081488, 21.02050218,\n", " 22.06647552, 23.10796216, 24.12963715, 25.16090905,\n", " 26.19211148, 27.22995512, 28.25392898],\n", " [ 1.11623762, 1.14047639, 1.15716928, 1.17048954,\n", " 1.16767464, 1.1815207 , 1.19946163, 1.21933288,\n", " 1.26280574, 1.28713178, 1.3071099 ],\n", " [ 1.1019572 , 1.13624426, 1.15021519, 1.16082895,\n", " 1.14802147, 1.16412337, 1.18589708, 1.19516611,\n", " 1.21383423, 1.23185399, 1.25016433],\n", " [ 1.33860286, 1.37918825, 1.40881647, 1.41970458,\n", " 1.40065206, 1.39697298, 1.39806354, 1.40791334,\n", " 1.42564488, 1.44633167, 1.46286837]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### import from numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To update a subset of the Variables workspace, use the [from_numpy](../_generated/iode.Variables.from_numpy.rst#iode.Variables.from_numpy) method." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['ACAF', 'ACAG', 'AOUC', 'AOUC_', 'AQC']" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vars_names = variables.get_names(\"A*\")\n", "vars_names" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_period = \"2000Y1\"\n", "last_periods = \"2010Y1\"\n", "sample = Sample(first_period, last_periods)\n", "nb_periods = sample.nb_periods\n", "nb_periods" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 10.04661079, 2.86792274, -0.92921251, -6.09156499,\n", " -14.58209446, -26.53878957, -28.98728798, -33.37842578,\n", " -38.40951778, -37.46350964, -37.82742883],\n", " [-41.53478657, 18.93980114, 19.98081488, 21.02050218,\n", " 22.06647552, 23.10796216, 24.12963715, 25.16090905,\n", " 26.19211148, 27.22995512, 28.25392898],\n", " [ 1.11623762, 1.14047639, 1.15716928, 1.17048954,\n", " 1.16767464, 1.1815207 , 1.19946163, 1.21933288,\n", " 1.26280574, 1.28713178, 1.3071099 ],\n", " [ 1.1019572 , 1.13624426, 1.15021519, 1.16082895,\n", " 1.14802147, 1.16412337, 1.18589708, 1.19516611,\n", " 1.21383423, 1.23185399, 1.25016433],\n", " [ 1.33860286, 1.37918825, 1.40881647, 1.41970458,\n", " 1.40065206, 1.39697298, 1.39806354, 1.40791334,\n", " 1.42564488, 1.44633167, 1.46286837]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# save original values to restore them later\n", "original_values = variables[\"A*\", \"2000Y1:2010Y1\"].to_numpy()\n", "original_values" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.],\n", " [ 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21.],\n", " [ 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32.],\n", " [ 33., 34., 35., 36., 37., 38., 39., 40., 41., 42., 43.],\n", " [ 44., 45., 46., 47., 48., 49., 50., 51., 52., 53., 54.]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# create the numpy ndarray containing the values to copy into the Variables database\n", "data = np.zeros((len(vars_names), nb_periods), dtype=float)\n", "for i in range(len(vars_names)):\n", " for j in range(nb_periods):\n", " data[i, j] = i * nb_periods + j\n", "data" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Workspace: Variables\n", "nb variables: 5\n", "filename: c:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data\\fun.var\n", "description: Modèle fun - Simulation 1 \n", "sample: 2000Y1:2010Y1\n", "mode: LEVEL\n", "\n", " name\t2000Y1\t2001Y1\t2002Y1\t2003Y1\t2004Y1\t2005Y1\t2006Y1\t2007Y1\t2008Y1\t2009Y1\t2010Y1\n", "ACAF \t 10.05\t 2.87\t -0.93\t -6.09\t-14.58\t-26.54\t-28.99\t-33.38\t-38.41\t-37.46\t-37.83\n", "ACAG \t-41.53\t 18.94\t 19.98\t 21.02\t 22.07\t 23.11\t 24.13\t 25.16\t 26.19\t 27.23\t 28.25\n", "AOUC \t 1.12\t 1.14\t 1.16\t 1.17\t 1.17\t 1.18\t 1.20\t 1.22\t 1.26\t 1.29\t 1.31\n", "AOUC_\t 1.10\t 1.14\t 1.15\t 1.16\t 1.15\t 1.16\t 1.19\t 1.20\t 1.21\t 1.23\t 1.25\n", "AQC \t 1.34\t 1.38\t 1.41\t 1.42\t 1.40\t 1.40\t 1.40\t 1.41\t 1.43\t 1.45\t 1.46" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variables[\"A*\", \"2000Y1:2010Y1\"]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Workspace: Variables\n", "nb variables: 5\n", "filename: c:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data\\fun.var\n", "description: Modèle fun - Simulation 1 \n", "sample: 2000Y1:2010Y1\n", "mode: LEVEL\n", "\n", " name\t2000Y1\t2001Y1\t2002Y1\t2003Y1\t2004Y1\t2005Y1\t2006Y1\t2007Y1\t2008Y1\t2009Y1\t2010Y1\n", "ACAF \t 0.00\t 1.00\t 2.00\t 3.00\t 4.00\t 5.00\t 6.00\t 7.00\t 8.00\t 9.00\t 10.00\n", "ACAG \t 11.00\t 12.00\t 13.00\t 14.00\t 15.00\t 16.00\t 17.00\t 18.00\t 19.00\t 20.00\t 21.00\n", "AOUC \t 22.00\t 23.00\t 24.00\t 25.00\t 26.00\t 27.00\t 28.00\t 29.00\t 30.00\t 31.00\t 32.00\n", "AOUC_\t 33.00\t 34.00\t 35.00\t 36.00\t 37.00\t 38.00\t 39.00\t 40.00\t 41.00\t 42.00\t 43.00\n", "AQC \t 44.00\t 45.00\t 46.00\t 47.00\t 48.00\t 49.00\t 50.00\t 51.00\t 52.00\t 53.00\t 54.00" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# copy the numpy ndarray into the Variables database (overriding the existing values)\n", "variables.from_numpy(data, vars_names, first_period, last_periods)\n", "variables[\"A*\", \"2000Y1:2010Y1\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you already work on the subset you whish to update the values, you can skip to specify the value for the parameters *vars_names*, *first_period* and *last_period*:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Workspace: Variables\n", "nb variables: 5\n", "filename: c:\\soft\\Miniconda3\\Lib\\site-packages\\iode\\tests\\data\\fun.var\n", "description: Modèle fun - Simulation 1 \n", "sample: 2000Y1:2010Y1\n", "mode: LEVEL\n", "\n", " name\t2000Y1\t2001Y1\t2002Y1\t2003Y1\t2004Y1\t2005Y1\t2006Y1\t2007Y1\t2008Y1\t2009Y1\t2010Y1\n", "ACAF \t 10.05\t 2.87\t -0.93\t -6.09\t-14.58\t-26.54\t-28.99\t-33.38\t-38.41\t-37.46\t-37.83\n", "ACAG \t-41.53\t 18.94\t 19.98\t 21.02\t 22.07\t 23.11\t 24.13\t 25.16\t 26.19\t 27.23\t 28.25\n", "AOUC \t 1.12\t 1.14\t 1.16\t 1.17\t 1.17\t 1.18\t 1.20\t 1.22\t 1.26\t 1.29\t 1.31\n", "AOUC_\t 1.10\t 1.14\t 1.15\t 1.16\t 1.15\t 1.16\t 1.19\t 1.20\t 1.21\t 1.23\t 1.25\n", "AQC \t 1.34\t 1.38\t 1.41\t 1.42\t 1.40\t 1.40\t 1.40\t 1.41\t 1.43\t 1.45\t 1.46" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vars_subset = variables[\"A*\", \"2000Y1:2010Y1\"]\n", "vars_subset.from_numpy(original_values)\n", "vars_subset" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.9" } }, "nbformat": 4, "nbformat_minor": 2 }