Skip to content

Commit

Permalink
EODC
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi authored and romainsacchi committed Mar 21, 2024
1 parent 92bfc36 commit 7e09cfe
Show file tree
Hide file tree
Showing 9 changed files with 502 additions and 557 deletions.
582 changes: 269 additions & 313 deletions dev/generate datapackages.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dev/timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
regions=[
"WEU",
"USA",
#"USA",
],
scenarios=[scenario],
years=[
Expand Down
5 changes: 1 addition & 4 deletions pathways/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from pathlib import Path

__version__ = (0, 0, 1)
__all__ = ("__version__", "DATA_DIR", "Pathways")
__all__ = ("__version__", "Pathways")

DATA_DIR = Path(__file__).resolve().parent / "data"

from .pathways import Pathways
4 changes: 2 additions & 2 deletions pathways/data_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def validate_datapackage(datapackage: datapackage.DataPackage):
dataframe = pd.DataFrame(data, columns=headers)

# Check that the scenario data is valid
validate_scenario_data(dataframe)
#validate_scenario_data(dataframe)

# Check that the mapping is valid
validate_mapping(datapackage.get_resource("mapping"), dataframe)
#validate_mapping(datapackage.get_resource("mapping"), dataframe)

# Check that the LCA data is valid
# validate_lca_data(datapackage)
Expand Down
19 changes: 19 additions & 0 deletions pathways/filesystem_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
This module contains constants for the filesystem paths used by Pathways.
"""

from pathlib import Path
import platformdirs

# Directories for data which comes with Pathways
DATA_DIR = Path(__file__).resolve().parent / "data"

# Directories for user-created data
USER_DATA_BASE_DIR = platformdirs.user_data_path(appname="pathways", appauthor="pylca")
USER_DATA_BASE_DIR.mkdir(parents=True, exist_ok=True)

DIR_CACHED_DB = USER_DATA_BASE_DIR / "cache"
DIR_CACHED_DB.mkdir(parents=True, exist_ok=True)

USER_LOGS_DIR = platformdirs.user_log_path(appname="pathways", appauthor="pylca")
USER_LOGS_DIR.mkdir(parents=True, exist_ok=True)
89 changes: 1 addition & 88 deletions pathways/lca.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import csv
import warnings
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from typing import Dict, Tuple

import bw_processing as bwp
import numpy as np
import scipy.sparse
import xarray as xr
from scipy import sparse
from scipy.sparse import csr_matrix

Expand All @@ -24,44 +21,6 @@
print("Solver: scikits.umfpack")


def create_demand_vector(
activities_idx: List[int],
A: scipy.sparse,
demand: xr.DataArray,
unit_conversion: np.ndarray,
) -> np.ndarray:
"""
Create a demand vector with the given activities indices, sparse matrix A, demand values, and unit conversion factors.
This function multiplies the given demand with the unit conversion factors and assigns the result to the positions in
the vector corresponding to the activities indices. All other positions in the vector are set to zero.
:param activities_idx: Indices of activities for which to create demand. These indices correspond to positions in the vector and matrix A.
:type activities_idx: List[int]
:param A: Sparse matrix used to determine the size of the demand vector.
:type A: scipy.sparse.csr_matrix
:param demand: Demand values for the activities, provided as a DataArray from the xarray package.
:type demand: xr.DataArray
:param unit_conversion: Unit conversion factors corresponding to each activity in activities_idx.
:type unit_conversion: numpy.ndarray
:return: The demand vector, represented as a 1-dimensional numpy array.
:rtype: numpy.ndarray
"""

# Initialize the demand vector with zeros, with length equal to the number of rows in A
f = np.zeros(A.shape[0])

# Assign demand values to the positions in the vector corresponding to the activities indices
# Demand values are converted to the appropriate units before assignment
f[activities_idx] = float(demand) * float(unit_conversion)

return f


def read_indices_csv(file_path: Path) -> Dict[Tuple[str, str, str, str], str]:
"""
Reads a CSV file and returns its contents as a dictionary.
Expand Down Expand Up @@ -229,49 +188,3 @@ def remove_double_counting(A: csr_matrix, vars_info: dict) -> csr_matrix:

A_coo.eliminate_zeros()
return A_coo.tocsr()


def characterize_inventory(C, lcia_matrix) -> np.ndarray:
"""
Characterize an inventory with an LCIA matrix.
:param C: Solved inventory
:param lcia_matrix: Characterization matrix
:return: Characterized inventory
"""

# Multiply C with lcia_matrix to get D
return C[..., None] * lcia_matrix


def solve_inventory(A: csr_matrix, B: np.ndarray, f: np.ndarray) -> np.ndarray:
"""
Solve the inventory problem for a set of activities, given technosphere and biosphere matrices, demand vector,
LCIA matrix, and the indices of activities to consider.
This function uses either the pypardiso or scipy library to solve the linear system, depending on the availability
of the pypardiso library. The solutions are then used to calculate LCIA scores.
...
:rtype: numpy.ndarray
"""

if A.shape[0] != A.shape[1]:
raise ValueError("A must be a square matrix")

if A.shape[0] != f.size:
raise ValueError("Incompatible dimensions between A and f")

if B.shape[0] != A.shape[0]:
raise ValueError("Incompatible dimensions between A and B")

with warnings.catch_warnings():
warnings.simplefilter("ignore")
# Solve the system Ax = f for x using sparse solver
A_inv = spsolve(A, f)[:, np.newaxis]

# Compute product of A_inv and B
C = A_inv * B

return C
6 changes: 1 addition & 5 deletions pathways/lcia.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import json

import numpy as np
import xarray as xr
from scipy import sparse

from . import DATA_DIR
from .filesystem_constants import DATA_DIR

LCIA_METHODS = DATA_DIR / "lcia_data.json"

Expand Down
Loading

0 comments on commit 7e09cfe

Please sign in to comment.