Skip to content

Commit

Permalink
tidy up syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentvanhees committed Nov 20, 2024
1 parent 036378b commit 3bdf464
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions R/MXLX.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,36 @@ MXLX = function(Y = NULL, X = 5, epochSize = 1, tseg = c(0, 24), resolutionMin =
do.MXLX = FALSE
}

nsh = 60 / resolutionMin # number of steps per hour
nes = (resolutionMin * 60) / epochSize # number of epochs per step
nStepsPerHour = 60 / resolutionMin # number of steps per hour
nEpochsPerStep = (resolutionMin * 60) / epochSize # number of epochs per step
if (do.MXLX == TRUE) { # only do the analysis if Y has values other than zero
Y = Y[((((tseg[1] - tseg[1]) * 3600)/epochSize) + 1):(((tseg[2] - tseg[1]) * 3600)/epochSize)]
Nwindows = Nwindows * nsh
Nwindows = Nwindows * nStepsPerHour
rollingMean = matrix(NA, Nwindows, 1)
for (hri in 1:Nwindows) { #e.g.9am-9pm
# start and end in terms of steps
e1 = hri - 1 #e.g. 9am
e2 = e1 + (X * nsh) #e.g. 9am + 5 hrs
indexStart = hri - 1 #e.g. 9am
indexEnd = indexStart + (X * nStepsPerHour) #e.g. 9am + 5 hrs
# start and end in terms of Y index
e1 = (e1 * nes) + 1
e2 = e2 * nes
einclude = e1:e2
if (e2 <= length(Y)) {
indexStart = (indexStart * nEpochsPerStep) + 1
indexEnd = indexEnd * nEpochsPerStep
einclude = indexStart:indexEnd
if (indexEnd <= length(Y)) {
rollingMean[hri,1] = mean(Y[einclude])
}
}
valid = which(is.na(rollingMean) == F)
minV = min(rollingMean[valid], na.rm = T)
maxV = max(rollingMean[valid], na.rm = T)
LXhr = ((which(rollingMean == minV & is.na(rollingMean) == F) - 1) / nsh) + tseg[1]
MXhr = ((which(rollingMean == maxV & is.na(rollingMean) == F) - 1) / nsh) + tseg[1]
LXvalue = minV
MXvalue = maxV
LXvalue = min(rollingMean[valid], na.rm = T)
MXvalue = max(rollingMean[valid], na.rm = T)
LXhr = ((which(rollingMean == LXvalue & is.na(rollingMean) == F) - 1) / nStepsPerHour) + tseg[1]
MXhr = ((which(rollingMean == MXvalue & is.na(rollingMean) == F) - 1) / nStepsPerHour) + tseg[1]

#-------------------------------------
if (length(LXvalue) > 1) { LXvalue = sort(LXvalue)[ceiling(length(LXvalue)/2)] }

Check warning on line 40 in R/MXLX.R

View check run for this annotation

Codecov / codecov/patch

R/MXLX.R#L40

Added line #L40 was not covered by tests
if (length(LXhr) > 1) { LXhr = sort(LXhr)[ceiling(length(LXhr)/2)] }
if (length(MXvalue) > 1) { MXvalue = sort(MXvalue)[ceiling(length(MXvalue)/2)] }
if (length(MXhr) > 1) { MXhr = sort(MXhr)[ceiling(length(MXhr)/2)] }

Check warning on line 43 in R/MXLX.R

View check run for this annotation

Codecov / codecov/patch

R/MXLX.R#L42-L43

Added lines #L42 - L43 were not covered by tests


#Note it is + 1 because first epoch is one and not zero:
LXstart = ((LXhr - tseg[1]) * (3600 / epochSize)) + 1
LXend = LXstart + (X * (3600 / epochSize)) - 1
Expand All @@ -67,7 +65,7 @@ MXLX = function(Y = NULL, X = 5, epochSize = 1, tseg = c(0, 24), resolutionMin =
MXindex1 = NA, stringsAsFactors = TRUE)
}
MXLXnames = c(paste0("L", X, "hr"), paste0("L", X), paste0("start_L", X), paste0("end_L", X),
paste0("M", X, "hr"), paste0("M", X), paste0("start_M", X), paste0("end_M", X))
paste0("M", X, "hr"), paste0("M", X), paste0("start_M", X), paste0("end_M", X))
names(MXLX) = MXLXnames
return(MXLX)
}

0 comments on commit 3bdf464

Please sign in to comment.