Skip to content

Add tests for reduce_dimensions() - #36

Open
ck37 wants to merge 1 commit into
masterfrom
claude/test-reduce-dimensions
Open

Add tests for reduce_dimensions()#36
ck37 wants to merge 1 commit into
masterfrom
claude/test-reduce-dimensions

Conversation

@ck37

@ck37 ck37 commented Sep 14, 2026

Copy link
Copy Markdown
Owner

Tests only — no behavior changes.

Why

tests/testthat/test-reduce_dimensions.R was a single line:

# TODO: need to test reduce_dimensions function.

That left the HOPACH dimension-reduction wrapper — the piece that decides which covariates every downstream model actually sees — with no coverage at all.

What this adds

6 tests / 18 assertions covering the paths callers depend on:

  • pass-through when the data is already narrower than the cutoff
  • constant-column removal from both data and newX (they have to stay aligned)
  • reduction when there are more columns than the cutoff
  • newX staying aligned with data in name and order
  • columns absent from newX filled with zeros, ordered to match data
  • max_variables = NULL disabling reduction

Two things I found while writing these

Both are asserted narrowly on purpose, and the test comments say why. Neither is fixed here — they are behavior questions, not test questions.

1. max_variables is not an upper bound. HOPACH selects the highest level of the tree holding at least max_variables clusters, so 20 columns with max_variables = 5 comes back as 9. The test asserts only that the result is narrower than the input; asserting <= max_variables would be asserting something the function does not do.

This matters beyond this function: the roxygen reads "Maximum we want to allow", and varimpact() exposes it as adjust_cutoff = 10L. Both read as a cap, so a user asking for at most 10 adjustment covariates may get more.

2. newX does not work at its own documented default of NULL. The signature is reduce_dimensions(data, newX = NULL, max_variables, verbose = FALSE), but:

reduce_dimensions(data, max_variables = 10)
#> Error in matrix(0, nrow = nrow(Wvsht), ncol = length(missing_cols)) :
#>   non-numeric matrix extent

With newX = NULL, Wvsht stays NULL, so every column of Wtsht lands in missing_cols and the fill-in at R/reduce_dimensions.R:151 calls matrix(0, nrow = nrow(NULL), ...). Inside varimpact() this never fires because newX is always supplied. I left it out rather than adding an expect_error, which would enshrine the bug. Happy to fix it in a follow-up if you want the default to work.

Verification

Green on master (34 assertions across the full suite, 0 failures) and green on the branch for #34 (18 assertions, 0 failures).

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xo3yCjZifUfAfLdHL1c1Hu


Generated by Claude Code

tests/testthat/test-reduce_dimensions.R was a single-line TODO, leaving
the HOPACH dimension-reduction wrapper - the one piece of the pipeline
that decides which covariates every downstream model sees - without any
coverage.

Adds 6 tests / 18 assertions covering the paths that matter to callers:
the pass-through when the data is already narrow enough, constant-column
removal from both frames, reduction when there are more columns than the
cutoff, newX staying aligned with data in name and order, absent newX
columns being filled with zeros in data's order, and a NULL
max_variables disabling reduction.

Two assertions are narrower than they look, and the comments say why:

- max_variables is not an upper bound. HOPACH selects the highest level
  of the tree holding at least max_variables clusters, so 20 columns
  with max_variables = 5 comes back as 9. The test asserts only that the
  result is narrower than the input, because asserting <= max_variables
  would assert something the function does not do. The roxygen ("Maximum
  we want to allow") and the varimpact() argument name adjust_cutoff
  both read as a cap, so this is worth a look separately.

- newX is not exercised at its documented default of NULL, because that
  default does not work: reduce_dimensions(data, max_variables = 10)
  fails in matrix(0, nrow = nrow(Wvsht), ...) with "non-numeric matrix
  extent", since a NULL newX reaches the missing-column fill-in with
  nrow(NULL). Left as-is rather than asserting the error, which would
  enshrine a bug.

No behavior changes - tests only.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xo3yCjZifUfAfLdHL1c1Hu
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.

2 participants