bw2io.strategies.generic#

Module Contents#

Functions#

add_database_name(db, name)

Add database name to datasets

assign_only_product_as_production(db)

Assign only product as reference product.

convert_activity_parameters_to_list(data)

Convert activity parameters from dictionary to list of dictionaries

convert_uncertainty_types_to_integers(db)

Generic number conversion function convert to floats. Return to integers.

drop_falsey_uncertainty_fields_but_keep_zeros(db)

Drop fields like '' but keep zero and NaN.

drop_unlinked(db)

This is the nuclear option - use at your own risk!

format_nonunique_key_error(obj, fields, others)

link_iterable_by_fields(unlinked[, other, fields, ...])

Generic function to link objects in unlinked to objects in other using fields fields.

link_technosphere_by_activity_hash(db[, ...])

Link technosphere exchanges using activity_hash function.

normalize_units(db)

Normalize units in datasets and their exchanges

set_code_by_activity_hash(db[, overwrite])

Use activity_hash to set dataset code.

split_exchanges(data, filter_params, changed_attributes)

Split unlinked exchanges in data which satisfy filter_params into new exchanges with changed attributes.

tupleize_categories(db)

bw2io.strategies.generic.add_database_name(db, name)[source]#

Add database name to datasets

bw2io.strategies.generic.assign_only_product_as_production(db)[source]#

Assign only product as reference product.

Skips datasets that already have a reference product or no production exchanges. Production exchanges must have a name and an amount.

Will replace the following activity fields, if not already specified:

  • ‘name’ - name of reference product

  • ‘unit’ - unit of reference product

  • ‘production amount’ - amount of reference product

bw2io.strategies.generic.convert_activity_parameters_to_list(data)[source]#

Convert activity parameters from dictionary to list of dictionaries

bw2io.strategies.generic.convert_uncertainty_types_to_integers(db)[source]#

Generic number conversion function convert to floats. Return to integers.

bw2io.strategies.generic.drop_falsey_uncertainty_fields_but_keep_zeros(db)[source]#

Drop fields like ‘’ but keep zero and NaN.

Note that this doesn’t strip False, which behaves exactly like 0.

bw2io.strategies.generic.drop_unlinked(db)[source]#

This is the nuclear option - use at your own risk!

bw2io.strategies.generic.format_nonunique_key_error(obj, fields, others)[source]#

Generic function to link objects in unlinked to objects in other using fields fields.

The database to be linked must have uniqueness for each object for the given fields.

If kind, limit the exchanges in unlinked objects to types in kind.

If relink, link to objects which already have an input. Otherwise, skip already linked objects.

If internal, linked unlinked to other objects in unlinked. Each object must have the attributes database and code.

Link technosphere exchanges using activity_hash function.

If external_db_name, link against a different database; otherwise link internally.

If fields, link using only certain fields.

bw2io.strategies.generic.normalize_units(db)[source]#

Normalize units in datasets and their exchanges

bw2io.strategies.generic.set_code_by_activity_hash(db, overwrite=False)[source]#

Use activity_hash to set dataset code.

By default, won’t overwrite existing codes, but will if overwrite is True.

bw2io.strategies.generic.split_exchanges(data, filter_params, changed_attributes, allocation_factors=None)[source]#

Split unlinked exchanges in data which satisfy filter_params into new exchanges with changed attributes.

changed_attributes is a list of dictionaries with the attributes that should be changed.

allocation_factors is an optional list of floats to allocate the original exchange amount to the respective copies defined in changed_attributes. They don’t have to sum to one. If allocation_factors are not defined, then exchanges are split equally.

Resets uncertainty to UndefinedUncertainty (0).

To use this function as a strategy, you will need to curry it first using functools.partial.

Example usage:

split_exchanges(
    [
        {'exchanges': [{
            'name': 'foo',
            'location': 'bar',
            'amount': 20
        }, {
            'name': 'food',
            'location': 'bar',
            'amount': 12
        }]}
    ],
    {'name': 'foo'},
    [{'location': 'A'}, {'location': 'B', 'cat': 'dog'}
]
>>> [
    {'exchanges': [{
        'name': 'food',
        'location': 'bar',
        'amount': 12
    }, {
        'name': 'foo',
        'location': 'A',
        'amount': 12.,
        'uncertainty_type': 0
    }, {
        'name': 'foo',
        'location': 'B',
        'amount': 8.,
        'uncertainty_type': 0,
        'cat': 'dog',
    }]}
]
bw2io.strategies.generic.tupleize_categories(db)[source]#