diff --git a/pathways/lca.py b/pathways/lca.py index 51c5326..38b1cc4 100644 --- a/pathways/lca.py +++ b/pathways/lca.py @@ -3,12 +3,12 @@ from pathlib import Path from typing import Dict, List, Optional, 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 -import bw_processing as bwp -import numpy as np from .lcia import get_lcia_methods @@ -149,13 +149,13 @@ def get_lca_matrices( ) dp.add_persistent_vector( - matrix='technosphere_matrix', + matrix="technosphere_matrix", indices_array=a_indices, data_array=a_data, flip_array=a_sign, ) dp.add_persistent_vector( - matrix='biosphere_matrix', + matrix="biosphere_matrix", indices_array=b_indices, data_array=b_data, flip_array=b_sign, @@ -185,10 +185,7 @@ def get_lca_matrices( return dp, A_inds, B_inds -def fill_characterization_factors_matrix( - biosphere_flows: dict, - methods -) -> np.ndarray: +def fill_characterization_factors_matrix(biosphere_flows: dict, methods) -> np.ndarray: """ Create a characterization matrix based on the list of biosphere flows given. @@ -201,7 +198,6 @@ def fill_characterization_factors_matrix( print(lcia_data) - # create a numpy array filled with zeros # of size equal to biosphere_flows and lcia methods @@ -217,6 +213,7 @@ def fill_characterization_factors_matrix( return cf_matrix + def remove_double_counting(A: csr_matrix, vars_info: dict) -> csr_matrix: """ Remove double counting from a technosphere matrix. diff --git a/pathways/lcia.py b/pathways/lcia.py index 70a485d..712570b 100644 --- a/pathways/lcia.py +++ b/pathways/lcia.py @@ -45,6 +45,3 @@ def get_lcia_methods(methods: list = None): data = [x for x in data if " - ".join(x["name"]) in methods] return {" - ".join(x["name"]): format_lcia_method_exchanges(x) for x in data} - - - diff --git a/pathways/pathways.py b/pathways/pathways.py index 41c16d5..350c578 100644 --- a/pathways/pathways.py +++ b/pathways/pathways.py @@ -10,6 +10,7 @@ from pathlib import Path from typing import Any, Dict, List, Optional, Tuple, Union +import bw2calc as bc import numpy as np import pandas as pd import pyprind @@ -17,7 +18,6 @@ import yaml from datapackage import DataPackage from premise.geomap import Geomap -import bw2calc as bc from . import DATA_DIR from .data_validation import validate_datapackage @@ -309,76 +309,74 @@ def process_region(data: Tuple) -> Union[None, Dict[str, Any]]: lca.lcia() lca.score - - - # - # - # # Create the demand vector - # f = create_demand_vector([idx], A, demand, unit_vector) - # - # # Solve the inventory - # C = solve_inventory(A, B, f) - # - # if lcia_matrix is not None: - # if lcia_matrix.ndim != 2 or lcia_matrix.shape[0] != B.shape[1]: - # raise ValueError("Incompatible dimensions between B and lcia_matrix") - # - # # Solve the LCA problem to get the LCIA scores - # D = characterize_inventory(C, lcia_matrix) - # - # # Sum along the first axis of D to get final result - # D = D.sum(axis=1) - # - # # Initialize the new array with zeros for missing data - # E = np.zeros((len(A_index), len(locations), len(impact_categories))) - # - # # Populate the result array - # act_locs = [a[-1] for a in rev_A_index.values()] - # - # for i, act in enumerate(rev_A_index.values()): - # if act[-1] in act_locs: - # loc_idx = location_to_index[act[-1]] - # E[i, loc_idx, :] = D[i, :] - # - # acts_idx = generate_A_indices( - # A_index, - # reverse_classifications, - # lca_results_coords, - # ) - # - # # Sum over the first axis of D, - # # using acts_idx for advanced indexing - # target[:, v] = E[acts_idx, ...].sum(axis=0) - # - # else: - # # else, just sum the results of the inventory - # acts_idx = generate_A_indices( - # A_index, - # reverse_classifications, - # lca_results_coords, - # ) - # if flows is not None: - # - # def transf_flow(f): - # return tuple(f.split(" - ")) - # - # flows_idx = [int(B_index[transf_flow(f)]) for f in flows] - # C = C[:, flows_idx] - # - # # Initialize the new array with zeros for missing data - # E = np.zeros((len(A_index), len(locations), len(flows_idx))) - # - # # Populate the result array - # act_locs = [a[-1] for a in rev_A_index.values()] - # - # for i, act in enumerate(rev_A_index.values()): - # if act[-1] in act_locs: - # loc_idx = location_to_index[act[-1]] - # E[i, loc_idx, :] = C[i, :] - # - # target[:, v] = E[acts_idx, ...].sum(axis=0) - # else: - # target[:, v] = C[acts_idx, ...].sum(axis=0) + # + # + # # Create the demand vector + # f = create_demand_vector([idx], A, demand, unit_vector) + # + # # Solve the inventory + # C = solve_inventory(A, B, f) + # + # if lcia_matrix is not None: + # if lcia_matrix.ndim != 2 or lcia_matrix.shape[0] != B.shape[1]: + # raise ValueError("Incompatible dimensions between B and lcia_matrix") + # + # # Solve the LCA problem to get the LCIA scores + # D = characterize_inventory(C, lcia_matrix) + # + # # Sum along the first axis of D to get final result + # D = D.sum(axis=1) + # + # # Initialize the new array with zeros for missing data + # E = np.zeros((len(A_index), len(locations), len(impact_categories))) + # + # # Populate the result array + # act_locs = [a[-1] for a in rev_A_index.values()] + # + # for i, act in enumerate(rev_A_index.values()): + # if act[-1] in act_locs: + # loc_idx = location_to_index[act[-1]] + # E[i, loc_idx, :] = D[i, :] + # + # acts_idx = generate_A_indices( + # A_index, + # reverse_classifications, + # lca_results_coords, + # ) + # + # # Sum over the first axis of D, + # # using acts_idx for advanced indexing + # target[:, v] = E[acts_idx, ...].sum(axis=0) + # + # else: + # # else, just sum the results of the inventory + # acts_idx = generate_A_indices( + # A_index, + # reverse_classifications, + # lca_results_coords, + # ) + # if flows is not None: + # + # def transf_flow(f): + # return tuple(f.split(" - ")) + # + # flows_idx = [int(B_index[transf_flow(f)]) for f in flows] + # C = C[:, flows_idx] + # + # # Initialize the new array with zeros for missing data + # E = np.zeros((len(A_index), len(locations), len(flows_idx))) + # + # # Populate the result array + # act_locs = [a[-1] for a in rev_A_index.values()] + # + # for i, act in enumerate(rev_A_index.values()): + # if act[-1] in act_locs: + # loc_idx = location_to_index[act[-1]] + # E[i, loc_idx, :] = C[i, :] + # + # target[:, v] = E[acts_idx, ...].sum(axis=0) + # else: + # target[:, v] = C[acts_idx, ...].sum(axis=0) # Return a dictionary containing the processed LCA data for the given region def get_indices(): diff --git a/setup.py b/setup.py index 12afebd..3ef524e 100644 --- a/setup.py +++ b/setup.py @@ -49,7 +49,7 @@ def package_files(directory): "scipy", "premise", "pyyaml", - "scikit-umfpack" + "scikit-umfpack", ], url="https://github.com/polca/premise", description="Scenario-level LCA of energy systems and transition pathways",