Skip to content

Commit

Permalink
Align wrt to Materials
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Nov 7, 2023
1 parent 426a13a commit 28c16fc
Showing 1 changed file with 57 additions and 46 deletions.
103 changes: 57 additions & 46 deletions premise/ecoinvent_modification.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,8 @@ def update_electricity(self) -> None:
for s, scenario in enumerate(self.scenarios):
self.scenarios[s] = results[s][0]
else:
for scenario in self.scenarios:
scenario, _ = _update_electricity(
for s, scenario in enumerate(self.scenarios):
self.scenarios[s], _ = _update_electricity(
scenario=scenario,
version=self.version,
system_model=self.system_model,
Expand Down Expand Up @@ -909,8 +909,8 @@ def update_dac(self) -> None:
self.scenarios[s] = results[s][0]

else:
for scenario in self.scenarios:
scenario, _ = _update_dac(
for s, scenario in enumerate(self.scenarios):
self.scenarios[s], _ = _update_dac(
scenario=scenario,
version=self.version,
system_model=self.system_model,
Expand Down Expand Up @@ -942,8 +942,8 @@ def update_fuels(self) -> None:
self.scenarios[s] = results[s][0]

else:
for scenario in self.scenarios:
scenario, _ = _update_fuels(
for s, scenario in enumerate(self.scenarios):
self.scenarios[s], _ = _update_fuels(
scenario=scenario,
version=self.version,
system_model=self.system_model,
Expand Down Expand Up @@ -975,8 +975,8 @@ def update_cement(self) -> None:
self.scenarios[s] = results[s][0]

else:
for scenario in self.scenarios:
scenario, _ = _update_cement(
for s, scenario in enumerate(self.scenarios):
self.scenarios[s], _ = _update_cement(
scenario=scenario,
version=self.version,
system_model=self.system_model,
Expand Down Expand Up @@ -1008,8 +1008,8 @@ def update_steel(self) -> None:
self.scenarios[s] = results[s][0]

else:
for scenario in self.scenarios:
scenario, _ = _update_steel(
for s, scenario in enumerate(self.scenarios):
self.scenarios[s], _ = _update_steel(
scenario=scenario,
version=self.version,
system_model=self.system_model,
Expand Down Expand Up @@ -1042,27 +1042,12 @@ def update_metals(self) -> None:
self.scenarios[s] = results[s][0]

else:
for scenario in self.scenarios:
if (
"exclude" not in scenario
or "update_metals" not in scenario["exclude"]
):
metals = Metals(
database=scenario["database"],
year=scenario["year"],
model=scenario["model"],
pathway=scenario["pathway"],
iam_data=scenario["iam data"],
version=self.version,
system_model=self.system_model,
)

_update_metals(
scenario,
self.version,
self.system_model,
)
scenario["database"] = metals.database
for s, scenario in enumerate(self.scenarios):
self.scenarios[s], _ = _update_metals(
scenario,
self.version,
self.system_model,
)

print("Done!\n")

Expand Down Expand Up @@ -1091,8 +1076,8 @@ def update_cars(self) -> None:
self.scenarios[s] = results[s][0]

else:
for scenario in self.scenarios:
scenario, _ = _update_vehicles(
for s, scenario in enumerate(self.scenarios):
self.scenarios[s], _ = _update_vehicles(
scenario=scenario,
vehicle_type="car",
version=self.version,
Expand Down Expand Up @@ -1126,8 +1111,8 @@ def update_two_wheelers(self) -> None:
self.scenarios[s] = results[s][0]

else:
for scenario in self.scenarios:
scenario, _ = _update_vehicles(
for s, scenario in enumerate(self.scenarios):
self.scenarios[s], _ = _update_vehicles(
scenario=scenario,
vehicle_type="two wheeler",
version=self.version,
Expand Down Expand Up @@ -1163,8 +1148,8 @@ def update_trucks(self) -> None:
self.scenarios[s] = results[s][0]

else:
for scenario in self.scenarios:
scenario, _ = _update_vehicles(
for s, scenario in enumerate(self.scenarios):
self.scenarios[s], _ = _update_vehicles(
scenario=scenario,
vehicle_type="truck",
version=self.version,
Expand Down Expand Up @@ -1199,8 +1184,8 @@ def update_buses(self) -> None:
self.scenarios[s] = results[s][0]

else:
for scenario in self.scenarios:
scenario, _ = _update_vehicles(
for s, scenario in enumerate(self.scenarios):
self.scenarios[s], _ = _update_vehicles(
scenario=scenario,
vehicle_type="bus",
version=self.version,
Expand Down Expand Up @@ -1277,11 +1262,11 @@ def update_emissions(self) -> None:
results = pool.starmap(_update_emissions, args)

for s, scenario in enumerate(self.scenarios):
self.scenarios[s] = results[s][0]
self.scenarios[s] = results[s]

else:
for scenario in self.scenarios:
scenario = _update_emissions(
for s, scenario in enumerate(self.scenarios):
self.scenarios[s] = _update_emissions(
scenario=scenario,
version=self.version,
system_model=self.system_model,
Expand Down Expand Up @@ -1319,11 +1304,11 @@ def update_all(self) -> None:
results = pool.starmap(_update_all, args)

for s, scenario in enumerate(self.scenarios):
self.scenarios[s] = results[s][0]
self.scenarios[s] = results[s]

else:
for scenario in self.scenarios:
scenario = _update_all(
for s, scenario in enumerate(self.scenarios):
self.scenarios[s] = _update_all(
scenario=scenario,
version=self.version,
system_model=self.system_model,
Expand Down Expand Up @@ -1448,6 +1433,9 @@ def write_db_to_brightway(self, name: [str, List[str]] = None):
# generate change report from logs
self.generate_change_report()

def _export_to_matrices(obj):
obj.export_db_to_matrices()

def write_db_to_matrices(self, filepath: str = None):
"""
Expand Down Expand Up @@ -1488,7 +1476,30 @@ def write_db_to_matrices(self, filepath: str = None):
cache = {}

# use multiprocessing to speed up the process
# use multiprocessing to speed up the process

if self.multiprocessing:
with ProcessPool(processes=multiprocessing.cpu_count()) as pool:
args = [
(
scenario,
cache,
self.version,
self.system_model,
)
for scenario in self.scenarios
]
results = pool.starmap(_prepare_database, args)

for s, scenario in enumerate(self.scenarios):
self.scenarios[s] = results[s][0]
cache.update(results[s][1])

with ProcessPool(processes=multiprocessing.cpu_count()) as pool:
args = [
Export(scenario, filepath[scen], self.version)
for scen, scenario in enumerate(self.scenarios)
]
pool.map(_export_to_matrices, args)

for scenario in self.scenarios:
scenario, cache = _prepare_database(
Expand Down

0 comments on commit 28c16fc

Please sign in to comment.