Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/sankey' into sankey
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi authored and romainsacchi committed Jun 25, 2024
2 parents 274526f + 7049e99 commit 348c1fe
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
4 changes: 2 additions & 2 deletions pathways/lcia.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import json
import logging

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

from .filesystem_constants import DATA_DIR

Expand Down Expand Up @@ -61,7 +61,6 @@ def build_characterization_matrix_for_sankey(method: str, biosphere_dict: dict):
lcia_data, unit = get_lcia_methods(methods=[method])
method_data = lcia_data[method]


indices, data = [], []

for flow in biosphere_dict:
Expand All @@ -71,6 +70,7 @@ def build_characterization_matrix_for_sankey(method: str, biosphere_dict: dict):

return np.array(data), np.array(indices, dtype=bwp.INDICES_DTYPE), unit[method]


def fill_characterization_factors_matrices(
methods: list, biosphere_matrix_dict: dict, biosphere_dict: dict, debug=False
) -> csr_matrix:
Expand Down
18 changes: 9 additions & 9 deletions pathways/pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .filesystem_constants import DATA_DIR, DIR_CACHED_DB, STATS_DIR, USER_LOGS_DIR
from .lca import _calculate_year, get_lca_matrices
from .lcia import get_lcia_method_names
from .sankey import Sankey
from .subshares import generate_samples
from .utils import (
clean_cache_directory,
Expand All @@ -33,7 +34,6 @@
load_units_conversion,
resize_scenario_data,
)
from .sankey import Sankey


def _get_mapping(data) -> dict:
Expand Down Expand Up @@ -240,14 +240,14 @@ def _get_scenarios(self, scenario_data: pd.DataFrame) -> xr.DataArray:
return data

def sankey(
self,
method: str,
model: str,
scenario: str,
region: str,
year: int,
variable: str,
cutoff: float = 1e-3,
self,
method: str,
model: str,
scenario: str,
region: str,
year: int,
variable: str,
cutoff: float = 1e-3,
):
"""
Display a Sankey diagram of the scenario data.
Expand Down
48 changes: 30 additions & 18 deletions pathways/sankey.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import bw_processing as bwp
import bw2calc as bc
import bw2data
import bw_processing as bwp
import xarray as xr

from .lcia import build_characterization_matrix_for_sankey
from .utils import get_unit_conversion_factors, load_units_conversion


class Sankey:
"""
A class to represent a Sankey diagram.
"""

def __init__(self,
method: str,
model: str,
scenario: str,
region: str,
year: int,
variable: str,
dp: bwp,
biosphere_dict: dict,
activity_dict: dict,
mapping: dict,
scenario_data: xr.DataArray,
cutoff: float = 1e-3,
):
def __init__(
self,
method: str,
model: str,
scenario: str,
region: str,
year: int,
variable: str,
dp: bwp,
biosphere_dict: dict,
activity_dict: dict,
mapping: dict,
scenario_data: xr.DataArray,
cutoff: float = 1e-3,
):
"""
Initialize the Sankey object.
"""
Expand Down Expand Up @@ -53,14 +55,24 @@ def __init__(self,
dataset_index = self.activity_dict[dataset_name]
self.dataset_index = dataset_index
# fetch demand
demand = self.scenario_data.sel(model=self.model, pathway=self.scenario, region=self.region, year=self.year, variables=self.variable).values
demand = self.scenario_data.sel(
model=self.model,
pathway=self.scenario,
region=self.region,
year=self.year,
variables=self.variable,
).values
demand_unit = self.scenario_data.attrs["units"][self.variable]
demand = float(demand) * float(get_unit_conversion_factors(demand_unit, dataset_name[2], load_units_conversion()))
demand = float(demand) * float(
get_unit_conversion_factors(
demand_unit, dataset_name[2], load_units_conversion()
)
)

self.lcia_unit = c_unit

self.dp.add_persistent_vector(
matrix='characterization_matrix',
matrix="characterization_matrix",
indices_array=c_indices,
data_array=c_data,
)
Expand Down

0 comments on commit 348c1fe

Please sign in to comment.