Skip to content

Commit

Permalink
Implement caching of results from relink_technosphere_exchanges(). + …
Browse files Browse the repository at this point in the history
…version bump.
  • Loading branch information
romainsacchi committed Aug 5, 2023
1 parent 867f42c commit 4ebff08
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 33 deletions.
Binary file modified dev/profile.prof
Binary file not shown.
11 changes: 5 additions & 6 deletions dev/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
bw2data.projects.set_current("ei39")

scenarios = [
{"model": "remind", "pathway": "SSP1-Base", "year": 2005},
{"model": "remind", "pathway": "SSP2-Base", "year": 2010},
{"model": "remind", "pathway": "SSP5-Base", "year": 2015},
#{"model": "remind", "pathway": "SSP1-Base", "year": 2005},
#{"model": "remind", "pathway": "SSP2-Base", "year": 2010},
{"model": "remind", "pathway": "SSP5-Base", "year": 2050},
]

ndb = NewDatabase(
scenarios=scenarios,
source_db="ecoinvent cutoff 3.9.1",
source_version="3.9",
source_db="ecoinvent 3.9.1 cutoff",
key=b"tUePmX_S5B8ieZkkM7WUU2CnO8SmShwmAeWK9x2rTFo=",
)

ndb.update_all()
ndb.write_datapackage(name="my_dp")
ndb.write_db_to_brightway()
2 changes: 1 addition & 1 deletion premise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__all__ = ("NewDatabase", "clear_cache", "get_regions_definition")
__version__ = (1, 5, 9)
__version__ = (1, 6, 0)

from pathlib import Path

Expand Down
8 changes: 6 additions & 2 deletions premise/cement.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
logger = create_logger("cement")


def _update_cement(scenario, version, system_model, modified_datasets):
def _update_cement(scenario, version, system_model, modified_datasets, cache):
cement = Cement(
database=scenario["database"],
model=scenario["model"],
Expand All @@ -31,16 +31,18 @@ def _update_cement(scenario, version, system_model, modified_datasets):
version=version,
system_model=system_model,
modified_datasets=modified_datasets,
cache=cache
)

if scenario["iam data"].cement_markets is not None:
cement.add_datasets_to_database()
scenario["database"] = cement.database
modified_datasets = cement.modified_datasets
cache = cement.cache
else:
print("No cement markets found in IAM data. Skipping.")

return scenario, modified_datasets
return scenario, modified_datasets, cache


class Cement(BaseTransformation):
Expand Down Expand Up @@ -74,6 +76,7 @@ def __init__(
version: str,
system_model: str,
modified_datasets: dict,
cache: dict = None
):
super().__init__(
database,
Expand All @@ -84,6 +87,7 @@ def __init__(
version,
system_model,
modified_datasets,
cache
)
self.version = version

Expand Down
8 changes: 6 additions & 2 deletions premise/direct_air_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def fetch_mapping(filepath: str) -> dict:
return mapping


def _update_dac(scenario, version, system_model, modified_datasets):
def _update_dac(scenario, version, system_model, modified_datasets, cache=None):
dac = DirectAirCapture(
database=scenario["database"],
iam_data=scenario["iam data"],
Expand All @@ -47,16 +47,18 @@ def _update_dac(scenario, version, system_model, modified_datasets):
version=version,
system_model=system_model,
modified_datasets=modified_datasets,
cache=cache
)

if scenario["iam data"].dac_markets is not None:
dac.generate_dac_activities()
scenario["database"] = dac.database
modified_datasets = dac.modified_datasets
cache = dac.cache
else:
print("No DAC markets found in IAM data. Skipping.")

return scenario, modified_datasets
return scenario, modified_datasets, cache


class DirectAirCapture(BaseTransformation):
Expand All @@ -75,6 +77,7 @@ def __init__(
version: str,
system_model: str,
modified_datasets: dict,
cache: dict = None,
):
super().__init__(
database,
Expand All @@ -85,6 +88,7 @@ def __init__(
version,
system_model,
modified_datasets,
cache
)
self.database = database
self.iam_data = iam_data
Expand Down
52 changes: 39 additions & 13 deletions premise/ecoinvent_modification.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,23 +410,48 @@ def _update_all(
vehicle_type,
gains_scenario,
):
scenario, modified_datasets = _update_vehicles(
scenario, vehicle_type, version, system_model, modified_datasets
scenario, modified_datasets, cache = _update_vehicles(
scenario=scenario,
vehicle_type=vehicle_type,
version=version,
system_model=system_model,
modified_datasets=modified_datasets
)
scenario, modified_datasets = _update_electricity(
scenario, version, system_model, modified_datasets, use_absolute_efficiency
scenario, modified_datasets, cache = _update_electricity(
scenario=scenario,
version=version,
system_model=system_model,
modified_datasets=modified_datasets,
use_absolute_efficiency=use_absolute_efficiency,
cache=cache
)
scenario, modified_datasets = _update_dac(
scenario, version, system_model, modified_datasets
scenario, modified_datasets, cache = _update_dac(
scenario=scenario,
version=version,
system_model=system_model,
modified_datasets=modified_datasets,
cache=cache
)
scenario, modified_datasets = _update_cement(
scenario, version, system_model, modified_datasets
scenario, modified_datasets, cache = _update_cement(
scenario=scenario,
version=version,
system_model=system_model,
modified_datasets=modified_datasets,
cache=cache
)
scenario, modified_datasets = _update_steel(
scenario, version, system_model, modified_datasets
scenario, modified_datasets, cache = _update_steel(
scenario=scenario,
version=version,
system_model=system_model,
modified_datasets=modified_datasets,
cache=cache
)
scenario, modified_datasets = _update_fuels(
scenario, version, system_model, modified_datasets
scenario, modified_datasets, cache = _update_fuels(
scenario=scenario,
version=version,
system_model=system_model,
modified_datasets=modified_datasets,
cache=cache
)
scenario, modified_datasets = _update_emissions(
scenario, version, system_model, gains_scenario, modified_datasets
Expand Down Expand Up @@ -1112,7 +1137,8 @@ def update_all(self) -> None:
print("`update_all()` will skip the following steps:")
print("update_two_wheelers(), update_cars(), and update_buses()")
print(
"If you want to update these steps, please run them separately afterwards."
"If you want to update these steps, "
"please run them separately afterwards."
)

# use multiprocessing to speed up the process
Expand Down
8 changes: 6 additions & 2 deletions premise/electricity.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def get_production_weighted_losses(


def _update_electricity(
scenario, version, system_model, modified_datasets, use_absolute_efficiency
scenario, version, system_model, modified_datasets, use_absolute_efficiency, cache=None
):
electricity = Electricity(
database=scenario["database"],
Expand All @@ -175,6 +175,7 @@ def _update_electricity(
system_model=system_model,
modified_datasets=modified_datasets,
use_absolute_efficiency=use_absolute_efficiency,
cache=cache,
)

electricity.create_missing_power_plant_datasets()
Expand Down Expand Up @@ -203,8 +204,9 @@ def _update_electricity(

scenario["database"] = electricity.database
modified_datasets = electricity.modified_datasets
cache = electricity.cache

return scenario, modified_datasets
return scenario, modified_datasets, cache


class Electricity(BaseTransformation):
Expand Down Expand Up @@ -235,6 +237,7 @@ def __init__(
system_model: str,
modified_datasets: dict,
use_absolute_efficiency: bool = False,
cache: dict = None,
) -> None:
super().__init__(
database,
Expand All @@ -245,6 +248,7 @@ def __init__(
version,
system_model,
modified_datasets,
cache
)
mapping = InventorySet(self.database)
self.powerplant_map = mapping.generate_powerplant_map()
Expand Down
8 changes: 6 additions & 2 deletions premise/fuels.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def update_dataset(dataset, supplier_key, amount):
return dataset


def _update_fuels(scenario, version, system_model, modified_datasets):
def _update_fuels(scenario, version, system_model, modified_datasets, cache):
fuels = Fuels(
database=scenario["database"],
iam_data=scenario["iam data"],
Expand All @@ -284,6 +284,7 @@ def _update_fuels(scenario, version, system_model, modified_datasets):
version=version,
system_model=system_model,
modified_datasets=modified_datasets,
cache=cache
)

if any(
Expand All @@ -298,10 +299,11 @@ def _update_fuels(scenario, version, system_model, modified_datasets):
fuels.generate_fuel_markets()
scenario["database"] = fuels.database
modified_datasets = fuels.modified_datasets
cache = fuels.cache
else:
print("No fuel markets found in IAM data. Skipping.")

return scenario, modified_datasets
return scenario, modified_datasets, cache


class Fuels(BaseTransformation):
Expand All @@ -319,6 +321,7 @@ def __init__(
version: str,
system_model: str,
modified_datasets: dict,
cache: dict = None
):
super().__init__(
database,
Expand All @@ -329,6 +332,7 @@ def __init__(
version,
system_model,
modified_datasets,
cache
)
# ecoinvent version
self.version = version
Expand Down
8 changes: 6 additions & 2 deletions premise/steel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
logger = create_logger("steel")


def _update_steel(scenario, version, system_model, modified_datasets):
def _update_steel(scenario, version, system_model, modified_datasets, cache):
steel = Steel(
database=scenario["database"],
model=scenario["model"],
Expand All @@ -26,16 +26,18 @@ def _update_steel(scenario, version, system_model, modified_datasets):
version=version,
system_model=system_model,
modified_datasets=modified_datasets,
cache=cache,
)

if scenario["iam data"].steel_markets is not None:
steel.generate_activities()
scenario["database"] = steel.database
modified_datasets = steel.modified_datasets
cache = steel.cache
else:
print("No steel markets found in IAM data. Skipping.")

return scenario, modified_datasets
return scenario, modified_datasets, cache


class Steel(BaseTransformation):
Expand All @@ -59,6 +61,7 @@ def __init__(
version: str,
system_model: str,
modified_datasets: dict,
cache: dict = None
) -> None:
super().__init__(
database,
Expand All @@ -69,6 +72,7 @@ def __init__(
version,
system_model,
modified_datasets,
cache
)
self.version = version

Expand Down
12 changes: 10 additions & 2 deletions premise/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@
FILEPATH_VEHICLES_MAP = DATA_DIR / "transport" / "vehicles_map.yaml"


def _update_vehicles(scenario, vehicle_type, version, system_model, modified_datasets):
def _update_vehicles(
scenario,
vehicle_type,
version,
system_model,
modified_datasets,
cache=None,
):
trspt = Transport(
database=scenario["database"],
year=scenario["year"],
Expand Down Expand Up @@ -63,10 +70,11 @@ def _update_vehicles(scenario, vehicle_type, version, system_model, modified_dat
trspt.create_vehicle_markets()
scenario["database"] = trspt.database
modified_datasets = trspt.modified_datasets
cache = trspt.cache
else:
print(f"No markets found for {vehicle_type} in IAM data. Skipping.")

return scenario, modified_datasets
return scenario, modified_datasets, {} or cache


def get_average_truck_load_factors() -> Dict[str, Dict[str, Dict[str, float]]]:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def package_files(directory):

setup(
name="premise",
version="1.5.9",
version="1.6.0",
python_requires=">=3.9,<3.11",
packages=packages,
author="Romain Sacchi <[email protected]>, Alois Dirnaichner <[email protected]>, Chris Mutel "
Expand Down

0 comments on commit 4ebff08

Please sign in to comment.