Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Validation Framework: Source + Product data #1241

Merged
merged 14 commits into from
Jan 14, 2025
Merged

Conversation

sf-dcp
Copy link
Contributor

@sf-dcp sf-dcp commented Nov 8, 2024

Partially resolves #650

What

This PR implements a raw architecture of data checks with Pandera in dcpy.

In order to run data checks on a dataframe with Pandera, you need to construct a pandera object, pandera.DataFrameSchema. The Pandera object takes inpandera.Columns which consist of pandera.Checks. Most of the PR is about creating these pandera objects in validate/pandera_utils.py module. The rest of the PR is creating a format for data checks in our templates.

Example of running data checks from Pandera docs:

import pandas as pd
import pandera as pa

# data to validate
df = pd.DataFrame({
    "column1": [1, 4, 0, 10, 9],
    "column2": [-1.3, -1.4, -2.9, -10.1, -20.4],
    "column3": ["value_1", "value_2", "value_3", "value_2", "value_1"],
})

# define schema
schema = pa.DataFrameSchema({
    "column1": pa.Column(int, checks=pa.Check.le(10)),
    "column2": pa.Column(float, checks=pa.Check.lt(-1.2)),
    ]),
})

validated_df = schema(df)
print(validated_df)

Example of data check format in ingest templates/metadata files:

columns:
  - id: bbl
    data_type: text
    is_required: True
    checks:
      - str_contains:
          args:
            pattern: "75"
          warn_only: False

  - id: custom_value
    data_type: integer
    is_required: False
    checks:
      - greater_than:
          description: This column must be greater than zero
          args:
            min_value: 0
      - unique_values_eq:
          description: This column must contain at least these values.
          warn_only: True
          args:
            values: [5, 10]

Check names in a template correspond to pandera check names or our custom checks (same approach as in preprocessing steps). In the last commit, I created a custom check as an example how we would do it; the check itself can be scraped later.

I recommend to review this PR commit by commit or starting with the validate.pandera_utils.run_data_checks() fn and work your way backwards.

🚨 Feedback needed

  • Naming of models/functions
  • validate.pandera_utils.create_check() fn: I have some validation logic in it where it checks correct input values (i.e. such check name or check parameters exist). I would like to move it under dataset.CheckAttributes model -- does it make sense to do that?
  • Better test structure?
  • How to appease mypy in the meantime, before pandera checks are fully implemented.

Next steps/PRs

  • Implement data types in pandera (map our custom defined data types in templates to pandera data types and custom defined data types like bbl)
    • Ex: text -> pandera.dtypes.DataType.String
  • Implement custom data checks.
  • Implement validate.run() function which reads in a local file into df, pulls in a template, and runs data checks.
  • Refactor ingest/distribution code to use new checks data type. Refactor distribution metadata files.

References

@sf-dcp sf-dcp force-pushed the sf-source-data-validation branch 2 times, most recently from 919a826 to f158ce4 Compare December 19, 2024 23:29
@sf-dcp sf-dcp force-pushed the sf-source-data-validation branch 5 times, most recently from 5482cb6 to 3ba1789 Compare January 2, 2025 17:48
@sf-dcp sf-dcp closed this Jan 3, 2025
@sf-dcp sf-dcp reopened this Jan 3, 2025
@sf-dcp sf-dcp force-pushed the sf-source-data-validation branch from 378e855 to 2fbd086 Compare January 5, 2025 20:31
@sf-dcp sf-dcp closed this Jan 5, 2025
@sf-dcp sf-dcp reopened this Jan 5, 2025
@sf-dcp sf-dcp force-pushed the sf-source-data-validation branch from 2fbd086 to 50d9cd6 Compare January 5, 2025 21:24
Copy link

codecov bot commented Jan 5, 2025

Codecov Report

Attention: Patch coverage is 84.41558% with 12 lines in your changes missing coverage. Please review.

Project coverage is 70.74%. Comparing base (76fbe85) to head (4227bdf).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
dcpy/lifecycle/validate/data.py 0.00% 3 Missing ⚠️
dcpy/lifecycle/validate/pandera_utils.py 92.68% 1 Missing and 2 partials ⚠️
dcpy/lifecycle/package/validate.py 0.00% 1 Missing and 1 partial ⚠️
dcpy/lifecycle/validate/pandera_custom_checks.py 71.42% 2 Missing ⚠️
dcpy/models/dataset.py 90.47% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1241      +/-   ##
==========================================
+ Coverage   70.58%   70.74%   +0.16%     
==========================================
  Files         115      119       +4     
  Lines        5966     6041      +75     
  Branches      695      706      +11     
==========================================
+ Hits         4211     4274      +63     
- Misses       1609     1617       +8     
- Partials      146      150       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sf-dcp sf-dcp force-pushed the sf-source-data-validation branch 16 times, most recently from 633c79f to 025ff0b Compare January 10, 2025 16:19
Copy link
Contributor

@fvankrieken fvankrieken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple small notes/thoughts but

  1. this all seems great to me, big picture-wise
  2. you and Alex seem to be in a good spot (and agreement) about some of the details, so I'll continue to peek and give any wanted feedback, but carry on!

Copy link
Member

@damonmcc damonmcc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't wanna miss out on the approval party! great stuff!

@sf-dcp
Copy link
Contributor Author

sf-dcp commented Jan 13, 2025

@fvankrieken @damonmcc @alexrichey Should I merge with unhappy mypy?

@fvankrieken
Copy link
Contributor

@fvankrieken @damonmcc @alexrichey Should I merge with unhappy mypy?

No, those should be resolved in some way, particularly the couple failures outside of validate.

You also have a pytest failure - looks like import issues due to renaming

@sf-dcp sf-dcp force-pushed the sf-source-data-validation branch 2 times, most recently from 8846d4b to 5c36807 Compare January 13, 2025 22:34
@sf-dcp sf-dcp force-pushed the sf-source-data-validation branch from 51defb4 to f874c8c Compare January 14, 2025 16:23
@sf-dcp sf-dcp closed this Jan 14, 2025
@sf-dcp sf-dcp reopened this Jan 14, 2025
@sf-dcp sf-dcp force-pushed the sf-source-data-validation branch 4 times, most recently from a90ef00 to 3f87188 Compare January 14, 2025 21:26
@sf-dcp sf-dcp force-pushed the sf-source-data-validation branch from 3f87188 to 3c5d0e7 Compare January 14, 2025 21:39
@sf-dcp sf-dcp merged commit 62d1198 into main Jan 14, 2025
25 checks passed
@sf-dcp sf-dcp deleted the sf-source-data-validation branch January 14, 2025 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Ingest v2 - validate input data
4 participants