util.py

lib.util.assign_values_based_on_series(series, dict)

This function fills a series based on the values of another series and a dictionary. The dictionary does not have to be sorted, it will be sorted before assigning the values. However, it must contain a key that is greater than any value in the series. It is equivalent to a function that maps ranges to discrete values.

Parameters
  • series (pandas series) – Series with input values that will be mapped.

  • dict (dictionary) – Dictionary defining the limits of the ranges that will be mapped.

Return result

Series with the mapped discrete values.

Return type

pandas series

lib.util.changem(A, newval, oldval)

This function replaces existing values oldval in a data array A by new values newval.

oldval and newval must have the same size.

Parameters
  • A (numpy array) – Input matrix.

  • newval (numpy array) – Vector of new values to be set.

  • oldval (numpy array) – Vector of old values to be replaced.

Return Out

The updated array.

Return type

numpy array

lib.util.create_json(filepath, param, param_keys, paths, paths_keys)

This function creates a metadata JSON file containing information about the file in filepath by storing the relevant keys from both the param and path dictionaries.

Parameters
  • filepath (string) – Path to the file for which the JSON file will be created.

  • param (dict) – Dictionary of dictionaries containing the user input parameters and intermediate outputs.

  • param_keys (list of strings) – Keys of the parameters to be extracted from the param dictionary and saved into the JSON file.

  • paths (dict) – Dictionary of dictionaries containing the paths for all files.

  • paths_keys (list of strings) – Keys of the paths to be extracted from the paths dictionary and saved into the JSON file.

Returns

The JSON file will be saved in the desired path filepath.

Return type

None

lib.util.display_progress(message, progress_stat)

This function displays a progress bar for long computations. To be used as part of a loop or with multiprocessing.

Parameters
  • message (string) – Message to be displayed with the progress bar.

  • progress_stat (tuple(int, int)) – Tuple containing the total length of the calculation and the current status or progress.

Returns

The status bar is printed.

Return type

None

lib.util.expand_dataframe(df, column_names)

This function reads a dataframe where columns with known column_names have multiple values separated by a semicolon in each entry. It expands the dataframe by creating a row for each value in each of these columns.

Parameters
  • df (pandas dataframe) – The original dataframe, with multiple values in some entries.

  • column_names (list) – Names of columns where multiple values have to be separated.

Return df_final

The expanded dataframe, where each row contains only one value per column.

Return type

pandas dataframe

lib.util.field_exists(field_name, shp_path)

This function returns whether the specified field exists or not in the shapefile linked by a path.

Parameters
  • field_name (str) – Name of the field to be checked for.

  • shp_path (str) – Path to the shapefile.

Returns

True if it exists or False if it doesn’t exist.

Return type

bool

lib.util.get_sectoral_profiles(paths, param)

This function reads the raw standard load profiles, repeats them to obtain a full year, normalizes them so that the sum is equal to 1, and stores the obtained load profile for each sector in the dataframe profiles.

Parameters
  • paths (dict) – Dictionary containing the paths to dict_daytype, dict_season, and to the raw standard load profiles.

  • param (dict) – Dictionary containing the year and load-related assumptions.

Return profiles

The normalized load profiles for the sectors.

Return type

pandas dataframe

lib.util.resizem(A_in, row_new, col_new)

This function resizes regular data grid, by copying and pasting parts of the original array.

Parameters
  • A_in (numpy array) – Input matrix.

  • row_new (integer) – New number of rows.

  • col_new (integer) – New number of columns.

Return A_out

Resized matrix.

Return type

numpy array

lib.util.reverse_lines(df)

This function reverses the line direction if the starting point is alphabetically after the end point.

Parameters

df (pandas dataframe) – Dataframe with columns ‘Region_start’ and ‘Region_end’.

Returns df_final

The same dataframe after the line direction has been reversed.

Return type

pandas dataframe

lib.util.timecheck(*args)

This function prints information about the progress of the script by displaying the function currently running, and optionally an input message, with a corresponding timestamp. If more than one argument is passed to the function, it will raise an exception.

Parameters

args (string) – Message to be displayed with the function name and the timestamp (optional).

Returns

The time stamp is printed.

Return type

None

Raise

Too many arguments have been passed to the function, the maximum is only one string.