Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

treat_runtimeparams fix optimization_time_step timedelta #396

Merged
merged 4 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 64 additions & 54 deletions src/emhass/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def treat_runtimeparams(
runtimeparams.get("optimization_time_step", runtimeparams.get("freq"))
)
params["retrieve_hass_conf"]["optimization_time_step"] = pd.to_timedelta(
optimization_time_step
optimization_time_step, "minutes"
)
else:
optimization_time_step = int(
Expand Down Expand Up @@ -338,59 +338,6 @@ def treat_runtimeparams(
optimization_time_step, delta_forecast, time_zone
)

# Treat passed forecast data lists
list_forecast_key = [
"pv_power_forecast",
"load_power_forecast",
"load_cost_forecast",
"prod_price_forecast",
"outdoor_temperature_forecast",
]
forecast_methods = [
"weather_forecast_method",
"load_forecast_method",
"load_cost_forecast_method",
"production_price_forecast_method",
"outdoor_temperature_forecast_method",
]

# Loop forecasts, check if value is a list and greater than or equal to forecast_dates
for method, forecast_key in enumerate(list_forecast_key):
if forecast_key in runtimeparams.keys():
if isinstance(runtimeparams[forecast_key], list) and len(
runtimeparams[forecast_key]
) >= len(forecast_dates):
params["passed_data"][forecast_key] = runtimeparams[forecast_key]
params["optim_conf"][forecast_methods[method]] = "list"
else:
logger.error(
f"ERROR: The passed data is either not a list or the length is not correct, length should be {str(len(forecast_dates))}"
)
logger.error(
f"Passed type is {str(type(runtimeparams[forecast_key]))} and length is {str(len(runtimeparams[forecast_key]))}"
)
# Check if string contains list, if so extract
if isinstance(runtimeparams[forecast_key], str):
if isinstance(ast.literal_eval(runtimeparams[forecast_key]), list):
runtimeparams[forecast_key] = ast.literal_eval(
runtimeparams[forecast_key]
)
list_non_digits = [
x
for x in runtimeparams[forecast_key]
if not (isinstance(x, int) or isinstance(x, float))
]
if len(list_non_digits) > 0:
logger.warning(
f"There are non numeric values on the passed data for {forecast_key}, check for missing values (nans, null, etc)"
)
for x in list_non_digits:
logger.warning(
f"This value in {forecast_key} was detected as non digits: {str(x)}"
)
else:
params["passed_data"][forecast_key] = None

# Add runtime exclusive (not in config) parameters to params
# regressor-model-fit
if set_type == "regressor-model-fit":
Expand Down Expand Up @@ -447,6 +394,16 @@ def treat_runtimeparams(
soc_final = runtimeparams["soc_final"]
params["passed_data"]["soc_final"] = soc_final

params["passed_data"]["operating_hours_of_each_deferrable_load"] = params[
"optim_conf"
].get("operating_hours_of_each_deferrable_load", None)
params["passed_data"]["start_timesteps_of_each_deferrable_load"] = params[
"optim_conf"
].get("start_timesteps_of_each_deferrable_load", None)
params["passed_data"]["end_timesteps_of_each_deferrable_load"] = params[
"optim_conf"
].get("end_timesteps_of_each_deferrable_load", None)

forecast_dates = copy.deepcopy(forecast_dates)[0:prediction_horizon]

# Load the default config
Expand Down Expand Up @@ -480,6 +437,59 @@ def treat_runtimeparams(
params["passed_data"]["soc_init"] = None
params["passed_data"]["soc_final"] = None

# Treat passed forecast data lists
list_forecast_key = [
"pv_power_forecast",
"load_power_forecast",
"load_cost_forecast",
"prod_price_forecast",
"outdoor_temperature_forecast",
]
forecast_methods = [
"weather_forecast_method",
"load_forecast_method",
"load_cost_forecast_method",
"production_price_forecast_method",
"outdoor_temperature_forecast_method",
]

# Loop forecasts, check if value is a list and greater than or equal to forecast_dates
for method, forecast_key in enumerate(list_forecast_key):
if forecast_key in runtimeparams.keys():
if isinstance(runtimeparams[forecast_key], list) and len(
runtimeparams[forecast_key]
) >= len(forecast_dates):
params["passed_data"][forecast_key] = runtimeparams[forecast_key]
params["optim_conf"][forecast_methods[method]] = "list"
else:
logger.error(
f"ERROR: The passed data is either not a list or the length is not correct, length should be {str(len(forecast_dates))}"
)
logger.error(
f"Passed type is {str(type(runtimeparams[forecast_key]))} and length is {str(len(runtimeparams[forecast_key]))}"
)
# Check if string contains list, if so extract
if isinstance(runtimeparams[forecast_key], str):
if isinstance(ast.literal_eval(runtimeparams[forecast_key]), list):
runtimeparams[forecast_key] = ast.literal_eval(
runtimeparams[forecast_key]
)
list_non_digits = [
x
for x in runtimeparams[forecast_key]
if not (isinstance(x, int) or isinstance(x, float))
]
if len(list_non_digits) > 0:
logger.warning(
f"There are non numeric values on the passed data for {forecast_key}, check for missing values (nans, null, etc)"
)
for x in list_non_digits:
logger.warning(
f"This value in {forecast_key} was detected as non digits: {str(x)}"
)
else:
params["passed_data"][forecast_key] = None

# Treat passed data for forecast model fit/predict/tune at runtime
if (
params["passed_data"].get("historic_days_to_retrieve", None) is not None
Expand Down
11 changes: 2 additions & 9 deletions src/emhass/web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import yaml
from flask import Flask, make_response, request
from flask import logging as log
from jinja2 import Environment, PackageLoader
from waitress import serve

Expand Down Expand Up @@ -384,7 +385,6 @@ def action_call(action_name):

ActionStr = " >> Setting input data dict"
app.logger.info(ActionStr)
app.logger.warning(costfun)
input_data_dict = set_input_data_dict(
emhass_conf, costfun, params, runtimeparams, action_name, app.logger
)
Expand Down Expand Up @@ -652,37 +652,30 @@ def action_call(action_name):
raise Exception("missing: " + str(emhass_conf["data_path"]))

# Define loggers
ch = logging.StreamHandler()
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
ch.setFormatter(formatter)
log.default_handler.setFormatter(formatter)
# Action file logger
fileLogger = logging.FileHandler(str(emhass_conf["data_path"] / "actionLogs.txt"))
formatter = logging.Formatter("%(levelname)s - %(name)s - %(message)s")
fileLogger.setFormatter(formatter) # add format to Handler
if logging_level == "DEBUG":
app.logger.setLevel(logging.DEBUG)
ch.setLevel(logging.DEBUG)
fileLogger.setLevel(logging.DEBUG)
elif logging_level == "INFO":
app.logger.setLevel(logging.INFO)
ch.setLevel(logging.INFO)
fileLogger.setLevel(logging.INFO)
elif logging_level == "WARNING":
app.logger.setLevel(logging.WARNING)
ch.setLevel(logging.WARNING)
fileLogger.setLevel(logging.WARNING)
elif logging_level == "ERROR":
app.logger.setLevel(logging.ERROR)
ch.setLevel(logging.ERROR)
fileLogger.setLevel(logging.ERROR)
else:
app.logger.setLevel(logging.DEBUG)
ch.setLevel(logging.DEBUG)
fileLogger.setLevel(logging.DEBUG)
app.logger.propagate = False
app.logger.addHandler(ch)
app.logger.addHandler(fileLogger)
# Clear Action File logger file, ready for new instance
clearFileLog()
Expand Down
Loading