{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting started\n",
    "\n",
    "Import Brightway2 functionality"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from brightway2 import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create new project"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "projects.current = \"find-multioutput-processes\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Install base data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Creating default biosphere\n",
      "\n",
      "Applying strategy: drop_unspecified_subcategories"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Writing activities to SQLite3 database:\n",
      "0%                          100%\n",
      "[##############################] | ETA[sec]: 0.000 \n",
      "Total time elapsed: 0.362 sec\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Title: Writing activities to SQLite3 database:\n",
      "  Started: 08/25/2015 10:41:24\n",
      "  Finished: 08/25/2015 10:41:24\n",
      "  Total time elapsed: 0.362 sec\n",
      "  CPU %: 94.100000\n",
      "  Memory %: 0.262725\n",
      "Created database: biosphere3\n",
      "Creating default LCIA methods\n",
      "\n",
      "Applying strategy: set_biosphere_type\n",
      "Applying strategy: drop_unspecified_subcategories\n",
      "Applying strategy: link_iterable_by_fields\n",
      "Wrote 692 LCIA methods with 170915 characterization factors\n",
      "Creating core data migrations\n",
      "\n"
     ]
    }
   ],
   "source": [
    "bw2setup()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Importing ecoinvent 3.1\n",
    "\n",
    "Import ecoinvent 3.1 cutoff, but don't do any data manipulation yet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Extracting ecospold2 files:\n",
      "0%                          100%\n",
      "[##############################] | ETA[sec]: 0.000 | Item ID: fff527b1-0fe4-4\n",
      "Total time elapsed: 84.606 sec\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Extracting ecospold2 files:\n",
      "  Started: 08/25/2015 10:42:13\n",
      "  Finished: 08/25/2015 10:43:37\n",
      "  Total time elapsed: 84.606 sec\n",
      "  CPU %: 82.800000\n",
      "  Memory %: 2.945280\n",
      "Extracted 11301 datasets in 84.80 seconds\n"
     ]
    }
   ],
   "source": [
    "ei31cutoff = SingleOutputEcospold2Importer(\n",
    "    \"/Users/cmutel/Documents/LCA Documents/Ecoinvent/3.1/cutoff/datasets\",\n",
    "    \"ecoinvent 3.1 cutoff\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get multioutput datasets\n",
    "\n",
    "It is easy to find multioutput datasets, as the allocated datasets include all products, but set the non-reference product flow amounts to zero."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(11301, 1836)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "multioutput = [x for x in ei31cutoff.data if len([y for y in x['exchanges'] if y['type'] == 'production']) > 1]\n",
    "len(ei31cutoff.data), len(multioutput)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Examine a single multioutput dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "treatment of coal gas, in power plant\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'activity': None,\n",
       "  'amount': 1.0,\n",
       "  'comment': 'Calculated value. 1kWh = 3.6 MJ',\n",
       "  'flow': '66c93e71-f32b-4591-901c-55395db5c132',\n",
       "  'loc': 1.0,\n",
       "  'name': 'electricity, high voltage',\n",
       "  'production volume': 835000000.0,\n",
       "  'type': 'production',\n",
       "  'uncertainty type': 0,\n",
       "  'unit': 'kilowatt hour'},\n",
       " {'activity': None,\n",
       "  'amount': 0.0,\n",
       "  'comment': 'Calculated value',\n",
       "  'flow': '71e2f1db-a2c5-44d0-8337-dfff15be974d',\n",
       "  'loc': 0.0,\n",
       "  'name': 'heat, district or industrial, other than natural gas',\n",
       "  'production volume': 1220924369.7479,\n",
       "  'type': 'production',\n",
       "  'uncertainty type': 0,\n",
       "  'unit': 'megajoule'}]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "example = multioutput[1226]\n",
    "print(example['name'])\n",
    "[exc for exc in example['exchanges'] if exc['type'] == 'production']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also find the \"partner\" dataset - the production of heat instead of electricity - for our example dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'activity': None,\n",
       "  'amount': 0.0,\n",
       "  'comment': 'Calculated value. 1kWh = 3.6 MJ',\n",
       "  'flow': '66c93e71-f32b-4591-901c-55395db5c132',\n",
       "  'loc': 0.0,\n",
       "  'name': 'electricity, high voltage',\n",
       "  'production volume': 835000000.0,\n",
       "  'type': 'production',\n",
       "  'uncertainty type': 0,\n",
       "  'unit': 'kilowatt hour'},\n",
       " {'activity': None,\n",
       "  'amount': 1.0,\n",
       "  'comment': 'Calculated value',\n",
       "  'flow': '71e2f1db-a2c5-44d0-8337-dfff15be974d',\n",
       "  'loc': 1.0,\n",
       "  'name': 'heat, district or industrial, other than natural gas',\n",
       "  'production volume': 1220924369.7479,\n",
       "  'type': 'production',\n",
       "  'uncertainty type': 0,\n",
       "  'unit': 'megajoule'}]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partner = [ds for ds in multioutput if ds['activity'] == example['activity'] and ds != example]\n",
    "assert len(partner) == 1\n",
    "partner = partner[0]\n",
    "[exc for exc in partner['exchanges'] if exc['type'] == 'production']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We don't know anything about the allocation procedure applied, but we can use the production volumes to reverse the allocation! This was we don't need access to all the master data (which isn't available for download in any case)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Wait a minute...\n",
    "\n",
    "However, out list of processes isn't exactly correct, as not all multioutput processes have datasets that produce all possible reference products. Let's get only the multioutput processes where we have access to both (or more) allocated products."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1489"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count_dict = {}\n",
    "for ds in multioutput:\n",
    "    count_dict[ds['activity']] = count_dict.get(ds['activity'], 0) + 1\n",
    "\n",
    "true_multioutput = [ds for ds in multioutput if count_dict[ds['activity']] > 1]\n",
    "\n",
    "len(true_multioutput)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So, there are 1489 allocated datasets for which we have both (or more) allocated reference products. How many activities are there?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "615"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len({x for x, y in count_dict.items() if y > 1})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Where can we apply substitution?\n",
    "\n",
    "We can only apply substitution if another process produces the \"byproduct\" flow as its reference product - the flow, unit, and location have to match to do this automatically."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'activity': None,\n",
       "  'amount': 0.0,\n",
       "  'comment': 'Calculated value',\n",
       "  'flow': '71e2f1db-a2c5-44d0-8337-dfff15be974d',\n",
       "  'loc': 0.0,\n",
       "  'name': 'heat, district or industrial, other than natural gas',\n",
       "  'production volume': 4386554.62184873,\n",
       "  'type': 'production',\n",
       "  'uncertainty type': 0,\n",
       "  'unit': 'megajoule'},\n",
       " {'activity': None,\n",
       "  'amount': 1.0,\n",
       "  'comment': 'Calculated value. 1kWh = 3.6 MJ',\n",
       "  'flow': '66c93e71-f32b-4591-901c-55395db5c132',\n",
       "  'loc': 1.0,\n",
       "  'name': 'electricity, high voltage',\n",
       "  'production volume': 3000000.0,\n",
       "  'type': 'production',\n",
       "  'uncertainty type': 0,\n",
       "  'unit': 'kilowatt hour'}]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[y for y in [\n",
    "    x for x in true_multioutput if x['name'] == 'treatment of coal gas, in power plant'\n",
    "][0]['exchanges'] if y['type'] == 'production']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1489 618\n"
     ]
    }
   ],
   "source": [
    "can_be_substituted = 0\n",
    "\n",
    "for ds in true_multioutput:\n",
    "    ref_prod_list = [exc for exc in ds['exchanges'] if exc['amount'] and exc['type'] == 'production']\n",
    "    assert len(ref_prod_list) == 1\n",
    "    ref_prod = ref_prod_list[0]\n",
    "    \n",
    "    if [x for x in ei31cutoff.data \n",
    "            if x != ds\n",
    "            and x['location'] == ds['location']\n",
    "            and x['name'] == 'market for ' + ref_prod['name']\n",
    "            ]:\n",
    "        can_be_substituted += 1\n",
    "\n",
    "print(len(true_multioutput), can_be_substituted)"
   ]
  },
  {
   "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
