Skip to content

Commit aa11314

Browse files
author
Ariana Strandburg-Peshkin
committed
fixed off-by-one bug that was causing failure on second test in test-known-issues
1 parent ce618b3 commit aa11314

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

R/identify_splits_and_merges.R

+21-6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#'
4646
#' @author Ariana Strandburg-Peshkin (primary author)
4747
#' @author Eli Strauss (code reviewer, May 2024)
48+
#' @author Reviewed by Brock (Jan 2025)
4849
#'
4950
#' @param xs UTM eastings matrix (`n_inds` x `n_times` matrix where xs\[i,t\] gives the easting of individual i at time step t)
5051
#' @param ys UTM northings matrix (`n_inds` x `n_times` matrix where ys\[i,t\] gives the northing of individual i at time step t)
@@ -132,8 +133,8 @@ identify_splits_and_merges <- function(xs, ys, timestamps, R_inner, R_outer,
132133
names = NULL,
133134
break_by_day = F
134135
){
135-
checkmate::assert_matrix(xs, 'numeric')
136-
checkmate::assert_matrix(ys, 'numeric')
136+
checkmate::assert_matrix(xs, 'numeric')
137+
checkmate::assert_matrix(ys, 'numeric')
137138
#error checking - xs and ys matrices
138139
if(nrow(xs) != nrow(ys) | ncol(xs) != ncol(ys)){
139140
stop('xs and ys matrices must have same dimensions')
@@ -226,13 +227,18 @@ identify_splits_and_merges <- function(xs, ys, timestamps, R_inner, R_outer,
226227

227228
#go backwards from crossing points into inner radius to find the 'starts' when crossed the outer radius
228229
inner_starts <- which(diff(together_inner)==1)+1 ## Add 1 to make indices of differences line up with indices of together_inner
230+
231+
#if they started together, add this to "inner_starts"
232+
#if(together_inner[1] == T){
233+
# inner_starts <- c(1, inner_starts)
234+
#}
235+
229236
if(length(inner_starts)==0){
230237
together[i,j,t_day] <- together[j,i,t_day] <- together_ij
231238
next
232239
}
233240
for(k in 1:length(inner_starts)){
234241
crossing <- inner_starts[k]
235-
curr_time <- crossing
236242
for(curr_time in seq(crossing,1,-1)){
237243
## If NA, treat as though they are outside of together_outer
238244
if(is.na(together_outer[curr_time])){
@@ -251,7 +257,7 @@ identify_splits_and_merges <- function(xs, ys, timestamps, R_inner, R_outer,
251257
}
252258

253259
}
254-
together_ij[start:crossing] <- T
260+
together_ij[(start+1):crossing] <- T #fixed
255261
}
256262

257263
#go forwards from crossing points out of outer radius to find the 'ends' when crossed the outer radius
@@ -262,7 +268,6 @@ identify_splits_and_merges <- function(xs, ys, timestamps, R_inner, R_outer,
262268
}
263269
for(k in 1:length(inner_ends)){
264270
crossing <- inner_ends[k]
265-
curr_time <- crossing
266271
for(curr_time in seq(crossing,length(together_ij),1)){
267272
## If NA, treat as though they are outside of together_outer
268273
if(is.na(together_outer[curr_time])){
@@ -281,7 +286,7 @@ identify_splits_and_merges <- function(xs, ys, timestamps, R_inner, R_outer,
281286
}
282287

283288
}
284-
together_ij[crossing:end] <- T
289+
together_ij[crossing:(end-1)] <- T #fixed - this was an off by one issue where the next element outside R_outer was also being counted as "together"
285290
}
286291

287292
together[i,j,t_day] <- together[j,i,t_day] <- together_ij
@@ -341,6 +346,16 @@ identify_splits_and_merges <- function(xs, ys, timestamps, R_inner, R_outer,
341346

342347
#for each time when the subgrouping patterns changed...
343348
all_events_info <- list()
349+
350+
#if no events found, return NULL for the relevant elements
351+
if(length(event_times)==0){
352+
events_detected <- NULL
353+
all_events_info <- NULL
354+
out <- list(events_detected = events_detected, all_events_info = all_events_info, groups_list = groups_list, groups = groups, together = together, R_inner = R_inner, R_outer = R_outer)
355+
return(out)
356+
}
357+
358+
#otherwise, collect up information about each event
344359
event_idx <- 1
345360
for(tidx in 1:length(event_times)){
346361
#print(tidx)

man/get_speed.Rd

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/identify_splits_and_merges.Rd

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)