mescal.database
Classes
Class to perform basic operations on LCI databases. Databases can be formulated as a list of dictionaries or as a |
|
Class to perform basic operations on LCI datasets (as dictionaries) |
Module Contents
- class mescal.database.Database(db_names: str | list[str] = None, db_as_list: list[dict] = None, create_pickle: bool = False, load_dependencies: bool = False)[source]
Class to perform basic operations on LCI databases. Databases can be formulated as a list of dictionaries or as a dictionary with the (database, code) tuple or the (name, product, location, database) tuple as key.
Initialize the database
- Parameters:
db_names (str | list[str]) – Name of the LCI database(s). Should be a string for a single database or a list of strings for several databases.
db_as_list (list[dict]) – List of dictionaries of the LCI database.
create_pickle (bool) – if True, create a pickle file to store the database. Only used if db_names is provided.
load_dependencies (bool) – if True, load the dependencies of the database(s). Only used if db_names is provided.
- change_name(new_db_name: str, database_type: str = 'technosphere') None[source]
Change the name of the database
- Parameters:
new_db_name (str) – new name of the database
database_type (str) – type of database to be written, can be ‘technosphere’ or ‘biosphere’
- Returns:
None
- Return type:
None
- create_complementary_database(df_mapping: pandas.DataFrame, main_db_name: str = None, complement_db_name: str = None, premise_changes: pandas.DataFrame = None, write_database: bool = True) pandas.DataFrame[source]
Create a new database containing all activities that tare not provided in your main database
- Parameters:
df_mapping (pandas.DataFrame) – dataframe with the mapping of the technologies and resources
main_db_name (str) – name of the main LCI database
complement_db_name (str) – name of the complementary LCI database
premise_changes (pandas.DataFrame) – file of the changes in names, products, locations, in premise regarding the mapping
write_database (bool) – if True, write the complementary database to Brightway
- Returns:
dataframe with the mapping of the technologies and resources linked to the premise database
- Return type:
pandas.DataFrame
- dependencies() list[str][source]
Get the dependencies of the database
- Returns:
list of dependencies
- Return type:
list[str]
- get_code(product: str, activity: str, location: str, database: str) str or None[source]
Get the code of an activity
- Parameters:
product (str) – product of the LCI dataset
activity (str) – name of the LCI dataset
location (str) – location of the LCI dataset
database (str) – name of the LCI database
- Returns:
activity code in the LCI database or None if it does not exist
- Return type:
str or None
- list_to_dict(key: str, database_type: str = 'technosphere') dict[source]
Converts a list of dictionaries into a dictionary with the (database, code) tuple as key when key = ‘code’, the (name, product, location, database) tuple as key when key = ‘name’ for a technosphere database, or the (name, categories, database) tuple as key when key = ‘name’ for a biosphere database.
- Parameters:
key (str) – can be ‘code’ or ‘name’
database_type (str) – can be ‘technosphere’ or ‘biosphere’
- Returns:
LCI database as a dictionary
- Return type:
dict
- load() list[dict][source]
Load or extract a single database
- Returns:
list of dictionaries of the LCI database
- Return type:
list[dict]
- load_multiple() list[dict][source]
Concatenates databases in a list of dictionaries
- Returns:
list of dictionaries of the concatenated databases
- Return type:
list[dict]
- merge(main_ecoinvent_db_name: str, new_db_name: str = None, old_main_db_names: list[str] or None = None, write: bool = False, check_duplicates: bool = False) None[source]
Merge multiple LCI databases in one database. The list of databases should contain one main database (e.g., an ecoinvent or premise database) towards which all other databases will be relinked. If you suspect that there might be duplicated LCI datasets over the different databases, you can set check_duplicates to True to remove them.
- Parameters:
main_ecoinvent_db_name (str) – name of the main database, e.g., ecoinvent or premise database
new_db_name (str) – name of the new merged database (only required if write is True)
old_main_db_names (list[str] or None) – other main databases that are not in the list of databases, thus the list of databases will be unlinked from those
write (bool) – if True, write the new database to Brightway
check_duplicates (bool) – if True, check for duplicates in terms of (product, name, location) and remove them from exchanges and from the database
- Returns:
None
- Return type:
None
- relink(name_database_unlink: str, name_database_relink: str = None, database_relink_as_list: list[dict] = None, name_new_db: str = None, based_on: str = 'code', write: bool = False) None[source]
Relink a database based on activity codes or activity names and write it to your brightway project
- Parameters:
name_database_unlink (str) – name of the database to unlink
name_database_relink (str) – name of the database to relink
database_relink_as_list (list[dict]) – list of dictionaries of the database to relink
name_new_db (str) – name of the new database, if None, the original database is overwritten (if write is True)
based_on (str) – can be ‘code’ or ‘name’, if ‘code’, the relinking is done based on the code of the activities, if ‘name’, the relinking is done based on the name, product, location and database of the activities.
write (bool) – if True, write the new database to Brightway
- Returns:
None
- Return type:
None
- static relink_complement_db_to_premise_db(name_complement_db: str, base_db_list: list[dict], premise_db_list: list[dict], name_premise_db: str) tuple[list[dict], list[dict]][source]
Relink the activities in the complementary database to the premise database
- Parameters:
name_complement_db (str) – name of the complementary database
base_db_list (list[dict]) – list of activities in the base database
premise_db_list (list[dict]) – list of activities in the premise database
name_premise_db (str) – name of the premise database
- Returns:
list of unlinked flows, updated premise database
- Return type:
tuple[list[dict], list[dict]]
- test_mapping_file(mapping: pandas.DataFrame) list[tuple[str, str, str, str]][source]
Test if the mapping file is correctly linked to the database
- Parameters:
mapping (pandas.DataFrame) – mapping file between the LCI database and the ESM database
- Returns:
the list missing flows if any
- Return type:
list[tuple[str, str, str, str]]
- write_to_brightway(new_db_name: str, database_type: str = 'technosphere', overwrite: bool = True) None[source]
Write a LCI database to a Brightway project. This function will overwrite the database if it already exists.
- Parameters:
new_db_name (str) – name of the brightway database to be written
database_type (str) – type of database to be written, can be ‘technosphere’ or ‘biosphere’
overwrite (bool) – if True, overwrite the database if it already exists
- Returns:
None
- Return type:
None
- class mescal.database.Dataset(act: dict)[source]
Class to perform basic operations on LCI datasets (as dictionaries)
Initialize the dataset
- Parameters:
act (dict) – LCI dataset
- get_biosphere_flows() list[dict][source]
Get the biosphere flows of an activity
- Returns:
list of biosphere flows
- Return type:
list[dict]
- get_downstream_consumers(db: list[dict]) list[dict][source]
Get the downstream consumers of an activity
- Parameters:
db (list[dict]) – list of activities of the LCI database
- Returns:
list of downstream consumers
- Return type:
list[dict]
- get_production_flow() dict or None[source]
Get the production flow of an activity
- Returns:
production flow or None if it does not exist
- Return type:
dict or None
- get_technosphere_flows() list[dict][source]
Get the technosphere flows of an activity
- Returns:
list of technosphere flows
- Return type:
list[dict]
- relink(name_database_unlink: str, name_database_relink: str = None, database_relink_as_list: list[dict] = None, except_units: list[str] = None) None[source]
Relink the exchanges of an activity from one database to another.
- Parameters:
name_database_unlink (str) – name of the database to unlink from
name_database_relink (str) – name of the database to relink to. If None, the database name is taken from the database_relink_as_list parameter.
database_relink_as_list (list[dict]) – list of dictionaries of the database to relink to. If None, the database name is taken from the name_database_relink parameter.
except_units (list[str]) – list of units to exclude from the relinking. If None, all units are relinked.
- Returns:
None
- Return type:
None