Skip to content

Commit

Permalink
Merge pull request #264 from wadpac/issue259_minorfixes345
Browse files Browse the repository at this point in the history
Fixes to issue259, 262, and metric ENMOa now better facilitated
  • Loading branch information
vincentvanhees authored Feb 28, 2020
2 parents d4f5ecd + 111a45a commit 05fa05e
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 127 deletions.
5 changes: 3 additions & 2 deletions R/g.analyse.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ g.analyse = function(I,C,M,IMP,qlevels=c(),qwindow=c(0,24),quantiletype = 7,L5M
HFENplusi = which(colnames(metashort) == "HFENplus")
MADi = which(colnames(metashort) == "MAD")
ENi = which(colnames(metashort) == "EN")
ENMOai = which(colnames(metashort) == "ENMOa")
ANYANGLEi = which(colnames(M$metashort) %in% c("anglex","angley","anglez") == TRUE)
if (length(ANYANGLEi) == 0) ANYANGLEi = -1
if (length(ENMOi) == 0) ENMOi = -1
Expand All @@ -149,6 +150,7 @@ g.analyse = function(I,C,M,IMP,qlevels=c(),qwindow=c(0,24),quantiletype = 7,L5M
if (length(HFENplusi) == 0) HFENplusi = -1
if (length(MADi) == 0) MADi = -1
if (length(ENi) == 0) ENi = -1
if (length(ENMOai) == 0) ENMOai = -1
#===============================================
# Extract features from the imputed data
qcheck = r5long
Expand Down Expand Up @@ -176,14 +178,13 @@ g.analyse = function(I,C,M,IMP,qlevels=c(),qwindow=c(0,24),quantiletype = 7,L5M
qlevels_names = output_avday$qlevels_names
ML5AD=output_avday$ML5AD
ML5AD_names = output_avday$ML5AD_names

#--------------------------------------------------------------
# Analysis per day
if (doperday == TRUE) {
output_perday = g.analyse.perday(selectdaysfile, ndays, firstmidnighti, time, nfeatures,
window.summary.size, qwindow, midnightsi, metashort, averageday,
ENMOi, LFENMOi, BFENi, ENi,
HFENi, HFENplusi, MADi, doiglevels, nfulldays, lastmidnight,
HFENi, HFENplusi, MADi, ENMOai, doiglevels, nfulldays, lastmidnight,
ws3, ws2, qcheck, fname, idloc, BodyLocation, wdayname,
tooshort, includedaycrit, winhr,L5M5window, M5L5res,
doquan, qlevels, quantiletype, doilevels, ilevels, iglevels, domvpa,
Expand Down
4 changes: 2 additions & 2 deletions R/g.analyse.perday.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
g.analyse.perday = function(selectdaysfile, ndays, firstmidnighti, time, nfeatures,
window.summary.size, qwindow, midnightsi, metashort, averageday,
ENMOi, LFENMOi, BFENi, ENi, HFENi, HFENplusi, MADi,
ENMOi, LFENMOi, BFENi, ENi, HFENi, HFENplusi, MADi, ENMOai,
doiglevels, nfulldays,lastmidnight, ws3, ws2, qcheck,
fname, idloc, BodyLocation, wdayname, tooshort, includedaycrit,
winhr, L5M5window, M5L5res,
Expand Down Expand Up @@ -300,7 +300,7 @@ g.analyse.perday = function(selectdaysfile, ndays, firstmidnighti, time, nfeatur
}
# Starting filling output matrix daysummary with variables per day segment and full day.
if (mi == ENMOi | mi == LFENMOi | mi == BFENi |
mi == ENi | mi == HFENi | mi == HFENplusi | mi == MADi) {
mi == ENi | mi == HFENi | mi == HFENplusi | mi == MADi | mi == ENMOai) {
if (length(varnum) > ((60/ws3)*60*5.5)) { # Calculate values
exfi = 0
for (winhr_value in winhr) {
Expand Down
10 changes: 5 additions & 5 deletions R/g.part3.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ g.part3 = function(metadatadir=c(),f0,f1,anglethreshold = 5,timethreshold = 5,
dir.create(file.path(paste(metadatadir,"/meta",sep=""),"sleep.qc"))
}
#------------------------------------------------------
fnames =dir(paste(metadatadir,"/meta/basic",sep=""))
fnames =dir(paste(metadatadir,"/meta/ms2.out",sep=""))
if (f1 > length(fnames) | f1 == 0) f1 = length(fnames)
if (f0 > length(fnames) | f0 == 0) f0 = 1
#========================================================================
Expand Down Expand Up @@ -67,14 +67,14 @@ g.part3 = function(metadatadir=c(),f0,f1,anglethreshold = 5,timethreshold = 5,
.export=functions2passon, .errorhandling=errhand) %myinfix% {
tryCatchResult = tryCatch({
# for (i in f0:f1) {
FI = file.info(paste(metadatadir,"/meta/basic/",fnames[i],sep=""))
FI = file.info(paste(metadatadir,"/meta/ms2.out/",fnames[i],sep=""))
if (is.na(FI$size) == TRUE) FI$size = 0
if (FI$size == 0 | is.na(FI$size) == TRUE | length(FI$size) == 0) {
cat(paste("P3 file ",fnames[i],sep=""))
cat("Filename not recognised")
}
fname = unlist(strsplit(fnames[i],".RData"))[1]
fname = unlist(strsplit(fname,"eta_"))[2]
# fname = unlist(strsplit(fname,"eta_"))[2]
#=========================================================
#check whether file has already been processed
#by comparing filename to read with list of processed files
Expand All @@ -92,8 +92,8 @@ g.part3 = function(metadatadir=c(),f0,f1,anglethreshold = 5,timethreshold = 5,
# Load previously stored meta-data from part1.R
cat(paste(" ",i,sep=""))
IMP = M = c()
load(paste(metadatadir,"/meta/basic/",fnames[i],sep=""))
load(paste(metadatadir,"/meta/ms2.out/",unlist(strsplit(fnames[i], "eta_"))[2],sep=""))
load(paste(metadatadir,"/meta/basic/meta_",fnames[i],sep=""))
load(paste(metadatadir,"/meta/ms2.out/",fnames[i],sep=""))
if (M$filecorrupt == FALSE & M$filetooshort == FALSE) {
# IMP = g.impute(M,I,strategy=1,hrs.del.start=0,hrs.del.end=0,maxdur=0)
SLE = g.sib.det(M,IMP,I,twd=c(-12,12),timethreshold=timethreshold,anglethreshold=anglethreshold,
Expand Down
15 changes: 12 additions & 3 deletions R/g.part4.R
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ g.part4 = function(datadir=c(),metadatadir=c(),f0=f0,f1=f1,idloc=1,loglocation =
calendardate[j] = DD$calendardate
}
spo = DD$spo
reversetime2 = reversetime3 = c()
if (daysleeper[j] == TRUE) {
if (loaddaysi == 1) {
w1 = which(spo[,3] >= 18) #only use periods ending after 6pm
Expand Down Expand Up @@ -461,14 +462,12 @@ g.part4 = function(datadir=c(),metadatadir=c(),f0=f0,f1=f1,idloc=1,loglocation =
name2 = paste("spo_day2",k,sep="")
tmpCmd = paste("spo = rbind(",name1,",",name2,")",sep="")
eval(parse(text=tmpCmd))
#reverse back the timestamps to remember that these timestamps were coming from different days
spo[which(spo[,3] >= 36),3] = spo[which(spo[,3] >= 36),3] - 24
spo[which(spo[,2] >= 36),2] = spo[which(spo[,2] >= 36),2] - 24
}
}
# spo is now a matrix of onset and wake for each sleep period (episode)
for (evi in 1:nrow(spo)) { #Now classify as being part of the SPT window or not
if (spo[evi,2] < SptWake & spo[evi,3] > SptOnset) { # = acconset < logwake & accwake > logonset

spo[evi,4] = 1 #nocturnal = all acc periods that end after diary onset and start before diary wake
# REDEFINITION OF ONSET/WAKE OF THIS PERIOD OVERLAPS
if (relyonsleeplog == TRUE) { #if TRUE then sleeplog value is assigned to accelerometer-based value for onset and wake up
Expand All @@ -481,6 +480,14 @@ g.part4 = function(datadir=c(),metadatadir=c(),f0=f0,f1=f1,idloc=1,loglocation =
}
}
}
if (daysleeper[j] == TRUE) {
# for the labelling above it was needed to have times > 36, but for the plotting
# time in the second day needs to be returned to a normal 24 hour scale.
reversetime2 = which(spo[,2] >= 36)
reversetime3 = which(spo[,3] >= 36)
if (length(reversetime2) > 0) spo[reversetime2,2] = spo[reversetime2,2] - 24
if (length(reversetime3) > 0) spo[reversetime3,3] = spo[reversetime3,3] - 24
}
#------------------------------------------------------------------------
# Variable 'spo' contains all the sleep periods FOR ONE SLEEP DEFINITION
# Variable 'spocum' contains all the sleep periods FOR MULTIPLE SLEEP DEFINITIONS
Expand Down Expand Up @@ -556,6 +563,8 @@ g.part4 = function(datadir=c(),metadatadir=c(),f0=f0,f1=f1,idloc=1,loglocation =

#------------------------------------
# ACCELEROMETER
if (is.matrix(spocum.t) == FALSE) spocum.t = as.matrix(spocum.t) # seems needed in rare occasions
if (ncol(spocum.t) < 4 & nrow(spocum.t) > 3) spocum.t = t(spocum.t) # seems needed in rare occasions
if (length(which(as.numeric(spocum.t[,4]) == 1)) > 0) {
rtl = which(spocum.t[,4] == 1)
nightsummary[sumi,3] =spocum.t[rtl[1],2]
Expand Down
Loading

0 comments on commit 05fa05e

Please sign in to comment.