-
Notifications
You must be signed in to change notification settings - Fork 152
OECD_Waste_Water_Treatment cloud job fix and goldens #2098
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
Open
saanikaaa
wants to merge
7
commits into
datacommonsorg:master
Choose a base branch
from
saanikaaa:oecd_waste_water_fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
be10572
Oecd job fix and goldens
saanikaaa 9af0d61
Merge branch 'datacommonsorg:master' into oecd_waste_water_fix
saanikaaa 56b84a8
PR comments
saanikaaa e971607
Merge branch 'datacommonsorg:master' into oecd_waste_water_fix
saanikaaa 2914d0f
Fix Lint
saanikaaa 5734e09
Internal PR Comments
saanikaaa 7ffe119
PR comments and lint
saanikaaa 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
53 changes: 53 additions & 0 deletions
53
statvar_imports/oecd/wastewater_treatment/golden_data/golden_observations.csv
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 |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| "observationAbout" | ||
| "country/AUS" | ||
| "country/AUT" | ||
| "country/BEL" | ||
| "country/CAN" | ||
| "country/CHL" | ||
| "country/CZE" | ||
| "country/DNK" | ||
| "country/EST" | ||
| "country/FIN" | ||
| "country/DEU" | ||
| "country/GRC" | ||
| "country/HUN" | ||
| "country/ISL" | ||
| "country/IRL" | ||
| "country/ITA" | ||
| "country/JPN" | ||
| "country/KOR" | ||
| "country/LUX" | ||
| "country/NLD" | ||
| "country/NOR" | ||
| "country/POL" | ||
| "country/PRT" | ||
| "country/SVK" | ||
| "country/SVN" | ||
| "country/ESP" | ||
| "country/SWE" | ||
| "country/CHE" | ||
| "country/TUR" | ||
| "country/GBR" | ||
| "country/BGR" | ||
| "country/HRV" | ||
| "country/ROU" | ||
| "country/FRA" | ||
| "country/LVA" | ||
| "country/USA" | ||
| "country/ARG" | ||
| "country/MDA" | ||
| "country/ZAF" | ||
| "country/ARM" | ||
| "country/NZL" | ||
| "country/ISR" | ||
| "country/LTU" | ||
| "country/CRI" | ||
| "country/BRA" | ||
| "country/AZE" | ||
| "country/CHN" | ||
| "country/GEO" | ||
| "country/SAU" | ||
| "country/BLR" | ||
| "country/IND" | ||
| "country/UKR" | ||
| "country/MEX" |
9 changes: 9 additions & 0 deletions
9
statvar_imports/oecd/wastewater_treatment/golden_data/golden_summary_report.csv
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| "MeasurementMethods","observationPeriods","MinDate","Units","NumPlaces","StatVar","ScalingFactors" | ||
| "[]","[]","1970","[Percent]","40","Count_Person_WasteWaterTreatment_TertiaryTreatment_WasteWaterTreated_PublicSewerage_WasteWaterTreatmentPlant_AsAFractionOf_Count_Person_WasteWaterTreatment_PublicSewerage","[100]" | ||
| "[]","[]","1970","[Percent]","48","Count_Person_WasteWaterTreatment_WasteWaterNotTreated_PublicSewerage_PublicSewerageWithoutTreatment_AsAFractionOf_Count_Person_WasteWaterTreatment_PublicSewerage","[100]" | ||
| "[]","[]","1970","[Percent]","39","Count_Person_WasteWaterTreatment_UnspecifiedTreatment_WasteWaterTreated_PublicSewerage_WasteWaterTreatmentPlant_AsAFractionOf_Count_Person_WasteWaterTreatment_PublicSewerage","[100]" | ||
| "[]","[]","1970","[Percent]","40","Count_Person_WasteWaterTreatment_PrimaryTreatment_WasteWaterTreated_PublicSewerage_WasteWaterTreatmentPlant_AsAFractionOf_Count_Person_WasteWaterTreatment_PublicSewerage","[100]" | ||
| "[]","[]","1970","[Percent]","49","Count_Person_WasteWaterTreatment_WasteWaterTreated_PublicSewerage_WasteWaterTreatmentPlant_AsAFractionOf_Count_Person_WasteWaterTreatment_PublicSewerage","[100]" | ||
| "[]","[]","1970","[Percent]","36","Count_Person_WasteWaterTreatment_WasteWaterTreated_WasteWaterIndependentlyTreated_AsAFractionOf_Count_Person","[100]" | ||
| "[]","[]","1970","[Percent]","42","Count_Person_WasteWaterTreatment_SecondaryTreatment_WasteWaterTreated_PublicSewerage_WasteWaterTreatmentPlant_AsAFractionOf_Count_Person_WasteWaterTreatment_PublicSewerage","[100]" | ||
| "[]","[]","1970","[Percent]","51","Count_Person_WasteWaterTreatment_PublicSewerage_AsAFractionOf_Count_Person","[100]" |
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
29 changes: 29 additions & 0 deletions
29
statvar_imports/oecd/wastewater_treatment/validation_config.json
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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "schema_version": "1.0", | ||
| "rules": [ | ||
| { | ||
| "rule_id": "check_deleted_records_percent", | ||
| "description": "Checks that the percentage of deleted points is within the threshold.", | ||
| "validator": "DELETED_RECORDS_PERCENT", | ||
| "params": { | ||
| "threshold": 0.1 | ||
| } | ||
| }, | ||
| { | ||
| "rule_id": "check_goldens_summary_report", | ||
| "validator": "GOLDENS_CHECK", | ||
| "params": { | ||
| "golden_files": "../../../../golden_data/golden_summary_report.csv" | ||
| } | ||
| }, | ||
| { | ||
| "rule_id": "Check_goldens_output_csv", | ||
|
saanikaaa marked this conversation as resolved.
Outdated
|
||
| "description": "Verifies the generated output CSV data matches established critical golden records", | ||
| "validator": "GOLDENS_CHECK", | ||
| "params": { | ||
| "golden_files": "../../../../golden_data/golden_observations.csv", | ||
| "input_files": "../../../../output/oecd_wastewatertreatment_merged.csv" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Utility script to check and retain missing historical data points. | ||
|
|
||
| This script compares a current output CSV with an older historical CSV. It | ||
| identifies rows in the historical file that are still missing from the current | ||
| output file based on specific composite keys and generates a unified | ||
| concatenated CSV. It safely accesses cloud storage locations using internal utilities. | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| # Dynamically find the path relative to this script's location | ||
| _SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
| # Navigate relative to tools/statvar_importer/ to find the shared util directory | ||
| _UTIL_PATH = os.path.abspath(os.path.join(_SCRIPT_DIR, '../../util/')) | ||
|
|
||
| if _UTIL_PATH not in sys.path: | ||
| sys.path.append(_UTIL_PATH) | ||
|
|
||
| try: | ||
| import file_util | ||
| except ModuleNotFoundError: | ||
| # Fallback logic to check alternative root folder configurations | ||
| _ALT_UTIL_PATH = os.path.abspath(os.path.join(_SCRIPT_DIR, '../../../util/')) | ||
| if _ALT_UTIL_PATH not in sys.path: | ||
| sys.path.append(_ALT_UTIL_PATH) | ||
| import file_util | ||
|
|
||
| from absl import app | ||
| from absl import flags | ||
| from absl import logging | ||
| import pandas as pd | ||
|
|
||
| FLAGS = flags.FLAGS | ||
|
|
||
| flags.DEFINE_string( | ||
| 'current_output', | ||
| None, | ||
| 'Path to the latest current output CSV file.', | ||
| ) | ||
| flags.DEFINE_string( | ||
| 'historical_output', | ||
| None, | ||
| 'Path to the historical retention CSV file (supports gs:// or storage.mtls paths).', | ||
| ) | ||
| flags.DEFINE_string( | ||
| 'output_file', | ||
| 'final_combined_output.csv', | ||
| 'Path to save the generated unified output file.', | ||
| ) | ||
|
|
||
|
|
||
| def _read_csv(file_path: str) -> pd.DataFrame: | ||
| """Reads a CSV into a DataFrame, converting HTTP storage links to gs:// paths.""" | ||
| # Convert browser mTLS links to gs:// URI syntax if passed to the flag | ||
| mtls_prefix = 'https://storage.mtls.cloud.google.com/' | ||
| if file_path.startswith(mtls_prefix): | ||
| file_path = 'gs://' + file_path[len(mtls_prefix):] | ||
| logging.info('Normalized mTLS URL to native path: %s', file_path) | ||
|
|
||
| if file_path.startswith('gs://'): | ||
| with file_util.FileIO(file_path, 'r') as f: | ||
| return pd.read_csv(f) | ||
| return pd.read_csv(file_path) | ||
|
|
||
|
|
||
| def process_and_reconcile_data(current_path: str, historical_path: str, | ||
| output_path: str) -> None: | ||
| """Compares historical data against current data and retains missing records. | ||
|
|
||
| Args: | ||
| current_path: Path to the current output data file. | ||
| historical_path: Path to the historical tracking data file. | ||
| output_path: Target path where the merged CSV file should be saved. | ||
| """ | ||
| logging.info('Loading input CSV datasets...') | ||
| try: | ||
| current_df = _read_csv(current_path) | ||
| historical_df = _read_csv(historical_path) | ||
| except Exception as e: | ||
| logging.fatal('Failed to read input files: %s', e) | ||
| sys.exit(1) | ||
|
|
||
| # Define the columns that serve as the unique identifier/key for each data point | ||
| keys = [ | ||
| 'observationAbout', | ||
| 'observationDate', | ||
| 'variableMeasured', | ||
| 'unit', | ||
| 'scalingFactor', | ||
| ] | ||
|
|
||
| logging.info('Generating series footprints for alignment validation...') | ||
|
|
||
| # Extract only the key columns from the current CSV and drop any duplicates | ||
| current_keys = current_df[keys].drop_duplicates() | ||
|
saanikaaa marked this conversation as resolved.
Outdated
|
||
|
|
||
| # Perform a left merge from the historical data onto the current keys | ||
| merged = historical_df.merge(current_keys, on=keys, how='left', indicator=True) | ||
|
saanikaaa marked this conversation as resolved.
Outdated
|
||
|
|
||
| # Keep only the rows from the historical CSV that DO NOT exist in the current CSV | ||
| historical_only = historical_df[merged['_merge'] == 'left_only'].copy() | ||
|
saanikaaa marked this conversation as resolved.
Outdated
|
||
|
|
||
| if historical_only.empty: | ||
| logging.info('All historical data points are already active in current.') | ||
| final_df = current_df | ||
| else: | ||
| logging.info( | ||
| 'Retaining %d records from the historical framework.', | ||
| len(historical_only), | ||
| ) | ||
| # Combine all data points from current CSV with the unique historical entries | ||
| final_df = pd.concat([current_df, historical_only], ignore_index=True) | ||
|
|
||
| try: | ||
| final_df.to_csv(output_path, index=False, encoding='utf-8') | ||
| logging.info('Successfully saved unified dataset to: %s', output_path) | ||
|
saanikaaa marked this conversation as resolved.
|
||
|
|
||
| print('=' * 80) | ||
|
saanikaaa marked this conversation as resolved.
Outdated
|
||
| print(f'Current Base Records Processed : {len(current_df)}') | ||
| print(f'Historical Points Re-inserted : {len(historical_only)}') | ||
| print(f'Total Unified Series Exported : {len(final_df)}') | ||
| print(f'Target File Location : {output_path}') | ||
| print('=' * 80) | ||
| except Exception as e: | ||
| logging.fatal('Failed to write reconciliation dataset matrix: %s', e) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def main(_): | ||
| flags.mark_flag_as_required('current_output') | ||
| flags.mark_flag_as_required('historical_output') | ||
| process_and_reconcile_data( | ||
| FLAGS.current_output, FLAGS.historical_output, FLAGS.output_file | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| app.run(main) | ||
Oops, something went wrong.
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.