45
45
# '
46
46
# ' @author Ariana Strandburg-Peshkin (primary author)
47
47
# ' @author Eli Strauss (code reviewer, May 2024)
48
+ # ' @author Reviewed by Brock (Jan 2025)
48
49
# '
49
50
# ' @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)
50
51
# ' @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,
132
133
names = NULL ,
133
134
break_by_day = F
134
135
){
135
- checkmate :: assert_matrix(xs , ' numeric' )
136
- checkmate :: assert_matrix(ys , ' numeric' )
136
+ checkmate :: assert_matrix(xs , ' numeric' )
137
+ checkmate :: assert_matrix(ys , ' numeric' )
137
138
# error checking - xs and ys matrices
138
139
if (nrow(xs ) != nrow(ys ) | ncol(xs ) != ncol(ys )){
139
140
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,
226
227
227
228
# go backwards from crossing points into inner radius to find the 'starts' when crossed the outer radius
228
229
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
+
229
236
if (length(inner_starts )== 0 ){
230
237
together [i ,j ,t_day ] <- together [j ,i ,t_day ] <- together_ij
231
238
next
232
239
}
233
240
for (k in 1 : length(inner_starts )){
234
241
crossing <- inner_starts [k ]
235
- curr_time <- crossing
236
242
for (curr_time in seq(crossing ,1 ,- 1 )){
237
243
# # If NA, treat as though they are outside of together_outer
238
244
if (is.na(together_outer [curr_time ])){
@@ -251,7 +257,7 @@ identify_splits_and_merges <- function(xs, ys, timestamps, R_inner, R_outer,
251
257
}
252
258
253
259
}
254
- together_ij [start : crossing ] <- T
260
+ together_ij [( start + 1 ) : crossing ] <- T # fixed
255
261
}
256
262
257
263
# 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,
262
268
}
263
269
for (k in 1 : length(inner_ends )){
264
270
crossing <- inner_ends [k ]
265
- curr_time <- crossing
266
271
for (curr_time in seq(crossing ,length(together_ij ),1 )){
267
272
# # If NA, treat as though they are outside of together_outer
268
273
if (is.na(together_outer [curr_time ])){
@@ -281,7 +286,7 @@ identify_splits_and_merges <- function(xs, ys, timestamps, R_inner, R_outer,
281
286
}
282
287
283
288
}
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"
285
290
}
286
291
287
292
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,
341
346
342
347
# for each time when the subgrouping patterns changed...
343
348
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
344
359
event_idx <- 1
345
360
for (tidx in 1 : length(event_times )){
346
361
# print(tidx)
0 commit comments