Forecast Features - Definitions¶
ForecastFeatureDefinitions(perfdb)
¶
Class used for handling forecast feature definitions. Can be accessed via perfdb.forecasts.features.definitions.
Parameters:
Source code in echo_postgres/perfdb_root.py
def __init__(self, perfdb: e_pg.PerfDB) -> None:
"""Base class that all subclasses should inherit from.
Parameters
----------
perfdb : PerfDB
Top level object carrying all functionality and the connection handler.
"""
self._perfdb: e_pg.PerfDB = perfdb
get(forecast_models=None, feature_names=None, data_source_types=None, filter_type='and', output_type='DataFrame')
¶
Gets all feature definitions.
The most useful keys/columns returned are:
- display_name
- description
- forecast_model_name
- data_source_type_name
- name_in_data_source
- id_in_data_source
Parameters:
-
(forecast_models¶list[str] | None, default:None) –List of forecast model names to filter the results. By default None
-
(feature_names¶list[str] | None, default:None) –List of feature names to filter the results. By default None
-
(data_source_types¶list[str] | None, default:None) –List of data source type 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"
-
(output_type¶Literal['dict', 'DataFrame'], default:'DataFrame') –Output type of the data. Can be one of ["dict", "DataFrame"] By default "DataFrame"
Returns:
-
DataFrame–If output_type is "DataFrame", returns a DataFrame with the following format: index=MultiIndex[forecast_model_name, name], columns=[attribute, ...]
-
dict[str, dict[str, dict[str, Any]]]–If output_type is "dict", returns a dictionary with the following format: {forecast_model_name: {feature_name: {attribute: value, ...}, ...}, ...}
Source code in echo_postgres/forecast_feature_definitions.py
@validate_call
def get(
self,
forecast_models: list[str] | None = None,
feature_names: list[str] | None = None,
data_source_types: list[str] | None = None,
filter_type: Literal["and", "or"] = "and",
output_type: Literal["dict", "DataFrame"] = "DataFrame",
) -> DataFrame | dict[str, dict[str, dict[str, Any]]]:
"""Gets all feature definitions.
The most useful keys/columns returned are:
- display_name
- description
- forecast_model_name
- data_source_type_name
- name_in_data_source
- id_in_data_source
Parameters
----------
forecast_models : list[str] | None, optional
List of forecast model names to filter the results.
By default None
feature_names : list[str] | None, optional
List of feature names to filter the results.
By default None
data_source_types : list[str] | None, optional
List of data source type 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"
output_type : Literal["dict", "DataFrame"], optional
Output type of the data. Can be one of ["dict", "DataFrame"]
By default "DataFrame"
Returns
-------
DataFrame
If output_type is "DataFrame", returns a DataFrame with the following format: index=MultiIndex[forecast_model_name, name], columns=[attribute, ...]
dict[str, dict[str, dict[str, Any]]]
If output_type is "dict", returns a dictionary with the following format: {forecast_model_name: {feature_name: {attribute: value, ...}, ...}, ...}
"""
# checking inputs
where = self._check_get_args(
forecast_models=forecast_models,
feature_names=feature_names,
data_source_types=data_source_types,
filter_type=filter_type,
)
if output_type not in ["dict", "DataFrame"]:
raise ValueError(f"output_type must be one of ['dict', 'DataFrame'], not {output_type}")
# getting the feature definitions
query = [
sql.SQL("SELECT * FROM performance.v_forecast_model_features "),
]
if where:
query.append(where)
query.append(sql.SQL(" ORDER BY forecast_model_name, feature_name"))
query = sql.Composed(query)
with self._perfdb.conn.reconnect() as conn:
df = conn.read_to_pandas(query)
df = df.set_index(["forecast_model_name", "feature_name"])
if output_type == "dict":
# getting rid of unnecessary columns
df = df.drop(columns=["forecast_model_id"])
# converting to dictionary
result = df.to_dict(orient="index")
# converting from format {(forecast_model_name, feature_name): {attribute: value, ...}, ...} to {forecast_model_name: {feature_name: {attribute: value, ...}, ...}, ...}
final_result = {}
for (obj_model, feature_name), feature_data in result.items():
final_result.setdefault(obj_model, {})[feature_name] = feature_data
return final_result
return df
get_ids(forecast_models=None, feature_names=None, data_source_types=None, filter_type='and')
¶
Gets all feature definitions and their respective ids.
Parameters:
-
(forecast_models¶list[str] | None, default:None) –List of forecast model names to filter the results. By default None
-
(feature_names¶list[str] | None, default:None) –List of feature names to filter the results. By default None
-
(data_source_types¶list[str] | None, default:None) –List of data source type 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, dict[str, int]]–Dictionary with the following format: {forecast_model_name: {feature_name: feature_id, ...}, ...}
Source code in echo_postgres/forecast_feature_definitions.py
@validate_call
def get_ids(
self,
forecast_models: list[str] | None = None,
feature_names: list[str] | None = None,
data_source_types: list[str] | None = None,
filter_type: Literal["and", "or"] = "and",
) -> dict[str, dict[str, int]]:
"""Gets all feature definitions and their respective ids.
Parameters
----------
forecast_models : list[str] | None, optional
List of forecast model names to filter the results.
By default None
feature_names : list[str] | None, optional
List of feature names to filter the results.
By default None
data_source_types : list[str] | None, optional
List of data source type 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, dict[str, int]]
Dictionary with the following format:
{forecast_model_name: {feature_name: feature_id, ...}, ...}
"""
# checking inputs
where = self._check_get_args(
forecast_models=forecast_models,
feature_names=feature_names,
data_source_types=data_source_types,
filter_type=filter_type,
)
# getting the feature definitions
query = [
sql.SQL("SELECT forecast_model_name, feature_name, feature_id FROM performance.v_forecast_model_features "),
]
if where:
query.append(where)
query.append(sql.SQL(" ORDER BY forecast_model_name, feature_name"))
query = sql.Composed(query)
with self._perfdb.conn.reconnect() as conn:
df = conn.read_to_pandas(query)
df = df.set_index(["forecast_model_name", "feature_name"])
# converting to dictionary
result = df["feature_id"].to_dict()
# converting from format {(forecast_model_name, feature_name): feature_id, ...} to {forecast_model_name: {feature_name: feature_id, ...}, ...}
final_result = {}
for (obj_model, feature_name), feature_id in result.items():
final_result.setdefault(obj_model, {})[feature_name] = feature_id
return final_result