Skip to content

Commit

Permalink
don't resample more temperature than will be used
Browse files Browse the repository at this point in the history
GGIRread::resample is very expensive, and we were resampling *all* of the temperature every time we needed to read a block of it.

This edit makes g.readaccfile for movisens got 2x faster.
  • Loading branch information
l-k- committed Jan 30, 2024
1 parent 19190b5 commit bf6398a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions R/g.readaccfile.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ g.readaccfile = function(filename, blocksize, blocknumber, filequality,
# there may or may not be a temp.bin file containing temperature
try(expr = {P$data$temperature = g.readtemp_movisens(filename,
from = startpage, to = endpage,
acc_sf = sf, acc_length = nrow(P$data),
interpolationType = params_rawdata[["interpolationType"]])
}, silent = TRUE)
} else if (mon == MONITOR$ACTIGRAPH && dformat == FORMAT$GT3X) {
Expand Down
27 changes: 19 additions & 8 deletions R/g.readtemp_movisens.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
g.readtemp_movisens = function(datafile, from = c(), to = c(), interpolationType=1) {
g.readtemp_movisens = function(datafile, from = c(), to = c(), acc_sf, acc_length, interpolationType=1) {
# Acceleration data and temperature were sampled at different rates,
# so we need to resample temperature to get the same sampling rate
# as what we have for acceleration.

temperature = unisensR::readUnisensSignalEntry(dirname(datafile), "temp.bin")
temp_sf = 1 # temperature is most likely sampled at 1Hz, but we'll double-check later

temp_from = ceiling(from / acc_sf * temp_sf)
temp_to = ceiling(to / acc_sf * temp_sf)

temperature = unisensR::readUnisensSignalEntry(dirname(datafile), "temp.bin",
startIndex = temp_from, endIndex = temp_to)
new_temp_sf = attr(temperature, "sampleRate")

# We had guessed that temperature was sampled at 1Hz. Let's check, and if we were wrong, then re-do.
if (temp_sf != new_temp_sf) {
temp_from = ceiling(from / acc_sf * new_temp_sf)
temp_to = ceiling(to / acc_sf * new_temp_sf)

temperature = unisensR::readUnisensSignalEntry(dirname(datafile), "temp.bin",
startIndex = temp_from, endIndex = temp_to)
}

temperature = temperature$temp

# we don't care about the exact timestamp values because we'll throw the timestamps away anyway.
rawTime = seq_len(length(temperature))

acc_length = unisensR::getUnisensSignalSampleCount(dirname(datafile), "acc.bin")
timeRes = seq(from = 1, to = rawTime[length(rawTime)], length.out = acc_length)

temperature = GGIRread::resample(as.matrix(temperature), rawTime, timeRes, length(temperature), type=interpolationType)

if(length(from) > 0 && length(to) > 0) {
temperature = temperature[from:to]
}

invisible(temperature)
}
11 changes: 9 additions & 2 deletions man/g.readtemp_movisens.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Reads the temperature from movisens files.
it to the matrix where accelerations are stored
}
\usage{
g.readtemp_movisens(datafile, from = c(), to = c(), interpolationType=1)
g.readtemp_movisens(datafile, from = c(), to = c(), acc_sf, acc_length,
interpolationType=1)
}
\arguments{
\item{datafile}{
Expand All @@ -24,6 +25,12 @@ g.readtemp_movisens(datafile, from = c(), to = c(), interpolationType=1)
End point to derive the temperature from movisens files (automatically
calculated by GGIR)
}
\item{acc_sf}{
Sample frequency of acceleration data
}
\item{acc_length}{
number of acceleration data samples
}
\item{interpolationType}{
Integer to indicate type of interpolation to be used when resampling time series (mainly relevant for Axivity sensors), 1=linear, 2=nearest neighbour.
}
Expand All @@ -34,6 +41,6 @@ g.readtemp_movisens(datafile, from = c(), to = c(), interpolationType=1)
\keyword{internal}
\examples{
\dontrun{
P = g.readtemp_movisens(datafile, from = c(), to = c())
P = g.readtemp_movisens(datafile, from = c(), to = c(), acc_sf = 64, acc_length = 3000)
}
}

0 comments on commit bf6398a

Please sign in to comment.