Skip to content

Commit

Permalink
fix bug when ndayswindow is higher than the days recorded in part 5 a…
Browse files Browse the repository at this point in the history
…nd minor change to g.plot to facilitate visualization of the study protocol
  • Loading branch information
jhmigueles committed Sep 19, 2023
1 parent 41161e3 commit e79190f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion R/g.impute.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,16 @@ g.impute = function(M, I, params_cleaning = c(), desiredtz = "",
if (p1 > length(atest)) break
atestlist[ati] = mean(atest[p0:p1], na.rm = TRUE)
}
atik = which(atestlist == max(atestlist))[1]
atik = 1 # intitialise atik as 1, which will be used in case ndayswindow is longer than recording days
if (!is.null(atestlist)) atik = which(atestlist == max(atestlist))[1]
if (firstmidnighti != 1) { #ignore everything before the first midnight plus hrs.del.start
ignore_until = (midnightsi[atik] - 1) + (params_cleaning[["hrs.del.start"]]*(3600/ws2))
r4[1:ignore_until] = 1 #-1 because first midnight 00:00 itself contributes to the first full day
}
#ignore everything after the last midnight plus hrs.del.end
ignore_from = midnightsi[atik + params_cleaning[["ndayswindow"]]] - (params_cleaning[["hrs.del.end"]]*(3600/ws2))
# if ndayswindow is higher than number of midnights, use last midnight
if (is.na(ignore_from)) ignore_from = midnightsi[length(midnightsi)] - (params_cleaning[["hrs.del.end"]]*(3600/ws2))
r4[ignore_from:length(r4)] = 1
}
starttimei = 1
Expand Down
19 changes: 13 additions & 6 deletions R/g.plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,16 @@ g.plot = function(IMP, M, I, durplot) {
}

MEND = length(timeline)
ticks = seq(0, nrow(M$metalong) + n_ws2_perday, by = n_ws2_perday)
mnights = grep("T00:00:00", M$metalong$timestamp)
noons = grep("T12:00:00", M$metalong$timestamp)
ticks = sort(c(mnights, noons))
if (mnights[1] < noons[1]) {
tick_labels = rep(c("00", "12"), length.out = length(ticks))
} else if (noons[1] < mnights[1]) {
tick_labels = rep(c("12", "00"), length.out = length(ticks))
}
# creating plot functions to avoid duplicated code
plot_acc = function(timeline, Acceleration, durplot, ticks, metricName) {
plot_acc = function(timeline, Acceleration, durplot, ticks, metricName, tick_labels) {
if (metricName %in% c("ZCX", "ZCY", "ZCX") == TRUE |
length(grep(pattern = "count", x = metricName, ignore.case = TRUE)) > 0) {
# Metric is not on a G scale
Expand All @@ -153,11 +160,11 @@ g.plot = function(IMP, M, I, durplot) {
YLIM = c(0, 0.6)
YTICKS = c(0, 0.2, 0.4, 0.6)
}
plot(timeline, Acceleration, type = "l", xlab = "Day (24 hour blocks relative to start of recording)",
plot(timeline, Acceleration, type = "l", xlab = "Time (Hours)",
ylab = ylabel,
bty = "l", lwd = 0.1, xlim = c(0, durplot), ylim = YLIM, axes = FALSE, cex.lab = 0.8)
axis(side = 2, at = YTICKS)
axis(side = 1, at = ticks, labels = 0:(length(ticks) - 1))
axis(side = 1, at = ticks, labels = tick_labels)
}

plot_nonwear = function(timeline, M, durplot, ticks) {
Expand All @@ -171,7 +178,7 @@ g.plot = function(IMP, M, I, durplot) {
if (mon == MONITOR$GENEACTIV || (mon == MONITOR$AXIVITY && dformat == FORMAT$CWA)) {
# Recordings with temperature
par(fig = c(0,1,0,0.65), new = T)
plot_acc(timeline, Acceleration, durplot, ticks, metricName)
plot_acc(timeline, Acceleration, durplot, ticks, metricName, tick_labels)

par(fig = c(0, 1, 0.45, 0.80), new = T)
plot_nonwear(timeline, M, durplot, ticks)
Expand All @@ -187,7 +194,7 @@ g.plot = function(IMP, M, I, durplot) {
} else {
# Recordings without temperature
par(fig = c(0, 1, 0, 0.80), new = T)
plot_acc(timeline, Acceleration, durplot, ticks, metricName)
plot_acc(timeline, Acceleration, durplot, ticks, metricName, tick_labels)

par(fig = c(0, 1, 0.60, 0.95), new = T)
plot_nonwear(timeline, M, durplot, ticks)
Expand Down

0 comments on commit e79190f

Please sign in to comment.