Skip to content

Commit

Permalink
Merge pull request #38 from opengridcc/issue19_standby
Browse files Browse the repository at this point in the history
Finalisation of standby analysis (#19)
  • Loading branch information
saroele committed Apr 26, 2018
2 parents 384c1bd + 5da6fc6 commit 151e4a5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
23 changes: 13 additions & 10 deletions opengrid/library/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def standby(df, resolution='24h', time_window=None):
Parameters
----------
df : Pandas DataFrame
df : pandas.DataFrame or pandas.Series
Electricity Power
resolution : str, default='d'
Resolution of the computation. Data will be resampled to this resolution (as mean) before computation
Expand All @@ -91,14 +91,17 @@ def standby(df, resolution='24h', time_window=None):
df : pandas.Series with DateTimeIndex in the given resolution
"""

if df.empty:
raise EmptyDataFrame()

df = pd.DataFrame(df) # if df was a pd.Series, convert to DataFrame
def parse_time(t):
if isinstance(t, numbers.Number):
return pd.Timestamp.utcfromtimestamp(t).time()
else:
return pd.Timestamp(t).time()

if df.empty:
raise EmptyDataFrame()

# first filter based on the time-window
if time_window is not None:
t_start = parse_time(time_window[0])
Expand All @@ -118,7 +121,7 @@ def share_of_standby(df, resolution='24h', time_window=None):
Parameters
----------
df : Pandas DataFrame
df : pandas.DataFrame or pandas.Series
Power (typically electricity, can be anything)
resolution : str, default='d'
Resolution of the computation. Data will be resampled to this resolution (as mean) before computation
Expand All @@ -134,13 +137,13 @@ def share_of_standby(df, resolution='24h', time_window=None):
fraction : float between 0-1 with the share of the standby consumption
"""

df_ = pd.DataFrame(df)
p_sb = standby(df_, resolution, time_window)
df_resampled = df_.resample(resolution).mean()
p_tot = df_resampled.sum()
p_sb = standby(df, resolution, time_window)
df = df.resample(resolution).mean()
p_tot = df.sum()
p_standby = p_sb.sum()
share_standby = p_standby/p_tot
return share_standby.iloc[0]
share_standby = p_standby / p_tot
res = share_standby.iloc[0]
return res


def count_peaks(ts):
Expand Down
4 changes: 2 additions & 2 deletions opengrid/tests/test_analyses.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def test_standby_with_time_window(self):
df = datasets.get('elec_power_min_1sensor')
res = og.analysis.standby(df, 'D', time_window=('01:00', '06:00'))
self.assertEqual(res.index.tz.zone, 'Europe/Brussels')
self.assertEqual(res.to_json(), '{"1507327200000":61.739999936,"1507413600000":214.9799999222,"1507500000000":53.0399997951,"1507586400000":55.7399999164,"1507672800000":59.94000006,"1507759200000":69.4800002407,"1507845600000":56.8200000236,"1507932000000":54.1799997864,"1508018400000":54.779999801,"1508104800000":54.7199997772,"1508191200000":98.5199999576,"1508277600000":55.6799999066,"1508364000000":53.9399997052,"1508450400000":109.5599999931,"1508536800000":144.3600001093,"1508623200000":52.7999997279}')
self.assertEqual(res.squeeze().to_json(), '{"1507327200000":61.739999936,"1507413600000":214.9799999222,"1507500000000":53.0399997951,"1507586400000":55.7399999164,"1507672800000":59.94000006,"1507759200000":69.4800002407,"1507845600000":56.8200000236,"1507932000000":54.1799997864,"1508018400000":54.779999801,"1508104800000":54.7199997772,"1508191200000":98.5199999576,"1508277600000":55.6799999066,"1508364000000":53.9399997052,"1508450400000":109.5599999931,"1508536800000":144.3600001093,"1508623200000":52.7999997279}')

res = og.analysis.standby(df, 'D', time_window=('22:00', '06:00'))
self.assertEqual(res.index.tz.zone, 'Europe/Brussels')
self.assertEqual(res.to_json(), '{"1507327200000":61.739999936,"1507413600000":119.2800000636,"1507500000000":53.0399997951,"1507586400000":55.7399999164,"1507672800000":59.94000006,"1507759200000":69.4800002407,"1507845600000":56.8200000236,"1507932000000":54.1799997864,"1508018400000":54.779999801,"1508104800000":54.7199997772,"1508191200000":98.5199999576,"1508277600000":55.6799999066,"1508364000000":53.9399997052,"1508450400000":96.3000000408,"1508536800000":133.9200000744,"1508623200000":52.7999997279}')
self.assertEqual(res.squeeze().to_json(), '{"1507327200000":61.739999936,"1507413600000":119.2800000636,"1507500000000":53.0399997951,"1507586400000":55.7399999164,"1507672800000":59.94000006,"1507759200000":69.4800002407,"1507845600000":56.8200000236,"1507932000000":54.1799997864,"1508018400000":54.779999801,"1508104800000":54.7199997772,"1508191200000":98.5199999576,"1508277600000":55.6799999066,"1508364000000":53.9399997052,"1508450400000":96.3000000408,"1508536800000":133.9200000744,"1508623200000":52.7999997279}')

def test_share_of_standby_1(self):
df = pd.DataFrame(data={'conso':np.ones(48)},
Expand Down

0 comments on commit 151e4a5

Please sign in to comment.