-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiurnal.py
More file actions
309 lines (272 loc) · 11.6 KB
/
Copy pathdiurnal.py
File metadata and controls
309 lines (272 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""Methods for plotting diurnal profiles from time-indexed data."""
import calendar
import textwrap
from typing import Tuple
import matplotlib.collections as mcollections
import matplotlib.lines as mlines
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np
import pandas as pd
from ..bhom.analytics import bhom_analytics
from .utilities import create_title
@bhom_analytics()
def diurnal(
series: pd.Series,
ax: plt.Axes = None,
period: str = "daily",
quantile_range: Tuple[float, float] = (0.05, 0.95),
median: bool = True,
mean: bool = True,
**kwargs,
) -> plt.Axes:
"""Plot a profile aggregated across days in the specified timeframe.
Args:
series (pd.Series):
A time-indexed Pandas Series object.
ax (plt.Axes, optional):
A matplotlib Axes object. Defaults to None.
period (str, optional):
The period to aggregate over. Must be one of "dailyy", "weekly", or "monthly". Defaults to "daily".
quantile_range (Tuple[float, float]):
The quantile range to display in a lighter (30% alpha) colour on the plot. Defaults to (0.05, 0.95).
median (bool, optional):
Whether to plot the median line. Default `True`.
mean (bool, optional):
Whether to plot the mean line. Default `True`.
**kwargs (Dict[str, Any], optional):
Additional keyword arguments to pass to the matplotlib plotting function.
legend (bool, optional):
If True, show the legend. Defaults to True.
style_context (string, optional):
The matplotlib style to use. Defaults to python_toolkit.bhom
Returns:
plt.Axes:
A matplotlib Axes object.
"""
if not isinstance(series.index, pd.DatetimeIndex):
raise ValueError("Series passed is not datetime indexed.")
show_legend = kwargs.pop("legend", True)
style_context = kwargs.pop("style_context", "python_toolkit.bhom")
with plt.style.context(style_context):
if ax is None:
ax = plt.gca()
# NOTE - no checks here for missing days, weeks, or months, it should be evident from the plot
# obtain plotting parameters
minmax_range = kwargs.pop("minmax_range", [0.0001, 0.9999])
if minmax_range[0] > minmax_range[1]:
raise ValueError("minmax_range must be increasing.")
minmax_alpha = kwargs.pop("minmax_alpha", 0.1)
if quantile_range[0] > quantile_range[1]:
raise ValueError("quantile_range must be increasing.")
if quantile_range[0] < minmax_range[0] or quantile_range[1] > minmax_range[1]:
raise ValueError("quantile_range must be within minmax_range.")
quantile_alpha = kwargs.pop("quantile_alpha", 0.3)
color = kwargs.pop("color", "slategray")
# resample to hourly to ensuure hour alignment
# TODO - for now we only resample to hourly, but this could be made more flexible by allowing any subset of period
series = series.resample("h").mean()
# remove nan/inf
series = series.replace([-np.inf, np.inf], np.nan).dropna()
# Remove outliers
series = series[
(series >= series.quantile(minmax_range[0]))
& (series <= series.quantile(minmax_range[1]))
]
# group data
if period == "daily":
group = series.groupby(series.index.hour)
target_idx = range(24)
major_ticks = target_idx[::3]
minor_ticks = target_idx
major_ticklabels = [f"{i:02d}:00" for i in major_ticks]
elif period == "weekly":
group = series.groupby([series.index.dayofweek, series.index.hour])
target_idx = pd.MultiIndex.from_product([range(7), range(24)])
major_ticks = range(len(target_idx))[::12]
minor_ticks = range(len(target_idx))[::3]
major_ticklabels = []
for i in target_idx:
if i[1] == 0:
major_ticklabels.append(f"{calendar.day_abbr[i[0]]}")
elif i[1] == 12:
major_ticklabels.append("")
elif period == "monthly":
group = series.groupby([series.index.month, series.index.hour])
target_idx = pd.MultiIndex.from_product([range(1, 13, 1), range(24)])
major_ticks = range(len(target_idx))[::12]
minor_ticks = range(len(target_idx))[::6]
major_ticklabels = []
for i in target_idx:
if i[1] == 0:
major_ticklabels.append(f"{calendar.month_abbr[i[0]]}")
elif i[1] == 12:
major_ticklabels.append("")
else:
raise ValueError("period must be one of 'daily', 'weekly', or 'monthly'")
samples_per_timestep = group.count().mean()
ax.set_title(
create_title(
kwargs.pop("title", None),
f"Average {period} diurnal profile (≈{samples_per_timestep:0.0f} samples per timestep)",
)
)
# Get values to plot
minima = group.min()
lower = group.quantile(quantile_range[0])
median = group.median()
mean = group.mean()
upper = group.quantile(quantile_range[1])
maxima = group.max()
# create df for re-indexing
df = pd.concat(
[minima, lower, median, mean, upper, maxima],
axis=1,
keys=["minima", "lower", "median", "mean", "upper", "maxima"],
).reindex(target_idx)
# populate plot
for n, i in enumerate(range(len(df) + 1)[::24]):
if n == len(range(len(df) + 1)[::24]) - 1:
continue
# q-q
ax.fill_between(
range(len(df) + 1)[i : i + 25],
(df["lower"].tolist() + [df["lower"].values[0]])[i : i + 24]
+ [(df["lower"].tolist() + [df["lower"].values[0]])[i : i + 24][0]],
(df["upper"].tolist() + [df["upper"].values[0]])[i : i + 24]
+ [(df["upper"].tolist() + [df["upper"].values[0]])[i : i + 24][0]],
alpha=quantile_alpha,
color=color,
lw=None,
ec=None,
label=f"{quantile_range[0]:0.0%}-{quantile_range[1]:0.0%}ile"
if n == 0
else "_nolegend_",
)
# q-extreme
ax.fill_between(
range(len(df) + 1)[i : i + 25],
(df["lower"].tolist() + [df["lower"].values[0]])[i : i + 24]
+ [(df["lower"].tolist() + [df["lower"].values[0]])[i : i + 24][0]],
(df["minima"].tolist() + [df["minima"].values[0]])[i : i + 24]
+ [(df["minima"].tolist() + [df["minima"].values[0]])[i : i + 24][0]],
alpha=minmax_alpha,
color=color,
lw=None,
ec=None,
label="Range" if n == 0 else "_nolegend_",
)
ax.fill_between(
range(len(df) + 1)[i : i + 25],
(df["upper"].tolist() + [df["upper"].values[0]])[i : i + 24]
+ [(df["upper"].tolist() + [df["upper"].values[0]])[i : i + 24][0]],
(df["maxima"].tolist() + [df["maxima"].values[0]])[i : i + 24]
+ [(df["maxima"].tolist() + [df["maxima"].values[0]])[i : i + 24][0]],
alpha=minmax_alpha,
color=color,
lw=None,
ec=None,
label="_nolegend_",
)
# mean/median
if mean:
ax.plot(
range(len(df) + 1)[i : i + 25],
(df["mean"].tolist() + [df["mean"].values[0]])[i : i + 24]
+ [(df["mean"].tolist() + [df["mean"].values[0]])[i : i + 24][0]],
c=color,
ls="-",
lw=1,
label="Average" if n == 0 else "_nolegend_",
)
if median:
ax.plot(
range(len(df) + 1)[i : i + 25],
(df["median"].tolist() + [df["median"].values[0]])[i : i + 24]
+ [(df["median"].tolist() + [df["median"].values[0]])[i : i + 24][0]],
c=color,
ls="--",
lw=1,
label="Median" if n == 0 else "_nolegend_",
)
# format axes
ax.set_xlim(0, len(df))
ax.xaxis.set_major_locator(mticker.FixedLocator(major_ticks))
ax.xaxis.set_minor_locator(mticker.FixedLocator(minor_ticks))
ax.set_xticklabels(
major_ticklabels,
minor=False,
ha="left",
)
if show_legend:
ax.legend(
bbox_to_anchor=(0.5, -0.2),
loc=8,
ncol=6,
borderaxespad=0,
)
ax.set_ylabel(series.name)
return ax
@bhom_analytics()
def stacked_diurnals(
datasets: list[pd.Series], period: str = "monthly", **kwargs
) -> plt.Figure:
"""Create a matplotlib figure with stacked diurnal profiles.
Args:
datasets (list[pd.Series]):
A list of time-indexed Pandas Series objects.
period (str, optional):
The period to aggregate over. Must be one of "dailyy", "weekly", or "monthly". Defaults to "monthly".
**kwargs (Dict[str, Any], optional):
Additional keyword arguments to pass to the matplotlib plotting function.
colors (list[str], optional):
A list of colors to use for the plots. Defaults to None, which uses the default diurnal color.
style_context (string, optional):
The matplotlib style to use. Defaults to python_toolkit.bhom
Returns:
plt.Figure:
A matplotlib Figure object.
"""
if len(datasets) <= 1:
raise ValueError("stacked_diurnals requires at least two datasets.")
style_context = kwargs.pop("style_context", "python_toolkit.bhom")
with plt.style.context(style_context):
fig, axes = plt.subplots(
len(datasets), 1, figsize=(12, 2 * len(datasets)), sharex=True
)
for n, (ax, series) in enumerate(zip(axes, datasets)):
if "colors" in kwargs:
kwargs["color"] = kwargs["colors"][n]
diurnal(series, ax=ax, period=period, **kwargs)
ax.set_title(None)
ax.get_legend().remove()
ax.set_ylabel(textwrap.fill(ax.get_ylabel(), 20))
handles, labels = axes[-1].get_legend_handles_labels()
new_handles = []
for handle in handles:
if isinstance(handle, mcollections.PolyCollection):
new_handles.append(
mpatches.Patch(
color="slategray", alpha=handle.get_alpha(), edgecolor=None
)
)
if isinstance(handle, mlines.Line2D):
new_handles.append(
mlines.Line2D(
(0,), (0,), color="slategray", linestyle=handle.get_linestyle()
)
)
plt.legend(
new_handles, labels, bbox_to_anchor=(0.5, -0.12), loc="upper center", ncol=4
)
fig.suptitle(
create_title(
kwargs.pop("title", None),
f"Average {period} diurnal profile" + "s" if len(datasets) > 1 else "",
),
x=fig.subplotpars.left,
ha="left",
)
plt.tight_layout()
return fig