Skip to content

Commit

Permalink
Merge branch 'davidusb-geek/fix/sonar_fixes1' of github.com:davidusb-…
Browse files Browse the repository at this point in the history
…geek/emhass into testing
  • Loading branch information
GeoDerp committed Feb 8, 2025
2 parents 3224014 + 206f614 commit fe0da10
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 131 deletions.
1 change: 0 additions & 1 deletion docs/_static/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

div {
border-radius: 7px;
box-align: start !important;
align-items: start;
text-align: left;
}
Expand Down
87 changes: 1 addition & 86 deletions scripts/load_clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
pio.renderers.default = "browser"
pd.options.plotting.backend = "plotly"

# from skopt.space import Categorical, Real, Integer
# from tslearn.clustering import TimeSeriesKMeans

# the root folder
root = pathlib.Path(str(get_root(__file__, num_parent=2)))
emhass_conf = {}
Expand Down Expand Up @@ -191,86 +188,4 @@ def load_forecast(data, forecast_date, freq, template):

# Call the forecasting method
data.columns = ["load"]
forecast, used_days = load_forecast(data, forecast_date, freq, template)

# data_lag = pd.concat([data, data.shift()], axis=1)
# data_lag.columns = ["power_load y(t)", "power_load y(t+1)"]
# data_lag = data_lag.dropna()

# fig2 = data_lag.plot.scatter(
# x="power_load y(t)", y="power_load y(t+1)", c="DarkBlue"
# )
# fig2.layout.template = template
# fig2.show()

# Elbow method to check how many clusters
# distortions = []
# K = range(1,12)

# for cluster_size in K:
# kmeans = KMeans(n_clusters=cluster_size, init='k-means++')
# kmeans = kmeans.fit(data_lag)
# distortions.append(kmeans.inertia_)

# df = pd.DataFrame({'Clusters': K, 'Distortions': distortions})
# fig = (px.line(df, x='Clusters', y='Distortions', template=template)).update_traces(mode='lines+markers')
# fig.show()

# The silouhette method
# silhouette_scores = []
# K = range(2, 12)

# for cluster_size in K:
# kmeans = KMeans(n_clusters=cluster_size, init="k-means++", random_state=200)
# labels = kmeans.fit(data_lag).labels_
# silhouette_score_tmp = silhouette_score(
# data_lag, labels, metric="euclidean", sample_size=1000, random_state=200
# )
# silhouette_scores.append(silhouette_score_tmp)

# df = pd.DataFrame({"Clusters": K, "Silhouette Score": silhouette_scores})
# fig = (
# px.line(df, x="Clusters", y="Silhouette Score", template=template)
# ).update_traces(mode="lines+markers")
# fig.show()

# The clustering
# kmeans = KMeans(n_clusters=6, init="k-means++")
# kmeans = kmeans.fit(data_lag)
# data_lag["cluster_group"] = kmeans.labels_

# fig = px.scatter(
# data_lag,
# x="power_load y(t)",
# y="power_load y(t+1)",
# color="cluster_group",
# template=template,
# )
# fig.show()

# km = TimeSeriesKMeans(n_clusters=6, verbose=True, random_state=200)
# y_pred = km.fit_predict(data_lag)
# data_lag["cluster_group_tslearn_euclidean"] = y_pred

# fig = px.scatter(
# data_lag,
# x="power_load y(t)",
# y="power_load y(t+1)",
# color="cluster_group_tslearn_euclidean",
# template=template,
# )
# fig.show()

# dba_km = TimeSeriesKMeans(n_clusters=6, n_init=2, metric="dtw", verbose=True, max_iter_barycenter=10, random_state=200)
# y_pred = dba_km.fit_predict(data_lag)
# data_lag['cluster_group_tslearn_dba'] = y_pred

# fig = px.scatter(data_lag, x='power_load y(t)', y='power_load y(t+1)', color='cluster_group_tslearn_dba', template=template)
# fig.show()

# sdtw_km = TimeSeriesKMeans(n_clusters=6, metric="softdtw", metric_params={"gamma": .01}, verbose=True, random_state=200)
# y_pred = sdtw_km.fit_predict(data_lag)
# data_lag['cluster_group_tslearn_sdtw'] = y_pred

# fig = px.scatter(data_lag, x='power_load y(t)', y='power_load y(t+1)', color='cluster_group_tslearn_sdtw', template=template)
# fig.show()
forecast, used_days = load_forecast(data, forecast_date, freq, template)
13 changes: 7 additions & 6 deletions scripts/load_forecast_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ def neg_r2_score(y_true, y_pred):

data = copy.deepcopy(rh.df_final)

y_axis_title = "Power (W)"
logger.info(data.describe())
fig = data.plot()
fig.layout.template = template
fig.update_yaxes(title_text="Power (W)")
fig.update_yaxes(title_text=y_axis_title)
fig.update_xaxes(title_text="Time")
fig.show()
fig.write_image(
Expand Down Expand Up @@ -169,7 +170,7 @@ def neg_r2_score(y_true, y_pred):
df["pred"] = predictions
fig = df.plot()
fig.layout.template = template
fig.update_yaxes(title_text="Power (W)")
fig.update_yaxes(title_text=y_axis_title)
fig.update_xaxes(title_text="Time")
fig.update_xaxes(range=[date_train + pd.Timedelta("10days"), data_exo.index[-1]])
fig.show()
Expand Down Expand Up @@ -200,7 +201,7 @@ def neg_r2_score(y_true, y_pred):
df["pred"] = predictions_backtest
fig = df.plot()
fig.layout.template = template
fig.update_yaxes(title_text="Power (W)")
fig.update_yaxes(title_text=y_axis_title)
fig.update_xaxes(title_text="Time")
fig.show()
fig.write_image(
Expand Down Expand Up @@ -273,7 +274,7 @@ def search_space(trial):
df["pred_optim"] = predictions_loaded
fig = df.plot()
fig.layout.template = template
fig.update_yaxes(title_text="Power (W)")
fig.update_yaxes(title_text=y_axis_title)
fig.update_xaxes(title_text="Time")
fig.update_xaxes(range=[date_train + pd.Timedelta("10days"), data_exo.index[-1]])
fig.show()
Expand Down Expand Up @@ -326,7 +327,7 @@ def search_space(trial):

# Let's perform a naive load forecast for comparison
retrieve_hass_conf, optim_conf, plant_conf = get_yaml_parse(
emhass_conf, use_secrets=True
params, logger
)
fcst = Forecast(
retrieve_hass_conf, optim_conf, plant_conf, params, emhass_conf, logger
Expand Down Expand Up @@ -365,7 +366,7 @@ def search_space(trial):
df["pred_prod"] = predictions_prod
fig = df.plot()
fig.layout.template = template
fig.update_yaxes(title_text="Power (W)")
fig.update_yaxes(title_text=y_axis_title)
fig.update_xaxes(title_text="Time")
fig.show()
fig.write_image(
Expand Down
7 changes: 4 additions & 3 deletions scripts/optim_results_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,15 @@ def get_forecast_optim_objects(
template = "presentation"

# Let's plot the input data
y_axis_title = "Power (W)"
fig_inputs1 = df_input_data[
[
retrieve_hass_conf["sensor_power_photovoltaics"],
str(retrieve_hass_conf["sensor_power_load_no_var_loads"] + "_positive"),
]
].plot()
fig_inputs1.layout.template = template
fig_inputs1.update_yaxes(title_text="Powers (W)")
fig_inputs1.update_yaxes(title_text=y_axis_title)
fig_inputs1.update_xaxes(title_text="Time")
if show_figures:
fig_inputs1.show()
Expand All @@ -155,7 +156,7 @@ def get_forecast_optim_objects(

fig_inputs_dah = df_input_data_dayahead.plot()
fig_inputs_dah.layout.template = template
fig_inputs_dah.update_yaxes(title_text="Powers (W)")
fig_inputs_dah.update_yaxes(title_text=y_axis_title)
fig_inputs_dah.update_xaxes(title_text="Time")
if show_figures:
fig_inputs_dah.show()
Expand Down Expand Up @@ -192,7 +193,7 @@ def get_forecast_optim_objects(
]
].plot() # 'P_def_start_0', 'P_def_start_1', 'P_def_bin2_0', 'P_def_bin2_1'
fig_res_dah.layout.template = template
fig_res_dah.update_yaxes(title_text="Powers (W)")
fig_res_dah.update_yaxes(title_text=y_axis_title)
fig_res_dah.update_xaxes(title_text="Time")
# if show_figures:
fig_res_dah.show()
Expand Down
13 changes: 7 additions & 6 deletions scripts/save_pvlib_module_inverter_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@
cec_inverters_emhass = pvlib.pvsystem.retrieve_sam(
path=str(emhass_conf["data_path"] / "emhass_inverters.csv")
)
logger.info("=================")
logger.info("=================")
strait_str = "================="
logger.info(strait_str)
logger.info(strait_str)

logger.info("Updating and saving databases")

Expand Down Expand Up @@ -91,8 +92,8 @@
tablefmt="psql",
)
)
logger.info("=================")
logger.info("=================")
logger.info(strait_str)
logger.info(strait_str)

# Inverters
cols_to_keep_inverters = [
Expand Down Expand Up @@ -124,8 +125,8 @@
tablefmt="psql",
)
)
logger.info("=================")
logger.info("=================")
logger.info(strait_str)
logger.info(strait_str)
logger.info("Modules databases")
print(tabulate(cec_modules.head(20).iloc[:, :5], headers="keys", tablefmt="psql"))
logger.info("Inverters databases")
Expand Down
9 changes: 5 additions & 4 deletions scripts/special_config_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ def get_forecast_optim_objects(
)

template = "presentation"
y_axis_title = "Power (W)"

# Let's plot the input data
fig_inputs1 = df_input_data[
Expand All @@ -291,7 +292,7 @@ def get_forecast_optim_objects(
]
].plot()
fig_inputs1.layout.template = template
fig_inputs1.update_yaxes(title_text="Powers (W)")
fig_inputs1.update_yaxes(title_text=y_axis_title)
fig_inputs1.update_xaxes(title_text="Time")
fig_inputs1.show()

Expand All @@ -303,7 +304,7 @@ def get_forecast_optim_objects(

fig_inputs_dah = df_input_data_dayahead.plot()
fig_inputs_dah.layout.template = template
fig_inputs_dah.update_yaxes(title_text="Powers (W)")
fig_inputs_dah.update_yaxes(title_text=y_axis_title)
fig_inputs_dah.update_xaxes(title_text="Time")
fig_inputs_dah.show()

Expand All @@ -313,7 +314,7 @@ def get_forecast_optim_objects(
opt_res_dah = opt.perform_dayahead_forecast_optim(df_input_data_dayahead, P_PV_forecast, P_load_forecast)
fig_res_dah = opt_res_dah[['P_deferrable0', 'P_deferrable1', 'P_grid']].plot()
fig_res_dah.layout.template = template
fig_res_dah.update_yaxes(title_text = "Powers (W)")
fig_res_dah.update_yaxes(title_text = y_axis_title)
fig_res_dah.update_xaxes(title_text = "Time")
fig_res_dah.show()"""

Expand Down Expand Up @@ -351,6 +352,6 @@ def get_forecast_optim_objects(
)
fig_res_mpc = opt_res_dayahead[["P_batt", "P_grid"]].plot()
fig_res_mpc.layout.template = template
fig_res_mpc.update_yaxes(title_text="Powers (W)")
fig_res_mpc.update_yaxes(title_text=y_axis_title)
fig_res_mpc.update_xaxes(title_text="Time")
fig_res_mpc.show()
13 changes: 7 additions & 6 deletions scripts/use_cases_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def get_forecast_optim_objects(
df_input_data = fcst.get_prod_price_forecast(df_input_data)

template = "presentation"
y_axis_title = "Power (W)"

# Let's plot the input data
fig_inputs1 = df_input_data[
Expand All @@ -128,7 +129,7 @@ def get_forecast_optim_objects(
]
].plot()
fig_inputs1.layout.template = template
fig_inputs1.update_yaxes(title_text="Powers (W)")
fig_inputs1.update_yaxes(title_text=y_axis_title)
fig_inputs1.update_xaxes(title_text="Time")
fig_inputs1.show()
if save_figures:
Expand All @@ -152,7 +153,7 @@ def get_forecast_optim_objects(

fig_inputs_dah = df_input_data_dayahead.plot()
fig_inputs_dah.layout.template = template
fig_inputs_dah.update_yaxes(title_text="Powers (W)")
fig_inputs_dah.update_yaxes(title_text=y_axis_title)
fig_inputs_dah.update_xaxes(title_text="Time")
fig_inputs_dah.show()
if save_figures:
Expand All @@ -166,7 +167,7 @@ def get_forecast_optim_objects(
opt_res = opt.perform_perfect_forecast_optim(df_input_data, days_list)
fig_res = opt_res[["P_deferrable0", "P_deferrable1", "P_grid"]].plot()
fig_res.layout.template = template
fig_res.update_yaxes(title_text="Powers (W)")
fig_res.update_yaxes(title_text=y_axis_title)
fig_res.update_xaxes(title_text="Time")
fig_res.show()
if save_figures:
Expand All @@ -190,7 +191,7 @@ def get_forecast_optim_objects(
)
fig_res_dah = opt_res_dah[["P_deferrable0", "P_deferrable1", "P_grid"]].plot()
fig_res_dah.layout.template = template
fig_res_dah.update_yaxes(title_text="Powers (W)")
fig_res_dah.update_yaxes(title_text=y_axis_title)
fig_res_dah.update_xaxes(title_text="Time")
fig_res_dah.show()
if save_figures:
Expand Down Expand Up @@ -220,7 +221,7 @@ def get_forecast_optim_objects(
)
fig_res_dah = opt_res_dah[["P_deferrable0", "P_deferrable1", "P_grid"]].plot()
fig_res_dah.layout.template = template
fig_res_dah.update_yaxes(title_text="Powers (W)")
fig_res_dah.update_yaxes(title_text=y_axis_title)
fig_res_dah.update_xaxes(title_text="Time")
fig_res_dah.show()
if save_figures:
Expand Down Expand Up @@ -253,7 +254,7 @@ def get_forecast_optim_objects(
["P_deferrable0", "P_deferrable1", "P_grid", "P_batt"]
].plot()
fig_res_dah.layout.template = template
fig_res_dah.update_yaxes(title_text="Powers (W)")
fig_res_dah.update_yaxes(title_text=y_axis_title)
fig_res_dah.update_xaxes(title_text="Time")
fig_res_dah.show()
if save_figures:
Expand Down
Loading

0 comments on commit fe0da10

Please sign in to comment.