/
section_generator.py
214 lines (188 loc) · 7.98 KB
/
section_generator.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
# Copyright (c) 2021 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.
import fnmatch
import multiprocessing
import numpy as np
import pandas as pd
from joblib import Parallel, delayed
from tqdm import tqdm
from ..base import Module
from ..config import get_stat_description
from ..utils import filter_metrics, short_date
from ..visualization.utils import _prune, plot_bars_b64
class SectionGenerator(Module):
"""This module takes the time-series data of already computed statistics, plots the data and
combines all the plots into a list which is stored together with the section name in a dictionary
which later will be used for the report generation.
"""
def __init__(
self,
read_key,
store_key,
section_name,
features=None,
ignore_features=None,
last_n=0,
skip_first_n=0,
skip_last_n=0,
static_bounds=None,
dynamic_bounds=None,
prefix="traffic_light_",
suffices=["_red_high", "_yellow_high", "_yellow_low", "_red_low"],
ignore_stat_endswith=None,
skip_empty_plots=True,
description="",
show_stats=None,
):
"""Initialize an instance of SectionGenerator.
:param str read_key: key of input data to read from the datastore and use for plotting
:param str store_key: key for output data to be stored in the datastore
:param str section_name: key of output data to store in the datastore
:param list features: list of features to pick up from input data (optional)
:param list ignore_features: ignore list of features, if present (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: in plot skip last 'n' periods. last_n takes precedence (optional)
:param str static_bounds: key to static traffic light bounds key in datastore (optional)
:param str dynamic_bounds: key to dynamic traffic light bounds key in datastore (optional)
:param str prefix: dynamic traffic light prefix. default is ``'traffic_light_'`` (optional)
:param str suffices: dynamic traffic light suffices. (optional)
:param list ignore_stat_endswith: ignore stats ending with any of list of suffices. (optional)
:param bool skip_empty_plots: if false, also show empty plots in report with only nans or zeroes (optional)
:param str description: description of the section. default is empty (optional)
:param list show_stats: list of statistic name patterns to show in the report. If None, show all (optional)
"""
super().__init__()
self.read_key = read_key
self.store_key = store_key
self.features = features or []
self.ignore_features = ignore_features or []
self.section_name = section_name
self.last_n = last_n
self.skip_first_n = skip_first_n
self.skip_last_n = skip_last_n
self.dynamic_bounds = dynamic_bounds
self.static_bounds = static_bounds
self.prefix = prefix
self.suffices = suffices
self.ignore_stat_endswith = ignore_stat_endswith or []
self.skip_empty_plots = skip_empty_plots
self.description = description
self.show_stats = show_stats
def transform(self, datastore):
data_obj = self.get_datastore_object(datastore, self.read_key, dtype=dict)
static_bounds = self.get_datastore_object(
datastore, self.static_bounds, dtype=dict, default={}
)
dynamic_bounds = self.get_datastore_object(
datastore, self.dynamic_bounds, dtype=dict, default={}
)
features = self.get_features(data_obj.keys())
features_w_metrics = []
num_cores = multiprocessing.cpu_count()
self.logger.info(
f'Generating section "{self.section_name}". skip empty plots: {self.skip_empty_plots}'
)
for feature in tqdm(features, ncols=100):
df = data_obj.get(feature, pd.DataFrame())
fdbounds = dynamic_bounds.get(feature, pd.DataFrame(index=df.index))
assert all(df.index == fdbounds.index)
# prepare date labels
df.drop(
columns=["histogram", "reference_histogram"],
inplace=True,
errors="ignore",
)
dates = [short_date(str(date)) for date in df.index.tolist()]
metrics = filter_metrics(
df.columns, self.ignore_stat_endswith, self.show_stats
)
plots = Parallel(n_jobs=num_cores)(
delayed(_plot_metric)(
feature,
metric,
dates,
df[metric],
static_bounds,
fdbounds,
self.prefix,
self.suffices,
self.last_n,
self.skip_first_n,
self.skip_last_n,
self.skip_empty_plots,
)
for metric in metrics
)
# filter out potential empty plots (from skip empty plots)
if self.skip_empty_plots:
plots = [e for e in plots if len(e["plot"])]
features_w_metrics.append(
{"name": feature, "plots": sorted(plots, key=lambda plot: plot["name"])}
)
params = {
"section_title": self.section_name,
"section_description": self.description,
"features": features_w_metrics,
}
if self.store_key not in datastore:
datastore[self.store_key] = []
datastore[self.store_key].append(params)
return datastore
def _plot_metric(
feature,
metric,
dates,
values,
static_bounds,
fdbounds,
prefix,
suffices,
last_n,
skip_first_n,
skip_last_n,
skip_empty,
):
"""Split off plot histogram generation to allow for parallel processing"""
# pick up static traffic light boundaries
name = feature + ":" + metric
sbounds = static_bounds.get(name, ())
# pick up dynamic traffic light boundaries
names = [prefix + metric + suffix for suffix in suffices]
dbounds = tuple(
[
_prune(fdbounds[n].tolist(), last_n, skip_first_n, skip_last_n)
for n in names
if n in fdbounds.columns
]
)
# choose dynamic bounds if present
bounds = dbounds if len(dbounds) > 0 else sbounds
# prune dates and values
dates = _prune(dates, last_n, skip_first_n, skip_last_n)
values = _prune(values, last_n, skip_first_n, skip_last_n)
# make plot. note: slow!
plot = plot_bars_b64(
data=np.array(values),
labels=dates,
ylim=True,
bounds=bounds,
skip_empty=skip_empty,
)
return {"name": metric, "description": get_stat_description(metric), "plot": plot}