From ab930550b4bc697b7e19d0d890682fba956c28bb Mon Sep 17 00:00:00 2001 From: Alex Barros Date: Fri, 30 Dec 2022 08:50:28 -0300 Subject: [PATCH] fix: comparison alerts (#1229) * fix: compare fail if alerts are not in all reports --- src/pandas_profiling/compare_reports.py | 26 ++++++++++++++++++- src/pandas_profiling/model/alerts.py | 2 ++ .../flavours/html/templates/alerts.html | 2 +- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/pandas_profiling/compare_reports.py b/src/pandas_profiling/compare_reports.py index f13152e94..e2df11864 100644 --- a/src/pandas_profiling/compare_reports.py +++ b/src/pandas_profiling/compare_reports.py @@ -220,6 +220,30 @@ def _apply_config(description: dict, config: Settings) -> dict: return description +def _is_alert_present(alert, alert_list): + for a in alert_list: + if a.column_name == alert.column_name and a.alert_type == alert.alert_type: + return True + return False + + +def _create_placehoder_alerts(report_alerts: tuple) -> tuple: + from copy import copy + + fixed = [[] for _ in report_alerts] + for idx, alerts in enumerate(report_alerts): + for alert in alerts: + fixed[idx].append(alert) + for i, fix in enumerate(fixed): + if i == idx: + continue + if not _is_alert_present(alert, report_alerts[i]): + empty_alert = copy(alert) + empty_alert._is_empty = True + fix.append(empty_alert) + return tuple(fixed) + + def compare( reports: List[ProfileReport], config: Optional[Settings] = None, @@ -279,7 +303,7 @@ def compare( res = _update_merge(res, r) res["analysis"]["title"] = _compare_title(res["analysis"]["title"]) - + res["alerts"] = _create_placehoder_alerts(res["alerts"]) profile = ProfileReport(None, config=_config) profile._description_set = res return profile diff --git a/src/pandas_profiling/model/alerts.py b/src/pandas_profiling/model/alerts.py index 33063d1d5..287d84318 100644 --- a/src/pandas_profiling/model/alerts.py +++ b/src/pandas_profiling/model/alerts.py @@ -80,6 +80,7 @@ def __init__( values: Optional[Dict] = None, column_name: Optional[str] = None, fields: Optional[Set] = None, + is_empty: bool = False, ): if values is None: values = {} @@ -90,6 +91,7 @@ def __init__( self.alert_type = alert_type self.values = values self.column_name = column_name + self._is_empty = is_empty @property def alert_type_name(self) -> str: diff --git a/src/pandas_profiling/report/presentation/flavours/html/templates/alerts.html b/src/pandas_profiling/report/presentation/flavours/html/templates/alerts.html index eaf2f8f6f..7eebb3e06 100644 --- a/src/pandas_profiling/report/presentation/flavours/html/templates/alerts.html +++ b/src/pandas_profiling/report/presentation/flavours/html/templates/alerts.html @@ -16,7 +16,7 @@ {% for alert in value %} - {% if alert is not none %} + {% if not alert._is_empty %} {% include 'alerts/alert_' + alert.alert_type.name | lower + '.html' %} {% else %} Alert not present in {{ style._labels[idx] }}