-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix multiprocessing implementation for matrices export.
- Loading branch information
1 parent
6b2251f
commit 1217a93
Showing
4 changed files
with
154 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
from typing import List | ||
import copy | ||
from datetime import date | ||
from multiprocessing import Pool as ProcessPool | ||
import multiprocessing | ||
from pathlib import Path | ||
from datapackage import Package | ||
|
||
from . import __version__ | ||
from .ecoinvent_modification import NewDatabase | ||
from .export import ( | ||
Export, | ||
_prepare_database, | ||
build_datapackage, | ||
generate_scenario_factor_file, | ||
generate_superstructure_db, | ||
) | ||
|
||
|
||
class PathwaysDataPackage: | ||
def __init__( | ||
self, | ||
scenarios: List[dict], | ||
source_version: str = "3.9", | ||
source_type: str = "brightway", | ||
key: bytes = None, | ||
source_db: str = None, | ||
source_file_path: str = None, | ||
additional_inventories: List[dict] = None, | ||
system_model: str = "cutoff", | ||
system_args: dict = None, | ||
external_scenarios: list = None, | ||
gains_scenario="CLE", | ||
use_absolute_efficiency=False, | ||
): | ||
self.scenarios = scenarios | ||
self.source_db = source_db | ||
self.source_version = source_version | ||
self.key = key | ||
self.years = None | ||
|
||
self.datapackage = NewDatabase( | ||
scenarios=scenarios, | ||
source_version=source_version, | ||
source_type=source_type, | ||
key=key, | ||
source_db=source_db, | ||
source_file_path=source_file_path, | ||
additional_inventories=additional_inventories, | ||
system_model=system_model, | ||
system_args=system_args, | ||
external_scenarios=external_scenarios, | ||
gains_scenario=gains_scenario, | ||
use_absolute_efficiency=use_absolute_efficiency, | ||
) | ||
|
||
def create_datapackage(self, name: str = f"pathways_{date.today()}"): | ||
self.datapackage.update_all() | ||
self.export_datapackage(name) | ||
|
||
def export_datapackage(self, name: str): | ||
# create matrices in current directory | ||
self.datapackage.write_db_to_matrices() | ||
self.build_datapackage(name) | ||
|
||
def build_datapackage(self, name): | ||
""" | ||
Create and export a scenario datapackage. | ||
""" | ||
|
||
package = Package(base_path=Path.cwd()) | ||
package.infer("**/*.csv") | ||
package.descriptor["name"] = name | ||
package.descriptor["title"] = name.capitalize() | ||
package.descriptor[ | ||
"description" | ||
] = f"Data package generated by premise {__version__}." | ||
package.descriptor["premise version"] = str(__version__) | ||
package.descriptor["scenarios"] = [ | ||
{ | ||
"name": f"{s['model'].upper()} - {s['pathway']}", | ||
"description": f"Prospective db, " | ||
f"based on {s['model'].upper()}, " | ||
f"pathway {s['pathway']}.", | ||
} | ||
for s in self.scenarios | ||
] | ||
package.descriptor["keywords"] = [ | ||
"ecoinvent", | ||
"scenario", | ||
"data package", | ||
"premise", | ||
"pathways" | ||
] | ||
package.descriptor["licenses"] = [ | ||
{ | ||
"id": "CC0-1.0", | ||
"title": "CC0 1.0", | ||
"url": "https://creativecommons.org/publicdomain/zero/1.0/", | ||
} | ||
] | ||
package.commit() | ||
|
||
# save the datapackage | ||
package.save(f"{name}.zip") | ||
|
||
print(f"Data package saved at {Path.cwd() / f'{name}.zip'}") |