DataConsistencyChecker¶
The main class. Create a checker, load data with init_data(), run the checks with check_data_quality(), then
use the methods below to examine the results.
DataConsistencyChecker
¶
DataConsistencyChecker(
iqr_limit: float = 3.5,
idr_limit: float = 1.0,
max_combinations: int = 100000,
verbose: int = 1,
)
Bases: BaseTestsMixin, NumericTestsMixin, DateTestsMixin, StringTestsMixin, BinaryTestsMixin, MultiColumnTestsMixin, DisplayMixin, PlotsMixin, SynthDataMixin, ResultsMixin, AnalysisCacheMixin, DataInitMixin, ExecutionMixin, ExportMixin
Automated data quality checker performing 164 tests to identify patterns and anomalies.
This class examines tabular datasets for consistency patterns across single columns, pairs of columns, and larger column sets. It identifies both patterns and exceptions to those patterns, useful for EDA and interpretable outlier detection.
Loading data and running checks¶
init_data
¶
Prepare data for quality checking. Must be called before check_data_quality().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame to be assessed for data quality |
required |
known_date_cols
|
list[str] | None
|
List of column names to treat as date columns. If None, date columns are auto-detected. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None. Data is stored in instance variables. |
check_data_quality
¶
check_data_quality(
append_results: bool = False,
execute_list: list[str] | None = None,
exclude_list: list[str] | None = None,
test_start_id: int = 0,
fast_only: bool = False,
include_code_tests: bool = True,
freq_contamination_level: float = 0.005,
rare_contamination_level: float = 0.1,
run_parallel: bool = False,
raise_on_error: bool = False,
) -> None
Execute data quality tests on the dataset specified in init_data().
Identifies patterns and exceptions in the data. Use additional API calls to access and analyze the results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
append_results
|
bool
|
If True, append to previous results; if False, clear previous results |
False
|
execute_list
|
list[str] | None
|
Specific test IDs to execute. If None, runs all tests |
None
|
exclude_list
|
list[str] | None
|
Test IDs to exclude. Cannot be used with execute_list |
None
|
test_start_id
|
int
|
Test number to start from (for resuming incomplete runs) |
0
|
fast_only
|
bool
|
If True, run only fast single-column tests |
False
|
include_code_tests
|
bool
|
If True, include tests for code/ID value columns |
True
|
freq_contamination_level
|
float
|
Max fraction (or count) of rows violating a pattern for tests that frequently find results. Lower values reduce false positives |
0.005
|
rare_contamination_level
|
float
|
Max fraction (or count) of rows violating a pattern for tests that rarely find results. Higher values reduce false negatives |
0.1
|
run_parallel
|
bool
|
Not supported; if True, a RuntimeWarning is issued and the tests run sequentially |
False
|
raise_on_error
|
bool
|
If True, stop on the first failed test and raise a structured OutlierDetectionError. If False, retain failures and continue running the remaining tests. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None. Results stored in instance variables accessible via other methods. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If both execute_list and exclude_list are specified |
OutlierDetectionError
|
If a test fails and raise_on_error is True |
test_contamination_level
¶
test_contamination_level(
contamination_levels_arr: Sequence[float] | None = None,
execute_list: list[str] | None = None,
exclude_list: list[str] | None = None,
fast_only: bool = False,
include_code_tests: bool = True,
plot: bool = True,
) -> tuple[list[int], list[int], list[int]]
Show how the findings depend on the contamination level, to help choose one.
A pattern is only recognised when fewer rows than the contamination level break it, and exceptions are only
found where a pattern is recognised. A small level finds only strong exceptions but may miss interesting
patterns; a larger level finds more, with more noise. This runs the checks once per level, on a copy of the
checker, so the results of the last check_data_quality() call are kept.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contamination_levels_arr
|
Sequence[float] | None
|
The levels to try, each a fraction of the rows (below 1) or a number of rows (an
integer), as |
None
|
execute_list
|
list[str] | None
|
Only run these checks. |
None
|
exclude_list
|
list[str] | None
|
Run every check except these. |
None
|
fast_only
|
bool
|
Only run the fast checks. |
False
|
include_code_tests
|
bool
|
Include the checks specific to code or ID values. |
True
|
plot
|
bool
|
Plot the three counts against the level. |
True
|
Returns:
| Type | Description |
|---|---|
tuple[list[int], list[int], list[int]]
|
One value per level in each list: the number of issues (patterns with exceptions), the number of rows flagged at least once, and the number of columns flagged at least once. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no data was loaded with |
Lists and summaries¶
Mixin providing result queries, processing, reports, summaries, state management, and scoring.
get_patterns_list
¶
get_patterns_list(
test_exclude_list: list[str] | None = None,
column_exclude_list: list[str] | None = None,
show_short_list_only: bool = True,
) -> pd.DataFrame | None
Get a DataFrame listing identified patterns without exceptions.
Returns patterns discovered in the data, optionally filtered by test or column. Each row represents one pattern (one test on a set of columns).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
test_exclude_list
|
list[str] | None
|
Test IDs to exclude from results |
None
|
column_exclude_list
|
list[str] | None
|
Column names to exclude from results |
None
|
show_short_list_only
|
bool
|
If True, return only high-relevance (low-noise) patterns |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame with columns
|
Test ID, Column(s), Description of Pattern |
DataFrame | None
|
Returns None if no patterns have been identified yet. |
get_exceptions_list
¶
Get a DataFrame listing patterns with exceptions.
Returns patterns that were discovered with violations. Similar to get_patterns_list() but includes an additional column for the number of exceptions found.
Returns:
| Type | Description |
|---|---|
DataFrame with columns
|
Test ID, Column(s), Description, Number of Exceptions |
DataFrame | None
|
Returns None if no exceptions have been identified yet. |
get_test_ids_with_results
¶
Return the IDs of the checks that found something.
Useful, for example, to loop over the checks calling display_detailed_results() for each.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_patterns
|
bool
|
Include the checks that found at least one pattern without exceptions. |
True
|
include_exceptions
|
bool
|
Include the checks that found at least one pattern with exceptions. |
True
|
Returns:
| Type | Description |
|---|---|
list[str]
|
The check IDs, in execution order. |
get_results_by_row_id
¶
Return the exceptions that flag one row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row_num
|
int
|
The 0-based row number in the original data. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, str]]
|
A list of |
get_exceptions
¶
Return which rows each exception flags, with the rows' scores.
The DataFrame has a row per row of the original data, a boolean column per pattern with exceptions (True
where the row is flagged), and the FINAL SCORE and NORMALIZED SCORE columns. It can be large, so it
is more useful for further analysis than for display.
Returns:
| Type | Description |
|---|---|
DataFrame | None
|
The DataFrame, or None before |
get_exceptions_by_column
¶
Return a score per cell of the original data.
Each pattern with exceptions contributes a score of 1.0 to each row it flags, divided evenly between the columns involved: a flagged row of a pattern over 4 columns adds 0.25 to each of those 4 cells.
Returns:
| Type | Description |
|---|---|
DataFrame
|
A DataFrame with the same shape as the original data, holding the summed scores. |
summarize_patterns_and_exceptions
¶
Summarize patterns and exceptions for each test.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_tests
|
bool
|
Include tests with no patterns when |
False
|
heatmap
|
bool
|
Display a heatmap when |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
summarize_patterns_by_test
¶
Return, for each check that found patterns without exceptions, how many columns or column sets have one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
heatmap
|
bool
|
Also display the table as a heatmap. |
False
|
summarize_exceptions_by_test
¶
Return, for each check that found exceptions, how many columns and rows it flagged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
heatmap
|
bool
|
Also display the table as a heatmap. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame | None
|
A DataFrame indexed by check ID, with the number of columns flagged at least once, rows flagged at least once, and issues in total. |
summarize_patterns_by_test_and_feature
¶
Return a table of the patterns found without exceptions, by check and column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_tests
|
bool
|
Include a row for every check, including those that found no patterns. |
False
|
heatmap
|
bool
|
Also display the table as a heatmap. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame | None
|
A DataFrame with a row per check and a column per original column, holding a
checkmark where the
check found a pattern involving that column. Checks such as |
summarize_exceptions_by_test_and_feature
¶
Return a table of the exceptions found, by check and column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_tests
|
bool
|
Include a row for every check, including those that found no exceptions. |
False
|
heatmap
|
bool
|
Also display the table as a heatmap. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame | None
|
A DataFrame with a row per check and a column per original column, holding the number of rows flagged by the check in that column (blank where none). |
get_single_feature_tests_matrix
¶
Returns a matrix with a column for every column in the original data and a row for each test executed. The cell values contain the percent of rows matching the pattern associated with the test. In many cases, this will be NaN (rendered as '-'), as not all tests execute on all columns. For example, tests for large numeric values execute only on numeric columns. As well, for efficiency, many tests skip examining columns that have many null values, many zero values, few or many unique values, etc. This in necessary to make the execution on many tests tractable where there are many columns and/or many rows.
Outlier scores¶
Mixin providing result queries, processing, reports, summaries, state management, and scoring.
get_outlier_scores
¶
Return row-level outlier scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalized
|
bool
|
If False, return the historical raw count of patterns flagging each row. If True, divide that count by the number of active exception-result columns, yielding values in [0, 1]. |
False
|
Returns:
| Type | Description |
|---|---|
list[int] | list[float]
|
One score per row in the original dataframe. |
get_outlier_score_summary
¶
Return raw and normalized outlier scores in a stable dataframe.
The returned index matches the checker row index. A copy is returned so callers cannot mutate checker state.
Detailed display¶
Mixin providing display helper methods.
display_detailed_results
¶
display_detailed_results(
test_id_list: list[str] | None = None,
col_name_list: Collection[str] | None = None,
issue_id_list: list[int] | None = None,
pattern_id_list: list[int] | None = None,
row_id_list: list[int] | None = None,
show_patterns: bool = True,
show_exceptions: bool = True,
show_short_list_only: bool = True,
include_examples: bool = True,
plot_results: bool = True,
max_shown: float = -1,
save_to_disk: bool = False,
output_folder: str | None = None,
) -> None
Display each pattern and exception found, in detail.
For each finding this shows the check and columns involved, a description, the number of exceptions,
example rows that were and were not flagged and, where available, plots. Without any filter, if more
findings exist than max_shown, a message explains how to narrow the results instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
test_id_list
|
list[str] | None
|
Only show findings of these checks. By default all checks with findings are shown. |
None
|
col_name_list
|
Collection[str] | None
|
Only show findings that involve at least one of these columns. |
None
|
issue_id_list
|
list[int] | None
|
Only show these exceptions (the |
None
|
pattern_id_list
|
list[int] | None
|
Only show these patterns (the |
None
|
row_id_list
|
list[int] | None
|
Only show exceptions that flag at least one of these 0-based row numbers. Patterns are then not shown. |
None
|
show_patterns
|
bool
|
Show patterns found without exceptions. |
True
|
show_exceptions
|
bool
|
Show patterns found with exceptions. |
True
|
show_short_list_only
|
bool
|
Only show patterns without exceptions for checks in the short list
( |
True
|
include_examples
|
bool
|
Show example rows for each finding. |
True
|
plot_results
|
bool
|
Show plots for the checks that have them. |
True
|
max_shown
|
float
|
Maximum number of findings to show. With -1, the limit is 200 (50,000 when saving to disk), halved when examples are included and halved again when plots are included. |
-1
|
save_to_disk
|
bool
|
Write the output to |
False
|
output_folder
|
str | None
|
Folder for the HTML report when |
None
|
display_next
¶
Display the detailed results of the next check that found something.
Each call shows the findings of one check, in execution order, by calling display_detailed_results() for it.
This spreads long output over several notebook cells. It prints "No further test results" once every check with
findings has been shown.
display_most_flagged_rows
¶
Display the rows with the highest outlier scores.
Only rows flagged at least once are shown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_results
|
bool
|
If True, show one table per row with the original values and, below them, which checks flagged which columns. If False, show the rows in a single table, highlighting flagged cells in notebooks. |
True
|
n_rows
|
int
|
Maximum number of rows to show. At most 10 rows are shown when |
10
|
display_least_flagged_rows
¶
Display the rows with the lowest outlier scores.
These are the most typical rows, and give context for the flagged ones. If most rows are flagged at least once, some of the rows shown may have non-zero scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_results
|
bool
|
If True, show one table per row with the original values and any checks that flagged it. If False, show the rows in a single table with their scores. |
True
|
n_rows
|
int
|
Maximum number of rows to show. At most 10 rows are shown when |
10
|
display_columns_types_list
¶
Display lists of columns for each inferred type.
display_columns_types_table
¶
Display the inferred types along with sample data.
Plots and quick report¶
Mixin providing plotting utilities for :class:DataConsistencyChecker.
quick_report
¶
Display an overview of the results in one call.
Shows the patterns list, the patterns by check and column, the exceptions list and summaries, and plots of the outlier-score distributions by row, column and check.
plot_final_scores_distribution_by_row
¶
Display a probability plot and histogram representing the distribution of final scores by row.
plot_final_scores_distribution_by_feature
¶
Display a bar plot representing the distribution of final scores by feature.
plot_final_scores_distribution_by_test
¶
Display a bar plot representing the distribution of final scores by test.
plot_columns_vs_final_scores
¶
Used to determine if there are any relationships between column values and the final scores of the rows. This displays tables and plots presenting any relationships found.
check_data_quality_by_feature_pairs
¶
Show how often simple relationships hold between each pair of numeric columns.
An alternative view to check_data_quality(): for each of a few relationships, it displays a heatmap of
the fraction of rows for which the relationship holds, for every pair of numeric columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_features_shown
|
int
|
Maximum number of numeric columns included in each heatmap. |
30
|
Curating results¶
Mixin providing result queries, processing, reports, summaries, state management, and scoring.
clear_results
¶
clear_results(
test_id_list: list[str] | None = None,
col_name_list=None,
pattern_id_list: list[int] | None = None,
issue_id_list: list[int] | None = None,
clear_code_tests: bool = False,
clear_all_patterns: bool = False,
clear_all_exceptions: bool = False,
) -> None
Remove selected patterns or exceptions from the results.
Use this to prune findings that are not meaningful for your data (for example, relationships between
columns that are not really comparable), keeping a final set of findings to report on. Exactly one option
may be given per call: call it repeatedly to clear more. restore_results() undoes all clearing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
test_id_list
|
list[str] | None
|
Remove all patterns and exceptions of these checks. |
None
|
col_name_list
|
list[str] | None
|
Remove all patterns and exceptions involving any of these columns, including those that also involve other columns. |
None
|
pattern_id_list
|
list[int] | None
|
Remove these patterns (see |
None
|
issue_id_list
|
list[int] | None
|
Remove these exceptions (see |
None
|
clear_code_tests
|
bool
|
Remove all findings of the checks specific to code and ID values
( |
False
|
clear_all_patterns
|
bool
|
Remove all patterns. Exceptions are not affected. |
False
|
clear_all_exceptions
|
bool
|
Remove all exceptions. Patterns are not affected. |
False
|
restore_results
¶
Restore the results of the last check_data_quality() call, undoing every clear_results() call.
Reports and failures¶
Mixin providing result queries, processing, reports, summaries, state management, and scoring.
get_report
¶
Return a structured, JSON-safe snapshot of the current results.
Returns:
| Type | Description |
|---|---|
DataConsistencyReport
|
A |
get_execution_failures
¶
Return diagnostics for the checks that raised an error during the last run.
A failing check does not stop the others (unless raise_on_error=True). Each entry is a dictionary with
the check's test_id and an error envelope describing the OutlierDetectionError, including the
original exception as its cause.
Information about the checks¶
Mixin providing display helper methods.
get_test_list
¶
Return the IDs of all implemented checks, in the order they are executed.
get_test_catalog
¶
Return typed metadata for every implemented check.
Returns:
| Type | Description |
|---|---|
list[TestMetadata]
|
One |
get_test_descriptions
¶
Return a dictionary mapping test IDs to short descriptions.
print_test_descriptions
¶
Print test descriptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
long_desc
|
bool
|
If |
False
|
f
|
TextIO | None
|
Optional file handle. If provided, output is written to this file as HTML. |
None
|
get_patterns_shortlist
¶
Return the IDs of the checks whose patterns (without exceptions) are shown by default.
Patterns found by the other checks are common in most datasets, so they are only listed when
show_short_list_only=False is passed to get_patterns_list() or display_detailed_results().
Exceptions are always reported, whatever the check.
get_tests_for_codes
¶
Return the IDs of the checks specific to code or ID values.
These checks treat individual characters as meaningful (for example the prefix of X7333). Skip them with
check_data_quality(include_code_tests=False) when no column holds codes or IDs.
demo_test
¶
Demonstrate a single test on synthetic data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
test_id
|
str
|
ID of the test to run. |
required |
include_nulls
|
bool
|
Whether to generate several versions of the demo data
containing different amounts of |
False
|
Synthetic data¶
Provide synthetic data utilities to :class:DataConsistencyChecker.
generate_synth_data
¶
generate_synth_data(
all_cols: bool = False,
execute_list: list[str] | None = None,
exclude_list: list[str] | None = None,
seed: int = 0,
add_nones: str = "none",
) -> pd.DataFrame
Generate a random dataset for demonstrating tests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_cols
|
bool
|
When |
False
|
execute_list
|
list[str] | None
|
Optional list of test IDs to generate columns for. |
None
|
exclude_list
|
list[str] | None
|
Optional list of test IDs not to generate columns
for. |
None
|
seed
|
int
|
Seed for the random number generators used during creation. |
0
|
add_nones
|
str
|
One of |
'none'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The generated synthetic DataFrame. |
modify_real_data
¶
modify_real_data(
df: DataFrame, num_modifications: int = 5
) -> tuple[pd.DataFrame, list[tuple[int, str]]]
Introduce slight modifications to an existing dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Real or synthetic dataset to modify. |
required |
num_modifications
|
int
|
Number of cells to alter. |
5
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A tuple with the modified DataFrame and a list describing the changed |
list[tuple[int, str]]
|
cells. Each list element is |