{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Activities and Exchanges\n",
    "\n",
    "This notebook shows how to use the `Activity` and `Exchange` classes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from brightway2 import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's create a new project just for this notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "projects.set_current(\"cats and dogs\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's start with a new database:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "db = Database(\"a&e\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and insert some basic data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Writing activities to SQLite3 database:\n",
      "0%  100%\n",
      "[####] | ETA[sec]: 0.000 \n",
      "Total time elapsed: 0.026 sec\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Writing activities to SQLite3 database:\n",
      "  Started: 07/15/2016 11:50:08\n",
      "  Finished: 07/15/2016 11:50:08\n",
      "  Total time elapsed: 0.026 sec\n",
      "  CPU %: 113.900000\n",
      "  Memory %: 0.349951\n"
     ]
    }
   ],
   "source": [
    "db.write({\n",
    "    (\"a&e\", \"cat\"): {\n",
    "        'name': 'cat',\n",
    "        'unit': 'kilogram',\n",
    "        'color': 'black',  # Custom field - you can add whatever fields you need\n",
    "        'exchanges': [{\n",
    "            'input': ('a&e', 'cat food'),\n",
    "            'amount': 10,\n",
    "            'type': 'technosphere'\n",
    "        }, {\n",
    "            'input': ('a&e', 'kitty litter'),\n",
    "            'amount': 10,\n",
    "            'type': 'technosphere'            \n",
    "        }, {\n",
    "            'input': ('a&e', 'smell'),\n",
    "            'amount': 1,\n",
    "            'type': 'biosphere'            \n",
    "        }]\n",
    "    },\n",
    "    (\"a&e\", \"kitty litter\"): {'name': 'yuck'},\n",
    "    (\"a&e\", \"cat food\"): {'name': 'yum'},\n",
    "    (\"a&e\", \"smell\"): {'name': 'stinky', 'type': 'biosphere'},\n",
    "})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can get an activity with ``.get()``:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cat' (kilogram, None, None)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "act = db.get(\"cat\")\n",
    "act"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ``cat`` process has no categories or location so far. Let's change that:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cat' (kilogram, inside, ['felis', 'catus'])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "act['location'] = 'inside'\n",
    "act['categories'] = ['felis', 'catus']\n",
    "act"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When we are ready, we can save our changes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "act.save()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can iterate over the available data fields:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "location : inside\n",
      "unit : kilogram\n",
      "database : a&e\n",
      "name : cat\n",
      "categories : ['felis', 'catus']\n",
      "color : black\n",
      "code : cat\n"
     ]
    }
   ],
   "source": [
    "for key in act:\n",
    "    print(key, ':', act[key])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the fields ``database`` and ``code`` are added automatically for us.\n",
    "\n",
    "There are only a few methods for an activity. We can see the activity key (combination of database and code):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a&e', 'cat')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "act.key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can iterate over all exchanges:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Exchange: 10 None 'yum' (None, None, None) to 'cat' (kilogram, inside, ['felis', 'catus'])>\n",
      "Exchange: 10 None 'yuck' (None, None, None) to 'cat' (kilogram, inside, ['felis', 'catus'])>\n",
      "Exchange: 1 None 'stinky' (None, None, None) to 'cat' (kilogram, inside, ['felis', 'catus'])>\n"
     ]
    }
   ],
   "source": [
    "for exc in act.exchanges():\n",
    "    print(exc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or just the technosphere/biosphere/production exchanges:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "technosphere:\n",
      "Exchange: 10 None 'yum' (None, None, None) to 'cat' (kilogram, inside, ['felis', 'catus'])>\n",
      "Exchange: 10 None 'yuck' (None, None, None) to 'cat' (kilogram, inside, ['felis', 'catus'])>\n",
      "biosphere:\n",
      "Exchange: 1 None 'stinky' (None, None, None) to 'cat' (kilogram, inside, ['felis', 'catus'])>\n",
      "production:\n"
     ]
    }
   ],
   "source": [
    "print(\"technosphere:\")\n",
    "for exc in act.technosphere():\n",
    "    print(exc)\n",
    "print(\"biosphere:\")\n",
    "for exc in act.biosphere():\n",
    "    print(exc)\n",
    "print(\"production:\")\n",
    "for exc in act.production():\n",
    "    print(exc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also count exchanges (or any of the specific types of exchanges):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(act.exchanges())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There weren't any production exchanges. Brightway2 will add a default production exchanges with amount of 1 when building the technosphere matrix if no production exchange is given.\n",
    "\n",
    "We can also look at upstream exchanges - those that consume this activity's reference product. We don't have any yet, so let's create a new activity:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "ValidityError",
     "evalue": "This activity can't be saved for the following reasons\n\t* Missing field ``name``",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValidityError\u001b[0m                             Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-13-1ce2ba0be795>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mna\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnew_activity\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"dog\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mna\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msave\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/Users/cmutel/local35/bw3/lib/python3.5/site-packages/bw2data/project.py\u001b[0m in \u001b[0;36mwritable_project\u001b[0;34m(wrapped, instance, args, kwargs)\u001b[0m\n\u001b[1;32m    383\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mprojects\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread_only\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    384\u001b[0m         \u001b[0;32mraise\u001b[0m \u001b[0mReadOnlyProject\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mREAD_ONLY_PROJECT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 385\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mwrapped\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/Users/cmutel/local35/bw3/lib/python3.5/site-packages/bw2data/backends/peewee/proxies.py\u001b[0m in \u001b[0;36msave\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    104\u001b[0m             raise ValidityError(\"This activity can't be saved for the \"\n\u001b[1;32m    105\u001b[0m                 \u001b[0;34m\"following reasons\\n\\t* \"\u001b[0m \u001b[0;34m+\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 106\u001b[0;31m                 \u001b[0;34m\"\\n\\t* \"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwhy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    107\u001b[0m             )\n\u001b[1;32m    108\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValidityError\u001b[0m: This activity can't be saved for the following reasons\n\t* Missing field ``name``"
     ]
    }
   ],
   "source": [
    "na = db.new_activity(\"dog\")\n",
    "na.save()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Oops, activities must have names. Let's fix that:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "na['name'] = 'fido'\n",
    "na.save()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will also get a nice error message for invalid new exchanges:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "ValidityError",
     "evalue": "This exchange can't be saved for the following reasons\n\t* Missing field ``input``\n\t* Invalid or missing field ``amount``\n\t* Missing field ``type``",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValidityError\u001b[0m                             Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-15-514e23f89f03>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mna\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnew_exchange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msave\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/Users/cmutel/local35/bw3/lib/python3.5/site-packages/bw2data/project.py\u001b[0m in \u001b[0;36mwritable_project\u001b[0;34m(wrapped, instance, args, kwargs)\u001b[0m\n\u001b[1;32m    383\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mprojects\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread_only\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    384\u001b[0m         \u001b[0;32mraise\u001b[0m \u001b[0mReadOnlyProject\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mREAD_ONLY_PROJECT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 385\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mwrapped\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/Users/cmutel/local35/bw3/lib/python3.5/site-packages/bw2data/backends/peewee/proxies.py\u001b[0m in \u001b[0;36msave\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    268\u001b[0m             raise ValidityError(\"This exchange can't be saved for the \"\n\u001b[1;32m    269\u001b[0m                 \u001b[0;34m\"following reasons\\n\\t* \"\u001b[0m \u001b[0;34m+\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 270\u001b[0;31m                 \u001b[0;34m\"\\n\\t* \"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwhy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    271\u001b[0m             )\n\u001b[1;32m    272\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValidityError\u001b[0m: This exchange can't be saved for the following reasons\n\t* Missing field ``input``\n\t* Invalid or missing field ``amount``\n\t* Missing field ``type``"
     ]
    }
   ],
   "source": [
    "na.new_exchange().save()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's add a link to our cat process. Don't worry, the dog won't really eat the cat!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "new_exc = na.new_exchange(input=act, amount=1, type='technosphere')\n",
    "new_exc.save()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now see that 'fido' links to 'cat':"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Exchange: 1 kilogram 'cat' (kilogram, inside, ['felis', 'catus']) to 'fido' (None, GLO, None)>\n",
      "Exchange: 1 kilogram 'cat' (kilogram, inside, ['felis', 'catus']) to 'fido' (None, GLO, None)>\n"
     ]
    }
   ],
   "source": [
    "for exc in na.technosphere():\n",
    "    print(exc)\n",
    "for exc in act.upstream():\n",
    "    print(exc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "``exc.input`` and ``exc.output`` will return activities:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('cat' (kilogram, inside, ['felis', 'catus']), 'fido' (None, GLO, None))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_exc.input, new_exc.output"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Exchanges have a few more methods:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('kilogram', 1, stats_arrays.distributions.undefined.UndefinedUncertainty)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_exc.unit, new_exc.amount, new_exc.uncertainty_type"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's add some uncertainty to this exchange (see http://stats-arrays.readthedocs.org/en/latest/):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from stats_arrays import NormalUncertainty\n",
    "\n",
    "new_exc['uncertainty type'] = NormalUncertainty.id\n",
    "new_exc['loc'], new_exc['scale'] = 1, 0.25\n",
    "new_exc.save()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now get an uncertainty dictionary for use in ``stats_arrays`` functions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'loc': 1, 'scale': 0.25, 'uncertainty type': 3}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_exc.uncertainty"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And can get a random sample:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.99004329,  1.07175437,  0.47354696,  1.32863177,  1.38786416,\n",
       "        1.40075775,  1.12460893,  1.08966507,  1.31904524,  1.27995624])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_exc.random_sample(n=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can copy activities. This will copy the activities exchanges:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'kudu' (kilogram, inside, ['felis', 'catus'])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kudu = act.copy(name=\"kudu\", code='antelope')\n",
    "kudu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Exchange: 10 None 'yum' (None, None, None) to 'kudu' (kilogram, inside, ['felis', 'catus'])>\n",
      "Exchange: 10 None 'yuck' (None, None, None) to 'kudu' (kilogram, inside, ['felis', 'catus'])>\n",
      "Exchange: 1 None 'stinky' (None, None, None) to 'kudu' (kilogram, inside, ['felis', 'catus'])>\n"
     ]
    }
   ],
   "source": [
    "for exc in kudu.exchanges():\n",
    "    print(exc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Upstream exchanges are (of course) not copied:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for exc in kudu.upstream():\n",
    "    print(exc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can delete the some or all of an activities exchanges:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before: 3\n",
      "After: 2\n"
     ]
    }
   ],
   "source": [
    "print(\"Before:\",len(kudu.exchanges()))\n",
    "kudu.biosphere().delete()\n",
    "print(\"After:\", len(kudu.exchanges()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also delete activities:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "kudu.delete()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our new activity is no longer in the database:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kudu in db"
   ]
  },
  {
   "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.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
