-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moving bout detection code to seperate function #653
- Loading branch information
1 parent
a80c178
commit c018b8a
Showing
3 changed files
with
145 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
detectEventBouts = function(myfun, varnum_event, varnum, | ||
UnitReScale, daysummary, ds_names, | ||
di, fi, ws3, boutnameEnding) { | ||
if ("ebout.dur" %in% names(myfun) == FALSE) myfun$ebout.dur = c(1, 5, 10) | ||
if ("ebout.th.cad" %in% names(myfun) == FALSE) myfun$ebout.th.cad = 30 | ||
if ("ebout.th.acc" %in% names(myfun) == FALSE) myfun$ebout.th.acc = 50 | ||
if ("ebout.criter" %in% names(myfun) == FALSE) myfun$ebout.criter = 0.8 | ||
if ("ebout.condition" %in% names(myfun) == FALSE) myfun$ebout.condition = "AND" | ||
|
||
# varnum = metashort[anwindices, mi] | ||
cadence = varnum_event * (60/ws3) | ||
# Event bouts | ||
for (boutdur in myfun$ebout.dur) { | ||
boutduration = boutdur * (60/ws3) # per minute | ||
rr1 = matrix(0, length(varnum), 1) | ||
if (myfun$ebout.condition == "AND") { | ||
p = which(varnum * UnitReScale >= myfun$ebout.th.acc & | ||
cadence >= myfun$ebout.th.cad); rr1[p] = 1 | ||
} else if (myfun$ebout.condition == "OR") { | ||
p = which(varnum * UnitReScale >= myfun$ebout.th.acc | | ||
cadence >= myfun$ebout.th.cad); rr1[p] = 1 | ||
} | ||
getboutout = g.getbout(x = rr1, boutduration = boutduration, | ||
boutcriter = myfun$ebout.criter, | ||
bout.metric = 6, ws3 = ws3) | ||
# time spent in bouts in minutes | ||
eventbout = length(which(getboutout$x == 1)) / (60/ws3) | ||
eboutname = paste0("ExtFunEvent_Bout_totdur_E", ws3, "S_B", boutdur, | ||
"M", (myfun$ebout.criter * 100), | ||
"%_cadT",myfun$ebout.th.cad,"_", | ||
myfun$ebout.condition, | ||
"_accT", myfun$ebout.th.acc) | ||
ebout_varname = paste0(eboutname, "_", boutnameEnding) | ||
# fi = correct_fi(di, ds_names, fi, varname = ebout_varname) | ||
daysummary[di,fi] = eventbout # total time in ebouts | ||
ds_names[fi] = ebout_varname; | ||
fi = fi + 1 | ||
# number of bouts | ||
rle_bout = rle(as.numeric(getboutout$x)) | ||
rle_bout1 = which(rle_bout$values == 1) | ||
number_of_bouts = length(rle_bout1) | ||
|
||
eboutname = paste0("ExtFunEvent_Bout_number_E", ws3, "S_B", boutdur, | ||
"M", (myfun$ebout.criter * 100), | ||
"%_cadT",myfun$ebout.th.cad,"_", | ||
myfun$ebout.condition, | ||
"_accT", myfun$ebout.th.acc) | ||
ebout_varname = paste0(eboutname, "_", boutnameEnding) | ||
daysummary[di,fi] = number_of_bouts | ||
ds_names[fi] = ebout_varname; | ||
fi = fi + 1 | ||
# average bout duration in minutes | ||
if (number_of_bouts > 0) { | ||
mn_dur_bouts = mean(rle_bout$lengths[which(rle_bout$values == 1)]) / (60/ws3) | ||
} else { | ||
mn_dur_bouts = 0 | ||
} | ||
eboutname = paste0("ExtFunEvent_Bout_meandur_E", ws3, "S_B", boutdur, | ||
"M", (myfun$ebout.criter * 100), | ||
"%_cadT",myfun$ebout.th.cad,"_", | ||
myfun$ebout.condition, | ||
"_accT", myfun$ebout.th.acc) | ||
ebout_varname = paste0(eboutname, "_", boutnameEnding) | ||
daysummary[di,fi] = mn_dur_bouts | ||
ds_names[fi] = ebout_varname; | ||
fi = fi + 1 | ||
} | ||
invisible(list(daysummary = daysummary, ds_names = ds_names, | ||
fi = fi, di = di)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
library(GGIR) | ||
context("detectEventBouts") | ||
test_that("Detect bouts in events", { | ||
skip_on_cran() | ||
|
||
# create dummy test data | ||
metashort = data.frame(ENMO = c(rep(0, 24), rep(0.12, 36), rep(0, 24)), | ||
step_count = c(rep(0, 24), rep(5, 36), rep(0, 24))) | ||
cn_metashort = colnames(metashort) | ||
anwindices = 1:84 | ||
anwi_index = 1 | ||
varnum = metashort$step_count[anwindices] | ||
varnum_event = rep(0, length(varnum)) | ||
varnum_event[which(metashort$ENMO[anwindices] > 0.05)] = 10 | ||
ws3 = 5 | ||
anwi_nameindices = "_1234hrs" | ||
daysummary = matrix("", 1, 25) | ||
fi = 1 | ||
di = 1 | ||
ds_names = "" | ||
myfun = list(ilevels = c(0, 50, 100), | ||
clevels = c(0, 30, 50), | ||
qlevels = c(0.25, 0.5, 0.75), | ||
ebout.dur = c(1, 5, 10), | ||
ebout.th.cad = 30, | ||
ebout.th.acc = 50, | ||
ebout.criter = 1, | ||
ebout.condition = "AND") | ||
|
||
# run function | ||
eventBouts = detectEventBouts(myfun, varnum_event = varnum_event, | ||
varnum = varnum, | ||
UnitReScale = 1000, | ||
daysummary = daysummary, | ||
ds_names = ds_names, | ||
di = di, fi = fi, | ||
ws3 = ws3, | ||
boutnameEnding = "_anyRandomText") | ||
|
||
|
||
expect_equal(eventBouts$daysummary[1], "3") | ||
expect_equal(eventBouts$daysummary[2], "1") | ||
expect_equal(eventBouts$daysummary[3], "3") | ||
expect_equal(eventBouts$daysummary[4], "0") | ||
expect_equal(eventBouts$daysummary[5], "0") | ||
expect_equal(eventBouts$ds_names[5], "ExtFunEvent_Bout_number_E5S_B5M100%_cadT30_AND_accT50__anyRandomText") | ||
|
||
}) |