Skip to content

Forecast Models

ForecastModels(perfdb)

Class used for handling forecast models. Can be accessed via perfdb.forecasts.models.

Parameters:

  • perfdb

    (PerfDB) –

    Top level object carrying all functionality and the connection handler.

Source code in echo_postgres/forecast_models.py
def __init__(self, perfdb: e_pg.PerfDB) -> None:
    """Class used for handling forecast models. Can be accessed via `perfdb.forecasts.models`.

    Parameters
    ----------
    perfdb : PerfDB
        Top level object carrying all functionality and the connection handler.
    """
    super().__init__(perfdb)

    from .forecast_model_attributes import ForecastModelAttributes

    # * subclasses

    self.attributes = ForecastModelAttributes(perfdb)

get(forecast_models=None, filter_type='and', get_attributes=False, output_type='dict')

Gets all forecast models definitions with detailed information.

The most useful keys/columns returned are:

  • id
  • display_name
  • description
  • data_source_name
  • data_source_type_name

Parameters:

  • forecast_models

    (list[str] | None, default: None ) –

    List of forecast model names to filter the results. By default None

  • filter_type

    (Literal['and', 'or'], default: 'and' ) –

    How to treat multiple filters. Can be one of ["and", "or"]. By default "and"

  • get_attributes

    (bool, default: False ) –

    If True, will also get the attributes of the forecast models.

  • output_type

    (Literal['dict', 'DataFrame'], default: 'dict' ) –

    Output type of the data. Can be one of ["dict", "DataFrame"] By default "dict"

Returns:

  • dict[str, dict[str, str | int]]

    In case output_type is "dict", returns a dictionary in the format {name: {attribute: value, ...}, ...}

  • DataFrame

    In case output_type is "DataFrame", returns a DataFrame with the following format: index = name, columns = [attribute, ...]

Source code in echo_postgres/forecast_models.py
@validate_call
def get(
    self,
    forecast_models: list[str] | None = None,
    filter_type: Literal["and", "or"] = "and",
    get_attributes: bool = False,
    output_type: Literal["dict", "DataFrame"] = "dict",
) -> dict[str, dict[str, str | int]] | DataFrame:
    """Gets all forecast models definitions with detailed information.

    The most useful keys/columns returned are:

    - id
    - display_name
    - description
    - data_source_name
    - data_source_type_name

    Parameters
    ----------
    forecast_models : list[str] | None, optional
        List of forecast model names to filter the results.
        By default None
    filter_type : Literal["and", "or"], optional
        How to treat multiple filters. Can be one of ["and", "or"].
        By default "and"
    get_attributes : bool, optional
        If True, will also get the attributes of the forecast models.
    output_type : Literal["dict", "DataFrame"], optional
        Output type of the data. Can be one of ["dict", "DataFrame"]
        By default "dict"

    Returns
    -------
    dict[str, dict[str, str | int]]
        In case output_type is "dict", returns a dictionary in the format {name: {attribute: value, ...}, ...}
    DataFrame
        In case output_type is "DataFrame", returns a DataFrame with the following format: index = name, columns = [attribute, ...]
    """
    # checking inputs
    where = self._check_get_args(forecast_models, filter_type)

    query = [
        sql.SQL("SELECT * FROM performance.v_forecast_models "),
        where,
        sql.SQL(" ORDER BY name"),
    ]
    query = sql.Composed(query)

    with self._perfdb.conn.reconnect() as conn:
        df = conn.read_to_pandas(query)
    df = df.set_index("name")

    # getting attributes
    if get_attributes:
        # names of the forecast models
        got_forecast_models = df.index.tolist()
        attrs: DataFrame = self._perfdb.forecasts.models.attributes.get(
            forecast_models=got_forecast_models,
            output_type="DataFrame",
            values_only=True,
        )
        # pivot the attributes
        attrs = attrs.reset_index(drop=False).pivot(index="forecast_model_name", columns="attribute_name", values="attribute_value")
        # merging the attributes with the forecast models
        df = df.merge(attrs, left_index=True, right_index=True, how="left")

    return df.to_dict(orient="index") if output_type == "dict" else df

get_ids(forecast_models=None, filter_type='and')

Gets all forecast models and their respective ids.

Parameters:

  • forecast_models

    (list[str] | None, default: None ) –

    List of forecast model names to filter the results. By default None

  • filter_type

    (Literal['and', 'or'], default: 'and' ) –

    How to treat multiple filters. Can be one of ["and", "or"]. By default "and"

Returns:

  • dict[str, int]

    Dictionary with all forecast models and their respective ids in the format {name: id, ...}.

Source code in echo_postgres/forecast_models.py
@validate_call
def get_ids(
    self,
    forecast_models: list[str] | None = None,
    filter_type: Literal["and", "or"] = "and",
) -> dict[str, int]:
    """Gets all forecast models and their respective ids.

    Parameters
    ----------
    forecast_models : list[str] | None, optional
        List of forecast model names to filter the results.
        By default None
    filter_type : Literal["and", "or"], optional
        How to treat multiple filters. Can be one of ["and", "or"].
        By default "and"

    Returns
    -------
    dict[str, int]
        Dictionary with all forecast models and their respective ids in the format {name: id, ...}.
    """
    # checking inputs
    where = self._check_get_args(forecast_models, filter_type)

    query = [
        sql.SQL("SELECT name, id FROM performance.v_forecast_models "),
        where,
        sql.SQL(" ORDER BY name"),
    ]
    query = sql.Composed(query)

    with self._perfdb.conn.reconnect() as conn:
        df = conn.read_to_pandas(query)

    return df.set_index("name").to_dict()["id"]