Skip to content

Commit

Permalink
Add quantiles quantification
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Apr 2, 2024
1 parent a57156a commit 28260b1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
42 changes: 28 additions & 14 deletions pathways/pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,6 @@ def _calculate_year(args):
data_objs=[
bw_datapackage,
],
use_distributions=True if use_distributions > 0 else False,
)
lca.lci(factorize=True)
else:
Expand Down Expand Up @@ -819,15 +818,14 @@ def calculate(

self.lca_results = create_lca_results_array(
methods=methods,
B_indices=biosphere_index,
years=years,
regions=regions,
locations=locations,
models=models,
scenarios=scenarios,
classifications=self.classifications,
mapping=self.mapping,
flows=flows,
use_distributions=True if use_distributions > 0 else False,
)

# Iterate over each combination of model, scenario, and year
Expand Down Expand Up @@ -912,6 +910,7 @@ def fill_in_result_array(self, results: dict):
for region, data in result.items()
if region != "other"
}

# use pyprint to display progress
bar = pyprind.ProgBar(len(results))
for coord, result in results.items():
Expand All @@ -936,17 +935,32 @@ def fill_in_result_array(self, results: dict):
summed_data = d[..., idx].sum(axis=-1)

if idx.size > 0:
self.lca_results.loc[
{
"region": region,
"model": model,
"scenario": scenario,
"year": year,
"act_category": cat,
"location": loc,
"variable": list(variables.keys()),
}
] = summed_data
try:
self.lca_results.loc[
{
"region": region,
"model": model,
"scenario": scenario,
"year": year,
"act_category": cat,
"location": loc,
"variable": list(variables.keys()),
}
] = summed_data

except:
print("summed data shape", summed_data.shape)
print(self.lca_results.loc[
{
"region": region,
"model": model,
"scenario": scenario,
"year": year,
"act_category": cat,
"location": loc,
"variable": list(variables.keys()),
}
].shape)

def characterize_planetary_boundaries(
self,
Expand Down
42 changes: 24 additions & 18 deletions pathways/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,33 +160,38 @@ def load_units_conversion():

def create_lca_results_array(
methods: [List[str], None],
B_indices: Dict,
years: List[int],
regions: List[str],
locations: List[str],
models: List[str],
scenarios: List[str],
classifications: dict,
mapping: dict,
flows: List[str] = None,
use_distributions: bool = False,
) -> xr.DataArray:
"""
Create an xarray DataArray to store Life Cycle Assessment (LCA) results.
The DataArray has dimensions `act_category`, `impact_category`, `year`, `region`, `model`, and `scenario`.
:param methods: List of impact assessment methods.
:param methods: A list of impact categories.
:type methods: List[str]
:param years: List of years to consider in the LCA results.
:param years: A list of years.
:type years: List[int]
:param regions: List of regions to consider in the LCA results.
:param regions: A list of regions.
:type regions: List[str]
:param locations: List of locations to consider in the LCA results.
:param locations: A list of locations.
:type locations: List[str]
:param models: List of models to consider in the LCA results.
:param models: A list of models.
:type models: List[str]
:param scenarios: List of scenarios to consider in the LCA results.
:param scenarios: A list of scenarios.
:type scenarios: List[str]
:param classifications: A dictionary mapping activities to categories.
:type classifications: dict
:param mapping: A dictionary mapping scenario variables to LCA datasets.
:type mapping: dict
:param use_distributions: A boolean indicating whether to use distributions.
:type use_distributions: bool
:return: An xarray DataArray with the appropriate coordinates and dimensions to store LCA results.
:rtype: xr.DataArray
Expand All @@ -201,8 +206,16 @@ def create_lca_results_array(
"location": locations,
"model": models,
"scenario": scenarios,
"impact_category": methods,
}

if use_distributions is True:
coords.update(
{
"quantile": [0.05, 0.5, 0.95]
}
)

dims = (
len(coords["act_category"]),
len(coords["variable"]),
Expand All @@ -211,18 +224,11 @@ def create_lca_results_array(
len(locations),
len(models),
len(scenarios),
len(methods),
)

if methods is not None:
coords["impact_category"] = methods
dims += (len(methods),)
else:
if flows is not None:
coords["impact_category"] = flows
dims += (len(flows),)
else:
coords["impact_category"] = [" - ".join(a) for a in list(B_indices.keys())]
dims += (len(B_indices),)
if use_distributions is True:
dims += (3,)

# Create the xarray DataArray with the defined coordinates and dimensions.
# The array is initialized with zeros.
Expand Down

0 comments on commit 28260b1

Please sign in to comment.