Skip to content

Commit

Permalink
Fix subshares overwriting
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarojhahn committed Aug 23, 2024
1 parent fc9a954 commit a4c4d9d
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions pathways/subshares.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ def adjust_matrix_based_on_shares(
:return: Tuple containing the data, indices, and signs.
"""

final_amounts = defaultdict(float)

# get coordinates of nonzero values in the technosphere matrix
nz_row, nz_col = lca.technosphere_matrix.nonzero()

Expand All @@ -238,24 +240,44 @@ def get_nz_col_indices(index):
for technology in technologies.values():
technology["consumer_idx"] = get_nz_col_indices(technology["idx"])

list_indices, list_amounts = [], []

for tech_category, regions in shares_dict.items():
for region, technologies in regions.items():
for name, tech in technologies.items():
for consumer in tech["consumer_idx"]:
initial_amount = lca.technosphere_matrix[tech["idx"], consumer]
amounts = initial_amount * subshares[tech_category][year][name] * -1
list_amounts.append(tuple(amounts))
list_indices.append((tech["idx"], consumer))

subshare = subshares[tech_category][year][name]
logging.info(
f"Applying subshare for '{name}' in '{tech_category}' for year {year}: {subshare}"
)
# Distribute the initial amount based on the subshare of the current technology
split_amount = initial_amount * subshare * -1

# Add the split amount to the combined amount for this technology and consumer
final_amounts[(tech["idx"], consumer)] += split_amount

for other_supplier, other_supplier_tech in technologies.items():
if other_supplier != name:
amounts = (
initial_amount
* subshares[tech_category][year][other_supplier]
additional_amount = (
initial_amount
* subshares[tech_category][year][other_supplier]
* -1
)
list_indices.append((other_supplier_tech["idx"], consumer))
list_amounts.append(tuple(amounts * -1))
final_amounts[(other_supplier_tech["idx"], consumer)] += additional_amount

# Prepare the list of indices and amounts for the modified technosphere matrix
list_indices = []
list_amounts = []

# Now, append the combined amounts to list_amounts and list_indices
for (tech_idx, consumer_idx), total_amount in final_amounts.items():
list_indices.append((tech_idx, consumer_idx))
list_amounts.append(tuple(total_amount))

logging.info(
f"Final combined amount for tech index {tech_idx} to consumer index {consumer_idx}: {total_amount}"
)

indices = np.array(list_indices, dtype=bwp.INDICES_DTYPE)
data = np.array(list_amounts)
Expand Down

0 comments on commit a4c4d9d

Please sign in to comment.