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

Write test for plotting function plot_carbon_bar #371

Merged
merged 4 commits into from
Sep 12, 2022
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
10 changes: 6 additions & 4 deletions postreise/plot/plot_carbon_bar.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -8,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 show the plot.
:raises ValueError:
if ``args`` are not scenario instances.
if ``labels`` has a different length than the number of passed scenarios.
Expand All @@ -30,12 +31,13 @@ def plot_carbon_bar(*args, labels=None, labels_size=15, show_plot=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)

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()))
Expand All @@ -57,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
90 changes: 90 additions & 0 deletions postreise/plot/tests/test_plot_carbon_bar.py
Original file line number Diff line number Diff line change
@@ -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)