Skip to content

Commit 87a0b7a

Browse files
Fix detection of calendar date for last night in the case recording ends before noon (#68)
Fixes #65 => The date corresponding to the noon following the detected wake-up time is used to assign a calendar date to each sleep period detected. In the case that the recording ends before 12pm, then assigning the calendar date to the last sleep period detected was problematic and triggered an error. This is now fixed by using the next calendar date This also includes a minor simplification of the code in the calibration function, this does not affect functionality.
2 parents ca85fde + 3a2e1d0 commit 87a0b7a

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Reading data: minor revision of ReadAndCalibrate when reading ActiGraph devices with the Idle Sleep Mode activated #58
88
* Feature extraction: numeric features are no longer rounded to 3 decimal places #61
99
* Calibration: trycatch function applied to calibration routine for cases in which coefficients cannot be retrieved #63
10+
* Aggregation per date: fixed minor bug that broke data aggregation in the last sleep period when recording ends before noon #65
1011

1112
# actimetric 0.1.3
1213

R/ReadAndCalibrate.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,15 @@ ReadAndCalibrate = function(file, sf, blocksize, blocknumber, inspectfileobject,
8585
}
8686
# -------------------------------------------------------------------------
8787
# MODULE 3 - EXTRACT CALIBRATION COEFFICIENTS -----------------------------
88-
calCoefs = vm.error.st = vm.error.end = NULL
88+
calCoefs = list(offset = c(0,0,0), scale = c(1, 1, 1))
89+
vm.error.st = vm.error.end = NA
8990
if (do.calibration == TRUE & iteration == 1) {
9091
accCols = which(colnames(data) %in% c("x","y","z"))
91-
cal = try(calibrateRaw(data[, accCols], sf = sf, verbose = verbose),silent = T)
92-
92+
cal = try(calibrateRaw(data[, accCols], sf = sf, verbose = verbose),
93+
silent = T)
9394
if (is.list(cal)) {
9495
calCoefs = cal$calCoefs; vm.error.st = cal$vm.error.st;
9596
vm.error.end = cal$vm.error.end
96-
} else{
97-
calCoefs = list(offset = c(0,0,0), scale = c(1, 1, 1))
9897
}
9998
}
10099
# -------------------------------------------------------------------------

R/aggregate_per_date.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,14 @@ aggregate_per_date = function(tsDir, epoch, classifier, classes,
110110
start_end_nighttime_dates = NULL
111111
for (ni in 1:length(start_end_nighttime$ends)) {
112112
next_noon = which(noons > start_end_nighttime$ends[ni])[1]
113-
start_end_nighttime_dates[ni] = ts$date[noons[next_noon]]
113+
if (is.na(next_noon)) {
114+
# if there is not a next_noon, meaning that recording finished before 12pm
115+
# following the last wake up
116+
prev_noon = max(which(noons < start_end_nighttime$ends[ni]))
117+
start_end_nighttime_dates[ni] = as.character(as.Date(ts$date[noons[prev_noon]]) + 1)
118+
} else {
119+
start_end_nighttime_dates[ni] = ts$date[noons[next_noon]]
120+
}
114121
}
115122
# start_end_nighttime_dates = ts$date[start_end_nighttime$ends] # dates based on wakeup
116123
rows2fill = which(availableDates %in% start_end_nighttime_dates)

0 commit comments

Comments
 (0)