Skip to content

Add plot= to the remaining functions; R's column names and bool factors - #163

Closed
gitRasheed wants to merge 11 commits into
rasheed/parity-cache-testsfrom
rasheed/api-polish
Closed

gitRasheed wants to merge 11 commits into
rasheed/parity-cache-testsfrom
rasheed/api-polish

Conversation

@gitRasheed

@gitRasheed gitRasheed commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

The remaining API rough edges from the audit. plot=True raised TypeError on eight compute functions that R and nns.plotting both support. nns_stack and nns_boost had confusing seed-alias and obj_fn errors. nns_reg renamed non-syntactic columns and kept bool predictors numeric where R makes them factors. Dead sampling helpers remained.

Stacked on #162; merge with a merge commit.

Changes

  • nns_part, fsd, ssd, tsd, nns_anova, nns_norm, nns_diff and nns_copula take plot: bool = False and draw with the existing plotters. Multivariate nns_reg(residual_plot=True) and nns_sd_cluster(dendrogram=True), which drew nothing, now draw, and undrawable cases warn. The default stays False, a documented divergence from R's plot = TRUE.
  • An obj_fn that returns more than one number raises R's "[obj.fn] must return one numeric value." random_seed= always applies, and two different explicit seeds raise. nns_boost(feature_importance=True) warns that the port draws nothing.
  • nns_reg keeps raw column names such as "my col" and only de-duplicates them, as R's check.names = FALSE does. Numeric class levels print as R's as.character does ("0", not "0.0"). The zero-variance R2 uses an all.equal port, so a constant fit off by 1e-12 scores 1, as in R.
  • bool predictors are factors with levels FALSE/TRUE, as R treats logicals. A univariate bool x now goes through NNS.M.reg with dummies, as in R: Point.est goes from 47.3 / 48.2 to 48.75 / 48.75.
  • prepare_factor_predictors now builds its design with nns_reg's own encoder, so the two always agree. Its dummy names follow nns_reg: V1_* for an unnamed matrix and x_* for a vector (they were X1_*), DataFrame column names are kept, and string columns without factor_levels become factors.
  • The _fast_lm fallback uses R's long-double means and sequential sums, like the native kernel. Before, nns_meboot with NNS_DISABLE_NATIVE=1 sorted tied residuals differently and gave different replicates for the same seed.
  • ncores below 1 (0, or scikit-style -1) maps to the default thread count with a DeprecationWarning instead of raising. These values worked before ncores was honoured.
  • Remove sample_int_prob_noreplace, which did not reproduce R's ProbSampleNoReplace and had no callers, the unused NumPy balancing helpers and their test, and boost's copies of two stack validators.

Numerical results for valid inputs do not change, except for bool predictors.

Tests

New tests/plotting/test_plot_flags.py, parity tests for raw names, numeric class levels and bool predictors, and invariants for the stack/boost errors, the R2 tie rule, the _fast_lm fallback against native, and ncores below 1. The parity cache encodes R 13.3 with the audit's R patches.

See #147

Update

This push fixes prepare_factor_predictors(names=...). Prediction columns used to be matched by the output prefixes, so names=['b', 'a'] swapped the values ([[25, 2.5]]), and a prefix that wasn't a training column raised ValueError. Columns now match the training columns by name, and names only sets the prefixes.

Review

GPT-6.1 Sol reviewed this slice and raised three findings. We took all three.

  • Boolean factors were encoded differently by nns_reg and prepare_factor_predictors (48.75 against 47.38; R gives 48.75). prepare_factor_predictors now uses nns_reg's encoder, and both give R's 48.75.
  • nns_diff(math.log, 5, plot=True) failed in the display grid with a math domain error. Undefined display points are drawn as gaps, and the call returns 0.200000000258.
  • The ncores=0 DeprecationWarning pointed at contextlib instead of the caller. It now names the caller, and the test asserts the filename.

Sharing the encoder goes slightly beyond what the review asked for: prepare_factor_predictors now names dummies V1_*/x_* instead of X1_*, keeps DataFrame column names and turns string columns into factors. The fix commits have not been re-reviewed.

plot=True worked on nns_reg (univariate), nns_cdf, nns_seas, nns_arma and
nns_arma_optim, but nns_part, fsd, ssd, tsd, nns_anova, nns_norm, nns_diff
and nns_copula raised TypeError for it although R has the argument and
nns.plotting already had a plotter for each. Multivariate nns_reg(plot=True,
residual_plot=True) and nns_sd_cluster(dendrogram=True) silently drew
nothing.

- Add keyword plot: bool = False to those eight functions, delegating to
  plot_nns_part / plot_fsd / plot_ssd / plot_tsd / plot_nns_anova /
  plot_nns_norm (raw and normalized panels, R's chart.type = "l") /
  plot_nns_diff / plot_nns_copula. Return values are unchanged.
- Multivariate nns_reg: residual_plot=True draws the NNS.M.reg fitted-values
  plot, plot=True a 3-D scatter for two regressors (as R's rgl branch) and a
  UserWarning for more.
- nns_sd_cluster(dendrogram=True) draws the dendrogram with scipy, as R does,
  and warns like R when fewer than two variables are clustered.
- nns_copula with more than three variables and nns_norm on unequal-length
  lists warn instead of drawing nothing.

The default stays False (no side effects), a documented divergence from
R's plot = TRUE defaults (docs/conventions.md, new Plotting section).
Numerical results do not change.
- _rrng.RRNG.sample_int_prob_noreplace had no callers and did not
  reproduce R's ProbSampleNoReplace (R revsorts the probabilities first:
  set.seed(42); sample.int(5, 3, prob = c(.1, .5, .2, .15, .05)) gives
  1 4 2 in R and 4 3 2 here). Removed rather than fixed, since nothing
  needs weighted sampling without replacement.
- categorical._down_sample_rows, _up_sample_rows, _balance_class_training
  and their _sampling_inputs helper were used only by their own test
  (tests/invariants/test_sampling.py, removed). They drew from the NumPy RNG
  in a layout that nns_stack and nns_boost no longer use.
- boost.py duplicated stack.py's _scalar_logical and _scalar_integer
  verbatim; boost now imports them from nns.stack.

No public behaviour changes.
- An obj_fn returning a vector failed deep inside with "TypeError: only
  0-dimensional arrays can be converted to Python scalars". nns_stack and
  nns_boost now raise R's "[obj.fn] must return one numeric value." for any
  result that is not a single number (a length-1 array is accepted, as R
  accepts a length-1 vector).
- random_seed= (alias of seed=) replaced seed only while seed was 123:
  seed=7, random_seed=5 silently used 7 and seed=None, random_seed=5 drew
  OS entropy. The alias now always applies, and two different explicit
  values raise ValueError (seed=123 is the default and cannot be told apart
  from an explicit 123).
- nns_boost(feature_importance=True) was silently ignored. R draws its
  learner/feature-frequency diagnostics there; the port draws nothing, so it
  now warns and points at result['feature.frequency'].

Results for valid inputs do not change. Skipped: status
printing style, the documented two-valued auto-classification, and the
seed=None stream structure (behaviour, not a bug). ncores is honoured
(caps native threads), so it is not a no-op.
- R's .nns_reg_as_frame builds the predictor frame with
  check.names = FALSE and only applies make.unique, so a column "my col"
  stays "my col" and "2nd" stays "2nd" in RPM / Fitted.xy. The port ran the
  names through make.names ("my.col", "X2nd"). Names are now only
  de-duplicated. Factor dummy names are unchanged (R make.names the levels:
  "my cyl_four").
- Numeric class levels were str(float): ['0.0', '1.0'] where R's
  as.character gives c("0", "1") (and "0.333333333333333" for 1/3). They
  now use 15 significant digits.
- The zero-variance branch of the Racine-Hastie R2 used exact equality
  where R uses isTRUE(all.equal(actual, predicted)); a constant fit off by
  1e-12 scored 0 instead of R's 1. Both _r2 helpers now use an all.equal
  port (R: f(rep(3,5), rep(3+1e-12,5)) = 1, f(rep(3,5), rep(3+1e-7,5)) = 0).

New parity tests record R from the patched NNS 13.3. Values do not change;
only result key names for non-syntactic column labels and the
class.levels strings of numeric responses do. Skipped: the
R-only location= plotting argument, and items already fixed (ncores
warning, the levels.index loop).
R's .nns_reg_encode_predictors makes a factor of every
is.factor || is.character || is.logical column. The port kept bool
columns numeric 0/1, so a univariate bool x stayed on the univariate
path (Point.est 47.3 / 48.2) where R builds the dummies x_FALSE. /
x_TRUE. and runs NNS.M.reg (48.75 / 48.75; audit api1 case).

bool columns, and object columns whose values are all booleans, are now
factors with levels FALSE/TRUE. Dummy names go through the R make.names
port that appends "." to reserved words (x_FALSE., x_TRUE.), which the
local make.names copy lacked; that copy is removed.

New parity tests (univariate bool vector, and a bool + numeric frame with
factor.2.dummy TRUE and FALSE) match R for Point.est, R2 and the RPM
names. The other predictor-typing case (numeric-looking strings)
already follows R: string columns are factors. Results change only for
bool predictors.
… kernel

The pure-Python _fast_lm used np.mean/np.sum (pairwise), while NNS-core's
fast_lm (R's Rcpp kernel) uses long-double two-pass means and sequential
sums. The intercept then differed in the last bit, tied residuals sorted
differently, and nns_meboot/nns_arma/nns_mc gave different replicates with
NNS_DISABLE_NATIVE=1 for the same seed (e.g. meboot ordxx [3,1,2] vs R's
[1,3,2]). Found by the independent review of the branch.
Before ncores was honoured it was ignored, and R's univariate paths accept
any value, so ncores=0 or scikit-style -1 now raised where they used to
work. Map them to the default thread count with a DeprecationWarning; other
invalid values (1.5, True, '2') still raise. Also pin the _fast_lm fallback
to the native kernel in a test.
It kept bool columns numeric, so direct and prepared regressions on a bool x
gave 48.75/48.75 vs 47.38/48.38; both now give R's 48.75 with x_FALSE./x_TRUE.
The display grid reaches x <= 0, so nns_diff(math.log, 5, plot=True) raised
"math domain error"; it now returns the derivative 0.200000000258 and plots.
external_stacklevel stopped at contextlib's __enter__, so the warning named
contextlib.py:141; it now names the caller's file and line.
They point at notes that don't live in this repo.
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Incorporated through NNS R 14.0 integration #192, merged as bd19c3303222af46da79d8c97f258248eb5674d2. Verified exact head f93a098c7714370098d20ae83f43e4a2ce8839fd is an ancestor of main; original commits and authorship are preserved. The integration includes the subsequent R14 compatibility corrections and passed the complete GitHub Python 3.11-3.14 matrix. This supersedes the separate landing, not the work or review history.

@github-actions github-actions Bot closed this Nov 7, 2026
@github-actions
github-actions Bot deleted the rasheed/api-polish branch November 7, 2026 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant