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

Fix - Fixed bug from forecast out method related to issue 240 #250

Merged
merged 2 commits into from
Apr 4, 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
22 changes: 12 additions & 10 deletions src/emhass/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ def set_input_data_dict(config_path: pathlib.Path, base_path: str, costfun: str,
days_list = utils.get_days_list(retrieve_hass_conf['days_to_retrieve'])
var_list = [retrieve_hass_conf['var_load'], retrieve_hass_conf['var_PV']]
if not rh.get_data(days_list, var_list,
minimal_response=False, significant_changes_only=False):
minimal_response=False, significant_changes_only=False):
return False
if not rh.prepare_data(retrieve_hass_conf['var_load'], load_negative = retrieve_hass_conf['load_negative'],
set_zero_min = retrieve_hass_conf['set_zero_min'],
var_replace_zero = retrieve_hass_conf['var_replace_zero'],
var_interp = retrieve_hass_conf['var_interp']):
set_zero_min = retrieve_hass_conf['set_zero_min'],
var_replace_zero = retrieve_hass_conf['var_replace_zero'],
var_interp = retrieve_hass_conf['var_interp']):
return False
df_input_data = rh.df_final.copy()
# What we don't need for this type of action
Expand Down Expand Up @@ -113,12 +113,12 @@ def set_input_data_dict(config_path: pathlib.Path, base_path: str, costfun: str,
days_list = utils.get_days_list(1)
var_list = [retrieve_hass_conf['var_load'], retrieve_hass_conf['var_PV']]
if not rh.get_data(days_list, var_list,
minimal_response=False, significant_changes_only=False):
minimal_response=False, significant_changes_only=False):
return False
if not rh.prepare_data(retrieve_hass_conf['var_load'], load_negative = retrieve_hass_conf['load_negative'],
set_zero_min = retrieve_hass_conf['set_zero_min'],
var_replace_zero = retrieve_hass_conf['var_replace_zero'],
var_interp = retrieve_hass_conf['var_interp']):
set_zero_min = retrieve_hass_conf['set_zero_min'],
var_replace_zero = retrieve_hass_conf['var_replace_zero'],
var_interp = retrieve_hass_conf['var_interp']):
return False
df_input_data = rh.df_final.copy()
# Get PV and load forecasts
Expand Down Expand Up @@ -201,9 +201,11 @@ def perfect_forecast_optim(input_data_dict: dict, logger: logging.Logger,
# Load cost and prod price forecast
df_input_data = input_data_dict['fcst'].get_load_cost_forecast(
input_data_dict['df_input_data'],
method=input_data_dict['fcst'].optim_conf['load_cost_forecast_method'])
method=input_data_dict['fcst'].optim_conf['load_cost_forecast_method'],
list_and_perfect=True)
df_input_data = input_data_dict['fcst'].get_prod_price_forecast(
df_input_data, method=input_data_dict['fcst'].optim_conf['prod_price_forecast_method'])
df_input_data, method=input_data_dict['fcst'].optim_conf['prod_price_forecast_method'],
list_and_perfect=True)
opt_res = input_data_dict['opt'].perform_perfect_forecast_optim(df_input_data, input_data_dict['days_list'])
# Save CSV file for analysis
if save_data_to_file:
Expand Down
86 changes: 61 additions & 25 deletions src/emhass/forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from emhass.retrieve_hass import RetrieveHass
from emhass.machine_learning_forecaster import MLForecaster
from emhass.utils import get_days_list, get_root
from emhass.utils import get_days_list, get_root, set_df_index_freq


class Forecast(object):
Expand Down Expand Up @@ -487,8 +487,9 @@ def get_forecast_days_csv(self, timedelta_days: Optional[int] = 1) -> pd.date_ra
forecast_dates_csv = forecast_dates_csv[0:self.params['passed_data']['prediction_horizon']]
return forecast_dates_csv

def get_forecast_out_from_csv(self, df_final: pd.DataFrame, forecast_dates_csv: pd.date_range,
csv_path: str, data_list: Optional[list] = None) -> pd.DataFrame:
def get_forecast_out_from_csv_or_list(self, df_final: pd.DataFrame, forecast_dates_csv: pd.date_range,
csv_path: str, data_list: Optional[list] = None,
list_and_perfect: Optional[bool] = False) -> pd.DataFrame:
r"""
Get the forecast data as a DataFrame from a CSV file.

Expand All @@ -506,34 +507,67 @@ def get_forecast_out_from_csv(self, df_final: pd.DataFrame, forecast_dates_csv:
:rtype: pd.DataFrame

"""
days_list = df_final.index.day.unique().tolist()
if csv_path is None:
data_dict = {'ts':forecast_dates_csv, 'yhat':data_list}
df_csv = pd.DataFrame.from_dict(data_dict)
df_csv.index = forecast_dates_csv
df_csv.drop(['ts'], axis=1, inplace=True)
df_csv = set_df_index_freq(df_csv)
if list_and_perfect:
days_list = df_final.index.day.unique().tolist()
else:
days_list = df_csv.index.day.unique().tolist()
else:
load_csv_file_path = self.root + csv_path
df_csv = pd.read_csv(load_csv_file_path, header=None, names=['ts', 'yhat'])
df_csv.index = forecast_dates_csv
df_csv.drop(['ts'], axis=1, inplace=True)
df_csv = set_df_index_freq(df_csv)
days_list = df_final.index.day.unique().tolist()
forecast_out = pd.DataFrame()
for day in days_list:
first_elm_index = [i for i, x in enumerate(df_final.index.day == day) if x][0]
last_elm_index = [i for i, x in enumerate(df_final.index.day == day) if x][-1]
fcst_index = pd.date_range(start=df_final.index[first_elm_index],
end=df_final.index[last_elm_index],
freq=df_final.index.freq)
first_hour = str(df_final.index[first_elm_index].hour)+":"+str(df_final.index[first_elm_index].minute)
last_hour = str(df_final.index[last_elm_index].hour)+":"+str(df_final.index[last_elm_index].minute)
if csv_path is None:
if list_and_perfect:
df_tmp = copy.deepcopy(df_final)
else:
df_tmp = copy.deepcopy(df_csv)
else:
df_tmp = copy.deepcopy(df_final)
first_elm_index = [i for i, x in enumerate(df_tmp.index.day == day) if x][0]
last_elm_index = [i for i, x in enumerate(df_tmp.index.day == day) if x][-1]
fcst_index = pd.date_range(start=df_tmp.index[first_elm_index],
end=df_tmp.index[last_elm_index],
freq=df_tmp.index.freq)
first_hour = str(df_tmp.index[first_elm_index].hour)+":"+str(df_tmp.index[first_elm_index].minute)
last_hour = str(df_tmp.index[last_elm_index].hour)+":"+str(df_tmp.index[last_elm_index].minute)
if len(forecast_out) == 0:
forecast_out = pd.DataFrame(
df_csv.between_time(first_hour, last_hour).values,
index=fcst_index)
if csv_path is None:
if list_and_perfect:
forecast_out = pd.DataFrame(
df_csv.between_time(first_hour, last_hour).values,
index=fcst_index)
else:
forecast_out = pd.DataFrame(
df_csv.loc[fcst_index,:].between_time(first_hour, last_hour).values,
index=fcst_index)
else:
forecast_out = pd.DataFrame(
df_csv.between_time(first_hour, last_hour).values,
index=fcst_index)
else:
forecast_tp = pd.DataFrame(
df_csv.between_time(first_hour, last_hour).values,
index=fcst_index)
if csv_path is None:
if list_and_perfect:
forecast_tp = pd.DataFrame(
df_csv.between_time(first_hour, last_hour).values,
index=fcst_index)
else:
forecast_tp = pd.DataFrame(
df_csv.loc[fcst_index,:].between_time(first_hour, last_hour).values,
index=fcst_index)
else:
forecast_tp = pd.DataFrame(
df_csv.between_time(first_hour, last_hour).values,
index=fcst_index)
forecast_out = pd.concat([forecast_out, forecast_tp], axis=0)
return forecast_out

Expand Down Expand Up @@ -668,7 +702,8 @@ def get_load_forecast(self, days_min_load_forecast: Optional[int] = 3, method: O
return P_Load_forecast

def get_load_cost_forecast(self, df_final: pd.DataFrame, method: Optional[str] = 'hp_hc_periods',
csv_path: Optional[str] = "data_load_cost_forecast.csv") -> pd.DataFrame:
csv_path: Optional[str] = "data_load_cost_forecast.csv",
list_and_perfect: Optional[bool] = False) -> pd.DataFrame:
r"""
Get the unit cost for the load consumption based on multiple tariff \
periods. This is the cost of the energy from the utility in a vector \
Expand Down Expand Up @@ -698,7 +733,7 @@ def get_load_cost_forecast(self, df_final: pd.DataFrame, method: Optional[str] =
df_final.loc[df_hp.index, self.var_load_cost] = self.optim_conf['load_cost_hp']
elif method == 'csv':
forecast_dates_csv = self.get_forecast_days_csv(timedelta_days=0)
forecast_out = self.get_forecast_out_from_csv(
forecast_out = self.get_forecast_out_from_csv_or_list(
df_final, forecast_dates_csv, csv_path)
df_final[self.var_load_cost] = forecast_out
elif method == 'list': # reading a list of values
Expand All @@ -712,8 +747,8 @@ def get_load_cost_forecast(self, df_final: pd.DataFrame, method: Optional[str] =
data_list = data_list[0:len(self.forecast_dates)]
# Define the correct dates
forecast_dates_csv = self.get_forecast_days_csv(timedelta_days=0)
forecast_out = self.get_forecast_out_from_csv(
df_final, forecast_dates_csv, None, data_list=data_list)
forecast_out = self.get_forecast_out_from_csv_or_list(
df_final, forecast_dates_csv, None, data_list=data_list, list_and_perfect=list_and_perfect)
# Fill the final DF
df_final[self.var_load_cost] = forecast_out
else:
Expand All @@ -722,7 +757,8 @@ def get_load_cost_forecast(self, df_final: pd.DataFrame, method: Optional[str] =
return df_final

def get_prod_price_forecast(self, df_final: pd.DataFrame, method: Optional[str] = 'constant',
csv_path: Optional[str] = "/data/data_prod_price_forecast.csv") -> pd.DataFrame:
csv_path: Optional[str] = "/data/data_prod_price_forecast.csv",
list_and_perfect: Optional[bool] = False) -> pd.DataFrame:
r"""
Get the unit power production price for the energy injected to the grid.\
This is the price of the energy injected to the utility in a vector \
Expand All @@ -747,7 +783,7 @@ def get_prod_price_forecast(self, df_final: pd.DataFrame, method: Optional[str]
df_final[self.var_prod_price] = self.optim_conf['prod_sell_price']
elif method == 'csv':
forecast_dates_csv = self.get_forecast_days_csv(timedelta_days=0)
forecast_out = self.get_forecast_out_from_csv(df_final,
forecast_out = self.get_forecast_out_from_csv_or_list(df_final,
forecast_dates_csv,
csv_path)
df_final[self.var_prod_price] = forecast_out
Expand All @@ -762,8 +798,8 @@ def get_prod_price_forecast(self, df_final: pd.DataFrame, method: Optional[str]
data_list = data_list[0:len(self.forecast_dates)]
# Define the correct dates
forecast_dates_csv = self.get_forecast_days_csv(timedelta_days=0)
forecast_out = self.get_forecast_out_from_csv(
df_final, forecast_dates_csv, None, data_list=data_list)
forecast_out = self.get_forecast_out_from_csv_or_list(
df_final, forecast_dates_csv, None, data_list=data_list, list_and_perfect=list_and_perfect)
# Fill the final DF
df_final[self.var_prod_price] = forecast_out
else:
Expand Down
Loading
Loading