Skip to content

Commit df51e8d

Browse files
committed
Merge branch 'main' of github.com:brightway-lca/bw_timex
2 parents 1c7e680 + e1df0c4 commit df51e8d

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

bw_timex/dynamic_biosphere_builder.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ def build_dynamic_biosphere_matrix(
131131
A sparse matrix with the dimensions (bio_flows at a specific time step) x (processes).
132132
"""
133133

134+
lci_dict = {}
135+
temporal_market_lci_dict = {}
136+
134137
for row in self.timeline.itertuples():
135138
idx = row.time_mapped_producer
136139
if from_timeline:
@@ -145,6 +148,8 @@ def build_dynamic_biosphere_matrix(
145148
time,
146149
) = self.activity_time_mapping_dict.reversed()[idx]
147150

151+
152+
148153
if idx in self.node_id_collection_dict["temporalized_processes"]:
149154

150155
time_in_datetime = convert_date_string_to_datetime(
@@ -204,6 +209,7 @@ def build_dynamic_biosphere_matrix(
204209
col=process_col_index,
205210
amount=amount,
206211
)
212+
207213
elif idx in self.node_id_collection_dict["temporal_markets"]:
208214
(
209215
(original_db, original_code),
@@ -215,11 +221,24 @@ def build_dynamic_biosphere_matrix(
215221
else:
216222
demand = self.demand_from_technosphere(idx, process_col_index)
217223

224+
218225
if demand:
219-
self.lca_obj.redo_lci(demand)
220-
# aggregated biosphere flows of background supply chain emissions.
221-
# Rows are bioflows.
222-
aggregated_inventory = self.lca_obj.inventory.sum(axis=1)
226+
for act, amount in demand.items():
227+
# check if lci already calculated for this activity
228+
if not act in lci_dict.keys():
229+
self.lca_obj.redo_lci({act: 1})
230+
# biosphere flows by activity of background supply chain emissions.
231+
# Rows are bioflows. Columns are activities.
232+
# save for reuse in dict
233+
lci_dict[act] = self.lca_obj.inventory
234+
# add lci of both background activities of the temporal market and save total lci
235+
if idx not in temporal_market_lci_dict.keys():
236+
temporal_market_lci_dict[idx] = lci_dict[act] * amount
237+
else:
238+
temporal_market_lci_dict[idx] += lci_dict[act] * amount
239+
240+
241+
aggregated_inventory = temporal_market_lci_dict[idx].sum(axis=1)
223242

224243
for row_idx, amount in enumerate(aggregated_inventory.A1):
225244
bioflow = self.lca_obj.dicts.biosphere.reversed[row_idx]
@@ -254,7 +273,7 @@ def build_dynamic_biosphere_matrix(
254273
dynamic_biomatrix = sp.coo_matrix((self.values, (self.rows, self.cols)), shape)
255274
self.dynamic_biomatrix = dynamic_biomatrix.tocsr()
256275

257-
return self.dynamic_biomatrix
276+
return self.dynamic_biomatrix, temporal_market_lci_dict
258277

259278
def demand_from_timeline(self, row, original_db):
260279
"""
@@ -278,13 +297,9 @@ def demand_from_timeline(self, row, original_db):
278297
"""
279298
demand = {}
280299
for db, amount in row.interpolation_weights.items():
281-
[(timed_act_id, _)] = [
282-
(act, db_name)
283-
for (act, db_name) in self.interdatabase_activity_mapping[
284-
(row.producer, original_db)
285-
]
286-
if db == db_name
287-
]
300+
timed_act_id = self.interdatabase_activity_mapping.find_match(
301+
row.producer, db
302+
)
288303
demand[timed_act_id] = amount
289304
return demand
290305

bw_timex/timex_lca.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,8 @@ def calculate_dynamic_inventory(
595595
None
596596
calculates the dynamic inventory and stores it in the attribute
597597
`dynamic_inventory` as a matrix and in `dynamic_inventory_df` as a DataFrame.
598+
Also calculates and stores the lci of the temporal markets in the attribute
599+
self.temporal_market_lcis for use in contribution analysis of the background processes.
598600
599601
See also
600602
--------
@@ -624,7 +626,7 @@ def calculate_dynamic_inventory(
624626
self.interdatabase_activity_mapping,
625627
from_timeline=from_timeline,
626628
)
627-
self.dynamic_biomatrix = (
629+
self.dynamic_biomatrix, self.temporal_market_lcis = (
628630
self.dynamic_biosphere_builder.build_dynamic_biosphere_matrix(
629631
from_timeline=from_timeline
630632
)
@@ -1176,7 +1178,7 @@ def collect_temporalized_processes_from_timeline(self) -> None:
11761178

11771179
for producer, time_mapped_producer in unique_producers:
11781180
if (
1179-
bd.get_activity(producer)["database"]
1181+
self.nodes_dict[producer]["database"]
11801182
in self.database_date_dict_static_only.keys()
11811183
):
11821184
temporal_market_ids.add(time_mapped_producer)

0 commit comments

Comments
 (0)