Repository navigation
[Sweden_Census_Demographic] Automation - #2354
Open
Krishnam24maheshwari wants to merge 9 commits into
Open
Krishnam24maheshwari wants to merge 9 commits into
Krishnam24maheshwari wants to merge 9 commits into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces an automated import pipeline for Sweden Census Demographic Population Statistics (1860–2025) from Statistics Sweden (SCB), including data downloading, processing, and validation scripts, alongside comprehensive tests and configuration files. The feedback suggests improving the robustness of download_demographic.py by handling potential ValueError exceptions in _parse_int_value when parsing non-numeric or null values, and wrapping the deletion of http_cache.sqlite in a try-except block to prevent failures if the file is locked.
…2024, and remove redundant metadata setting
… and parallelize run.sh
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR introduces a fully automated, end-to-end Data Commons import pipeline for the Sweden Census — Category 1: Demographic Population Statistics (
Sweden_Census_Demographic) dataset, sourced from Statistics Sweden (Statistiska centralbyrån, SCB) across 4 official PxWeb v1 REST API tables underBE0101A(Population Statistics):1860–present):1860–2024,BE0101A/BefolkningR1860N): https://api.scb.se/OV0104/v1/doris/sv/ssd/START/BE/BE0101/BE0101A/BefolkningR1860N2025–present,BE0101A/BefolkningR1860NCKM): https://api.scb.se/OV0104/v1/doris/sv/ssd/START/BE/BE0101/BE0101A/BefolkningR1860NCKM1968–present):1968–2024,BE0101A/BefolkningNy): https://api.scb.se/OV0104/v1/doris/sv/ssd/START/BE/BE0101/BE0101A/BefolkningNy2025–present,BE0101A/BefolkningCKM): https://api.scb.se/OV0104/v1/doris/sv/ssd/START/BE/BE0101/BE0101A/BefolkningCKMThe pipeline replaces legacy manual scripts with a resilient, zero-touch Python download, aggregation, and
stat_var_processor.pyworkflow that ingests population counts across 322 Swedish geographies (country/SWE, 3 NUTS1 major regions, 8 NUTS2 national areas, 21 NUTS3 counties, and 289 municipalities) and 9 marginal breakdowns (across 23 CSV sub-imports):1860–2022,426,062observations across139StatVars): Preserves 100.00% historical coverage and zero-deletion continuity (0 deleted records, 0 modified records) across all marginal groups and 322 places.2023–2025,23,010observations across139StatVars): Automatically discovered, downloaded, aggregated, and processed from the upstream SCB PxWeb REST API (7,670observations per year × 3 years).1860–2025National /1968–2025Sub-National): Produces449,072StatVarObservationnodes across 139 canonical Google3StatisticalVariables with 0 deleted records, 0 modified records, 0 missing references, and 0 lint errors.Key Changes & Features
1. Automated SCB PxWeb API Fetch & Multi-Level Rollup (
download_demographic.py)download_util.request_url(handling UTF-8 BOMutf-8-sigpayloads) to fetch1860–2024for national population inBefolkningR1860Nand1968–2024for sub-national population inBefolkningNy, and dynamically discovers2025+releases (2025,2026,2027, ...) from the active CKM endpoints (BefolkningR1860NCKMandBefolkningCKM) without code changes.Alder='tot'for<= 2024andAlder='TotSA'for2025+) directly for non-age breakdowns (population_country.csv,population_gender_country.csv, and regional marital-status/gender totals) rather than summing single-year ages, preserving historical unknown-age counts (1861–1869,1871–1875,1956–1959) and exact regional totals (1978Örebro län) with 0 modified historical observations.BefolkningR1860N/BefolkningR1860NCKMnational queries by 60-year windows,BefolkningNy/BefolkningCKMcounty single-year age queries by 5-year windows, and municipality/county total-age (Alder='tot'/'TotSA') queries by 20-year windows to stay well below SCB PxWeb's 150,000-cell limit.10 requests / 10 secondsrate limit (1.2sinter-request pacing) with built-in retry handling viadownload_util.sweden_places_resolved.csv, aggregating 21 Swedish counties (01–25) into 3 NUTS1 (SE1–SE3), 8 NUTS2 (SE11–SE33), and 21 NUTS3 (SE110–SE332) regional hierarchies alongside 290 raw municipalities (289 resolved active municipalities).0..110+and CKM open-ended100+1) into 12 canonical national age brackets (0-4 årthrough95-104 årand105+ årforBefolkningR1860N/BefolkningR1860NCKM) and 11 canonical sub-national age brackets (0-4 årthrough85-94 årand95+ årforBefolkningNy/BefolkningCKM), while ignoring CKM multi-year summary codes (100+5,100+10,5-9, etc.)...,.,-,"") to0to maintain 100% historical baseline continuity, writing all 23 wide-format CSVs atomically via.tmpfiles.2. Unified Single-Config Marginal Schema &
stat_var_processor.pyPipeline (run.sh)run.sh):stat_var_processor.pyacross all 23 CSVs with bounded concurrency (MAX_PARALLEL=4) and strict non-zero exit propagation.demographic_pvmap.csv,demographic_metadata.csv,demographic_statvar_remap.csv):demographic_metadata.csv(mapped_columns=4), a singledemographic_pvmap.csv, and a singledemographic_statvar_remap.csv.Cell:3:1,observationAbout,dcid:country/SWEalongsideregion,observationAbout,{Data}so the same PVMap seamlessly handles both the 4 national files and the 19 sub-national files.None:1860throughNone:2035) so year headers match exclusively on Row 3 (get_last_column_header_key(col_index) == None) without colliding with numeric population counts in data rows whenmapped_columns=4.139Canonical StatVars across23CSV Sub-Imports):39StatVars, 4 CSVs:population_country,population_gender_country,population_age_country,population_age_gender_country):Count_Person), Gender (Count_Person_Male,Count_Person_Female), Age (12 brackets), and Age × Gender (24 StatVars) forcountry/SWE(1860–2025;6,474SVObs).1StatVar, 4 CSVs:population_{nuts1,nuts2,nuts3,municipality}):Count_Personacross 321 regional places (1968–2025;18,618SVObs).2StatVars, 4 CSVs:population_gender_{nuts1,nuts2,nuts3,municipality}):Count_Person_Male,Count_Person_Femaleacross 321 regional places (1968–2025;37,236SVObs).4StatVars, 4 CSVs:population_maritalstatus_{nuts1,nuts2,nuts3,municipality}):Count_Person_{Unmarried,Married,Widowed,Divorced}across 321 regional places (1968–2025;74,472SVObs).8StatVars, 4 CSVs:population_maritalstatus_gender_{nuts1,nuts2,nuts3,municipality}):Count_Person_{Male,Female}_{Unmarried,Married,Widowed,Divorced}across 321 regional places (1968–2025;148,944SVObs).88StatVars, 3 CSVs:population_age_maritalstatus_gender_{nuts1,nuts2,nuts3}):Count_Person_{Age}_{Male,Female}_{Unmarried,Married,Widowed,Divorced}across 11 age brackets, 4 marital statuses, 2 genders, and 32 NUTS1–3 regions (1968–2025;163,328SVObs).13Mapped Age Brackets):Kon):män→dcs:Male,kvinnor→dcs:FemaleCivilstand):ogifta→dcs:Unmarried,gifta→dcs:Married,änkor/änklingar→dcs:Widowed,skilda→dcs:Divorced0–94 Years):0-4 år→[0 4 Years]5-14 år→[5 14 Years](remapped toCount_Person_Years5To14,Count_Person_Years5To14_Male,Count_Person_Years5To14_Femaleviademographic_statvar_remap.csv)15-24 år→[15 24 Years]25-34 år→[25 34 Years]35-44 år→[35 44 Years]45-54 år→[45 54 Years]55-64 år→[55 64 Years]65-74 år→[65 74 Years]75-84 år→[75 84 Years]85-94 år→[85 94 Years]BefolkningR1860N/BefolkningR1860NCKM):95-104 år→[95 104 Years]105+ år→[105 - Years]BefolkningNy/BefolkningCKM):95+ år→[95 - Years]sweden_places_resolved.csv(nuts/SE*andwikidataId/Q*,with2463 Åselemarked#ignore,skipindemographic_pvmap.csvfor 100% historical parity) and nationalcountry/SWE.None:1860–None:2035observationDatekeys indemographic_pvmap.csvfor zero-touch future annual releases.3. Production Orchestration & Validation (
manifest.json,validation_config.json)manifest.json: Configured for automated weekly execution (cron_schedule: "0 05 * * 1", every Monday at 05:00 UTC) across all 23 cleaned CSVs (output/*.csv) withoutput/population_nuts1.tmcf.validation_config.json: EnforcesMAX_DATE_CONSISTENTandSQL_VALIDATORfreshness (MaxDate >= '2025').Directory Structure