Skip to content

Validate init.hiv.prev length matches the race flag (#59)#75

Merged
smjenness merged 1 commit intomainfrom
fix/issue-59-race-false-prev
Apr 25, 2026
Merged

Validate init.hiv.prev length matches the race flag (#59)#75
smjenness merged 1 commit intomainfrom
fix/issue-59-race-false-prev

Conversation

@smjenness
Copy link
Copy Markdown
Contributor

Quick fix for #59 (open since 2025-01-09).

What was wrong

Under race = FALSE, the user expected to pass a single overall-population HIV prevalence (length 1), but the docstring said init.hiv.prev was a vector of size 3. The previous active validation was the commented-out

#if (length(init.hiv.prev) != 3) { stop("...vector of size three") }

so anything passed silently. Behavior:

Input Old behavior New behavior
length 1 + race = FALSE Worked (user's request) Works, now documented
length 3 + race = FALSE Worked (extra elements ignored) Works, preserves backward compat
length 3 + race = TRUE Worked Works
length 1 + race = TRUE Cryptic "vector size cannot be NA/NaN" from downstream Clean error: "init.hiv.prev must have length 3 ..."
length 2 + race = TRUE Same cryptic failure Clean error

What changed

  • R/EpiStats.R: @param init.hiv.prev doc spells out the length contract per race value. Replaced the commented-out length check with an active one that enforces length(init.hiv.prev) == length(race.level) when race = TRUE, and keeps length >= 1 permissive under race = FALSE (extra elements ignored, with a comment noting that the downstream sampler reads only init.hiv.prev[1]).
  • tests/testthat/test-init-hiv-prev.R (new): 5 test blocks covering each combination — valid length-1 + race=FALSE, valid length-3 + race=TRUE, clean errors for length-1 + race=TRUE and length-2 + race=TRUE, out-of-range value still rejected with the original message.

Backward-compat note

The inst/validation/ snapshot harness's atlanta_no_race scenario uses init.hiv.prev = c(0.33, 0.137, 0.084) with race = FALSE. That combination still works under the new validation (length >= 1 is permissive when race = FALSE), and the snapshot match is preserved 3/3.

Test plan

  • Backward-compat snapshot 3/3 (atlanta_default, national_no_geog, atlanta_no_race)
  • New tests pass (5/5)
  • Full testthat suite passes locally
  • R CMD check 0/0/0

Closes #59.

Issue #59 reported confusion under `race = FALSE`: the docstring said
init.hiv.prev had to be length 3 (one per race category), but the
underlying flow only used the first element when race = FALSE so a
length-1 input ought to work. The previous active validation accepted
any length silently, which led to:

  - length 1 + race = FALSE: actually worked (user's request).
  - length 1 + race = TRUE: failed downstream with the cryptic message
    "vector size cannot be NA/NaN" because init.hiv.prev[2:3] returns NA.
  - length 2 + race = TRUE: same cryptic failure.

Fix:

  - Update the @param doc on build_epistats to spell out: length 1 is
    correct under race = FALSE; length must equal length(race.level)
    (default 3) under race = TRUE.
  - Replace the commented-out length check with an active one:
    + race = TRUE requires length(init.hiv.prev) == length(race.level).
      Clear error message including the expected and observed lengths.
    + race = FALSE accepts any length >= 1; only init.hiv.prev[1] is
      used downstream. Length > 1 is permitted (extra elements ignored)
      to preserve backward compat with existing callers — notably the
      atlanta_no_race scenario in inst/validation/.

Out-of-range elements (>= 1 or <= 0) still raise the existing
"between 0 and 1 non-inclusive" error.

Validation:
- Backward-compat snapshot harness still matches 3/3 (atlanta_no_race
  uses length-3 + race = FALSE; that combination still works under
  the new validation, just no longer documented as the recommended
  shape).
- New tests in test-init-hiv-prev.R (5 blocks, 8 assertions): valid
  length-1 + race=FALSE; valid length-3 + race=TRUE; clear errors for
  length-1 + race=TRUE and length-2 + race=TRUE; out-of-range value
  still rejected.
- R CMD check: 0/0/0.

Closes #59.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@smjenness smjenness merged commit 4ac8dbb into main Apr 25, 2026
1 check passed
@smjenness smjenness deleted the fix/issue-59-race-false-prev branch April 25, 2026 17:57
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.

Is the race = FALSE flag working correctly?

1 participant