From 7049e99e94f04b087752c6fcc8f2ae131e6c7eb0 Mon Sep 17 00:00:00 2001 From: romainsacchi Date: Fri, 21 Jun 2024 16:07:36 +0000 Subject: [PATCH] Black reformating --- pathways/lcia.py | 4 ++-- pathways/pathways.py | 18 ++++++++--------- pathways/sankey.py | 48 +++++++++++++++++++++++++++----------------- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/pathways/lcia.py b/pathways/lcia.py index bf8a130..be5bbba 100644 --- a/pathways/lcia.py +++ b/pathways/lcia.py @@ -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 @@ -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: @@ -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: diff --git a/pathways/pathways.py b/pathways/pathways.py index c5932f1..83dd674 100644 --- a/pathways/pathways.py +++ b/pathways/pathways.py @@ -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, @@ -33,7 +34,6 @@ load_units_conversion, resize_scenario_data, ) -from .sankey import Sankey def _get_mapping(data) -> dict: @@ -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. diff --git a/pathways/sankey.py b/pathways/sankey.py index 5cd0844..812a9e2 100644 --- a/pathways/sankey.py +++ b/pathways/sankey.py @@ -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. """ @@ -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, )