test-Cluster.R 2.97 KB
Newer Older
context("s2dv::Cluster tests")

##############################################
  # dat1
  set.seed(1)
  dat1 <- array(rnorm(100), 
                dim = c(sdate = 50, space = 2))
  weights1 <- array(c(0.9, 1.1), dim = c(space = 2))

  # dat2
  set.seed(2)
  dat2 <- array(rnorm(300),
                dim = c(sdate = 50, lat = 2, lon = 3))
  weights2 <- array(c(0.9, 1.1), dim = c(lat = 2, lon = 3))

##############################################

test_that("1. Input checks", {
  # data
  expect_error(
  Cluster(c()),
  "Parameter 'data' cannot be NULL."
  )
  expect_error(
    Cluster(c(NA, NA)),
    "Parameter 'data' must be a numeric array."
  )
  expect_error(
    Cluster(array(1:10, dim = c(2, 5))),
    "Parameter 'data' must have dimension names."
  )
  # weights
  expect_error(
  Cluster(dat1, weights = 'lat'),
  "Parameter 'weights' must be a numeric array."
  )
  expect_error(
  Cluster(dat1, weights = 2),
  "Parameter 'weights' must have dimension names."
  )
  expect_error(
  Cluster(dat1, weights = array(2, dim = c(lat = 2))),
  "Parameter 'weights' must have dimensions that can be found in 'data' dimensions."
  )
  # time_dim
  expect_error(
    Cluster(dat1, weights1, time_dim = 'a'),
    "Parameter 'time_dim' is not found in 'data' dimension."
  )
  expect_error(
    Cluster(array(c(1:25), dim = c(dat = 1, time = 5, space = 2)), weights1),
    "Parameter 'time_dim' is not found in 'data' dimension."
  )
  expect_error(
    Cluster(dat1, weights1, time_dim = 2),
    "Parameter 'time_dim' must be a character string."
  )
  expect_error(
    Cluster(dat1, weights1, time_dim = c('a', 'sdate')),
    "Parameter 'time_dim' must be a character string."
  )
  # nclusters
  expect_error(
  Cluster(dat1, weights1, ncluster = 1),
  "Parameter 'nclusters' must be an integer bigger than 1."
  )
  # index
  expect_error(
  Cluster(dat1, weights1, index = 1),
  "Parameter 'index' should be a character strings accepted as 'index' by the function NbClust::NbClust."
  )
  # ncores
  expect_error(
    Cluster(dat1, weights1, ncore = 0),
    "Parameter 'ncores' must be a positive integer."
  )
})

##############################################
test_that("2. Output checks: dat1", {
# The output is random. Only check dimensions.
  expect_equal(
    length(Cluster(dat1, weights1)$cluster),
    50
  )
  expect_equal(
    length(Cluster(dat1)$cluster),
    100
  )
  expect_equal(
    dim(Cluster(dat1, weights1)$centers),
    c(8, 2)
  )
  expect_equal(
    dim(Cluster(dat1, weights1, nclusters = 3)$centers),
    c(3, 2)
  )

})


##############################################
test_that("3. Output checks: dat2", {

  expect_equal(
    length(Cluster(dat2, weights2)$cluster),
    50
  )
  expect_equal(
    length(Cluster(dat2)$cluster),
    300
  )
  expect_equal(
    length(Cluster(dat2, space_dim = c('lon', 'lat'))$cluster),
    50
  )
  expect_equal(
    dim(Cluster(dat2, weights2)$centers),
    c(7, 6)
  )
  expect_equal(
    dim(Cluster(dat2, weights2, nclusters = 5)$centers),
    c(5, 6)
  )

})