ecotyper.nmf¶
Port of the parts of the R package NMF (v0.28) that EcoTyper uses.
nmf(x, rank, method="brunet", seed=<int>, .opt="P1")→nmf_brunet()NMFpredict(EcoTyperlib/ecotyper.R) →nmf_predict()NMFfitX(list, .merge=TRUE),cophcor,dispersion,silhouette→merge_runs()and friends.
The multiplicative updates replicate NMF’s C++ kernels
(src/divergence.cpp) operation by operation — same accumulation order and
the fused multiply-adds clang emits for them — so that, given the same initial
matrices, W and H agree with R bit for bit.
Functions
|
|
|
|
|
Codes of |
|
|
|
|
|
|
|
|
|
Alternative solver ( |
|
EcoTyper's |
|
|
|
|
|
|
|
|
|
Run Brunet multiplicative updates from (W0, H0) with the connectivity stop. |
|
Average silhouette widths as in |
Classes
|
Result of a single NMF run ( |
|
|
- ecotyper.nmf.rnmf(rank, target, rng)[source]¶
rnmf(rank, target): W (n×r) then H (r×p) ~ U(0, max(max(target), 1)), clamped withpmin(., max(abs(target))).
- class ecotyper.nmf.NMFFit(W, H, niter, converged, deviance=nan, seed=None)[source]¶
Bases:
objectResult of a single NMF run (
NMFfit).
- ecotyper.nmf.run_brunet(V, W0, H0, *, update_w=True, max_iter=2000, exact=True, threads=1)[source]¶
Run Brunet multiplicative updates from (W0, H0) with the connectivity stop.
exact=Falseuses BLAS products (_brunet_loop_fast()) instead of the R-exact kernels.threads: OpenMP threads for the C kernel (same result for any value).
- ecotyper.nmf.fitted(W, H)[source]¶
W %*% Hvia BLAS dgemm called as R does (column-major, no transposes).
- ecotyper.nmf.nmf_brunet(V, rank, seed, *, max_iter=2000, exact=True)[source]¶
nmf(V, rank, method="brunet", seed=seed, .opt="P1")(single run).exact=False: same initialisation and stopping rule, BLAS updates (not bit-identical to R).
- ecotyper.nmf.nmf_cd(V, rank, seed, *, max_iter=2000, tol=0.0001)[source]¶
Alternative solver (
nmf_method="cd", not EcoTyper/R-equivalent): Frobenius-loss NMF by coordinate descent (scikit-learnNMF(solver="cd")), started from the samernmfinitialisation asnmf_brunet()and stopped by scikit-learn’s tolerance rule instead of the connectivity rule.devianceis the Frobenius reconstruction error (used to pick the best restart).
- ecotyper.nmf.nmf_predict(W, new_data, rng, *, max_iter=2000, exact=True, threads=1)[source]¶
EcoTyper’s
NMFpredict(W, new_data).Whas2grows (<gene>__posthen<gene>__neg) andnew_datais the (scaled)g × pmatrix already aligned to the training genes. Rows of the posneg-transformed data with zero variance are dropped from both, an initial H is drawn withrnmf(after an unused W draw, as in R), and only H is updated. Returns(H, niter, keep_rows).
- ecotyper.nmf.predict_columns(H)[source]¶
.predict.nmf(t(H)):which.max(abs(h))per column (1-based).
- ecotyper.nmf.factor_codes(values)[source]¶
Codes of
as.factor(values)for positive integers (levels sorted numerically).
- class ecotyper.nmf.NMFMerged(fit, consensus, nrun, runs_used=<factory>)[source]¶
Bases:
objectNMFfitX1: best fit + consensus matrix over restarts.