Skip to content

Commit

Permalink
Cleanup matrix conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Jul 24, 2024
1 parent fea496c commit d9a15fa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# `bw2calc` Changelog

## 2.0.DEV18 (2024-07-24)

* [#101](https://github.com/brightway-lca/brightway2-calc/pull/101): Convert `technosphere_matrix` to `csr` when using `pypardiso` for speed boost on repeated calculations.
* [#104](https://github.com/brightway-lca/brightway2-calc/pull/104): Explicitly set array-size in MultiLCA's `lci_calculation()`. Solves bug with a single functional unit passed to `MultiLCA`.

## 2.0.DEV17 (2024-06-05)

### New `MultiLCA` implementation
Expand Down
5 changes: 5 additions & 0 deletions bw2calc/lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ def __next__(self) -> None:
if not skip_first_iteration and hasattr(self, "after_matrix_iteration"):
self.after_matrix_iteration()

# Avoid this conversion each time we do a calculation in the future
# See https://github.com/haasad/PyPardiso/issues/75#issuecomment-2186825609
if PYPARDISO:
self.technosphere_matrix = self.technosphere_matrix.tocsr()

if skip_first_iteration:
delattr(self, "keep_first_iteration_flag")

Expand Down
25 changes: 7 additions & 18 deletions bw2calc/lca_base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import warnings
from collections.abc import Iterator
from functools import partial
from scipy.sparse import csc_matrix, csr_matrix
from typing import Optional, Tuple

import matrix_utils as mu
Expand Down Expand Up @@ -46,15 +45,14 @@ def load_lci_data(self, nonsquare_ok=False) -> None:
use_distributions=use_distributions,
seed_override=self.seed_override,
)

# explicitly set technosphere format to optimal CSR/CSC format depending on solver
# see this link for discussion https://github.com/haasad/PyPardiso/issues/75#issuecomment-2186825609
if PYPARDISO:
self.technosphere_matrix = self.technosphere_mm.matrix.tocsr()
else:
self.technosphere_matrix = self.technosphere_mm.matrix.tocsc()
self.dicts.product = partial(self.technosphere_mm.row_mapper.to_dict)
self.dicts.activity = partial(self.technosphere_mm.col_mapper.to_dict)
self.technosphere_matrix = self.technosphere_mm.matrix

# Avoid this conversion each time we do a calculation in the future
# See https://github.com/haasad/PyPardiso/issues/75#issuecomment-2186825609
if PYPARDISO:
self.technosphere_matrix = self.technosphere_matrix.tocsr()

if (
len(self.technosphere_mm.row_mapper) != len(self.technosphere_mm.col_mapper)
Expand Down Expand Up @@ -126,8 +124,6 @@ def decompose_technosphere(self) -> None:
if PYPARDISO:
warnings.warn("PARDISO installed; this is a no-op")
else:
if isinstance(self.technosphere_matrix, csr_matrix):
self.technosphere_matrix.tocsc()
self.solver = factorized(self.technosphere_matrix)

def solve_linear_system(self, demand: Optional[np.ndarray] = None) -> None:
Expand All @@ -154,14 +150,7 @@ def solve_linear_system(self, demand: Optional[np.ndarray] = None) -> None:
if hasattr(self, "solver"):
return self.solver(demand)
else:
if ((PYPARDISO and isinstance(self.technosphere_matrix, csr_matrix)) or
(not PYPARDISO and isinstance(self.technosphere_matrix, csc_matrix))):
return spsolve(self.technosphere_matrix, demand)
elif PYPARDISO:
return spsolve(self.technosphere_matrix.tocsr(), demand)
else:
return spsolve(self.technosphere_matrix.tocsc(), demand)

return spsolve(self.technosphere_matrix, demand)

def lci(self, demand: Optional[dict] = None, factorize: bool = False) -> None:
"""
Expand Down
14 changes: 11 additions & 3 deletions bw2calc/multi_lca.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
import warnings
from pathlib import Path
from scipy.sparse import csc_matrix, csr_matrix
from typing import Iterable, Optional, Union

import bw_processing as bwp
Expand Down Expand Up @@ -184,6 +183,11 @@ def __next__(self) -> None:
if not skip_first_iteration and hasattr(self, "after_matrix_iteration"):
self.after_matrix_iteration()

# Avoid this conversion each time we do a calculation in the future
# See https://github.com/haasad/PyPardiso/issues/75#issuecomment-2186825609
if PYPARDISO:
self.technosphere_matrix = self.technosphere_matrix.tocsr()

if skip_first_iteration:
delattr(self, "keep_first_iteration_flag")

Expand Down Expand Up @@ -350,8 +354,12 @@ def lci_calculation(self) -> None:
"""
count = len(self.dicts.activity)
demand_matrix = np.vstack([arr for arr in self.demand_arrays.values()]).T
solutions = spsolve(self.technosphere_matrix, demand_matrix).reshape(count, -1)
self.supply_arrays = {name: arr for name, arr in zip(self.demands, solutions.T)}
self.supply_arrays = {
name: arr
for name, arr in zip(
self.demands, spsolve(self.technosphere_matrix, demand_matrix).reshape(count, -1).T
)
}
# Turn 1-d array into diagonal matrix
self.inventories = mu.SparseMatrixDict(
[
Expand Down
2 changes: 1 addition & 1 deletion tests/multi_lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def test_single_demand(dps, config):
mlca = MultiLCA(demands=single_func_unit, method_config=config, data_objs=dps)
mlca.lci()
mlca.lcia()

assert mlca.scores[(("first", "category"), "γ")] == 8 + 3
assert mlca.scores[(("second", "category"), "γ")] == 3 * 10

Expand Down

0 comments on commit d9a15fa

Please sign in to comment.