{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This notebook shows how the flexible data model of Brightway2 can be used to tag inventory datasets, and group tags for analysis."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from brightway2 import *"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Create a copy of ecoinvent, so we don't mess up the original data."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ei = Database(\"ecoinvent 2.2\").copy(\"ecoinvent-tagged\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Each process now belongs to ``ecoinvent-tagged``, not ``ecoinvent 2.2``. Exchanges are also corrected to the new database name."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ei = Database(\"ecoinvent-tagged\")\n",
      "key = ei.random()\n",
      "print key\n",
      "data = ei.load()\n",
      "\n",
      "data[key]['exchanges'][0]['input']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "('ecoinvent-tagged', u'4f5eff40bca88c78c22972bf11d4a416')\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 10,
       "text": [
        "('ecoinvent-tagged', u'4e077a06306bed3e89cfccd0eaf1751e')"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "In this example, we assign tags at random. Presumably you would be a bit more systematic..."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import random\n",
      "\n",
      "tag_set =  {\"red\", \"orange\", \"yellow\", \"green\", \"blue\", \"indigo\", \"violet\"}\n",
      "tag_generator = lambda : random.choice(tag_set)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for value in data.values():\n",
      "    value['tag'] = tag_generator()\n",
      "\n",
      "ei.write(data)\n",
      "ei.process()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 10
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Calculate LCA results"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "lca = LCA({key: 1}, method=methods.random())\n",
      "lca.lci()\n",
      "lca.decompose_technosphere()\n",
      "lca.lcia()\n",
      "lca.fix_dictionaries()\n",
      "print lca.score"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0.132887557703\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Group results by tag"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "results = []\n",
      "original_process = key\n",
      "\n",
      "for tag in tag_set:\n",
      "    activities = {key for key, value in data.iteritems() if value['tag'] == tag and key != original_process}\n",
      "    score_from_tag = sum([lca.characterized_inventory[:, lca.technosphere_dict[key]].sum() for key in activities])\n",
      "    results.append((tag, score_from_tag))\n",
      "\n",
      "results.sort(key=lambda x: x[1], reverse=True)\n",
      "results"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 15,
       "text": [
        "[('red', 0.079913446611423175),\n",
        " ('green', 0.019448595270122424),\n",
        " ('indigo', 0.01917933005324966),\n",
        " ('orange', 0.01554348468269242),\n",
        " ('blue', 0.010834937916882512),\n",
        " ('yellow', 0.0085484454034305147),\n",
        " ('violet', 0.0033658880928293076)]"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "See also [graph traversal](https://brightway2-calc.readthedocs.org/en/latest/graph_traversal.html) for how to calculate impacts down the supply chain"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}