shrecc.database =============== .. py:module:: shrecc.database Attributes ---------- .. autoapisummary:: shrecc.database.UNUSED_SOURCE Functions --------- .. autoapisummary:: shrecc.database.apply_cutoff shrecc.database.apply_mapping shrecc.database.create_activity_dict shrecc.database.create_database shrecc.database.filt_cutoff shrecc.database.filter_by_countries shrecc.database.filter_by_range shrecc.database.filter_by_times shrecc.database.get_network_activities shrecc.database.load_mapping_data shrecc.database.load_time_series_data shrecc.database.map_known_inputs shrecc.database.prepare_consumption_data shrecc.database.setup_database shrecc.database.tech_mapping Module Contents --------------- .. py:function:: apply_cutoff(df_filt, cutoff, include_cutoff) Apply a cutoff value to filter out smaller values in the dataframe and optionally include a "rest" category. :param df_filt: The filtered dataframe. :type df_filt: pd.DataFrame :param cutoff: The cutoff value for technology values. :type cutoff: float :param include_cutoff: If True, sums values below cutoff and includes them as a new technology "The rest". :type include_cutoff: bool :returns: A dataframe with values below the cutoff set to zero, optionally including a "rest" category. :rtype: pd.DataFrame .. py:function:: apply_mapping(Z_cons_to_multiply, el_map_all_norm) Apply the technology mapping to the consumption data. :param Z_cons_to_multiply: The consumption data to be mapped. :type Z_cons_to_multiply: pd.DataFrame :param el_map_all_norm: The normalized mapping data. :type el_map_all_norm: pd.DataFrame :returns: The resulting DataFrame after applying the technology mapping. :rtype: pd.DataFrame .. py:function:: create_activity_dict(dataframe_filt, known_inputs, known_inputs_network, db_name) Creates a dictionary of activities for the BW database based on the filtered dataframe and known inputs. :param dataframe_filt: The filtered dataframe containing technology data. :type dataframe_filt: pd.DataFrame :param known_inputs: A dictionary mapping known inputs to ecoinvent database entries. :type known_inputs: dict :param known_inputs_network: A dictionary mapping known network inputs to ecoinvent database entries. :type known_inputs_network: dict :param db_name: The name of the BW database. :type db_name: str :returns: A dictionary containing activities to be written to the BW2 database. :rtype: dict .. py:function:: create_database(dataframe_filt, project_name, db_name, eidb_name, network='True') Creates an "ecoinvent-like" BW database based on a previously filtered dataframe. :param dataframe_filt: Scaled and filtered dataframe. :type dataframe_filt: pd.DataFrame :param project_name: BW project name to which the database will be saved. :type project_name: str :param db_name: Name of the BW database to be created. :type db_name: str :param eidb_name: Name of the ecoinvent database. Must be the same as in the BW project. :type eidb_name: str :param network: If True, network activities will be considered. :type network: bool :returns: None .. py:function:: filt_cutoff(countries, times=[], general_range=0, refined_range=0, freq=0, cutoff=0.001, include_cutoff=True, path_to_data=None) Filters data based on selected countries and times (either one-off, a range, or periodical range). :param year: Selected year of the downloaded data. :type year: int :param countries: Countries selected by the user for their database. E.g. countries=['FR', 'DE']. :type countries: list of str :param times: Selecting one specific time, e.g. times = ['2023-06-16 8:00:00', '2023-06-16 22:00:00']. Can be applied alone. :type times: list of str :param general_range: Selecting a general range, e.g. for the month of June general_range = ['2023-06-01 01:00:00', '2023-06-30 23:00:00']. Can be applied alone. :type general_range: list of str :param refined_range: Refining range of general range, e.g. mornings of June (previously selected in general_range): refined_range = [8, 9, 10, 11]. Can only be applied with general_range. :type refined_range: list of int :param freq: Days to be included, e.g. freq='D' selects calendar days, see https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases. :type freq: str :param cutoff: Cutoff value for technology values. :type cutoff: float :param include_cutoff: If True, cutoff is applied and summed at the end to create a new technology "The rest". If False, cutoff is applied but new technology not created. :type include_cutoff: bool :param path_to_data: location of the data. If none, the data is taken from within the package. :type path_to_data: str or str or Path :returns: The filtered dataframe. :rtype: pd.DataFrame .. py:function:: filter_by_countries(dataframe, countries) Filter the dataframe by selected countries. :param dataframe: The original dataframe containing data for multiple countries. :type dataframe: pd.DataFrame :param countries: A list of country codes to filter by. :type countries: list of str :returns: A dataframe filtered by the specified countries. :rtype: pd.DataFrame .. py:function:: filter_by_range(dataframe, general_range, refined_range, freq) Filter the dataframe by a general time range and optionally by a refined time range. :param dataframe: The original dataframe containing data. :type dataframe: pd.DataFrame :param general_range: The start and end of the general range to filter by (e.g., ['2023-06-01', '2023-06-30']). :type general_range: list of str :param refined_range: A list specifying the refined range of hours to filter within the general range. :type refined_range: list of int :param freq: The frequency for generating timestamps (e.g., 'D' for daily). :type freq: str :returns: A dataframe filtered by the specified time range and refined range. :rtype: pd.DataFrame .. py:function:: filter_by_times(dataframe, times) Filter the dataframe by specific times. :param dataframe: The original dataframe containing data for multiple times. :type dataframe: pd.DataFrame :param times: A list of specific times to filter by. :type times: list of str :returns: A dataframe filtered by the specified times. :rtype: pd.DataFrame .. py:function:: get_network_activities(eidb_name) .. py:function:: load_mapping_data(mapping_location) Load the mapping data from an Excel file. mapping_collection can be either a string pointing to a full file, or a directory. If it is a directory, it will assume that the file name is `el_map_all_norm.csv` :param mapping_location: a full filename as string or path to the scaled technology mapping. :type mapping_location: str or Path :returns: A DataFrame containing the technology mapping data from the Excel file. :rtype: pd.DataFrame .. py:function:: load_time_series_data(path_to_data, year) Load the time series data from a pickle file and format it as a DataFrame. :param path_to_data: The path to the directory containing the time series data. :type path_to_data: str or Path :param year: The year corresponding to the time series data. :type year: int :returns: A DataFrame containing the time series data, with levels reordered and sorted. :rtype: pd.DataFrame .. py:function:: map_known_inputs(eidb_name, dataframe_filt) Maps known inputs from the ecoinvent database to the filtered dataframe. :param eidb_name: The name of the ecoinvent database in the BW project. :type eidb_name: str :param dataframe_filt: The filtered dataframe containing technology data. :type dataframe_filt: pd.DataFrame :returns: A dictionary mapping known inputs to their corresponding entries in the ecoinvent database. :rtype: dict .. py:function:: prepare_consumption_data(Z_cons) Prepare the consumption data by removing trade data and adjusting indices. :param Z_cons: The original consumption data DataFrame. :type Z_cons: pd.DataFrame :returns: The prepared consumption data, with the trade data removed and indices swapped. :rtype: pd.DataFrame .. py:function:: setup_database(project_name, db_name) Sets up the BW2 database for the given project. :param project_name: The name of the BW project. :type project_name: str :param db_name: The name of the BW database to set up. :type db_name: str :returns: The newly registered BW2 database. :rtype: bd.Database .. py:function:: tech_mapping(year, path_to_data, path_to_mapping=None) Main function to map the technologies and scale them to 1 kWh. :param year: The year corresponding to the data. :type year: int :param path_to_data: Root directory of the data. :type path_to_data: str or Path :param path_to_mapping: File with the mapping of the scaled technology mappings. If None, it will use the mapping from the package. :type path_to_mapping: str or Path :returns: A DataFrame with the scaled technology mappings. :rtype: pd.DataFrame .. py:data:: UNUSED_SOURCE :value: 'Import balance (physical)'