{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction\n",
    "\n",
    "This notebook presents **lca-algebraic** a small libray above **brightay2**, designed for the definition of **parametric inventories** with fast computation of LCA impacts, suitable for **monte-carlo** analyis.\n",
    "\n",
    "**lca-algebraic** provides a set of  **helper functions** for : \n",
    "* **compact** & **human readable** definition of activites :  \n",
    "    * search background (tech and biosphere) activities \n",
    "    * create new foreground activites with parametrized amounts\n",
    "    * parametrize / update existing background activities (extending the class **Activity**)\n",
    "* Definition of parameters\n",
    "* Computation of LCAs \n",
    "* Computation of statistics (including Sobols indices)\n",
    "* Compute simplified parametric model by fixing minor input parameters\n",
    "\n",
    "  \n",
    "## Principles \n",
    "\n",
    "The main idea of this libray is to move from **procedural definition** of models (slow and prone to errors) to a **declarative / purely functionnal** definition of parametric models (models as **pure functions**). \n",
    "\n",
    "This enables **fast computation of LCA impacts**, useful for Monte Carlo methods and statistical analysis. \n",
    "We leverage the **power of symbolic calculus** provided by the library [SymPy](https://www.sympy.org/en/index.html).\n",
    "\n",
    "We define our model in a **separate database**, as a nested combination of : \n",
    "* other foreground activities\n",
    "* background activities :\n",
    "    * Technical, refering **ecoinvent DB**\n",
    "    * Biosphere, refering **brightway2** biosphere activities\n",
    "    \n",
    "The **amounts** in exchanges are expressed either as **static amounts**, or **symbolic expressions** of pre-defined **parameters**.\n",
    "\n",
    "Each activity of our **root model** is defined as a **parametrized combination** of the **foreground activities**, which can themselves be expressed by the **background activities**.\n",
    "\n",
    "When computing LCA for foreground models, the library develops the model as a combination of **only background activities**. It computes **once for all** the impact of **all required background activities** and compiles a **fast numpy** (vectorial) function for each impact, replacing each background activity by the **static value of the corresponding impact**.\n",
    "\n",
    "By providing **large vectors** of **parameter values** to those numpy functions, we can compute LCA for **thousands of values** at a time.\n",
    "\n",
    "![](https://oie-mines-paristech.github.io/lca_algebraic/doc/lca-algebraic.png)\n",
    "\n",
    "\n",
    "## Compatiblity with brightway2 \n",
    "\n",
    "Under the hood, the activities we define with **lca-algebraic** are standard **brightway2** activities. \n",
    "The amounts of exchanges are stored as **float values** or **serialized as string** in the property **formula**.\n",
    "\n",
    "Parameters are also stored in the **brightay2** projets, making it fully compatible with **brightway**.\n",
    "\n",
    "Thus, a model defined with **lca-algebraic** is stored as a regular **bw2** projet. We can use **bw2** native support for [parametrized dataset](https://2.docs.brightway.dev/intro.html#parameterized-datasets) for computing LCAs, even if much more slower than the method explain here.\n",
    "\n",
    "## Doc\n",
    "\n",
    "The followng notebook explores the main functions.\n",
    "Full documentation of the functions is [available here](https://oie-mines-paristech.github.io/lca_algebraic/doc/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:52:41.329897Z",
     "start_time": "2022-02-28T10:52:39.513521Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/rjolivet/.local/share/Brightway3/default.c21f969b5f03d33d43e04f8f136e7682/databases.json\n",
      "/home/rjolivet/.local/share/Brightway3/default.c21f969b5f03d33d43e04f8f136e7682/methods.json\n",
      "/home/rjolivet/.local/share/Brightway3/default.c21f969b5f03d33d43e04f8f136e7682/normalizations.json\n",
      "/home/rjolivet/.local/share/Brightway3/default.c21f969b5f03d33d43e04f8f136e7682/weightings.json\n",
      "/home/rjolivet/.local/share/Brightway3/default.c21f969b5f03d33d43e04f8f136e7682/databases.json\n",
      "/home/rjolivet/.local/share/Brightway3/default.c21f969b5f03d33d43e04f8f136e7682/methods.json\n",
      "/home/rjolivet/.local/share/Brightway3/default.c21f969b5f03d33d43e04f8f136e7682/normalizations.json\n",
      "/home/rjolivet/.local/share/Brightway3/default.c21f969b5f03d33d43e04f8f136e7682/weightings.json\n",
      "/home/rjolivet/.local/share/Brightway3/default.c21f969b5f03d33d43e04f8f136e7682/migrations.json\n",
      "/home/rjolivet/.local/share/Brightway3/default.c21f969b5f03d33d43e04f8f136e7682/unlinked_data.json\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/rjolivet/anaconda3/envs/acv3.6/lib/python3.6/site-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.\n",
      "  import pandas.util.testing as tm\n"
     ]
    }
   ],
   "source": [
    "# Those two lines are for dev only : they watch imported libraries for changes\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import pandas as pd\n",
    "import time\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import brightway2 as bw\n",
    "\n",
    "# Custom utils defined for inter-acv\n",
    "from lca_algebraic import *\n",
    "from lca_algebraic.stats import * \n",
    "import lca_algebraic\n",
    "from sympy import init_printing, simplify\n",
    "\n",
    "init_printing()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Init brightway2 and databases"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:58:29.608270Z",
     "start_time": "2022-02-28T10:53:58.395865Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Extracting ecospold2 files:\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/rjolivet/.local/share/Brightway3/MyProject.6801fb521646e13e5eabca9a6bd84430/databases.json\n",
      "/home/rjolivet/.local/share/Brightway3/MyProject.6801fb521646e13e5eabca9a6bd84430/methods.json\n",
      "/home/rjolivet/.local/share/Brightway3/MyProject.6801fb521646e13e5eabca9a6bd84430/normalizations.json\n",
      "/home/rjolivet/.local/share/Brightway3/MyProject.6801fb521646e13e5eabca9a6bd84430/weightings.json\n",
      "/home/rjolivet/.local/share/Brightway3/MyProject.6801fb521646e13e5eabca9a6bd84430/migrations.json\n",
      "/home/rjolivet/.local/share/Brightway3/MyProject.6801fb521646e13e5eabca9a6bd84430/unlinked_data.json\n",
      "Biosphere database already present!!! No setup is needed\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "0% [##############################] 100% | ETA: 00:00:00 | Item ID: 82af64d7-c56e-4\n",
      "Total time elapsed: 00:01:56\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Extracting ecospold2 files:\n",
      "  Started: 02/28/2022 11:53:58\n",
      "  Finished: 02/28/2022 11:55:54\n",
      "  Total time elapsed: 00:01:56\n",
      "  CPU %: 97.50\n",
      "  Memory %: 7.58\n",
      "Extracted 14889 datasets in 116.35 seconds\n",
      "Applying strategy: normalize_units\n",
      "Applying strategy: update_ecoinvent_locations\n",
      "Applying strategy: remove_zero_amount_coproducts\n",
      "Applying strategy: remove_zero_amount_inputs_with_no_activity\n",
      "Applying strategy: remove_unnamed_parameters\n",
      "Applying strategy: es2_assign_only_product_with_amount_as_reference_product\n",
      "Applying strategy: assign_single_product_as_activity\n",
      "Applying strategy: create_composite_code\n",
      "Applying strategy: drop_unspecified_subcategories\n",
      "Applying strategy: fix_ecoinvent_flows_pre35\n",
      "/home/rjolivet/.local/share/Brightway3/MyProject.6801fb521646e13e5eabca9a6bd84430/migrations/fix-ecoinvent-flows-pre-35.8a1947cccbeab5ee4e017f41b83b2110.json\n",
      "Applying strategy: drop_temporary_outdated_biosphere_flows\n",
      "Applying strategy: link_biosphere_by_flow_uuid\n",
      "Applying strategy: link_internal_technosphere_by_composite_code\n",
      "Applying strategy: delete_exchanges_missing_activity\n",
      "Applying strategy: delete_ghost_exchanges\n",
      "Applying strategy: remove_uncertainty_from_negative_loss_exchanges\n",
      "Applying strategy: fix_unreasonably_high_lognormal_uncertainties\n",
      "Applying strategy: set_lognormal_loc_value\n",
      "Applying strategy: convert_activity_parameters_to_list\n",
      "Applying strategy: add_cpc_classification_from_single_reference_product\n",
      "Applied 20 strategies in 5.89 seconds\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Writing activities to SQLite3 database:\n",
      "IOPub data rate exceeded.\n",
      "The notebook server will temporarily stop sending output\n",
      "to the client in order to avoid crashing it.\n",
      "To change this limit, set the config variable\n",
      "`--NotebookApp.iopub_data_rate_limit`.\n",
      "\n",
      "Current values:\n",
      "NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)\n",
      "NotebookApp.rate_limit_window=3.0 (secs)\n",
      "\n",
      "0% [##############################] 100% | ETA: 00:00:00\n",
      "Total time elapsed: 00:01:07\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Writing activities to SQLite3 database:\n",
      "  Started: 02/28/2022 11:56:07\n",
      "  Finished: 02/28/2022 11:57:14\n",
      "  Total time elapsed: 00:01:07\n",
      "  CPU %: 69.30\n",
      "  Memory %: 8.56\n",
      "Created database: ecoinvent 3.4\n"
     ]
    }
   ],
   "source": [
    "### Init the brightway2 project :choose any project name\n",
    "initProject('MyProject')\n",
    "\n",
    "# Import Ecoinvent DB (if not already done)\n",
    "# Update the name and path to the location of the ecoinvent database\n",
    "importDb(\"ecoinvent 3.4\", './ecoinvent 3.4_cutoff_ecoSpold02/datasets')\n",
    "\n",
    "# We use a separate DB for defining our foreground model / activities\n",
    "# Choose any name\n",
    "USER_DB = 'Foreground DB'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:24.241228Z",
     "start_time": "2022-02-28T10:59:24.169052Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Db Foreground DB was here. Reseting it\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>backend</th>\n",
       "      <th>type</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>biosphere3</th>\n",
       "      <td>sqlite</td>\n",
       "      <td>background</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ecoinvent 3.4</th>\n",
       "      <td>sqlite</td>\n",
       "      <td>background</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Foreground DB</th>\n",
       "      <td>sqlite</td>\n",
       "      <td>foreground</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              backend        type\n",
       "name                             \n",
       "biosphere3     sqlite  background\n",
       "ecoinvent 3.4  sqlite  background\n",
       "Foreground DB  sqlite  foreground"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# This is better to cleanup the whole foreground model each time, and redefine it in the notebook\n",
    "# instead of relying on a state or previous run.\n",
    "# Any persistent state is prone to errors.\n",
    "resetDb(USER_DB)\n",
    "\n",
    "# Parameters are stored at project level : \n",
    "# Reset them also\n",
    "# You may remove this line if you import a project and parameters from an external source (see loadParam(..))\n",
    "resetParams()\n",
    "\n",
    "# Overview of the databases\n",
    "list_databases()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction to Numpy\n",
    "\n",
    "\n",
    "Numpy is a python libray for symbolic calculus. \n",
    "\n",
    "You write Sympy expression as you write **standard python expressions**, using **sympy symbols** in them. \n",
    "\n",
    "\n",
    "The result is then a **symbolic expression that can be manipulated**, instead of a **numeric value**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:26.778439Z",
     "start_time": "2022-02-28T10:59:26.466460Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEAAAAASCAYAAADrL9giAAAABHNCSVQICAgIfAhkiAAAAndJREFUWIXt10uIT3EUB/DPMHYUZiHFgoUoCQsrC48oG8qjRB7DbKQmsRChyYqFEEV5J7JioYRkPKa8i2hispgQ8kxGyDAW5465Xff/iL+i8a1fv9vvfDv33O/vnN/53aqGhgbdGT1SzzWowwk8xEe8QxOWZrj/EhagIxl1WWN16nkOduEZGvEIAzATezEt4XT82XgrisHYgTb0ziOkd7UF0zEI87EGSzAcjzFLiPGnsViIPOE3/VThAF5jdyFSWoDzOIlvGc7zlIO8oObhiBDwPd7iBmp/IehKoh6Tkjg+FCKVW9dfkrk9s94HhzAUl7ETxzEE+7G6/HgrihHYhO24VIxYXcyY4ixMnk9nbB2izp5n1teJjKjF5jLeUUlU47A4w9aWQy6FTRiJUziTsbUlI4tneIr+ZfivNDZgDMaLTlYUpUqgHqtwX7STLPphPa6K2v+qq+UMw5MS/ltT/M5xILE15tgOlvA3Tuz6FlwpwUXxDFguaqgZk/EmYx+Fs6JVXsexhPNFnAELcafE+7ehb2ZtNGaIs6U1Y7tdxFdn6reITSkLhQRYga24Jz7+RQ7nsAh+Ii5kbBuT+WaJ92/LWVssBDiY47cYeousg08FOHuSsV18Y64Aq0Xd38YUvMrhDBYZcCYnyL66WuCtskKvDD5jXwHbWHEuNOGBVHlkBVgvdu8Wpvo57TvRqfBQ9NLVJmtwVFym2hVP2Urjo5yrboIGIcAhcav9gbQAi8THfxU9vT7HUatIzZfi4jQJ13AOA8V1uVFcppoVTsW/BmkBhiRzT0l95OCirpN4rjhtp2EZ7mJlMs9Wuv7/ClT9/x3u5uj2AnwH03aUdFtR7XcAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle 2 a + 4$"
      ],
      "text/plain": [
       "2⋅a + 4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy import symbols \n",
    "\n",
    "# create sympy symbol\n",
    "a = symbols(\"a\")\n",
    "\n",
    "# Expressions are not directly evaluated \n",
    "f = a * 2 + 4 \n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:27.445779Z",
     "start_time": "2022-02-28T10:59:27.325077Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAABsAAAASCAYAAACq26WdAAAABHNCSVQICAgIfAhkiAAAAW1JREFUOI3F1L9rU1EYxvFPipPSRBdx6FBaTNx0EH/QqRGCdMrgKuJoBUVwEBRMBWelpR1Fiv+Aq0sGael/UDUEFEWXtqCtiIs6nJMQjl5vQ9L2hcNzz3lezvfec+77FhqNhv2KkWR+BQt4jW/4jRc5e4zhGT7jJ97jKY6liYeS+QOcxg4+4VQOaBKrOI6XeINzuI3LmMJmJzn9sjsoo4gbOSBYiqBbqOMeqniCCh73JqewJlrC8eXFBGrCsS0m3kN8x1UcyYL1E9Wor/Ar8baxgsO4MAxYJeq7DL8VtTwMWCnq1wy/s350GLC8KETt3v8gsM6blzL8YpI3EOxt1HKGfzJq904HgTWj1v6xz6hQ0D+wNgxYW/jtx3Ez8eaE+loW6g1/t6t6HHAi6kU8j88buNuTPyu0q3lcwjrOY1o4vvu9m6ewM7iWrE3EAR8SWBtn8UjohTP4EuFz2PofrBFHP/ER13eTuJd1drCwPwZfQWYV4tVCAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle 10$"
      ],
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# symbols can be replaced by values afterwards \n",
    "f.subs(dict(a=3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In practice, you don't need to care about Sympy. Just remember that : \n",
    "* The parameters defined below are **instances of sympy symbols**\n",
    "* Any **valid python expression** containing a **sympy symbol** will create a **sympy symbolic expression**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Define input parameters\n",
    "\n",
    "First, we define the input parameters of the model together with their distribution.\n",
    "\n",
    "The numeric parameters are **instances of sympy 'Symbol'**. \n",
    "\n",
    "Thus, any python arithmetic expression composed of parameters will result in a **symbolic expression** to be used later in the definition of the model, rather than a static numeric result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:28.768260Z",
     "start_time": "2022-02-28T10:59:28.621688Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Warning : param 'a' linked to root project instead of a specific DB\n",
      "Warning : param 'b' linked to root project instead of a specific DB\n",
      "No 'min/max' provided, param b marked as FIXED\n",
      "No 'min/max' provided, param b marked as FIXED\n",
      "Warning : param 'share_recycled_aluminium' linked to root project instead of a specific DB\n",
      "Warning : param 'c' linked to root project instead of a specific DB\n",
      "Warning : param 'beta' linked to root project instead of a specific DB\n",
      "Warning : param 'bool_param' linked to root project instead of a specific DB\n",
      "Warning : param 'elec_switch_param' linked to root project instead of a specific DB\n",
      "Warning : param 'techno_param' linked to root project instead of a specific DB\n"
     ]
    }
   ],
   "source": [
    "# Example of 'float' parameters\n",
    "a = newFloatParam(\n",
    "    'a', \n",
    "    default=0.5, min=0.2, max=2,  distrib=DistributionType.TRIANGLE, # Distribution type, linear by default\n",
    "    description=\"hello world\",\n",
    "    label=\"extended label for a\")\n",
    "\n",
    "b = newFloatParam(\n",
    "    'b',\n",
    "    default=0.5, # Fixed if no min /max provided\n",
    "    description=\"foo bar\")\n",
    "\n",
    "# Parameters can be attached to a specific DB instead of global project\n",
    "# We recommand this for team working / working with several DBs\n",
    "# lca_algebraic supports resolution of parameters with duplicate names attached to different scopes \n",
    "b2 = newFloatParam(\n",
    "    'b',\n",
    "    default=0.5, # Fixed if no min /max provided\n",
    "    description=\"foo bar\",\n",
    "    dbname=USER_DB)\n",
    "\n",
    "share_recycled_aluminium = newFloatParam(\n",
    "    'share_recycled_aluminium',  \n",
    "    default=0.6, min=0, max=1, std=0.2, distrib=DistributionType.NORMAL, # Normal distrib, with std dev\n",
    "    description=\"Share of reycled aluminium\")\n",
    "\n",
    "c = newFloatParam(\n",
    "    'c',  \n",
    "    default=0.6, std=0.2, distrib=DistributionType.LOGNORMAL)\n",
    "\n",
    "beta = newFloatParam(\n",
    "    'beta',  \n",
    "    default=0.6, std=0.2, a=2, b=5, distrib=DistributionType.BETA)\n",
    "\n",
    "# You can define boolean parameters, taking only discrete values 0 or 1\n",
    "bool_param = newBoolParam(\n",
    "    'bool_param', \n",
    "    default=1)\n",
    "\n",
    "# Example 'enum' parameter, acting like a switch between several possibilities\n",
    "# Enum parameters are not Symbol themselves\n",
    "# They are a facility to represent many boolean parameters at once '<paramName>_<enumValue>' \n",
    "# and should be used with the 'newSwitchAct' method \n",
    "elec_switch_param = newEnumParam(\n",
    "    'elec_switch_param', \n",
    "    values=[\"us\", \"eu\"], # If provided as list, all possibilities have te same probability\n",
    "    default=\"us\", \n",
    "    description=\"Switch on electricty mix\")\n",
    "\n",
    "# Another example enum param\n",
    "techno_param = newEnumParam(\n",
    "    'techno_param', \n",
    "    values={\n",
    "        \"technoA\":0.4, \n",
    "        \"technoB\":0.1,\n",
    "        \"technoC\":0.5}, # You can provide a statistical weight for each value, by using a dict\n",
    "    default=\"technoA\", \n",
    "    description=\"Choice of techonoly\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:28.895801Z",
     "start_time": "2022-02-28T10:59:28.874489Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th>group  </th><th>name                    </th><th>label                   </th><th>default  </th><th style=\"text-align: right;\">  min</th><th style=\"text-align: right;\">  max</th><th style=\"text-align: right;\">  std</th><th>distrib  </th><th>unit  </th><th>db           </th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td>       </td><td>a                       </td><td>extended label for a    </td><td>0.5      </td><td style=\"text-align: right;\">  0.2</td><td style=\"text-align: right;\">    2</td><td style=\"text-align: right;\">     </td><td>triangle </td><td>      </td><td>[project]    </td></tr>\n",
       "<tr><td>       </td><td>b                       </td><td>b                       </td><td>0.5      </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">     </td><td>fixed    </td><td>      </td><td>[project]    </td></tr>\n",
       "<tr><td>       </td><td>b                       </td><td>b                       </td><td>0.5      </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">     </td><td>fixed    </td><td>      </td><td>Foreground DB</td></tr>\n",
       "<tr><td>       </td><td>beta                    </td><td>beta                    </td><td>0.6      </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">  0.2</td><td>beta     </td><td>      </td><td>[project]    </td></tr>\n",
       "<tr><td>       </td><td>bool_param              </td><td>bool param              </td><td>1        </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">     </td><td>         </td><td>      </td><td>[project]    </td></tr>\n",
       "<tr><td>       </td><td>c                       </td><td>c                       </td><td>0.6      </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">  0.2</td><td>lognormal</td><td>      </td><td>[project]    </td></tr>\n",
       "<tr><td>       </td><td>elec_switch_param       </td><td>elec switch param       </td><td>us       </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">     </td><td>         </td><td>      </td><td>[project]    </td></tr>\n",
       "<tr><td>       </td><td>share_recycled_aluminium</td><td>share recycled aluminium</td><td>0.6      </td><td style=\"text-align: right;\">  0  </td><td style=\"text-align: right;\">    1</td><td style=\"text-align: right;\">  0.2</td><td>normal   </td><td>      </td><td>[project]    </td></tr>\n",
       "<tr><td>       </td><td>techno_param            </td><td>techno param            </td><td>technoA  </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">     </td><td style=\"text-align: right;\">     </td><td>         </td><td>      </td><td>[project]    </td></tr>\n",
       "</tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# List of parameters\n",
    "list_parameters()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Persistance of parameters\n",
    "\n",
    "By default, new parameters are kept in memory but also persisted in the project (unless save=False).\n",
    "You can persist parameters afterwards with `persistParams()`.\n",
    "You can load also load parameters from an existing database with `loadParams()`.\n",
    "The persistance of parameters and the distribution is compatible with **Brightway2** and **Activity Browser**  [see documentation of stat_arrays](https://stats-arrays.readthedocs.io/en/latest/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:30.106403Z",
     "start_time": "2022-02-28T10:59:30.069016Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[ParamRegistry] Param b was already defined in 'Foreground DB' : overriding.\n",
      "[ParamRegistry] Param b was already defined in 'Foreground DB' : overriding.\n",
      "Warning : param 'a' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param a was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param a was already defined in '<project>' : overriding.\n",
      "Warning : param 'b' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param b was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param b was already defined in '<project>' : overriding.\n",
      "Variable 'b' was already defined : overidding it with param.\n",
      "Warning : param 'share_recycled_aluminium' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param share_recycled_aluminium was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param share_recycled_aluminium was already defined in '<project>' : overriding.\n",
      "Warning : param 'c' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param c was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param c was already defined in '<project>' : overriding.\n",
      "Warning : param 'beta' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param beta was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param beta was already defined in '<project>' : overriding.\n",
      "Warning : param 'bool_param' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param bool_param was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param bool_param was already defined in '<project>' : overriding.\n",
      "Warning : param 'elec_switch_param' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param elec_switch_param was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param elec_switch_param was already defined in '<project>' : overriding.\n",
      "Warning : param 'techno_param' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param techno_param was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param techno_param was already defined in '<project>' : overriding.\n"
     ]
    }
   ],
   "source": [
    "# Load parameters previously  persisted in the dabatase.\n",
    "loadParams()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-21T12:51:54.353447Z",
     "start_time": "2021-06-21T12:51:54.326757Z"
    }
   },
   "source": [
    "# Manage several Datasets\n",
    "\n",
    "lca_algebraic supports several foreground / background datasets. Background datasets are considered static / non parametrized by the library : they use standard LCA method of **Brightway2**. \n",
    "\n",
    "Foreground databases are considered parametric and their activities are developped as functions of parameters and background activities.\n",
    "\n",
    "## Set status of a database\n",
    "\n",
    "The functions **setForeground(...)** and **setBackground(...)** change the status of a database."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:30.436388Z",
     "start_time": "2022-02-28T10:59:30.395103Z"
    }
   },
   "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>backend</th>\n",
       "      <th>type</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>biosphere3</th>\n",
       "      <td>sqlite</td>\n",
       "      <td>background</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ecoinvent 3.4</th>\n",
       "      <td>sqlite</td>\n",
       "      <td>background</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Foreground DB</th>\n",
       "      <td>sqlite</td>\n",
       "      <td>background</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              backend        type\n",
       "name                             \n",
       "biosphere3     sqlite  background\n",
       "ecoinvent 3.4  sqlite  background\n",
       "Foreground DB  sqlite  background"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "setBackground(USER_DB)\n",
    "list_databases()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:30.568230Z",
     "start_time": "2022-02-28T10:59:30.541053Z"
    }
   },
   "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>backend</th>\n",
       "      <th>type</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>biosphere3</th>\n",
       "      <td>sqlite</td>\n",
       "      <td>background</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ecoinvent 3.4</th>\n",
       "      <td>sqlite</td>\n",
       "      <td>background</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Foreground DB</th>\n",
       "      <td>sqlite</td>\n",
       "      <td>foreground</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              backend        type\n",
       "name                             \n",
       "biosphere3     sqlite  background\n",
       "ecoinvent 3.4  sqlite  background\n",
       "Foreground DB  sqlite  foreground"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "setForeground(USER_DB)\n",
    "list_databases()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-21T12:58:44.685305Z",
     "start_time": "2021-06-21T12:58:44.666551Z"
    }
   },
   "source": [
    "## Import / export\n",
    "\n",
    "`lca_algebraic` extends [BW2Package](https://2.docs.brightway.dev/technical/bw2io.html), adding persistence of parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:30.919145Z",
     "start_time": "2022-02-28T10:59:30.878740Z"
    }
   },
   "outputs": [],
   "source": [
    "# Save database and parameters as Bzipped JSON\n",
    "export_db(USER_DB, \"db.bw2\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:31.088288Z",
     "start_time": "2022-02-28T10:59:31.029712Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[ParamRegistry] Param b was already defined in 'Foreground DB' : overriding.\n",
      "[ParamRegistry] Param b was already defined in 'Foreground DB' : overriding.\n",
      "Variable 'b' was already defined : overidding it with param.\n",
      "Warning : param 'a' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param a was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param a was already defined in '<project>' : overriding.\n",
      "Variable 'a' was already defined : overidding it with param.\n",
      "Warning : param 'b' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param b was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param b was already defined in '<project>' : overriding.\n",
      "Variable 'b' was already defined : overidding it with param.\n",
      "Warning : param 'share_recycled_aluminium' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param share_recycled_aluminium was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param share_recycled_aluminium was already defined in '<project>' : overriding.\n",
      "Variable 'share_recycled_aluminium' was already defined : overidding it with param.\n",
      "Warning : param 'c' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param c was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param c was already defined in '<project>' : overriding.\n",
      "Variable 'c' was already defined : overidding it with param.\n",
      "Warning : param 'beta' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param beta was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param beta was already defined in '<project>' : overriding.\n",
      "Variable 'beta' was already defined : overidding it with param.\n",
      "Warning : param 'bool_param' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param bool_param was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param bool_param was already defined in '<project>' : overriding.\n",
      "Variable 'bool_param' was already defined : overidding it with param.\n",
      "Warning : param 'elec_switch_param' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param elec_switch_param was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param elec_switch_param was already defined in '<project>' : overriding.\n",
      "Variable 'elec_switch_param' was already defined : overidding it with param.\n",
      "Warning : param 'techno_param' linked to root project instead of a specific DB\n",
      "[ParamRegistry] Param techno_param was already defined in '<project>' : overriding.\n",
      "[ParamRegistry] Param techno_param was already defined in '<project>' : overriding.\n",
      "Variable 'techno_param' was already defined : overidding it with param.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Brightway2 SQLiteBackend: Foreground DB"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Reimport DB\n",
    "import_db(\"db.bw2\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Freeze \n",
    "\n",
    "A foreground database can be \"frozen\" to be used as a background database for a specific scenario : the parametrized amounts in the exhanges are computed for a given configuration of the parameters, and replaced by their value. The formulas are still stored in the database and not lost : the database can still be used as a foreground database until its status is changed with `setBackground(...)`.\n",
    "\n",
    "This feature is useful for studies requiring several datasets to be used as background by other ones. It also enables to use standard Brightway2 tools, not aware of parametrization. \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:31.417785Z",
     "start_time": "2022-02-28T10:59:31.384898Z"
    }
   },
   "outputs": [],
   "source": [
    "freezeParams(\n",
    "    USER_DB, # Name of database to freeze\n",
    "    \n",
    "    a=1, b=2) # custom parameter values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Get references to background activities\n",
    "\n",
    "We provide two functions for easy and fast (indexed) search of activities in reference databases : \n",
    "* **findBioAct** : Search activity in **biosphere3** db\n",
    "* **findTechAct** : Search activity in **ecoinvent** db\n",
    "\n",
    "Those methods are **faster** and **safer** than using traditionnal \"list-comprehension\" search : \n",
    "They will **fail with an error** if **more than one activity** matches, preventing the model to be based on a random selection of one activity.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:32.055591Z",
     "start_time": "2022-02-28T10:59:31.709726Z"
    }
   },
   "outputs": [],
   "source": [
    "# Biosphere activities\n",
    "ground_occupuation = findBioAct('Occupation, industrial area') # Search by name\n",
    "heat = findBioAct('Heat, waste', categories=['air']) # Add category selector\n",
    "\n",
    "# Technosphere activities\n",
    "\n",
    "# You can add an optionnal location selector\n",
    "alu = findTechAct(\"aluminium alloy production, AlMg3\", loc=\"RER\")\n",
    "alu_scrap = findTechAct('aluminium scrap, new, Recycled Content cut-off')\n",
    "\n",
    "# Elec \n",
    "eu_elec = findTechAct(\"market group for electricity, medium voltage\", 'ENTSO-E')\n",
    "us_elec = findTechAct(\"market group for electricity, medium voltage\", 'US')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Define the model\n",
    "\n",
    "The model is defined as a nested combination of background activities with amounts.\n",
    "\n",
    "Amounts are defined either as constant float values or algebric formulas implying the parameters defined above."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create new activities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:32.509113Z",
     "start_time": "2022-02-28T10:59:32.403391Z"
    }
   },
   "outputs": [],
   "source": [
    "# Create a new activity\n",
    "activity1 = newActivity(USER_DB, # We define foreground activities in our own DB\n",
    "    \"first foreground activity\", # Name of the activity\n",
    "    \"kg\", # Unit\n",
    "    exchanges= { # We define exhanges as a dictionarry of 'activity : amount'\n",
    "        ground_occupuation:3 * b, # Amount can be a fixed value \n",
    "        heat: b + 0.2  # Amount can be a Sympy expression (any arithmetic expression of Parameters)\n",
    "    })\n",
    "\n",
    "# You can create a virtual \"switch\" activity combining several activities with a switch parameter\n",
    "elec_mix = newSwitchAct(USER_DB, \n",
    "    \"elect mix\", # Name\n",
    "    elec_switch_param, # Sith parameter\n",
    "    { # Dictionnary of enum values / activities\n",
    "        \"us\" : us_elec, # By default associated amount is 1\n",
    "        \"eu\" : (eu_elec, 0.8)  # You can also provide custom amout or formula with a tuple \n",
    "    })"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Copy and update existing activity\n",
    "\n",
    "You can copy and update an existing background activity.\n",
    "\n",
    "Several new helper methods have been added to the class **Activity** for easy update of exchanges."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:32.981510Z",
     "start_time": "2022-02-28T10:59:32.793112Z"
    }
   },
   "outputs": [],
   "source": [
    "alu2 = copyActivity(\n",
    "    USER_DB, # The copy of a background activity is done in our own DB, so that we can safely update it                \n",
    "    alu, # Initial activity : won't be altered\n",
    "    \"Aluminium 2\") # New name\n",
    "\n",
    "# Update exchanges by their name \n",
    "alu2.updateExchanges({\n",
    "    \n",
    "    # Update amount : the special symbol *old_amount* references the previous amount of this exchange\n",
    "    \"aluminium, cast alloy\": old_amount * (1 - share_recycled_aluminium),\n",
    "    \n",
    "    # Update input activity. Note also that you can use '*' wildcard in exchange name\n",
    "    \"electricity*\": elec_mix,\n",
    "    \n",
    "    # Update both input activity and amount. \n",
    "    # Note that you can use '#' for specifying the location of exchange (useful for duplicate exchange names)\n",
    "    \"chromium#GLO\" : dict(amount=4.0, input=activity1)\n",
    "}) \n",
    "\n",
    "# Add exchanges \n",
    "alu2.addExchanges({alu_scrap :  12})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Final model\n",
    "\n",
    "Usually, we normalize the final model as the whole LCI divided by the functional value (production of the system)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:33.228394Z",
     "start_time": "2022-02-28T10:59:33.116684Z"
    }
   },
   "outputs": [],
   "source": [
    "functional_value = a + 5\n",
    "\n",
    "model = newActivity(USER_DB, \"model\", \"kg\", {\n",
    "    activity1 : b * 5 + a + 1, # Reference the activity we just created\n",
    "    alu2: 3 * share_recycled_aluminium, \n",
    "    alu:0.4 * a})\n",
    "\n",
    "normalized_model = newActivity(USER_DB, \"normalized model\", \"kg\", {\n",
    "    model : 1 / functional_value})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Or load existing model /activities from database\n",
    "\n",
    "Alternatively, you may not define the model again, but load it from the USER DB.\n",
    "We don't recommand it while developping the model as it may lead to unstable code / uncontrolled state of the DB."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:33.513640Z",
     "start_time": "2022-02-28T10:59:33.471764Z"
    }
   },
   "outputs": [],
   "source": [
    "activity1 = findActivity(\"first foreground activity\", db_name=USER_DB)\n",
    "model = findActivity(\"model\", db_name=USER_DB)\n",
    "normalized_model = findActivity(\"normalized model\", db_name=USER_DB)\n",
    "alu2 = findActivity(\"Aluminium 2\", db_name=USER_DB)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display activities\n",
    "\n",
    "**printAct** displays the list of all exchanges of an activity.\n",
    "\n",
    "Note that symbolic expressions have not been evaluated at this stage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:33.881417Z",
     "start_time": "2022-02-28T10:59:33.809903Z"
    }
   },
   "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",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">first foreground activity (1.000000 kg)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>input</th>\n",
       "      <th>amount</th>\n",
       "      <th>unit</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Heat, waste</th>\n",
       "      <td>Heat, waste</td>\n",
       "      <td>b + 0.2</td>\n",
       "      <td>megajoule</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Occupation, industrial area</th>\n",
       "      <td>Occupation, industrial area</td>\n",
       "      <td>3*b</td>\n",
       "      <td>square meter-year</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            first foreground activity (1.000000 kg)           \\\n",
       "                                                              input   amount   \n",
       "Heat, waste                                             Heat, waste  b + 0.2   \n",
       "Occupation, industrial area             Occupation, industrial area      3*b   \n",
       "\n",
       "                                                \n",
       "                                          unit  \n",
       "Heat, waste                          megajoule  \n",
       "Occupation, industrial area  square meter-year  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "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",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">model (1.000000 kg)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>input</th>\n",
       "      <th>amount</th>\n",
       "      <th>unit</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Aluminium 2</th>\n",
       "      <td>Aluminium 2[RER]{FG}</td>\n",
       "      <td>3*share_recycled_aluminium</td>\n",
       "      <td>kilogram</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>aluminium alloy production, AlMg3</th>\n",
       "      <td>aluminium alloy production, AlMg3[RER]</td>\n",
       "      <td>0.4*a</td>\n",
       "      <td>kilogram</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first foreground activity</th>\n",
       "      <td>first foreground activity{FG}</td>\n",
       "      <td>a + 5*b + 1</td>\n",
       "      <td>kg</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                      model (1.000000 kg)  \\\n",
       "                                                                    input   \n",
       "Aluminium 2                                          Aluminium 2[RER]{FG}   \n",
       "aluminium alloy production, AlMg3  aluminium alloy production, AlMg3[RER]   \n",
       "first foreground activity                   first foreground activity{FG}   \n",
       "\n",
       "                                                                         \n",
       "                                                       amount      unit  \n",
       "Aluminium 2                        3*share_recycled_aluminium  kilogram  \n",
       "aluminium alloy production, AlMg3                       0.4*a  kilogram  \n",
       "first foreground activity                         a + 5*b + 1        kg  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "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",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">normalized model (1.000000 kg)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>input</th>\n",
       "      <th>amount</th>\n",
       "      <th>unit</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>model</th>\n",
       "      <td>model{FG}</td>\n",
       "      <td>1/(a + 5)</td>\n",
       "      <td>kg</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      normalized model (1.000000 kg)                \n",
       "                               input     amount unit\n",
       "model                      model{FG}  1/(a + 5)   kg"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Print_act displays activities as tables\n",
    "printAct(activity1) \n",
    "printAct(model)\n",
    "printAct(normalized_model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:34.033075Z",
     "start_time": "2022-02-28T10:59:33.988554Z"
    }
   },
   "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",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">first foreground activity (1.000000 kg)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>input</th>\n",
       "      <th>amount</th>\n",
       "      <th>unit</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Heat, waste</th>\n",
       "      <td>Heat, waste</td>\n",
       "      <td>1.70000000000000</td>\n",
       "      <td>megajoule</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Occupation, industrial area</th>\n",
       "      <td>Occupation, industrial area</td>\n",
       "      <td>4.50000000000000</td>\n",
       "      <td>square meter-year</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            first foreground activity (1.000000 kg)  \\\n",
       "                                                              input   \n",
       "Heat, waste                                             Heat, waste   \n",
       "Occupation, industrial area             Occupation, industrial area   \n",
       "\n",
       "                                                                  \n",
       "                                       amount               unit  \n",
       "Heat, waste                  1.70000000000000          megajoule  \n",
       "Occupation, industrial area  4.50000000000000  square meter-year  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# You can also compute amounts by replacing parameters with a float value \n",
    "printAct(activity1, b=1.5) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:34.409956Z",
     "start_time": "2022-02-28T10:59:34.181688Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "    #T_83632470_9885_11ec_9ce4_c566243b363brow0_col0 {\n",
       "            background-color: yellow;\n",
       "        }    #T_83632470_9885_11ec_9ce4_c566243b363brow0_col1 {\n",
       "            background-color: yellow;\n",
       "        }    #T_83632470_9885_11ec_9ce4_c566243b363brow0_col3 {\n",
       "            background-color: yellow;\n",
       "        }    #T_83632470_9885_11ec_9ce4_c566243b363brow0_col4 {\n",
       "            background-color: yellow;\n",
       "        }    #T_83632470_9885_11ec_9ce4_c566243b363brow1_col1 {\n",
       "            background-color: yellow;\n",
       "        }    #T_83632470_9885_11ec_9ce4_c566243b363brow1_col4 {\n",
       "            background-color: yellow;\n",
       "        }    #T_83632470_9885_11ec_9ce4_c566243b363brow3_col0 {\n",
       "            background-color: yellow;\n",
       "        }    #T_83632470_9885_11ec_9ce4_c566243b363brow3_col1 {\n",
       "            background-color: yellow;\n",
       "        }    #T_83632470_9885_11ec_9ce4_c566243b363brow3_col3 {\n",
       "            background-color: yellow;\n",
       "        }    #T_83632470_9885_11ec_9ce4_c566243b363brow3_col4 {\n",
       "            background-color: yellow;\n",
       "        }    #T_83632470_9885_11ec_9ce4_c566243b363brow5_col0 {\n",
       "            background-color: yellow;\n",
       "        }    #T_83632470_9885_11ec_9ce4_c566243b363brow5_col3 {\n",
       "            background-color: yellow;\n",
       "        }</style><table id=\"T_83632470_9885_11ec_9ce4_c566243b363b\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" colspan=3>aluminium alloy production, AlMg3[RER] (1.000000 kilogram)</th>        <th class=\"col_heading level0 col3\" colspan=3>Aluminium 2[RER] (1.000000 kilogram)</th>    </tr>    <tr>        <th class=\"blank level1\" ></th>        <th class=\"col_heading level1 col0\" >input</th>        <th class=\"col_heading level1 col1\" >amount</th>        <th class=\"col_heading level1 col2\" >unit</th>        <th class=\"col_heading level1 col3\" >input</th>        <th class=\"col_heading level1 col4\" >amount</th>        <th class=\"col_heading level1 col5\" >unit</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_83632470_9885_11ec_9ce4_c566243b363blevel0_row0\" class=\"row_heading level0 row0\" >aluminium scrap, new, Recycled Content cut-off</th>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow0_col0\" class=\"data row0 col0\" >nan</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow0_col1\" class=\"data row0 col1\" >nan</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow0_col2\" class=\"data row0 col2\" >nan</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow0_col3\" class=\"data row0 col3\" >aluminium scrap, new, Recycled Content cut-off</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow0_col4\" class=\"data row0 col4\" >12</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow0_col5\" class=\"data row0 col5\" >kilogram</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_83632470_9885_11ec_9ce4_c566243b363blevel0_row1\" class=\"row_heading level0 row1\" >aluminium, cast alloy</th>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow1_col0\" class=\"data row1 col0\" >market for aluminium, cast alloy</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow1_col1\" class=\"data row1 col1\" >0.965000</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow1_col2\" class=\"data row1 col2\" >kilogram</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow1_col3\" class=\"data row1 col3\" >market for aluminium, cast alloy</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow1_col4\" class=\"data row1 col4\" >0.965 - 0.965*share_recycled_aluminium</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow1_col5\" class=\"data row1 col5\" >kilogram</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_83632470_9885_11ec_9ce4_c566243b363blevel0_row2\" class=\"row_heading level0 row2\" >cast iron</th>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow2_col0\" class=\"data row2 col0\" >market for cast iron</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow2_col1\" class=\"data row2 col1\" >0.004060</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow2_col2\" class=\"data row2 col2\" >kilogram</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow2_col3\" class=\"data row2 col3\" >market for cast iron</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow2_col4\" class=\"data row2 col4\" >0.004060</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow2_col5\" class=\"data row2 col5\" >kilogram</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_83632470_9885_11ec_9ce4_c566243b363blevel0_row3\" class=\"row_heading level0 row3\" >chromium</th>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow3_col0\" class=\"data row3 col0\" >market for chromium</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow3_col1\" class=\"data row3 col1\" >0.003050</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow3_col2\" class=\"data row3 col2\" >kilogram</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow3_col3\" class=\"data row3 col3\" >first foreground activity{FG}</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow3_col4\" class=\"data row3 col4\" >4.000000</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow3_col5\" class=\"data row3 col5\" >kg</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_83632470_9885_11ec_9ce4_c566243b363blevel0_row4\" class=\"row_heading level0 row4\" >copper</th>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow4_col0\" class=\"data row4 col0\" >market for copper</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow4_col1\" class=\"data row4 col1\" >0.001020</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow4_col2\" class=\"data row4 col2\" >kilogram</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow4_col3\" class=\"data row4 col3\" >market for copper</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow4_col4\" class=\"data row4 col4\" >0.001020</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow4_col5\" class=\"data row4 col5\" >kilogram</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_83632470_9885_11ec_9ce4_c566243b363blevel0_row5\" class=\"row_heading level0 row5\" >electricity, high voltage, aluminium industry</th>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow5_col0\" class=\"data row5 col0\" >market for electricity, high voltage, aluminium industry[IAI Area, EU27 & EFTA]</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow5_col1\" class=\"data row5 col1\" >1.590000</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow5_col2\" class=\"data row5 col2\" >kilowatt hour</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow5_col3\" class=\"data row5 col3\" >elect mix{FG}</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow5_col4\" class=\"data row5 col4\" >1.590000</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow5_col5\" class=\"data row5 col5\" >kilowatt hour</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_83632470_9885_11ec_9ce4_c566243b363blevel0_row6\" class=\"row_heading level0 row6\" >magnesium</th>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow6_col0\" class=\"data row6 col0\" >market for magnesium</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow6_col1\" class=\"data row6 col1\" >0.030500</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow6_col2\" class=\"data row6 col2\" >kilogram</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow6_col3\" class=\"data row6 col3\" >market for magnesium</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow6_col4\" class=\"data row6 col4\" >0.030500</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow6_col5\" class=\"data row6 col5\" >kilogram</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_83632470_9885_11ec_9ce4_c566243b363blevel0_row7\" class=\"row_heading level0 row7\" >manganese</th>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow7_col0\" class=\"data row7 col0\" >market for manganese</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow7_col1\" class=\"data row7 col1\" >0.005080</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow7_col2\" class=\"data row7 col2\" >kilogram</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow7_col3\" class=\"data row7 col3\" >market for manganese</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow7_col4\" class=\"data row7 col4\" >0.005080</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow7_col5\" class=\"data row7 col5\" >kilogram</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_83632470_9885_11ec_9ce4_c566243b363blevel0_row8\" class=\"row_heading level0 row8\" >silicon, metallurgical grade</th>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow8_col0\" class=\"data row8 col0\" >market for silicon, metallurgical grade</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow8_col1\" class=\"data row8 col1\" >0.004060</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow8_col2\" class=\"data row8 col2\" >kilogram</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow8_col3\" class=\"data row8 col3\" >market for silicon, metallurgical grade</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow8_col4\" class=\"data row8 col4\" >0.004060</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow8_col5\" class=\"data row8 col5\" >kilogram</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_83632470_9885_11ec_9ce4_c566243b363blevel0_row9\" class=\"row_heading level0 row9\" >zinc</th>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow9_col0\" class=\"data row9 col0\" >market for zinc</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow9_col1\" class=\"data row9 col1\" >0.002030</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow9_col2\" class=\"data row9 col2\" >kilogram</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow9_col3\" class=\"data row9 col3\" >market for zinc</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow9_col4\" class=\"data row9 col4\" >0.002030</td>\n",
       "                        <td id=\"T_83632470_9885_11ec_9ce4_c566243b363brow9_col5\" class=\"data row9 col5\" >kilogram</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7f145d7a3be0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# You print several activities at once to compare them\n",
    "printAct(alu, alu2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Select the impacts to consider"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:34.613732Z",
     "start_time": "2022-02-28T10:59:34.592520Z"
    }
   },
   "outputs": [],
   "source": [
    "# List of impacts to consider\n",
    "impacts = [m for m in bw.methods if 'ILCD 1.0.8 2016' in str(m) and 'no LT' in str(m)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:34.780073Z",
     "start_time": "2022-02-28T10:59:34.761830Z"
    }
   },
   "outputs": [],
   "source": [
    "# You can provide alternate names for some methods\n",
    "set_custom_impact_labels({\n",
    "    impacts[0] : 'Resource usage',\n",
    "    impacts[1]: 'Climate change'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Compute LCA\n",
    "\n",
    "We provide two methods  for computing LCA : \n",
    "* **multiLCA** : It uses **brightway2** native parametric support. It is **much slower** and kept for **comparing results**.\n",
    "* **multiLCAAlgebric** : It computes an algebric expression of the model and computes LCA once for all the background activities. Then it express each impact as a function of the parameters. This expression is then compiled into 'numpy' native code, for fast computation on vectors of samples. This version is 1 million time faster."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:35.728540Z",
     "start_time": "2022-02-28T10:59:35.123776Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Freezing 'model' (kg, GLO, None) // first foreground activity : a + 5*b + 1 => 4\n",
      "Freezing 'model' (kg, GLO, None) // Aluminium 2 : 3*share_recycled_aluminium => 1\n",
      "Freezing 'model' (kg, GLO, None) // aluminium alloy production, AlMg3 : 0.4*a => 0\n",
      "Freezing 'first foreground activity' (kg, GLO, None) // Occupation, industrial area : 3*b => 1\n",
      "Freezing 'first foreground activity' (kg, GLO, None) // Heat, waste : b + 0.2 => 0\n",
      "Freezing 'elect mix' (kilowatt hour, GLO, None) // market group for electricity, medium voltage : elec_switch_param_us => 1\n",
      "Freezing 'elect mix' (kilowatt hour, GLO, None) // market group for electricity, medium voltage : 0.8*elec_switch_param_eu => 0\n",
      "Freezing 'normalized model' (kg, GLO, None) // model : 1/(a + 5) => 0\n",
      "Freezing 'Aluminium 2' (kilogram, RER, None) // aluminium, cast alloy : 0.965 - 0.965*share_recycled_aluminium => 0\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>Resource usage</th>\n",
       "      <th>Climate change</th>\n",
       "      <th>ecosystem quality - freshwater and terrestrial acidification</th>\n",
       "      <th>ecosystem quality - freshwater ecotoxicity</th>\n",
       "      <th>ecosystem quality - freshwater eutrophication</th>\n",
       "      <th>ecosystem quality - ionising radiation</th>\n",
       "      <th>ecosystem quality - marine eutrophication</th>\n",
       "      <th>ecosystem quality - terrestrial eutrophication</th>\n",
       "      <th>human health - carcinogenic effects</th>\n",
       "      <th>human health - ionising radiation</th>\n",
       "      <th>human health - non-carcinogenic effects</th>\n",
       "      <th>human health - ozone layer depletion</th>\n",
       "      <th>human health - photochemical ozone creation</th>\n",
       "      <th>human health - respiratory effects, inorganics</th>\n",
       "      <th>resources - land use</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>normalized model</th>\n",
       "      <td>0.001918</td>\n",
       "      <td>1.313747</td>\n",
       "      <td>0.007555</td>\n",
       "      <td>1.172257</td>\n",
       "      <td>0.000092</td>\n",
       "      <td>3.406898e-07</td>\n",
       "      <td>0.001017</td>\n",
       "      <td>0.011743</td>\n",
       "      <td>5.694797e-08</td>\n",
       "      <td>0.04294</td>\n",
       "      <td>4.267009e-07</td>\n",
       "      <td>5.558090e-08</td>\n",
       "      <td>0.003601</td>\n",
       "      <td>0.001927</td>\n",
       "      <td>35.421118</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Resource usage  Climate change  \\\n",
       "normalized model        0.001918        1.313747   \n",
       "\n",
       "                  ecosystem quality - freshwater and terrestrial acidification  \\\n",
       "normalized model                                           0.007555              \n",
       "\n",
       "                  ecosystem quality - freshwater ecotoxicity  \\\n",
       "normalized model                                    1.172257   \n",
       "\n",
       "                  ecosystem quality - freshwater eutrophication  \\\n",
       "normalized model                                       0.000092   \n",
       "\n",
       "                  ecosystem quality - ionising radiation  \\\n",
       "normalized model                            3.406898e-07   \n",
       "\n",
       "                  ecosystem quality - marine eutrophication  \\\n",
       "normalized model                                   0.001017   \n",
       "\n",
       "                  ecosystem quality - terrestrial eutrophication  \\\n",
       "normalized model                                        0.011743   \n",
       "\n",
       "                  human health - carcinogenic effects  \\\n",
       "normalized model                         5.694797e-08   \n",
       "\n",
       "                  human health - ionising radiation  \\\n",
       "normalized model                            0.04294   \n",
       "\n",
       "                  human health - non-carcinogenic effects  \\\n",
       "normalized model                             4.267009e-07   \n",
       "\n",
       "                  human health - ozone layer depletion  \\\n",
       "normalized model                          5.558090e-08   \n",
       "\n",
       "                  human health - photochemical ozone creation  \\\n",
       "normalized model                                     0.003601   \n",
       "\n",
       "                  human health - respiratory effects, inorganics  \\\n",
       "normalized model                                        0.001927   \n",
       "\n",
       "                  resources - land use  \n",
       "normalized model             35.421118  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Uses brightway2 parameters\n",
    "multiLCA(\n",
    "    normalized_model, \n",
    "    impacts, \n",
    "                   \n",
    "    # Parameters of the model\n",
    "    a=1, \n",
    "    elec_switch_param=\"us\",\n",
    "    share_recycled_aluminium=0.4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:36.709224Z",
     "start_time": "2022-02-28T10:59:35.847690Z"
    }
   },
   "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>Resource usage[kg Sb-Eq]</th>\n",
       "      <th>Climate change[kg CO2-Eq]</th>\n",
       "      <th>ecosystem quality - freshwater and terrestrial acidification[mol H+-Eq]</th>\n",
       "      <th>ecosystem quality - freshwater ecotoxicity[CTUh.m3.yr]</th>\n",
       "      <th>ecosystem quality - freshwater eutrophication[kg P-Eq]</th>\n",
       "      <th>ecosystem quality - ionising radiation[mol N-Eq]</th>\n",
       "      <th>ecosystem quality - marine eutrophication[kg N-Eq]</th>\n",
       "      <th>ecosystem quality - terrestrial eutrophication[mol N-Eq]</th>\n",
       "      <th>human health - carcinogenic effects[CTUh]</th>\n",
       "      <th>human health - ionising radiation[kg U235-Eq]</th>\n",
       "      <th>human health - non-carcinogenic effects[CTUh]</th>\n",
       "      <th>human health - ozone layer depletion[kg CFC-11.]</th>\n",
       "      <th>human health - photochemical ozone creation[kg ethyle.]</th>\n",
       "      <th>human health - respiratory effects, inorganics[kg PM2.5-.]</th>\n",
       "      <th>resources - land use[kg Soil O.]</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>normalized model</th>\n",
       "      <td>0.001918</td>\n",
       "      <td>1.313747</td>\n",
       "      <td>0.007555</td>\n",
       "      <td>1.172257</td>\n",
       "      <td>0.000092</td>\n",
       "      <td>3.406898e-07</td>\n",
       "      <td>0.001017</td>\n",
       "      <td>0.011743</td>\n",
       "      <td>5.694796e-08</td>\n",
       "      <td>0.04294</td>\n",
       "      <td>4.267008e-07</td>\n",
       "      <td>5.558090e-08</td>\n",
       "      <td>0.003601</td>\n",
       "      <td>0.001927</td>\n",
       "      <td>35.421116</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Resource usage[kg Sb-Eq]  Climate change[kg CO2-Eq]  \\\n",
       "normalized model                  0.001918                   1.313747   \n",
       "\n",
       "                  ecosystem quality - freshwater and terrestrial acidification[mol H+-Eq]  \\\n",
       "normalized model                                           0.007555                         \n",
       "\n",
       "                  ecosystem quality - freshwater ecotoxicity[CTUh.m3.yr]  \\\n",
       "normalized model                                           1.172257        \n",
       "\n",
       "                  ecosystem quality - freshwater eutrophication[kg P-Eq]  \\\n",
       "normalized model                                           0.000092        \n",
       "\n",
       "                  ecosystem quality - ionising radiation[mol N-Eq]  \\\n",
       "normalized model                                      3.406898e-07   \n",
       "\n",
       "                  ecosystem quality - marine eutrophication[kg N-Eq]  \\\n",
       "normalized model                                           0.001017    \n",
       "\n",
       "                  ecosystem quality - terrestrial eutrophication[mol N-Eq]  \\\n",
       "normalized model                                           0.011743          \n",
       "\n",
       "                  human health - carcinogenic effects[CTUh]  \\\n",
       "normalized model                               5.694796e-08   \n",
       "\n",
       "                  human health - ionising radiation[kg U235-Eq]  \\\n",
       "normalized model                                        0.04294   \n",
       "\n",
       "                  human health - non-carcinogenic effects[CTUh]  \\\n",
       "normalized model                                   4.267008e-07   \n",
       "\n",
       "                  human health - ozone layer depletion[kg CFC-11.]  \\\n",
       "normalized model                                      5.558090e-08   \n",
       "\n",
       "                  human health - photochemical ozone creation[kg ethyle.]  \\\n",
       "normalized model                                           0.003601         \n",
       "\n",
       "                  human health - respiratory effects, inorganics[kg PM2.5-.]  \\\n",
       "normalized model                                           0.001927            \n",
       "\n",
       "                  resources - land use[kg Soil O.]  \n",
       "normalized model                         35.421116  "
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Compute with algebric implementation \n",
    "# The values should be the same as above\n",
    "multiLCAAlgebric(\n",
    "    normalized_model, # The model \n",
    "    impacts, # Impacts\n",
    "    \n",
    "    # Parameters of the model\n",
    "    a=1, \n",
    "    elec_switch_param=\"us\",\n",
    "    share_recycled_aluminium=0.4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:36.941173Z",
     "start_time": "2022-02-28T10:59:36.819746Z"
    }
   },
   "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>Resource usage[kg Sb-Eq]</th>\n",
       "      <th>Climate change[kg CO2-Eq]</th>\n",
       "      <th>ecosystem quality - freshwater and terrestrial acidification[mol H+-Eq]</th>\n",
       "      <th>ecosystem quality - freshwater ecotoxicity[CTUh.m3.yr]</th>\n",
       "      <th>ecosystem quality - freshwater eutrophication[kg P-Eq]</th>\n",
       "      <th>ecosystem quality - ionising radiation[mol N-Eq]</th>\n",
       "      <th>ecosystem quality - marine eutrophication[kg N-Eq]</th>\n",
       "      <th>ecosystem quality - terrestrial eutrophication[mol N-Eq]</th>\n",
       "      <th>human health - carcinogenic effects[CTUh]</th>\n",
       "      <th>human health - ionising radiation[kg U235-Eq]</th>\n",
       "      <th>human health - non-carcinogenic effects[CTUh]</th>\n",
       "      <th>human health - ozone layer depletion[kg CFC-11.]</th>\n",
       "      <th>human health - photochemical ozone creation[kg ethyle.]</th>\n",
       "      <th>human health - respiratory effects, inorganics[kg PM2.5-.]</th>\n",
       "      <th>resources - land use[kg Soil O.]</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>aluminium alloy production, AlMg3[RER]</th>\n",
       "      <td>0.010063</td>\n",
       "      <td>5.731382</td>\n",
       "      <td>0.035596</td>\n",
       "      <td>5.890408</td>\n",
       "      <td>0.000352</td>\n",
       "      <td>0.000001</td>\n",
       "      <td>0.004777</td>\n",
       "      <td>0.054339</td>\n",
       "      <td>2.961301e-07</td>\n",
       "      <td>0.144029</td>\n",
       "      <td>0.000002</td>\n",
       "      <td>2.358739e-07</td>\n",
       "      <td>0.016481</td>\n",
       "      <td>0.007688</td>\n",
       "      <td>4.544623</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Aluminium 2[RER]</th>\n",
       "      <td>0.007194</td>\n",
       "      <td>5.088309</td>\n",
       "      <td>0.028764</td>\n",
       "      <td>4.335949</td>\n",
       "      <td>0.000366</td>\n",
       "      <td>0.000001</td>\n",
       "      <td>0.003863</td>\n",
       "      <td>0.044837</td>\n",
       "      <td>2.035257e-07</td>\n",
       "      <td>0.172125</td>\n",
       "      <td>0.000002</td>\n",
       "      <td>2.138992e-07</td>\n",
       "      <td>0.013672</td>\n",
       "      <td>0.007482</td>\n",
       "      <td>92.712588</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        Resource usage[kg Sb-Eq]  \\\n",
       "aluminium alloy production, AlMg3[RER]                  0.010063   \n",
       "Aluminium 2[RER]                                        0.007194   \n",
       "\n",
       "                                        Climate change[kg CO2-Eq]  \\\n",
       "aluminium alloy production, AlMg3[RER]                   5.731382   \n",
       "Aluminium 2[RER]                                         5.088309   \n",
       "\n",
       "                                        ecosystem quality - freshwater and terrestrial acidification[mol H+-Eq]  \\\n",
       "aluminium alloy production, AlMg3[RER]                                           0.035596                         \n",
       "Aluminium 2[RER]                                                                 0.028764                         \n",
       "\n",
       "                                        ecosystem quality - freshwater ecotoxicity[CTUh.m3.yr]  \\\n",
       "aluminium alloy production, AlMg3[RER]                                           5.890408        \n",
       "Aluminium 2[RER]                                                                 4.335949        \n",
       "\n",
       "                                        ecosystem quality - freshwater eutrophication[kg P-Eq]  \\\n",
       "aluminium alloy production, AlMg3[RER]                                           0.000352        \n",
       "Aluminium 2[RER]                                                                 0.000366        \n",
       "\n",
       "                                        ecosystem quality - ionising radiation[mol N-Eq]  \\\n",
       "aluminium alloy production, AlMg3[RER]                                          0.000001   \n",
       "Aluminium 2[RER]                                                                0.000001   \n",
       "\n",
       "                                        ecosystem quality - marine eutrophication[kg N-Eq]  \\\n",
       "aluminium alloy production, AlMg3[RER]                                           0.004777    \n",
       "Aluminium 2[RER]                                                                 0.003863    \n",
       "\n",
       "                                        ecosystem quality - terrestrial eutrophication[mol N-Eq]  \\\n",
       "aluminium alloy production, AlMg3[RER]                                           0.054339          \n",
       "Aluminium 2[RER]                                                                 0.044837          \n",
       "\n",
       "                                        human health - carcinogenic effects[CTUh]  \\\n",
       "aluminium alloy production, AlMg3[RER]                               2.961301e-07   \n",
       "Aluminium 2[RER]                                                     2.035257e-07   \n",
       "\n",
       "                                        human health - ionising radiation[kg U235-Eq]  \\\n",
       "aluminium alloy production, AlMg3[RER]                                       0.144029   \n",
       "Aluminium 2[RER]                                                             0.172125   \n",
       "\n",
       "                                        human health - non-carcinogenic effects[CTUh]  \\\n",
       "aluminium alloy production, AlMg3[RER]                                       0.000002   \n",
       "Aluminium 2[RER]                                                             0.000002   \n",
       "\n",
       "                                        human health - ozone layer depletion[kg CFC-11.]  \\\n",
       "aluminium alloy production, AlMg3[RER]                                      2.358739e-07   \n",
       "Aluminium 2[RER]                                                            2.138992e-07   \n",
       "\n",
       "                                        human health - photochemical ozone creation[kg ethyle.]  \\\n",
       "aluminium alloy production, AlMg3[RER]                                           0.016481         \n",
       "Aluminium 2[RER]                                                                 0.013672         \n",
       "\n",
       "                                        human health - respiratory effects, inorganics[kg PM2.5-.]  \\\n",
       "aluminium alloy production, AlMg3[RER]                                           0.007688            \n",
       "Aluminium 2[RER]                                                                 0.007482            \n",
       "\n",
       "                                        resources - land use[kg Soil O.]  \n",
       "aluminium alloy production, AlMg3[RER]                          4.544623  \n",
       "Aluminium 2[RER]                                               92.712588  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# You can compute several LCAs at a time and compare them:\n",
    "multiLCAAlgebric(\n",
    "    [alu, alu2], # The models\n",
    "    \n",
    "    impacts, # Impacts\n",
    "    \n",
    "    # Parameters of the model\n",
    "    share_recycled_aluminium=0.3,\n",
    "    elec_switch_param=\"us\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Evaluate the contribution of a subset of activites to global impact\n",
    "\n",
    "You can extract the contribution of a subset of activites to the global impact of the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:37.188576Z",
     "start_time": "2022-02-28T10:59:37.056975Z"
    }
   },
   "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>Resource usage[kg Sb-Eq]</th>\n",
       "      <th>Climate change[kg CO2-Eq]</th>\n",
       "      <th>ecosystem quality - freshwater and terrestrial acidification[mol H+-Eq]</th>\n",
       "      <th>ecosystem quality - freshwater ecotoxicity[CTUh.m3.yr]</th>\n",
       "      <th>ecosystem quality - freshwater eutrophication[kg P-Eq]</th>\n",
       "      <th>ecosystem quality - ionising radiation[mol N-Eq]</th>\n",
       "      <th>ecosystem quality - marine eutrophication[kg N-Eq]</th>\n",
       "      <th>ecosystem quality - terrestrial eutrophication[mol N-Eq]</th>\n",
       "      <th>human health - carcinogenic effects[CTUh]</th>\n",
       "      <th>human health - ionising radiation[kg U235-Eq]</th>\n",
       "      <th>human health - non-carcinogenic effects[CTUh]</th>\n",
       "      <th>human health - ozone layer depletion[kg CFC-11.]</th>\n",
       "      <th>human health - photochemical ozone creation[kg ethyle.]</th>\n",
       "      <th>human health - respiratory effects, inorganics[kg PM2.5-.]</th>\n",
       "      <th>resources - land use[kg Soil O.]</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>normalized model</th>\n",
       "      <td>0.001247</td>\n",
       "      <td>0.931655</td>\n",
       "      <td>0.005182</td>\n",
       "      <td>0.779563</td>\n",
       "      <td>0.000069</td>\n",
       "      <td>2.604286e-07</td>\n",
       "      <td>0.000698</td>\n",
       "      <td>0.008121</td>\n",
       "      <td>3.720596e-08</td>\n",
       "      <td>0.033338</td>\n",
       "      <td>2.820458e-07</td>\n",
       "      <td>3.985597e-08</td>\n",
       "      <td>0.002503</td>\n",
       "      <td>0.001415</td>\n",
       "      <td>18.468141</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Resource usage[kg Sb-Eq]  Climate change[kg CO2-Eq]  \\\n",
       "normalized model                  0.001247                   0.931655   \n",
       "\n",
       "                  ecosystem quality - freshwater and terrestrial acidification[mol H+-Eq]  \\\n",
       "normalized model                                           0.005182                         \n",
       "\n",
       "                  ecosystem quality - freshwater ecotoxicity[CTUh.m3.yr]  \\\n",
       "normalized model                                           0.779563        \n",
       "\n",
       "                  ecosystem quality - freshwater eutrophication[kg P-Eq]  \\\n",
       "normalized model                                           0.000069        \n",
       "\n",
       "                  ecosystem quality - ionising radiation[mol N-Eq]  \\\n",
       "normalized model                                      2.604286e-07   \n",
       "\n",
       "                  ecosystem quality - marine eutrophication[kg N-Eq]  \\\n",
       "normalized model                                           0.000698    \n",
       "\n",
       "                  ecosystem quality - terrestrial eutrophication[mol N-Eq]  \\\n",
       "normalized model                                           0.008121          \n",
       "\n",
       "                  human health - carcinogenic effects[CTUh]  \\\n",
       "normalized model                               3.720596e-08   \n",
       "\n",
       "                  human health - ionising radiation[kg U235-Eq]  \\\n",
       "normalized model                                       0.033338   \n",
       "\n",
       "                  human health - non-carcinogenic effects[CTUh]  \\\n",
       "normalized model                                   2.820458e-07   \n",
       "\n",
       "                  human health - ozone layer depletion[kg CFC-11.]  \\\n",
       "normalized model                                      3.985597e-08   \n",
       "\n",
       "                  human health - photochemical ozone creation[kg ethyle.]  \\\n",
       "normalized model                                           0.002503         \n",
       "\n",
       "                  human health - respiratory effects, inorganics[kg PM2.5-.]  \\\n",
       "normalized model                                           0.001415            \n",
       "\n",
       "                  resources - land use[kg Soil O.]  \n",
       "normalized model                         18.468141  "
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Contribution of impact of aluminium\n",
    "multiLCAAlgebric(\n",
    "    normalized_model, # The model \n",
    "    impacts, # Impacts\n",
    "    \n",
    "    # List of sub activites to consider\n",
    "    extract_activities = [alu2],\n",
    "    \n",
    "    # Parameters of the model\n",
    "    a=1, \n",
    "    elec_switch_param=\"us\",\n",
    "    share_recycled_aluminium=0.4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fast computation of many parameter values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:41.615576Z",
     "start_time": "2022-02-28T10:59:40.485735Z"
    }
   },
   "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>Resource usage[kg Sb-Eq]</th>\n",
       "      <th>Climate change[kg CO2-Eq]</th>\n",
       "      <th>ecosystem quality - freshwater and terrestrial acidification[mol H+-Eq]</th>\n",
       "      <th>ecosystem quality - freshwater ecotoxicity[CTUh.m3.yr]</th>\n",
       "      <th>ecosystem quality - freshwater eutrophication[kg P-Eq]</th>\n",
       "      <th>ecosystem quality - ionising radiation[mol N-Eq]</th>\n",
       "      <th>ecosystem quality - marine eutrophication[kg N-Eq]</th>\n",
       "      <th>ecosystem quality - terrestrial eutrophication[mol N-Eq]</th>\n",
       "      <th>human health - carcinogenic effects[CTUh]</th>\n",
       "      <th>human health - ionising radiation[kg U235-Eq]</th>\n",
       "      <th>human health - non-carcinogenic effects[CTUh]</th>\n",
       "      <th>human health - ozone layer depletion[kg CFC-11.]</th>\n",
       "      <th>human health - photochemical ozone creation[kg ethyle.]</th>\n",
       "      <th>human health - respiratory effects, inorganics[kg PM2.5-.]</th>\n",
       "      <th>resources - land use[kg Soil O.]</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.005511</td>\n",
       "      <td>6.999776</td>\n",
       "      <td>0.039485</td>\n",
       "      <td>5.628570</td>\n",
       "      <td>0.000565</td>\n",
       "      <td>0.000003</td>\n",
       "      <td>0.005364</td>\n",
       "      <td>0.069420</td>\n",
       "      <td>3.499349e-07</td>\n",
       "      <td>0.341596</td>\n",
       "      <td>0.000001</td>\n",
       "      <td>3.922766e-07</td>\n",
       "      <td>0.021970</td>\n",
       "      <td>0.014016</td>\n",
       "      <td>3.716098e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.009536</td>\n",
       "      <td>9.292329</td>\n",
       "      <td>0.053724</td>\n",
       "      <td>7.984734</td>\n",
       "      <td>0.000706</td>\n",
       "      <td>0.000003</td>\n",
       "      <td>0.007275</td>\n",
       "      <td>0.091155</td>\n",
       "      <td>4.683870e-07</td>\n",
       "      <td>0.399208</td>\n",
       "      <td>0.000002</td>\n",
       "      <td>4.866262e-07</td>\n",
       "      <td>0.028563</td>\n",
       "      <td>0.017091</td>\n",
       "      <td>3.956277e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.013561</td>\n",
       "      <td>11.584882</td>\n",
       "      <td>0.067962</td>\n",
       "      <td>10.340897</td>\n",
       "      <td>0.000847</td>\n",
       "      <td>0.000004</td>\n",
       "      <td>0.009186</td>\n",
       "      <td>0.112891</td>\n",
       "      <td>5.868390e-07</td>\n",
       "      <td>0.456819</td>\n",
       "      <td>0.000003</td>\n",
       "      <td>5.809757e-07</td>\n",
       "      <td>0.035155</td>\n",
       "      <td>0.020167</td>\n",
       "      <td>4.196455e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.017586</td>\n",
       "      <td>13.877435</td>\n",
       "      <td>0.082200</td>\n",
       "      <td>12.697060</td>\n",
       "      <td>0.000988</td>\n",
       "      <td>0.000004</td>\n",
       "      <td>0.011096</td>\n",
       "      <td>0.134626</td>\n",
       "      <td>7.052910e-07</td>\n",
       "      <td>0.514431</td>\n",
       "      <td>0.000004</td>\n",
       "      <td>6.753253e-07</td>\n",
       "      <td>0.041748</td>\n",
       "      <td>0.023242</td>\n",
       "      <td>4.436634e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.021612</td>\n",
       "      <td>16.169988</td>\n",
       "      <td>0.096439</td>\n",
       "      <td>15.053223</td>\n",
       "      <td>0.001129</td>\n",
       "      <td>0.000005</td>\n",
       "      <td>0.013007</td>\n",
       "      <td>0.156362</td>\n",
       "      <td>8.237431e-07</td>\n",
       "      <td>0.572043</td>\n",
       "      <td>0.000005</td>\n",
       "      <td>7.696748e-07</td>\n",
       "      <td>0.048340</td>\n",
       "      <td>0.026318</td>\n",
       "      <td>4.676812e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99994</th>\n",
       "      <td>402.507202</td>\n",
       "      <td>229248.525310</td>\n",
       "      <td>1423.796676</td>\n",
       "      <td>235607.816832</td>\n",
       "      <td>14.083766</td>\n",
       "      <td>0.048157</td>\n",
       "      <td>191.055409</td>\n",
       "      <td>2173.484699</td>\n",
       "      <td>1.184484e-02</td>\n",
       "      <td>5761.164185</td>\n",
       "      <td>0.086789</td>\n",
       "      <td>9.434782e-03</td>\n",
       "      <td>659.233962</td>\n",
       "      <td>307.530314</td>\n",
       "      <td>2.402012e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99995</th>\n",
       "      <td>402.511228</td>\n",
       "      <td>229250.817863</td>\n",
       "      <td>1423.810914</td>\n",
       "      <td>235610.172995</td>\n",
       "      <td>14.083907</td>\n",
       "      <td>0.048157</td>\n",
       "      <td>191.057320</td>\n",
       "      <td>2173.506435</td>\n",
       "      <td>1.184496e-02</td>\n",
       "      <td>5761.221796</td>\n",
       "      <td>0.086790</td>\n",
       "      <td>9.434876e-03</td>\n",
       "      <td>659.240554</td>\n",
       "      <td>307.533390</td>\n",
       "      <td>2.402036e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99996</th>\n",
       "      <td>402.515253</td>\n",
       "      <td>229253.110416</td>\n",
       "      <td>1423.825153</td>\n",
       "      <td>235612.529159</td>\n",
       "      <td>14.084047</td>\n",
       "      <td>0.048158</td>\n",
       "      <td>191.059230</td>\n",
       "      <td>2173.528170</td>\n",
       "      <td>1.184508e-02</td>\n",
       "      <td>5761.279408</td>\n",
       "      <td>0.086791</td>\n",
       "      <td>9.434970e-03</td>\n",
       "      <td>659.247147</td>\n",
       "      <td>307.536465</td>\n",
       "      <td>2.402060e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99997</th>\n",
       "      <td>402.519278</td>\n",
       "      <td>229255.402968</td>\n",
       "      <td>1423.839391</td>\n",
       "      <td>235614.885322</td>\n",
       "      <td>14.084188</td>\n",
       "      <td>0.048158</td>\n",
       "      <td>191.061141</td>\n",
       "      <td>2173.549906</td>\n",
       "      <td>1.184520e-02</td>\n",
       "      <td>5761.337020</td>\n",
       "      <td>0.086792</td>\n",
       "      <td>9.435065e-03</td>\n",
       "      <td>659.253739</td>\n",
       "      <td>307.539540</td>\n",
       "      <td>2.402085e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99998</th>\n",
       "      <td>402.523303</td>\n",
       "      <td>229257.695521</td>\n",
       "      <td>1423.853629</td>\n",
       "      <td>235617.241485</td>\n",
       "      <td>14.084329</td>\n",
       "      <td>0.048159</td>\n",
       "      <td>191.063052</td>\n",
       "      <td>2173.571641</td>\n",
       "      <td>1.184532e-02</td>\n",
       "      <td>5761.394632</td>\n",
       "      <td>0.086793</td>\n",
       "      <td>9.435159e-03</td>\n",
       "      <td>659.260332</td>\n",
       "      <td>307.542616</td>\n",
       "      <td>2.402109e+06</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>99999 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       Resource usage[kg Sb-Eq]  Climate change[kg CO2-Eq]  \\\n",
       "0                      0.005511                   6.999776   \n",
       "1                      0.009536                   9.292329   \n",
       "2                      0.013561                  11.584882   \n",
       "3                      0.017586                  13.877435   \n",
       "4                      0.021612                  16.169988   \n",
       "...                         ...                        ...   \n",
       "99994                402.507202              229248.525310   \n",
       "99995                402.511228              229250.817863   \n",
       "99996                402.515253              229253.110416   \n",
       "99997                402.519278              229255.402968   \n",
       "99998                402.523303              229257.695521   \n",
       "\n",
       "       ecosystem quality - freshwater and terrestrial acidification[mol H+-Eq]  \\\n",
       "0                                               0.039485                         \n",
       "1                                               0.053724                         \n",
       "2                                               0.067962                         \n",
       "3                                               0.082200                         \n",
       "4                                               0.096439                         \n",
       "...                                                  ...                         \n",
       "99994                                        1423.796676                         \n",
       "99995                                        1423.810914                         \n",
       "99996                                        1423.825153                         \n",
       "99997                                        1423.839391                         \n",
       "99998                                        1423.853629                         \n",
       "\n",
       "       ecosystem quality - freshwater ecotoxicity[CTUh.m3.yr]  \\\n",
       "0                                               5.628570        \n",
       "1                                               7.984734        \n",
       "2                                              10.340897        \n",
       "3                                              12.697060        \n",
       "4                                              15.053223        \n",
       "...                                                  ...        \n",
       "99994                                      235607.816832        \n",
       "99995                                      235610.172995        \n",
       "99996                                      235612.529159        \n",
       "99997                                      235614.885322        \n",
       "99998                                      235617.241485        \n",
       "\n",
       "       ecosystem quality - freshwater eutrophication[kg P-Eq]  \\\n",
       "0                                               0.000565        \n",
       "1                                               0.000706        \n",
       "2                                               0.000847        \n",
       "3                                               0.000988        \n",
       "4                                               0.001129        \n",
       "...                                                  ...        \n",
       "99994                                          14.083766        \n",
       "99995                                          14.083907        \n",
       "99996                                          14.084047        \n",
       "99997                                          14.084188        \n",
       "99998                                          14.084329        \n",
       "\n",
       "       ecosystem quality - ionising radiation[mol N-Eq]  \\\n",
       "0                                              0.000003   \n",
       "1                                              0.000003   \n",
       "2                                              0.000004   \n",
       "3                                              0.000004   \n",
       "4                                              0.000005   \n",
       "...                                                 ...   \n",
       "99994                                          0.048157   \n",
       "99995                                          0.048157   \n",
       "99996                                          0.048158   \n",
       "99997                                          0.048158   \n",
       "99998                                          0.048159   \n",
       "\n",
       "       ecosystem quality - marine eutrophication[kg N-Eq]  \\\n",
       "0                                               0.005364    \n",
       "1                                               0.007275    \n",
       "2                                               0.009186    \n",
       "3                                               0.011096    \n",
       "4                                               0.013007    \n",
       "...                                                  ...    \n",
       "99994                                         191.055409    \n",
       "99995                                         191.057320    \n",
       "99996                                         191.059230    \n",
       "99997                                         191.061141    \n",
       "99998                                         191.063052    \n",
       "\n",
       "       ecosystem quality - terrestrial eutrophication[mol N-Eq]  \\\n",
       "0                                               0.069420          \n",
       "1                                               0.091155          \n",
       "2                                               0.112891          \n",
       "3                                               0.134626          \n",
       "4                                               0.156362          \n",
       "...                                                  ...          \n",
       "99994                                        2173.484699          \n",
       "99995                                        2173.506435          \n",
       "99996                                        2173.528170          \n",
       "99997                                        2173.549906          \n",
       "99998                                        2173.571641          \n",
       "\n",
       "       human health - carcinogenic effects[CTUh]  \\\n",
       "0                                   3.499349e-07   \n",
       "1                                   4.683870e-07   \n",
       "2                                   5.868390e-07   \n",
       "3                                   7.052910e-07   \n",
       "4                                   8.237431e-07   \n",
       "...                                          ...   \n",
       "99994                               1.184484e-02   \n",
       "99995                               1.184496e-02   \n",
       "99996                               1.184508e-02   \n",
       "99997                               1.184520e-02   \n",
       "99998                               1.184532e-02   \n",
       "\n",
       "       human health - ionising radiation[kg U235-Eq]  \\\n",
       "0                                           0.341596   \n",
       "1                                           0.399208   \n",
       "2                                           0.456819   \n",
       "3                                           0.514431   \n",
       "4                                           0.572043   \n",
       "...                                              ...   \n",
       "99994                                    5761.164185   \n",
       "99995                                    5761.221796   \n",
       "99996                                    5761.279408   \n",
       "99997                                    5761.337020   \n",
       "99998                                    5761.394632   \n",
       "\n",
       "       human health - non-carcinogenic effects[CTUh]  \\\n",
       "0                                           0.000001   \n",
       "1                                           0.000002   \n",
       "2                                           0.000003   \n",
       "3                                           0.000004   \n",
       "4                                           0.000005   \n",
       "...                                              ...   \n",
       "99994                                       0.086789   \n",
       "99995                                       0.086790   \n",
       "99996                                       0.086791   \n",
       "99997                                       0.086792   \n",
       "99998                                       0.086793   \n",
       "\n",
       "       human health - ozone layer depletion[kg CFC-11.]  \\\n",
       "0                                          3.922766e-07   \n",
       "1                                          4.866262e-07   \n",
       "2                                          5.809757e-07   \n",
       "3                                          6.753253e-07   \n",
       "4                                          7.696748e-07   \n",
       "...                                                 ...   \n",
       "99994                                      9.434782e-03   \n",
       "99995                                      9.434876e-03   \n",
       "99996                                      9.434970e-03   \n",
       "99997                                      9.435065e-03   \n",
       "99998                                      9.435159e-03   \n",
       "\n",
       "       human health - photochemical ozone creation[kg ethyle.]  \\\n",
       "0                                               0.021970         \n",
       "1                                               0.028563         \n",
       "2                                               0.035155         \n",
       "3                                               0.041748         \n",
       "4                                               0.048340         \n",
       "...                                                  ...         \n",
       "99994                                         659.233962         \n",
       "99995                                         659.240554         \n",
       "99996                                         659.247147         \n",
       "99997                                         659.253739         \n",
       "99998                                         659.260332         \n",
       "\n",
       "       human health - respiratory effects, inorganics[kg PM2.5-.]  \\\n",
       "0                                               0.014016            \n",
       "1                                               0.017091            \n",
       "2                                               0.020167            \n",
       "3                                               0.023242            \n",
       "4                                               0.026318            \n",
       "...                                                  ...            \n",
       "99994                                         307.530314            \n",
       "99995                                         307.533390            \n",
       "99996                                         307.536465            \n",
       "99997                                         307.539540            \n",
       "99998                                         307.542616            \n",
       "\n",
       "       resources - land use[kg Soil O.]  \n",
       "0                          3.716098e+02  \n",
       "1                          3.956277e+02  \n",
       "2                          4.196455e+02  \n",
       "3                          4.436634e+02  \n",
       "4                          4.676812e+02  \n",
       "...                                 ...  \n",
       "99994                      2.402012e+06  \n",
       "99995                      2.402036e+06  \n",
       "99996                      2.402060e+06  \n",
       "99997                      2.402085e+06  \n",
       "99998                      2.402109e+06  \n",
       "\n",
       "[99999 rows x 15 columns]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Fast computation for millions of separate samples\n",
    "multiLCAAlgebric(\n",
    "    model, # The model \n",
    "    impacts, # Impacts\n",
    "    \n",
    "    # Parameters of the model\n",
    "    a=list(range(1, 100000)), # All lists should have the same size\n",
    "    share_recycled_aluminium=1, # Those parameters are fixed\n",
    "    elec_switch_param=\"eu\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " # Statistic functions\n",
    " \n",
    " ## One at a time \n",
    " \n",
    " We provide several functions for computing **statistics** for **local variations** of parameters (one at a time).\n",
    " \n",
    " ### oat_matrix(model, impacts)\n",
    " \n",
    " Shows a **matrix of impacts x parameters** colored according to the variation of the impact in the bounds of the parameter.\n",
    "\n",
    " \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:42.375540Z",
     "start_time": "2022-02-28T10:59:41.725105Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 129448 characters]\n",
      "text/plain": [
       "<Figure size 1224x1224 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "oat_matrix(model, impacts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### oat_dashboard_matrix\n",
    "\n",
    "This functions draws a dashboard showing :\n",
    "* A dropdown list, for choosing a parameter\n",
    "* Several graphs of evolution of impacts for this parameter\n",
    "* Full table of data\n",
    "* A graph of \"bars\" representing the variation of each impact for this parameter (similar to the information given in oat_matrix) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:45.907998Z",
     "start_time": "2022-02-28T10:59:42.490763Z"
    }
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d90c5ad7ad7f4abebc09eb34ac959c2c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(Dropdown(description='param', options=('a', 'elec_switch_param', 'share_recycled_alumini…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "oat_dashboard_interact(\n",
    "    model, impacts, \n",
    "    \n",
    "    # Optionnal layout parameters\n",
    "    figspace=(0.5,0.5),\n",
    "    figsize=(15, 15),\n",
    "    sharex=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Monte-carlo methods & Sobol indices\n",
    "\n",
    "Here we leverage fast computation of monte-carlo approches. \n",
    "\n",
    "We compute **global sensivity analysis** (GSA).\n",
    "Not only local ones.\n",
    "\n",
    "\n",
    "### Sobol Matrix \n",
    "\n",
    "Similar to OAT matrix, we compute Sobol indices. they represent the ratio between the variance due to a given parameter and the total variance.\n",
    "\n",
    "for easier comparison, we translate those relative sobol indices into \"deviation / mean\" importance :\n",
    "\n",
    "$$RelativeDeviation = \\frac{\\sqrt{sobol(param) \\times totalVariance(impact))}}{mean(impact)}$$\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:47.887401Z",
     "start_time": "2022-02-28T10:59:46.033832Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Processing Sobol indices ...\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'resources', 'mineral, fossils and renewables')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'climate change', 'GWP 100a')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'freshwater and terrestrial acidification')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'freshwater ecotoxicity')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'freshwater eutrophication')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'ionising radiation')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'marine eutrophication')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'terrestrial eutrophication')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'carcinogenic effects')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'ionising radiation')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'non-carcinogenic effects')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'ozone layer depletion')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'photochemical ozone creation')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'respiratory effects, inorganics')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'resources', 'land use')\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e473dd9689a247de8e16c49fcb21f374",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(Dropdown(description='indice', options=('s1', 'st'), value='s1'), Dropdown(description='…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Show sobol indices \n",
    "incer_stochastic_matrix(model, impacts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  Graphs of impacts and their distribution\n",
    "\n",
    "We provide a dashboard showing **violin graphs** : the exact probabilistic distribution for each impact. Together with medians of the impacts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T10:59:56.523640Z",
     "start_time": "2022-02-28T10:59:48.026985Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generating samples ...\n",
      "Transforming samples ...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Param 'b' is marked as FIXED, but passed in parameters : ignored\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 173032 characters]\n",
      "text/plain": [
       "<Figure size 1080x1080 with 15 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "incer_stochastic_violin(\n",
    "    model, impacts,\n",
    "    \n",
    "    # Optionnal layout parameters\n",
    "    figspace=(0.5,0.5),\n",
    "    figsize=(15, 15),\n",
    "    sharex=True, \n",
    "    nb_cols=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T11:00:26.209611Z",
     "start_time": "2022-02-28T10:59:56.642498Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generating samples ...\n",
      "Transforming samples ...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Param 'b' is marked as FIXED, but passed in parameters : ignored\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>Resource usage [kg Sb-Eq / kWh]</th>\n",
       "      <th>Climate change [kg CO2-Eq / kWh]</th>\n",
       "      <th>ecosystem quality - freshwater and terrestrial acidification [mol H+-Eq / kWh]</th>\n",
       "      <th>ecosystem quality - freshwater ecotoxicity [CTUh.m3.yr / kWh]</th>\n",
       "      <th>ecosystem quality - freshwater eutrophication [kg P-Eq / kWh]</th>\n",
       "      <th>ecosystem quality - ionising radiation [mol N-Eq / kWh]</th>\n",
       "      <th>ecosystem quality - marine eutrophication [kg N-Eq / kWh]</th>\n",
       "      <th>ecosystem quality - terrestrial eutrophication [mol N-Eq / kWh]</th>\n",
       "      <th>human health - carcinogenic effects [CTUh / kWh]</th>\n",
       "      <th>human health - ionising radiation [kg U235-Eq / kWh]</th>\n",
       "      <th>human health - non-carcinogenic effects [CTUh / kWh]</th>\n",
       "      <th>human health - ozone layer depletion [kg CFC-11. / kWh]</th>\n",
       "      <th>human health - photochemical ozone creation [kg ethyle. / kWh]</th>\n",
       "      <th>human health - respiratory effects, inorganics [kg PM2.5-. / kWh]</th>\n",
       "      <th>resources - land use [kg Soil O. / kWh]</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>median</th>\n",
       "      <td>0.0103354</td>\n",
       "      <td>7.97502</td>\n",
       "      <td>0.0453042</td>\n",
       "      <td>6.83672</td>\n",
       "      <td>0.000575822</td>\n",
       "      <td>2.31096e-06</td>\n",
       "      <td>0.00610773</td>\n",
       "      <td>0.0729293</td>\n",
       "      <td>3.52286e-07</td>\n",
       "      <td>0.287978</td>\n",
       "      <td>2.35041e-06</td>\n",
       "      <td>3.67887e-07</td>\n",
       "      <td>0.0225549</td>\n",
       "      <td>0.0127728</td>\n",
       "      <td>264.043</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.00241918</td>\n",
       "      <td>1.38383</td>\n",
       "      <td>0.00761456</td>\n",
       "      <td>1.21397</td>\n",
       "      <td>0.000118535</td>\n",
       "      <td>5.9669e-07</td>\n",
       "      <td>0.00102499</td>\n",
       "      <td>0.0119723</td>\n",
       "      <td>6.14161e-08</td>\n",
       "      <td>0.0765116</td>\n",
       "      <td>4.93822e-07</td>\n",
       "      <td>6.49994e-08</td>\n",
       "      <td>0.00375232</td>\n",
       "      <td>0.00260405</td>\n",
       "      <td>54.4061</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>p</th>\n",
       "      <td>[0.006023220169773611, 0.0138676524576231]</td>\n",
       "      <td>[5.629625120064793, 10.091416619812927]</td>\n",
       "      <td>[0.03278971629930504, 0.057536041121099266]</td>\n",
       "      <td>[4.9111532591289295, 8.848234983057926]</td>\n",
       "      <td>[0.00038110769804725097, 0.0007659789826786883]</td>\n",
       "      <td>[1.343937531983165e-06, 3.322835916043601e-06]</td>\n",
       "      <td>[0.00443152724422405, 0.007749661422027176]</td>\n",
       "      <td>[0.05291725216881531, 0.09171051623461154]</td>\n",
       "      <td>[2.528757633977035e-07, 4.5361414282291875e-07]</td>\n",
       "      <td>[0.1667815682181064, 0.42092813970485343]</td>\n",
       "      <td>[1.5042667477112382e-06, 3.1040619445319275e-06]</td>\n",
       "      <td>[2.4586453589047684e-07, 4.579091863390413e-07]</td>\n",
       "      <td>[0.016084375823452412, 0.028314497585451168]</td>\n",
       "      <td>[0.008140025818130758, 0.016646070910413868]</td>\n",
       "      <td>[173.45101252530444, 352.3945801937672]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.0102566</td>\n",
       "      <td>7.92583</td>\n",
       "      <td>0.0454203</td>\n",
       "      <td>6.87331</td>\n",
       "      <td>0.000577044</td>\n",
       "      <td>2.31996e-06</td>\n",
       "      <td>0.00612111</td>\n",
       "      <td>0.0729498</td>\n",
       "      <td>3.53972e-07</td>\n",
       "      <td>0.290299</td>\n",
       "      <td>2.34524e-06</td>\n",
       "      <td>3.62399e-07</td>\n",
       "      <td>0.0225034</td>\n",
       "      <td>0.0126608</td>\n",
       "      <td>263.585</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>var</th>\n",
       "      <td>0.235865</td>\n",
       "      <td>0.174597</td>\n",
       "      <td>0.167647</td>\n",
       "      <td>0.176622</td>\n",
       "      <td>0.205418</td>\n",
       "      <td>0.257198</td>\n",
       "      <td>0.167452</td>\n",
       "      <td>0.164117</td>\n",
       "      <td>0.173505</td>\n",
       "      <td>0.263561</td>\n",
       "      <td>0.210564</td>\n",
       "      <td>0.179359</td>\n",
       "      <td>0.166744</td>\n",
       "      <td>0.205678</td>\n",
       "      <td>0.206408</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   Resource usage [kg Sb-Eq / kWh]  \\\n",
       "median                                   0.0103354   \n",
       "std                                     0.00241918   \n",
       "p       [0.006023220169773611, 0.0138676524576231]   \n",
       "mean                                     0.0102566   \n",
       "var                                       0.235865   \n",
       "\n",
       "               Climate change [kg CO2-Eq / kWh]  \\\n",
       "median                                  7.97502   \n",
       "std                                     1.38383   \n",
       "p       [5.629625120064793, 10.091416619812927]   \n",
       "mean                                    7.92583   \n",
       "var                                    0.174597   \n",
       "\n",
       "       ecosystem quality - freshwater and terrestrial acidification [mol H+-Eq / kWh]  \\\n",
       "median                                          0.0453042                               \n",
       "std                                            0.00761456                               \n",
       "p             [0.03278971629930504, 0.057536041121099266]                               \n",
       "mean                                            0.0454203                               \n",
       "var                                              0.167647                               \n",
       "\n",
       "       ecosystem quality - freshwater ecotoxicity [CTUh.m3.yr / kWh]  \\\n",
       "median                                            6.83672              \n",
       "std                                               1.21397              \n",
       "p                 [4.9111532591289295, 8.848234983057926]              \n",
       "mean                                              6.87331              \n",
       "var                                              0.176622              \n",
       "\n",
       "       ecosystem quality - freshwater eutrophication [kg P-Eq / kWh]  \\\n",
       "median                                        0.000575822              \n",
       "std                                           0.000118535              \n",
       "p         [0.00038110769804725097, 0.0007659789826786883]              \n",
       "mean                                          0.000577044              \n",
       "var                                              0.205418              \n",
       "\n",
       "       ecosystem quality - ionising radiation [mol N-Eq / kWh]  \\\n",
       "median                                        2.31096e-06        \n",
       "std                                            5.9669e-07        \n",
       "p          [1.343937531983165e-06, 3.322835916043601e-06]        \n",
       "mean                                          2.31996e-06        \n",
       "var                                              0.257198        \n",
       "\n",
       "       ecosystem quality - marine eutrophication [kg N-Eq / kWh]  \\\n",
       "median                                         0.00610773          \n",
       "std                                            0.00102499          \n",
       "p             [0.00443152724422405, 0.007749661422027176]          \n",
       "mean                                           0.00612111          \n",
       "var                                              0.167452          \n",
       "\n",
       "       ecosystem quality - terrestrial eutrophication [mol N-Eq / kWh]  \\\n",
       "median                                          0.0729293                \n",
       "std                                             0.0119723                \n",
       "p              [0.05291725216881531, 0.09171051623461154]                \n",
       "mean                                            0.0729498                \n",
       "var                                              0.164117                \n",
       "\n",
       "       human health - carcinogenic effects [CTUh / kWh]  \\\n",
       "median                                      3.52286e-07   \n",
       "std                                         6.14161e-08   \n",
       "p       [2.528757633977035e-07, 4.5361414282291875e-07]   \n",
       "mean                                        3.53972e-07   \n",
       "var                                            0.173505   \n",
       "\n",
       "       human health - ionising radiation [kg U235-Eq / kWh]  \\\n",
       "median                                           0.287978     \n",
       "std                                             0.0765116     \n",
       "p               [0.1667815682181064, 0.42092813970485343]     \n",
       "mean                                             0.290299     \n",
       "var                                              0.263561     \n",
       "\n",
       "       human health - non-carcinogenic effects [CTUh / kWh]  \\\n",
       "median                                        2.35041e-06     \n",
       "std                                           4.93822e-07     \n",
       "p        [1.5042667477112382e-06, 3.1040619445319275e-06]     \n",
       "mean                                          2.34524e-06     \n",
       "var                                              0.210564     \n",
       "\n",
       "       human health - ozone layer depletion [kg CFC-11. / kWh]  \\\n",
       "median                                        3.67887e-07        \n",
       "std                                           6.49994e-08        \n",
       "p         [2.4586453589047684e-07, 4.579091863390413e-07]        \n",
       "mean                                          3.62399e-07        \n",
       "var                                              0.179359        \n",
       "\n",
       "       human health - photochemical ozone creation [kg ethyle. / kWh]  \\\n",
       "median                                          0.0225549               \n",
       "std                                            0.00375232               \n",
       "p            [0.016084375823452412, 0.028314497585451168]               \n",
       "mean                                            0.0225034               \n",
       "var                                              0.166744               \n",
       "\n",
       "       human health - respiratory effects, inorganics [kg PM2.5-. / kWh]  \\\n",
       "median                                          0.0127728                  \n",
       "std                                            0.00260405                  \n",
       "p            [0.008140025818130758, 0.016646070910413868]                  \n",
       "mean                                            0.0126608                  \n",
       "var                                              0.205678                  \n",
       "\n",
       "        resources - land use [kg Soil O. / kWh]  \n",
       "median                                  264.043  \n",
       "std                                     54.4061  \n",
       "p       [173.45101252530444, 352.3945801937672]  \n",
       "mean                                    263.585  \n",
       "var                                    0.206408  "
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 254408 characters]\n",
      "text/plain": [
       "<Figure size 1080x4032 with 16 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "##### Alternatively, graphs can be shown horizontally, together with a box of statistical outcomes\n",
    "distrib(\n",
    "    model, impacts,\n",
    "    \n",
    "    # Optionnal layout parameters\n",
    "    height=7, width=15,\n",
    "    nb_cols=2,\n",
    "    percentiles=[5, 95])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Full dashboard\n",
    "\n",
    "A dashboard groups all this information in a single interface with tabs.\n",
    "\n",
    "It also shows total variation of impacts. This last graph could be improved by showing stacked colored bars with the contribution of each parameter to this variation, according to Sobol indices. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T11:04:00.201483Z",
     "start_time": "2022-02-28T11:03:46.642244Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generating samples ...\n",
      "Transforming samples ...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Param 'b' is marked as FIXED, but passed in parameters : ignored\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing Sobol indices ...\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'resources', 'mineral, fossils and renewables')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'climate change', 'GWP 100a')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'freshwater and terrestrial acidification')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'freshwater ecotoxicity')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'freshwater eutrophication')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'ionising radiation')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'marine eutrophication')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'terrestrial eutrophication')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'carcinogenic effects')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'ionising radiation')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'non-carcinogenic effects')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'ozone layer depletion')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'photochemical ozone creation')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'respiratory effects, inorganics')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'resources', 'land use')\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9cdff9d0de6946cab4fe87510b1377b4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Tab(children=(Output(), Output(), Output(), Output()), _titles={'0': 'Violin graphs', '1': 'Impact variations'…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "incer_stochastic_dashboard(model=model, methods=impacts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Producing simplified models \n",
    "\n",
    "One of te outcome of the statisticall analysis above would be to identify main input parameters and produce simplidied models, fixing the minor ones.\n",
    "\n",
    "We provide several functions for doing this.\n",
    "\n",
    "## Explore initial algrebraic model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T11:00:40.537543Z",
     "start_time": "2022-02-28T11:00:39.545886Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17352 characters]\n",
      "text/latex": [
       "$\\displaystyle \\frac{0.4 a aluminium_{alloy production AlMg3} + 3 share_{recycled aluminium} \\left(2.8 Heat_{waste} + 6.0 Occupation_{industrial area} + 12 aluminium_{scrap new Recycled Content cut off} + 1.272 elec_{switch param eu} market_{group for electricity medium voltage1} + 1.59 elec_{switch param us} market_{group for electricity medium voltage} + market_{for aluminium cast alloy} \\left(0.965 - 0.965 share_{recycled aluminium}\\right) + 0.00406 market_{for cast iron} + 0.00102 market_{for copper} + 0.0305 market_{for magnesium} + 0.00508 market_{for manganese} + 0.00406 market_{for silicon \\mathcal{metallurgi} grade} + 0.00203 market_{for zinc}\\right) + \\left(0.7 Heat_{waste} + 1.5 Occupation_{industrial area}\\right) \\left(a + 3.5\\right)}{a + 5}$"
      ],
      "text/plain": [
       "0.4⋅a⋅aluminium_alloy_production_AlMg3 + 3⋅share_recycled_aluminium⋅(2.8⋅Heat_\n",
       "──────────────────────────────────────────────────────────────────────────────\n",
       "                                                                              \n",
       "\n",
       "waste + 6.0⋅Occupation_industrial_area + 12⋅aluminium_scrap_new_Recycled_Conte\n",
       "──────────────────────────────────────────────────────────────────────────────\n",
       "                                                                              \n",
       "\n",
       "nt_cut_off + 1.272⋅elec_switch_param_eu⋅market_group_for_electricity_medium_vo\n",
       "──────────────────────────────────────────────────────────────────────────────\n",
       "                                                                              \n",
       "\n",
       "ltage1 + 1.59⋅elec_switch_param_us⋅market_group_for_electricity_medium_voltage\n",
       "──────────────────────────────────────────────────────────────────────────────\n",
       "                                                                              \n",
       "\n",
       " + market_for_aluminium_cast_alloy⋅(0.965 - 0.965⋅share_recycled_aluminium) + \n",
       "──────────────────────────────────────────────────────────────────────────────\n",
       "        a + 5                                                                 \n",
       "\n",
       "0.00406⋅market_for_cast_iron + 0.00102⋅market_for_copper + 0.0305⋅market_for_m\n",
       "──────────────────────────────────────────────────────────────────────────────\n",
       "                                                                              \n",
       "\n",
       "agnesium + 0.00508⋅market_for_manganese + 0.00406⋅market_for_silicon_metallurg\n",
       "──────────────────────────────────────────────────────────────────────────────\n",
       "                                                                              \n",
       "\n",
       "ical_grade + 0.00203⋅market_for_zinc) + (0.7⋅Heat_waste + 1.5⋅Occupation_indus\n",
       "──────────────────────────────────────────────────────────────────────────────\n",
       "                                                                              \n",
       "\n",
       "trial_area)⋅(a + 3.5)\n",
       "─────────────────────\n",
       "                     "
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# First, let's look at the full expression defining our model\n",
    "expr, _ = actToExpression(normalized_model)\n",
    "expr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compute simplified models\n",
    "\n",
    "We provide some method to automatically select a subset of parameters, based on the **sobol indices**, and then compute simplified models for it.\n",
    "\n",
    "We also round numerical expression to 3 digits, and we remove terms in sums that are less than 1% of total."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T11:01:56.296682Z",
     "start_time": "2022-02-28T11:00:40.788013Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generating samples ...\n",
      "Transforming samples ...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Param 'b' is marked as FIXED, but passed in parameters : ignored\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'resources', 'mineral, fossils and renewables')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'climate change', 'GWP 100a')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'freshwater and terrestrial acidification')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'freshwater ecotoxicity')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'freshwater eutrophication')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'ionising radiation')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'marine eutrophication')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'ecosystem quality', 'terrestrial eutrophication')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'carcinogenic effects')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'ionising radiation')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'non-carcinogenic effects')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'ozone layer depletion')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'photochemical ozone creation')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'human health', 'respiratory effects, inorganics')\n",
      "Processing sobol for ('ILCD 1.0.8 2016 midpoint no LT (obsolete)', 'resources', 'land use')\n",
      "> Method :  Resource usage\n",
      "S1:  1.0009128589530478\n",
      "S2:  -0.041998044710496125\n",
      "ST:  1.0025331500680812\n",
      "Selected params :  ['share_recycled_aluminium'] explains:  0.8126658675141357\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 6908 characters]\n",
      "text/latex": [
       "$\\displaystyle - 0.515 ShareRecycledAluminium \\left(0.00957 ShareRecycledAluminium - 0.0101\\right) + 0.000574$"
      ],
      "text/plain": [
       "-0.515⋅ShareRecycledAluminium⋅(0.00957⋅ShareRecycledAluminium - 0.0101) + 0.00\n",
       "0574"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  Climate change\n",
      "S1:  0.972312713265432\n",
      "S2:  0.03108774835824793\n",
      "ST:  1.0231853970256206\n",
      "Selected params :  ['share_recycled_aluminium', 'elec_switch_param'] explains:  0.8654475873696057\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 0.514 ShareRecycledAluminium \\left(- 4.3 ShareRecycledAluminium + \\begin{cases} 0.545 & \\text{for}\\: ElecSwitchParam = Eu \\\\1.05 & \\text{for}\\: ElecSwitchParam = Us \\end{cases} + 5.32\\right) + 0.329$"
      ],
      "text/plain": [
       "                             ⎛                              ⎛⎧0.545  for ElecS\n",
       "0.514⋅ShareRecycledAluminium⋅⎜-4.3⋅ShareRecycledAluminium + ⎜⎨                \n",
       "                             ⎝                              ⎝⎩1.05   for ElecS\n",
       "\n",
       "witchParam = Eu⎞       ⎞        \n",
       "               ⎟ + 5.32⎟ + 0.329\n",
       "witchParam = Us⎠       ⎠        "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  ecosystem quality - freshwater and terrestrial acidification\n",
      "S1:  0.9985199973685366\n",
      "S2:  -0.003961684013821801\n",
      "ST:  1.0060124601361182\n",
      "Selected params :  ['share_recycled_aluminium'] explains:  0.804302260197975\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 7188 characters]\n",
      "text/latex": [
       "$\\displaystyle - 0.517 ShareRecycledAluminium \\left(0.0285 ShareRecycledAluminium - 0.0371\\right) + 0.00198$"
      ],
      "text/plain": [
       "-0.517⋅ShareRecycledAluminium⋅(0.0285⋅ShareRecycledAluminium - 0.0371) + 0.001\n",
       "98"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  ecosystem quality - freshwater ecotoxicity\n",
      "S1:  0.9934658493878188\n",
      "S2:  -0.003591312844785751\n",
      "ST:  1.0077483400120841\n",
      "Selected params :  ['share_recycled_aluminium', 'a'] explains:  0.9637730841759999\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 4652 characters]\n",
      "text/latex": [
       "$\\displaystyle \\frac{2.36 A - 3 ShareRecycledAluminium \\left(4.38 ShareRecycledAluminium - 5.56\\right)}{A + 5}$"
      ],
      "text/plain": [
       "2.36⋅A - 3⋅ShareRecycledAluminium⋅(4.38⋅ShareRecycledAluminium - 5.56)\n",
       "──────────────────────────────────────────────────────────────────────\n",
       "                                A + 5                                 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  ecosystem quality - freshwater eutrophication\n",
      "S1:  0.9590186966001336\n",
      "S2:  0.04381108902431924\n",
      "ST:  1.0358599962347754\n",
      "Selected params :  ['share_recycled_aluminium', 'elec_switch_param'] explains:  0.9352353901303712\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 0.512 ShareRecycledAluminium \\left(- 0.000231 ShareRecycledAluminium + \\begin{cases} 6.2 \\cdot 10^{-5} & \\text{for}\\: ElecSwitchParam = Eu \\\\0.000125 & \\text{for}\\: ElecSwitchParam = Us \\end{cases} + 0.00031\\right) + 2.06 \\cdot 10^{-5}$"
      ],
      "text/plain": [
       "                             ⎛                                   ⎛⎧ 6.2e-5   f\n",
       "0.512⋅ShareRecycledAluminium⋅⎜-0.000231⋅ShareRecycledAluminium + ⎜⎨           \n",
       "                             ⎝                                   ⎝⎩0.000125  f\n",
       "\n",
       "or ElecSwitchParam = Eu⎞          ⎞          \n",
       "                       ⎟ + 0.00031⎟ + 2.06e-5\n",
       "or ElecSwitchParam = Us⎠          ⎠          "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  ecosystem quality - ionising radiation\n",
      "S1:  0.9719501235495673\n",
      "S2:  0.023411172155669863\n",
      "ST:  1.0258079512120246\n",
      "Selected params :  ['share_recycled_aluminium', 'elec_switch_param'] explains:  0.96834915124387\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 0.513 ShareRecycledAluminium \\left(- 4.24 \\cdot 10^{-7} ShareRecycledAluminium + \\begin{cases} 9.56 \\cdot 10^{-7} & \\text{for}\\: ElecSwitchParam = Us \\\\6.74 \\cdot 10^{-7} & \\text{for}\\: ElecSwitchParam = Eu \\end{cases} + 5.15 \\cdot 10^{-7}\\right) + 7.01 \\cdot 10^{-8}$"
      ],
      "text/plain": [
       "                             ⎛                                  ⎛⎧9.56e-7  for\n",
       "0.513⋅ShareRecycledAluminium⋅⎜-4.24e-7⋅ShareRecycledAluminium + ⎜⎨            \n",
       "                             ⎝                                  ⎝⎩6.74e-7  for\n",
       "\n",
       " ElecSwitchParam = Us⎞          ⎞          \n",
       "                     ⎟ + 5.15e-7⎟ + 7.01e-8\n",
       " ElecSwitchParam = Eu⎠          ⎠          "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  ecosystem quality - marine eutrophication\n",
      "S1:  0.9956186127058981\n",
      "S2:  0.0017684898355731851\n",
      "ST:  1.0070984838473842\n",
      "Selected params :  ['share_recycled_aluminium', 'a'] explains:  0.9817718380594843\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 5168 characters]\n",
      "text/latex": [
       "$\\displaystyle \\frac{0.00191 A - 3 ShareRecycledAluminium \\left(0.00372 ShareRecycledAluminium - 0.00493\\right)}{A + 5}$"
      ],
      "text/plain": [
       "0.00191⋅A - 3⋅ShareRecycledAluminium⋅(0.00372⋅ShareRecycledAluminium - 0.00493\n",
       "──────────────────────────────────────────────────────────────────────────────\n",
       "                                     A + 5                                    \n",
       "\n",
       ")\n",
       "─\n",
       " "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  ecosystem quality - terrestrial eutrophication\n",
      "S1:  1.0000010128657375\n",
      "S2:  0.0041065374598224765\n",
      "ST:  1.0074461051530517\n",
      "Selected params :  ['share_recycled_aluminium'] explains:  0.8392626918396056\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 7064 characters]\n",
      "text/latex": [
       "$\\displaystyle - 0.511 ShareRecycledAluminium \\left(0.0423 ShareRecycledAluminium - 0.0579\\right) + 0.00322$"
      ],
      "text/plain": [
       "-0.511⋅ShareRecycledAluminium⋅(0.0423⋅ShareRecycledAluminium - 0.0579) + 0.003\n",
       "22"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  human health - carcinogenic effects\n",
      "S1:  0.9967180536517019\n",
      "S2:  0.012943640305802447\n",
      "ST:  1.0063246049256778\n",
      "Selected params :  ['share_recycled_aluminium', 'a'] explains:  0.9913972081360208\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 5520 characters]\n",
      "text/latex": [
       "$\\displaystyle \\frac{1.18 \\cdot 10^{-7} A - 3 ShareRecycledAluminium \\left(1.75 \\cdot 10^{-7} ShareRecycledAluminium - 2.54 \\cdot 10^{-7}\\right)}{A + 5}$"
      ],
      "text/plain": [
       "1.18e-7⋅A - 3⋅ShareRecycledAluminium⋅(1.75e-7⋅ShareRecycledAluminium - 2.54e-7\n",
       "──────────────────────────────────────────────────────────────────────────────\n",
       "                                     A + 5                                    \n",
       "\n",
       ")\n",
       "─\n",
       " "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  human health - ionising radiation\n",
      "S1:  0.968002703037859\n",
      "S2:  0.026917695254938406\n",
      "ST:  1.0295738242024866\n",
      "Selected params :  ['share_recycled_aluminium', 'elec_switch_param'] explains:  0.9660071530774667\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 0.511 ShareRecycledAluminium \\left(- 0.0543 ShareRecycledAluminium + \\begin{cases} 0.0833 & \\text{for}\\: ElecSwitchParam = Eu \\\\0.123 & \\text{for}\\: ElecSwitchParam = Us \\end{cases} + 0.0657\\right) + 0.00857$"
      ],
      "text/plain": [
       "                             ⎛                                 ⎛⎧0.0833  for E\n",
       "0.511⋅ShareRecycledAluminium⋅⎜-0.0543⋅ShareRecycledAluminium + ⎜⎨             \n",
       "                             ⎝                                 ⎝⎩0.123   for E\n",
       "\n",
       "lecSwitchParam = Eu⎞         ⎞          \n",
       "                   ⎟ + 0.0657⎟ + 0.00857\n",
       "lecSwitchParam = Us⎠         ⎠          "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  human health - non-carcinogenic effects\n",
      "S1:  0.9999407005892093\n",
      "S2:  -0.03649544794672706\n",
      "ST:  1.0031652451011994\n",
      "Selected params :  ['share_recycled_aluminium', 'a'] explains:  0.9980749432725429\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 5640 characters]\n",
      "text/latex": [
       "$\\displaystyle \\frac{8.68 \\cdot 10^{-7} A - 3 ShareRecycledAluminium \\left(1.99 \\cdot 10^{-6} ShareRecycledAluminium - 2.2 \\cdot 10^{-6}\\right)}{A + 5}$"
      ],
      "text/plain": [
       "8.68e-7⋅A - 3⋅ShareRecycledAluminium⋅(1.99e-6⋅ShareRecycledAluminium - 2.2e-6)\n",
       "──────────────────────────────────────────────────────────────────────────────\n",
       "                                    A + 5                                     "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  human health - ozone layer depletion\n",
      "S1:  0.9895955907975253\n",
      "S2:  0.020451133809570317\n",
      "ST:  1.0098869449519126\n",
      "Selected params :  ['share_recycled_aluminium'] explains:  0.8975456733085345\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 7692 characters]\n",
      "text/latex": [
       "$\\displaystyle - 0.515 ShareRecycledAluminium \\left(1.46 \\cdot 10^{-7} ShareRecycledAluminium - 2.52 \\cdot 10^{-7}\\right) + 1.34 \\cdot 10^{-8}$"
      ],
      "text/plain": [
       "-0.515⋅ShareRecycledAluminium⋅(1.46e-7⋅ShareRecycledAluminium - 2.52e-7) + 1.3\n",
       "4e-8"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  human health - photochemical ozone creation\n",
      "S1:  0.9937754436413623\n",
      "S2:  0.015487693629801097\n",
      "ST:  1.0079847835078861\n",
      "Selected params :  ['share_recycled_aluminium'] explains:  0.8431908857999599\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 6736 characters]\n",
      "text/latex": [
       "$\\displaystyle - 0.515 ShareRecycledAluminium \\left(0.0116 ShareRecycledAluminium - 0.0169\\right) + 0.00093$"
      ],
      "text/plain": [
       "-0.515⋅ShareRecycledAluminium⋅(0.0116⋅ShareRecycledAluminium - 0.0169) + 0.000\n",
       "93"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  human health - respiratory effects, inorganics\n",
      "S1:  0.9771801054048728\n",
      "S2:  0.026122079648410276\n",
      "ST:  1.0200539975877807\n",
      "Selected params :  ['share_recycled_aluminium'] explains:  0.8132248808317574\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 7216 characters]\n",
      "text/latex": [
       "$\\displaystyle - 0.512 ShareRecycledAluminium \\left(0.00408 ShareRecycledAluminium - 0.00822\\right) + 0.000453$"
      ],
      "text/plain": [
       "-0.512⋅ShareRecycledAluminium⋅(0.00408⋅ShareRecycledAluminium - 0.00822) + 0.0\n",
       "00453"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> Method :  resources - land use\n",
      "S1:  0.9962502104818469\n",
      "S2:  -0.006349560607685791\n",
      "ST:  1.0036815580050527\n",
      "Selected params :  ['share_recycled_aluminium'] explains:  0.9740608386076699\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 7384 characters]\n",
      "text/latex": [
       "$\\displaystyle - 0.513 ShareRecycledAluminium \\left(3.72 ShareRecycledAluminium - 93.8\\right) + 16.8$"
      ],
      "text/plain": [
       "-0.513⋅ShareRecycledAluminium⋅(3.72⋅ShareRecycledAluminium - 93.8) + 16.8"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "simplified = sobol_simplify_model(\n",
    "    normalized_model, # The model\n",
    "    impacts, # Impacts to consider\n",
    "    n=10000, # For large model, you may test other value and ensure ST and sum(S1) are close to 1.0 \n",
    "    fixed_mode = FixedParamMode.MEDIAN, # We replace minor parameters by median by default,\n",
    "    min_ratio=0.8, # Min ratio of variability to explain\n",
    "\n",
    "    num_digits=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T11:02:41.691390Z",
     "start_time": "2022-02-28T11:02:41.450120Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 6016 characters]\n",
      "text/latex": [
       "$\\displaystyle - 0.515 share_{recycled aluminium} \\left(0.00957 share_{recycled aluminium} - 0.0101\\right) + 0.000574$"
      ],
      "text/plain": [
       "-0.515⋅share_recycled_aluminium⋅(0.00957⋅share_recycled_aluminium - 0.0101) + \n",
       "0.000574"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Let's look at the expression for first impact again \n",
    "# much simpler ! \n",
    "simplified[0].expr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compare simplified model with full model\n",
    "\n",
    "Finally, we can compare the distribution of those simplified model against the full model. We provide a function for graphical display of it, and compuation of de R-Square score.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-02-28T11:02:41.157971Z",
     "start_time": "2022-02-28T11:01:56.981988Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n",
      "Generating samples ...\n",
      "Transforming samples ...\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 434420 characters]\n",
      "text/plain": [
       "<Figure size 1080x5760 with 16 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "compare_simplified(normalized_model, impacts, simplified)"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "formats": "Rmd,ipynb",
   "notebook_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
