Skip to content

Commit

Permalink
Allow running shares without other uncertainties.
Browse files Browse the repository at this point in the history
Also, allow for a MC seed for reproducibility.
  • Loading branch information
romainsacchi committed Aug 11, 2024
1 parent dde4b52 commit db2cff0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 2 additions & 0 deletions pathways/lca.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ def _calculate_year(args: tuple):
shares,
uncertain_parameters,
remove_uncertainty,
seed
) = args

print(f"------ Calculating LCA results for {year}...")
Expand Down Expand Up @@ -603,6 +604,7 @@ def _calculate_year(args: tuple):
bw_datapackage,
],
use_distributions=True if use_distributions > 0 else False,
seed_override=seed
)

with CustomFilter("(almost) singular matrix"):
Expand Down
5 changes: 5 additions & 0 deletions pathways/pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ def calculate(
use_distributions: int = 0,
subshares: bool = False,
remove_uncertainty: bool = False,
seed: int = 0,
multiprocessing: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -345,6 +346,8 @@ def calculate(
:type subshares: bool, default is False
:param remove_uncertainty: Boolean. If True, remove uncertainty from inventory exchanges.
:type remove_uncertainty: bool, default is False
:param seed: Integer. Seed for random number generator.
:type seed: int, default is 0
"""

self.scenarios = harmonize_units(self.scenarios, variables)
Expand Down Expand Up @@ -430,6 +433,7 @@ def calculate(
shares = generate_samples(
years=self.scenarios.coords["year"].values.tolist(),
iterations=use_distributions,
seed=seed,
)

# Iterate over each combination of model, scenario, and year
Expand Down Expand Up @@ -461,6 +465,7 @@ def calculate(
shares,
uncertain_parameters,
remove_uncertainty,
seed
)
for year in years
]
Expand Down
10 changes: 6 additions & 4 deletions pathways/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,11 @@ def log_mc_parameters_to_excel(

indices = tehnosphere_indices[region]

df_technosphere_indices = create_mapping_sheet(indices=indices)
if indices:
df_technosphere_indices = create_mapping_sheet(indices=indices)
df_technosphere_indices.to_excel(
writer, sheet_name="Indices mapping", index=False
)

df_sum_impacts.to_excel(writer, sheet_name="Total impacts", index=False)
df_uncertainty_values.to_excel(
Expand All @@ -339,9 +343,7 @@ def log_mc_parameters_to_excel(
df_technology_shares.to_excel(
writer, sheet_name="Technology shares", index=False
)
df_technosphere_indices.to_excel(
writer, sheet_name="Indices mapping", index=False
)


print(f"Monte Carlo parameters added to: {export_path.resolve()}")

Expand Down
11 changes: 7 additions & 4 deletions pathways/subshares.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def default_dict_factory():
return defaultdict(dict)


def load_and_normalize_shares(ranges: dict, iterations: int) -> dict:
def load_and_normalize_shares(ranges: dict, iterations: int, seed: int) -> dict:
"""
Load and normalize shares for parameters to sum to 1 while respecting their specified ranges.
:param ranges: A dictionary with categories, technologies and market shares data.
Expand All @@ -282,7 +282,10 @@ def load_and_normalize_shares(ranges: dict, iterations: int) -> dict:
for technology, params in technologies.items():
for y, share in params["share"].items():
uncertainty_base = UncertaintyBase.from_dicts(share)
random_generator = MCRandomNumberGenerator(uncertainty_base)
random_generator = MCRandomNumberGenerator(
params=uncertainty_base,
seed=seed
)
shares[technology_group][y][technology] = np.squeeze(
np.array([random_generator.next() for _ in range(iterations)])
)
Expand Down Expand Up @@ -375,7 +378,7 @@ def interpolate_for_year(
shares[technology_group][target_year][technology] = f(target_year)


def generate_samples(years: list, iterations: int = 10) -> dict:
def generate_samples(years: list, iterations: int = 10, seed: int = None) -> dict:
"""
Generates and adjusts randomly selected shares for parameters to sum to 1
while respecting their specified ranges, and interpolates missing years.
Expand All @@ -385,6 +388,6 @@ def generate_samples(years: list, iterations: int = 10) -> dict:
:return: A dict with adjusted and interpolated shares for each technology and year.
"""
ranges = load_subshares()
shares = load_and_normalize_shares(ranges, iterations)
shares = load_and_normalize_shares(ranges, iterations, seed)
interpolate_shares(shares, years)
return shares

0 comments on commit db2cff0

Please sign in to comment.