diff --git a/dev/generate datapackages.ipynb b/dev/generate datapackages.ipynb index 9b7498a..4c5c735 100644 --- a/dev/generate datapackages.ipynb +++ b/dev/generate datapackages.ipynb @@ -219,7 +219,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "a081c764-a942-4627-9197-fcd373e98b62", "metadata": {}, "outputs": [ @@ -236,55 +236,199 @@ "name": "stderr", "output_type": "stream", "text": [ - "0% [# ] 100% | ETA: 00:00:01" + "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.21e+13)\n", + " warnings.warn(msg, UmfpackWarning)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n" + ] + }, + { + "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.21e+13)\n", + " warnings.warn(msg, UmfpackWarning)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "0% [### ] 100% | ETA: 00:02:10/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.21e+13)\n", + " warnings.warn(msg, UmfpackWarning)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "[{35812: Size: 8B\n", - "array(6.18299776e+11)\n", - "Coordinates:\n", - " pathway Size: 8B\n", - "array(6.18299776e+11)\n", - "Coordinates:\n", - " pathway Size: 8B\n", - "array(5.26656812e+11)\n", - "Coordinates:\n", - " pathway Size: 8B\n", - "array(1.15323356e+12)\n", - "Coordinates:\n", - " pathway 2\u001b[0m dp \u001b[38;5;241m=\u001b[39m \u001b[43mp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcalculate\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[43m \u001b[49m\u001b[43mmethods\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\n\u001b[1;32m 4\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m#'EF v3.1 - climate change - global warming potential (GWP100)',\u001b[39;49;00m\n\u001b[1;32m 5\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mRELICS - metals extraction - Lithium\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 6\u001b[0m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 7\u001b[0m \u001b[43m \u001b[49m\u001b[43mregions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43mr\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mr\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mscenarios\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcoords\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mregion\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalues\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mr\u001b[49m\u001b[38;5;241;43m!=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mWorld\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 8\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m#regions=[\"WEU\",],\u001b[39;49;00m\n\u001b[1;32m 9\u001b[0m \u001b[43m \u001b[49m\u001b[43mscenarios\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mSSP2-RCP19\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 10\u001b[0m \u001b[43m \u001b[49m\u001b[43mvariables\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\n\u001b[1;32m 11\u001b[0m \u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mscenarios\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcoords\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mvariables\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalues\u001b[49m\n\u001b[1;32m 12\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43many\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mi\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mi\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mIndustry\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mTransport\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mHeating\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 13\u001b[0m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 14\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m#years=[2005, 2020, 2050, 2070, 2100],\u001b[39;49;00m\n\u001b[1;32m 15\u001b[0m \u001b[43m \u001b[49m\u001b[43mcharacterization\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 16\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m#flows=[\"Lithium - natural resource - in ground - kilogram\",],\u001b[39;49;00m\n\u001b[1;32m 17\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m#data_type=np.float32,\u001b[39;49;00m\n\u001b[1;32m 18\u001b[0m \u001b[43m \u001b[49m\u001b[43mmultiprocessing\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 19\u001b[0m \u001b[43m \u001b[49m\u001b[43mdemand_cutoff\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m0.1\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 20\u001b[0m \u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/GitHub/pathways/pathways/pathways.py:742\u001b[0m, in \u001b[0;36mPathways.calculate\u001b[0;34m(self, methods, models, scenarios, regions, years, variables, characterization, flows, multiprocessing, data_type, demand_cutoff)\u001b[0m\n\u001b[1;32m 739\u001b[0m bar\u001b[38;5;241m.\u001b[39mupdate()\n\u001b[1;32m 740\u001b[0m \u001b[38;5;66;03m# Iterate over each region\u001b[39;00m\n\u001b[1;32m 741\u001b[0m results\u001b[38;5;241m.\u001b[39mappend(\n\u001b[0;32m--> 742\u001b[0m \u001b[43mprocess_region\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 743\u001b[0m \u001b[43m \u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 744\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 745\u001b[0m \u001b[43m \u001b[49m\u001b[43mscenario\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 746\u001b[0m \u001b[43m \u001b[49m\u001b[43myear\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 747\u001b[0m \u001b[43m \u001b[49m\u001b[43mregion\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 748\u001b[0m \u001b[43m \u001b[49m\u001b[43mvariables\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 749\u001b[0m \u001b[43m \u001b[49m\u001b[43mvars_info\u001b[49m\u001b[43m[\u001b[49m\u001b[43mregion\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 750\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mscenarios\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 751\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmapping\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 752\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43munits\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 753\u001b[0m \u001b[43m \u001b[49m\u001b[43mdp\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 754\u001b[0m \u001b[43m \u001b[49m\u001b[43mA_index\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 755\u001b[0m \u001b[43m \u001b[49m\u001b[43mB_index\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 756\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreverse_classifications\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 757\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlca_results\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcoords\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 758\u001b[0m \u001b[43m \u001b[49m\u001b[43mflows\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 759\u001b[0m \u001b[43m \u001b[49m\u001b[43mdemand_cutoff\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 760\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 761\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 762\u001b[0m )\n\u001b[1;32m 764\u001b[0m \u001b[38;5;66;03m# Store results in the LCA results xarray\u001b[39;00m\n\u001b[1;32m 765\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m result \u001b[38;5;129;01min\u001b[39;00m results:\n", - "File \u001b[0;32m~/GitHub/pathways/pathways/pathways.py:303\u001b[0m, in \u001b[0;36mprocess_region\u001b[0;34m(data)\u001b[0m\n\u001b[1;32m 295\u001b[0m FU\u001b[38;5;241m.\u001b[39mappend(\n\u001b[1;32m 296\u001b[0m {\n\u001b[1;32m 297\u001b[0m idx: demand \u001b[38;5;241m*\u001b[39m \u001b[38;5;28mfloat\u001b[39m(unit_vector),\n\u001b[1;32m 298\u001b[0m }\n\u001b[1;32m 299\u001b[0m )\n\u001b[1;32m 301\u001b[0m \u001b[38;5;28mprint\u001b[39m(FU)\n\u001b[0;32m--> 303\u001b[0m lca \u001b[38;5;241m=\u001b[39m \u001b[43mbc\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mMultiLCA\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 304\u001b[0m \u001b[43m \u001b[49m\u001b[43mdemands\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mFU\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 305\u001b[0m \u001b[43m \u001b[49m\u001b[43mmethod_config\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdp\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 306\u001b[0m \u001b[43m \u001b[49m\u001b[43mdata_objs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43mdp\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 307\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 308\u001b[0m lca\u001b[38;5;241m.\u001b[39mlci()\n\u001b[1;32m 309\u001b[0m lca\u001b[38;5;241m.\u001b[39mlcia()\n", - "\u001b[0;31mTypeError\u001b[0m: MultiLCA.__init__() got an unexpected keyword argument 'demands'" + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "0% [#### ] 100% | ETA: 00:02:07/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.21e+13)\n", + " warnings.warn(msg, UmfpackWarning)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "0% [###### ] 100% | ETA: 00:02:12/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.21e+13)\n", + " warnings.warn(msg, UmfpackWarning)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n" + ] + }, + { + "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.21e+13)\n", + " warnings.warn(msg, UmfpackWarning)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "0% [######## ] 100% | ETA: 00:02:05/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.21e+13)\n", + " warnings.warn(msg, UmfpackWarning)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "0% [######### ] 100% | ETA: 00:01:57/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.21e+13)\n", + " warnings.warn(msg, UmfpackWarning)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n" + ] + }, + { + "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.21e+13)\n", + " warnings.warn(msg, UmfpackWarning)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "0% [########### ] 100% | ETA: 00:01:55/Users/romain/anaconda3/envs/pathways/lib/python3.11/site-packages/scikits/umfpack/umfpack.py:736: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 1.21e+13)\n", + " warnings.warn(msg, UmfpackWarning)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n" + ] + }, + { + "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.21e+13)\n", + " warnings.warn(msg, UmfpackWarning)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n" ] } ], @@ -292,8 +436,8 @@ "import numpy as np\n", "dp = p.calculate(\n", " methods=[\n", - " #'EF v3.1 - climate change - global warming potential (GWP100)',\n", - " 'RELICS - metals extraction - Lithium',\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", " #regions=[\"WEU\",],\n", @@ -313,32 +457,173 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, + "id": "c9786349-3b91-4439-8494-208ba7bc3b4a", + "metadata": {}, + "outputs": [], + "source": [ + "import bw2calc as bc\n", + "import bw_processing as bwp" + ] + }, + { + "cell_type": "code", + "execution_count": 4, "id": "dedf1bb9-47a2-497c-b788-7fa64986827a", "metadata": {}, + "outputs": [ + { + "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.21e+13)\n", + " warnings.warn(msg, UmfpackWarning)\n" + ] + }, + { + "data": { + "text/plain": [ + "20.305023566997583" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lca = bc.LCA(\n", + " demand={1329: 1},\n", + " data_objs=[dp],\n", + ")\n", + "lca.lci()\n", + "lca.lcia()\n", + "lca.score" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "f501423c-de64-4604-9409-2a40c77c32bb", + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "\u001b[0;31mSignature:\u001b[0m \u001b[0mdp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdehydrated_interfaces\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mList\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mSource:\u001b[0m \n", - " \u001b[0;32mdef\u001b[0m \u001b[0mdehydrated_interfaces\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mList\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0;34m\"\"\"Return a list of the resource groups which have dehydrated interfaces\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"group\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mindex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mobj\u001b[0m \u001b[0;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresources\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mUndefinedInterface\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mFile:\u001b[0m /opt/homebrew/Caskroom/miniforge/base/envs/pathways/lib/python3.11/site-packages/bw_processing/datapackage.py\n", - "\u001b[0;31mType:\u001b[0m method" + "[array([( 0, 0), ( 0, 11778), ( 0, 17244), ...,\n", + " (36540, 21436), (36540, 19141), (36540, 24252)],\n", + " dtype=[('row', ' 1: - # dp.add_persistent_array( - # matrix='characterization_matrix', - # indices_array=c_indices, - # data_array=c_data, - # flip_array=c_sign, - # ) - # else: - # dp.add_persistent_vector( - # matrix='characterization_matrix', - # indices_array=c_indices, - # data_array=c_data, - # flip_array=c_sign, - # ) + c_data, c_indices = fill_characterization_factors_matrix( + B_inds, methods + ) + + dp.add_persistent_vector( + matrix='characterization_matrix', + indices_array=c_indices, + data_array=c_data, + ) return dp, A_inds, B_inds @@ -188,7 +176,7 @@ def get_lca_matrices( def fill_characterization_factors_matrix( biosphere_flows: dict, methods -) -> np.ndarray: +) -> Tuple[np.ndarray, np.ndarray]: """ Create a characterization matrix based on the list of biosphere flows given. @@ -199,24 +187,24 @@ def fill_characterization_factors_matrix( lcia_data = get_lcia_methods(methods=methods) - print(lcia_data) - - - # create a numpy array filled with zeros - # of size equal to biosphere_flows and lcia methods + biosphere_flows = { + k[:3]:v for k, v in biosphere_flows.items() + } - cf_matrix = np.zeros((len(biosphere_flows), len(methods))) + list_flows = [] + list_cf = [] - # fill the matrix - for i, flow in enumerate(biosphere_flows): - for j, method in enumerate(methods): - try: - cf_matrix[i, j] = lcia_data[method][flow[:3]] - except KeyError: - continue + for method, flows in lcia_data.items(): + for flow, cf in flows.items(): + flow_idx = int(biosphere_flows.get(flow)) + if flow_idx: + list_flows.append((flow_idx, flow_idx)) + list_cf.append(cf) - return cf_matrix + data = np.array(list_cf) + indices = np.array(list_flows, dtype=bwp.INDICES_DTYPE) + return data, indices def remove_double_counting(A: csr_matrix, vars_info: dict) -> csr_matrix: """ Remove double counting from a technosphere matrix. diff --git a/pathways/pathways.py b/pathways/pathways.py index 41c16d5..13d3f41 100644 --- a/pathways/pathways.py +++ b/pathways/pathways.py @@ -239,15 +239,15 @@ def process_region(data: Tuple) -> Union[None, Dict[str, Any]]: act_categories = lca_results_coords["act_category"].values # if lcia_matrix is not None: - # impact_categories = lca_results_coords["impact_category"].values - # target = np.zeros( - # ( - # len(act_categories), - # len(list(vars_idx)), - # len(locations), - # len(impact_categories), - # ) - # ) + impact_categories = lca_results_coords["impact_category"].values + target = np.zeros( + ( + len(act_categories), + len(list(vars_idx)), + len(locations), + len(impact_categories), + ) + ) # else: # if flows is not None: # target = np.zeros( @@ -260,6 +260,7 @@ def process_region(data: Tuple) -> Union[None, Dict[str, Any]]: FU = [] + vars_list = [] for v, variable in enumerate(variables): idx, dataset = vars_idx[variable]["idx"], vars_idx[variable]["dataset"] @@ -298,57 +299,42 @@ def process_region(data: Tuple) -> Union[None, Dict[str, Any]]: } ) - print(FU) + vars_list.append(v) - lca = bc.MultiLCA( - demands=FU, - method_config=dp, + lca = bc.LCA( + demand=FU[0], data_objs=[dp], ) lca.lci() - lca.lcia() - lca.score + # Populate the result array + act_locs = [a[-1] for a in rev_A_index.values()] + # Initialize the new array with zeros for missing data + E = np.zeros((len(A_index), len(locations), len(impact_categories))) - # - # - # # Create the demand vector - # f = create_demand_vector([idx], A, demand, unit_vector) - # - # # Solve the inventory - # C = solve_inventory(A, B, f) - # - # if lcia_matrix is not None: - # if lcia_matrix.ndim != 2 or lcia_matrix.shape[0] != B.shape[1]: - # raise ValueError("Incompatible dimensions between B and lcia_matrix") - # - # # Solve the LCA problem to get the LCIA scores - # D = characterize_inventory(C, lcia_matrix) - # - # # Sum along the first axis of D to get final result - # D = D.sum(axis=1) - # - # # Initialize the new array with zeros for missing data - # E = np.zeros((len(A_index), len(locations), len(impact_categories))) - # - # # Populate the result array - # act_locs = [a[-1] for a in rev_A_index.values()] - # - # for i, act in enumerate(rev_A_index.values()): - # if act[-1] in act_locs: - # loc_idx = location_to_index[act[-1]] - # E[i, loc_idx, :] = D[i, :] - # - # acts_idx = generate_A_indices( - # A_index, - # reverse_classifications, - # lca_results_coords, - # ) - # - # # Sum over the first axis of D, - # # using acts_idx for advanced indexing - # target[:, v] = E[acts_idx, ...].sum(axis=0) + acts_idx = generate_A_indices( + A_index, + reverse_classifications, + lca_results_coords, + ) + + for f, fu in enumerate(FU): + print(f) + lca.redo_lcia(fu) + + # Sum along the first axis of D to get final result + D = lca.characterized_inventory.sum(axis=0) + + for i, act in enumerate(rev_A_index.values()): + if act[-1] in act_locs: + loc_idx = location_to_index[act[-1]] + E[i, loc_idx] = D[:, i] + + + # Sum over the first axis of D, + # using acts_idx for advanced indexing + target[:, vars_list[f]] = E[acts_idx, ...].sum(axis=0) # # else: # # else, just sum the results of the inventory @@ -389,9 +375,7 @@ def get_indices(): return { "act_category": act_categories, "variable": list(vars_idx.keys()), - "impact_category": ( - impact_categories if lcia_matrix is not None else get_indices() - ), + "impact_category": impact_categories, "year": year, "region": region, "D": target, @@ -730,7 +714,6 @@ def calculate( # Process each region in parallel with Pool(cpu_count()) as p: results = p.map(process_region, data_for_regions) - else: results = [] # use pyprind diff --git a/requirements.txt b/requirements.txt index dd2058f..9e5c453 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,5 @@ xarray premise pyyaml scikit-umfpack +bw_processing +bw2calc == 2.0.DEV16 diff --git a/setup.py b/setup.py index 12afebd..060a835 100644 --- a/setup.py +++ b/setup.py @@ -49,7 +49,9 @@ def package_files(directory): "scipy", "premise", "pyyaml", - "scikit-umfpack" + "scikit-umfpack", + "bw_processing", + "bw2calc==2.0.DEV16" ], url="https://github.com/polca/premise", description="Scenario-level LCA of energy systems and transition pathways",