Skip to content

Fix NNS.norm unequal-length list detection and variable assignment#37

Merged
OVVO-Financial merged 1 commit into
NNS-Beta-Versionfrom
claude/book-python-code-integration-xijzxt
Jul 5, 2026
Merged

Fix NNS.norm unequal-length list detection and variable assignment#37
OVVO-Financial merged 1 commit into
NNS-Beta-Versionfrom
claude/book-python-code-integration-xijzxt

Conversation

@OVVO-Financial

Copy link
Copy Markdown
Owner

Summary

This PR fixes bugs in the NNS.norm function related to detecting unequal-length lists and properly assigning variables in the nonlinear normalization path. It also adds comprehensive test coverage for the normalization function.

Key Changes

  • Fixed unequal-length list detection: Changed from sum(diff(sapply(X, length))) != 0 to length(unique(sapply(X, length))) > 1 to correctly identify when list elements have different lengths. The original approach could miss cases where length differences sum to zero (e.g., +1, -1).

  • Fixed variable assignment in nonlinear path: Added explicit assignment of X_mat when converting list input to matrix form. Previously, the result of do.call(cbind, X) or X was not being assigned, causing subsequent cor() and NNS.dep() calls to operate on the wrong data.

  • Improved column name handling: Updated the logic for extracting base column names to handle both list and matrix inputs correctly, using names(X) for lists and colnames(X) for matrices.

  • Code cleanup: Fixed trailing whitespace and formatting inconsistencies.

Test Coverage

Added comprehensive test suite (test_Normalization.R) covering:

  • Linear normalization with mean equalization
  • Nonlinear normalization returning finite values
  • Equal-length list equivalence to matrix input
  • Unequal-length list handling with linear scaling
  • Edge case: zero-sum length differences still detected as unequal

https://claude.ai/code/session_01JMQ9tSZyAMwerv7BdY6E5w

Two defects in the list branch of NNS.norm:

1. The nonlinear scale-factor step discarded the cbind result
   ('do.call(cbind, X)' was never assigned), so cor()/NNS.dep() received
   the raw list and errored. Equal-length lists therefore only worked
   with linear = TRUE. Assign the bound matrix and use it for the scale
   factor, so equal-length lists now support the nonlinear path and
   match the equivalent matrix call.

2. Unequal-length detection used sum(diff(lengths)) != 0, which misses
   patterns whose diffs cancel (e.g. lengths 5, 6, 5) and let them fall
   through to cbind recycling. Use length(unique(lengths)) > 1 instead.

Also label the matrix result from list input via names(X) (colnames() on
a list is NULL, which previously stripped the ' Normalized' labels).

Adds tests/testthat/test_Normalization.R covering the matrix path, the
equal-length list path against the matrix result, forced linear scaling
for unequal lengths, and the zero-sum length-diff case.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JMQ9tSZyAMwerv7BdY6E5w
@OVVO-Financial OVVO-Financial merged commit ef1e89b into NNS-Beta-Version Jul 5, 2026
7 checks passed
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