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

Issue22 load factor #41

Merged
merged 4 commits into from
Apr 23, 2018
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
30 changes: 30 additions & 0 deletions opengrid/library/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,33 @@ def count_peaks(ts):
result = on_toggles & shifted
count = result.sum()
return count


def load_factor(ts, resolution=None, norm=None):
"""
Calculate the ratio of input vs. norm over a given interval.

Parameters
----------
ts : pandas.Series
timeseries
resolution : str, optional
interval over which to calculate the ratio
default: resolution of the input timeseries
norm : int | float, optional
denominator of the ratio
default: the maximum of the input timeseries

Returns
-------
pandas.Series
"""
if norm is None:
norm = ts.max()

if resolution is not None:
ts = ts.resample(rule=resolution).mean()

lf = ts / norm

return lf
11 changes: 11 additions & 0 deletions opengrid/tests/test_analyses.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ def test_count_peaks(self):
count = og.analysis.count_peaks(ts)
self.assertEqual(count, 13)

def test_load_factor(self):
ts = og.datasets.get('electricity_2016_hour')
ts = ts['e1de'].truncate(after=pd.Timestamp('20160107'))
lf1 = og.analysis.load_factor(ts)
self.assertIsInstance(ts, pd.Series)
self.assertAlmostEqual(ts.iloc[0], (lf1 * ts.max()).iloc[0])

lf2 = og.analysis.load_factor(ts, resolution='3h', norm=800)
self.assertIsInstance(ts, pd.Series)
self.assertAlmostEqual(175.0345212009457, (lf2 * 800).iloc[0])


if __name__ == '__main__':
unittest.main()