@datagrok-libraries/sci-comp
v0.6.0
Published
Pure TypeScript library for numerical methods
Readme
Sci Comp
Pure TypeScript library of numerical methods for the Datagrok platform.
Modules
Optimization:
- single-objective (detailed docs):
- Nelder-Mead - derivative-free simplex method
- PSO (Particle Swarm Optimization) - stochastic population-based method
- Gradient Descent - first-order method with numerical gradients, momentum, and learning rate decay
- Adam - adaptive moment estimation with per-parameter learning rates
- L-BFGS - limited-memory quasi-Newton method with two-loop recursion and Armijo line search
- L-BFGS-B - limited-memory BFGS with native box constraints, Moré–Thuente strong-Wolfe line search, compact representation, and Morales–Nocedal 2011 subspace refinement
- multi-objective:
- single-objective (detailed docs):
- extraction of 45 features per value column (statistics, trend, complexity, threshold-based metrics)
Statistics (detailed docs):
- two-group comparison: Welch's t-test, Mann-Whitney U (exact + asymptotic), Hedges' g effect size
- rank correlation: Spearman ρ, severity-trend wrapper
- multi-group vs control: Welch pairwise + Bonferroni, Dunnett's many-to-one
- trend tests: Jonckheere-Terpstra (continuous), Cochran-Armitage (incidence, with Buonaccorsi-modified variant)
- sequential threshold test for proportions (Young 1985)
- categorical: Fisher 2×2 exact (two-sided minlike + one-sided + odds ratio), Boschloo's unconditional exact (uniformly more powerful)
- dose-response: PAVA isotonic regression, Williams step-down with 1971/1972 critical-value tables
- covariate-adjusted analysis: ANCOVA (LS means, slope homogeneity, effect decomposition)
NCA (Non-Compartmental Analysis):
- 8 PK parameters: Cmax, Tmax, AUClast, AUCinf, %AUCextrap, λz, t½, CL, Vz
- 3 AUC methods (linear, log-linear, linear-up/log-down) × naive Float64 + Neumaier-compensated summation
- 4 BLQ-handling rules × 4 phases
- λz auto best-fit (subset search by adjusted R²) + manual
- IV bolus
c0back-extrapolation (logslope/c1/cmin/set0chain), extravascular pre-dose insertion - validated against PKNCA on 26 reference profiles (theophylline, indomethacin, synthetic rat)
Installation
npm install @datagrok-libraries/sci-compQuick start
Optimization
Minimize the Rosenbrock function with Nelder-Mead:
import {singleObjective} from '@datagrok-libraries/sci-comp';
const rosenbrock = (x: Float64Array): number => {
let sum = 0;
for (let i = 0; i < x.length - 1; i++)
sum += 100 * (x[i + 1] - x[i] ** 2) ** 2 + (1 - x[i]) ** 2;
return sum;
};
const nm = new singleObjective.NelderMead();
const result = nm.minimize(rosenbrock, new Float64Array([-1.2, 1.0]), {
maxIterations: 5_000,
tolerance: 1e-12,
});
// result.point ≈ [1, 1], result.value ≈ 0See single-objective optimization docs for
- constrained optimization
- implemented methods
- sync and async objectives
- callbacks
- registry
- benchmarks
- examples
Time-series feature extraction
Extract tsfresh-compatible features from time-series data:
import {timeSeries} from '@datagrok-libraries/sci-comp';
const df: timeSeries.featureExtraction.TimeSeriesDataFrame = {
ids: new Uint32Array([1, 1, 1, 1, 1]),
time: new Float64Array([0, 1, 2, 3, 4]),
rowCount: 5,
columns: [{
name: 'temperature',
data: new Float64Array([20.1, 20.5, 21.0, 20.8, 21.3]),
}],
};
const result = timeSeries.featureExtraction.extractFeatures(df);
// result.nSamples = 1, result.columns.length = 45
// e.g. temperature__mean, temperature__standard_deviation, temperature__linear_trend__attr_slope, ...See feature extraction docs for
- full feature list (45 features across 11 categories)
- input/output format
- multiple samples and multiple columns
- validation
- naming convention
- examples
Statistics
Run Welch's t-test on two samples with NaN handling:
import {stats} from '@datagrok-libraries/sci-comp';
const a = [69, 70, 66, 63, 68, 70, 69, 67, 62, 63];
const b = [68, 62, 67, 68, 69, 67, 61, 59, 62, 61];
const r = stats.welchTTest(a, b);
// r.statistic ≈ 1.511, r.pValue ≈ 0.149Or compare each treated group to a control with Dunnett's family-wise-error-controlled test:
const control = [7.40, 8.50, 7.20, 8.24, 9.84, 8.32];
const treated = [
{doseLevel: 1, values: [9.76, 8.80, 7.68, 9.36]},
{doseLevel: 2, values: [12.80, 9.68, 12.16, 9.20, 10.55]},
];
const dun = stats.dunnettPairwise(control, treated);
// dun[1].pValueAdj ≈ 0.0058 → drug B differs from control at α = 0.05See statistics docs for
- full method list (14 tests across 8 domains)
- input types (
number[],Float32Array,Float64Array,Int32Array, …) - NaN handling (NaN as missing-value sentinel, stripped per-method)
- worked examples reproducing published references (Dunnett 1955, NIST Iris, Williams 1971/1972, Young 1985, Montgomery 15.10 vs SAS PROC GLM)
- validation against scipy via JSON fixtures (179 cases, 14 fixture files)
NCA
Run the full Non-Compartmental Analysis pipeline on one PK profile:
import {nca} from '@datagrok-libraries/sci-comp';
const inputs: nca.ProfileInputs = {
time: new Float64Array([0, 0.25, 0.5, 1, 2, 4, 8, 12]),
conc: new Float64Array([0, 1.5, 2.4, 3.0, 1.8, 0.9, 0.3, 0.1]),
blqMask: new Uint8Array(8),
lloq: 0.05,
dose: 2.5,
doseUnits: 'mg',
concentrationUnits: 'mg/L',
timeUnits: 'h',
route: nca.ROUTE_PO,
infusionDuration: null,
bodyWeight: null,
};
const rules: nca.NcaRules = {
aucMethod: 'linear-up-log-down',
blq: {
preFirstMeasurable: 'set-zero', embedded: 'set-zero',
afterLast: 'set-zero', consecutiveAfterLast: 'set-zero',
},
lambdaZ: {
mode: 'auto-best-fit',
minPoints: 3, minRSquared: 0.85, excludeCmax: true,
adjRSquaredFactor: 1e-4,
},
extrapWarnPct: 20, extrapErrorPct: 50,
compensatedSummation: false,
};
const result = nca.computeNca(inputs, rules);
// result.values: Cmax, Tmax, AUClast, AUCinf, lambdaZ, halfLife, cl, vz, pctExtrap
// result.provenance: lambda_z fit details, BLQ trace, AUC method, warnings
// result.status: 'ok' | 'partial' | 'failed'See NCA docs for
- algorithm details (BLQ phasing, lambda_z best-fit search, IV bolus c0 back-extrapolation)
- parameter contract (
ProfileInputs,NcaRules,ComputeResult) - validation against fixtures (26 profiles across 3 datasets, all within §9.2 tolerances)
