Skip to content

filter: pass na_filter=False in read_weights_file to preserve literal 'NA' group values - #2076

Open
rpinho wants to merge 1 commit into
nextstrain:masterfrom
rpinho:fix-weights-file-na-filter
Open

rpinho wants to merge 1 commit into
nextstrain:masterfrom
rpinho:fix-weights-file-na-filter

Conversation

@rpinho

@rpinho rpinho commented Nov 26, 2026

Copy link
Copy Markdown

Who hits this: Anyone running augur filter --group-by country_code --group-by-weights weights.tsv when a grouping column contains literal strings parsed as missing by pandas by default, such as ISO alpha-2 country code NA (Namibia) or influenza segment NA. https://docs.nextstrain.org/en/latest/guides/bioinformatics/filtering-and-subsampling.html

Description of proposed changes

augur/io/metadata.py:read_metadata() reads TSV metadata with na_filter=False, so literal values such as NA (Namibia) or None stay strings. However, augur/filter/weights_file.py:read_weights_file() called pd.read_csv(..., dtype="string") without na_filter=False, so pandas converted NA, N/A, None, NULL, nan, and blank cells to pd.NA.

Because the metadata reader kept 'NA' as a string while the weights reader turned 'NA' into pd.NA:

  1. Explicit positive weight silently overridden: with NA=3, ZA=1, default=1 (budget 4, --subsample-seed 42), NA fell back to default=1, yielding 2:2 quotas (nam1, nam3, zaf2, zaf4) instead of 3:1 (nam1, nam3, nam4, zaf2).
  2. Explicit zero-weight exclusion silently ignored: with NA=0, ZA=1, default=1, NA fell back to default=1 (2:2 quotas) instead of excluding NA (0:4, selecting only zaf1..zaf4).
  3. Complete weights file falsely rejected: with NA=3, ZA=1 and no default row, augur filter exited with code 2 reporting 'country_code': ['NA'] as uncovered.
  4. Malformed NA/blank weight cells bypassed validation: a literal NA or blank cell in the weight column became pd.NA, bypassed the nullable-boolean mask in read_weights_file(), and crashed downstream with an unhandled ValueError: cannot convert NA to integer traceback instead of raising InvalidWeightsFile with the offending line number.

Changes

  • augur/filter/weights_file.py: pass na_filter=False to pd.read_csv(...) in read_weights_file(), matching read_metadata().
  • tests/filter/test_weights_file.py: add unit tests covering literal NA-like group tokens ("NA", "N/A", "None", "NULL", "nan", ""), explicit weights, zero-weight exclusion, complete weights files without a default, and clean InvalidWeightsFile rejection for NA/blank weight cells.

Verification

  • Verified end-to-end with augur filter --group-by country_code --group-by-weights (single-key and multi-key country_code × year, plus blank-value and default-row controls).
  • pytest tests/filter augur/filter: 68 passed (baseline: 54 passed).
  • Full pytest --no-cov: 1,559 passed (baseline: 1,545 passed), all 6/6 weighted cram functional tests passed, mypy clean (0 issues in 87 files), flake8 clean (0 diagnostics).

Checklist

  • Automated checks pass (pytest, weighted cram tests, mypy, flake8)
  • Checked changelog guidance (bug fix; maintainer can add release note if desired)
  • Added unit tests in tests/filter/test_weights_file.py
  • Checked docs (no CLI/API signature changes)

Found and written with AI agents; a separate AI referee reproduced the problem before this PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

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.

1 participant