tests/testthat/test-coordinates.R

context("coordinates")

test_that("sub2ind returns correct indices", {
  data <- runif(120)
  data.array <- array(data=data, dim=c(4,5,6))
  subs <- mapply(function(x) which(data.array == x, arr.ind=T), data.array)
  inds <- apply(subs, 2, function(x) sub2ind(indices=x, dims=dim(data.array)))
  expect_equal(data[20:40], data[inds[20:40]])
})

test_that("ind2coord returns correct coordinates when given an im3d", {
  testImage <- read.im3d("testdata/nrrd/LHMask.nrrd")
  coord <- ind2coord(testImage)
  coord <- coord[1:10]
  coord.expected <- c(40.6, 42, 36.4, 37.8, 39.2, 40.6, 42, 36.4, 37.8, 39.2)
  expect_equal(coord, coord.expected)
})

test_that("coord2ind returns correct coordinates", {
  testImage <- read.im3d("testdata/nrrd/LHMask.nrrd", ReadData = F)
  ind <- coord2ind(matrix(c(10, 20, 30, 11, 20, 30), nrow=2, byrow=TRUE), testImage)
  ind.expected <- c(53208, 53209)
  expect_equal(ind, ind.expected)
  # with 3d rather than 1d indices
  expect_equal(coord2ind(
      matrix(c(10, 20, 30, 11, 20, 30), nrow = 2, byrow = TRUE),
      testImage, linear.indices = F),
    structure(c(8, 9, 15, 15, 22, 22), .Dim = 2:3))
})
jefferis/nat documentation built on Feb. 22, 2024, 12:45 p.m.