tests/testthat/test-dist.R

# This tests the distance calculating machinery.
# library(cydar); library(testthat); source("test-dist.R")

set.seed(600)
test_that("neighborDistances works correctly", {
    ncells <- 1000
    nmarkers <- 10
    coords <- matrix(rnorm(ncells*nmarkers, sd=1), nrow=ncells, ncol=nmarkers)
    colnames(coords) <- paste0("X", seq_len(nmarkers))
    suppressWarnings(cd <- prepareCellData(list(A=coords)))

    ci <- BiocNeighbors::bndata(cd$precomputed)
    preorder <- BiocNeighbors::bnorder(cd$precomputed)

    # Computing reference distances.
    refdist <- as.matrix(dist(t(ci)))
    nn <- 50L
    refbands <- apply(refdist, 1, function(x) { sort(x)[2:(nn+1)] })
    dimnames(refbands) <- NULL
    refbands <- t(refbands)

    stuff <- neighborDistances(cd, downsample=1, neighbors=nn, as.tol=FALSE)
    expect_equal(stuff, refbands)
        
    stuff2 <- neighborDistances(cd, downsample=1, neighbors=nn)
    expect_equal(stuff, stuff2*sqrt(nmarkers))

    # Increasing the downsampling.
    ds <- 10
    stuff3 <- neighborDistances(cd, downsample=ds, neighbors=nn)
    chosen <- preorder %% ds == 1L
    expect_equal(stuff2[chosen,,drop=FALSE], stuff3)
})

Try the cydar package in your browser

Any scripts or data that you put into this service are public.

cydar documentation built on April 17, 2021, 6:01 p.m.