-
Notifications
You must be signed in to change notification settings - Fork 40
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
test: Use fake profile data instead of test_usa_tamu #586
Merged
BainanXia
merged 1 commit into
Breakthrough-Energy:develop
from
c-voegele:c-voegele/test_with_fake_data
Feb 18, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import numpy as np | ||
import pandas as pd | ||
|
||
from powersimdata.input.grid import Grid | ||
|
||
|
||
class MockInputData: | ||
BainanXia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
MockInputData is a mock of powersimdata.input.input_data.InputData | ||
that generates random profiles. | ||
|
||
Exactly 3 of {`start_time`, `end_time`, `periods`, `freq`} must be specified. See <https://pandas.pydata.org/docs/reference/api/pandas.date_range.html>. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
|
||
:param powersimdata.input.grid.Grid grid: instance of Grid object. | ||
:param str start_time: when profiles begin. | ||
:param str end_time: when profiles end. | ||
:param int periods: number of times in profile. | ||
:param str freq: frequency of times in profile. | ||
:param int random_seed: used to initialize the random generator. | ||
:raises ValueError: raised if `field_name` specified in `get_data()` is not specified by this mock | ||
:return: (*powersimdata.tests.mock_input_data.MockInputData*) | ||
""" | ||
|
||
_RESOURCES = { | ||
"wind": {"wind", "wind_offshore"}, | ||
"solar": {"solar"}, | ||
"hydro": {"hydro"}, | ||
} | ||
|
||
def __init__( | ||
self, | ||
grid: Grid, | ||
start_time="2016-01-01 00:00:00", | ||
end_time=None, | ||
periods=24, | ||
BainanXia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
freq="H", | ||
random_seed=6669, | ||
): | ||
self._grid = grid | ||
self._start_time = start_time | ||
self._end_time = end_time | ||
self._periods = periods | ||
self._freq = freq | ||
self._random = np.random.default_rng(seed=random_seed) | ||
|
||
self._profiles = { | ||
"demand": self._get_demand(), | ||
**{ | ||
resource: self._get_resource_profile(resource) | ||
for resource in self._RESOURCES.keys() | ||
}, | ||
} | ||
|
||
def get_data(self, scenario_info, field_name): | ||
"""Returns fake profile data. | ||
|
||
:param dict scenario_info: not used. | ||
:param str field_name: Can be any of *'demand'*, *'hydro'*, *'solar'*, or *'wind'*. | ||
:return: (*pandas.DataFrame*) -- fake profile data | ||
""" | ||
profile = self._profiles.get(field_name) | ||
|
||
if profile is None: | ||
raise ValueError(f"No profile specified for {field_name}!") | ||
|
||
return profile | ||
|
||
def _get_demand(self): | ||
"""Returns fake demand data. | ||
|
||
:return: (*pandas.DataFrame*) -- fake demand data | ||
""" | ||
zone_ids = set(self._grid.plant["zone_id"]) | ||
fake_demand_profile = self._create_fake_profile(zone_ids) | ||
return fake_demand_profile | ||
|
||
def _get_resource_profile(self, resource_type): | ||
"""Returns fake data for given resource_type. | ||
|
||
:param str resource_type: Can be any of *'hydro'*, *'solar'*, or *'wind'*. | ||
:return: (*pandas.DataFrame*) -- fake data for resource | ||
""" | ||
plant_ids = self._get_plant_ids_for_type(resource_type) | ||
fake_resource_profile = self._create_fake_profile(plant_ids) | ||
return fake_resource_profile | ||
|
||
def _get_plant_ids_for_type(self, resource_type): | ||
"""Retrieves plant_ids for plants of `resource_type` from the grid. | ||
|
||
:param str resource_type: Can be any of *'hydro'*, *'solar'*, or *'wind'*. | ||
:return: (*list*) -- list of plant_ids | ||
""" | ||
plant_ids = list( | ||
self._grid.plant.query("type in @self._RESOURCES[@resource_type]").index | ||
) | ||
return plant_ids | ||
|
||
def _create_fake_profile(self, columns): | ||
"""Generates a fake profile. | ||
|
||
:param list columns: columns for the DataFrame | ||
:return: (*pandas.DataFrame*) -- a fake profile | ||
""" | ||
times = pd.date_range( | ||
start=self._start_time, | ||
end=self._end_time, | ||
periods=self._periods, | ||
freq=self._freq, | ||
) | ||
index = pd.Index(times, name="UTC Time") | ||
data = self._random.uniform(low=0, high=1, size=(len(times), len(columns))) | ||
fake_profile = pd.DataFrame(data=data, index=index, columns=columns) | ||
return fake_profile |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is clean.