Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/decomposition' into decomposition
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Jan 31, 2025
2 parents bdaf9fd + 446dc9a commit 2f52a62
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 91 deletions.
62 changes: 29 additions & 33 deletions premise/electricity.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
get_efficiency_solar_photovoltaics,
get_water_consumption_factors,
rescale_exchanges,
rescale_exchange, )
rescale_exchange,
)
from .uncertainty import flag_change
from .validation import ElectricityValidation

Expand Down Expand Up @@ -731,7 +732,7 @@ def generate_regional_markets(region: str, period: int, subset: list) -> dict:
flag_change(
exchange=new_exchange,
factor=new_exchange["amount"],
sector="electricity - market"
sector="electricity - market",
)

if "log parameters" not in new_dataset:
Expand Down Expand Up @@ -957,7 +958,7 @@ def generate_regional_markets(region: str, period: int) -> dict:
flag_change(
exchange=new_exchange,
factor=new_exchange["amount"],
sector="electricity - market"
sector="electricity - market",
)

if "log parameters" not in new_dataset:
Expand Down Expand Up @@ -1224,14 +1225,13 @@ def generate_regional_markets(region: str, period: int, subset: list) -> dict:
}
)


new_dataset["exchanges"] = new_exchanges

for new_exchange in new_exchanges:
flag_change(
exchange=new_exchange,
factor=new_exchange["amount"],
sector="electricity - market"
sector="electricity - market",
)

if "log parameters" not in new_dataset:
Expand All @@ -1257,8 +1257,7 @@ def generate_regional_markets(region: str, period: int, subset: list) -> dict:
# Using a list comprehension to process all technologies
subset = filter_technology(
dataset_names=[
item for subset in ecoinvent_technologies.values()
for item in subset
item for subset in ecoinvent_technologies.values() for item in subset
],
database=self.database,
)
Expand Down Expand Up @@ -1413,7 +1412,7 @@ def generate_world_market(
flag_change(
exchange=exchange,
factor=exchange["amount"],
sector="electricity - market"
sector="electricity - market",
)
dataset["exchanges"].append(exchange)

Expand Down Expand Up @@ -1450,12 +1449,11 @@ def correct_hydropower_water_emissions(self) -> None:
):
rescale_exchange(
exc=exc,
value=(flow["amount"]/exc["amount"]),
value=(flow["amount"] / exc["amount"]),
remove_uncertainty=False,
sector="electricity - hydro - correction"
sector="electricity - hydro - correction",
)


def update_efficiency_of_solar_pv(self) -> None:
"""
Update the efficiency of solar PV modules.
Expand Down Expand Up @@ -1575,8 +1573,12 @@ def update_efficiency_of_solar_pv(self) -> None:
sector="electricity - efficiency",
remove_uncertainty=False,
uncertainty_type=5,
minimum=exc["amount"] * scaling_factor * (new_min_eff / new_mean_eff),
maximum=exc["amount"] * scaling_factor * (new_max_eff / new_mean_eff)
minimum=exc["amount"]
* scaling_factor
* (new_min_eff / new_mean_eff),
maximum=exc["amount"]
* scaling_factor
* (new_max_eff / new_mean_eff),
)

dataset["comment"] = (
Expand Down Expand Up @@ -1609,7 +1611,7 @@ def update_efficiency_of_solar_pv(self) -> None:
exc=exc,
value=scaling_factor,
remove_uncertainty=False,
sector="electricity - efficiency"
sector="electricity - efficiency",
)

def create_region_specific_power_plants(self):
Expand Down Expand Up @@ -1732,7 +1734,7 @@ def create_region_specific_power_plants(self):
exc=exc,
value=(co2_amount * 0.9 / exc["amount"]),
remove_uncertainty=False,
sector="electricity - CHP - correction"
sector="electricity - CHP - correction",
)

if (
Expand Down Expand Up @@ -1872,7 +1874,7 @@ def update_electricity_efficiency(self) -> None:
rescale_exchanges(
ds=dataset,
value=scaling_factor,
sector="electricity - efficiency"
sector="electricity - efficiency",
)

self.write_log(dataset=dataset, status="updated")
Expand Down Expand Up @@ -2018,7 +2020,7 @@ def adjust_coal_power_plant_emissions(self) -> None:
exc=exc,
value=scaling_factor,
remove_uncertainty=False,
sector="electricity - air emissions"
sector="electricity - air emissions",
)

if "log parameters" not in dataset:
Expand Down Expand Up @@ -2085,7 +2087,7 @@ def create_missing_power_plant_datasets(self) -> None:
rescale_exchanges(
ds=new_dataset,
value=ei_eff / new_eff,
sector="electricity - efficiency"
sector="electricity - efficiency",
)

self.database.append(new_dataset)
Expand Down Expand Up @@ -2178,21 +2180,15 @@ def adjust_aluminium_electricity_markets(self) -> None:

# add the new electricity market
exchange = {
"name": f"market group for electricity, high voltage",
"product": f"electricity, high voltage",
"amount": 1,
"uncertainty type": 0,
"location": self.geo.ecoinvent_to_iam_location(
dataset["location"]
),
"type": "technosphere",
"unit": "kilowatt hour",
}
flag_change(
exchange=exchange,
factor=1,
sector="electricity - market"
)
"name": f"market group for electricity, high voltage",
"product": f"electricity, high voltage",
"amount": 1,
"uncertainty type": 0,
"location": self.geo.ecoinvent_to_iam_location(dataset["location"]),
"type": "technosphere",
"unit": "kilowatt hour",
}
flag_change(exchange=exchange, factor=1, sector="electricity - market")
dataset["exchanges"].append(exchange)

self.write_log(dataset=dataset, status="updated")
Expand Down
67 changes: 51 additions & 16 deletions premise/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ def find_technosphere_keys(db, df):

return db, df


def generate_decomposition_db(
origin_db,
scenarios,
Expand All @@ -867,23 +868,57 @@ def generate_decomposition_db(
)

unwanted_keys = (
'input', 'Comment', 'scale', 'minimum', 'simapro category',
'production volume', 'comment', 'reference product', 'ub',
'formula', 'categories', 'u2', 'loc', 'product', 'allocation',
'normalization', 'u4', 'system model', 'database', 'unit',
'maximum', 'name', 'tag', 'type', 'original_amount',
'simapro name', 'dataset name', 'shape', 'u1', 'negative',
'u6', 'u5', 'u3', 'amount', 'input type', 'uncertainty type',
'pedigree', 'location'
"input",
"Comment",
"scale",
"minimum",
"simapro category",
"production volume",
"comment",
"reference product",
"ub",
"formula",
"categories",
"u2",
"loc",
"product",
"allocation",
"normalization",
"u4",
"system model",
"database",
"unit",
"maximum",
"name",
"tag",
"type",
"original_amount",
"simapro name",
"dataset name",
"shape",
"u1",
"negative",
"u6",
"u5",
"u3",
"amount",
"input type",
"uncertainty type",
"pedigree",
"location",
)

cols = list(set([
k
for a in scenarios[0]["database"]
for e in a["exchanges"]
for k in e.keys()
if k not in unwanted_keys
]))
cols = list(
set(
[
k
for a in scenarios[0]["database"]
for e in a["exchanges"]
for k in e.keys()
if k not in unwanted_keys
]
)
)

return db, df, cols

Expand All @@ -906,7 +941,6 @@ def generate_decomposition_db(
cols,
] = [exc.get(k) for k in cols] * df["original"]


if filepath is not None:
filepath = Path(filepath)
else:
Expand Down Expand Up @@ -942,6 +976,7 @@ def generate_decomposition_db(

return


def generate_superstructure_db(
origin_db,
scenarios,
Expand Down
4 changes: 1 addition & 3 deletions premise/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,7 @@ def adjust_efficiency_of_new_markets(

if "includes" not in ineff:
rescale_exchanges(
ds=datatset,
value=scaling_factor,
sector="external - efficiency"
ds=datatset, value=scaling_factor, sector="external - efficiency"
)

else:
Expand Down
20 changes: 10 additions & 10 deletions premise/new_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
generate_scenario_factor_file,
generate_superstructure_db,
prepare_db_for_export,
generate_decomposition_db
generate_decomposition_db,
)
from .external import _update_external_scenarios
from .external_data_validation import check_external_scenarios
Expand Down Expand Up @@ -989,14 +989,15 @@ def update(self, sectors: [str, list, None] = None) -> None:
print("Done!\n")

def write_decomposition_db_to_brightway(
self,
name: str = f"decomposition_db_{datetime.now().strftime('%d-%m-%Y')}",
filepath: str = None,
file_format: str = "csv",
self,
name: str = f"decomposition_db_{datetime.now().strftime('%d-%m-%Y')}",
filepath: str = None,
file_format: str = "csv",
):
if len(self.scenarios) < 1:
raise ValueError("Decomposition analysis is only possible for"
"one scenario at a time.")
raise ValueError(
"Decomposition analysis is only possible for" "one scenario at a time."
)

self.scenarios[0] = load_database(self.scenarios[0])

Expand All @@ -1023,11 +1024,10 @@ def write_decomposition_db_to_brightway(
# biosphere_name=self.biosphere_name,
# )

#write_brightway_database(
# write_brightway_database(
# data=self.database,
# name=name,
#)

# )

def write_superstructure_db_to_brightway(
self,
Expand Down
37 changes: 19 additions & 18 deletions premise/uncertainty.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@


def rescale_exchange(
exc: dict,
value: float,
sector: str,
remove_uncertainty: bool =True,
uncertainty_type: int = None,
loc: float = None,
scale: float = None,
minimum: float = None,
maximum: float = None
exc: dict,
value: float,
sector: str,
remove_uncertainty: bool = True,
uncertainty_type: int = None,
loc: float = None,
scale: float = None,
minimum: float = None,
maximum: float = None,
):
"""Function to rescale exchange amount and uncertainty.
Expand All @@ -30,12 +30,7 @@ def rescale_exchange(
# Scale the amount
exc["amount"] *= value

flag_change(
exchange=exc,
factor=float(value),
sector=sector

)
flag_change(exchange=exc, factor=float(value), sector=sector)

# Scale the uncertainty fields if uncertainty is not being removed
if not remove_uncertainty:
Expand Down Expand Up @@ -74,7 +69,11 @@ def rescale_exchange(

# If remove_uncertainty is True, then remove all uncertainty info
elif remove_uncertainty:
FIELDS = ("scale", "minimum", "maximum", )
FIELDS = (
"scale",
"minimum",
"maximum",
)
exc["uncertainty type"] = 0
exc["loc"] = exc["amount"]
for field in FIELDS:
Expand All @@ -84,13 +83,15 @@ def rescale_exchange(
return exc


def flag_change(exchange: dict, sector: str, value: float = None, factor: float = None) -> dict:
def flag_change(
exchange: dict, sector: str, value: float = None, factor: float = None
) -> dict:
"""
Flag an exchange with the factor applied on the amount.
"""

if value:
exchange[sector] = (value / exchange["amount"])
exchange[sector] = value / exchange["amount"]
return exchange

if factor:
Expand Down
Loading

0 comments on commit 2f52a62

Please sign in to comment.