From 6d8199b0839b50a917f565a60fcc60986ad5a796 Mon Sep 17 00:00:00 2001 From: romainsacchi Date: Tue, 28 Jan 2025 19:24:35 +0200 Subject: [PATCH] Add notebook to use premise matrices --- dev/Untitled1.ipynb | 496 ++++++++++++++++- .../example_matrices_to_bw_processing.ipynb | 504 ++++++++++++++++++ .../examples user-defined scenarios.ipynb | 2 +- 3 files changed, 986 insertions(+), 16 deletions(-) create mode 100644 examples/example_matrices_to_bw_processing.ipynb diff --git a/dev/Untitled1.ipynb b/dev/Untitled1.ipynb index ad0f834e..73484e97 100644 --- a/dev/Untitled1.ipynb +++ b/dev/Untitled1.ipynb @@ -5,15 +5,7 @@ "execution_count": 1, "id": "5018b7ee-0169-49d7-9455-2f1aea562e9e", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "USER_DATA_BASE_DIR: test\n" - ] - } - ], + "outputs": [], "source": [ "from premise import *\n", "from datapackage import Package\n", @@ -50,7 +42,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "premise v.(2, 2, 2)\n", + "premise v.(2, 3, 0, 'dev0')\n", "+------------------------------------------------------------------+\n", "| Warning |\n", "+------------------------------------------------------------------+\n", @@ -85,7 +77,432 @@ "Hide these messages?\n", "NewDatabase(..., quiet=True)\n", "- Extracting source database\n", + "Cannot find cached database. Will create one now for next time...\n", + "Getting activity data\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "100%|█████████████████████████████████| 23523/23523 [00:00<00:00, 385863.57it/s]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Adding exchange data to activities\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "100%|████████████████████████████████| 743409/743409 [00:30<00:00, 24109.44it/s]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Filling out exchange data\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "100%|███████████████████████████████████| 23523/23523 [00:02<00:00, 8119.15it/s]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Set missing location of datasets to global scope.\n", + "Set missing location of production exchanges to scope of dataset.\n", + "Correct missing location of technosphere exchanges.\n", + "Correct missing flow categories for biosphere exchanges\n", + "Remove empty exchanges.\n", + "Remove uncertainty data.\n", "- Extracting inventories\n", + "Cannot find cached inventories. Will create them now for next time...\n", + "Importing default inventories...\n", + "\n", + "Extracted 1 worksheets in 0.12 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 4 worksheets in 0.16 seconds\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 7 worksheets in 0.02 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.03 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.03 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 2 worksheets in 0.02 seconds\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.27 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "The following datasets to import already exist in the source database. They will not be imported\n", + "+--------------------------------+--------------------------------+----------+-------------+\n", + "| Name | Reference product | Location | File |\n", + "+--------------------------------+--------------------------------+----------+-------------+\n", + "| fluorspar production, 97% puri | fluorspar, 97% purity | GLO | lci-PV.xlsx |\n", + "| metallization paste production | metallization paste, back side | RER | lci-PV.xlsx |\n", + "| metallization paste production | metallization paste, back side | RER | lci-PV.xlsx |\n", + "| metallization paste production | metallization paste, front sid | RER | lci-PV.xlsx |\n", + "| photovoltaic module production | photovoltaic module, building- | RER | lci-PV.xlsx |\n", + "| photovoltaic module production | photovoltaic module, building- | RER | lci-PV.xlsx |\n", + "| photovoltaic mounting system p | photovoltaic mounting system, | RER | lci-PV.xlsx |\n", + "| photovoltaic mounting system p | photovoltaic mounting system, | RER | lci-PV.xlsx |\n", + "| photovoltaic mounting system p | photovoltaic mounting system, | RER | lci-PV.xlsx |\n", + "| photovoltaic panel factory con | photovoltaic panel factory | GLO | lci-PV.xlsx |\n", + "| polyvinylfluoride production | polyvinylfluoride | US | lci-PV.xlsx |\n", + "| polyvinylfluoride production, | polyvinylfluoride, dispersion | US | lci-PV.xlsx |\n", + "| polyvinylfluoride, film produc | polyvinylfluoride, film | US | lci-PV.xlsx |\n", + "| silicon production, metallurgi | silicon, metallurgical grade | NO | lci-PV.xlsx |\n", + "| vinyl fluoride production | vinyl fluoride | US | lci-PV.xlsx |\n", + "| wafer factory construction | wafer factory | DE | lci-PV.xlsx |\n", + "+--------------------------------+--------------------------------+----------+-------------+\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.04 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "The following datasets to import already exist in the source database. They will not be imported\n", + "+--------------------------------+--------------------------------+----------+--------------------------------+\n", + "| Name | Reference product | Location | File |\n", + "+--------------------------------+--------------------------------+----------+--------------------------------+\n", + "| carbon dioxide, captured at ce | carbon dioxide, captured and r | RER | lci-synfuels-from-methanol-fro |\n", + "+--------------------------------+--------------------------------+----------+--------------------------------+\n", + "Extracted 1 worksheets in 0.00 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "The following datasets to import already exist in the source database. They will not be imported\n", + "+--------------------------------+--------------------------------+----------+--------------------------------+\n", + "| Name | Reference product | Location | File |\n", + "+--------------------------------+--------------------------------+----------+--------------------------------+\n", + "| hydrogen production, coal gasi | hydrogen, gaseous, low pressur | RoW | lci-hydrogen-coal-gasification |\n", + "| methanol production, coal gasi | methanol | RoW | lci-hydrogen-coal-gasification |\n", + "+--------------------------------+--------------------------------+----------+--------------------------------+\n", + "Extracted 1 worksheets in 0.00 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "The following datasets to import already exist in the source database. They will not be imported\n", + "+--------------------------------+--------------------------------+----------+--------------------------------+\n", + "| Name | Reference product | Location | File |\n", + "+--------------------------------+--------------------------------+----------+--------------------------------+\n", + "| hydrogen production, steam met | hydrogen, gaseous, low pressur | RER | lci-hydrogen-smr-atr-natgas.xl |\n", + "+--------------------------------+--------------------------------+----------+--------------------------------+\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.00 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "The following datasets to import already exist in the source database. They will not be imported\n", + "+--------------------------------+--------------------------------+----------+--------------------------------+\n", + "| Name | Reference product | Location | File |\n", + "+--------------------------------+--------------------------------+----------+--------------------------------+\n", + "| methanol production facility, | methanol production facility, | RER | lci-synfuels-from-methanol-fro |\n", + "+--------------------------------+--------------------------------+----------+--------------------------------+\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.00 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 5 worksheets in 0.13 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.03 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.00 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.07 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.04 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.01 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 19 worksheets in 0.28 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 1.54 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.15 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.73 seconds\n", + "Migrating to 3.8 first\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.03 seconds\n", + "Applying strategy: migrate_datasets\n", + "Applying strategy: migrate_exchanges\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Extracted 1 worksheets in 0.02 seconds\n", + "Data cached. It is advised to restart your workflow at this point.\n", + "This allows premise to use the cached data instead, which results in\n", + "a faster workflow.\n", "- Fetching IAM data\n", "Done!\n" ] @@ -119,7 +536,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "0c80994c-cbac-4143-81ee-1de1531a6f95", "metadata": {}, "outputs": [ @@ -129,6 +546,40 @@ "text": [ "Processing scenarios for all sectors: 0%| | 0/1 [00:00 dict:\n", + " \"\"\"\n", + " Reads a CSV file and returns its contents as a dictionary.\n", + "\n", + " Each row of the CSV file is expected to contain four string values followed by an index.\n", + " These are stored in the dictionary as a tuple of the four strings mapped to the index.\n", + "\n", + " \"\"\"\n", + " indices = dict()\n", + " with open(file_path, encoding=\"utf-8\") as read_obj:\n", + " csv_reader = csv.reader(read_obj, delimiter=\";\")\n", + " next(csv_reader, None) # skip the headers\n", + " for row in csv_reader:\n", + " indices[(row[0], row[1], row[2], row[3])] = int(row[4])\n", + " # remove any unicode characters\n", + " indices = {tuple([str(x) for x in k]): v for k, v in indices.items()}\n", + " return indices\n", + "\n", + "A_idx = read_indices_csv(fp_A_idx)\n", + "B_idx = read_indices_csv(fp_B_idx)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "13d71880-07d5-4eeb-ac99-097fec366a50", + "metadata": {}, + "outputs": [], + "source": [ + "def build_array(fp):\n", + " array = np.genfromtxt(fp, delimiter=\";\", skip_header=1)\n", + " \n", + " # give `indices_array` a list of tuples of indices\n", + " indices_array = np.array(\n", + " list(zip(array[:, 1].astype(int), array[:, 0].astype(int))),\n", + " dtype=bwp.INDICES_DTYPE,\n", + " )\n", + " \n", + " data_array = array[:, 2]\n", + " \n", + " # make a boolean scalar array to store the sign of the data\n", + " flip_array = array[:, -1].astype(bool)\n", + " \n", + " distributions_array = np.array(\n", + " list(\n", + " zip(\n", + " array[:, 3].astype(int), # uncertainty type\n", + " array[:, 4].astype(float), # loc\n", + " array[:, 5].astype(float), # scale\n", + " array[:, 6].astype(float), # shape\n", + " array[:, 7].astype(float), # minimum\n", + " array[:, 8].astype(float), # maximum\n", + " array[:, 9].astype(bool), # negative\n", + " )\n", + " ),\n", + " dtype=bwp.UNCERTAINTY_DTYPE,\n", + " )\n", + " return indices_array, data_array, flip_array, distributions_array" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "795016db-8704-4db7-a750-84ac08b4d6cb", + "metadata": {}, + "outputs": [], + "source": [ + "# we create a static data package\n", + "dp = bwp.create_datapackage()" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "92a2483e-6e81-4c42-8298-5aa526eedf67", + "metadata": {}, + "outputs": [], + "source": [ + "indices_array, data_array, flip_array, _ = build_array(fp_A_matrix)\n", + "\n", + "# we add the technosphere vector\n", + "dp.add_persistent_vector(\n", + " matrix=\"technosphere_matrix\",\n", + " indices_array=indices_array,\n", + " data_array=data_array,\n", + " flip_array=flip_array,\n", + " #distributions_array=distributions_array,\n", + ")\n", + "\n", + "indices_array, data_array, _, _ = build_array(fp_B_matrix)\n", + "\n", + "# we add the biosphere vector\n", + "dp.add_persistent_vector(\n", + " matrix=\"biosphere_matrix\",\n", + " indices_array=indices_array,\n", + " data_array=data_array,\n", + " flip_array=None,\n", + " #distributions_array=distributions_array,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "62482300-8ccb-43c1-a95c-9fd426be5055", + "metadata": {}, + "outputs": [], + "source": [ + "# let's build a characterization matrix for fossil CO2 = 1\n", + "# more complex LCIA vector building is possible \n", + "# if one accesses bw2data.Method()\n", + "c_indices = np.array(\n", + " [\n", + " (v, v) \n", + " for k, v in B_idx.items() \n", + " if \"carbon dioxide, fossil\" in str(k).lower()\n", + " ],\n", + " dtype=bwp.INDICES_DTYPE \n", + ")\n", + "c_data = np.ones(len(c_indices))" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "d035cddf-4c74-4545-adb4-8db2aeb54ab3", + "metadata": {}, + "outputs": [], + "source": [ + "# we add the method vector\n", + "dp.add_persistent_vector(\n", + " matrix='characterization_matrix',\n", + " indices_array=c_indices,\n", + " data_array=c_data,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "757c9934-426b-4a81-814c-615bfe10eedb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "25553" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# let's look for a gasoline car\n", + "ix = [\n", + " v for k, v in A_idx.items() \n", + " if \"transport, passenger car\" in str(k)\n", + " and \"gasoline\" in str(k)\n", + "][0]\n", + "ix" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "aff74989-c3c2-4448-a5cd-3adc9f263908", + "metadata": {}, + "outputs": [], + "source": [ + "lca = bc.LCA(\n", + " demand={ix: 1},\n", + " data_objs=[\n", + " dp,\n", + " ],\n", + " use_distributions=False,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "21099522-8802-473b-a006-4ffa50e3a1c2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.27376991977578063" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lca.lci()\n", + "lca.lcia()\n", + "lca.score" + ] + }, + { + "cell_type": "markdown", + "id": "4b39de01-9afa-4aea-952f-98d56c7db24f", + "metadata": {}, + "source": [ + "## Scenarios\n", + "\n", + "Now, let's vary the input of gasoline in the car dataset (and related CO2 emissions)." + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "db6fb89e-d25c-465d-9cfb-e578c7b1a81e", + "metadata": {}, + "outputs": [], + "source": [ + "# let's build reverse indices' lists for convenience\n", + "rev_A_idx = {v: k for k, v in A_idx.items()}\n", + "rev_B_idx = {v: k for k, v in B_idx.items()}" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "fe2f5d0f-67f9-4d0c-b73a-dfa1fdd83128", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gasoline input index 28947\n", + "Scenario values [0.04 0.05 0.06 0.07 0.08 0.09]\n" + ] + } + ], + "source": [ + "# let's print the inputs labels and indices to the car dataset\n", + "for i in np.argwhere(lca.technosphere_matrix[:, ix]):\n", + " i_ = lca.activity_dict.reversed[i[0]]\n", + " label = rev_A_idx[i_]\n", + " if label[0] == 'market for petrol, low-sulfur':\n", + " gasoline_ix = i_\n", + "gasoline_values = np.arange(4/100, 10/100, 0.01)\n", + "\n", + "print(\"Gasoline input index\", gasoline_ix)\n", + "print(\"Scenario values\", gasoline_values)" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "4659d0f7-d45f-42b2-a6a7-e71582024bc1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.08797021909304188" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# current gasoline consumption\n", + "lca.technosphere_matrix[lca.dicts.product[gasoline_ix], lca.dicts.activity[ix]] * -1" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "id": "200212e0-2738-4f30-9c68-e4ff2001d164", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "401\n", + "[0.126 0.1575 0.189 0.2205 0.252 0.2835]\n" + ] + } + ], + "source": [ + "# same for CO2 emissions\n", + "# let's print the inputs labels and indices to the car dataset\n", + "for i in np.argwhere(lca.biosphere_matrix[:, ix]):\n", + " i_ = lca.biosphere_dict.reversed[i[0]]\n", + " label = rev_B_idx[i_]\n", + " if label[0] == 'Carbon dioxide, fossil':\n", + " co2_ix = i_\n", + "print(co2_ix)\n", + "co2_values = gasoline_values * 3.15 # kg CO2/kg gasoline\n", + "print(co2_values)" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "b56f90ba-1587-4530-9a5e-dd0bb41da3a4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.13075946643594696" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# current CO2 emissions of the car\n", + "lca.biosphere_matrix[lca.biosphere_dict[co2_ix], lca.dicts.activity[ix]]" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "id": "0504e37c-7c29-44ed-84cb-29ba153b4c9e", + "metadata": {}, + "outputs": [], + "source": [ + "# we create a second data package, to store our scenario values\n", + "dp_scenarios = bwp.create_datapackage(sequential=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "1cf1246f-9fa5-4c10-9f4d-348742b411bc", + "metadata": {}, + "outputs": [], + "source": [ + "dp_scenarios.add_persistent_array(\n", + " matrix='technosphere_matrix',\n", + " indices_array=np.array([(gasoline_ix, ix)], dtype=bwp.INDICES_DTYPE),\n", + " data_array=np.array([tuple(gasoline_values)]).reshape(1, -1),\n", + " flip_array=np.array([True])\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "9b08c9a0-a494-4fc5-9c26-957bae12c99b", + "metadata": {}, + "outputs": [], + "source": [ + "dp_scenarios.add_persistent_array(\n", + " matrix='biosphere_matrix',\n", + " indices_array=np.array([(co2_ix, ix)], dtype=bwp.INDICES_DTYPE),\n", + " data_array=np.array([tuple(co2_values)]).reshape(1, -1),\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "311a5e51-87db-4125-958c-235841c6f78a", + "metadata": {}, + "source": [ + "Now we can iterate over the LCA object (which is a generator) for as many times as we have scenario values." + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "id": "ed4e0e66-69ee-4b6e-98ad-e15c8f01b070", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fuel: 0.04 CO2: 0.126 Score: 0.2378711545922471\n", + "Fuel: 0.05 CO2: 0.1575 Score: 0.2758625359818526\n", + "Fuel: 0.060000000000000005 CO2: 0.189 Score: 0.31385391737145824\n", + "Fuel: 0.07 CO2: 0.2205 Score: 0.3518452987610632\n", + "Fuel: 0.08000000000000002 CO2: 0.25200000000000006 Score: 0.38983668015066975\n", + "Fuel: 0.09000000000000001 CO2: 0.28350000000000003 Score: 0.42782806154027464\n" + ] + } + ], + "source": [ + "for v, val in enumerate(gasoline_values):\n", + " if v == 0:\n", + " lca = bc.LCA(\n", + " demand={ix: 1},\n", + " data_objs=[\n", + " dp, dp_scenarios\n", + " ],\n", + " use_distributions=False,\n", + " use_arrays=True,\n", + " )\n", + " lca.lci()\n", + " lca.lcia()\n", + " else:\n", + " next(lca)\n", + " print(\n", + " \"Fuel:\", -1 * lca.technosphere_matrix[lca.dicts.product[gasoline_ix], lca.dicts.activity[ix]], \n", + " \"CO2:\", lca.biosphere_matrix[lca.biosphere_dict[co2_ix], lca.dicts.activity[ix]], \n", + " \"Score:\", lca.score\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4eb165b7-b96a-4288-a916-492ec257f8a7", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/examples user-defined scenarios.ipynb b/examples/examples user-defined scenarios.ipynb index de716f9c..6b692ff1 100644 --- a/examples/examples user-defined scenarios.ipynb +++ b/examples/examples user-defined scenarios.ipynb @@ -572,7 +572,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.11.11" } }, "nbformat": 4,