{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Getting Started with Brightway2\n",
    "\n",
    "This is an introduction to Brightway2, an open source framework for Life Cycle Assessment. This notebook will cover the basics of databases and activities and looking at LCI databases and LCIA methods.\n",
    "\n",
    "At the end of this notebook, you will be able to:\n",
    "\n",
    "* Import basic data like the biosphere database\n",
    "* Import and explore the [FORWAST](http://forwast.brgm.fr/) database\n",
    "\n",
    "If you finish the notebook, you get a kitten.\n",
    "\n",
    "This introduction is written in an Jupyter notebook, an online scientific notebook which combines, text, data, images, and programming. It is amazing, and could be a fantastic way to do and communicate advanced LCA work. See the [documentation](http://ipython.org/ipython-doc/dev/interactive/htmlnotebook.html) and a list of [awesome examples](https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks). Please see the [Brightway2 documentation](https://docs.brightwaylca.org) for a complete list of [example Brightway2 notebooks](https://docs.brightwaylca.org).\n",
    "\n",
    "You should **download this notebook** and run it cell by cell - don't just read it on the web!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Starting at the beginning\n",
    "\n",
    "Import brightway2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from brightway2 import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you get a warning like `Warning: bad escape \\s`, don't worry! You can just ignore it. It is coming from an imported dependency."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python 2 and 3\n",
    "\n",
    "Brightway2 is compatible with both Python 2 and 3. If you are using Python 2, I strongly recommend you execute the following cell, which will make all your text string unicode without have to type the letter ``\"u\"`` in front of each text string. On Python 3, the following **doesn't do anything** - your text strings are unicode by default.\n",
    "\n",
    "I won't include this cell in future notebooks, but you should copy and paste it if you will use Python 2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from __future__ import unicode_literals, print_function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Projects\n",
    "\n",
    "In Brightway2, a project is a separate directory with its own copies of LCI databases, LCIA methods, and any other data you use. Each research project or article should probably be its own project, so that any changes you want to make will not interfere with your other work.\n",
    "\n",
    "The default project is called ``default``:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "projects.current"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each project is stored in a separate directory in a place in your filesystem reserved for application data. It varies depending on the operating system; on OS X, this is the `Library` directory in your user home directory:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "projects.dir"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, you really need to care about the specifics. \n",
    "\n",
    "We can create a new project:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "projects.set_current(\"BW2 introduction\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And list the available projects:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "projects"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting basic data\n",
    "\n",
    "Let's import some basic data - a database of elementary flows, some LCIA methods, and some metadata used for importing other databases:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "bw2setup()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The iPython notebook by default prints all logged messages. On your machine, there might be messages tracking how long it took to download and process the biosphere and methods packages.\n",
    "\n",
    "## A biosphere dataset\n",
    "\n",
    "The ``biosphere3`` database was installed. It is called the ``biosphere3`` database because elementary flow names are normalized to the ecoinvent 3 standard.\n",
    "\n",
    "Let's see how many flows there are in this database, and then look at one random flow:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "db = Database(\"biosphere3\")\n",
    "print(\"Number of flows in `biosphere3`:\", len(db))\n",
    "random_flow = db.random()\n",
    "print(random_flow)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(random_flow['name'])\n",
    "print(random_flow['unit'])\n",
    "print(random_flow['categories'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Brightway2 uses keys to identify datasets. Each dataset is identified by a combination of its database and some unique code. The code can be anything - a number, a UUID, or just a name. All of the following would be valid keys:\n",
    "\n",
    "    (\"biosphere\", \"f66d00944691d54d6b072310b6f9de37\")\n",
    "    (\"my new database\", \"building my dream house\")\n",
    "    (\"skynet\", 14832)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "random_flow.key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## An LCIA method dataset\n",
    "\n",
    "We also installed a large number of LCIA methods:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "len(methods)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because LCIA methods have many different impact categories, they are identified not by a single label, but by a list of labels. Let's look at an example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "method_key = methods.random()\n",
    "method_key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this case, the LCIA method has three levels of specificity, from the general name (first level) to the specific impact category (last level). There is nothing magic about three levels - you could have one, or one hundred - but Brightway2 expects that LCIA methods will be a list of text labels ``('like', 'this')``.\n",
    "\n",
    "Note that method identifiers need to be a special kind of list that uses `()` instead of `[]`. These are called `tuples`. To create a tuple with only one element, you need to add a comma, to distinguish it from a set of parentheses:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print (1 + 2)           # This is not a tuple\n",
    "print (1,), type((1,))  # This is a tuple with one element"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can load the method data, show a sample.\n",
    "\n",
    "Method data has the format:\n",
    "\n",
    "    biosphere flow, numeric value, location\n",
    "\n",
    "Where:\n",
    "\n",
    "* `biosphere flow` is a dataset from any database which is used as a biosphere flow.\n",
    "* `numeric value` can be either a fixed number or an uncertainty distribution.\n",
    "* `location` is optional; the default value is that this characterization factor is valid everywhere.\n",
    "\n",
    "The method data format is pretty flexible, and the following are all acceptable:\n",
    "\n",
    "    [('biosphere', 'CO2'), 1.0],                                             # Default location\n",
    "    [('biosphere', 'CO2'), 1.0, 'Australia, mate!'],                         # Custom location\n",
    "    [('biosphere', 'CO2'), 1.0, ('Population density', 'Raster cell 4,2')],  # Location inside a geocollection\n",
    "    [('biosphere', 'CO2'), {'amount': 1.0, 'uncertainty type': 0}],          # Uncertain characterization factor\n",
    "\n",
    "[Geocollections](http://brightway2-regional.readthedocs.org/#spatial-scales-geocollections) are needed for regionalized LCA.\n",
    "\n",
    "If you are wondering why we need to identify biosphere flows like `('biosphere', '2fe885840cebfcc7d56b607b0acd9359')`, this is a good question! The short answer is that there is no single field that uniquely identifies biosphere flows or activities. The longer answer [is in the manual](https://docs.brightwaylca.org/intro.html#uniquely-identifying-activities).\n",
    "\n",
    "Brightway2 is designed to be flexible enough for many different problems. Therefore, there are no limits on what constitutes a biosphere flow. Rather, anything that is linked to in a biosphere exchange will be put in the biosphere matrix. We installed a database called `biosphere3`, but you can define new flows in a database alongside process datasets, or create your own biosphere database."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "method_data = Method(method_key).load()\n",
    "print(\"Number of CFs:\", len(method_data))\n",
    "method_data[:20]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Importing the FORWAST LCI database\n",
    "\n",
    "We will use the FORWAST database, as it is both a high quality, comprehensive LCI database, and freely available. [FORWAST](http://forwast.brgm.fr/Overview.asp) is a physical MRIO table for Europe. It can be downloaded directly from the [2.-0 website](http://lca-net.com/projects/show/forwast/).\n",
    "\n",
    "The following cell will download and install the FORWAST database. Note that an internet connection is required."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import zipfile\n",
    "import os\n",
    "from bw2data.utils import download_file\n",
    "\n",
    "filepath = download_file(\"forwast.bw2package.zip\", url=\"http://lca-net.com/wp-content/uploads/\")\n",
    "dirpath = os.path.dirname(filepath)\n",
    "zipfile.ZipFile(filepath).extractall(dirpath)\n",
    "BW2Package.import_file(os.path.join(dirpath, \"forwast.bw2package\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Searching datasets\n",
    "\n",
    "By default, every database is added to a search engine powered by [Whoosh](https://bitbucket.org/mchaput/whoosh/wiki/Home). Searching covers the following data fields:\n",
    "* `name`\n",
    "* `comment`\n",
    "* `product`\n",
    "* `categories`\n",
    "* `location`\n",
    "\n",
    "Searching is done by using the `Database.search` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Database(\"forwast\").search(\"food\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Searches can also be filtered (where only the results that meet the specified criteria are *included*) or masked (where results that meet the specified criteria are *excluded*)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default we return 25 search results, but this can be changed by specifying the `limit` keyword argument.\n",
    "\n",
    "You can also use `*` wild cards in search queries:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Database(\"biosphere3\").search(\"carb*\", limit=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can specify inclusion or exclusion criteria for fields with `filter` and `mask`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Database(\"biosphere3\").search(\"carbon\", filter={\"categories\": 'forestry'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Database(\"biosphere3\").search(\"carbon\", limit=10, mask={\"categories\": 'forestry'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, you can facet search results by another field. This is a bit complicated, so test your queries before assuming certain behaviour."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sr = Database(\"biosphere3\").search(\"carbon\", facet=\"categories\")\n",
    "for key, value in sr.items():\n",
    "    print(\"Facet term:\", key, \"\\n\\t\", value[:3], \"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Changing iteration order\n",
    "\n",
    "You can also change the way that processes are iterated over in the database. The default order is random:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "db = Database(\"forwast\")\n",
    "\n",
    "def print_10(db):\n",
    "    for i, x in enumerate(db):\n",
    "        if i < 10:\n",
    "            print(x)\n",
    "        else:\n",
    "            break\n",
    "            \n",
    "print_10(db)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print_10(db)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can sort by `location`, `name`, `product` (reference product), or `type`, by specifying the `order_by` property."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "db.order_by = \"name\"\n",
    "print_10(db)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If the above seems wrong, remember that the names start with `100`, `101`, etc.\n",
    "\n",
    "Set `.order_by = None` to remove any ordering."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "db.order_by = None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because accessing activities in the database is quite fast, you can also filter the activities you want by just iterating over the entire database:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "my_activities = [x for x in db if 'Electr' in x['name']]\n",
    "my_activities"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic LCA calculations\n",
    "\n",
    "Let's pick and random process and LCIA method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "process = Database(\"forwast\").random()\n",
    "process"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A brief review of LCA calculations:\n",
    "\n",
    "In matrix-based LCA, we construct a *technosphere* matrix, which describes the inputs needed to produce different products (e.g. cars need metal and electricity), and a *biosphere* matrix, which describes the emissions and resource consumption associated with the production of each product (e.g. car manufacturing releases air emissions). These two matrices come from the life cycle inventory database(s). We also have a *functional unit*, which is what we are trying to assess, e.g. one car. We then calculate the *life cycle inventory* (LCI) by first solving the linear system of the technosphere matrix and the functional unit, and then by multiplying the biosphere matrix.\n",
    "\n",
    "To do *life cycle impact assessment* (LCIA), we multiply the life cycle inventory by a matrix of characterization factors, which tell how bad different emissions and resource consumptions are.\n",
    "\n",
    "For more details on the math, see the [manual](https://docs.brightwaylca.org/lca.html).\n",
    "\n",
    "So, our first step is to specify the functional unit, which is relatively easy:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "functional_unit = {process: 1}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can then instantiate our [LCA object](https://docs.brightwaylca.org/technical/bw2calc.html#lca). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lca = LCA(functional_unit, method_key)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And do the LCI and LCIA calculations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lca.lci()\n",
    "lca.lcia()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we can print the LCA score:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lca.score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can reuse the same matrices but change the functional unit by using the `redo_lci` and `redo_lcia` functions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "new_process = Database(\"forwast\").random()\n",
    "print(new_process)\n",
    "lca.redo_lcia({new_process: 1})\n",
    "lca.score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Looking into the LCA object\n",
    "\n",
    "Let's see what is in this `LCA` thing, anyway. Put your cursor in the following cell and hit tab:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lca."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So, there is a lot. Let's look at a few things:\n",
    "\n",
    "* The technosphere matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lca.technosphere_matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* The biosphere matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lca.biosphere_matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* The characterization matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lca.characterization_matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Graphing matrices\n",
    "\n",
    "Sometimes it can be helpful to visualize both the inputs and calculation results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you get some warnings here, you can ignore them again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from bw2analyzer.matrix_grapher import SparseMatrixGrapher"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, let's look at the technosphere matrix.\n",
    "\n",
    "You can pass in a filename to get a higher resolution figure saved as a file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "SparseMatrixGrapher(lca.technosphere_matrix).ordered_graph()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Not so interesting - I am sure your inventory data will be much nicer. The problem is that this is an IO matrix, so there is a value at each point, even if it is small, and this graph only shows where values are or aren't zero. For example, here is the same graph for ecoinvent 3.2 (cutoff):\n",
    "\n",
    "<img src=\"https://brightwaylca.org/data/ecoinvent-32-cutoff.png\">\n",
    "\n",
    "You can also graph the biosphere and any other LCA matrices."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Contribution analysis\n",
    "\n",
    "We can calculate the most important activities and biosphere flows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from bw2analyzer import ContributionAnalysis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Most important activities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ContributionAnalysis().annotated_top_processes(lca)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Most important biosphere flows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ContributionAnalysis().annotated_top_emissions(lca)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Monte Carlo LCA\n",
    "\n",
    "Unfortunately, the forwast database doesn't unclude uncertainty. Let's put some in anyways, using the utility function [uncertainify](https://docs.brightwaylca.org/technical/bw2data.html#bw2data.utils.uncertainify)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from bw2data.utils import uncertainify\n",
    "from stats_arrays import NormalUncertainty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "uncertain_db = Database(\"forwast uncertain +\")\n",
    "uncertain_db.write(\n",
    "    uncertain_db.relabel_data(\n",
    "        uncertainify(\n",
    "            Database(\"forwast\").load(), \n",
    "            NormalUncertainty\n",
    "        ), \n",
    "        \"forwast uncertain +\" \n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now calculate some Monte Carlo iterations for a random activity."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "mc = MonteCarloLCA(demand={uncertain_db.random(): 1}, method=method_key)\n",
    "mc.load_data()\n",
    "for x in range(10):\n",
    "    print(next(mc))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That't it! Here is your promised kitten:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from IPython.display import Image\n",
    "import random\n",
    "dimensions = sorted((int(random.random() * 600 + 200), int(random.random() * 600 + 200)))\n",
    "Image(url=\"http://placekitten.com/{}/{}/\".format(*dimensions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
