-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
other_section_renderer.py
239 lines (213 loc) · 7.79 KB
/
other_section_renderer.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
import json
from string import Template
from collections import defaultdict
from .renderer import Renderer
from .content_block import(
ValueListContentBlockRenderer,
GraphContentBlockRenderer,
TableContentBlockRenderer,
PrescriptiveBulletListContentBlockRenderer
)
class DescriptiveOverviewSectionRenderer(Renderer):
@classmethod
def render(cls, evrs, column=None):
content_blocks = []
# NOTE: I don't love the way this builds content_blocks as a side effect.
# The top-level API is clean and scannable, but the function internals are counterintutitive and hard to test.
# I wonder if we can enable something like jquery chaining for this. Tha would be concise AND testable.
# Pressing on for now...
cls._render_header(evrs, content_blocks)
cls._render_dataset_info(evrs, content_blocks)
cls._render_variable_types(evrs, content_blocks)
cls._render_warnings(evrs, content_blocks)
cls._render_expectation_types(evrs, content_blocks)
return {
"section_name": column,
"content_blocks": content_blocks
}
@classmethod
def _render_header(cls, evrs, content_blocks):
content_blocks.append({
"content_block_type": "header",
"header": "Overview",
"styling": {
"classes": ["col-12"]
}
})
@classmethod
def _render_dataset_info(cls, evrs, content_blocks):
table_rows = []
table_rows.append(["Number of variables", "12", ])
row_count_evr = cls._find_evr_by_type(
evrs["results"],
"expect_table_row_count_to_be_between"
)
if row_count_evr != None:
table_rows.append([
{
"template": "Number of observations",
"params": {},
"styling": {
"attributes": {
"data-toggle": "popover",
"data-trigger": "hover",
"data-placement": "top",
"data-content": "expect_table_row_count_to_be_between",
"container": "body",
}
}
},
row_count_evr["result"]["observed_value"]
])
table_rows += [
["Missing cells", "866 (8.1%)", ],
["Duplicate rows", "0 (0.0%)", ],
["Total size in memory", "83.6 KiB", ],
["Average record size in memory", "96.1 B", ],
]
content_blocks.append({
"content_block_type": "table",
"header": "Dataset info",
"table_rows": table_rows,
"styling": {
"classes": ["col-6", "table-responsive"],
"styles": {
"margin-top": "20px"
},
"body": {
"classes": ["table", "table-sm"]
}
},
})
@classmethod
def _render_variable_types(cls, evrs, content_blocks):
table_rows = [
["Numeric", "5", ],
["Categorical", "5", ],
["Boolean", "1", ],
["Date", "0", ],
["URL", "0", ],
["Text (Unique)", "1", ],
["Rejected", "0", ],
["Unsupported", "0", ],
]
content_blocks.append({
"content_block_type": "table",
"header": "Variable types",
"table_rows": table_rows,
"styling": {
"classes": ["col-6", "table-responsive", ],
"styles": {
"margin-top": "20px"
},
"body": {
"classes": ["table", "table-sm"]
}
},
})
@classmethod
def _render_expectation_types(cls, evrs, content_blocks):
type_counts = defaultdict(int)
for evr in evrs["results"]:
type_counts[evr["expectation_config"]["expectation_type"]] += 1
# table_rows = sorted(type_counts.items(), key=lambda kv: -1*kv[1])
bullet_list = sorted(type_counts.items(), key=lambda kv: -1*kv[1])
bullet_list = [{
"template": "$expectation_type $expectation_count",
"params": {
"expectation_type": tr[0],
"expectation_count": tr[1],
},
"styling": {
"classes": ["list-group-item", "d-flex", "justify-content-between", "align-items-center"],
"params": {
"expectation_count": {
"classes": ["badge", "badge-secondary", "badge-pill"],
}
}
}
} for tr in bullet_list]
content_blocks.append({
"content_block_type": "bullet_list",
"header": 'Expectation types <span class="mr-3 triangle"></span>',
"bullet_list": bullet_list,
"styling": {
"classes": ["col-12"],
"styles": {
"margin-top": "20px"
},
"header": {
"classes": ["collapsed"],
"attributes": {
"data-toggle": "collapse",
"href": "#{{content_block_id}}-body",
"aria-expanded": "true",
"aria-controls": "collapseExample",
},
"styles": {
"cursor": "pointer"
}
},
"body": {
"classes": ["list-group", "collapse"],
},
},
})
@classmethod
def _render_warnings(cls, evrs, content_blocks):
def render_warning_row(template, column, n, p, badge_label):
return [{
"template": template,
"params": {
"column": column,
"n": n,
"p": p,
},
"styling": {
"params": {
"column": {
"classes": ["badge", "badge-primary", ]
}
}
}
}, {
"template": "$badge_label",
"params": {
"badge_label": badge_label,
},
"styling": {
"params": {
"badge_label": {
"classes": ["badge", "badge-warning", ]
}
}
}
}]
table_rows = [
render_warning_row(
"$column has $n ($p%) missing values", "Age", 177, 19.9, "Missing"),
render_warning_row(
"$column has a high cardinality: $n distinct values", "Cabin", 148, None, "Warning"),
render_warning_row(
"$column has $n ($p%) missing values", "Cabin", 687, 77.1, "Missing"),
render_warning_row(
"$column has $n (< $p%) zeros", "Fare", 15, "0.1", "Zeros"),
render_warning_row(
"$column has $n (< $p%) zeros", "Parch", 678, "76.1", "Zeros"),
render_warning_row(
"$column has $n (< $p%) zeros", "SibSp", 608, "68.2", "Zeros"),
]
content_blocks.append({
"content_block_type": "table",
"header": "Warnings",
"table_rows": table_rows,
"styling": {
"classes": ["col-12"],
"styles": {
"margin-top": "20px"
},
"body": {
"classes": ["table", "table-sm"]
}
},
})