Consolidate duplicated input-coercion helpers into _helpers#84
Merged
Conversation
Five near-identical validation helpers were re-defined per module: _as_matrix in causation/norm/distance (strict 2D), _as_matrix in boost/stack (1D-to-column promoting), _as_point_matrix and _as_vector in boost/stack, and _as_pair in dependence/part. Each pair or triple was byte-identical modulo the name interpolated into error messages. Move one canonical copy of each into nns._helpers: _as_matrix (strict), _as_vector_or_matrix (promoting), _as_point_matrix, _as_flat_vector, and _as_pair. The promoting and flattening variants are renamed so the shared module distinguishes them from the strict ones. All error messages and validation semantics are unchanged. Deliberate variants stay local: co_moments._as_pair (permits NaN/inf), pm_matrix._as_matrix (no finite check), copula._as_matrix (>=2-column rule), and the norm/distance _as_vector helpers (bespoke messages).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Five input-validation helpers were re-defined near-identically across modules:
_as_matrix(strict 2D, finite)_helpers._as_matrix(x, name)_as_matrix(promotes 1D to column)_helpers._as_vector_or_matrix(x, name)_as_point_matrix_helpers._as_point_matrix(x, n_cols)_as_vector(flattens any shape)_helpers._as_flat_vector(x, name)_as_pair(strict, finite)_helpers._as_pair(x, y)Each group was byte-identical modulo the name interpolated into error messages. One canonical copy of each now lives in
nns._helpers; the promoting/flattening variants are renamed so the shared module distinguishes them from the strict ones. causation previously imported_as_pairfrom dependence and now imports it from_helpersdirectly.Net: −57 lines, one source of truth per validation rule.
What deliberately did NOT move
These look similar but differ behaviorally, so they stay local:
co_moments._as_pair— no finite/empty checks; co-moments accept NaN/inf by design and parity edge cases depend on itpm_matrix._as_matrix— no finite checkcopula._as_matrix— enforces ≥2 columns with copula-specific messagesnorm._as_vector/distance._as_vector— bespoke error messages (x[{i}],dist_estimate)Verification
ruff checkandmypycleantest_practical_examplescompound tests that fail on this machine fail identically on unmodified main — pre-existing local-environment sensitivity, green in CI)