-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathalerts.py
More file actions
763 lines (610 loc) · 21.4 KB
/
alerts.py
File metadata and controls
763 lines (610 loc) · 21.4 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
"""Logic for alerting the user on possibly problematic patterns in the data (e.g. high number of zeros , constant
values, high correlations)."""
from enum import Enum, auto, unique
from typing import Dict, List, Optional, Set
import numpy as np
import pandas as pd
from ydata_profiling.config import Settings
from ydata_profiling.model.correlations import perform_check_correlation
from ydata_profiling.utils.formatters import fmt_percent
from ydata_profiling.utils.styles import get_alert_styles
@unique
class AlertType(Enum):
"""Alert types"""
CONSTANT = auto()
"""This variable has a constant value."""
ZEROS = auto()
"""This variable contains zeros."""
HIGH_CORRELATION = auto()
"""This variable is highly correlated."""
HIGH_CARDINALITY = auto()
"""This variable has a high cardinality."""
UNSUPPORTED = auto()
"""This variable is unsupported."""
DUPLICATES = auto()
"""This variable contains duplicates."""
NEAR_DUPLICATES = auto()
"""This variable contains duplicates."""
SKEWED = auto()
"""This variable is highly skewed."""
IMBALANCE = auto()
"""This variable is imbalanced."""
MISSING = auto()
"""This variable contains missing values."""
INFINITE = auto()
"""This variable contains infinite values."""
TYPE_DATE = auto()
"""This variable is likely a datetime, but treated as categorical."""
UNIQUE = auto()
"""This variable has unique values."""
DIRTY_CATEGORY = auto()
"""This variable is a categories with potential fuzzy values, and for that reason might incur in consistency issues."""
CONSTANT_LENGTH = auto()
"""This variable has a constant length."""
REJECTED = auto()
"""Variables are rejected if we do not want to consider them for further analysis."""
UNIFORM = auto()
"""The variable is uniformly distributed."""
NON_STATIONARY = auto()
"""The variable is a non-stationary series."""
SEASONAL = auto()
"""The variable is a seasonal time series."""
EMPTY = auto()
"""The DataFrame is empty."""
class Alert:
"""An alert object (type, values, column)."""
_anchor_id: Optional[str] = None
def __init__(
self,
alert_type: AlertType,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
fields: Optional[Set] = None,
is_empty: bool = False,
):
self.fields = fields or set()
self.alert_type = alert_type
self.values = values or {}
self.column_name = column_name
self._is_empty = is_empty
self._styles = get_alert_styles()
@property
def alert_type_name(self) -> str:
return self.alert_type.name.replace("_", " ").capitalize()
@property
def anchor_id(self) -> Optional[str]:
if self._anchor_id is None:
self._anchor_id = str(hash(self.column_name))
return self._anchor_id
def fmt(self) -> str:
# TODO: render in template
style = self._styles.get(self.alert_type.name.lower(), "secondary")
hint = ""
if self.alert_type == AlertType.HIGH_CORRELATION and self.values is not None:
num = len(self.values["fields"])
title = ", ".join(self.values["fields"])
corr = self.values["corr"]
hint = f'data-bs-toggle="tooltip" data-bs-placement="right" data-bs-title="This variable has a high {corr} correlation with {num} fields: {title}"'
return (
f'<span class="badge text-bg-{style}" {hint}>{self.alert_type_name}</span>'
)
def _get_description(self) -> str:
"""Return a human level description of the alert.
Returns:
str: alert description
"""
alert_type = self.alert_type.name
column = self.column_name
return f"[{alert_type}] alert on column {column}"
def __repr__(self):
return self._get_description()
class ConstantLengthAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.CONSTANT_LENGTH,
values=values,
column_name=column_name,
fields={"composition_min_length", "composition_max_length"},
is_empty=is_empty,
)
def _get_description(self) -> str:
return f"[{self.column_name}] has a constant length"
class ConstantAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.CONSTANT,
values=values,
column_name=column_name,
fields={"n_distinct"},
is_empty=is_empty,
)
def _get_description(self) -> str:
return f"[{self.column_name}] has a constant value"
class DuplicatesAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.DUPLICATES,
values=values,
column_name=column_name,
fields={"n_duplicates"},
is_empty=is_empty,
)
def _get_description(self) -> str:
if self.values is not None:
return f"Dataset has {self.values['n_duplicates']} ({fmt_percent(self.values['p_duplicates'])}) duplicate rows"
else:
return "Dataset has no duplicated rows"
class NearDuplicatesAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.NEAR_DUPLICATES,
values=values,
column_name=column_name,
fields={"n_near_dups"},
is_empty=is_empty,
)
def _get_description(self) -> str:
if self.values is not None:
return f"Dataset has {self.values['n_near_dups']} ({fmt_percent(self.values['p_near_dups'])}) near duplicate rows"
else:
return "Dataset has no near duplicated rows"
class EmptyAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.EMPTY,
values=values,
column_name=column_name,
fields={"n"},
is_empty=is_empty,
)
def _get_description(self) -> str:
return "Dataset is empty"
class HighCardinalityAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.HIGH_CARDINALITY,
values=values,
column_name=column_name,
fields={"n_distinct"},
is_empty=is_empty,
)
def _get_description(self) -> str:
if self.values is not None:
return f"[{self.column_name}] has {self.values['n_distinct']:} ({fmt_percent(self.values['p_distinct'])}) distinct values"
else:
return f"[{self.column_name}] has a high cardinality"
class DirtyCategoryAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.DIRTY_CATEGORY,
values=values,
column_name=column_name,
fields={"n_fuzzy_vals"},
is_empty=is_empty,
)
def _get_description(self) -> str:
if self.values is not None:
return f"[{self.column_name}] has {self.values['n_fuzzy_vals']} fuzzy values: {fmt_percent(self.values['p_fuzzy_vals'])} per category"
else:
return f"[{self.column_name}] no dirty categories values."
class HighCorrelationAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.HIGH_CORRELATION,
values=values,
column_name=column_name,
is_empty=is_empty,
)
def _get_description(self) -> str:
if self.values is not None:
description = f"[{self.column_name}] is highly {self.values['corr']} correlated with [{self.values['fields'][0]}]"
if len(self.values["fields"]) > 1:
description += f" and {len(self.values['fields']) - 1} other fields"
else:
return (
f"[{self.column_name}] has a high correlation with one or more colums"
)
return description
class ImbalanceAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.IMBALANCE,
values=values,
column_name=column_name,
fields={"imbalance"},
is_empty=is_empty,
)
def _get_description(self) -> str:
description = f"[{self.column_name}] is highly imbalanced"
if self.values is not None:
return description + f" ({self.values['imbalance']})"
else:
return description
class InfiniteAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.INFINITE,
values=values,
column_name=column_name,
fields={"p_infinite", "n_infinite"},
is_empty=is_empty,
)
def _get_description(self) -> str:
if self.values is not None:
return f"[{self.column_name}] has {self.values['n_infinite']} ({fmt_percent(self.values['p_infinite'])}) infinite values"
else:
return f"[{self.column_name}] has infinite values"
class MissingAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.MISSING,
values=values,
column_name=column_name,
fields={"p_missing", "n_missing"},
is_empty=is_empty,
)
def _get_description(self) -> str:
if self.values is not None:
return f"[{self.column_name}] {self.values['n_missing']} ({fmt_percent(self.values['p_missing'])}) missing values"
else:
return f"[{self.column_name}] has missing values"
class NonStationaryAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.NON_STATIONARY,
values=values,
column_name=column_name,
is_empty=is_empty,
)
def _get_description(self) -> str:
return f"[{self.column_name}] is non stationary"
class SeasonalAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.SEASONAL,
values=values,
column_name=column_name,
is_empty=is_empty,
)
def _get_description(self) -> str:
return f"[{self.column_name}] is seasonal"
class SkewedAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.SKEWED,
values=values,
column_name=column_name,
fields={"skewness"},
is_empty=is_empty,
)
def _get_description(self) -> str:
description = f"[{self.column_name}] is highly skewed"
if self.values is not None:
return description + f"(\u03b31 = {self.values['skewness']})"
else:
return description
class TypeDateAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.TYPE_DATE,
values=values,
column_name=column_name,
is_empty=is_empty,
)
def _get_description(self) -> str:
return f"[{self.column_name}] only contains datetime values, but is categorical. Consider applying `pd.to_datetime()`"
class UniformAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.UNIFORM,
values=values,
column_name=column_name,
is_empty=is_empty,
)
def _get_description(self) -> str:
return f"[{self.column_name}] is uniformly distributed"
class UniqueAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.UNIQUE,
values=values,
column_name=column_name,
fields={"n_distinct", "p_distinct", "n_unique", "p_unique"},
is_empty=is_empty,
)
def _get_description(self) -> str:
return f"[{self.column_name}] has unique values"
class UnsupportedAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.UNSUPPORTED,
values=values,
column_name=column_name,
is_empty=is_empty,
)
def _get_description(self) -> str:
return f"[{self.column_name}] is an unsupported type, check if it needs cleaning or further analysis"
class ZerosAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.ZEROS,
values=values,
column_name=column_name,
fields={"n_zeros", "p_zeros"},
is_empty=is_empty,
)
def _get_description(self) -> str:
if self.values is not None:
return f"[{self.column_name}] has {self.values['n_zeros']} ({fmt_percent(self.values['p_zeros'])}) zeros"
else:
return f"[{self.column_name}] has predominantly zeros"
class RejectedAlert(Alert):
def __init__(
self,
values: Optional[Dict] = None,
column_name: Optional[str] = None,
is_empty: bool = False,
):
super().__init__(
alert_type=AlertType.REJECTED,
values=values,
column_name=column_name,
is_empty=is_empty,
)
def _get_description(self) -> str:
return f"[{self.column_name}] was rejected"
def check_table_alerts(table: dict) -> List[Alert]:
"""Checks the overall dataset for alerts.
Args:
table: Overall dataset statistics.
Returns:
A list of alerts.
"""
alerts: List[Alert] = []
if alert_value(table.get("n_duplicates", np.nan)):
alerts.append(
DuplicatesAlert(
values=table,
)
)
if table["n"] == 0:
alerts.append(
EmptyAlert(
values=table,
)
)
return alerts
def numeric_alerts(config: Settings, summary: dict) -> List[Alert]:
alerts: List[Alert] = []
# Skewness
if skewness_alert(summary["skewness"], config.vars.num.skewness_threshold):
alerts.append(SkewedAlert(summary))
# Infinite values
if alert_value(summary["p_infinite"]):
alerts.append(InfiniteAlert(summary))
# Zeros
if alert_value(summary["p_zeros"]):
alerts.append(ZerosAlert(summary))
if (
"chi_squared" in summary
and summary["chi_squared"]["pvalue"] > config.vars.num.chi_squared_threshold
):
alerts.append(UniformAlert())
return alerts
def timeseries_alerts(config: Settings, summary: dict) -> List[Alert]:
alerts: List[Alert] = numeric_alerts(config, summary)
if not summary["stationary"]:
alerts.append(NonStationaryAlert())
if summary["seasonal"]:
alerts.append(SeasonalAlert())
return alerts
def categorical_alerts(config: Settings, summary: dict) -> List[Alert]:
alerts: List[Alert] = []
# High cardinality
if summary.get("n_distinct", np.nan) > config.vars.cat.cardinality_threshold:
alerts.append(HighCardinalityAlert(summary))
if (
"chi_squared" in summary
and summary["chi_squared"]["pvalue"] > config.vars.cat.chi_squared_threshold
):
alerts.append(UniformAlert())
if summary.get("date_warning"):
alerts.append(TypeDateAlert())
# Constant length
if "composition" in summary and summary["min_length"] == summary["max_length"]:
alerts.append(ConstantLengthAlert())
# Imbalance
if (
"imbalance" in summary
and summary["imbalance"] > config.vars.cat.imbalance_threshold
):
alerts.append(ImbalanceAlert(summary))
return alerts
def boolean_alerts(config: Settings, summary: dict) -> List[Alert]:
alerts: List[Alert] = []
if (
"imbalance" in summary
and summary["imbalance"] > config.vars.bool.imbalance_threshold
):
alerts.append(ImbalanceAlert())
return alerts
def generic_alerts(summary: dict) -> List[Alert]:
alerts: List[Alert] = []
# Missing
if alert_value(summary["p_missing"]):
alerts.append(MissingAlert())
return alerts
def supported_alerts(summary: dict) -> List[Alert]:
alerts: List[Alert] = []
if summary.get("n_distinct", np.nan) == summary["n"]:
alerts.append(UniqueAlert())
if summary.get("n_distinct", np.nan) == 1:
alerts.append(ConstantAlert(summary))
return alerts
def unsupported_alerts() -> List[Alert]:
alerts: List[Alert] = [
UnsupportedAlert(),
RejectedAlert(),
]
return alerts
def check_variable_alerts(config: Settings, col: str, description: dict) -> List[Alert]:
"""Checks individual variables for alerts.
Args:
col: The column name that is checked.
description: The series description.
Returns:
A list of alerts.
"""
alerts: List[Alert] = []
alerts += generic_alerts(description)
if description["type"] == "Unsupported":
alerts += unsupported_alerts()
else:
alerts += supported_alerts(description)
if description["type"] == "Categorical":
alerts += categorical_alerts(config, description)
if description["type"] == "Numeric":
alerts += numeric_alerts(config, description)
if description["type"] == "TimeSeries":
alerts += timeseries_alerts(config, description)
if description["type"] == "Boolean":
alerts += boolean_alerts(config, description)
for idx in range(len(alerts)):
alerts[idx].column_name = col
alerts[idx].values = description
return alerts
def check_correlation_alerts(config: Settings, correlations: dict) -> List[Alert]:
alerts: List[Alert] = []
correlations_consolidated = {}
for corr, matrix in correlations.items():
if config.correlations[corr].warn_high_correlations:
threshold = config.correlations[corr].threshold
correlated_mapping = perform_check_correlation(matrix, threshold)
for col, fields in correlated_mapping.items():
set(fields).update(set(correlated_mapping.get(col, [])))
correlations_consolidated[col] = fields
if len(correlations_consolidated) > 0:
for col, fields in correlations_consolidated.items():
alerts.append(
HighCorrelationAlert(
column_name=col,
values={"corr": "overall", "fields": fields},
)
)
return alerts
def get_alerts(
config: Settings, table_stats: dict, series_description: dict, correlations: dict
) -> List[Alert]:
alerts: List[Alert] = check_table_alerts(table_stats)
for col, description in series_description.items():
alerts += check_variable_alerts(config, col, description)
alerts += check_correlation_alerts(config, correlations)
alerts.sort(key=lambda alert: str(alert.alert_type))
return alerts
def alert_value(value: float) -> bool:
return not pd.isna(value) and value > 0.01
def skewness_alert(v: float, threshold: int) -> bool:
return not pd.isna(v) and (v < (-1 * threshold) or v > threshold)
def type_date_alert(series: pd.Series) -> bool:
from dateutil.parser import ParserError, parse
try:
series.apply(parse)
except ParserError:
return False
else:
return True