Skip to content

Commit

Permalink
test: Use fake profile data instead of test_usa_tamu
Browse files Browse the repository at this point in the history
Fixes: #489
  • Loading branch information
chadvoegele committed Feb 15, 2022
1 parent 5b1500e commit 5d306f8
Showing 1 changed file with 92 additions and 60 deletions.
152 changes: 92 additions & 60 deletions powersimdata/input/tests/test_transform_profile.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
from unittest.mock import MagicMock, patch

import numpy as np
import pandas as pd
import pytest
from numpy.testing import assert_almost_equal

from powersimdata.input.change_table import ChangeTable
from powersimdata.input.grid import Grid
from powersimdata.input.input_data import InputData
from powersimdata.input.transform_grid import TransformGrid
from powersimdata.input.transform_profile import TransformProfile

interconnect = ["Western"]
param = {
"demand": "vJan2021",
"hydro": "vJan2021",
"solar": "vJan2021",
"wind": "vJan2021",
"n_zone_to_scale": 6,
"n_plant_to_scale": 50,
"n_plant_to_add": 100,
Expand All @@ -36,6 +34,11 @@ def get_plant_with_resource(base_grid, resource):
return plant_id


def _get_plant_ids_for_type(grid, type):
plant_ids = list(grid.plant.query("type in @profile_type[@type]").index)
return plant_ids


def get_change_table_for_zone_scaling(base_grid, resource):
n_zone = param["n_zone_to_scale"]
zones = get_zone_with_resource(base_grid, resource)
Expand Down Expand Up @@ -88,7 +91,7 @@ def get_change_table_for_new_plant_addition(base_grid, resource):
return ct.ct


def _check_plants_are_scaled(ct, base_grid, profile_info, raw_profile, resource):
def _check_plants_are_scaled(ct, base_grid, raw_profile, resource):
plant_id_type = get_plant_with_resource(base_grid, resource)

base_profile = (
Expand All @@ -98,7 +101,8 @@ def _check_plants_are_scaled(ct, base_grid, profile_info, raw_profile, resource)
tg = TransformGrid(base_grid, ct)
transformed_grid = tg.get_grid()

tp = TransformProfile(profile_info, transformed_grid, ct)
empty_scenario_info = {} # scenario_info not needed since input_data is mocked
tp = TransformProfile(empty_scenario_info, transformed_grid, ct)
transformed_profile = tp.get_profile(resource)

scaled_plant_id = []
Expand Down Expand Up @@ -134,19 +138,18 @@ def _check_plants_are_scaled(ct, base_grid, profile_info, raw_profile, resource)
return transformed_profile


def _check_new_plants_are_added(ct, base_grid, profile_info, raw_profile, resource):
def _check_new_plants_are_added(ct, base_grid, raw_profile, resource):
n_plant = param["n_plant_to_add"]
plant_id_type = (
base_grid.plant.isin(profile_type[resource]).query("type == True").index
)
plant_id_type = _get_plant_ids_for_type(base_grid, resource)
base_profile = (
raw_profile[plant_id_type] * base_grid.plant.loc[plant_id_type, "Pmax"]
)

tg = TransformGrid(base_grid, ct)
transformed_grid = tg.get_grid()

tp = TransformProfile(profile_info, transformed_grid, ct)
empty_scenario_info = {} # scenario_info not needed since input_data is mocked
tp = TransformProfile(empty_scenario_info, transformed_grid, ct)
transformed_profile = tp.get_profile(resource)

assert not transformed_profile.equals(base_profile)
Expand All @@ -159,30 +162,24 @@ def _check_new_plants_are_added(ct, base_grid, profile_info, raw_profile, resour
return transformed_profile.drop(base_profile.columns, axis=1)


def _check_new_plants_are_not_scaled(base_grid, profile_info, raw_profile, resource):
def _check_new_plants_are_not_scaled(base_grid, raw_profile, resource):
ct_zone = get_change_table_for_zone_scaling(base_grid, resource)
ct_id = get_change_table_for_id_scaling(base_grid, resource)
ct_new = get_change_table_for_new_plant_addition(base_grid, resource)
ct = {**ct_zone, **ct_id[resource], **ct_new}
# profile of new plants
new_profile = _check_new_plants_are_added(
ct_new, base_grid, profile_info, raw_profile, resource
)
new_profile = _check_new_plants_are_added(ct_new, base_grid, raw_profile, resource)
# transformed profile
scaled_profile = _check_plants_are_scaled(
ct, base_grid, profile_info, raw_profile, resource
)
scaled_profile = _check_plants_are_scaled(ct, base_grid, raw_profile, resource)
# check that the profiles of new plants in the scaled profile are not scaled
assert new_profile.equals(scaled_profile[new_profile.columns])


def _check_profile_of_new_plants_are_produced_correctly(
base_grid, profile_info, raw_profile, resource
base_grid, raw_profile, resource
):
ct_new = get_change_table_for_new_plant_addition(base_grid, resource)
new_profile = _check_new_plants_are_added(
ct_new, base_grid, profile_info, raw_profile, resource
)
new_profile = _check_new_plants_are_added(ct_new, base_grid, raw_profile, resource)
neighbor_id = [d["plant_id_neighbor"] for d in ct_new["new_plant"]]
new_plant_pmax = [d["Pmax"] for d in ct_new["new_plant"]]

Expand All @@ -191,45 +188,79 @@ def _check_profile_of_new_plants_are_produced_correctly(
assert new_profile[c].equals(neighbor_profile.multiply(new_plant_pmax[i]))


@pytest.fixture(scope="module")
def random():
return np.random.default_rng(seed=6669)


@pytest.fixture(scope="module")
def base_grid():
grid = Grid(interconnect)
return grid


def raw_profile(kind):
input_data = InputData()
grid_model = "test_usa_tamu"
profile_info = {
"grid_model": grid_model,
f"base_{kind}": param[kind],
}
profile = input_data.get_data(profile_info, kind)
return profile_info, profile
def _create_fake_profile(random, columns):
start_time = "2016-01-01 00:00:00"
times = pd.date_range(start_time, periods=24, freq="H")
index = pd.Index(times, name="UTC Time")
data = random.uniform(low=0, high=1, size=(len(times), len(columns)))
fake_profile = pd.DataFrame(data=data, index=index, columns=columns)
return fake_profile


@pytest.fixture(scope="module")
def raw_hydro():
return raw_profile("hydro")
def raw_hydro(base_grid, random):
plant_ids = _get_plant_ids_for_type(base_grid, "hydro")
fake_hydro_profile = _create_fake_profile(random, plant_ids)
return fake_hydro_profile


@pytest.fixture(scope="module")
def raw_wind():
return raw_profile("wind")
def raw_wind(base_grid, random):
plant_ids = _get_plant_ids_for_type(base_grid, "wind")
fake_wind_profile = _create_fake_profile(random, plant_ids)
return fake_wind_profile


@pytest.fixture(scope="module")
def raw_solar():
return raw_profile("solar")
def raw_solar(base_grid, random):
plant_ids = _get_plant_ids_for_type(base_grid, "solar")
fake_solar_profile = _create_fake_profile(random, plant_ids)
return fake_solar_profile


@pytest.fixture(scope="module")
def raw_demand():
return raw_profile("demand")
def raw_demand(base_grid, random):
zone_ids = list(base_grid.id2zone.keys())
fake_demand_profile = _create_fake_profile(random, zone_ids)
return fake_demand_profile


@pytest.fixture(scope="module", autouse=True)
def mock_input_data(raw_demand, raw_hydro, raw_wind, raw_solar):
with patch(
"powersimdata.input.transform_profile.InputData"
) as mock_input_data_class:

def mock_get_data(scenario_info, field_name):
if field_name == "demand":
return raw_demand
elif field_name == "hydro":
return raw_hydro
elif field_name == "wind":
return raw_wind
elif field_name == "solar":
return raw_solar

raise ValueError(f"No return specified for {field_name}!")

mock_input_data_object = MagicMock()
mock_input_data_object.get_data.side_effect = mock_get_data
mock_input_data_class.return_value = mock_input_data_object
yield


def test_demand_is_scaled(base_grid, raw_demand):
demand_info, raw_demand = raw_demand
base_demand = raw_demand[base_grid.id2zone.keys()]

n_zone = param["n_zone_to_scale"]
Expand All @@ -249,7 +280,8 @@ def test_demand_is_scaled(base_grid, raw_demand):
tg = TransformGrid(base_grid, ct.ct)
transformed_grid = tg.get_grid()

tp = TransformProfile(demand_info, transformed_grid, ct.ct)
empty_scenario_info = {} # scenario_info not needed since input_data is mocked
tp = TransformProfile(empty_scenario_info, transformed_grid, ct.ct)
transformed_profile = tp.get_profile("demand")
assert not base_demand.equals(transformed_profile)

Expand All @@ -265,89 +297,89 @@ def test_demand_is_scaled(base_grid, raw_demand):

def test_solar_is_scaled_by_zone(base_grid, raw_solar):
ct = get_change_table_for_zone_scaling(base_grid, "solar")
_check_plants_are_scaled(ct, base_grid, *raw_solar, "solar")
_check_plants_are_scaled(ct, base_grid, raw_solar, "solar")


def test_solar_is_scaled_by_id(base_grid, raw_solar):
ct = get_change_table_for_id_scaling(base_grid, "solar")
_check_plants_are_scaled(ct, base_grid, *raw_solar, "solar")
_check_plants_are_scaled(ct, base_grid, raw_solar, "solar")


def test_solar_is_scaled_by_zone_and_id(base_grid, raw_solar):
ct_zone = get_change_table_for_zone_scaling(base_grid, "solar")
ct_id = get_change_table_for_id_scaling(base_grid, "solar")
ct = {**ct_zone, **ct_id["solar"]}
_check_plants_are_scaled(ct, base_grid, *raw_solar, "solar")
_check_plants_are_scaled(ct, base_grid, raw_solar, "solar")


def test_wind_is_scaled_by_zone(base_grid, raw_wind):
ct = get_change_table_for_zone_scaling(base_grid, "wind")
_check_plants_are_scaled(ct, base_grid, *raw_wind, "wind")
_check_plants_are_scaled(ct, base_grid, raw_wind, "wind")


def test_wind_is_scaled_by_id(base_grid, raw_wind):
ct = get_change_table_for_id_scaling(base_grid, "wind")
_check_plants_are_scaled(ct, base_grid, *raw_wind, "wind")
_check_plants_are_scaled(ct, base_grid, raw_wind, "wind")


def test_wind_is_scaled_by_zone_and_id(base_grid, raw_wind):
ct_zone = get_change_table_for_zone_scaling(base_grid, "wind")
ct_id = get_change_table_for_id_scaling(base_grid, "wind")
ct = {**ct_zone, **ct_id["wind"]}
_check_plants_are_scaled(ct, base_grid, *raw_wind, "wind")
_check_plants_are_scaled(ct, base_grid, raw_wind, "wind")


def test_hydro_is_scaled_by_zone(base_grid, raw_hydro):
ct = get_change_table_for_zone_scaling(base_grid, "hydro")
_check_plants_are_scaled(ct, base_grid, *raw_hydro, "hydro")
_check_plants_are_scaled(ct, base_grid, raw_hydro, "hydro")


def test_hydro_is_scaled_by_id(base_grid, raw_hydro):
ct = get_change_table_for_id_scaling(base_grid, "hydro")
_check_plants_are_scaled(ct, base_grid, *raw_hydro, "hydro")
_check_plants_are_scaled(ct, base_grid, raw_hydro, "hydro")


def test_hydro_is_scaled_by_zone_and_id(base_grid, raw_hydro):
ct_zone = get_change_table_for_zone_scaling(base_grid, "hydro")
ct_id = get_change_table_for_id_scaling(base_grid, "hydro")
ct = {**ct_zone, **ct_id["hydro"]}
_check_plants_are_scaled(ct, base_grid, *raw_hydro, "hydro")
_check_plants_are_scaled(ct, base_grid, raw_hydro, "hydro")


def test_new_solar_are_added(base_grid, raw_solar):
ct = get_change_table_for_new_plant_addition(base_grid, "solar")
_ = _check_new_plants_are_added(ct, base_grid, *raw_solar, "solar")
_ = _check_new_plants_are_added(ct, base_grid, raw_solar, "solar")


def test_new_wind_are_added(base_grid, raw_wind):
ct = get_change_table_for_new_plant_addition(base_grid, "wind")
_ = _check_new_plants_are_added(ct, base_grid, *raw_wind, "wind")
_ = _check_new_plants_are_added(ct, base_grid, raw_wind, "wind")


def test_new_hydro_added(base_grid, raw_hydro):
ct = get_change_table_for_new_plant_addition(base_grid, "hydro")
_ = _check_new_plants_are_added(ct, base_grid, *raw_hydro, "hydro")
_ = _check_new_plants_are_added(ct, base_grid, raw_hydro, "hydro")


def test_new_solar_are_not_scaled(base_grid, raw_solar):
_check_new_plants_are_not_scaled(base_grid, *raw_solar, "solar")
_check_new_plants_are_not_scaled(base_grid, raw_solar, "solar")


def test_new_wind_are_not_scaled(base_grid, raw_wind):
_check_new_plants_are_not_scaled(base_grid, *raw_wind, "wind")
_check_new_plants_are_not_scaled(base_grid, raw_wind, "wind")


def test_new_hydro_are_not_scaled(base_grid, raw_hydro):
_check_new_plants_are_not_scaled(base_grid, *raw_hydro, "hydro")
_check_new_plants_are_not_scaled(base_grid, raw_hydro, "hydro")


def test_new_solar_profile(base_grid, raw_solar):
_check_profile_of_new_plants_are_produced_correctly(base_grid, *raw_solar, "solar")
_check_profile_of_new_plants_are_produced_correctly(base_grid, raw_solar, "solar")


def test_new_wind_profile(base_grid, raw_wind):
_check_profile_of_new_plants_are_produced_correctly(base_grid, *raw_wind, "wind")
_check_profile_of_new_plants_are_produced_correctly(base_grid, raw_wind, "wind")


def test_new_hydro_profile(base_grid, raw_hydro):
_check_profile_of_new_plants_are_produced_correctly(base_grid, *raw_hydro, "hydro")
_check_profile_of_new_plants_are_produced_correctly(base_grid, raw_hydro, "hydro")

0 comments on commit 5d306f8

Please sign in to comment.