Created
September 29, 2016 09:40
-
-
Save joaomeirelles/761ac733da237827142f44b75705b308 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "#Import Packages\n", | |
| "from ocelot import *\n", | |
| "import pandas as pd\n", | |
| "from copy import deepcopy\n", | |
| "import copy\n", | |
| "import json\n", | |
| "import numpy as np\n", | |
| "import math\n", | |
| "import matplotlib.pyplot as plt" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "#################\n", | |
| "#DATA Processing#\n", | |
| "#################" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "#Function to create new regionalised datasets with region-specific production and irrigation values\n", | |
| "def add_bananas(data):\n", | |
| " with open('/home/jm/Documents/PhD_EPFL/events/LCA_summerschool/hands_on/countries.json') as data_file: \n", | |
| " countries = json.load(data_file)\n", | |
| " \n", | |
| " #Preparing the ecoinvent countries dataset for merging the naming conventions of ecoinvent and FAO\n", | |
| " c = countries['Countries']\n", | |
| " c = pd.DataFrame(c)\n", | |
| " c['name'] = c['name'].str.lower()\n", | |
| " c = c[['name', 'shortname']]\n", | |
| " \n", | |
| " #Preparing the dataset from FAO with the production volume per country - this data was locally stored path = '/home/jm/Documents/PhD_EPFL/events/LCA_summerschool/group/'\n", | |
| " filename = 'fao_bananadata_production.csv'\n", | |
| " countries = pd.read_csv((path+filename))\n", | |
| " countries['AreaName'] = countries['AreaName'].str.lower()\n", | |
| " banana_production = countries[['AreaName', 'Value']]\n", | |
| " banana_production.columns = ['name', 'banana_production']\n", | |
| " \n", | |
| " #Merging the ecoinvent region dataset with FAO's production volume dataset\n", | |
| " result = pd.merge(c, banana_production, how='right', on=['name'])\n", | |
| " result = result.dropna(subset = ['shortname'])\n", | |
| " \n", | |
| " #Cutoff of the countries that account for 90% of the production volume\n", | |
| " result = result.sort_values(['banana_production'], ascending=[False])\n", | |
| " result['proportion_globalproduction'] = result['banana_production']/result['banana_production'].sum()\n", | |
| " result['proportion_globalproduction_cumsum'] = result['proportion_globalproduction'].cumsum()\n", | |
| " result.index = result['name']\n", | |
| " result = result.loc[result['proportion_globalproduction_cumsum'] < 0.9]\n", | |
| " \n", | |
| " \n", | |
| " #Scraping of web data on precipitation\n", | |
| " #Creating the headers (not in an easily readable format on the webside, hence easier to not import them)\n", | |
| " header = ['name',\n", | |
| " 'agriculturalland_perclandarea_00_02','agriculturalland_perclandarea_11_13','agriculturalland_percirrigated_11_13',\n", | |
| " 'avrgannualprecipitation_mm_14',\n", | |
| " 'landcerealproduction_hat_00_02','landcerealproduction_hat_11_13',\n", | |
| " 'fertlizerconsumption_percproduction_11_13', 'fertlizerconsumption_kgperha_11_13',\n", | |
| " 'agriculturalemployment_perctotalempl_00_02','agriculturalemployment_perctotalempl_11_13',\n", | |
| " 'agriculturalmachinery_tractorpor100sqrkm_00','agriculturalmachinery_tractorpor100sqrkm_09']\n", | |
| " \n", | |
| " #Scaping the precipitation data from the website\n", | |
| " overviewtable = pd.read_html('http://wdi.worldbank.org/table/3.2')\n", | |
| " \n", | |
| " #Formatting the extracted data and eliminating unecessary information\n", | |
| " wbdata = overviewtable[2]\n", | |
| " wbdata.columns = header\n", | |
| " wbdata = wbdata.replace('..','')\n", | |
| " wbdata = wbdata[['name', 'avrgannualprecipitation_mm_14']]\n", | |
| " \n", | |
| " #Formatting the precipitation data for merging with the production data (naming conventions)\n", | |
| " wbdata['name'] = wbdata['name'].str.lower()\n", | |
| " wbdata['avrgannualprecipitation_mm_14']=pd.to_numeric(wbdata['avrgannualprecipitation_mm_14'])\n", | |
| "\n", | |
| " #Merging the precipitation dataset with the production volume dataset\n", | |
| " input_data = pd.merge(result[['name', 'shortname', 'banana_production']], wbdata, how='left', on=['name'])\n", | |
| " input_data = input_data.set_index('shortname')\n", | |
| " \n", | |
| " #Performing a regression analysis based on a small sample to calculate irrigation need per \n", | |
| " #annual precipitation - this data came from literature\n", | |
| " rainfall = [2926,2274,3240]\n", | |
| " irrigation = [0.106,0.142,0.079]\n", | |
| " regression = np.polyfit(rainfall, irrigation, 1)\n", | |
| " \n", | |
| " #Calculating the irrigation need for each country based on the annual precipitation\n", | |
| " #Adding result to the dataset, formatting (adjusting precision to original information)\n", | |
| " input_data['irrigation_demand'] = (regression[1] + input_data['avrgannualprecipitation_mm_14']*regression[0])\n", | |
| " input_data['irrigation_demand'] = round(input_data['irrigation_demand'], 3)\n", | |
| " \n", | |
| " #Finding the dataset on bananaproduction in the ecoinvent database\n", | |
| " for banana_ds in data:\n", | |
| " if banana_ds['name']=='banana production':\n", | |
| " break\n", | |
| " \n", | |
| " #Create a copy of the banana production dataset for each country\n", | |
| " for index in input_data.index:\n", | |
| " new_ds = copy.deepcopy(banana_ds)\n", | |
| " new_ds['location'] = index\n", | |
| " \n", | |
| " #In each copy, replace the original production volume with the country-specific production volume\n", | |
| " for exc in new_ds['exchanges']:\n", | |
| " if exc['name']=='banana':\n", | |
| " exc['production volume']['amount']= input_data.loc[index]['banana_production']\n", | |
| " \n", | |
| " #In each copy, replace the original irrigation demand with the country-specific irrigation demand\n", | |
| " #Keep the global average for countries with no data on irrigation demand\n", | |
| " elif exc['name']=='irrigation':\n", | |
| " if math.isnan(input_data.loc[index]['irrigation_demand']):\n", | |
| " print('keeping global irrigation demand')\n", | |
| " else:\n", | |
| " exc['amount']= input_data.loc[index]['irrigation_demand']\n", | |
| " \n", | |
| " #Create a list with the datasets\n", | |
| " data.append(copy.deepcopy(new_ds))\n", | |
| " return data" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "#Add the datasets to ocelot database\n", | |
| "from ocelot.configuration import default_configuration\n", | |
| "default_configuration.insert(0, add_cool_bananas)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Starting Ocelot model run\n", | |
| "Using cached ecospold2 data\n", | |
| "Opening log file at: /home/jm/.local/share/Ocelot/model-runs/c0567b6636114919867fe54f09fe510e/report.log.json\n", | |
| "Applying transformation add_cool_bananas\n", | |
| "keeping global irrigation demand\n", | |
| "Applying transformation variable_names_are_unique\n", | |
| "Applying transformation ensure_markets_only_have_one_reference_product\n", | |
| "Applying transformation ensure_markets_dont_consume_their_ref_product\n", | |
| "Applying transformation fix_specific_ecoinvent_issues\n", | |
| "Applying transformation replace_implicit_references\n", | |
| "Applying transformation fix_known_bad_formula_strings\n", | |
| "Applying transformation lowercase_all_parameters\n", | |
| "Applying transformation fix_math_formulas\n", | |
| "Applying transformation replace_reserved_words\n", | |
| "Applying transformation delete_unparsable_formulas\n", | |
| "Applying transformation ensure_production_exchanges_have_production_volume\n", | |
| "Applying transformation add_pv_to_allocatable_byproducts\n", | |
| "Applying transformation create_pv_parameters\n", | |
| "Applying transformation remove_consequential_exchanges\n", | |
| "Applying transformation drop_rp_activity_links\n", | |
| "Applying transformation drop_zero_amount_activity_links\n", | |
| "Applying transformation check_activity_link_validity\n", | |
| "Applying transformation add_hard_linked_production_volumes\n", | |
| "Applying transformation rename_recyclable_content_exchanges\n", | |
| "Applying transformation create_recycled_content_datasets\n", | |
| "Applying transformation flip_non_allocatable_byproducts\n", | |
| "Applying transformation label_allocation_method\n", | |
| "Applying transformation economic_allocation\n", | |
| "Applying transformation constrained_market_allocation\n", | |
| "Applying transformation recycling_allocation\n", | |
| "Applying transformation waste_treatment_allocation\n", | |
| "Applying transformation combined_production_without_products\n", | |
| "Applying transformation combined_production\n" | |
| ] | |
| }, | |
| { | |
| "name": "stderr", | |
| "output_type": "stream", | |
| "text": [ | |
| "NameError\n", | |
| " 5.36e-11* cement+silica_fume*0+ ggbfs*0\n", | |
| " ^^^\n", | |
| "name 'silica_fume' is not defined\n", | |
| "NameError\n", | |
| " 5.36e-11* cement+silica_fume*0+ ggbfs*0\n", | |
| " ^^^\n", | |
| "name 'silica_fume' is not defined\n" | |
| ] | |
| }, | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Applying transformation combined_production_with_byproducts\n", | |
| "Applying transformation delete_allocation_method\n", | |
| "Applying transformation drop_rp_activity_links\n", | |
| "Applying transformation label_reference_product\n", | |
| "Applying transformation delete_allowed_zero_pv_market_datsets\n", | |
| "Applying transformation assign_fake_pv_to_confidential_datasets\n", | |
| "Applying transformation relabel_global_to_row\n", | |
| "Applying transformation add_unique_codes\n", | |
| "Applying transformation actualize_activity_links\n", | |
| "Applying transformation add_recycled_content_suppliers_to_markets\n", | |
| "Applying transformation add_suppliers_to_markets\n", | |
| "Applying transformation update_market_production_volumes\n", | |
| "Applying transformation add_suppliers_to_markets\n", | |
| "Applying transformation update_market_production_volumes\n", | |
| "Applying transformation allocate_suppliers\n", | |
| "Skipping zero total PV with multiple inputs:\n", | |
| "\tmarket for waste newspaper/waste newspaper (GLO, 3 suppliers)\n", | |
| "Applying transformation link_consumers_to_regional_markets\n", | |
| "Applying transformation link_consumers_to_recycled_content_activities\n", | |
| "Applying transformation link_consumers_to_global_markets\n", | |
| "Applying transformation add_reference_product_codes\n", | |
| "Applying transformation log_and_delete_unlinked_exchanges\n", | |
| "Saving final results\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "fp, data = system_model('/home/jm/Documents/PhD_EPFL/events/LCA_summerschool/'\n", | |
| " 'ecoinvent_3.2_modified_for_ocelot_summer_school')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "################\n", | |
| "#Data Uploading#\n", | |
| "################" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Creating default biosphere\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "name": "stderr", | |
| "output_type": "stream", | |
| "text": [ | |
| "Writing activities to SQLite3 database:\n", | |
| "0% 100%\n", | |
| "[### ] | ETA: 00:00:01" | |
| ] | |
| }, | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Applying strategy: normalize_units\n", | |
| "Applying strategy: drop_unspecified_subcategories\n", | |
| "Applied 2 strategies in 0.01 seconds\n" | |
| ] | |
| }, | |
| { | |
| "name": "stderr", | |
| "output_type": "stream", | |
| "text": [ | |
| "[##############################] | ETA: 00:00:00\n", | |
| "Total time elapsed: 00:00:01\n" | |
| ] | |
| }, | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Title: Writing activities to SQLite3 database:\n", | |
| " Started: 09/08/2016 22:28:34\n", | |
| " Finished: 09/08/2016 22:28:35\n", | |
| " Total time elapsed: 00:00:01\n", | |
| " CPU %: 70.80\n", | |
| " Memory %: 12.81\n", | |
| "Created database: biosphere3\n", | |
| "Creating default LCIA methods\n", | |
| "\n", | |
| "Applying strategy: normalize_units\n", | |
| "Applying strategy: set_biosphere_type\n", | |
| "Applying strategy: drop_unspecified_subcategories\n", | |
| "Applying strategy: link_iterable_by_fields\n", | |
| "Applied 4 strategies in 1.02 seconds\n", | |
| "Wrote 665 LCIA methods with 169551 characterization factors\n", | |
| "Creating core data migrations\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "#Setup brightway2\n", | |
| "from brightway2 import *\n", | |
| "projects.set_current(\"bananas2\")\n", | |
| "bw2setup()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 17, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Creating database `ecoinvent gone bananas` in project `bananas2`\n", | |
| "Applying strategy: normalize_units\n", | |
| "Applying strategy: drop_unspecified_subcategories\n", | |
| "Applying strategy: strip_biosphere_exc_locations\n", | |
| "Applied 3 strategies in 0.38 seconds\n", | |
| "Applying strategy: link_iterable_by_fields\n" | |
| ] | |
| }, | |
| { | |
| "name": "stderr", | |
| "output_type": "stream", | |
| "text": [ | |
| "Writing activities to SQLite3 database:\n", | |
| "0% 100%\n", | |
| "[ ]" | |
| ] | |
| }, | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "13292 datasets\n", | |
| "319917 exchanges\n", | |
| "0 unlinked exchanges\n", | |
| " \n", | |
| "Writing database\n" | |
| ] | |
| }, | |
| { | |
| "name": "stderr", | |
| "output_type": "stream", | |
| "text": [ | |
| "[##############################] | ETA: 00:00:00\n", | |
| "Total time elapsed: 00:01:24\n" | |
| ] | |
| }, | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Title: Writing activities to SQLite3 database:\n", | |
| " Started: 09/08/2016 22:29:37\n", | |
| " Finished: 09/08/2016 22:31:01\n", | |
| " Total time elapsed: 00:01:24\n", | |
| " CPU %: 62.00\n", | |
| " Memory %: 13.03\n", | |
| "Created database: ecoinvent gone bananas\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "#Import the new databsae\n", | |
| "from ocelot.io.brightway2 import *\n", | |
| "db = import_into_brightway2(data, \"ecoinvent gone bananas\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "anaconda-cloud": {}, | |
| "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": 1 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment