From b3a7147c29f4b5a6f12798991b98f2d26622318f Mon Sep 17 00:00:00 2001 From: Jairo H Migueles Date: Tue, 17 Dec 2024 21:03:59 +0100 Subject: [PATCH] include helper function to decide guider when HASPT.algo is of lenght 2 folowing Vincent's feedback --- R/g.sib.det.R | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/R/g.sib.det.R b/R/g.sib.det.R index a6b56be65..ededb61ff 100644 --- a/R/g.sib.det.R +++ b/R/g.sib.det.R @@ -21,6 +21,7 @@ g.sib.det = function(M, IMP, I, twd = c(-12, 12), # So, out of convenience I keep the object name SPTE. dstime_handling_check = function(tmpTIME = tmpTIME, spt_estimate = spt_estimate, tz = c(), calc_SPTE_end = c(), calc_SPTE_start = c()) { + # Function to readjust estimated SPTE_start and SPTE_end in DST days t = tmpTIME[c(1, spt_estimate$SPTE_start, spt_estimate$SPTE_end)] timezone = format(GGIR::iso8601chartime2POSIX(t, tz = tz), "%z") if (length(unique(timezone)) == 1) { @@ -34,6 +35,14 @@ g.sib.det = function(M, IMP, I, twd = c(-12, 12), return(c(calc_SPTE_start + offset[1], calc_SPTE_end + offset[2])) } } + decide_guider = function(HASPT.algo, nonwear_percentage) { + # Function to decide the guider to use in the case that HASPT.algo is of length 2 + if (HASPT.algo[1] == "NotWorn" && nonwear_percentage < 25 && length(HASPT.algo) == 2) { + return(2) + } else { + return(1) + } + } #================================================== # get variables nD = nrow(IMP$metashort) @@ -230,13 +239,7 @@ g.sib.det = function(M, IMP, I, twd = c(-12, 12), detection.failed = FALSE # Calculate nonwear percentage for this window nonwear_percentage = (length(which(invalid[qqq1:qqq2] == 1)) / (qqq2 - qqq1 + 1)) * 100 - guider_to_use = 1 - if (params_sleep[["HASPT.algo"]][1] == "NotWorn" && - nonwear_percentage < 25 && - length(params_sleep[["HASPT.algo"]]) == 2) { - # Nonwear percentage was low, so use alternative guider specified as second element - guider_to_use = 2 - } + guider_to_use = decide_guider(params_sleep[["HASPT.algo"]], nonwear_percentage) #------------------------------------------------------------------ # calculate L5 because this is used as back-up approach tmpACC = ACC[qqq1:qqq2] @@ -293,13 +296,7 @@ g.sib.det = function(M, IMP, I, twd = c(-12, 12), if (newqqq2 < length(anglez) & (newqqq2 - newqqq1) > (23*(3600/ws3)) ) { # Recalculate nonwear percentage for new window (6pm to 6pm) nonwear_percentage = (length(which(invalid[newqqq1:newqqq2] == 1)) / (newqqq2 - newqqq1 + 1)) * 100 - guider_to_use = 1 - if (params_sleep[["HASPT.algo"]][1] == "NotWorn" && - nonwear_percentage < 25 && - length(params_sleep[["HASPT.algo"]]) == 2) { - # Nonwear percentage was low, so use alternative guider specified as second element - guider_to_use = 2 - } + guider_to_use = decide_guider(params_sleep[["HASPT.algo"]], nonwear_percentage) # new TIME (6pm tp 6pm) tmpTIME = time[newqqq1:newqqq2] if (params_sleep[["HASPT.algo"]][guider_to_use] != "NotWorn") {