Skip to content

Commit

Permalink
#369 completing the addition of BrondCounts as optional metric
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentvanhees committed Dec 22, 2021
1 parent 5c0ecdf commit d336e52
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 27 deletions.
17 changes: 11 additions & 6 deletions R/g.applymetrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,19 @@ epochsize = ws3 #epochsize in seconds
#================================================
# Brond Counts
if (do.brondcounts == TRUE) {
if (ncol(data) == 4) data= data[,2:4]
mycounts = activityCounts::counts(data=data, hertz=sf,
if (ncol(data) > 3) data= data[,2:4]
mycounts = activityCounts::counts(data=data, hertz=sf,
x_axis=1, y_axis=2, z_axis=3,
start_time = Sys.time()) # ignoring timestamps, because GGIR has its own timestamps
allmetrics$BrondCount_x = mycounts[, 2]
allmetrics$BrondCount_y = mycounts[, 3]
allmetrics$BrondCount_z = mycounts[, 4]
if (sf < 30) {
warning("\nNote: activityCounts not designed for handling sample frequencies below 30 Hertz")
}
# activityCount output is per second
# aggregate to our epoch size:
allmetrics$BrondCount_x = sumPerEpoch(mycounts[,2], sf=1, epochsize)
allmetrics$BrondCount_y = sumPerEpoch(mycounts[,3], sf=1, epochsize)
allmetrics$BrondCount_z = sumPerEpoch(mycounts[,4], sf=1, epochsize)
}

return(allmetrics)
}

4 changes: 1 addition & 3 deletions R/g.getmeta.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ g.getmeta = function(datafile,desiredtz = "",windowsizes = c(5,900,3600),
if (sf == 0) stop("Sample frequency not recognised") #assume 80Hertz in the absense of any other info
header = INFI$header
options(warn=-1)
if (useRDA == FALSE) decn =g.dotorcomma(datafile,dformat,mon=mon, desiredtz=desiredtz, rmc.dec = rmc.dec)
if (useRDA == FALSE) decn =g.dotorcomma(datafile, dformat, mon=mon, desiredtz=desiredtz, rmc.dec = rmc.dec)
options(warn=0)

ID = g.getidfromheaderobject(filename=filename,header=header,dformat=dformat,mon=mon)
Expand Down Expand Up @@ -516,7 +516,6 @@ g.getmeta = function(datafile,desiredtz = "",windowsizes = c(5,900,3600),
BrondCount_x = accmetrics$BrondCount_x
BrondCount_y = accmetrics$BrondCount_y
BrondCount_z = accmetrics$BrondCount_z

#--------------------------------------------------------------------
if (length(myfun) != 0) { # apply external function to the data to extract extra features
#starttime
Expand Down Expand Up @@ -636,7 +635,6 @@ g.getmeta = function(datafile,desiredtz = "",windowsizes = c(5,900,3600),
metashort[count:(count-1+length(BrondCount_y)),col_msi] = BrondCount_y; col_msi = col_msi + 1
metashort[count:(count-1+length(BrondCount_z)),col_msi] = BrondCount_z; col_msi = col_msi + 1
}

if (length(myfun) != 0) { # if an external function is applied.
NcolEF = ncol(OutputExternalFunction)-1 # number of extra columns needed
metashort[count:(count-1+nrow(OutputExternalFunction)),col_msi:(col_msi+NcolEF)] = as.matrix(OutputExternalFunction); col_msi = col_msi + NcolEF + 1
Expand Down
2 changes: 1 addition & 1 deletion R/g.part1.R
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ g.part1 = function(datadir=c(),outputdir=c(),f0=1,f1=c(),windowsizes = c(5,900,3
do.hfx=do.hfx, do.hfy=do.hfy, do.hfz=do.hfz,
do.bfx=do.bfx, do.bfy=do.bfy, do.bfz=do.bfz,
do.zcx=do.zcx, do.zcy=do.zcy, do.zcz=do.zcz,
do.brondcounts=do.brondcounts,
lb = lb, hb = hb, n = n,
desiredtz=desiredtz, daylimit=daylimit, windowsizes=windowsizes,
tempoffset=C$tempoffset, scale=C$scale, offset=C$offset,
Expand Down Expand Up @@ -576,7 +577,6 @@ g.part1 = function(datadir=c(),outputdir=c(),f0=1,f1=c(),windowsizes = c(5,900,3
outputfoldername = unlist(strsplit(datadir,"/"))[length(unlist(strsplit(datadir,"/")))]
metadatadir = paste(outputdir,"/output_",outputfoldername,sep="")
}

rm(M); rm(I); rm(C)
# } # for loop
}
Expand Down
2 changes: 2 additions & 0 deletions R/g.shell.GGIR.R
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ g.shell.GGIR = function(mode = 1:5, datadir = c(), outputdir = c(), studyname =
cat('\n')
cat(paste0(rep('_',options()$width),collapse=''))
cat("\nPart 1\n")
print(do.brondcounts)
g.part1(datadir = datadir, outputdir = outputdir,
f0 = f0, f1 = f1, windowsizes = windowsizes,
desiredtz = desiredtz, chunksize = chunksize,
Expand All @@ -449,6 +450,7 @@ g.shell.GGIR = function(mode = 1:5, datadir = c(), outputdir = c(), studyname =
do.hfx=do.hfx, do.hfy=do.hfy, do.hfz=do.hfz,
do.bfx=do.bfx, do.bfy=do.bfy, do.bfz=do.bfz,
do.zcx=do.zcx, do.zcy=do.zcy, do.zcz=do.zcz,
do.brondcounts=do.brondcounts,
printsummary=printsummary,
do.cal = do.cal,print.filename=print.filename,
overwrite=overwrite,backup.cal.coef=backup.cal.coef,
Expand Down
1 change: 1 addition & 0 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
\itemize{
\item Part 1: Depricating function g.metric as its functionality has been taken over by g.applymetrics.
\item Part 2: Adding warning when ID cannot be extracted from file based on argument idloc.
\item Part 1: Adding BrondCounts as optional acceleration metric and dependency on activityCounts.
}
}

Expand Down
7 changes: 4 additions & 3 deletions man/g.applymetrics.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ Dataframe with metric values in columns average per epoch (ws3)
do.dev_roll_med_acc_x=FALSE,do.dev_roll_med_acc_y=FALSE,
do.dev_roll_med_acc_z=FALSE,do.enmoa=FALSE,
do.lfx=FALSE, do.lfy=FALSE, do.lfz=FALSE,
do.hfx=FALSE, do.hfy=FALSE, do.hfz=FALSE,
do.bfx=FALSE, do.bfy=FALSE, do.bfz=FALSE,
do.zcx=FALSE, do.zcy=FALSE, do.zcz=FALSE)
do.hfx=FALSE, do.hfy=FALSE, do.hfz=FALSE,
do.bfx=FALSE, do.bfy=FALSE, do.bfz=FALSE,
do.zcx=FALSE, do.zcy=FALSE, do.zcz=FALSE, do.brondcounts=FALSE)

extractedmetrics = g.applymetrics(data,n=4,sf=40,ws3=5,metrics2do)
}

Expand Down
32 changes: 18 additions & 14 deletions tests/testthat/test_part1_with_allmetrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,36 @@ test_that("Part 1 can run with all metrics", {
#=======================
# Part 1 with all metrics
Ndays = 2
create_test_acc_csv(Nmin=Ndays*1440, sf = 9) # 9 Hertz, because metric zc filters at 4 Hertz
# Using sf = 30 Hertz because metric zc filters at 4 Hertz and brondcounts requires 30 Hertz
create_test_acc_csv(Nmin = Ndays * 1440, sf = 30)
create_test_sleeplog_csv()
fn = "123A_testaccfile.csv"
sleeplog_fn = "testsleeplogfile.csv"
metadatadir = paste0(getwd(),"/output_test")
metadatadir = paste0(getwd(), "/output_test")
desiredtz="Europe/London"
dn = "output_test"
if (file.exists(dn)) unlink(dn,recursive=TRUE)
g.part1(datadir=fn,outputdir=getwd(),f0=1,f1=1,overwrite=TRUE,desiredtz=desiredtz,
if (file.exists(dn)) unlink(dn, recursive = TRUE)
g.part1(datadir = fn, outputdir = getwd(),f0 = 1 , f1 = 1, overwrite = TRUE, desiredtz = desiredtz,
studyname="test",do.cal = FALSE,
# We are not doing all the metrics, because Travis-CI cannot allocate enough memory
do.enmo = TRUE,do.lfenmo = TRUE,
do.bfen = TRUE,
do.hfenplus = TRUE,
do.mad = TRUE, do.sgAccEN=FALSE,do.zcx=TRUE, do.brondcounts=TRUE,
windowsizes = c(15,3600,3600), do.parallel=FALSE,
minimumFileSizeMB=0)
rn = dir("output_test/meta/basic/",full.names = TRUE)
do.mad = TRUE, do.sgAccEN=FALSE,do.zcx=TRUE, do.brondcounts = TRUE,
windowsizes = c(15, 3600, 3600), do.parallel = FALSE,
minimumFileSizeMB = 0)
rn = dir("output_test/meta/basic/", full.names = TRUE)
load(rn[1])
expect_that(ncol(M$metashort),equals(7))
expect_that(ncol(M$metashort),equals(10))
expect_that(nrow(M$metashort),equals(11280))
expect_that(round(mean(M$metashort$BFEN),digits=4),equals(0.0413))
expect_that(round(mean(M$metashort$LFENMO),digits=4),equals(0.0442))
expect_that(round(mean(M$metashort$HFENplus),digits=4),equals(0.0904))
expect_that(round(mean(M$metashort$MAD),digits=4),equals(0.0073))
expect_that(round(mean(M$metashort$ZCX),digits=4),equals(17.4165))
expect_that(round(mean(M$metashort$BFEN), digits = 4), equals(0.0458))
expect_that(round(mean(M$metashort$LFENMO), digits = 4), equals(0.0447))
expect_that(round(mean(M$metashort$HFENplus), digits = 4), equals(0.0914))
expect_that(round(mean(M$metashort$MAD), digits = 4), equals(0.0073))
expect_that(round(mean(M$metashort$ZCX), digits = 4), equals(14.9395))
expect_equal(sum(M$metashort$BrondCounts_x), 17630)
expect_equal(sum(M$metashort$BrondCounts_y), 61354)
expect_equal(sum(M$metashort$BrondCounts_z), 958370)

if (file.exists(fn)) file.remove(fn)
if (file.exists(sleeplog_fn)) file.remove(sleeplog_fn)
Expand Down

0 comments on commit d336e52

Please sign in to comment.