bw_graph_tools.graph_traversal.utils

Classes

CachingSolver

Class which caches cumulative LCA scores during graph traversal.

Counter

Custom counter to have easy access to current value

Functions

get_demand_vector_for_activity(node, skip_coproducts, ...)

Get input matrix indices and amounts for a given activity. Ignores the reference production

Module Contents

class bw_graph_tools.graph_traversal.utils.CachingSolver(lca: bw2calc.LCA)[source]

Class which caches cumulative LCA scores during graph traversal.

_score_cache stores per-unit cumulative LCA scores (scalars) keyed by product index. The graph traversal only needs cumulative scores, not full supply vectors, so the batched scores method solves for several products at once following the same strategy as bw2calc.FastSupplyArraysMixin:

  • With PARDISO (pypardiso), all requested products are solved in a single multi-right-hand-side spsolve call, which reuses the cached factorization and is much faster than solving one product at a time.

  • Otherwise (UMFPACK / SuperLU), a single multi-right-hand-side solve is slower than reusing a cached factorization, so the LCA’s technosphere matrix is decomposed once (via decompose_technosphere) and each product is solved iteratively through lca.solver.

_unit_scores_iterative(indices: list[int]) numpy.ndarray[source]

Solve each of indices separately, reusing the LCA’s cached factorization.

A single multi-right-hand-side solve is slower than this under UMFPACK / SuperLU, so we mirror bw2calc.FastSupplyArraysMixin._calculate_umfpack. We make sure the technosphere matrix has been decomposed so that solve_linear_system reuses lca.solver instead of re-factorizing on every solve.

_unit_scores_pardiso(indices: list[int]) numpy.ndarray[source]

Solve all indices in a single multi-right-hand-side PARDISO solve.

add_to_cache(index: int, unit_score: float) None[source]

Store a pre-computed per-unit cumulative score (for a demand amount of 1).

in_cache(indices: set[int]) set[int][source]

Return all indices values which already have a cached score.

scores(indices: list[int], amounts: list[float]) list[float][source]

Compute cumulative LCA scores for several products in a single batched solve.

Parameters:
  • indices (list[int]) – Product (technosphere row) indices to demand, one unit each.

  • amounts (list[float]) – Demanded amount for each product index, in the same order.

Returns:

Cumulative LCA score for each (index, amount) pair, in input order.

Return type:

list[float]

set_score_row(characterized_biosphere: scipy.sparse.spmatrix) None[source]

Pre-compute the per-activity score row used to reduce supply vectors to scores.

characterized_biosphere is the characterization-times-biosphere matrix (biosphere flows by activities). Its column sums give, for each activity, the cumulative score per unit of supply, so that score_row @ supply equals (characterized_biosphere * supply).sum().

_score_cache[source]
lca[source]
score_row = None[source]
class bw_graph_tools.graph_traversal.utils.Counter[source]

Custom counter to have easy access to current value

value = -1[source]
bw_graph_tools.graph_traversal.utils.get_demand_vector_for_activity(node: bw_graph_tools.graph_traversal.graph_objects.Node, skip_coproducts: bool, matrix: scipy.sparse.spmatrix)[source]

Get input matrix indices and amounts for a given activity. Ignores the reference production exchanges and optionally other co-production exchanges.

Parameters:
  • node (Node) – Activity whose inputs we are iterating over

  • skip_coproducts (bool) – Whether or not to ignore positive production exchanges other than the reference product, which is always ignored

  • matrix (scipy.sparse.spmatrix) – Technosphere matrix

Returns:

  • row indices (list) – Integer row indices for products consumed by Node

  • amounts (list) – The amount of each product consumed, scaled to Node.supply_amount. Same order as row indices.