Skip to content

Commit

Permalink
standardise filename and calendar date in reports from parts 2, 4, 5,…
Browse files Browse the repository at this point in the history
… and 6
  • Loading branch information
jhmigueles committed Sep 13, 2024
1 parent a70a430 commit e349942
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
24 changes: 20 additions & 4 deletions R/g.part6.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ g.part6 = function(datadir = c(), metadatadir = c(), f0 = c(), f1 = c(),
# Identify correct subfolder
expected_ts_path = paste0(metadatadir, "/meta/ms5.outraw/", params_phyact[["part6_threshold_combi"]])
expected_ms5raw_path = paste0(metadatadir, "/meta/ms5.outraw")
expected_ms5_path = paste0(metadatadir, "/meta/ms5.out")
if (!dir.exists(expected_ts_path)) {
if (!dir.exists(expected_ms5raw_path)) {
stop("\nPath ", expected_ms5raw_path, " does not exist", call. = FALSE)
Expand All @@ -44,6 +45,7 @@ g.part6 = function(datadir = c(), metadatadir = c(), f0 = c(), f1 = c(),
}
}
fnames.ms5raw = dir(expected_ts_path)
fnames.ms5 = dir(expected_ms5_path)
# It can be that the use stored rdata and csv files
# only use rdata in that case
getExt = function(x) {
Expand Down Expand Up @@ -95,7 +97,7 @@ g.part6 = function(datadir = c(), metadatadir = c(), f0 = c(), f1 = c(),
# Declare recording level functionality, which at the end of this g.part6 is either
# applied to the file in parallel with foreach or serially with a loop
main_part6_recordlevel = function(i, metadatadir = c(), f0 = c(), f1 = c(),
fnames.ms5raw, ffdone, EXT, verbose) {
fnames.ms5raw, ffdone, EXT, verbose, fnames.ms5) {
if (length(ffdone) > 0) {
if (length(which(ffdone == fnames.ms5raw[i])) > 0) {
skip = 1 #skip this file because it was analysed before")
Expand All @@ -109,6 +111,7 @@ g.part6 = function(datadir = c(), metadatadir = c(), f0 = c(), f1 = c(),

if (skip == 0) {
# Load time series:
lookfor = unlist(strsplit(fnames.ms5raw[i], "_"))[1] # filename without extension and without sib detection indicator (e.g., _T5A5.csv)
if (EXT == "RData") {
load(file = paste0(metadatadir, "/meta/ms5.outraw/",
params_phyact[["part6_threshold_combi"]], "/", fnames.ms5raw[i]))
Expand All @@ -117,6 +120,17 @@ g.part6 = function(datadir = c(), metadatadir = c(), f0 = c(), f1 = c(),
mdat = data.table::fread(file = paste0(metadatadir, "/meta/ms5.outraw/",
params_phyact[["part6_threshold_combi"]], "/", fnames.ms5raw[i]), data.table = FALSE)
}
# also load corresponding ms5 file to extract filename
ms5toload = grep(pattern = lookfor, fnames.ms5, fixed = T, value = T)
if (length(ms5toload) == 1) {
output = c()
load(file.path(metadatadir, "meta/ms5.out", ms5toload))
filename = gsub(".RData$", "", unique(output$filename))
rm(output)
} else {
# if couldn't match the ms5 data, then use the ms5raw as filename as it was done before
filename = gsub(pattern = "[.]RData|[.]csv", replacement = "", x = fnames.ms5raw[i])
}
nfeatures = 50
summary = matrix(NA, nfeatures, 1)
s_names = rep("", nfeatures)
Expand Down Expand Up @@ -188,7 +202,7 @@ g.part6 = function(datadir = c(), metadatadir = c(), f0 = c(), f1 = c(),
summary[fi] = format(starttime)
s_names[fi] = "starttime"
fi = fi + 1
summary[fi] = gsub(pattern = "[.]RData|[.]csv", replacement = "", x = fnames.ms5raw[i])
summary[fi] = filename
s_names[fi] = "filename"
fi = fi + 1
summary[fi] = ifelse(test = nrow(ts) == 1,
Expand Down Expand Up @@ -333,7 +347,8 @@ g.part6 = function(datadir = c(), metadatadir = c(), f0 = c(), f1 = c(),
.export = functions2passon, .errorhandling = errhand) %myinfix% {
tryCatchResult = tryCatch({
main_part6_recordlevel(i, metadatadir, f0, f1,
fnames.ms5raw, ffdone, EXT, verbose)
fnames.ms5raw, ffdone, EXT, verbose,
fnames.ms5)
})
return(tryCatchResult)
}
Expand All @@ -348,7 +363,8 @@ g.part6 = function(datadir = c(), metadatadir = c(), f0 = c(), f1 = c(),
for (i in f0:f1) {
if (verbose == TRUE) cat(paste0(i, " "))
main_part6_recordlevel(i, metadatadir, f0, f1,
fnames.ms5raw, ffdone, EXT, verbose)
fnames.ms5raw, ffdone, EXT, verbose,
fnames.ms5)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion R/g.report.part2.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ g.report.part2 = function(metadatadir = c(), f0 = c(), f1 = c(), maxdur = 0,
# tidy up data.frames
SUMMARY_clean = tidyup_df(SUMMARY)
daySUMMARY_clean = tidyup_df(daySUMMARY)

# format calendar dates (dates are stored as iso8601, so date is in the first 10 characters )
daySUMMARY_clean$calendar_date = substr(daySUMMARY_clean$calendar_date, 1, 10)
#===============================================================================
# store final matrices again
data.table::fwrite(x = SUMMARY_clean, file = paste0(metadatadir, "/results/part2_summary.csv"),
Expand Down
3 changes: 3 additions & 0 deletions R/g.report.part4.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ g.report.part4 = function(datadir = c(), metadatadir = c(), loglocation = c(),
}
nightsummary2 = as.data.frame(do.call(rbind, lapply(fnames.ms4, myfun)), stringsAsFactors = FALSE)
nightsummary2$night = as.numeric(gsub(" ", "", nightsummary2$night))
nightsummary2$calendar_date = as.Date(nightsummary2$calendar_date, format = "%d/%m/%Y")
nightsummary2$calendar_date = as.character(nightsummary2$calendar_date)
nightsummary2$filename = gsub(".RData$", "", nightsummary2$filename)
# ====================================== Add non-wearing during SPT from part 5, if it is availabe:
ms5.out = "/meta/ms5.out"
if (file.exists(paste(metadatadir, ms5.out, sep = ""))) {
Expand Down
3 changes: 3 additions & 0 deletions R/g.report.part5.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ g.report.part5 = function(metadatadir = c(), f0 = c(), f1 = c(), loglocation = c
outputfinal = outputfinal[,-cut]
}

# revise filename
outputfinal$filename = gsub(".RData$", "", outputfinal$filename)

# order data.frame
outputfinal$window_number = as.numeric(gsub(" ", "", outputfinal$window_number))
outputfinal = outputfinal[order(outputfinal$filename, outputfinal$window_number, outputfinal$window), ]
Expand Down

0 comments on commit e349942

Please sign in to comment.