Skip to content

Commit

Permalink
add starting datetime argument to build_timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoDiepers committed Sep 18, 2024
1 parent 58aafac commit 25e484a
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bw_timex/timeline_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TimelineBuilder:
def __init__(
self,
slca: LCA,
starting_datetime: datetime,
edge_filter_function: Callable,
database_date_dict: dict,
database_date_dict_static_only: dict,
Expand All @@ -45,6 +46,8 @@ def __init__(
----------
slca: LCA
A static LCA object.
starting_datetime: datetime | str, optional
Point in time when the demand occurs.
edge_filter_function: Callable
A callable that filters edges. If not provided, a function that always returns False is used.
database_date_dict: dict
Expand All @@ -65,6 +68,7 @@ def __init__(
Keyword arguments passed to the EdgeExtractor which inherits from TemporalisLCA.
"""
self.slca = slca
self.starting_datetime = starting_datetime
self.edge_filter_function = edge_filter_function
self.database_date_dict = database_date_dict
self.database_date_dict_static_only = database_date_dict_static_only
Expand All @@ -90,6 +94,7 @@ def __init__(

self.edge_extractor = EdgeExtractor(
slca,
starting_datetime=self.starting_datetime,
*args,
edge_filter_function=edge_filter_function,
cutoff=self.cutoff,
Expand Down
6 changes: 6 additions & 0 deletions bw_timex/timex_lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def __init__(

def build_timeline(
self,
starting_datetime: datetime | str = "now",
temporal_grouping: str = "year",
interpolation_type: str = "linear",
edge_filter_function: Callable = None,
Expand All @@ -178,6 +179,9 @@ def build_timeline(
Parameters
----------
starting_datetime: datetime | str, optional
Point in time when the demand occurs. This is the initial starting point of the
timeline. Something like `"now"` or `"2023-01-01"`. Default is `"now"`.
temporal_grouping : str, optional
Time resolution for grouping exchanges over time in the timeline. Default is 'year',
other options are 'month', 'day', 'hour'.
Expand Down Expand Up @@ -223,6 +227,7 @@ def build_timeline(
else:
self.edge_filter_function = edge_filter_function

self.starting_datetime = starting_datetime
self.temporal_grouping = temporal_grouping
self.interpolation_type = interpolation_type
self.cutoff = cutoff
Expand All @@ -237,6 +242,7 @@ def build_timeline(
# with the TimelineBuilder.build_timeline() method.
self.timeline_builder = TimelineBuilder(
self.base_lca,
self.starting_datetime,
self.edge_filter_function,
self.database_date_dict,
self.database_date_dict_static_only,
Expand Down
117 changes: 117 additions & 0 deletions tests/test_starting_datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import math
from datetime import datetime

import bw2data as bd
import pytest
from bw2data.tests import bw2test

from bw_timex import TimexLCA

@pytest.fixture
@bw2test
def ab_db():
bd.Database("bio").write(
{
("bio", "CO2"): {
"type": "biosphere",
"name": "carbon dioxide",
},
},
)

bd.Database("db_2030").write(
{
("db_2030", "B"): {
"name": "B",
"location": "somewhere",
"reference product": "B",
"exchanges": [
{
"amount": 1,
"type": "production",
"input": ("db_2030", "B"),
},
{
"amount": 10,
"input": ("bio", "CO2"),
"type": "biosphere",
},
],
},
},
)

bd.Database("db_2020").write(
{
("db_2020", "B"): {
"name": "B",
"location": "somewhere",
"reference product": "B",
"exchanges": [
{
"amount": 1,
"type": "production",
"input": ("db_2020", "B"),
},
{
"amount": 15,
"input": ("bio", "CO2"),
"type": "biosphere",
},
],
},
},
)

bd.Database("foreground").write(
{
("foreground", "A"): {
"name": "A",
"location": "somewhere",
"reference product": "A",
"exchanges": [
{
"amount": 1,
"type": "production",
"input": ("foreground", "A"),
},
{
"amount": 1,
"input": ("db_2020", "B"),
"type": "technosphere",
},
],
},
}
)

bd.Method(("GWP", "example")).write(
[
(("bio", "CO2"), 1),
]
)

def test_starting_datetime(ab_db):
method = ("GWP", "example")
database_date_dict = {
"db_2020": datetime.strptime("2020", "%Y"),
"db_2030": datetime.strptime("2030", "%Y"),
"foreground": "dynamic",
}
fu = ("foreground", "A")
tlca = TimexLCA({fu: 1}, method, database_date_dict)

tlca.build_timeline(starting_datetime="2020-01-01")
tlca.lci()
tlca.static_lcia()
assert math.isclose(tlca.static_score, 15, rel_tol=1e-9)

tlca.build_timeline(starting_datetime="2030-01-01")
tlca.lci()
tlca.static_lcia()
assert math.isclose(tlca.static_score, 10, rel_tol=1e-9)

tlca.build_timeline(starting_datetime="2025-01-01")
tlca.lci()
tlca.static_lcia()
assert math.isclose(tlca.static_score, 12.5, rel_tol=1e-3)

0 comments on commit 25e484a

Please sign in to comment.