Skip to content

Commit 7951996

Browse files
author
Ariana Strandburg-Peshkin
committed
added get_together_sticky function within identify_splits_and_merges and associated testing functions. This may have fixed the issues encountered by Jack, which were possibly due to improper handling of NAs in the previous version.
1 parent 5ed3496 commit 7951996

File tree

3 files changed

+151
-4
lines changed

3 files changed

+151
-4
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export(get_pulls_and_anchors)
1717
export(get_pulls_and_anchors_with_NA)
1818
export(get_spatially_discretized_trajectories)
1919
export(get_speed)
20+
export(get_together_sticky)
2021
export(get_turn_and_speed_influence_simplified)
2122
export(identify_splits_and_merges)
2223
export(import_axytrek_gps_file)

R/get_together_sticky.R

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#'Get whether two individuals are together, using a "sticky" definition
22
#'
3-
#'
4-
3+
#' @export
54
get_together_sticky <- function(together_inner, together_outer){
5+
6+
#check lengths
7+
checkmate::assert_set_equal(length(together_inner), length(together_outer))
8+
checkmate::assert_set_equal(which(is.na(together_inner)),which(is.na(together_outer)))
9+
610
#build vector to hold whether you are together by a sticky definition
711
together_ij <- rep(NA, length(together_inner))
812

@@ -14,11 +18,18 @@ get_together_sticky <- function(together_inner, together_outer){
1418
apart_idxs <- which(!together_outer)
1519
together_ij[apart_idxs] <- FALSE
1620

17-
#make a vector to hold the information on whether your status is ambiguous or not]
21+
#NAs are considered FALSE for now (they cannot be part of together sequences), but later we will replace them all with NAs in together_ij
22+
na_idxs <- which(is.na(together_outer))
23+
together_ij[na_idxs] <- FALSE
24+
together_outer[na_idxs] <- FALSE
25+
together_inner[na_idxs] <- FALSE
26+
27+
#make a vector to hold the information on whether your status is ambiguous or not
1828
#if you are between the thresholds, your status is ambiguous
1929
status_ambig <- rep(T, length(together_inner))
2030
status_ambig[inner_idxs] <- F
2131
status_ambig[apart_idxs] <- F
32+
status_ambig[na_idxs] <- F
2233

2334
#get indexes to the ambiguous times
2435
ambig_idxs <- which(status_ambig)
@@ -69,6 +80,8 @@ get_together_sticky <- function(together_inner, together_outer){
6980

7081
#if unambiguous times were found both before and after
7182
if(found_fwd & found_bwd){
83+
#if both are NA, set together_ij at that ambiguous time to FALSE
84+
7285
#if both are outside the outer threshold, then set together_ij at that ambiguous time to FALSE
7386
#otherwise, set it to TRUE because they are together due to the "sticky" rule
7487
if(!together_outer[ambig_idx - backward] & !together_outer[ambig_idx + forward]){
@@ -94,9 +107,12 @@ get_together_sticky <- function(together_inner, together_outer){
94107
}
95108
}
96109

97-
#any NAs are not considered together
110+
#any lingering ambiguous values are turned to FALSE
98111
together_ij[which(is.na(together_ij))] <- FALSE
99112

113+
#any NAs that were in the original vectors are turned back into NAs
114+
together_ij[na_idxs] <- NA
115+
100116
return(together_ij)
101117
}
102118

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#test together sticky
2+
3+
#various situations of coming together and splitting
4+
5+
R_inner <- 0.4
6+
R_outer <- 0.6
7+
8+
a <- 1 #apart distance
9+
m <- 0.5 #mid distance (between the thresholds)
10+
t <- 0.3 #together distance
11+
12+
#Test 1
13+
dists <- c(a, a, m, m, t, t, m, m, a, a)
14+
result <- c(F, F, T, T, T, T, T, T, F, F)
15+
16+
together_inner <- dists <= R_inner
17+
together_outer <- dists <= R_outer
18+
19+
expect_equal(get_together_sticky(together_inner, together_outer), result)
20+
21+
#Test 2
22+
#apart -- mid -- apart -- mid -- together -- mid -- apart
23+
dists <- c(a, a, m, m, a, a, m, t, t, m, m, a, a)
24+
result <- c(F, F, F, F, F, F, T, T, T, T, T, F, F)
25+
26+
together_inner <- dists <= R_inner
27+
together_outer <- dists <= R_outer
28+
29+
expect_equal(get_together_sticky(together_inner, together_outer), result)
30+
31+
#Test 3
32+
#apart -- mid -- together -- mid -- together -- mid -- apart
33+
dists <- c(a, a, m, m, a, a, m, t, t, m, m, a, a)
34+
result <- c(F, F, F, F, F, F, T, T, T, T, T, F, F)
35+
36+
together_inner <- dists <= R_inner
37+
together_outer <- dists <= R_outer
38+
39+
expect_equal(get_together_sticky(together_inner, together_outer), result)
40+
41+
#Test 4
42+
#apart -- together -- mid -- apart
43+
dists <- c(a, a, t, t, m, m, a, a)
44+
result <- c(F, F, T, T, T, T, F, F)
45+
46+
together_inner <- dists <= R_inner
47+
together_outer <- dists <= R_outer
48+
49+
expect_equal(get_together_sticky(together_inner, together_outer), result)
50+
51+
#Test 5
52+
#together -- mid -- apart -- mid -- together
53+
dists <- c(t, m, a, m, t)
54+
result <- c(T, T, F, T, T)
55+
56+
together_inner <- dists <= R_inner
57+
together_outer <- dists <= R_outer
58+
59+
expect_equal(get_together_sticky(together_inner, together_outer), result)
60+
61+
#Test 6
62+
#together -- apart -- together -- apart -- together
63+
dists <- c(t, a, t, a, t)
64+
result <- c(T, F, T, F, T)
65+
66+
together_inner <- dists <= R_inner
67+
together_outer <- dists <= R_outer
68+
69+
expect_equal(get_together_sticky(together_inner, together_outer), result)
70+
71+
#Test 7
72+
#NA -- apart -- mid -- together -- mid -- apart
73+
dists <- c(NA, a, m, t, m, a)
74+
result <- c(NA, F, T, T, T, F)
75+
76+
together_inner <- dists <= R_inner
77+
together_outer <- dists <= R_outer
78+
79+
expect_equal(get_together_sticky(together_inner, together_outer), result)
80+
81+
#Test 8
82+
#apart -- mid -- together -- NA -- together -- mid -- apart
83+
dists <- c(a, m, t, NA, t, m, a)
84+
result <- c(F, T, T, NA, T, T, F)
85+
86+
together_inner <- dists <= R_inner
87+
together_outer <- dists <= R_outer
88+
89+
expect_equal(get_together_sticky(together_inner, together_outer), result)
90+
91+
#Test 9
92+
#NA -- mid -- NA -- mid -- together -- apart
93+
dists <- c(NA, m, NA, m, t, a)
94+
result <- c(NA, F, NA, T, T, F)
95+
96+
together_inner <- dists <= R_inner
97+
together_outer <- dists <= R_outer
98+
99+
expect_equal(get_together_sticky(together_inner, together_outer), result)
100+
101+
#Test 10
102+
#NA -- together -- mid -- NA -- mid -- together -- apart
103+
dists <- c(NA, t, m, NA, m, t)
104+
result <- c(NA, T, T, NA, T, T)
105+
106+
together_inner <- dists <= R_inner
107+
together_outer <- dists <= R_outer
108+
109+
expect_equal(get_together_sticky(together_inner, together_outer), result)
110+
111+
#Test 11
112+
#NA -- together -- mid -- NA -- mid -- together -- apart
113+
dists <- c(NA, t, m, NA, m, NA)
114+
result <- c(NA, T, T, NA, F, NA)
115+
116+
together_inner <- dists <= R_inner
117+
together_outer <- dists <= R_outer
118+
119+
expect_equal(get_together_sticky(together_inner, together_outer), result)
120+
121+
#Test 11
122+
#apart -- apart -- NA
123+
dists <- c(a, a, NA)
124+
result <- c(F, F, NA)
125+
126+
together_inner <- dists <= R_inner
127+
together_outer <- dists <= R_outer
128+
129+
expect_equal(get_together_sticky(together_inner, together_outer), result)
130+

0 commit comments

Comments
 (0)