Skip to content

Commit

Permalink
Fix export of superstructure DBs.
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Oct 9, 2023
1 parent 1215dbf commit 874acd5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
85 changes: 63 additions & 22 deletions dev/Untitled.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"cell_type": "code",
"execution_count": 1,
"id": "6d93e33a-b89e-4e56-bfb7-5a8a0a232053",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from premise import *\n",
Expand All @@ -16,7 +18,9 @@
"cell_type": "code",
"execution_count": 2,
"id": "10b37313-f780-4102-a6d4-7a813dad9d3f",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"scenarios = [\n",
Expand Down Expand Up @@ -47,13 +51,15 @@
"cell_type": "code",
"execution_count": 3,
"id": "bc61f1aa-81d4-4a83-b7c5-0b10ad1a5b65",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"premise v.(1, 7, 1)\n",
"premise v.(1, 7, 5)\n",
"+------------------------------------------------------------------+\n",
"| Warning |\n",
"+------------------------------------------------------------------+\n",
Expand Down Expand Up @@ -110,17 +116,19 @@
" #source_type=\"ecospold\",\n",
" #source_file_path=f\"/Users/romain/Documents/ecoinvent 3.9.1_cutoff_ecoSpold02/datasets\", # <-- this is NEW\n",
" key='tUePmX_S5B8ieZkkM7WUU2CnO8SmShwmAeWK9x2rTFo=',\n",
" system_model=\"consequential\",\n",
" use_multiprocessing=False\n",
" #system_model=\"consequential\",\n",
" #use_multiprocessing=False\n",
" #system_args=combinations[0]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "c7996c75-e3e7-42ba-9ecb-c016b6462109",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
Expand All @@ -129,7 +137,7 @@
"`update_all()` will skip the following steps:\n",
"update_two_wheelers(), update_cars(), and update_buses()\n",
"If you want to update these steps, please run them separately afterwards.\n",
"Extracted 1 worksheets in 4.56 seconds\n",
"Extracted 1 worksheets in 4.53 seconds\n",
"Migrating to 3.8 first\n",
"Applying strategy: migrate_datasets\n",
"Applying strategy: migrate_exchanges\n",
Expand All @@ -139,7 +147,7 @@
"Applying strategy: migrate_exchanges\n",
"Done!\n",
"Done!\n",
"Extracted 1 worksheets in 4.96 seconds\n",
"Extracted 1 worksheets in 4.51 seconds\n",
"Migrating to 3.8 first\n",
"Applying strategy: migrate_datasets\n",
"Applying strategy: migrate_exchanges\n",
Expand All @@ -149,14 +157,8 @@
"Applying strategy: migrate_exchanges\n",
"Done!\n",
"Done!\n",
"Extracted 1 worksheets in 4.91 seconds\n",
"Migrating to 3.8 first\n",
"Applying strategy: migrate_datasets\n",
"Applying strategy: migrate_exchanges\n",
"Applying strategy: migrate_datasets\n",
"Applying strategy: migrate_exchanges\n",
"Applying strategy: migrate_datasets\n",
"Applying strategy: migrate_exchanges\n"
"Done!\n",
"\n"
]
}
],
Expand All @@ -174,12 +176,51 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "a85ca090-8d49-42c2-8b21-ffc69fabc12d",
"metadata": {},
"outputs": [],
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Building superstructure database...\n",
"Dropped 0 duplicate(s).\n",
"Scenario difference file exported to /Users/romain/GitHub/premise/dev/export/scenario diff files!\n",
"Database test already exists: it will be overwritten.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Writing activities to SQLite3 database:\n",
"0% [##############################] 100% | ETA: 00:00:00\n",
"Total time elapsed: 00:00:50\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Title: Writing activities to SQLite3 database:\n",
" Started: 10/09/2023 10:04:44\n",
" Finished: 10/09/2023 10:05:34\n",
" Total time elapsed: 00:00:50\n",
" CPU %: 95.40\n",
" Memory %: 24.03\n",
"Created database: test\n",
"Generate scenario report.\n",
"Report saved under /Users/romain/GitHub/premise/dev/export/scenario_report.\n",
"Generate change report.\n",
"Report saved under /Users/romain/GitHub/premise/dev.\n"
]
}
],
"source": [
"ndb.write_db_to_brightway()"
"ndb.write_superstructure_db_to_brightway(\"test\")"
]
},
{
Expand Down
2 changes: 2 additions & 0 deletions premise/data/consequential/leadtimes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Solar PV Residential: 2
Wind Onshore: 2
Wind Offshore: 3
Wave: 3
Storage, Hydrogen: 3
Storage, Flow Battery: 3
diesel: 3
gasoline: 3
diesel, synthetic, from wood: 3
Expand Down
6 changes: 6 additions & 0 deletions premise/ecoinvent_modification.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,13 +1347,19 @@ def write_superstructure_db_to_brightway(
modified_datasets=self.modified_datasets,
)

if hasattr(self, "datapackages"):
list_scenarios = create_scenario_list(self.scenarios, self.datapackages)
else:
list_scenarios = create_scenario_list(self.scenarios)

self.database = generate_superstructure_db(
origin_db=self.database,
scenarios=self.scenarios,
db_name=name,
filepath=filepath,
version=self.version,
format=format,
scenario_list=list_scenarios,
)

write_brightway2_database(
Expand Down
8 changes: 6 additions & 2 deletions premise/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ def generate_scenario_difference_file(


def generate_superstructure_db(
origin_db, scenarios, db_name, filepath, version, format="excel"
origin_db, scenarios, db_name, filepath, version, scenario_list, format="excel",
) -> List[dict]:
"""
Build a superstructure database from a list of databases
Expand All @@ -878,7 +878,11 @@ def generate_superstructure_db(

# create the dataframe
df, new_db, _ = generate_scenario_difference_file(
origin_db=origin_db, scenarios=scenarios, db_name=db_name, version=version
origin_db=origin_db,
scenarios=scenarios,
db_name=db_name,
version=version,
scenario_list=scenario_list
)

# remove unneeded columns "to unit"
Expand Down

0 comments on commit 874acd5

Please sign in to comment.