Nothing
library(iSTATS)
app_functions <- system.file("app", "functions.R", package = "iSTATS")
stopifnot(nzchar(app_functions))
helpers <- new.env(parent = globalenv())
sys.source(app_functions, envir = helpers)
set.seed(42)
x <- matrix(rnorm(120), 12, 10)
x[, 2] <- -x[, 1]
cases <- list(
random = x,
ties = round(x),
single = x[, 1, drop = FALSE],
one_sample = x[1, , drop = FALSE],
constant = cbind(x, 1),
missing = replace(x, c(1, 20), NA_real_)
)
for (m in cases) {
for (method in c("pearson", "spearman")) {
reference <- suppressWarnings(stats::cor(m, method = method))
for (driver in seq_len(ncol(m))) {
actual <- suppressWarnings(helpers$driver_correlations(m, driver)[[method]])
stopifnot(isTRUE(all.equal(actual, unname(reference[driver, ]),
tolerance = 1e-14)))
}
}
}
# The vectorized Shapiro-Wilk and rank helpers must reproduce one
# shapiro.test()/rank() call per column: small n branches, ties, constant and
# tiny-range columns, missing/infinite values and invalid sample sizes.
#
# The ranks are compared exactly: rank() only compares and averages, so every
# platform returns the same doubles. The p-values are not compared exactly.
# shapiro.test() computes in C, where the compiler may contract a * b + c into
# a single fused multiply-add, while the R code here never contracts; on
# aarch64 that contraction is the compiler default. The difference is one unit
# in the last place, but w1 = (A - B) * (A + B) / (ssa * ssx) amplifies it,
# because A - B cancels for a near-normal sample. The p-values are therefore
# required to agree to 1e-6 relative: orders of magnitude looser than that
# arithmetic noise, and orders of magnitude tighter than any drift between the
# two implementations of the algorithm would produce. The pattern of NA and
# NaN must still match exactly.
reference_p <- function(m) vapply(seq_len(ncol(m)), function(h)
tryCatch(stats::shapiro.test(m[, h])$p.value, error = function(e) NA_real_), 0)
same_p_values <- function(actual, expected)
length(actual) == length(expected) &&
identical(is.na(actual), is.na(expected)) &&
identical(is.nan(actual), is.nan(expected)) &&
all(abs(actual - expected) <= 1e-12 |
abs(actual - expected) <= 1e-6 * abs(expected), na.rm = TRUE)
reference_ranks <- function(m)
if (nrow(m) > 1L) apply(m, 2L, rank, na.last = "keep") else row(m)
shapiro_cases <- list(matrix(rnorm(2 * 4), 2), matrix(rnorm(5001 * 2), 5001))
for (n in c(3:12, 20, 52)) {
y <- matrix(rexp(n * 40), n)
y[, 2] <- round(y[, 2])
y[, 3] <- 1
y[, 4] <- y[, 4] * 1e-12 + 5
y[, 5] <- c(rep(0, n - 1), 1)
y[, 6] <- -0
shapiro_cases[[length(shapiro_cases) + 1L]] <- y
}
y <- matrix(rnorm(52 * 8), 52, dimnames = list(NULL, paste0("c", 1:8)))
y[1, 1] <- NA; y[2, 2] <- Inf; y[, 3] <- NaN; y[1:50, 4] <- NA
shapiro_cases[[length(shapiro_cases) + 1L]] <- y
default_blocks <- helpers$.column_blocks
for (block_size in c(8192L, 3L)) { # one block, and many uneven blocks
helpers$.column_blocks <- function(columns, size = block_size)
default_blocks(columns, size)
for (m in c(cases, shapiro_cases)) {
stopifnot(same_p_values(helpers$.shapiro_p_values(m), reference_p(m)),
identical(helpers$.spearman_ranks(m), reference_ranks(m)))
}
}
helpers$.column_blocks <- default_blocks
# The vectorized STOCSY recoloring must reproduce the legacy per-point loops
# (D11) exactly. The reference below is the loop body as it stood before
# front 20.
legacy_positions <- function(points, selected)
unname(split(seq_along(selected), factor(selected, levels = seq_len(points))))
legacy_standard <- function(colors, points, selected, values, p, n = NULL,
unselected = "B") {
at <- legacy_positions(points, selected)
suppressWarnings(for (k in seq_len(points)) {
if (length(at[[k]]) > 0L) {
z <- at[[k]]
if (isTRUE(values[z] >= p)) colors[k] <- "A" else colors[k] <- "B"
if (!is.null(n) && isTRUE(values[z] <= n)) colors[k] <- "C"
} else if (!is.null(unselected)) colors[k] <- unselected
})
colors
}
local({
set.seed(20)
for (case in seq_len(400L)) {
points <- sample(c(1L, 2L, 7L, 30L), 1L)
selected <- sort(sample(0:(points + 1L), sample(0:(points + 2L), 1L),
replace = TRUE))
if (case %% 3L == 0L) selected <- unique(selected[selected >= 1L & selected <= points])
size <- length(selected) + sample(-1:1, 1L)
pearson <- round(runif(max(0L, size), -1, 1), 1)
spearman <- round(runif(max(0L, size), -1, 1), 1)
pearson[sample(length(pearson), length(pearson) %/% 5L)] <- NA
previous <- switch(sample(4L, 1L), NULL, character(),
sample(c("A", "B", "C"), max(1L, points %/% 2L), TRUE),
sample(c("A", "B", "C"), points + 3L, TRUE))
p <- switch(sample(5L, 1L), 0.5, 0.0, NULL, c(0.2, 0.8), NA_real_)
n <- sample(c(-0.5, -0.1), 1L)
for (unselected in list("B", NULL)) {
stopifnot(
identical(legacy_standard(previous, points, selected, pearson, p,
n = n, unselected = unselected),
helpers$.stocsy_classes(previous, points, selected,
pearson, p, n, unselected)),
identical(legacy_standard(previous, points, selected, spearman, p,
unselected = unselected),
helpers$.stocsy_classes(previous, points, selected,
spearman, p, NULL, unselected)))
}
}
})
# norm_test counts as normal the columns Shapiro does not reject (p > 0.05)
# and marks them green (rr_nn); rejected columns go red (rr_n); D61.
local({
set.seed(7)
m <- cbind(normal = rnorm(40), skewed = rexp(40)^3, constant = 1)
p <- helpers$.shapiro_p_values(m)
stopifnot(p[1L] > 0.05, p[2L] <= 0.05, is.na(p[3L]))
helpers$col_select <- c(11L, 12L, 13L)
helpers$CS_values_real <- matrix(seq(10, 0.5, length.out = 20), 1)
helpers$rr_n <- new.env(); helpers$rr_nn <- new.env()
helpers$stop_menssager <- 1
helpers$norm_var <- helpers$dif <- NULL
helpers$col_sel_norm <- helpers$col_sel_norm2 <- NULL
helpers$norm_test(0.05, m)
stopifnot(identical(helpers$norm_var, c("YES", "NO", "UNAVAILABLE")),
helpers$dif == round(100 / 3, 2),
identical(helpers$col_sel_norm, 12L),
identical(helpers$col_sel_norm2, 11L),
identical(helpers$rr_n$csranges, helpers$CS_values_real[1, 12L]),
identical(helpers$rr_nn$csranges, helpers$CS_values_real[1, 11L]))
})
# Ranks computed once must reproduce cor(method = "spearman") bit for bit.
for (m in cases) {
ranks <- helpers$.spearman_ranks(m)
for (driver in seq_len(ncol(m))) {
stopifnot(identical(
suppressWarnings(helpers$driver_correlations(m, driver, ranks)),
suppressWarnings(helpers$driver_correlations(m, driver))))
}
}
helpers$col_select <- seq_len(ncol(x))
helpers$.start_stocsy(x)
stopifnot(identical(helpers$.stocsy_ranks, helpers$.spearman_ranks(x)))
helpers$.update_stocsy_peak(2L)
stopifnot(isTRUE(all.equal(helpers$cor_pearson, unname(stats::cor(x)[2, ]))),
identical(helpers$cor_spearman,
as.vector(replace(stats::cor(x[, 2], x, method = "spearman"),
2L, 1))))
helpers$.clear_stocsy()
stopifnot(is.null(helpers$.stocsy_data), is.null(helpers$.stocsy_ranks),
length(helpers$cor_pearson) == 0L,
length(helpers$cor_spearman) == 0L)
axis <- seq(1, -1, length.out = 64)
intervals <- helpers$.parse_icoshift_intervals("-0.8, 0.1\n0.2; 0.8", axis)
stopifnot(identical(unname(intervals), matrix(c(-0.8, 0.1, 0.2, 0.8), 2,
byrow = TRUE)))
pending <- helpers$.parse_icoshift_interval_text(
"-0.75 (-0.8), 0.1\n0.2; 0.85 (0.8)", axis)
stopifnot(identical(unname(pending$intervals),
matrix(c(-0.75, 0.1, 0.2, 0.85), 2, byrow = TRUE)),
identical(unname(pending$references),
matrix(c(-0.8, NA, NA, 0.8), 2, byrow = TRUE)),
identical(helpers$.format_icoshift_pending_intervals(
pending$intervals, pending$references),
"-0.75 (-0.8), 0.1\n0.2, 0.85 (0.8)"))
applied <- matrix(c(-0.8, 0.1, 0.2, 0.8), 2, byrow = TRUE)
edited <- matrix(c(-0.75, 0.1, 0.2, 0.85), 2, byrow = TRUE)
refs <- helpers$.reconcile_icoshift_interval_references(applied, applied, edited)
stopifnot(identical(refs, applied),
identical(helpers$.reconcile_icoshift_interval_references(
rbind(applied, c(-0.1, 0)), rbind(applied, c(NA, NA)), applied), applied))
comparison <- helpers$.icoshift_interval_comparison_table(edited, refs)
stopifnot(identical(names(comparison), c(
"Region", "Original position (ppm)", "New position (ppm)")),
identical(comparison[["Original position (ppm)"]],
c("-0.8, 0.1", "0.2, 0.8")),
identical(comparison[["New position (ppm)"]],
c("-0.75, 0.1", "0.2, 0.85")))
visible_applied <- helpers$.icoshift_visible_applied_intervals(
edited, refs, rbind(applied, c(-0.1, 0)))
stopifnot(identical(unname(visible_applied), matrix(c(-0.1, 0), 1L)))
bad_intervals <- c("", "not-a-pair", "0.2 0.2", "-2 0")
stopifnot(all(vapply(bad_intervals, function(value) {
inherits(try(helpers$.parse_icoshift_intervals(value, axis), silent = TRUE),
"try-error")
}, logical(1))))
upload <- data.frame(name = c("sample10.csv", "sample2.csv"), value = 1:2)
ordered <- helpers$.order_uploaded_spectra(upload, list("ten", "two"))
stopifnot(identical(as.character(ordered$upload$name),
c("sample2.csv", "sample10.csv")),
identical(unname(unlist(ordered$spectra)), c("two", "ten")))
discontinuous_axes <- rbind(
c(5, 4, 3, 2, 1),
c(5, 4, 3, 4, 5)
)
discontinuous_data <- matrix(seq_len(10), nrow = 2L, byrow = TRUE)
trimmed <- helpers$.trim_trailing_axis_discontinuity(
discontinuous_data, discontinuous_axes)
stopifnot(trimmed$removed == 2L,
identical(trimmed$data, discontinuous_data[, 1:3, drop = FALSE]),
identical(trimmed$chemical_shifts,
discontinuous_axes[, 1:3, drop = FALSE]))
continuous <- helpers$.trim_trailing_axis_discontinuity(
discontinuous_data, rbind(5:1, 10:6))
stopifnot(continuous$removed == 0L,
identical(continuous$data, discontinuous_data))
spectra <- matrix(1:12, 3, 4, dimnames = list(NULL, paste0("old", 1:4)))
exported <- helpers$.selected_points_export(
spectra, c(4L, 2L), c(4.1, 3.2, 2.3, 1.4), c("A", "B", "C"))
stopifnot(identical(exported$Sample, c("A", "B", "C")),
identical(names(exported), c("Sample", "3.2", "1.4")),
isTRUE(all.equal(as.matrix(exported[-1L]),
spectra[, c(2L, 4L), drop = FALSE],
check.attributes = FALSE)))
peak <- exp(-((seq_len(64) - 32) / 4)^2)
data <- rbind(left = c(0, peak[-64]), reference = peak,
right = c(peak[-1], 0))
choices <- helpers$.alignment_reference_choices(
c("duplicate", "middle", "duplicate"), nrow(data))
stopifnot(identical(unname(choices), c("average", "1", "2", "3")),
identical(names(choices),
c("Average spectrum", "duplicate", "middle", "duplicate")))
average_target <- helpers$.resolve_icoshift_target(
data, "average", sample_names = NULL)
first_target <- helpers$.resolve_icoshift_target(
data, "1", c("duplicate", "middle", "duplicate"))
last_target <- helpers$.resolve_icoshift_target(
data, "3", c("duplicate", "middle", "duplicate"))
stopifnot(identical(average_target$target, "average"),
identical(first_target$target, as.numeric(data[1L, ])),
identical(last_target$target, as.numeric(data[3L, ])),
identical(first_target$label, "duplicate (sample 1)"),
identical(last_target$label, "duplicate (sample 3)"))
bad_references <- list(NULL, "", "missing", "0", "4", c("1", "2"))
stopifnot(all(vapply(bad_references, function(reference) {
inherits(try(helpers$.resolve_icoshift_target(data, reference), silent = TRUE),
"try-error")
}, logical(1))))
for (values in list(c(2, 5), c(-5, -2), c(-3, 4), rep(7, 3),
c(1e-300, 2e-300), c(NA_real_, Inf, -Inf))) {
limits <- helpers$.stocsy_ylim(values)
finite_values <- values[is.finite(values)]
data_min <- if (length(finite_values)) min(finite_values) else 0
stopifnot(length(limits) == 2L, all(is.finite(limits)), limits[1L] < limits[2L],
isTRUE(all.equal((data_min - limits[1L]) / diff(limits), 0.10,
tolerance = 1e-9)))
data_span <- if (length(finite_values)) diff(range(finite_values)) else 0
if (is.finite(data_span) &&
data_span > .Machine$double.eps *
max(1, abs(data_min), abs(max(finite_values, 0))))
stopifnot(isTRUE(all.equal((limits[2L] - max(finite_values)) / diff(limits),
0.03, tolerance = 1e-9)))
}
fit <- suppressMessages(helpers$.run_icoshift_alignment(
data, axis, matrix(c(-0.6, 0.6), nrow = 1L)))
sample_fit <- suppressMessages(helpers$.run_icoshift_alignment(
data, axis, matrix(c(-0.6, 0.6), nrow = 1L), target = data[1L, ]))
seen_target <- NULL
alignment_spy <- function(xT, xP, ...) {
seen_target <<- xT
list(xCS = xP, ind = matrix(0L, nrow(xP), 1L))
}
invisible(helpers$.run_icoshift_alignment(
data, axis, matrix(c(-0.6, 0.6), nrow = 1L), aligner = alignment_spy))
stopifnot(identical(seen_target, "average"))
invisible(helpers$.run_icoshift_alignment(
data, axis, matrix(c(-0.6, 0.6), nrow = 1L), target = data[3L, ],
aligner = alignment_spy))
stopifnot(identical(dim(fit$xCS), dim(data)),
identical(dimnames(fit$xCS), dimnames(data)),
identical(dim(sample_fit$xCS), dim(data)),
identical(seen_target, as.numeric(data[3L, ])))
cat(paste0("PASS app helpers: correlations, state, intervals, ordering, export, ",
"alignment targets and STOCSY vertical limits\n"))
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.