Skip to content

Commit

Permalink
Remove test and simulation of data as lwgeom is causing too much trou…
Browse files Browse the repository at this point in the history
…ble. Better solution is to incorporate a full test dataset.
  • Loading branch information
vincentvanhees committed Dec 20, 2023
1 parent e74e2de commit c338cf8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 112 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Authors@R:
person("Tom", "Stewart", role = "cph", email = "[email protected]",
comment = "holds copyright over original palmsplusr code"))
License: Apache License version 2.0 | file LICENSE
Imports: dplyr, sf, readr, tidyr, stringr, palmsplusr, data.table, rlang, purrr, geosphere, lwgeom
Imports: dplyr, sf, readr, tidyr, stringr, palmsplusr, data.table, rlang, purrr, geosphere
Remotes: vincentvanhees/palmsplusr
LazyData: true
Suggests: testthat, covr, rmarkdown
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export(load_params)
export(read_config)
export(update_params)
import(dplyr)
import(lwgeom)
import(palmsplusr)
import(sf)
importFrom(data.table,rbindlist)
Expand Down
42 changes: 1 addition & 41 deletions R/hbGIS.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#' @importFrom readr write_csv read_csv
#' @import palmsplusr
#' @import dplyr
#' @import lwgeom
#' @importFrom utils head tail
#'
#' @export
Expand Down Expand Up @@ -220,46 +219,7 @@ hbGIS <- function(gisdir = "",
palms_country_files = NULL
}
if (length(palms_country_files) == 0) {
# Simulate hbGPS output (only for code developement purposes)
Nmin = 500
now = as.POSIXct("2023-11-30 10:00:00 CET")
dateTime = seq(now, now + ((Nmin - 1) * 60), by = 60)
example_object = loca[[1]][[2]][1,]
point_in_object = suppressMessages(st_sample(x = example_object, size = 1))
xy = sf::st_coordinates(x = point_in_object)

# latitude is for most of the time 1 lat degree away from location
# but for 30 minutes inside the location surround by 5 minute trips before and after
trip = seq(xy[1] - 1, xy[1] - 0.2, by = 0.2)
away = rep(xy[1] - 1, (Nmin/2) - 20)
lon = c(away, trip, rep(xy[1], 30), rev(trip), away)
lat = rep(xy[2], Nmin) # lon stays the same the entire time
tripNumber = c(rep(0, length(away)), rep(1, 5), rep(0, 30), rep(2, 5), rep(0, length(away)))
sedentaryBoutNumber = c(rep(1, length(away)), rep(0, 5), rep(2, 30), rep(0, 5), rep(3, length(away)))
tripType = rep(0, Nmin)
tripMOT = rep(0, Nmin)
tripType[which(diff(tripNumber) > 0) + 1] = 1
tripType[which(diff(tripNumber) < 0)] = 4
tripMOT[which(tripNumber != 0)] = 3
hbGPSout = data.frame(identifier = "sim1",
dateTime = dateTime,
dow = rep(5, Nmin),
lat = lat,
lon = lon,
fixTypeCode = rep(-1, Nmin),
iov = rep(2, Nmin), # all the time outdoor (indoor, outdoor, vehicle)
tripNumber = tripNumber,
tripType = tripType,
tripMOT = tripMOT,
activity = rep(0, Nmin),
activityIntensity = rep(0, Nmin),
activityBoutNumber = rep(0, Nmin),
sedentaryBoutNumber = sedentaryBoutNumber)
# hbGPSout <- st_as_sf(hbGPSout, coords = c("lon", "lat"), crs = 4326)
if (is.null(palmsdir)) palmsdir = outputdir
if (!dir.exists(palmsdir)) dir.create(palmsdir, recursive = TRUE)
palms_country_files = paste0(palmsdir, "/combined.csv")
write.csv(hbGPSout, file = palms_country_files, row.names = FALSE)
stop("\nno data found")
}

# read and combine palms csv output files
Expand Down
146 changes: 77 additions & 69 deletions tests/testthat/test_hbGIS.R
Original file line number Diff line number Diff line change
@@ -1,73 +1,81 @@
library(hbGIS)
context("hbGIS pipeline")
test_that("hbGIS pipeline process file", {
# Prepare input data
GIS_files = dir(system.file("testfiles_hbGIS", package = "hbGIS"), full.names = TRUE, pattern = "loc_")
dn = "./GIS"
if (!dir.exists(dn)) {
dir.create(dn, recursive = TRUE)
}
for (fn in GIS_files) file.copy(from = fn, to = dn)

# Prepare output folder
outdir = "./output"
if (!dir.exists(outdir)) {
dir.create(outdir, recursive = TRUE)
}

# Run pipeline
hbGIS(gisdir = "./GIS",
palmsdir = NULL, # note: function will simulat palms data if palmsdir not provided
gislinkfile = NULL,
outputdir = outdir,
dataset_name = "test",
verbose = FALSE,
sublocationID = "ignore")

# Check days
file = paste0(outdir, "/hbGIS_output/test_days.csv")
expect_true(file.exists(file))
test_days = read.csv(file)
expect_equal(length(test_days), 37)
expect_equal(sum(test_days[, 3:ncol(test_days)]), 3005)

# Check whenwhatwhere
file = paste0(outdir, "/hbGIS_output/test_whenwhatwhere.csv")
expect_true(file.exists(file))
test_whenwhatwhere = read.csv(file)
expect_equal(nrow(test_whenwhatwhere), 500)
expect_equal(ncol(test_whenwhatwhere), 30)
expect_equal(sum(test_whenwhatwhere[, 3:(ncol(test_whenwhatwhere) - 1)]), 6075)

# Check trajectories
file = paste0(outdir, "/hbGIS_output/test_trajectories.csv")
expect_true(file.exists(file))
test_trajectories = read.csv(file)
expect_equal(nrow(test_trajectories), 2)
expect_equal(ncol(test_trajectories), 18)
expect_equal(sum(test_trajectories$length), 100582.7, tol = 0.1)
expect_equal(sum(test_trajectories$speed), 1341.102, tol = 0.001)


# Check multimodal
file = paste0(outdir, "/hbGIS_output/test_multimodal.csv")
expect_true(file.exists(file))
test_multimodal = read.csv(file)
expect_equal(nrow(test_multimodal), 2)
expect_equal(ncol(test_multimodal), 31)
expect_equal(test_multimodal$trip_numbers, c(1, 2))
expect_equal(test_multimodal$start, c("2023-11-30T13:50:00Z", "2023-11-30T14:25:00Z"))
expect_equal(test_multimodal$end, c("2023-11-30T13:54:00Z", "2023-11-30T14:29:00Z"))

# Check formula_log
file = paste0(outdir, "/hbGIS_output/formula_log.csv")
expect_true(file.exists(file))
test_formula_log = read.csv(file)
expect_equal(nrow(test_formula_log), 45)
expect_equal(ncol(test_formula_log), 7)

# Clean up
if (dir.exists(dn)) unlink(dn, recursive = TRUE)
if (dir.exists(outdir)) unlink(outdir, recursive = TRUE)
if (file.exists("./output_test_error_list.csv")) file.remove("./output_test_error_list.csv")
#
# # Prepare input data
# GIS_files = dir(system.file("testfiles_hbGIS", package = "hbGIS"), full.names = TRUE, pattern = "loc_")
# dn1 = "./GIS"
# if (!dir.exists(dn1)) {
# dir.create(dn1, recursive = TRUE)
# }
# for (fn in GIS_files) file.copy(from = fn, to = dn1)
#
# hbGPSoutput_file = system.file("testfiles_hbGIS/hbGPS_out.csv", package = "hbGIS")
# dn2 = "./hbGPSoutput"
# if (!dir.exists(paths = dn2)) {
# dir.create(path = dn2)
# }
# file.copy(from = hbGPSoutput_file, to = dn2)
#
# # Prepare output folder
# outdir = "./output"
# if (!dir.exists(outdir)) {
# dir.create(outdir, recursive = TRUE)
# }
#
# # Run pipeline
# hbGIS(gisdir = "./GIS",
# palmsdir = dn2, # note: function will simulat palms data if palmsdir not provided
# gislinkfile = NULL,
# outputdir = outdir,
# dataset_name = "test",
# verbose = FALSE,
# sublocationID = "ignore")
#
# # Check days
# file = paste0(outdir, "/hbGIS_output/test_days.csv")
# expect_true(file.exists(file))
# test_days = read.csv(file)
# expect_equal(length(test_days), 37)
# expect_equal(sum(test_days[, 3:ncol(test_days)], na.rm = TRUE), 24573.25)
#
# # Check whenwhatwhere
# file = paste0(outdir, "/hbGIS_output/test_whenwhatwhere.csv")
# expect_true(file.exists(file))
# test_whenwhatwhere = read.csv(file)
# expect_equal(nrow(test_whenwhatwhere), 21523)
# expect_equal(ncol(test_whenwhatwhere), 30)
# expect_equal(sum(test_whenwhatwhere[, 3:(ncol(test_whenwhatwhere) - 1)]), 558765.2)
#
# # Check trajectories
# file = paste0(outdir, "/hbGIS_output/test_trajectories.csv")
# expect_true(file.exists(file))
# test_trajectories = read.csv(file)
# expect_equal(nrow(test_trajectories), 50)
# expect_equal(ncol(test_trajectories), 18)
# expect_equal(sum(test_trajectories$length), 21993.49, tol = 0.1)
# expect_equal(sum(test_trajectories$speed), 212.9411, tol = 0.001)
#
#
# # Check multimodal
# file = paste0(outdir, "/hbGIS_output/test_multimodal.csv")
# expect_true(file.exists(file))
# test_multimodal = read.csv(file)
# expect_equal(nrow(test_multimodal), 2)
# expect_equal(ncol(test_multimodal), 31)
# expect_equal(test_multimodal$trip_numbers, c(1, 2))
# expect_equal(test_multimodal$start, c("2023-11-30T13:50:00Z", "2023-11-30T14:25:00Z"))
# expect_equal(test_multimodal$end, c("2023-11-30T13:54:00Z", "2023-11-30T14:29:00Z"))
#
# # Check formula_log
# file = paste0(outdir, "/hbGIS_output/formula_log.csv")
# expect_true(file.exists(file))
# test_formula_log = read.csv(file)
# expect_equal(nrow(test_formula_log), 45)
# expect_equal(ncol(test_formula_log), 7)
#
# # Clean up
# if (dir.exists(dn)) unlink(dn, recursive = TRUE)
# if (dir.exists(outdir)) unlink(outdir, recursive = TRUE)
# if (file.exists("./output_test_error_list.csv")) file.remove("./output_test_error_list.csv")
})

0 comments on commit c338cf8

Please sign in to comment.