Skip to content

Commit

Permalink
Update data sets and associated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steffilazerte committed Nov 4, 2017
1 parent a25e732 commit 579c0e3
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 23 deletions.
2 changes: 1 addition & 1 deletion man/finches_lg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/testthat/test_a02_errorchecks.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ test_that("check_input renames columns (regular)", {

expect_message(r2 <- check_input(r1, input = "animal_id", options = c("animal_id", "bird_id")), "Renaming column 'Animal_ID' to 'animal_id'")
expect_is(r2, "data.frame")
expect_named(r2, c("animal_id", "TIME", "feeder_id", "species", "sex", "lon", "lat"))
expect_named(r2, c("animal_id", "date", "TIME", "feeder_id", "species", "age", "sex", "site_name", "lon", "lat"))

expect_message(r3 <- check_input(r2, input = "time", options = "time"), "Renaming column 'TIME' to 'time'")
expect_is(r3, "data.frame")
expect_named(r3, c("animal_id", "time", "feeder_id", "species", "sex", "lon", "lat"))
expect_named(r3, c("animal_id", "date", "time", "feeder_id", "species", "age", "sex", "site_name", "lon", "lat"))

expect_message(r4 <- check_input(r3, input = "logger_id", options = c("feeder_id", "logger_id")), "Renaming column 'feeder_id' to 'logger_id'")
expect_is(r4, "data.frame")
expect_named(r4, c("animal_id", "time", "logger_id", "species", "sex", "lon", "lat"))
expect_named(r4, c("animal_id", "date", "time", "logger_id", "species", "age", "sex", "site_name", "lon", "lat"))

expect_equal(r4, finches)
})
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_a03_load.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test_that("load_format fixes names", {
expect_message(load_format(r1), "Renaming column 'TIME' to 'time'")

expect_named(suppressMessages(load_format(r1)),
c("animal_id", "date", "time", "logger_id", "specIes", "SEX", "lon", "lat"))
c("animal_id", "date", "time", "logger_id", "specIes", "age", "SEX", "site_name","lon", "lat"))
})


Expand Down
29 changes: 25 additions & 4 deletions tests/testthat/test_b01_visits.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,27 @@ test_that("visits() returns appropriate, non-empty dataframe", {
expect_is(v$logger_id, "factor")
expect_is(v$start, "POSIXct")
expect_is(v$end, "POSIXct")
expect_true(all(as.numeric(v$end - v$start) >= 0))
expect_is(v$date, "Date")

for(r in list(finches, finches_lg, chickadees)) {
v <- visits(r)
expect_true(all(!is.na(v$start)))
expect_true(all(!is.na(v$end)))
expect_true(all(!is.na(v$date)))
expect_true(all(as.numeric(v$end - v$start) >= 0))
expect_equal(v$date, as.Date(v$start, tz = lubridate::tz(v$start)))
}

for(r in list(finches, finches_lg, chickadees)) {
v <- visits(r, bw = 10000000000000000000)
expect_true(all(!is.na(v$start)))
expect_true(all(!is.na(v$end)))
expect_true(all(!is.na(v$date)))
expect_equal(v$date, as.Date(v$start, tz = lubridate::tz(v$start)))
expect_true(all(as.numeric(v$end - v$start) >= 0))
}


})

test_that("visits() has correct spacing", {
Expand Down Expand Up @@ -92,8 +112,9 @@ test_that("visits() returns correct data", {
v <- visits(finches)
expect_equal(v$animal_id[2], factor("041868D396", levels = c("041868D396", "041868D861", "062000043E", "06200004F8", "0620000514")))
expect_equal(v$logger_id[2], factor("2100", levels = c("2100", "2200", "2400", "2700")))
expect_equal(v$start[2], as.POSIXct("2016-01-29 11:21:23", tz = "America/Vancouver"))
expect_equal(v$end[2], as.POSIXct("2016-01-29 11:21:26", tz = "America/Vancouver"))
expect_equal(v$start[2], as.POSIXct("2016-01-29 11:21:23", tz = "Etc/GMT+8"))
expect_equal(v$end[2], as.POSIXct("2016-01-29 11:21:26", tz = "Etc/GMT+8"))
expect_equal(v$date[2], as.Date("2016-01-29"))
expect_equal(v$animal_n[2], 5)
expect_equal(v$logger_n[2], 4)
expect_equal(round(v$lon[2], 4), -120.3624)
Expand All @@ -118,7 +139,7 @@ test_that("visits() na_rm", {

test_that("visits() pass", {
expect_length(visits(finches, pass = FALSE), 7)
expect_length(visits(finches, pass = TRUE), 11)
expect_length(visits(finches, pass = TRUE), 13)
})

test_that("visits() with group_by", {
Expand Down
10 changes: 7 additions & 3 deletions tests/testthat/test_b02_presence.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ test_that("presence() handles single animal", {

## Format
expect_is(p, "data.frame")
expect_length(p, 12)
expect_length(p, 14)
expect_match(names(p)[1:5], "^logger_id$|^animal_id$|^date$|^start$|^end$|^length$")
expect_is(p$animal_id, "factor")
expect_is(p$logger_id, "factor")
expect_is(p$date, "Date")
expect_is(p$start, "POSIXct")

expect_equal(p$date, as.Date(p$start, tz = lubridate::tz(p$start)))

## Not impossible nor missing
expect_equal(sum(is.na(p)), 0)
expect_true(all(p$end >= p$start))
Expand All @@ -28,10 +31,11 @@ test_that("presence() handles multiple animals", {

## Format
expect_is(p, "data.frame")
expect_length(p, 12)
expect_length(p, 14)
expect_match(names(p)[1:5], "^logger_id$|^animal_id$|^date$|^start$|^end$|^length$")
expect_is(p$animal_id, "factor")
expect_is(p$logger_id, "factor")
expect_is(p$date, "Date")
expect_is(p$start, "POSIXct")

## Not impossible nor missing
Expand All @@ -41,6 +45,6 @@ test_that("presence() handles multiple animals", {

test_that("presence() pass", {
expect_length(presence(visits(finches), pass = FALSE), 6)
expect_length(presence(visits(finches), pass = TRUE), 12)
expect_length(presence(visits(finches), pass = TRUE), 14)
})

14 changes: 10 additions & 4 deletions tests/testthat/test_b03_move.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ test_that("move() returns appropriate, non-empty dataframe", {

## Format
expect_is(m, "data.frame")
expect_length(m, 15)
expect_length(m, 17)
expect_equal(nrow(m), 30)
expect_equal(sum(is.na(m)), 0)
expect_match(names(m)[1:3], "^animal_id$|^date$|^time$|^logger_id$|^direction$|^move_dir$|^move_path$|^strength$")
expect_is(m$animal_id, "factor")
expect_is(m$logger_id, "factor")
expect_is(m$date, "Date")
expect_is(m$time, "POSIXct")


})

test_that("move() returns non-impossible data", {
Expand All @@ -28,7 +31,10 @@ test_that("move() returns expected data", {
m <- move(visits(finches))
expect_equal(unique(m$animal_id[1]), factor("041868D396", levels = c("041868D396", "041868D861", "062000043E", "06200004F8", "0620000514")))
expect_equal(m$logger_id[1:2], factor(c("2400", "2100"), levels = c("2100", "2200", "2400", "2700")))
expect_equal(m$time[1:2], as.POSIXct(c("2016-01-29 08:35:26", "2016-01-29 11:21:23"), tz = "America/Vancouver"))
expect_equal(m$time[1:2], as.POSIXct(c("2016-01-29 08:35:26", "2016-01-29 11:21:23"),
tz = "Etc/GMT+8"))
expect_equal(m$date[2], as.Date("2016-01-29"))
expect_equal(m$date, as.Date(m$time, tz = lubridate::tz(m$time)))
expect_equal(m$animal_n[1], 5)
expect_equal(m$logger_n[1], 4)
expect_equal(round(m$lon[2], 4), -120.3624)
Expand All @@ -48,13 +54,13 @@ test_that("move() handles zero movements and single animal", {
## No movements (all = TRUE)
expect_silent(m <- visits(finches) %>% dplyr::filter(animal_id == "062000043E") %>% move(., all = TRUE))
expect_is(m, "data.frame")
expect_length(m, 15)
expect_length(m, 17)
expect_equal(nrow(m), 1)
expect_equal(m$animal_id[1], factor("062000043E", levels = c("041868D396", "041868D861", "062000043E", "06200004F8", "0620000514")))
})

test_that("move() pass", {
expect_length(move(visits(finches), pass = FALSE), 9)
expect_length(move(visits(finches), pass = TRUE), 15)
expect_length(move(visits(finches), pass = TRUE), 17)
})

10 changes: 7 additions & 3 deletions tests/testthat/test_b04_disp.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test_that("disp() returns appropriate, non-empty dataframe", {
expect_is(d2 <- d[["summaries"]], "data.frame")
expect_is(d3 <- d[["interactions"]], "data.frame")

expect_length(d1, 12)
expect_length(d1, 14)
expect_length(d2, 4)
expect_length(d3, 3)

Expand Down Expand Up @@ -49,7 +49,11 @@ test_that("disp() returns correct data", {
## Data
expect_equal(unique(d1$animal_id[1]), factor("0620000514", levels = c("041868D396", "041868D861", "062000043E", "06200004F8", "0620000514")))
expect_equal(d1$logger_id[1], factor(c("2200"), levels = c("2100", "2200", "2400", "2700")))
expect_equal(d1$left[1], as.POSIXct(c("2016-01-28 12:34:28"), tz = "America/Vancouver"))
expect_equal(d1$left[1], as.POSIXct(c("2016-01-28 12:34:28"), tz = "Etc/GMT+8"))
expect_equal(d1$date[1], as.Date("2016-01-28"))

expect_equal(d1$date, as.Date(d1$left, tz = lubridate::tz(d1$left)))

expect_equal(d1$animal_n[1], 5)
expect_equal(d1$logger_n[1], 4)
expect_equal(round(d1$lon[2], 4), -120.3612)
Expand All @@ -63,5 +67,5 @@ test_that("disp() returns correct data", {

test_that("disp() pass", {
expect_length(visits(finches) %>% disp(., bw = 30, pass = FALSE) %>% .$displacements, 6)
expect_length(visits(finches) %>% disp(., bw = 30, pass = TRUE) %>% .$displacements, 12)
expect_length(visits(finches) %>% disp(., bw = 30, pass = TRUE) %>% .$displacements, 14)
})
10 changes: 7 additions & 3 deletions tests/testthat/test_b06_activity.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ test_that("activity() in general", {
expect_match(names(a)[1:6], "^animal_id$|^time$|^date$|^activity$|^activity_c$|^logger_id$")
expect_is(a$animal_id, "factor")
expect_is(a$logger_id, "factor")
expect_is(a$date, "Date")
expect_is(a$time, "POSIXct")

expect_equal(a$animal_id[1], factor("06200004F8", levels = c("041868D396", "041868D861", "062000043E", "06200004F8", "0620000514")))
expect_equal(a$logger_id[1], factor(NA, levels = c("2100", "2200", "2400", "2700")))
expect_equal(a$time[1], as.POSIXct("2016-01-28", tz = "America/Vancouver"))
expect_equal(a$date[1], as.Date("2016-01-28"))
expect_equal(a$time[1], as.POSIXct("2016-01-28", tz = "Etc/GMT+8"))
expect_equal(nrow(a), 386)

expect_equal(a$date, as.Date(a$time, tz = lubridate::tz(a$time)))
})

# activity()
Expand Down Expand Up @@ -76,14 +80,14 @@ test_that("daily() by_logger == FALSE", {

expect_equal(d$animal_id[1], factor("06200004F8", levels = c("041868D396", "041868D861", "062000043E", "06200004F8", "0620000514")))
expect_equal(d$logger_id[1], factor(NA, levels = c("2100", "2200", "2400", "2700")))
expect_equal(d$time[1], as.POSIXct("1970-01-01", tz = "America/Vancouver"))
expect_equal(d$time[1], as.POSIXct("1970-01-01", tz = "Etc/GMT+8"))
expect_equal(nrow(d), 192)

a <- activity(p, by_logger = TRUE)
d <- daily(a)

expect_equal(d$animal_id[1], factor("06200004F8", levels = c("041868D396", "041868D861", "062000043E", "06200004F8", "0620000514")))
expect_equal(d$logger_id[1], factor(2100, levels = c("2100", "2200", "2400", "2700")))
expect_equal(d$time[1], as.POSIXct("1970-01-01", tz = "America/Vancouver"))
expect_equal(d$time[1], as.POSIXct("1970-01-01", tz = "Etc/GMT+8"))
expect_equal(nrow(d), 768)
})
2 changes: 1 addition & 1 deletion tests/testthat/test_b07_convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ test_that("convert_activity converts as expected", {
expect_is(i, "list")
expect_named(i, as.character(sort(unique(r$animal_id))))
expect_is(i[[1]], "numeric")
expect_equal(i[["062000043E"]], c(3.067125, 3.067271), tolerance = 0.0000001)
expect_equal(i[["062000043E"]], c(2.805326, 2.805471), tolerance = 0.0000001)
})

test_that("convert_activity runs activity functions as expected", {
Expand Down

0 comments on commit 579c0e3

Please sign in to comment.