Skip to content

Commit

Permalink
clarify some things in the notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoDiepers committed May 4, 2024
1 parent dfec42a commit bd1944f
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 188 deletions.
174 changes: 133 additions & 41 deletions docs/content/examples/example_ev.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@
"source": [
"import bw2data as bd\n",
"\n",
"project_name = \"timex_example_electric_vehicle\"\n",
"if project_name in bd.projects:\n",
" bd.projects.delete_project(project_name)\n",
" bd.projects.purge_deleted_directories()\n",
"\n",
"bd.projects.set_current(project_name)"
"bd.projects.set_current(\"timex_example_electric_vehicle\")"
]
},
{
Expand Down Expand Up @@ -72,8 +67,9 @@
"metadata": {},
"outputs": [],
"source": [
"del bd.databases[\"foreground\"] # to make sure we create the foreground from scratch\n",
"foreground = bd.Database(\"foreground\")\n",
"foreground.write({}) # to make sure we start from scratch"
"foreground.write({}) "
]
},
{
Expand Down Expand Up @@ -272,7 +268,7 @@
"output_type": "stream",
"text": [
"td_production.date [s]: [-126227808 -94670856 -63113904 -31556952 0]\n",
"td_production.amount [%]: [0. 0.16666667 0.33333333 0.5 0. ]\n"
"td_production.amount [-]: [0. 0.16666667 0.33333333 0.5 0. ]\n"
]
},
{
Expand All @@ -288,7 +284,7 @@
],
"source": [
"print(\"td_production.date [s]: \", td_production.date)\n",
"print(\"td_production.amount [%]:\", td_production.amount)\n",
"print(\"td_production.amount [-]:\", td_production.amount)\n",
"type(td_production)"
]
},
Expand Down Expand Up @@ -407,8 +403,7 @@
"\n",
"TimexLCA calculates:\n",
" 1) a static LCA score (`TimexLCA.static_lca.score`, same as `bw2calc.lca.score`),\n",
" 2) a static time-explicit LCA score (`TimexLCA.score`), which links LCIs to the respective background databases\n",
" but without additional temporal dynamics of the biosphere flows \n",
" 2) a static time-explicit LCA score (`TimexLCA.score`), which links LCIs to the respective background databases but without additional temporal dynamics of the biosphere flows,\n",
" 3) a dynamic time-explicit LCA score (`TimexLCA.dynamic_score`), with dynamic inventory and dynamic charaterization factors. These are provided for radiative forcing and GWP but can also be user-defined.\n",
"\n",
"Example\n",
Expand All @@ -425,7 +420,7 @@
">>> timex_lca.dynamic_lcia(metric=\"radiative_forcing\") # different metrics can be used, e.g. \"GWP\", \"radiative_forcing\"\n",
">>> timex_lca.dynamic_score\n",
"\u001b[0;31mInit docstring:\u001b[0m\n",
"Initializes the `TimexLCA` object. Calculates a static LCA, initializes time mapping dicts for activities and biosphere flows, and stores useful subsets of ids in the node_id_collection_dict.\n",
"Instantiating a `TimexLCA` object calculates a static LCA, initializes time mapping dicts for activities and biosphere flows, and stores useful subsets of ids in the node_id_collection_dict.\n",
"\n",
"Parameters\n",
"----------\n",
Expand Down Expand Up @@ -488,7 +483,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/timodiepers/Documents/Coding/timex/timex_lca/timex_lca.py:175: UserWarning: No edge filter function provided. Skipping all edges within background databases.\n",
"/Users/timodiepers/Documents/Coding/timex/timex_lca/timex_lca.py:182: UserWarning: No edge filter function provided. Skipping all edges within background databases.\n",
" warnings.warn(\n"
]
},
Expand Down Expand Up @@ -918,9 +913,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we calculate the LCI.\n",
"\n",
"If we are only interested in the new overall time-explicit scores and don't care about the timing of the emissions, we can set `build_dynamic_biosphere`=`False`, which saves time and memory."
"Next, we calculate the time-explicit LCI. The `TimexLCA.lci()` function takes care of all the relinking, based on the information from the timeline. "
]
},
{
Expand All @@ -936,12 +929,95 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The static results without dynamic characterization:"
"Taking a look at the `dynamic_inventory` that was now created, we can see that it has more rows (emissions) than our usual biosphere3 flows. Instead of one row for each emission in the biosphere database we now get one row for each emission at each point in time."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<51009x72345 sparse matrix of type '<class 'numpy.float64'>'\n",
"\twith 63817 stored elements in Compressed Sparse Row format>"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tlca.dynamic_inventory"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The standard, non-dynamic inventory has far less rows because the temporal resolution is missing. Looking at the timeline again, we see that we have processes at 21 different points in time, which should exactly match the ratio of the dimensions of our two inventories:"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<2429x72345 sparse matrix of type '<class 'numpy.float64'>'\n",
"\twith 973421 stored elements in Compressed Sparse Row format>"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tlca.inventory"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"21.0"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tlca.dynamic_inventory.shape[0] / tlca.inventory.shape[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we are only interested in the new overall time-explicit scores and don't care about the timing of the emissions, we can set `build_dynamic_biosphere=False` (default is `True`), which saves time and memory. In that case, you only get the `TimexLCA.inventory`, but not the `TimexLCA.dynamic_inventory`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In case the timing of emissions is not important, one can directly calculate the LCIA the \"standard way\" using static characterization methods. Per default, the following calculates the static lcia score based on the impact method chosen in the very beginning:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 48,
"metadata": {},
"outputs": [
{
Expand All @@ -950,7 +1026,7 @@
"15697.671861435201"
]
},
"execution_count": 20,
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -965,9 +1041,11 @@
"metadata": {},
"source": [
"## Dynamic Characterization\n",
"The time-explicit inventory generated by a `TimexLCA` allow for dynamic characterization. Users can provide their own dynamic characterization functions and link them to corresponding biosphere flows (see example on [dynamic characterization](https://github.com/TimoDiepers/timex/blob/main/notebooks/example_simple_dynamic_characterization.ipynb)), or use the ones we provide out of the box. We provide two different metrics for dynamic LCIA of Climate Change: Radiative forcing [W/m2] and Global Warming Potential (GWP) [kg CO2-eq]. For both of these metrics, we have parameterized dynamic characterization functions for all GHG's that [IPCC AR6](https://www.ipcc.ch/report/ar6/wg1/chapter/chapter-7/) provides data for.\n",
"In addition to the standard static characterization, the time-explicit, dynamic inventory generated by a `TimexLCA` allows for dynamic characterization. Users can provide their own dynamic characterization functions and link them to corresponding biosphere flows (see example on [dynamic characterization](https://github.com/TimoDiepers/timex/blob/main/notebooks/example_simple_dynamic_characterization.ipynb)), or use the ones we provide out of the box. \n",
"\n",
"We provide two different metrics for dynamic LCIA of Climate Change: Radiative forcing [W/m2] and Global Warming Potential (GWP) [kg CO2-eq]. For both of these metrics, we have parameterized dynamic characterization functions for all GHG's that [IPCC AR6](https://www.ipcc.ch/report/ar6/wg1/chapter/chapter-7/) provides data for.\n",
"\n",
"For dynamic LCIA metrics, users can select the length of the considered time horizon (`time_horizon`) and whether it is a fixed time horizon (`fixed_time_horizon`). Fixed means that the time horizon for all emissions (no matter when they occur) starts counting at the time of the functional unit, resulting in shorter time horizons for emissions occuring later. If the time horizon is not fixed (this is what conventional impact assessment factors assume), it starts counting from the timing of the emission.\n"
"For the dynamic characterization, users can also choose the length of the considered time horizon (`time_horizon`) and whether it is a fixed time horizon (`fixed_time_horizon`). Fixed means that the time horizon for all emissions (no matter when they occur) starts counting at the time of the functional unit, resulting in shorter time horizons for emissions occuring later. If the time horizon is not fixed (this is what conventional impact assessment factors assume), it starts counting from the timing of the emission.\n"
]
},
{
Expand All @@ -977,6 +1055,13 @@
"### Radiative forcing\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's characterize our dynamic inventory, regarding radiative forcing with a fixed time horizon and the default time horizon length of 100 years:"
]
},
{
"cell_type": "code",
"execution_count": 21,
Expand Down Expand Up @@ -1188,6 +1273,13 @@
"tlca.dynamic_lcia(metric=\"radiative_forcing\", fixed_time_horizon=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The method call returns a dataframe of all the individual emissions at their respective timesteps, but we can also just look at the overall score:"
]
},
{
"cell_type": "code",
"execution_count": 22,
Expand All @@ -1212,12 +1304,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Simple plotting functions are available:"
"To visualize the results, we provide a simple plotting functions:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -1244,7 +1336,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 53,
"metadata": {},
"outputs": [
{
Expand All @@ -1266,12 +1358,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Cumulative radiative forcing:"
"There is also a flag to plot the cumulative radiative forcing:"
]
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 54,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -1305,7 +1397,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 55,
"metadata": {},
"outputs": [
{
Expand All @@ -1324,7 +1416,7 @@
"15896.845450102795"
]
},
"execution_count": 26,
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1343,7 +1435,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 56,
"metadata": {},
"outputs": [
{
Expand All @@ -1370,7 +1462,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 57,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -1407,7 +1499,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 58,
"metadata": {},
"outputs": [
{
Expand All @@ -1426,7 +1518,7 @@
"15446.54411830293"
]
},
"execution_count": 29,
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1445,7 +1537,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 59,
"metadata": {},
"outputs": [
{
Expand All @@ -1454,7 +1546,7 @@
"31401.500705260198"
]
},
"execution_count": 37,
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1467,12 +1559,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"However, further down we also want to assess what part of the life cycle has what contribution. To get this info, we need some more calculations:"
"However, further down we also want to look at what part of the life cycle has what contribution. To get this info, we need some more calculations:"
]
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -1483,7 +1575,7 @@
" if e.input == ev_lifecycle.key:\n",
" continue\n",
" lca = bc.LCA({e.input: e.amount}, method)\n",
" lca.lci()\n",
" lca.lci() # one could probably do this more efficiently by using .redo_lcia, but who doesn't like a 15s break :)\n",
" lca.lcia()\n",
" static_scores[e.input[\"name\"]] = lca.score"
]
Expand All @@ -1492,12 +1584,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, we calculate the 2040 (prospective) scores:"
"Similarly, we calculate the 2040 (prospective) scores by just changing the database the exchanges point to:"
]
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -1536,7 +1628,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 62,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -1564,7 +1656,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 64,
"metadata": {},
"outputs": [
{
Expand Down
Loading

0 comments on commit bd1944f

Please sign in to comment.