/
report_pipelines.py
365 lines (338 loc) · 14 KB
/
report_pipelines.py
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# Copyright (c) 2020 ING Wholesale Banking Advanced Analytics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from pathlib import PosixPath
from ..base import Pipeline
from ..config import config
from ..io import FileWriter
from ..pipeline.metrics_pipelines import (
metrics_expanding_reference,
metrics_external_reference,
metrics_rolling_reference,
metrics_self_reference,
)
from ..visualization import (
HistogramSection,
ReportGenerator,
SectionGenerator,
TrafficLightSectionGenerator,
)
def self_reference(
hists_key="test_hists",
time_axis="date",
window=10,
monitoring_rules={},
pull_rules={},
features=None,
skip_empty_plots=True,
last_n=0,
plot_hist_n=2,
report_filepath=None,
show_stats=None,
**kwargs,
):
"""Example pipeline for comparing test data with itself (full test set)
:param str hists_key: key to test histograms in datastore. default is 'test_hists'
:param str time_axis: name of datetime feature. default is 'date'
:param int window: window size for trend detection. default is 10
:param dict monitoring_rules: traffic light rules
:param dict pull_rules: pull rules to determine dynamic boundaries
:param list features: features of histograms to pick up from input data (optional)
:param bool skip_empty_plots: if false, also show empty plots in report with only nans or zeroes (optional)
:param int last_n: plot statistic data for last 'n' periods (optional)
:param int plot_hist_n: plot histograms for last 'n' periods. default is 1 (optional)
:param str report_filepath: the file path where to output the report (optional)
:param list show_stats: list of statistic name patterns to show in the report. If None, show all (optional)
:param kwargs: residual keyword arguments
:return: assembled self reference pipeline
"""
modules = [
metrics_self_reference(
hists_key,
time_axis,
window,
monitoring_rules,
pull_rules,
features,
**kwargs,
),
ReportPipe(
sections_key="report_sections",
store_key="html_report",
skip_empty_plots=skip_empty_plots,
last_n=last_n,
plot_hist_n=plot_hist_n,
report_filepath=report_filepath,
show_stats=show_stats,
),
]
pipeline = Pipeline(modules)
return pipeline
def external_reference(
hists_key="test_hists",
ref_hists_key="ref_hists",
time_axis="date",
window=10,
monitoring_rules={},
pull_rules={},
features=None,
skip_empty_plots=True,
last_n=0,
plot_hist_n=2,
report_filepath=None,
show_stats=None,
**kwargs,
):
"""Example pipeline for comparing test data with other (full) external reference set
:param str hists_key: key to test histograms in datastore. default is 'test_hists'
:param str ref_hists_key: key to reference histograms in datastore. default is 'ref_hists'
:param str time_axis: name of datetime feature. default is 'date'
:param int window: window size for trend detection. default is 10
:param dict monitoring_rules: traffic light rules
:param dict pull_rules: pull rules to determine dynamic boundaries
:param list features: features of histograms to pick up from input data (optional)
:param bool skip_empty_plots: if false, show empty plots in report with only nans or zeroes (optional)
:param int last_n: plot statistic data for last 'n' periods (optional)
:param int plot_hist_n: plot histograms for last 'n' periods. default is 1 (optional)
:param str report_filepath: the file path where to output the report (optional)
:param list show_stats: list of statistic name patterns to show in the report. If None, show all (optional)
:param kwargs: residual keyword arguments
:return: assembled external reference pipeline
"""
modules = [
metrics_external_reference(
hists_key,
ref_hists_key,
time_axis,
window,
monitoring_rules,
pull_rules,
features,
**kwargs,
),
ReportPipe(
sections_key="report_sections",
store_key="html_report",
skip_empty_plots=skip_empty_plots,
last_n=last_n,
plot_hist_n=plot_hist_n,
report_filepath=report_filepath,
show_stats=show_stats,
),
]
pipeline = Pipeline(modules)
return pipeline
def rolling_reference(
hists_key="test_hists",
time_axis="date",
window=10,
shift=1,
monitoring_rules={},
pull_rules={},
features=None,
skip_empty_plots=True,
last_n=0,
plot_hist_n=2,
report_filepath=None,
show_stats=None,
**kwargs,
):
"""Example pipeline for comparing test data with itself (rolling test set)
:param str hists_key: key to test histograms in datastore. default is 'test_hists'
:param str time_axis: name of datetime feature. default is 'date'
:param int window: size of rolling window and for trend detection. default is 10
:param int shift: shift in rolling window. default is 1
:param dict monitoring_rules: traffic light rules
:param dict pull_rules: pull rules to determine dynamic boundaries
:param list features: features of histograms to pick up from input data (optional)
:param bool skip_empty_plots: if false, show empty plots in report with only nans or zeroes (optional)
:param int last_n: plot statistic data for last 'n' periods (optional)
:param int plot_hist_n: plot histograms for last 'n' periods. default is 1 (optional)
:param str report_filepath: the file path where to output the report (optional)
:param list show_stats: list of statistic name patterns to show in the report. If None, show all (optional)
:param kwargs: residual keyword arguments
:return: assembled rolling reference pipeline
"""
modules = [
metrics_rolling_reference(
hists_key,
time_axis,
window,
shift,
monitoring_rules,
pull_rules,
features,
**kwargs,
),
ReportPipe(
sections_key="report_sections",
store_key="html_report",
skip_empty_plots=skip_empty_plots,
last_n=last_n,
plot_hist_n=plot_hist_n,
report_filepath=report_filepath,
show_stats=show_stats,
),
]
pipeline = Pipeline(modules)
return pipeline
def expanding_reference(
hists_key="test_hists",
time_axis="date",
window=10,
shift=1,
monitoring_rules={},
pull_rules={},
features=None,
skip_empty_plots=True,
last_n=0,
plot_hist_n=2,
report_filepath=None,
show_stats=None,
**kwargs,
):
"""Example pipeline for comparing test data with itself (expanding test set)
:param str hists_key: key to test histograms in datastore. default is 'test_hists'
:param str time_axis: name of datetime feature. default is 'date'
:param int window: window size for trend detection. default is 10
:param int shift: shift in expanding window. default is 1
:param dict monitoring_rules: traffic light rules
:param dict pull_rules: pull rules to determine dynamic boundaries
:param list features: features of histograms to pick up from input data (optional)
:param bool skip_empty_plots: if false, show empty plots in report with only nans or zeroes (optional)
:param int last_n: plot statistic data for last 'n' periods (optional)
:param int plot_hist_n: plot histograms for last 'n' periods. default is 1 (optional)
:param str report_filepath: the file path where to output the report (optional)
:param list show_stats: list of statistic name patterns to show in the report. If None, show all (optional)
:param kwargs: residual keyword arguments
:return: assembled expanding reference pipeline
"""
modules = [
metrics_expanding_reference(
hists_key,
time_axis,
window,
shift,
monitoring_rules,
pull_rules,
features,
**kwargs,
),
ReportPipe(
sections_key="report_sections",
store_key="html_report",
skip_empty_plots=skip_empty_plots,
last_n=last_n,
plot_hist_n=plot_hist_n,
report_filepath=report_filepath,
show_stats=show_stats,
),
]
pipeline = Pipeline(modules)
return pipeline
class ReportPipe(Pipeline):
"""Pipeline of modules for generating sections and a final report."""
def __init__(
self,
sections_key="report_sections",
store_key="html_report",
profiles_section="Profiles",
comparisons_section="Comparisons",
traffic_lights_section="Traffic Lights",
alerts_section="Alerts",
histograms_section="Histograms",
report_filepath=None,
show_stats=None,
skip_empty_plots=True,
last_n=0,
skip_first_n=0,
skip_last_n=0,
plot_hist_n=2,
):
"""Initialize an instance of Report.
:param str sections_key: key to store sections data in the datastore
:param str store_key: key to store the HTML report data in the datastore
:param str profiles_section: name for the profile data section. default is 'Profiles'
:param str comparisons_section: name for the comparison data section. default is 'Comparisons'
:param str traffic_lights_section: name for the traffic light section. default is 'Traffic Lights'
:param str alerts_section: name for the alerts section. default is 'Alerts'
:param str histograms_section: name for the histograms section. default is 'Histograms'
:param str report_filepath: the file path where to output the report (optional)
:param bool skip_empty_plots: if false, also show empty plots in report with only nans or zeroes (optional)
:param int last_n: plot statistic data for last 'n' periods (optional)
:param int skip_first_n: when plotting data skip first 'n' periods. last_n takes precedence (optional)
:param int skip_last_n: when plotting data skip last 'n' periods. last_n takes precedence (optional)
:param int plot_hist_n: plot histograms for last 'n' periods. default is 1 (optional)
:param list show_stats: list of statistic name patterns to show in the report. If None, show all (optional)
"""
super().__init__(modules=[])
self.store_key = store_key
# dictionary of section descriptions
descs = config["section_descriptions"]
# default keyword arguments for each section
def sg_kws(read_key):
return dict(
read_key=read_key,
store_key=sections_key,
skip_empty_plots=skip_empty_plots,
last_n=last_n,
skip_first_n=skip_first_n,
skip_last_n=skip_last_n,
show_stats=show_stats,
description=descs.get(read_key, ""),
)
self.modules = [
# --- o generate sections
# - a section of profiled statistics with dynamic or static traffic light bounds
# - a section of histogram and pull comparison statistics
# - a section showing all traffic light alerts of monitored statistics
# - a section with a summary of traffic light alerts
# --- o generate report
SectionGenerator(
dynamic_bounds="dynamic_bounds",
section_name=profiles_section,
static_bounds="static_bounds",
ignore_stat_endswith=["_mean", "_std", "_pull"],
**sg_kws("profiles"),
),
SectionGenerator(
dynamic_bounds="dynamic_bounds_comparisons",
static_bounds="static_bounds_comparisons",
section_name=comparisons_section,
ignore_stat_endswith=["_mean", "_std", "_pull"],
**sg_kws("comparisons"),
),
TrafficLightSectionGenerator(
section_name=traffic_lights_section, **sg_kws("traffic_lights")
),
SectionGenerator(section_name=alerts_section, **sg_kws("alerts")),
HistogramSection(
read_key="split_hists",
store_key=sections_key,
section_name=histograms_section,
hist_name_starts_with="histogram",
last_n=plot_hist_n,
description=descs.get("histograms", ""),
),
ReportGenerator(read_key=sections_key, store_key=store_key),
]
if isinstance(report_filepath, (str, PosixPath)) and len(report_filepath) > 0:
self.modules.append(FileWriter(store_key, file_path=report_filepath))
def transform(self, datastore):
self.logger.info(f'Generating report "{self.store_key}".')
return super().transform(datastore)