Skip to content

Store scenario metadata on exported Brightway databases - #303

Merged
romainsacchi merged 1 commit into
polca:masterfrom
TimoDiepers:master
Aug 21, 2026
Merged

Store scenario metadata on exported Brightway databases#303
romainsacchi merged 1 commit into
polca:masterfrom
TimoDiepers:master

Conversation

@TimoDiepers

Copy link
Copy Markdown
Contributor

What

Writing new databases did not save any metadata on what the databases represent. This PR attaches information on the used scenario, years, etc. to the Brightway database metadata, next to the fields brightway maintains itself:

import bw2data as bd

bd.databases["ei_cutoff_3.10.1_remind_SSP2-PkBudg500_2050"]
{
    # written by brightway
    "format": "Ecoinvent XML",
    "depends": ["ecoinvent-3.10.1-biosphere"],
    "backend": "sqlite",
    "number": 43648,
    "modified": "2026-08-14T12:09:25.945746",
    "processed": "2026-08-14T12:09:56.124243",
    "geocollections": ["world"],
    "searchable": True,
    # written by premise
    "premise_version": "2.4.9.1",
    "iam_model": "remind",
    "pathway": "SSP2-PkBudg500",
    "representative_time": "2050-01-01T00:00:00",
    "ecoinvent_version": "3.10.1",
    "system_model": "cutoff",
}

bd.databases["db_name"] and bd.Database("db_name").metadata are the same mapping, so the fields can be read either way.

User (external) scenarios, if any, are listed under external_scenarios. Superstructure and scenario-array databases list their scenarios under scenarios, and only carry representative_time at the top level when all their scenarios share the same year.

How

  • scenario_metadata() and database_metadata() in premise/utils.py.
  • write_brightway_database() in both premise/brightway2.py and premise/brightway25.py takes an optional metadata argument, applied after the database is registered (fast and regular write paths).
  • NewDatabase.write_db_to_brightway, write_superstructure_db_to_brightway and write_scenario_array_db_to_brightway pass the metadata of the scenario(s) they write.

Tests

New tests/test_database_metadata.py; existing writer test doubles updated for the new keyword argument.

* Store scenario metadata on exported Brightway databases

Databases written to Brightway only encoded the IAM model, pathway and
year in their name. Attach them to the Brightway database metadata
instead, together with an ISO 8601 `representative_time` timestamp of the
point in time the database represents, the ecoinvent version, the system
model and the premise version.

Superstructure and scenario-array databases list their scenarios under
`scenarios`, and only carry `year`/`representative_time` at the top level
when all their scenarios share the same year.

* Address review: drop redundant year, expand docs example

- `year` is dropped from the metadata: it is already carried by the ISO
  8601 `representative_time` timestamp.
- The changelog entry moves under `[Unreleased]`.
- The docs example shows the fields brightway writes itself next to the
  ones premise adds, and notes that `bd.databases["db_name"]` and
  `bd.Database("db_name").metadata` are the same mapping.
- The `metadata` argument docstrings are removed from both writers.
@romainsacchi
romainsacchi merged commit 2d0853b into polca:master Aug 21, 2026
4 of 5 checks passed
@romainsacchi

Copy link
Copy Markdown
Collaborator

Very nice, thanks @TimoDiepers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants