{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exporting a disclosure file from lcopt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: For this to work you need the latest version of lcopt-dev from the `disclosures` branch on github" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load (or create) an lcopt model" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from lcopt import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "model = LcoptModel(load=r\"files\\Example Model.lcopt\") # Note: you can download 'Example Model.lcopt' from the files folder on github" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's what the model looks like:\n", "\n", "![Flow chart](images/tea_flow_chart.jpg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create the disclosure file and store the filename" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using `export_disclosure` with no arguments exports the unspecified model (all values 1)\n", "\n", "(Note: `folder_path` optionally chooses a folder to put the files in, otherwise they are saved to the working directory)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'files\\\\Example_Model_unspecified.json'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unspecified_file_name = model.export_disclosure(folder_path='files')\n", "unspecified_file_name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or you can specify a parameter set by its index:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'files\\\\Example_Model_ps_0.json'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ps0_file_name = model.export_disclosure(0, folder_path='files')\n", "ps0_file_name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or its name:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'files\\\\Example_Model_ps_Milky tea.json'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ps1_file_name = model.export_disclosure('Milky tea', folder_path='files')\n", "ps1_file_name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load one of the files to see the format" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import json\n", "from pprint import pprint" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "with open(unspecified_file_name, 'r') as j:\n", " data = json.load(j)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Ad': {'data': [[[0, 1], 1.0], [[1, 1], 1.0], [[2, 2], 1.0], [[3, 0], 1.0]],\n", " 'shape': [4, 3]},\n", " 'Af': {'data': [[[1, 2], 1.0], [[2, 0], 1.0]], 'shape': [3, 3]},\n", " 'Bf': {'data': [[[0, 1], 1.0]], 'shape': [1, 3]},\n", " 'background flows': [{'brightway_id': ['Ecoinvent3_3_cutoff',\n", " 'b80c575f22df17a0fbc7b4ae11f65dd2'],\n", " 'ecoinvent_id': '832cb6db-89ea-45a8-878b-42a580a3e33e',\n", " 'ecoinvent_name': 'market for electricity, medium '\n", " 'voltage',\n", " 'index': 0,\n", " 'location': 'SE',\n", " 'unit': 'kilowatt hour'},\n", " {'brightway_id': ['Ecoinvent3_3_cutoff',\n", " '05013963d77778b2c11336e1448b1fe2'],\n", " 'ecoinvent_id': '08852717-6b16-426c-be3d-649b2e6381b1',\n", " 'ecoinvent_name': 'market for tap water',\n", " 'index': 1,\n", " 'location': 'Europe without Switzerland',\n", " 'unit': 'kilogram'},\n", " {'brightway_id': ['Ecoinvent3_3_cutoff',\n", " '45996ff4d44c8a72ba5bf0ad89fbdb5e'],\n", " 'ecoinvent_id': '6aaf3f99-7e9c-45f9-9217-8afff755e9d9',\n", " 'ecoinvent_name': 'market for tea, dried',\n", " 'index': 2,\n", " 'location': 'GLO',\n", " 'unit': 'kilogram'},\n", " {'brightway_id': ['Ecoinvent3_3_cutoff',\n", " '214da18debd3c0074df1c1996cf0fed4'],\n", " 'ecoinvent_id': '98c6a35d-80bb-4a23-b11e-68688a93b7b4',\n", " 'ecoinvent_name': 'market for cow milk',\n", " 'index': 3,\n", " 'location': 'GLO',\n", " 'unit': 'kilogram'}],\n", " 'foreground emissions': [{'biosphere3_id': ['biosphere3',\n", " '075e433b-4be4-448e-9510-9a5029c1ce94'],\n", " 'index': 0,\n", " 'name': 'Water, emission, air',\n", " 'unit': 'cubic meter'}],\n", " 'foreground flows': [{'index': 0,\n", " 'location': 'GLO',\n", " 'name': 'Tea',\n", " 'unit': 'l'},\n", " {'index': 1,\n", " 'location': 'GLO',\n", " 'name': 'Boiling water',\n", " 'unit': 'l'},\n", " {'index': 2,\n", " 'location': 'GLO',\n", " 'name': 'Black tea',\n", " 'unit': 'l'}]}\n" ] } ], "source": [ "pprint(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reconstruct the matrices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's what we're expecting:\n", "\n", "![disclosure_pic.png](images/disclosure_pic.png)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a helper function to reconstruct a matrix as a numpy array from the json data" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def reconstruct_matrix(matrix_dict):\n", " m = np.zeros(matrix_dict['shape'])\n", " for (r, c), v in matrix_dict['data']:\n", " m[r, c] = v\n", " \n", " return m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create pandas DataFrames for easy viewing of the resulting matrices" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "Af = pd.DataFrame(reconstruct_matrix(data['Af']), index=[\"({}) {}, {}\".format(x['index'], x['name'], x['unit']) for x in data['foreground flows']])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "Ad = pd.DataFrame(reconstruct_matrix(data['Ad']), index=[\"{} [{}], {}, ({}, {})\".format(x['ecoinvent_name'], x['location'],x['unit'], *x['brightway_id']) for x in data['background flows']])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "Bf = pd.DataFrame(reconstruct_matrix(data['Bf']), index=[\"{}, {}, ({}, {})\".format(x['name'], x['unit'], *x['biosphere3_id']) for x in data['foreground emissions']])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
(0) Tea, l0.00.00.0
(1) Boiling water, l0.00.01.0
(2) Black tea, l1.00.00.0
\n", "
" ], "text/plain": [ " 0 1 2\n", "(0) Tea, l 0.0 0.0 0.0\n", "(1) Boiling water, l 0.0 0.0 1.0\n", "(2) Black tea, l 1.0 0.0 0.0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Af" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
market for electricity, medium voltage [SE], kilowatt hour, (Ecoinvent3_3_cutoff, b80c575f22df17a0fbc7b4ae11f65dd2)0.01.00.0
market for tap water [Europe without Switzerland], kilogram, (Ecoinvent3_3_cutoff, 05013963d77778b2c11336e1448b1fe2)0.01.00.0
market for tea, dried [GLO], kilogram, (Ecoinvent3_3_cutoff, 45996ff4d44c8a72ba5bf0ad89fbdb5e)0.00.01.0
market for cow milk [GLO], kilogram, (Ecoinvent3_3_cutoff, 214da18debd3c0074df1c1996cf0fed4)1.00.00.0
\n", "
" ], "text/plain": [ " 0 1 2\n", "market for electricity, medium voltage [SE], ki... 0.0 1.0 0.0\n", "market for tap water [Europe without Switzerlan... 0.0 1.0 0.0\n", "market for tea, dried [GLO], kilogram, (Ecoinv... 0.0 0.0 1.0\n", "market for cow milk [GLO], kilogram, (Ecoinven... 1.0 0.0 0.0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Ad" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
Water, emission, air, cubic meter, (biosphere3, 075e433b-4be4-448e-9510-9a5029c1ce94)0.01.00.0
\n", "
" ], "text/plain": [ " 0 1 2\n", "Water, emission, air, cubic meter, (biosphere3,... 0.0 1.0 0.0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Bf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reconstruct the data from an lcopt parameter set\n", "If we want to recreate the results of an lcopt model we can do the same thing with one of the specified files" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "with open(ps1_file_name, 'r') as j:\n", " specified_data = json.load(j)\n", " \n", "Af_ps1 = pd.DataFrame(reconstruct_matrix(specified_data['Af']), index=[\"({}) {}, {}\".format(x['index'], x['name'], x['unit']) for x in specified_data['foreground flows']])\n", "Ad_ps1 = pd.DataFrame(reconstruct_matrix(specified_data['Ad']), index=[\"{} [{}], {}, ({}, {})\".format(x['ecoinvent_name'], x['location'],x['unit'], *x['brightway_id']) for x in specified_data['background flows']])\n", "Bf_ps1 = pd.DataFrame(reconstruct_matrix(specified_data['Bf']), index=[\"{}, {}, ({}, {})\".format(x['name'], x['unit'], *x['biosphere3_id']) for x in specified_data['foreground emissions']])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
(0) Tea, l0.000.00.0
(1) Boiling water, l0.000.01.0
(2) Black tea, l0.990.00.0
\n", "
" ], "text/plain": [ " 0 1 2\n", "(0) Tea, l 0.00 0.0 0.0\n", "(1) Boiling water, l 0.00 0.0 1.0\n", "(2) Black tea, l 0.99 0.0 0.0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Af_ps1" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
market for electricity, medium voltage [SE], kilowatt hour, (Ecoinvent3_3_cutoff, b80c575f22df17a0fbc7b4ae11f65dd2)0.000.0750.000
market for tap water [Europe without Switzerland], kilogram, (Ecoinvent3_3_cutoff, 05013963d77778b2c11336e1448b1fe2)0.001.0000.000
market for tea, dried [GLO], kilogram, (Ecoinvent3_3_cutoff, 45996ff4d44c8a72ba5bf0ad89fbdb5e)0.000.0000.012
market for cow milk [GLO], kilogram, (Ecoinvent3_3_cutoff, 214da18debd3c0074df1c1996cf0fed4)0.010.0000.000
\n", "
" ], "text/plain": [ " 0 1 2\n", "market for electricity, medium voltage [SE], ki... 0.00 0.075 0.000\n", "market for tap water [Europe without Switzerlan... 0.00 1.000 0.000\n", "market for tea, dried [GLO], kilogram, (Ecoinv... 0.00 0.000 0.012\n", "market for cow milk [GLO], kilogram, (Ecoinven... 0.01 0.000 0.000" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Ad_ps1" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
Water, emission, air, cubic meter, (biosphere3, 075e433b-4be4-448e-9510-9a5029c1ce94)0.00.000010.0
\n", "
" ], "text/plain": [ " 0 1 2\n", "Water, emission, air, cubic meter, (biosphere3,... 0.0 0.00001 0.0" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Bf_ps1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This should be all the information we need to recreate the structure of the original foreground model, with the correct links to the background and biosphere databases" ] } ], "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }