Skip to content

Commit

Permalink
Merge pull request #9 from habitus-eu/issue3_prepare_first_release
Browse files Browse the repository at this point in the history
Prepare first release
  • Loading branch information
vincentvanhees authored Dec 20, 2023
2 parents da00977 + af7f41c commit 38094bc
Show file tree
Hide file tree
Showing 26 changed files with 458 additions and 322 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: hbGIS
Title: Process GIS Data for Human Behaviour Research
Description: Processes GIS data merge with hbGPS pre-processed data of wearable GPS and accelerometation sensors.
Version: 0.0.1
Date: 2023-11-09
Date: 2023-12-20
Authors@R:
c(person(given = "Vincent",
family = "van Hees",
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Generated by roxygen2: do not edit by hand

export(build_days)
export(build_hbGIS)
export(build_multimodal)
export(build_trajectories)
export(build_whenwhatwhere)
export(check_and_clean_palms_data)
export(check_missing_id)
export(hbGIS)
Expand Down
58 changes: 24 additions & 34 deletions R/build_days.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@

#' Calculate day-level summaries from the palmsplus dataset
#' Calculate day-level summaries from the whenwhat dataset
#'
#' @description Build a days dataset by summarising \code{palmsplus}
#' by day and person (\code{identifier}). Not all variables in \code{palmsplus}
#' are summarised, only those specified using \code{\link{palms_add_field}} with
#' the argument \code{domain_field = TRUE}. By default, a \code{duration} field
#' is added (e.g., the total minutes per day).
#' @description Build a days dataset by summarising \code{whenwhat}
#' by day and person (\code{identifier}).
#'
#' All data are summarised by default. However, additional aggragation \emph{domains}
#' can be specified using \code{\link{palms_add_domain}} before building days.
#' Domains are a subset of data, such as during school time. All \code{domain_field}
#' variables will be summarised for each \emph{domain} seperatly.
#'
#' @param data The palmsplus data obtained from \code{\link{palms_build_palmsplus}}.
#' @param data The whenwhat data obtained from \code{\link{build_whenwhatwhere}}.
#' @param verbose Print progress to console. Default is \code{TRUE}.
#' @param palmsplus_domains ...
#' @param palmsplus_fields ...
#' @param where_field ...
#' @param whenwhat_field ...
#' @param loca Nested list with location information
#' @param participant_basis participant_basis
#'
#'
#'#'
#' @return A table summarised by day.
#'
#' @import dplyr
Expand All @@ -30,8 +21,8 @@
#' @export
# Code modified from https://thets.github.io/palmsplusr/
build_days <- function(data = NULL, verbose = TRUE,
palmsplus_domains = NULL,
palmsplus_fields = NULL,
where_field = NULL,
whenwhat_field = NULL,
loca = NULL,
participant_basis = NULL) {
# Note:
Expand All @@ -47,38 +38,37 @@ build_days <- function(data = NULL, verbose = TRUE,
}
}

duration = datetime = name = domain_field = NULL

domain_fields <- palmsplus_domains %>% filter(domain_field == TRUE)
domain_names <- domain_fields %>% pull(name)
duration = datetime = name = is_where_field = NULL
where_field <- where_field %>% filter(is_where_field == TRUE)
where_names <- where_field %>% pull(name)

if (is.null(domain_names)) {
domain_names <- "total"
if (is.null(where_names)) {
where_names <- "total"
} else {
domain_names <- c("total", domain_names)
where_names <- c("total", where_names)
}
domain_args <- setNames("1", "total") %>% lapply(parse_expr)
domain_args <- c(domain_args, setNames(domain_fields[[2]], domain_fields[[1]]) %>%
where_args <- setNames("1", "total") %>% lapply(parse_expr)
where_args <- c(where_args, setNames(where_field[[2]], where_field[[1]]) %>%
lapply(parse_expr))
data <- data %>%
mutate(!!! domain_args) %>%
mutate(!!! where_args) %>%
mutate_if(is.logical, as.integer)

fields <- palmsplus_fields %>% filter(domain_field == TRUE) %>% pull(name)
fields <- whenwhat_field %>% filter(is_where_field == TRUE) %>% pull(name)

data <- data %>%
st_set_geometry(NULL) %>%
dplyr::select(identifier, datetime, any_of(domain_names), all_of(fields)) %>%
dplyr::select(identifier, datetime, any_of(where_names), all_of(fields)) %>%
mutate(duration = 1) %>%
mutate_at(vars(-identifier,-datetime), ~ . * palms_epoch(data) / 60) %>%
group_by(identifier, date = as.Date(datetime)) %>%
dplyr::select(-datetime)

x <- list()
for (i in domain_names) {
for (i in where_names) {
x[[i]] <- data %>%
filter(!!(as.name(i)) > 0) %>%
dplyr::select(-any_of(domain_names), duration) %>%
dplyr::select(-any_of(where_names), duration) %>%
summarise_all(~ sum(.)) %>%
ungroup() %>%
rename_at(vars(-identifier, -date), ~ paste0(i, "_", .))
Expand All @@ -87,13 +77,13 @@ build_days <- function(data = NULL, verbose = TRUE,
result <- x %>%
reduce(left_join, by = c("identifier" = "identifier", "date" = "date"))

# Count the number of segments per domain per day per identifier
# Count the number of segments per where per day per identifier
segmentcount = function(x) {
x = as.numeric(unlist(x)) # x is a tibble column, so first convert to numeric vector
return(length(which(rle(x)$values != 0)))
}

for (dom in domain_names) {
for (dom in where_names) {
if (dom != "total") {
result[, dom] <- NA
for (id in unique(result$identifier)) {
Expand Down
8 changes: 4 additions & 4 deletions R/build_multimodal.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @param data The trajectories object built with \code{palms_calc_trajectories}.
#' @param spatial_threshold Spatial threshold in meters
#' @param temporal_threshold Temporal threshold in minutes
#' @param palmsplus The dataset build by \code{build_hbGIS}
#' @param whenwhat The dataset build by \code{build_hbGIS}
#' @param verbose Print progress after each step. Default is \code{TRUE}.
#' @param multimodal_fields ...
#' @param trajectory_locations ...
Expand Down Expand Up @@ -40,7 +40,7 @@
build_multimodal <- function(data = NULL,
spatial_threshold,
temporal_threshold,
palmsplus = NULL,
whenwhat = NULL,
verbose = TRUE,
multimodal_fields = NULL,
trajectory_locations = NULL) {
Expand Down Expand Up @@ -127,8 +127,8 @@ build_multimodal <- function(data = NULL,
trajectory_locations$end_criteria))


# Rather than recalculating geometry, just lookup in palmsplus
lookup <- palmsplus %>%
# Rather than recalculating geometry, just lookup in whenwhat
lookup <- whenwhat %>%
filter(tripnumber > 0 & triptype %in% c(1, 4)) %>%
as.data.frame() %>%
dplyr::select(all_of(c("identifier", "tripnumber", "triptype", names)))
Expand Down
16 changes: 7 additions & 9 deletions R/build_hbGIS.R → R/build_whenwhatwhere.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

#' Build the hbGIS dataset
#' build_whenwhatwhere
#'
#' @description Build the \code{hbGIS} dataset by adding additional columns to the hbGPS output data.
#' The additional columns are specified using \code{\link{palms_add_field}}.
#' @description Build the whenwhat dataset by adding additional columns to the
#' hbGPS output data based on whenwhat_field.
#'
#' @param data The hbGPS data obtained using \code{read_palms} from palmplusr.
#' @param verbose Print progress to console after each iteration. Default is \code{TRUE}.
#' @param palmsplus_fields palmsplus_fields defined in hbGIS
#' @param whenwhat_field whenwhat_field defined in hbGIS
#' @param loca Nested list with location information
#' @param participant_basis participant_basis
#'
Expand All @@ -21,13 +20,13 @@
#' @export
#'
# Code modified from https://thets.github.io/palmsplusr/
build_hbGIS <- function(data = NULL, verbose = TRUE, palmsplus_fields = NULL,
build_whenwhatwhere <- function(data = NULL, verbose = TRUE, whenwhat_field = NULL,
loca = NULL,
participant_basis = NULL) {
# Note:
# home, school, home_nbh, school_nbh (or similar) need to be present,
# because the functions that are passed on assume that they exist
# So, now we need to create those objects from object loca
# So, create those objects from object loca
identifier = NULL
Nlocations = length(loca)
for (i in 1:Nlocations) {
Expand All @@ -37,8 +36,7 @@ build_hbGIS <- function(data = NULL, verbose = TRUE, palmsplus_fields = NULL,
}
}


field_args <- setNames(palmsplus_fields$formula, palmsplus_fields$name) %>%
field_args <- setNames(whenwhat_field$formula, whenwhat_field$name) %>%
lapply(parse_expr)

x <- list()
Expand Down
5 changes: 3 additions & 2 deletions R/check_and_clean_palms_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ check_and_clean_palms_data <- function(palms_to_clean, country_name, outputdir =

# Saving the new 'clean' dataset - %>% ---------------------------------------
# write_csv(palms, str_replace(link_to_csv, pattern = '.csv', '_cleaned.csv'), na = "")
data.table::fwrite(error_list, paste(outputdir, country_name,"error_list.csv", sep = "_"))

if (nrow(error_list) > 0) {
data.table::fwrite(error_list, paste(outputdir, country_name,"error_list.csv", sep = "_"))
}
return(palms_to_clean_lower)
}

Loading

0 comments on commit 38094bc

Please sign in to comment.