Skip to content

Commit

Permalink
refactor: remove dependency on ScenarioInfo object in plot_sim_vs_his…
Browse files Browse the repository at this point in the history
…t module
  • Loading branch information
rouille committed Apr 29, 2022
1 parent f9aa1f8 commit aee7f25
Showing 1 changed file with 51 additions and 23 deletions.
74 changes: 51 additions & 23 deletions postreise/plot/plot_sim_vs_hist.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import warnings

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from postreise.analyze.generation.summarize import sum_generation_by_state


class PlotSettings:
Expand All @@ -10,41 +11,66 @@ class PlotSettings:
size_inches = (20, 10)


def plot_generation_sim_vs_hist(sim_gen, hist_gen, s_info, state, showmax=True):
"""
Plot simulated vs historical generation of each resource in the scenario
def plot_generation_sim_vs_hist(
scenario,
hist_gen,
state,
show_max=True,
plot_show=True,
):
"""Plot simulated vs historical generation of each resource in the scenario
for the given state. Optionally include max generation.
:param pandas.DataFrame sim_gen: simulated generation dataframe
:param pandas.DataFrame hist_gen: historical generation dataframe
:param powersimdata.design.ScenarioInfo s_info: scenario info instance.
:param powersimdata.scenario.scenario.Scenario scenario: scenario instance.
:param pandas.DataFrame hist_gen: historical generation data frame. Columns are
resources and indices are state names.
:param str state: the US state
:param bool showmax: determine whether to additionally plot max generation of each resource
:param bool show_max: determine whether to additionally plot max generation of each
resource.
:param plot_show: show the plot or not, defaults to True.
:return: (*matplotlib.axes.Axes*) -- axes object of the plot.
:raises TypeError:
if ``hist_gen`` is not a data frame.
if ``state`` is not a str.
:raises ValueError: if ``state is not in ``hist_gen`` or ``scenario``.
"""
labels = list(sim_gen.columns)
all_resources = s_info.get_available_resource("all")
if not isinstance(hist_gen, pd.DataFrame):
raise TypeError("hist_gen must be a pandas.DataFrame")
if not isinstance(state, str):
raise TypeError("state must be a str")

sim_gen = sum_generation_by_state(scenario)
if state not in sim_gen.index or state not in hist_gen.index:
raise ValueError("Invalid state")

all_resources = list(sim_gen.columns)

sim = [int(round(sim_gen.loc[state, res])) for res in all_resources]
hist = [int(round(hist_gen.loc[state, res])) for res in all_resources]

def calc_max(gen_type):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
return int(
s_info.get_capacity(gen_type, state) * len(s_info.pg.index) / 1000
)
def calc_max(fuel):
grid = scenario.get_grid()
loadzone = list(grid.model_immutables.area_to_loadzone(state, "state")) # noqa
capacity = grid.plant.query("type == @fuel & zone_name == @loadzone")[
"Pmax"
].sum()
if capacity == 0:
print(f"No {fuel} generator in {state}")

pg = scenario.get_pg()
return int(capacity * pg.shape[0] / 1000)

max_gen = []
if showmax:
if show_max:
max_gen = [calc_max(res) for res in all_resources]

x = np.arange(len(labels)) # the label locations
x = np.arange(len(all_resources)) # the label locations
width = PlotSettings.width
fontsize = PlotSettings.fontsize
size_inches_x, size_inches_y = PlotSettings.size_inches

fig, ax = plt.subplots()
if showmax:
if show_max:
rects1 = ax.bar(x - width, hist, width, label="Historical")
rects2 = ax.bar(x, sim, width, label="Simulated")
rects3 = ax.bar(x + width, max_gen, width, label="Max Generation")
Expand All @@ -56,7 +82,7 @@ def calc_max(gen_type):
ax.set_ylabel("GWh", fontsize=fontsize)
ax.set_title(state, fontsize=fontsize)
ax.set_xticks(x)
ax.set_xticklabels(labels, fontsize=fontsize)
ax.set_xticklabels(all_resources, fontsize=fontsize)
ax.legend(fontsize=fontsize)

def autolabel(rects):
Expand All @@ -74,8 +100,10 @@ def autolabel(rects):

autolabel(rects1)
autolabel(rects2)
if showmax:
if show_max:
autolabel(rects3)
fig.tight_layout()
fig.set_size_inches(size_inches_x, size_inches_y)
plt.show()
if plot_show:
plt.show()
return ax

0 comments on commit aee7f25

Please sign in to comment.