Skip to content

Commit

Permalink
Merge pull request #61 from habitus-eu/issue60_make_persistent
Browse files Browse the repository at this point in the history
Make GGIR persistent
  • Loading branch information
vincentvanhees authored Dec 17, 2022
2 parents a24add7 + c4d6f48 commit 95507b2
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 78 deletions.
45 changes: 35 additions & 10 deletions R/GGIRshiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,41 @@
#' @import GGIR
#' @export

GGIRshiny = function(rawaccdir, outputdir, sleepdiary=c(), configfile=c(), do.Counts = FALSE) {
GGIRshiny = function(rawaccdir, outputdir, sleepdiary = c(), configfile = c(),
do.Counts = FALSE) {
if (length(sleepdiary) == 0) sleepdiary = c()
if (length(configfile) == 0) configfile = c()
if (do.Counts == FALSE) {
GGIR::GGIR(datadir = rawaccdir, outputdir = outputdir, overwrite = FALSE, do.neishabouricounts = FALSE,
configfile = configfile, loglocation = sleepdiary, do.parallel = TRUE)
} else {
GGIR::GGIR(datadir = rawaccdir, outputdir = outputdir, overwrite = FALSE, do.neishabouricounts = TRUE,
configfile = configfile, loglocation = sleepdiary,
do.parallel = TRUE, backup.cal.coef = "retrieve")
Counts2csv(outputdir = paste0(outputdir, "/output_", basename(rawaccdir)), configfile = configfile)
}

# create R script with the code to run the data analysis via a command line call
# in this way turning off or restarting the app will not kill the data analysis
fileConn <- file(paste0(outputdir, "/ggir_cmdline.R"))
writeLines(c("#!/usr/bin/env Rscript",
"args = commandArgs(trailingOnly = TRUE)",
"if (length(args) < 4) {",
"stop(\"At least four arguments are expected\", call. = FALSE)",
"}",
"if (length(args) == 5) {",
"GGIR::GGIR(datadir = args[1], outputdir = args[2], ",
" overwrite = FALSE, do.neishabouricounts = args[3],",
"configfile = args[4], loglocation = args[5],",
"do.parallel = TRUE)",
"} else {",
"GGIR::GGIR(datadir = args[1], outputdir = args[2], ",
" overwrite = FALSE, do.neishabouricounts = as.logical(args[3]),",
"configfile = args[4], do.parallel = TRUE)",
"}",
"HabitusGUI::Counts2csv(outputdir = paste0(args[2], \"/output_\", basename(args[1])), configfile = args[4])"),
fileConn)
close(fileConn)

basecommand = paste0("cd ", outputdir, " ; nohup Rscript ggir_cmdline.R ",
rawaccdir, " ",
outputdir, " ",
do.Counts, " ",
configfile, " ",
sleepdiary, " > ", outputdir, "/GGIR.log 2>&1 &")

system2(command = "cd", args = gsub(pattern = "cd ", replacement = "", x = basecommand),
stdout = "", stderr = "", wait = TRUE)

}
15 changes: 13 additions & 2 deletions R/PALMSpyshiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@
#' @export

PALMSpyshiny = function(outputdir, gpsdir, count_file_location) {
basecommand = paste0("cd ",outputdir," ; nohup palmspy --gps-path ", gpsdir,
if (tolower(Sys.getenv("USERNAME")) %in% c("ucloud", "", "shiny") == FALSE) {
#assuming palmspy is in a local conda environment"
# assuming unbuffer is available in Ubuntu(apt install expect)
palmspypath = " /home/vincent/miniconda3/bin/conda run --no-capture-output -n palmspy "
} else {
#assuming palmspy is directly available from command line (ucloud)
palmspypath = ""
}
basecommand = paste0("cd ",outputdir, "; nohup unbuffer", palmspypath, " palmspy --gps-path ", gpsdir,
" --acc-path ", count_file_location,
" --config-file ", paste0(outputdir, "/config.json"), " > /work/PALMSpy.log 2>&1 &")
" --config-file ", paste0(outputdir, "/config.json"),
" > ", outputdir, "/PALMSpy.log 2>&1 &")
print(basecommand)
system2(command = "cd", args = gsub(pattern = "cd ", replacement = "", x = basecommand),
stdout = "", stderr = "", wait = TRUE)
return()
}
""
103 changes: 39 additions & 64 deletions R/myApp.R
Original file line number Diff line number Diff line change
Expand Up @@ -613,23 +613,19 @@ overflow-y:scroll; max-height: 300px; background: ghostwhite;}")),
id_ggir = showNotification("GGIR in progress ...", type = "message", duration = NULL, closeButton = FALSE)
do.Counts = FALSE
}


if (file.exists(paste0(global$data_out, "/sleepdiary.csv"))) { # because this is not a global variable
sleepdiaryfile_local = paste0(global$data_out, "/sleepdiary.csv")
} else {
sleepdiaryfile_local = c()
}

# on.exit(file.copy(from = stdout_GGIR_tmp, to = logfile, overwrite = TRUE))
# on.exit(file.copy(from = logfile, to = stdout_GGIR_tmp, overwrite = TRUE))

write.table(x = NULL, file = stdout_GGIR_tmp) # initialise empty file
output$mylog_GGIR <- renderText({
paste(mylog_GGIR(), collapse = '\n')
})

# If process somehow unexpectedly terminates, always copy tmp log
# file to actual log file for user to see
logfile = paste0(isolate(global$data_out), "/GGIR.log")
on.exit(file.copy(from = stdout_GGIR_tmp, to = logfile, overwrite = TRUE))

# Start GGIR
x_ggir <- r_bg(func = function(GGIRshiny, rawaccdir, outputdir,
sleepdiary, configfile, do.Counts){
Expand All @@ -639,25 +635,31 @@ overflow-y:scroll; max-height: 300px; background: ghostwhite;}")),
args = list(GGIRshiny = GGIRshiny,
rawaccdir = isolate(global$raw_acc_in),
outputdir = global$data_out,
sleepdiary = sleepdiaryfile_local,
configfile = paste0(global$data_out, "/config.csv"), #isolate(configfileGGIR()),
do.Counts = do.Counts),
stdout = stdout_GGIR_tmp,
stderr = "2>&1")

# GGIRshiny(rawaccdir = isolate(global$raw_acc_in), outputdir = global$data_out,
# sleepdiary = sleepdiaryfile_local, configfile = paste0(global$data_out, "/config.csv"), #isolate(configfileGGIR()),
# do.Counts = do.Counts)



sleepdiary = sleepdiaryfile_local,
configfile = paste0(global$data_out, "/config.csv"), #isolate(configfileGGIR()),
do.Counts = do.Counts),
stdout = "", #,
stderr = "")

# Expected location of log file
logfile = paste0(isolate(global$data_out), "/GGIR.log")

observe({
if (x_ggir$poll_io(0)[["process"]] != "ready") {
invalidateLater(5000)
# Copy local log file to server to update progress log
if (file.exists(logfile)) file.copy(from = logfile, to = stdout_GGIR_tmp, overwrite = TRUE)
invalidateLater(2000)
} else {
# Copy local log file to server to update progress log
if (file.exists(logfile)) file.copy(from = logfile, to = stdout_GGIR_tmp, overwrite = TRUE)
on.exit(removeNotification(id_ggir), add = TRUE)
# When process is finished copy tmp log file to actual log file for user to see
file.copy(from = stdout_GGIR_tmp, to = logfile, overwrite = TRUE)

# Delete Rscript that is created by GGIRshiny because user does not need this
ggir_cmdline = paste0(global$data_out, "/ggir_cmdline.R")
if (file.exists(ggir_cmdline)) {
file.remove(ggir_cmdline)
}

# Now check whether results are correctly generated:
expected_outputdir_ggir = paste0(global$data_out, "/output_", basename(global$raw_acc_in))
expected_ggiroutput_file = paste0(global$data_out, "/output_", basename(global$raw_acc_in), "/results/part2_daysummary.csv")
Expand Down Expand Up @@ -732,62 +734,33 @@ overflow-y:scroll; max-height: 300px; background: ghostwhite;}")),
if (ready_to_run_palsmpy == TRUE) {
id_palmspy = showNotification("PALMSpy in progress ...", type = "message", duration = NULL, closeButton = FALSE)
shinyjs::hide(id = "start_palmspy")
# on.exit(removeNotification(id_palmspy), add = TRUE)

write.table(x = NULL, file = stdout_PALMSpy_tmp) # initialise empty file

output$mylog_PALMSpy <- renderText({
paste(mylog_PALMSpy(), collapse = '\n')
})


# basecommand = paste0("cd ",global$data_out," ; palmspy --gps-path ", global$gps_in,
# " --acc-path ", count_file_location,
# " --config-file ", paste0(global$data_out, "/config.json"))

# # system2(command = "cd", args = gsub(pattern = "cd ", replacement = "", x = basecommand),
# # stdout = "stdout_palmspy.log", stderr = "stderr_palmspy.log", wait = TRUE)
#
# # Start PALMSpy
# x_palmspy <- r_bg(func = function(system2, command, args, stdout, stderr, wait){
# system2(command, args, stdout, stderr, wait)
# },
# args = list(system2 = system2,
# command = "cd",
# args = gsub(pattern = "cd ", replacement = "", x = basecommand),
# stdout = "",
# stderr = "",
# wait = TRUE),
# stdout = stdout_PALMSpy_tmp,
# stderr = "2>&1")
#

# If process somehow unexpectedly terminates, always copy tmp log
# file to actual log file for user to see
logfile = paste0(isolate(global$data_out), "/PALMSpy.log")
on.exit(file.copy(from = stdout_PALMSpy_tmp, to = logfile, overwrite = TRUE))

# # Start PALMSpy
x_palmspy <- r_bg(func = function(PALMSpyshiny, outputdir, gpsdir, count_file_location){
x_palmspy <- r_bg(func = function(PALMSpyshiny, outputdir, gpsdir, count_file_location) {
PALMSpyshiny(outputdir, gpsdir, count_file_location)
},
args = list(PALMSpyshiny = PALMSpyshiny,
outputdir = global$data_out,
gpsdir = global$gps_in,
count_file_location = count_file_location),
stdout = stdout_PALMSpy_tmp,
stderr = "2>&1")

# basecommand = paste0("cd ",global$data_out," ; palmspy --gps-path ", global$gps_in,
# " --acc-path ", count_file_location,
# " --config-file ", paste0(global$data_out, "/config.json"))
# # stdout = system(command = basecommand, intern = TRUE) # old command
# system2(command = "cd", args = gsub(pattern = "cd ", replacement = "", x = basecommand),
# stdout = "stdout_palmspy.log", stderr = "stderr_palmspy.log", wait = TRUE)
outputdir = global$data_out,
gpsdir = global$gps_in,
count_file_location = count_file_location),
stdout = "",
stderr = "")

logfile = paste0(isolate(global$data_out), "/PALMSpy.log")

observe({
if (x_palmspy$poll_io(0)[["process"]] != "ready") {
invalidateLater(5000)
if (file.exists(logfile)) file.copy(from = logfile, to = stdout_PALMSpy_tmp, overwrite = TRUE)
invalidateLater(2000)
} else {
file.copy(from = logfile, to = stdout_PALMSpy_tmp, overwrite = TRUE)
on.exit(removeNotification(id_palmspy), add = TRUE)
# When process is finished copy tmp log file to actual log file for user to see
file.copy(from = stdout_PALMSpy_tmp, to = logfile, overwrite = TRUE)
Expand Down Expand Up @@ -859,6 +832,8 @@ overflow-y:scroll; max-height: 300px; background: ghostwhite;}")),
if (ready_to_run_palmsplusr == TRUE) {
shinyjs::hide(id = "start_palmsplusr")
id_palmsplusr = showNotification("palmsplusr in progress ...", type = "message", duration = NULL, closeButton = FALSE)

write.table(x = NULL, file = stdout_PALMSpy_tmp) # initialise empty file
output$mylog_palmsplusr <- renderText({
paste(mylog_palmsplusr(), collapse = '\n')
})
Expand Down
4 changes: 2 additions & 2 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

\section{Changes in version 0.1.5 (GitHub-only-release date: ??-??-2022)}{
\itemize{
\item PALMSpy now persistent (PR #58)
\item PALMSpy now run via persistent command line call (PR #58)
\item BrondCounts deprecated and replaced by Neishabouricounts
\item GGIR now run via persistent command line call (issue #60)
}
}

\section{Changes in version 0.1.4 (GitHub-only-release date: 26-07-2022)}{
\itemize{
\item Improved integrated of palmsplusr which now also uses GIS files
Expand Down

0 comments on commit 95507b2

Please sign in to comment.