Skip to content

Commit

Permalink
feat(core): prettier prints in report method (#25)
Browse files Browse the repository at this point in the history
Features:
- Better visualization of warnings summary (report method)
  • Loading branch information
jfsantos-ds authored Sep 22, 2021
1 parent 8cb471b commit c831709
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
15 changes: 9 additions & 6 deletions src/ydata_quality/core/data_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pandas as pd

from ydata_quality.core.warnings import Priority, QualityWarning
from ydata_quality.core.warnings import Priority, QualityWarning, WarningStyling
from ydata_quality.drift import DriftAnalyser
from ydata_quality.duplicates import DuplicateChecker
from ydata_quality.labelling import LabelInspector
Expand Down Expand Up @@ -158,11 +158,14 @@ def report(self):
self.__store_warnings() # fetch all warnings from the engines
self.__clean_warnings()
if not self._warnings:
print('No warnings found.')
print(f'{WarningStyling.OKAY}No warnings found.{WarningStyling.ENDC}')
else:
prio_counts = Counter([warn.priority.value for warn in self._warnings])
print('Warnings count by priority:')
print(*(f"\tPriority {prio}: {count} warning(s)" for prio, count in prio_counts.items()), sep='\n')
print(f'{WarningStyling.BOLD}Warnings:{WarningStyling.ENDC}')
print(f'\tTOTAL: {len(self._warnings)} warning(s)')
print('List of warnings sorted by priority:')
print(*(f"\t{warn}" for warn in self._warnings), sep='\n')
print(*(f"\t{WarningStyling.BOLD}{WarningStyling.PRIORITIES[prio]}Priority {prio}{WarningStyling.ENDC}: {count} warning(s)" for prio, count in prio_counts.items()), sep='\n')
warns = [[warn for warn in self._warnings if warn.priority.value == level] for level in range(4)]
for warn_list in warns:
if len(warn_list)>0:
print(warn_list[0].priority)
print(*(f"\t{warn}" for warn in warn_list), sep='\n')
16 changes: 9 additions & 7 deletions src/ydata_quality/core/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import pandas as pd
from numpy import random

from ydata_quality.core.warnings import Priority, QualityWarning
from ydata_quality.core.warnings import Priority, QualityWarning, WarningStyling
from ydata_quality.utils.auxiliary import infer_df_type, infer_dtypes
from ydata_quality.utils.enum import DataFrameType


class QualityEngine(ABC):
Expand Down Expand Up @@ -116,14 +115,17 @@ def report(self):
"Prints a report containing all the warnings detected during the data quality analysis."
self.__clean_warnings()
if not self._warnings:
print('No warnings found.')
print(f'{WarningStyling.OKAY}No warnings found.{WarningStyling.ENDC}')
else:
prio_counts = Counter([warn.priority.value for warn in self._warnings])
print('Warnings count by priority:')
print(*(f"\tPriority {prio}: {count} warning(s)" for prio, count in prio_counts.items()), sep='\n')
print(f'{WarningStyling.BOLD}Warnings:{WarningStyling.ENDC}')
print(f'\tTOTAL: {len(self._warnings)} warning(s)')
print('List of warnings sorted by priority:')
print(*(f"\t{warn}" for warn in self._warnings), sep='\n')
print(*(f"\t{WarningStyling.BOLD}{WarningStyling.PRIORITIES[prio]}Priority {prio}{WarningStyling.ENDC}: {count} warning(s)" for prio, count in prio_counts.items()), sep='\n')
warns = [[warn for warn in self._warnings if warn.priority.value == level] for level in range(4)]
for warn_list in warns:
if len(warn_list)>0:
print(warn_list[0].priority)
print(*(f"\t{warn}" for warn in warn_list), sep='\n')

def evaluate(self):
"Runs all the indidividual tests available within the same suite. Returns a dict of (name: results)."
Expand Down
23 changes: 21 additions & 2 deletions src/ydata_quality/core/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@
from ydata_quality.utils.enum import OrderedEnum


class WarningStyling:
PRIORITIES_F = {
0: u"\u001b[48;5;1m",
1: u"\u001b[48;5;209m",
2: u"\u001b[48;5;11m",
3: u"\u001b[48;5;69m"
}
PRIORITIES = {
0: u"\u001b[38;5;1m",
1: u"\u001b[38;5;209m",
2: u"\u001b[38;5;11m",
3: u"\u001b[38;5;69m"
}
OKAY = u"\u001b[38;5;2m"
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'


class Priority(OrderedEnum):
"""Priorities translate the expected impact of data quality issues.
Expand All @@ -30,7 +49,7 @@ def __str__(self):
2: 'usage allowed, limited human intelligibility',
3: 'minor impact, aesthetic'
}
return f"Priority {self.value}: {_descriptions[self.value]}"
return f"{WarningStyling.PRIORITIES[self.value]}{WarningStyling.BOLD}Priority {self.value}{WarningStyling.ENDC} - {WarningStyling.BOLD}{_descriptions[self.value]}{WarningStyling.ENDC}:"


class QualityWarning(BaseModel):
Expand All @@ -53,7 +72,7 @@ class QualityWarning(BaseModel):
# String Representation #
#########################
def __str__(self):
return f"[{self.test.upper()}] {self.description} ({str(self.priority)})"
return f"{WarningStyling.PRIORITIES[self.priority.value]}*{WarningStyling.ENDC} {WarningStyling.BOLD}[{self.category.upper()}{WarningStyling.ENDC} - {WarningStyling.UNDERLINE}{self.test.upper()}]{WarningStyling.ENDC} {self.description}"

########################
# Comparison Operators #
Expand Down

0 comments on commit c831709

Please sign in to comment.