Skip to content

Commit

Permalink
Fix issue with loading and dumping databases.
Browse files Browse the repository at this point in the history
Version bump.
  • Loading branch information
romainsacchi committed May 13, 2024
1 parent 1a7eb8a commit 5b98b13
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
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__ = (2, 1, 0, "dev6")
__version__ = (2, 1, 0, "dev7")


from premise.new_database import NewDatabase
Expand Down
2 changes: 1 addition & 1 deletion premise/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ def check_geographical_linking(scenario, original_database):

# geo = Geomap(scenario["model"])

index = scenario["index"]
index = scenario.get("index") or {}
database = scenario["database"]
original_datasets = [
(a["name"], a["reference product"], a["location"]) for a in original_database
Expand Down
13 changes: 8 additions & 5 deletions premise/new_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@ def __init__(
else:
self.additional_inventories = None

# unlink all files in the cache directory
delete_all_pickles()

if external_scenarios:
print(
"External scenarios should now be given as part of the scenarios list. "
Expand Down Expand Up @@ -883,15 +886,15 @@ def update(self, sectors: [str, list, None] = None) -> None:
[item for item in sectors if item not in sector_update_methods]
)

# unlink all files in the cache directory
delete_all_pickles()

with tqdm(
total=len(self.scenarios), desc="Processing scenarios", ncols=70
) as pbar_outer:
for scenario in self.scenarios:
# add database to scenarios
scenario["database"] = pickle.loads(pickle.dumps(self.database, -1))
if "database filepath" in scenario:
scenario = load_database(scenario)
else:
scenario["database"] = pickle.loads(pickle.dumps(self.database, -1))
for sector in sectors:
if sector in scenario.get("applied functions", []):
print(
Expand Down Expand Up @@ -1206,7 +1209,7 @@ def write_datapackage(
def generate_scenario_report(
self,
filepath: [str, Path] = None,
name: str = f"scenario_report_{datetime.now().strftime('%d-%m-%Y %H-%M')} (v.{str(__version__)}).xlsx",
name: str = f"scenario_report_{datetime.now().strftime('%d-%m-%Y')}.xlsx",
):
"""
Generate a report of the scenarios.
Expand Down
5 changes: 2 additions & 3 deletions premise/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def dump_database(scenario):
:param scenario: scenario dictionary
"""

if "database filepath" in scenario:
if scenario.get("database") is None:
return scenario

# generate random name
Expand All @@ -375,7 +375,7 @@ def load_database(scenario):
:param scenario: scenario dictionary
"""

if "database" in scenario:
if scenario.get("database") is not None:
return scenario

filepath = scenario["database filepath"]
Expand All @@ -394,6 +394,5 @@ def delete_all_pickles():
"""
Delete all pickle files in the cache folder.
"""

for file in DIR_CACHED_FILES.glob("*.pickle"):
file.unlink()

0 comments on commit 5b98b13

Please sign in to comment.