From 8f2973c7358271a88ba6472660b6f8880af50021 Mon Sep 17 00:00:00 2001 From: Ben RdO Date: Mon, 12 Sep 2022 12:57:21 -0700 Subject: [PATCH 1/4] refactor: check that scenarios are in analyze state --- postreise/plot/plot_carbon_bar.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/postreise/plot/plot_carbon_bar.py b/postreise/plot/plot_carbon_bar.py index b5b62232..0ac69400 100644 --- a/postreise/plot/plot_carbon_bar.py +++ b/postreise/plot/plot_carbon_bar.py @@ -1,5 +1,6 @@ import numpy as np from matplotlib import pyplot as plt +from powersimdata.scenario.check import _check_scenario_is_in_analyze_state from powersimdata.scenario.scenario import Scenario from postreise.analyze.generation.emissions import ( @@ -36,6 +37,7 @@ def plot_carbon_bar(*args, labels=None, labels_size=15, show_plot=True): carbon_val = {"coal": [], "ng": []} for i, s in enumerate(args): + _check_scenario_is_in_analyze_state(s) grid = s.get_grid() carbon_by_bus = summarize_emissions_by_bus(generate_emissions_stats(s), grid) carbon_val["coal"].append(sum(carbon_by_bus["coal"].values())) From b369e779f4bc711838f428e609531852d8413df5 Mon Sep 17 00:00:00 2001 From: Ben RdO Date: Mon, 12 Sep 2022 13:03:11 -0700 Subject: [PATCH 2/4] fix: use plot_show as parameter name across entire function --- postreise/plot/plot_carbon_bar.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/postreise/plot/plot_carbon_bar.py b/postreise/plot/plot_carbon_bar.py index 0ac69400..dedf33d6 100644 --- a/postreise/plot/plot_carbon_bar.py +++ b/postreise/plot/plot_carbon_bar.py @@ -9,13 +9,13 @@ ) -def plot_carbon_bar(*args, labels=None, labels_size=15, show_plot=True): +def plot_carbon_bar(*args, labels=None, labels_size=15, plot_show=True): """Make bar chart of carbon emissions by fuel type for n scenarios. :param powersimdata.scenario.scenario.Scenario args: scenario instances. :param list/tuple/set labels: labels on plot. Default to scenario id. :param int labels_size: size of labels. - :param bool show_plot: whether to save the plot. + :param bool plot_show: whether to save the plot. :raises ValueError: if ``args`` are not scenario instances. if ``labels`` has a different length than the number of passed scenarios. @@ -59,6 +59,6 @@ def plot_carbon_bar(*args, labels=None, labels_size=15, show_plot=True): plt.yticks(y_pos, labels) plt.subplots_adjust(wspace=0.1) - if show_plot: + if plot_show: plt.show() return ax1, ax2 From 55d71f72f8ba6e8951f020f14ae894352469584b Mon Sep 17 00:00:00 2001 From: Ben RdO Date: Mon, 12 Sep 2022 13:34:18 -0700 Subject: [PATCH 3/4] docs: improve docstring and error message --- postreise/plot/plot_carbon_bar.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/postreise/plot/plot_carbon_bar.py b/postreise/plot/plot_carbon_bar.py index dedf33d6..c0d45fba 100644 --- a/postreise/plot/plot_carbon_bar.py +++ b/postreise/plot/plot_carbon_bar.py @@ -15,7 +15,7 @@ def plot_carbon_bar(*args, labels=None, labels_size=15, plot_show=True): :param powersimdata.scenario.scenario.Scenario args: scenario instances. :param list/tuple/set labels: labels on plot. Default to scenario id. :param int labels_size: size of labels. - :param bool plot_show: whether to save the plot. + :param bool plot_show: whether to show the plot. :raises ValueError: if ``args`` are not scenario instances. if ``labels`` has a different length than the number of passed scenarios. @@ -31,7 +31,7 @@ def plot_carbon_bar(*args, labels=None, labels_size=15, plot_show=True): if labels is not None and len(args) != len(labels): raise ValueError("labels must have same length as number of scenarios") if not isinstance(labels_size, int): - raise TypeError("labels_size must be an integer") + raise TypeError("labels_size must be an int") labels = tuple([s.info["id"] for s in args]) if labels is None else tuple(labels) From d5ab28f6006d7add0c4fc97fe8d9722a01a167d8 Mon Sep 17 00:00:00 2001 From: Ben RdO Date: Mon, 12 Sep 2022 13:34:53 -0700 Subject: [PATCH 4/4] test: write unit test for plot_carbon_bar --- postreise/plot/tests/test_plot_carbon_bar.py | 90 ++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 postreise/plot/tests/test_plot_carbon_bar.py diff --git a/postreise/plot/tests/test_plot_carbon_bar.py b/postreise/plot/tests/test_plot_carbon_bar.py new file mode 100644 index 00000000..01b58058 --- /dev/null +++ b/postreise/plot/tests/test_plot_carbon_bar.py @@ -0,0 +1,90 @@ +import pandas as pd +import pytest +from powersimdata.tests.mock_scenario import MockScenario + +from postreise.plot.plot_carbon_bar import plot_carbon_bar + +mock_plant_s1 = { + "plant_id": ["A", "B", "C", "D", "E", "F", "G", "H"], + "bus_id": [1, 2, 3, 4, 5, 6, 7, 9], + "type": ["coal", "wind", "coal", "coal", "ng", "coal", "ng", "ng"], +} + +mock_pg_s1 = pd.DataFrame( + { + "A": [80, 75, 72, 81], + "B": [22, 22, 25, 20], + "C": [130, 130, 130, 130], + "D": [25, 26, 27, 28], + "E": [10, 11, 9, 12], + "F": [290, 295, 295, 294], + "G": [190, 190, 191, 190], + "H": [61, 63, 65, 67], + }, + index=pd.date_range(start="2016-01-01", periods=4, freq="H"), +) + +s1 = MockScenario({"plant": mock_plant_s1}, pg=mock_pg_s1) +s1.info["name"] = "Carbon Scenario 1" + + +mock_plant_s2 = { + "plant_id": ["a", "b", "c", "d", "e", "f", "g", "h"], + "bus_id": [1, 2, 3, 4, 5, 6, 7, 9], + "type": ["ng", "ng", "ng", "coal", "coal", "hydro", "ng", "solar"], +} + +mock_pg_s2 = pd.DataFrame( + { + "a": [180, 175, 172, 181], + "b": [122, 122, 125, 120], + "c": [330, 330, 330, 330], + "d": [225, 226, 227, 228], + "e": [110, 111, 119, 112], + "f": [90, 95, 95, 94], + "g": [90, 90, 91, 90], + "h": [61, 63, 65, 67], + }, + index=pd.date_range(start="2016-01-01", periods=4, freq="H"), +) + +s2 = MockScenario({"plant": mock_plant_s2}, pg=mock_pg_s2) +s2.info["name"] = "Carbon Scenario 2" + + +def test_plot_carbon_bar(): + plot_carbon_bar( + s1, + s2, + labels_size=10, + plot_show=False, + ) + plot_carbon_bar( + s1, + s2, + labels=[s1.info["name"], s2.info["name"]], + plot_show=False, + ) + + +def test_plot_carbon_bar_argument_type(): + with pytest.raises(TypeError) as excinfo: + plot_carbon_bar( + s1, + labels={"first": s1.info["name"]}, + ) + assert "labels must be a list/tuple/set" in str(excinfo.value) + + with pytest.raises(TypeError) as excinfo: + plot_carbon_bar(s1, labels_size=12.5) + assert "labels_size must be an int" in str(excinfo.value) + + +def test_plot_carbon_bar_argument_value(): + with pytest.raises(ValueError) as excinfo: + plot_carbon_bar(1, "2", s1) + assert "all inputs must be Scenario objects" in str(excinfo.value) + + with pytest.raises(ValueError) as excinfo: + plot_carbon_bar(s2, labels=["scenario 1", "scenario 2"]) + assert "labels must have same length as number of scenarios" in str(excinfo.value)