From 325247a2b8ae2c41fa7135bf48fa57ca3e181df7 Mon Sep 17 00:00:00 2001 From: romainsacchi Date: Sun, 24 Mar 2024 08:26:42 +0100 Subject: [PATCH] EODC --- dev/generate datapackages.ipynb | 398 +++++++++++++++++++------------- pathways/data_validation.py | 31 +-- pathways/lca.py | 29 ++- pathways/pathways.py | 126 +++++++--- 4 files changed, 360 insertions(+), 224 deletions(-) diff --git a/dev/generate datapackages.ipynb b/dev/generate datapackages.ipynb index 32151ac..7888bd2 100644 --- a/dev/generate datapackages.ipynb +++ b/dev/generate datapackages.ipynb @@ -308,7 +308,7 @@ "output_type": "stream", "text": [ "0% [##########################] 100% | ETA: 00:00:00\n", - "Total time elapsed: 00:00:18\n" + "Total time elapsed: 00:00:19\n" ] }, { @@ -324,8 +324,62 @@ "name": "stderr", "output_type": "stream", "text": [ + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [# ] 100% | ETA: 00:00:01/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [### ] 100% | ETA: 00:00:12/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [#### ] 100% | ETA: 00:00:16/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [###### ] 100% | ETA: 00:00:15/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [######## ] 100% | ETA: 00:00:14/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [######### ] 100% | ETA: 00:00:13/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [########### ] 100% | ETA: 00:00:12/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [############# ] 100% | ETA: 00:00:11/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [############## ] 100% | ETA: 00:00:10/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [################ ] 100% | ETA: 00:00:08/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [################# ] 100% | ETA: 00:00:07/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [################### ] 100% | ETA: 00:00:06/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [##################### ] 100% | ETA: 00:00:04/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [###################### ] 100% | ETA: 00:00:03/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [######################## ] 100% | ETA: 00:00:01/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", "0% [##########################] 100% | ETA: 00:00:00\n", - "Total time elapsed: 00:00:23\n" + "Total time elapsed: 00:00:24\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.03e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n" ] }, { @@ -342,7 +396,7 @@ "output_type": "stream", "text": [ "0% [##########################] 100% | ETA: 00:00:00\n", - "Total time elapsed: 00:00:22\n" + "Total time elapsed: 00:00:21\n" ] }, { @@ -356,8 +410,62 @@ "name": "stderr", "output_type": "stream", "text": [ + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [# ] 100% | ETA: 00:00:01/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [### ] 100% | ETA: 00:00:15/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [#### ] 100% | ETA: 00:00:15/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [###### ] 100% | ETA: 00:00:14/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [######## ] 100% | ETA: 00:00:13/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [######### ] 100% | ETA: 00:00:13/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [########### ] 100% | ETA: 00:00:12/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [############# ] 100% | ETA: 00:00:10/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [############## ] 100% | ETA: 00:00:10/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [################ ] 100% | ETA: 00:00:08/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [################# ] 100% | ETA: 00:00:06/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [################### ] 100% | ETA: 00:00:06/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [##################### ] 100% | ETA: 00:00:04/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [###################### ] 100% | ETA: 00:00:03/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "0% [######################## ] 100% | ETA: 00:00:01/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n", "0% [##########################] 100% | ETA: 00:00:00\n", - "Total time elapsed: 00:00:23\n" + "Total time elapsed: 00:00:23\n", + "/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.19e+12)\n", + " warnings.warn(msg, UmfpackWarning)\n" ] }, { @@ -387,7 +495,7 @@ "output_type": "stream", "text": [ "0% [##########################] 100% | ETA: 00:00:00\n", - "Total time elapsed: 00:00:18\n" + "Total time elapsed: 00:00:16\n" ] }, { @@ -402,7 +510,7 @@ "output_type": "stream", "text": [ "0% [##########################] 100% | ETA: 00:00:00\n", - "Total time elapsed: 00:00:16\n" + "Total time elapsed: 00:00:15\n" ] }, { @@ -419,7 +527,7 @@ "0% [##########################] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:17\n", "0% [###########] 100% | ETA: 00:00:00\n", - "Total time elapsed: 00:07:27\n" + "Total time elapsed: 00:07:25\n" ] } ], @@ -427,7 +535,7 @@ "import numpy as np\n", "p.calculate(\n", " methods=[\n", - " 'EF v3.1 - climate change - global warming potential (GWP100)',\n", + " #'EF v3.1 - climate change - global warming potential (GWP100)',\n", " 'RELICS - metals extraction - Lithium',\n", " ],\n", " regions=[r for r in p.scenarios.coords[\"region\"].values if r!=\"World\"],\n", @@ -436,6 +544,7 @@ " v for v in p.scenarios.coords[\"variables\"].values\n", " if any(i in v for i in [\"Industry\", \"Transport\", \"Heating\"])\n", " ],\n", + " #years=[2005, 2010],\n", " characterization=True,\n", " multiprocessing=False,\n", " demand_cutoff=0.01,\n", @@ -444,60 +553,17 @@ }, { "cell_type": "code", - "execution_count": 20, - "id": "6efd3e31-4758-4758-9431-eaf6eb028a68", - "metadata": {}, - "outputs": [], - "source": [ - "p.lca_results = p.lca_results.sum(dim=\"act_category\")" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "id": "3daf39e4-aa1f-41a2-b522-411a64dcb1cb", - "metadata": {}, - "outputs": [], - "source": [ - "import numpy as np\n", - "p.lca_results.loc[dict(year=[2015, 2025, 2035, 2045])] = np.nan" - ] - }, - { - "cell_type": "code", - "execution_count": 67, - "id": "2421f433-a541-403a-885a-ff79cb6168dd", - "metadata": {}, - "outputs": [], - "source": [ - "p.lca_results = p.lca_results.interpolate_na(dim=\"year\")" - ] - }, - { - "cell_type": "code", - "execution_count": 68, - "id": "93bba319-d07b-43b6-8509-aae510313d54", - "metadata": {}, - "outputs": [], - "source": [ - "arr = p.lca_results.interp(\n", - " year=range(p.lca_results.coords[\"year\"].values.min(), p.lca_results.coords[\"year\"].values.max())\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 69, + "execution_count": 3, "id": "162124db-232b-494e-a8ef-8b90d1b87411", "metadata": {}, "outputs": [], "source": [ - "df = arr.to_dataframe(\"value\")" + "df = p.lca_results.sum(dim=\"act_category\").to_dataframe(\"value\")" ] }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 4, "id": "cb52417b-e7a3-46ed-bbe6-8ff1d1607231", "metadata": {}, "outputs": [ @@ -546,33 +612,39 @@ " Industry_Food and Tobacco_Electricity\n", " 2005\n", " BRA\n", - " RW\n", - " image\n", - " SSP2-RCP19\n", - " EF v3.1 - climate change - global warming potential (GWP100)\n", - " 1.231356e+00\n", + " BR-AP\n", + " image\n", + " SSP2-RCP19\n", + " RELICS - metals extraction - Lithium\n", + " 0.0\n", " \n", " \n", + " KP\n", + " image\n", + " SSP2-RCP19\n", " RELICS - metals extraction - Lithium\n", - " 0.000000e+00\n", + " 0.0\n", " \n", " \n", - " SR\n", - " image\n", - " SSP2-RCP19\n", - " EF v3.1 - climate change - global warming potential (GWP100)\n", - " 1.470214e+00\n", + " JP\n", + " image\n", + " SSP2-RCP19\n", + " RELICS - metals extraction - Lithium\n", + " 0.0\n", " \n", " \n", + " BR-ES\n", + " image\n", + " SSP2-RCP19\n", " RELICS - metals extraction - Lithium\n", - " 0.000000e+00\n", + " 0.0\n", " \n", " \n", - " CHN\n", + " CA-MB\n", " image\n", " SSP2-RCP19\n", - " EF v3.1 - climate change - global warming potential (GWP100)\n", - " 2.006539e+07\n", + " RELICS - metals extraction - Lithium\n", + " 0.0\n", " \n", " \n", " ...\n", @@ -586,60 +658,66 @@ " \n", " \n", " Transport_Freight (train)_Liquid fossil\n", - " 2099\n", + " 2100\n", " WEU\n", - " LA\n", + " IN-MH\n", " image\n", " SSP2-RCP19\n", " RELICS - metals extraction - Lithium\n", - " 0.000000e+00\n", + " 0.0\n", " \n", " \n", - " SE\n", - " image\n", - " SSP2-RCP19\n", - " EF v3.1 - climate change - global warming potential (GWP100)\n", - " 0.000000e+00\n", + " IN-CT\n", + " image\n", + " SSP2-RCP19\n", + " RELICS - metals extraction - Lithium\n", + " 0.0\n", " \n", " \n", + " RME\n", + " image\n", + " SSP2-RCP19\n", " RELICS - metals extraction - Lithium\n", - " 0.000000e+00\n", + " 0.0\n", " \n", " \n", - " AT\n", - " image\n", - " SSP2-RCP19\n", - " EF v3.1 - climate change - global warming potential (GWP100)\n", - " 0.000000e+00\n", + " IN\n", + " image\n", + " SSP2-RCP19\n", + " RELICS - metals extraction - Lithium\n", + " 0.0\n", " \n", " \n", + " AU\n", + " image\n", + " SSP2-RCP19\n", " RELICS - metals extraction - Lithium\n", - " 0.000000e+00\n", + " 0.0\n", " \n", " \n", "\n", - "

172025620 rows × 1 columns

\n", + "

13580970 rows × 1 columns

\n", "" ], "text/plain": [ - " value\n", - "variable year region location model scenario impact_category \n", - "Industry_Food and Tobacco_Electricity 2005 BRA RW image SSP2-RCP19 EF v3.1 - climate change - global warming poten... 1.231356e+00\n", - " RELICS - metals extraction - Lithium 0.000000e+00\n", - " SR image SSP2-RCP19 EF v3.1 - climate change - global warming poten... 1.470214e+00\n", - " RELICS - metals extraction - Lithium 0.000000e+00\n", - " CHN image SSP2-RCP19 EF v3.1 - climate change - global warming poten... 2.006539e+07\n", - "... ...\n", - "Transport_Freight (train)_Liquid fossil 2099 WEU LA image SSP2-RCP19 RELICS - metals extraction - Lithium 0.000000e+00\n", - " SE image SSP2-RCP19 EF v3.1 - climate change - global warming poten... 0.000000e+00\n", - " RELICS - metals extraction - Lithium 0.000000e+00\n", - " AT image SSP2-RCP19 EF v3.1 - climate change - global warming poten... 0.000000e+00\n", - " RELICS - metals extraction - Lithium 0.000000e+00\n", - "\n", - "[172025620 rows x 1 columns]" + " value\n", + "variable year region location model scenario impact_category \n", + "Industry_Food and Tobacco_Electricity 2005 BRA BR-AP image SSP2-RCP19 RELICS - metals extraction - Lithium 0.0\n", + " KP image SSP2-RCP19 RELICS - metals extraction - Lithium 0.0\n", + " JP image SSP2-RCP19 RELICS - metals extraction - Lithium 0.0\n", + " BR-ES image SSP2-RCP19 RELICS - metals extraction - Lithium 0.0\n", + " CA-MB image SSP2-RCP19 RELICS - metals extraction - Lithium 0.0\n", + "... ...\n", + "Transport_Freight (train)_Liquid fossil 2100 WEU IN-MH image SSP2-RCP19 RELICS - metals extraction - Lithium 0.0\n", + " IN-CT image SSP2-RCP19 RELICS - metals extraction - Lithium 0.0\n", + " RME image SSP2-RCP19 RELICS - metals extraction - Lithium 0.0\n", + " IN image SSP2-RCP19 RELICS - metals extraction - Lithium 0.0\n", + " AU image SSP2-RCP19 RELICS - metals extraction - Lithium 0.0\n", + "\n", + "[13580970 rows x 1 columns]" ] }, - "execution_count": 70, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -650,17 +728,17 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 5, "id": "9e187610-3d3d-4998-a5de-9a88049ae031", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "172025620" + "13580970" ] }, - "execution_count": 71, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -671,7 +749,7 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 6, "id": "7a8e3d53-89f2-4cd3-b957-516c0206b653", "metadata": {}, "outputs": [], @@ -682,17 +760,17 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 7, "id": "10253a05-a90d-4cc6-851b-d28ce89188cb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "17529539" + "51489" ] }, - "execution_count": 73, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -703,7 +781,7 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": 8, "id": "aff3301d-4731-4802-97c0-ed88226cd58b", "metadata": {}, "outputs": [], @@ -713,7 +791,7 @@ }, { "cell_type": "code", - "execution_count": 75, + "execution_count": 9, "id": "998d8893-98af-4299-b1eb-10ef3a8c7c7c", "metadata": {}, "outputs": [ @@ -754,55 +832,55 @@ " Industry_Food and Tobacco_Electricity\n", " 2005\n", " BRA\n", - " RW\n", + " CL\n", " image\n", " SSP2-RCP19\n", - " EF v3.1 - climate change - global warming pote...\n", - " 1.231356e+00\n", + " RELICS - metals extraction - Lithium\n", + " 2.957607e-02\n", " \n", " \n", " 1\n", " Industry_Food and Tobacco_Electricity\n", " 2005\n", " BRA\n", - " SR\n", + " RER\n", " image\n", " SSP2-RCP19\n", - " EF v3.1 - climate change - global warming pote...\n", - " 1.470214e+00\n", + " RELICS - metals extraction - Lithium\n", + " 5.798639e-06\n", " \n", " \n", " 2\n", " Industry_Food and Tobacco_Electricity\n", " 2005\n", " BRA\n", - " CHN\n", + " RNA\n", " image\n", " SSP2-RCP19\n", - " EF v3.1 - climate change - global warming pote...\n", - " 2.006539e+07\n", + " RELICS - metals extraction - Lithium\n", + " 4.660310e-06\n", " \n", " \n", " 3\n", " Industry_Food and Tobacco_Electricity\n", " 2005\n", " BRA\n", - " DZ\n", + " DE\n", " image\n", " SSP2-RCP19\n", - " EF v3.1 - climate change - global warming pote...\n", - " 3.311854e+05\n", + " RELICS - metals extraction - Lithium\n", + " 6.650308e-08\n", " \n", " \n", " 4\n", " Industry_Food and Tobacco_Electricity\n", " 2005\n", " BRA\n", - " PL\n", + " US\n", " image\n", " SSP2-RCP19\n", - " EF v3.1 - climate change - global warming pote...\n", - " 1.060831e+06\n", + " RELICS - metals extraction - Lithium\n", + " 7.912068e-07\n", " \n", " \n", "\n", @@ -810,21 +888,21 @@ ], "text/plain": [ " variable year region location model \\\n", - "0 Industry_Food and Tobacco_Electricity 2005 BRA RW image \n", - "1 Industry_Food and Tobacco_Electricity 2005 BRA SR image \n", - "2 Industry_Food and Tobacco_Electricity 2005 BRA CHN image \n", - "3 Industry_Food and Tobacco_Electricity 2005 BRA DZ image \n", - "4 Industry_Food and Tobacco_Electricity 2005 BRA PL image \n", - "\n", - " scenario impact_category value \n", - "0 SSP2-RCP19 EF v3.1 - climate change - global warming pote... 1.231356e+00 \n", - "1 SSP2-RCP19 EF v3.1 - climate change - global warming pote... 1.470214e+00 \n", - "2 SSP2-RCP19 EF v3.1 - climate change - global warming pote... 2.006539e+07 \n", - "3 SSP2-RCP19 EF v3.1 - climate change - global warming pote... 3.311854e+05 \n", - "4 SSP2-RCP19 EF v3.1 - climate change - global warming pote... 1.060831e+06 " + "0 Industry_Food and Tobacco_Electricity 2005 BRA CL image \n", + "1 Industry_Food and Tobacco_Electricity 2005 BRA RER image \n", + "2 Industry_Food and Tobacco_Electricity 2005 BRA RNA image \n", + "3 Industry_Food and Tobacco_Electricity 2005 BRA DE image \n", + "4 Industry_Food and Tobacco_Electricity 2005 BRA US image \n", + "\n", + " scenario impact_category value \n", + "0 SSP2-RCP19 RELICS - metals extraction - Lithium 2.957607e-02 \n", + "1 SSP2-RCP19 RELICS - metals extraction - Lithium 5.798639e-06 \n", + "2 SSP2-RCP19 RELICS - metals extraction - Lithium 4.660310e-06 \n", + "3 SSP2-RCP19 RELICS - metals extraction - Lithium 6.650308e-08 \n", + "4 SSP2-RCP19 RELICS - metals extraction - Lithium 7.912068e-07 " ] }, - "execution_count": 75, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -835,25 +913,17 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 10, "id": "5f2c4b3b-b26c-47ee-8c36-582feb027f58", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,\n", - " 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026,\n", - " 2027, 2028, 2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037,\n", - " 2038, 2039, 2040, 2041, 2042, 2043, 2044, 2045, 2046, 2047, 2048,\n", - " 2049, 2050, 2051, 2052, 2053, 2054, 2055, 2056, 2057, 2058, 2059,\n", - " 2060, 2061, 2062, 2063, 2064, 2065, 2066, 2067, 2068, 2069, 2070,\n", - " 2071, 2072, 2073, 2074, 2075, 2076, 2077, 2078, 2079, 2080, 2081,\n", - " 2082, 2083, 2084, 2085, 2086, 2087, 2088, 2089, 2090, 2091, 2092,\n", - " 2093, 2094, 2095, 2096, 2097, 2098, 2099])" + "array([2005, 2010, 2020, 2030, 2040, 2050, 2060, 2070, 2080, 2090, 2100])" ] }, - "execution_count": 76, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -864,7 +934,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 11, "id": "1de1d921-4156-46b8-9113-f94214320f50", "metadata": {}, "outputs": [], @@ -875,7 +945,7 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 12, "id": "57b097ed-0108-4d62-8aaf-0a3d7c76fc48", "metadata": {}, "outputs": [], @@ -888,7 +958,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 13, "id": "c0b92aed-c162-4f5a-b200-512452d5157e", "metadata": {}, "outputs": [], @@ -898,7 +968,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 14, "id": "829eef08-5bc8-4ee3-8d51-fd55e2beb2b0", "metadata": {}, "outputs": [], @@ -908,7 +978,7 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 15, "id": "11c1861f-1a2b-49f6-a1be-94680d5a6b30", "metadata": {}, "outputs": [], @@ -920,17 +990,17 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 16, "id": "9acd0483-a477-4250-a28a-aa0d2f510509", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1816549" + "28605" ] }, - "execution_count": 82, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -941,7 +1011,7 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 17, "id": "5b5dde7a-3eb9-4f1d-997e-3c6310e76580", "metadata": {}, "outputs": [], @@ -952,17 +1022,17 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 18, "id": "3361c4c8-c006-4f09-85f4-9d50c10f327f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "1816549" + "28605" ] }, - "execution_count": 84, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -973,7 +1043,7 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 19, "id": "87646c30-91c9-4dcf-b4a0-a52471634580", "metadata": {}, "outputs": [ @@ -992,10 +1062,10 @@ " " ], "text/plain": [ - "" + "" ] }, - "execution_count": 85, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } diff --git a/pathways/data_validation.py b/pathways/data_validation.py index 4c7a846..cfcf355 100644 --- a/pathways/data_validation.py +++ b/pathways/data_validation.py @@ -3,14 +3,19 @@ in the datapackage.json file. """ -import json -from pathlib import Path +import logging import datapackage import pandas as pd import yaml from datapackage import DataPackageException, validate +logging.basicConfig(level=logging.DEBUG, + filename='pathways.log', # Log file to save the entries + filemode='a', # Append to the log file if it exists, 'w' to overwrite + format='%(asctime)s - %(levelname)s - %(module)s - %(message)s', + datefmt='%Y-%m-%d %H:%M:%S') + def validate_datapackage(datapackage: datapackage.DataPackage): """ @@ -57,13 +62,10 @@ def validate_datapackage(datapackage: datapackage.DataPackage): dataframe = pd.DataFrame(data, columns=headers) # Check that the scenario data is valid - # validate_scenario_data(dataframe) + validate_scenario_data(dataframe) # Check that the mapping is valid - # validate_mapping(datapackage.get_resource("mapping"), dataframe) - - # Check that the LCA data is valid - # validate_lca_data(datapackage) + validate_mapping(datapackage.get_resource("mapping"), dataframe) return datapackage, dataframe @@ -124,18 +126,3 @@ def validate_mapping(resource: datapackage.Resource, dataframe: pd.DataFrame): f"Duplicate values: {set([x for x in scenario_variables if scenario_variables.count(x) > 1])}" ) - -def validate_lca_data(datapackage): - """ - Ensure that the required LCA data is present and valid. - :param datapackage: - :return: - """ - - # Check that resources `exchanges` and `labels` are present - required_resources = ["exchanges", "labels"] - for resource in required_resources: - try: - datapackage.get_resource(resource).read() - except DataPackageException: - raise ValueError(f"Missing resource: {resource}") diff --git a/pathways/lca.py b/pathways/lca.py index a51c684..bf01a54 100644 --- a/pathways/lca.py +++ b/pathways/lca.py @@ -1,12 +1,11 @@ import csv from pathlib import Path from typing import Any, Dict, List, Tuple +import logging import bw_processing as bwp import numpy as np -import sparse as sp from bw_processing import Datapackage -from numpy import dtype, ndarray from scipy import sparse from scipy.sparse import csr_matrix @@ -23,6 +22,13 @@ print("Solver: scikits.umfpack") +logging.basicConfig(level=logging.DEBUG, + filename='pathways.log', # Log file to save the entries + filemode='a', # Append to the log file if it exists, 'w' to overwrite + format='%(asctime)s - %(levelname)s - %(module)s - %(message)s', + datefmt='%Y-%m-%d %H:%M:%S') + + def read_indices_csv(file_path: Path) -> Dict[Tuple[str, str, str, str], str]: """ @@ -105,7 +111,7 @@ def get_lca_matrices( dirpath / "A_matrix.csv", ) - b_data, b_indices, _ = load_matrix_and_index( + b_data, b_indices, b_sign = load_matrix_and_index( dirpath / "B_matrix.csv", ) @@ -118,14 +124,15 @@ def get_lca_matrices( dp.add_persistent_vector( matrix="biosphere_matrix", indices_array=b_indices, - data_array=b_data, + data_array=b_data * -1, + flip_array=b_sign, ) return dp, A_inds, B_inds def fill_characterization_factors_matrices( - biosphere_flows: dict, methods, biosphere_dict + biosphere_flows: dict, methods, biosphere_dict, debug=False ) -> csr_matrix: """ Create one CSR matrix for all LCIA method, with the last dimension being the index of the method @@ -143,6 +150,11 @@ def fill_characterization_factors_matrices( dtype=np.float64, ) + if debug: + logging.info(f"LCIA matrix shape: {matrix.shape}") + + l = [] + for m, method in enumerate(methods): method_data = lcia_data[method] for flow_idx, f in biosphere_dict.items(): @@ -150,6 +162,13 @@ def fill_characterization_factors_matrices( flow = reversed_biosphere_flows[flow_idx] if flow in method_data: matrix[m, f] = method_data[flow] + l.append((method, flow, f, method_data[flow])) + if debug: + # sort l by method and flow + l = sorted(l, key=lambda x: (x[0], x[1])) + for x in l: + method, flow, f, value = x + logging.info(f"LCIA method: {method}, Flow: {flow}, Index: {f}, Value: {value}") return matrix diff --git a/pathways/pathways.py b/pathways/pathways.py index e47f23d..9aa7348 100644 --- a/pathways/pathways.py +++ b/pathways/pathways.py @@ -9,8 +9,8 @@ import warnings from collections import defaultdict from multiprocessing import Pool, cpu_count -from pathlib import Path -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Dict, List, Optional, Tuple +import logging import bw2calc as bc import numpy as np @@ -45,7 +45,7 @@ warnings.filterwarnings("ignore") -def check_unclassified_activities(A_index, classifications): +def check_unclassified_activities(technosphere_indices, classifications) -> List: """ Check if there are activities in the technosphere matrix that are not in the classifications. :param A: @@ -53,23 +53,22 @@ def check_unclassified_activities(A_index, classifications): :return: """ missing_classifications = [] - for act in A_index: + for act in technosphere_indices: if act not in classifications: missing_classifications.append(list(act)) if missing_classifications: - print( - f"{len(missing_classifications)} activities are not found in the classifications. " - "There are exported in the file 'missing_classifications.csv'." - ) + with open("missing_classifications.csv", "a") as f: writer = csv.writer(f) writer.writerows(missing_classifications) + return missing_classifications + def csv_to_dict(filename): output_dict = {} - with open(filename, "r") as file: + with open(filename, encoding="utf-8") as file: reader = csv.reader(file, delimiter=";") for row in reader: # Making sure there are at least 5 items in the row @@ -78,6 +77,8 @@ def csv_to_dict(filename): key = tuple(row[:4]) value = row[4] output_dict[int(value)] = key + else: + logging.warning(f"Row {row} has less than 5 items.") return output_dict @@ -160,6 +161,11 @@ def fetch_indices(mapping, regions, variables, A_index, geo): print("Warning: mismatch between activities and variables.") print(f"Number of variables: {len(variables)}: {variables}") print(f"Number of datasets: {len(activities)}: {activities}") + logging.warning( + f"Mismatch between activities and variables for region {region}." + f"Number of variables: {len(variables)}." + f"Number of datasets: {len(activities)}." + ) idxs = _get_activity_indices(activities, A_index, geo) @@ -184,7 +190,10 @@ def fetch_inventories_locations(A_index: Dict[str, Tuple[str, str, str]]) -> Lis :return: List of locations. """ - return list(set([act[3] for act in A_index])) + locations = list(set([act[3] for act in A_index])) + logging.info(f"Unique locations in LCA database: {locations}") + + return locations def group_technosphere_indices_by_category( @@ -259,11 +268,11 @@ def process_region(data: Tuple) -> dict[str, ndarray[Any, dtype[Any]] | list[int rev_A_index, lca, characterization_matrix, + debug ) = data - FU = [] - variables_demand = [] - variables_idx = [] + variables_demand = {} + d = [] for v, variable in enumerate(variables): idx, dataset = vars_idx[variable]["idx"], vars_idx[variable]["dataset"] @@ -296,27 +305,34 @@ def process_region(data: Tuple) -> dict[str, ndarray[Any, dtype[Any]] | list[int ) < demand_cutoff: continue - FU.append( - { - idx: demand.values * float(unit_vector), - } - ) - variables_demand.append(variable) - variables_idx.append(v) + variables_demand[variable] = { + "id": idx, + "demand": demand.values * float(unit_vector), + } - d = [] - for f, fu in enumerate(FU): - lca.lci(fu) - d.append((characterization_matrix @ lca.inventory).toarray()) + lca.lci(demand={idx: demand.values * float(unit_vector)}) + characterized_inventory = (characterization_matrix @ lca.inventory).toarray() + d.append(characterized_inventory) + + if debug: + logging.info( + f"var.: {variable}, name: {dataset[0][:50]}, " + f"ref.: {dataset[1]}, unit: {dataset[2][:50]}, idx: {idx}," + f"loc.: {dataset[3]}, demand: {round(float(demand.values * float(unit_vector)), 2)}, " + f"unit conv.: {unit_vector}, " + f"impact: {round(float(characterized_inventory.sum(axis=-1) / (demand.values * float(unit_vector))), 3)}. " + ) id_array = uuid.uuid4() - np.save(file=DIR_CACHED_DB / f"{id_array}.npy", arr=np.stack(d, axis=0)) + np.save(file=DIR_CACHED_DB / f"{id_array}.npy", arr=np.stack(d)) + + del d # concatenate the list of sparse matrices and # add a third dimension and concatenate along it return { "id_array": id_array, - "variables": {v: idx for v, idx in zip(variables_demand, variables_idx)}, + "variables": {k: v["demand"] for k, v in variables_demand.items()}, } @@ -336,9 +352,14 @@ def _calculate_year(args): classifications, scenarios, reverse_classifications, + debug, ) = args print(f"------ Calculating LCA results for {year}...") + if debug: + logging.info(f"############################### " + f"{model}, {scenario}, {year} " + f"###############################") geo = Geomap(model=model) @@ -350,7 +371,8 @@ def _calculate_year(args): except FileNotFoundError: # If LCA matrices can't be loaded, skip to the next iteration - print("LCA matrices not found for the given model, scenario, and year.") + if debug: + logging.warning(f"Skipping {model}, {scenario}, {year}, as data not found.") return # Fetch indices @@ -360,7 +382,14 @@ def _calculate_year(args): # A = remove_double_counting(A, vars_info) # check unclassified activities - check_unclassified_activities(technosphere_indices, classifications) + missing_classifications = check_unclassified_activities(technosphere_indices, classifications) + + if missing_classifications: + if debug: + logging.warning( + f"{len(missing_classifications)} activities are not found in the classifications." + "See missing_classifications.csv for more details." + ) # Initialize list to store activities not found in classifications missing_class = [] @@ -420,9 +449,12 @@ def _calculate_year(args): lca.lci(factorize=True) characterization_matrix = fill_characterization_factors_matrices( - biosphere_indices, methods, lca.dicts.biosphere + biosphere_indices, methods, lca.dicts.biosphere, debug ) + if debug: + logging.info(f"Characterization matrix created. Shape: {characterization_matrix.shape}") + bar = pyprind.ProgBar(len(regions)) for region in regions: bar.update() @@ -449,6 +481,7 @@ def _calculate_year(args): reverse_technosphere_index, lca, characterization_matrix, + debug ) ) @@ -465,11 +498,12 @@ class Pathways: Path to the datapackage.json file. """ - def __init__(self, datapackage): + def __init__(self, datapackage, debug=False): self.datapackage = datapackage self.data, dataframe = validate_datapackage(self.read_datapackage()) self.mapping = self.get_mapping() self.mapping.update(self.get_final_energy_mapping()) + self.debug = debug self.scenarios = self.get_scenarios(dataframe) self.classifications = load_classifications() @@ -485,6 +519,16 @@ def __init__(self, datapackage): clean_cache_directory() + if self.debug: + logging.basicConfig(level=logging.DEBUG, + filename='pathways.log', # Log file to save the entries + filemode='a', # Append to the log file if it exists, 'w' to overwrite + format='%(asctime)s - %(levelname)s - %(module)s - %(message)s', + datefmt='%Y-%m-%d %H:%M:%S') + logging.info("#" * 600) + logging.info(f"Pathways initialized with datapackage: {datapackage}") + + def read_datapackage(self) -> DataPackage: """Read the datapackage.json file. @@ -580,7 +624,8 @@ def get_scenarios(self, scenario_data: pd.DataFrame) -> xr.DataArray: # check if all variables in mapping are in scenario_data for var in mapping_vars: if var not in scenario_data["variables"].values: - print(f"Variable {var} not found in scenario data.") + if self.debug: + logging.warning(f"Variable {var} not found in scenario data.") # remove rows which do not have a value under the `variable` # column that correspond to any value in self.mapping for `scenario variable` @@ -634,7 +679,7 @@ def calculate( flows: Optional[List[str]] = None, multiprocessing: bool = False, demand_cutoff: float = 1e-3, - ) -> dict[Any, Any] | dict[int | Any, dict[Any, dict[str, Any]] | None]: + ) -> None: """ Calculate Life Cycle Assessment (LCA) results for given methods, models, scenarios, regions, and years. @@ -675,18 +720,30 @@ def calculate( # Set default values if arguments are not provided if methods is None and characterization is True: methods = get_lcia_method_names() + if self.debug: + logging.info(f"Using the following LCIA methods: {methods}") if models is None: models = self.scenarios.coords["model"].values models = [m.lower() for m in models] + if self.debug: + logging.info(f"Using the following models: {models}") if scenarios is None: scenarios = self.scenarios.coords["pathway"].values + if self.debug: + logging.info(f"Using the following scenarios: {scenarios}") if regions is None: regions = self.scenarios.coords["region"].values + if self.debug: + logging.info(f"Using the following regions: {regions}") if years is None: years = self.scenarios.coords["year"].values + if self.debug: + logging.info(f"Using the following years: {years}") if variables is None: variables = self.scenarios.coords["variables"].values variables = [str(v) for v in variables] + if self.debug: + logging.info(f"Using the following variables: {variables}") # resize self.scenarios array to fit the given arguments self.scenarios = resize_scenario_data( @@ -745,6 +802,7 @@ def calculate( self.classifications, self.scenarios, self.reverse_classifications, + self.debug ) for year in years ] @@ -777,6 +835,7 @@ def calculate( self.classifications, self.scenarios, self.reverse_classifications, + self.debug ) ) for year in years @@ -821,6 +880,7 @@ def fill_in_result_array(self, results: dict): for loc, act_loc_idx in acts_location_idx_dict.items(): idx = np.intersect1d(act_cat_idx, act_loc_idx) idx = idx[idx != -1] + summed_data = d[..., idx].sum(axis=-1) if idx.size > 0: self.lca_results.loc[ @@ -833,7 +893,7 @@ def fill_in_result_array(self, results: dict): "location": loc, "variable": list(variables.keys()), } - ] = d[..., idx].sum(axis=-1) + ] = summed_data def characterize_planetary_boundaries( self,