Skip to content

USFEMA_FloodInsuranceClaims: Optimize download and transformation pipeline - #2217

Open
kartik-s21 wants to merge 1 commit into
datacommonsorg:masterfrom
kartik-s21:usfema-flood-claims-optimization
Open

kartik-s21 wants to merge 1 commit into
datacommonsorg:masterfrom
kartik-s21:usfema-flood-claims-optimization

Conversation

@kartik-s21

@kartik-s21 kartik-s21 commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Updated the download and data processing pipeline for USFEMA_FloodInsuranceClaims to eliminate multi-hour runtime bottlenecks and improve error resilience.

Changes

  • Download Stage (fema_download.py):
    • Implements direct streaming from OpenFEMA's static bulk CSV endpoint (~60s download time).
    • Maintains automatic fallback to paginated OpenFEMA API retrieval with connection reuse and exponential backoff retry logic.
  • Processing Stage (process.py):
    • Replaces sequential Python iteration with a multi-process vectorized pipeline using concurrent.futures.ProcessPoolExecutor and multiprocessing.get_context('spawn').
    • Fixes missing leading zeroes for FIPS codes < 10 (e.g. California, Connecticut, Alabama) preserving 100% of sub-state county and tract places.
    • Dynamically sizes chunks across available CPU cores and removes the 32-worker cap.
    • Enforces atomic output publishing (.tmp staging with os.replace) for .csv, .tmcf, .mcf, and counters.
    • Replaces eval() with ast.literal_eval() for safe mapping parsing.
    • Ensures pipeline failures propagate non-zero exit codes when aggregations are empty.
  • Manifest & Documentation (manifest.json, README.md):
    • Updates node_mcf to "output/*.mcf" and includes "manifest.json" in source_files.
    • Expands README.md with prerequisites, layout, CLI documentation, and testing steps.
  • Validation & Testing (process_test.py, fema_download_test.py):
    • Adds 20 comprehensive unit tests covering bulk/paginated downloads, numerical aggregation sums, unexpected flood zone mappings, and unpadded FIPS parsing.
    • Cloud Batch Job run (test env) : link
    • Validation Output : link
  • Reports:
    • CRA Report : link
    • Postmortem Report : link
    • Import Diagnostics & Performance Optimization Report : link

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request re-engineers the FEMA flood insurance claims import pipeline, replacing a slow, sequential, row-by-row legacy processor with a highly optimized, vectorized, multi-process chunked pipeline. It introduces direct bulk CSV downloading with paginated API fallback, updates the manifest and documentation, and adds comprehensive unit tests. Feedback suggests further optimizing the generation of StatVar names in process.py by replacing .apply() with .map() using pre-computed unique flood zone names to avoid Python function call overhead when generating millions of observations.

Comment thread statvar_imports/fema/flood_insurance_claims/process.py Outdated
@kartik-s21
kartik-s21 force-pushed the usfema-flood-claims-optimization branch 2 times, most recently from 4d7f6d6 to 56746b1 Compare September 10, 2026 10:15
@kartik-s21
kartik-s21 force-pushed the usfema-flood-claims-optimization branch 4 times, most recently from 58c6d1a to 3848d33 Compare September 11, 2026 10:57
"source_files": [
"input_file/fema_nfip_claims.csv"
"input_file/fema_nfip_claims.csv",
"manifest.json",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we are not adding manifest.json

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed.

@kartik-s21
kartik-s21 force-pushed the usfema-flood-claims-optimization branch from c6a8334 to 7bb1d11 Compare September 11, 2026 12:15

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

add counters

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added.

…ipeline

- Re-engineer download pipeline with direct bulk CSV streaming and API pagination fallback.
- Replace sequential row iteration in process.py with multi-process vectorized pipeline.
- Use concurrent.futures.ProcessPoolExecutor with spawn context for safe multiprocessing.
- Add FIPS zero-padding for tract and county places to prevent data loss for states 01-09.
- Enforce atomic publishing for observations, MCF, TMCF, and counters.
- Improve error handling with non-zero exit on empty aggregations.
- Replace eval() with ast.literal_eval() for secure mapping parsing.
- Update manifest.json node_mcf pattern and source_files.
- Expand README documentation and add comprehensive unit test suite.
@kartik-s21
kartik-s21 force-pushed the usfema-flood-claims-optimization branch from 7bb1d11 to 0c411ef Compare September 11, 2026 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants