generated from MITLibraries/python-cli-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Use DeepDiff library for a/b record diffing #47
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| # ruff: noqa: S608 | ||
|
|
||
| import json | ||
| import logging | ||
| import os | ||
| import time | ||
|
|
@@ -48,12 +47,11 @@ def create_record_diff_matrix_dataset( | |
| ) -> str: | ||
| """Create a boolean sparse matrix of modified fields for all records. | ||
|
|
||
| This writes a single parquet file with rows for each record, and columns for each | ||
| TIMDEX field, and a value of integer 1 if that field has a diff and 0 if not. This | ||
| provides a handy way to calculate aggregate metrics for a given field or source in | ||
| later steps. The column "has_diff" is also carried over from the diffs dataset to | ||
| provide a single column to check if ANY of the field columns indicate a diff for a | ||
| record row. | ||
| This writes a single parquet file with rows for each record, columns for each TIMDEX | ||
| field, and a value of integer 1 if that field has a diff and 0 if not. This provides | ||
| a handy way to calculate aggregate metrics for a given field or source in later steps. | ||
| The column "has_diff" is also carried over from the diffs dataset to provide a single | ||
| column to check if ANY of the field columns indicate a diff for a record row. | ||
|
|
||
| This code momentarily creates a single dataframe in memory for all rows. This is safe | ||
| given the nature of the dataframe: there may be 10m rows, and potentially 20-30 | ||
|
|
@@ -66,32 +64,31 @@ def create_record_diff_matrix_dataset( | |
| for i, batch in enumerate( | ||
| diffs_ds.to_batches( | ||
| batch_size=batch_size, | ||
| columns=["timdex_record_id", "source", "ab_diff", "has_diff"], | ||
| columns=["timdex_record_id", "source", "modified_timdex_fields", "has_diff"], | ||
| ) | ||
| ): | ||
| start_time = time.time() | ||
| batch_df = batch.to_pandas() | ||
|
|
||
| # parse diff JSON to dictionary for batch | ||
| batch_df["ab_diff"] = batch_df["ab_diff"].apply( | ||
| lambda diff_json: json.loads(diff_json) | ||
| ) | ||
|
|
||
| batch_metrics = [] | ||
| for _, row in batch_df.iterrows(): | ||
| record_metrics = { | ||
| "timdex_record_id": row["timdex_record_id"], | ||
| "source": row["source"], | ||
| "has_diff": 1 if row["has_diff"] == "true" else 0, | ||
| "has_diff": (1 if row["has_diff"] == "true" else 0), | ||
| } | ||
| diff_data = row["ab_diff"] | ||
| record_metrics.update(generate_field_diff_bools_for_record(diff_data)) | ||
|
|
||
| # for each modified field (root key in diff), set column and value = 1 (True) | ||
| if row["modified_timdex_fields"] is not None: | ||
| for field in row["modified_timdex_fields"]: | ||
| record_metrics[field] = 1 | ||
|
|
||
ghukill marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| batch_metrics.append(record_metrics) | ||
|
|
||
| # build dataframe for batch | ||
| batch_metrics_df = pd.DataFrame(batch_metrics) | ||
| batch_metrics_dfs.append(batch_metrics_df) | ||
| logger.info(f"batch: {i+1}, elapsed: {time.time()-start_time}") | ||
| logger.info(f"batch: {i + 1}, elapsed: {time.time() - start_time}") | ||
|
|
||
| # concatenate all dataframes into single dataframe for writing and replace None with 0 | ||
| metrics_df = pd.concat(batch_metrics_dfs) | ||
|
|
@@ -107,32 +104,6 @@ def create_record_diff_matrix_dataset( | |
| return metrics_dataset | ||
|
|
||
|
|
||
| def generate_field_diff_bools_for_record(diff_data: dict) -> dict: | ||
| """Function to return dictionary of fields that have a diff. | ||
|
|
||
| Determining if a field had a diff is as straight-forward as looking to see if it shows | ||
| up in the parsed diff JSON. The fields may be at the root of the diff, or they could | ||
| be nested under "$insert" or "$delete" nodes in the diff. | ||
|
|
||
| If a field from the original A/B records are not in the diff at all, then they did not | ||
| have changes, and therefore will not receive a 1 here to indicate a diff. | ||
| """ | ||
| fields_with_diffs = {} | ||
|
|
||
| for key in diff_data: | ||
|
|
||
| # identify modified fields nested in $insert or $delete blocks | ||
| if key in ("$insert", "$delete"): | ||
| for subfield in diff_data[key]: | ||
| fields_with_diffs[subfield] = 1 | ||
|
|
||
| # identified modified fields at root of diff | ||
| else: | ||
| fields_with_diffs[key] = 1 | ||
|
|
||
| return fields_with_diffs | ||
|
|
||
|
|
||
|
Comment on lines
-110
to
-135
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Woohoo! ✨ |
||
| def calculate_metrics_data(field_matrix_parquet: str) -> dict: | ||
| """Create a dictionary of metrics via DuckDB queries.""" | ||
| summary: dict = {} | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.