Skip to main content

Use aggregated processes for quicker calculations

Project description

bw_aggregation

PyPI Status Python Version License

Read the documentation at https://bw_aggregation.readthedocs.io/ Tests Codecov

pre-commit Black

Installation

You can install bw_aggregation via [pip] from [PyPI]:

$ pip install bw_aggregation

It is also available via conda or mamba on the channel cmutel.

Theory

This library allows you to trade space for time by pre-computing some inventory results. Each Brightway Database can be aggregated - i.e. we can calculate the cumulative biosphere flows needed for each process in that flow. We then store these values separately, to be used instead of the normal technosphere supply chain entries. This is faster as we don't need to solve the linear proble Ax=b for that database subgraph.

As the supply chain data is removed, we can't do calculations which would use that supply chain data. That means we can't do:

  • Uncertainty analysis (no values in the technosphere array to sample from)
  • Graph traversal (the graph is cutoff for each process)
  • Regionalized LCIA (every biosphere flow would be matched to the location of the aggregated process)
  • Temporal LCA (no temporal supply chain data available)
  • Contribution analysis (no supply chain data to get contributions from)

As these downsides are significant, this library keeps both the unit process and aggregated data, and allows you to choose which to use during each calculation.

Usage

Start by getting an estimate on how much faster an aggregated calculation would be with:

import bw_aggregated as bwa
bwa.AggregatedDatabase.estimate_speedup("<database label>")

That will return something like:

TBD

If you want to convert that database, you can with:

bwa.AggregatedDatabase.convert_existing("<database label>")

From now on, calling bw2data.Database("<database label>") will return an instance of AggregatedDatabase. You can do everything you normally would with this database, including making changes.

:warning: Any existing Database("<database label>") reference is out of date: You need to create new Database object references.

The conversion command will also set the default to use the aggregated values during calculations. You can change the default back to using unit process data with:

import bw2data as bd
bd.Database("<database label>").use_aggregated(False)

To create a new Database as aggregated from the beginning, use:

bd.Database('<name>', backend='aggregated')

You can then write data with .write(some_data), and the aggregated datapackage will be generated automatically. However, individual changes to nodes or edges won't trigger a recalculation of the aggregated results - that needs to be done manually, see below.

You can also use a context manager to control which aggregated databases use their aggregated values during a calculation. The context manager allows you to set things globally - for example, to force the use of aggregated values for all aggregated databases:

import bw2calc as bc

with bwa.AggregationContext(True):
    lca = bc.LCA(my_functional_unit)
    lca.lci()

Passing in False will disable all use of aggregated values during the calculation. You can also be more fine grained by using a dictionary of database labels:

with bwa.AggregationContext({"<database label>": True, "<another database label>": False}):
    lca = bc.LCA(my_functional_unit)
    lca.lci()

As above, True forces the use of aggregated values, False prohibits their use.

Aggregated database results are checked at calculation time to make sure they are still valid. If the aggregated results are out of date, an ObsoleteAggregatedDatapackage error will be raised. You can then refresh the aggregation result cache with:

bd.Database("<database label>").refresh()

We don't do that for you automatically as it is usually quite computationally expensive.

You can build inventories such that two aggregated databases mutually reference each other. If both are obsolete, trying to refresh one will raise an error that the other is obsolete. In this case, you can refresh all obsolete aggregated databases with:

bwa.AggregatedDatabase.refresh_all()

Implementation

This library gets the possibility of using both aggregated and unit process data by overriding the .datapackage method, and loading one or two different datapackages depending on the current context. This approach is compatiable with both manual loading of datapackages, and with the bw2data function prepare_lca_inputs. The .datapackage method of an AggregatedDatabase is roughly:

if global_context is True:
    load_aggregated()
elif local_context(me) is True:
    load_aggregated()
elif me.perfer_aggregated is True:
    load_aggregated()
else:
    load_unit_process()

Contributing

Contributions are very welcome. To learn more, see the Contributor Guide.

License

Distributed under the terms of the MIT license, bw_aggregation is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bw_aggregation-1.0rc1.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

bw_aggregation-1.0rc1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file bw_aggregation-1.0rc1.tar.gz.

File metadata

  • Download URL: bw_aggregation-1.0rc1.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for bw_aggregation-1.0rc1.tar.gz
Algorithm Hash digest
SHA256 69a3cd1f97f15e2bd1de14b3d57f87e6d2b5f346b64dd588a4fdbabca5495ec9
MD5 7706d88ed3d2c7dd41985a266606ac2f
BLAKE2b-256 5280ae474ebf1b7ed4bd5efec038bb2ccb858a34a97f6ee09427359eedf319cc

See more details on using hashes here.

File details

Details for the file bw_aggregation-1.0rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for bw_aggregation-1.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 4c2721b09b5bbba0e76687dc257e4dd857484fc4b854222856614489eafc0900
MD5 2cf10fe7b4b30865fff802bc470d10b6
BLAKE2b-256 47f91e093e251e3f27a4db05ebd57a551ab7808c516d125fe75c7dfe6eaf16bb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page