mescal.database =============== .. py:module:: mescal.database Classes ------- .. autoapisummary:: mescal.database.Database mescal.database.Dataset Module Contents --------------- .. py:class:: Database(db_names: str | list[str] = None, db_as_list: list[dict] = None, create_pickle: bool = False, load_dependencies: bool = False) 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 :param db_names: Name of the LCI database(s). Should be a string for a single database or a list of strings for several databases. :param db_as_list: List of dictionaries of the LCI database. :param create_pickle: if True, create a pickle file to store the database. Only used if db_names is provided. :param load_dependencies: if True, load the dependencies of the database(s). Only used if db_names is provided. .. py:method:: change_name(new_db_name: str, database_type: str = 'technosphere') -> None Change the name of the database :param new_db_name: new name of the database :param database_type: type of database to be written, can be 'technosphere' or 'biosphere' :return: None .. py:method:: 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 Create a new database containing all activities that tare not provided in your main database :param df_mapping: dataframe with the mapping of the technologies and resources :param main_db_name: name of the main LCI database :param complement_db_name: name of the complementary LCI database :param premise_changes: file of the changes in names, products, locations, in premise regarding the mapping :param write_database: if True, write the complementary database to Brightway :return: dataframe with the mapping of the technologies and resources linked to the premise database .. py:method:: delete() -> None Delete a database from Brightway :return: None .. py:method:: dependencies() -> list[str] Get the dependencies of the database :return: list of dependencies .. py:method:: get_code(product: str, activity: str, location: str, database: str) -> str or None Get the code of an activity :param product: product of the LCI dataset :param activity: name of the LCI dataset :param location: location of the LCI dataset :param database: name of the LCI database :return: activity code in the LCI database or None if it does not exist .. py:method:: list_to_dict(key: str, database_type: str = 'technosphere') -> dict 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. :param key: can be 'code' or 'name' :param database_type: can be 'technosphere' or 'biosphere' :return: LCI database as a dictionary .. py:method:: load() -> list[dict] Load or extract a single database :return: list of dictionaries of the LCI database .. py:method:: load_multiple() -> list[dict] Concatenates databases in a list of dictionaries :return: list of dictionaries of the concatenated databases .. py:method:: 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, based_on: str = 'code') -> None 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. :param main_ecoinvent_db_name: name of the main database, e.g., ecoinvent or premise database :param new_db_name: name of the new merged database (only required if write is True) :param old_main_db_names: other main databases that are not in the list of databases, thus the list of databases will be unlinked from those :param write: if True, write the new database to Brightway :param check_duplicates: if True, check for duplicates in terms of (product, name, location) and remove them from exchanges and from the database :param based_on: can be 'code' or 'name'. Useful in case of background databases being different from main_ecoinvent_db_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. :return: None .. py:method:: 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 Relink a database based on activity codes or activity names and write it to your brightway project :param name_database_unlink: name of the database to unlink :param name_database_relink: name of the database to relink :param database_relink_as_list: list of dictionaries of the database to relink :param name_new_db: name of the new database, if None, the original database is overwritten (if write is True) :param based_on: 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. :param write: if True, write the new database to Brightway :return: None .. py:method:: 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]] :staticmethod: Relink the activities in the complementary database to the premise database :param name_complement_db: name of the complementary database :param base_db_list: list of activities in the base database :param premise_db_list: list of activities in the premise database :param name_premise_db: name of the premise database :return: list of unlinked flows, updated premise database .. py:method:: test_mapping_file(mapping: pandas.DataFrame) -> list[tuple[str, str, str, str]] Test if the mapping file is correctly linked to the database :param mapping: mapping file between the LCI database and the ESM database :return: the list missing flows if any .. py:method:: write_to_brightway(new_db_name: str, database_type: str = 'technosphere', overwrite: bool = True) -> None Write a LCI database to a Brightway project. This function will overwrite the database if it already exists. :param new_db_name: name of the brightway database to be written :param database_type: type of database to be written, can be 'technosphere' or 'biosphere' :param overwrite: if True, overwrite the database if it already exists :return: None .. py:method:: wurst_to_brightway(database_type: str = 'technosphere') -> None Adjust the database to the Brightway format :param database_type: type of database to be written, can be 'technosphere' or 'biosphere' :return: None .. py:attribute:: create_pickle :value: False .. py:property:: db_as_dict_code :type: dict .. py:property:: db_as_dict_name :type: dict .. py:attribute:: logger .. py:attribute:: overwritten_cpc_categories :value: [] .. py:class:: Dataset(act: dict) Class to perform basic operations on LCI datasets (as dictionaries) Initialize the dataset :param act: LCI dataset .. py:method:: get_biosphere_flows() -> list[dict] Get the biosphere flows of an activity :return: list of biosphere flows .. py:method:: get_downstream_consumers(db: list[dict]) -> list[dict] Get the downstream consumers of an activity :param db: list of activities of the LCI database :return: list of downstream consumers .. py:method:: get_production_flow() -> dict or None Get the production flow of an activity :return: production flow or None if it does not exist .. py:method:: get_technosphere_flows() -> list[dict] Get the technosphere flows of an activity :return: list of technosphere flows .. py:method:: relink(name_database_unlink: str, name_database_relink: str = None, database_relink_as_list: list[dict] = None, except_units: list[str] = None) -> None Relink the exchanges of an activity from one database to another. :param name_database_unlink: name of the database to unlink from :param name_database_relink: name of the database to relink to. If None, the database name is taken from the `database_relink_as_list` parameter. :param database_relink_as_list: list of dictionaries of the database to relink to. If None, the database name is taken from the `name_database_relink` parameter. :param except_units: list of units to exclude from the relinking. If None, all units are relinked. :return: None .. py:attribute:: act .. py:attribute:: activity .. py:attribute:: database .. py:attribute:: location .. py:attribute:: product