From 11df0c67b30e188e4192cbbeebf27165b1a71eee Mon Sep 17 00:00:00 2001 From: Jan Pecinovsky Date: Thu, 26 Apr 2018 17:22:25 +0200 Subject: [PATCH 1/2] load duration curve with tests --- opengrid/library/plotting.py | 24 ++++++++++++++++++++++++ opengrid/tests/test_plotting.py | 8 ++++++++ 2 files changed, 32 insertions(+) diff --git a/opengrid/library/plotting.py b/opengrid/library/plotting.py index 3908fb7..e3ed0b1 100644 --- a/opengrid/library/plotting.py +++ b/opengrid/library/plotting.py @@ -114,3 +114,27 @@ def carpet(timeseries, **kwargs): plt.title(title) return im + + +def load_duration_curve(df, trim_zeros=False, **kwargs): + """ + Plot a load duration curve + + Parameters + ---------- + df : pd.DataFrame or pd.Series + trim_zeros : bool + trim trailing zero's + kwargs : anything you would pass to pd.DataFrame.plot()` + + Returns + ------- +` matplotlib plot + """ + df = pd.DataFrame(df) # in case a series is passed, wrap it in a dataframe + load_factors = (df[column].reset_index(drop=True).sort_values(ascending=False).reset_index(drop=True) for column in df) + if trim_zeros: + load_factors = (np.trim_zeros(s, trim='b') for s in load_factors) + df = pd.concat(load_factors, axis=1) + fig = df.plot(**kwargs) + return fig diff --git a/opengrid/tests/test_plotting.py b/opengrid/tests/test_plotting.py index 2cde7a4..de6f5b4 100644 --- a/opengrid/tests/test_plotting.py +++ b/opengrid/tests/test_plotting.py @@ -7,6 +7,7 @@ import unittest import pandas as pd +import opengrid as og from opengrid.library import plotting @@ -26,5 +27,12 @@ def test_empty(self): assert plotting.carpet(pd.Series(index=list('abc'))) is None +class LoadDurationCurveTest(unittest.TestCase): + def test_default(self): + df = og.datasets.get('gas_2016_hour') + assert og.plotting.load_duration_curve(df) is not None + assert og.plotting.load_duration_curve(df, trim_zeros=True) is not None + + if __name__ == '__main__': unittest.main() From ec98d78430ffb1e3d77c56be452fd58cf46a74cf Mon Sep 17 00:00:00 2001 From: Jan Pecinovsky Date: Sun, 29 Apr 2018 17:23:19 +0200 Subject: [PATCH 2/2] moved load duration to analysis --- opengrid/library/analysis.py | 24 ++++++++++++++++++++++++ opengrid/library/plotting.py | 24 ------------------------ opengrid/tests/test_analyses.py | 7 +++++++ opengrid/tests/test_plotting.py | 7 ------- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/opengrid/library/analysis.py b/opengrid/library/analysis.py index 3fa9a4a..297119b 100644 --- a/opengrid/library/analysis.py +++ b/opengrid/library/analysis.py @@ -197,3 +197,27 @@ def load_factor(ts, resolution=None, norm=None): lf = ts / norm return lf + + +def load_duration(df, trim_zeros=False): + """ + Create descending load duration series + (mainly for use in a load duration curve) + + Parameters + ---------- + df : pd.DataFrame or pd.Series + trim_zeros : bool + trim trailing zero's + + Returns + ------- + pd.DataFrame or pd.Series + """ + df = pd.DataFrame(df) # in case a series is passed, wrap it in a dataframe + load_durations = (df[column].reset_index(drop=True).sort_values(ascending=False).reset_index(drop=True) for column in df) + if trim_zeros: + load_durations = (np.trim_zeros(s, trim='b') for s in load_durations) + df = pd.concat(load_durations, axis=1) + result = df.squeeze() + return result diff --git a/opengrid/library/plotting.py b/opengrid/library/plotting.py index 50e6e7f..f659c50 100644 --- a/opengrid/library/plotting.py +++ b/opengrid/library/plotting.py @@ -171,27 +171,3 @@ def boxplot(df, plot_mean=False, plot_ids=None, title=None, xlabel=None, ylabel= plt.ylabel(ylabel) return plt.gcf() - - -def load_duration_curve(df, trim_zeros=False, **kwargs): - """ - Plot a load duration curve - - Parameters - ---------- - df : pd.DataFrame or pd.Series - trim_zeros : bool - trim trailing zero's - kwargs : anything you would pass to pd.DataFrame.plot()` - - Returns - ------- -` matplotlib plot - """ - df = pd.DataFrame(df) # in case a series is passed, wrap it in a dataframe - load_factors = (df[column].reset_index(drop=True).sort_values(ascending=False).reset_index(drop=True) for column in df) - if trim_zeros: - load_factors = (np.trim_zeros(s, trim='b') for s in load_factors) - df = pd.concat(load_factors, axis=1) - fig = df.plot(**kwargs) - return fig diff --git a/opengrid/tests/test_analyses.py b/opengrid/tests/test_analyses.py index b18633c..3786f9a 100644 --- a/opengrid/tests/test_analyses.py +++ b/opengrid/tests/test_analyses.py @@ -64,6 +64,13 @@ def test_load_factor(self): self.assertIsInstance(ts, pd.Series) self.assertAlmostEqual(175.0345212009457, (lf2 * 800).iloc[0]) + def test_load_duration(self): + ts = pd.Series([1, 5, 7, 0, 6, 0, 3, 2]) + ld = og.analysis.load_duration(ts) + self.assertTrue(ld.equals(pd.Series([7, 6, 5, 3, 2, 1, 0, 0]))) + ld2 = og.analysis.load_duration(ts, trim_zeros=True) + self.assertTrue(ld2.equals(pd.Series([7, 6, 5, 3, 2, 1]))) + if __name__ == '__main__': unittest.main() diff --git a/opengrid/tests/test_plotting.py b/opengrid/tests/test_plotting.py index 2375ea3..c73574f 100644 --- a/opengrid/tests/test_plotting.py +++ b/opengrid/tests/test_plotting.py @@ -44,12 +44,5 @@ def test_arguments(self): plotting.boxplot(df, plot_mean=True, plot_ids=[2, 3], title="Title", xlabel="xlable", ylabel="ylable") -class LoadDurationCurveTest(unittest.TestCase): - def test_default(self): - df = og.datasets.get('gas_2016_hour') - assert og.plotting.load_duration_curve(df) is not None - assert og.plotting.load_duration_curve(df, trim_zeros=True) is not None - - if __name__ == '__main__': unittest.main()