build a familty for single trap
H QIn
Jane 16, 2018
setup the systems
rm(list=ls())
debug = 0;
read null distribution from single cell data
moving_buffer = read.csv("data/moving_buffer.csv")
p_dist = function(x, input_buffer) {
input_buffer = input_buffer[ !is.na(input_buffer)];
lower = input_buffer[ input_buffer < x]; lower = lower[!is.na(lower)];
upper = input_buffer[input_buffer > x]; upper = upper[!is.na(upper)];
LowerP = (length(lower) / length(input_buffer))
UpperP = (length(upper) / length(input_buffer))
return(list(LowerP,UpperP, mean(input_buffer, na.rm=T), sd(input_buffer, na.rm=T)))
}
my_dist = function(x1, y1, x2, y2) {sqrt( (x1 - x2)^2 + (y1-y2)^2 ) };
Load the segmented data
DataTrap <- read.csv("data/DataSet_v6.csv",header=TRUE,stringsAsFactors = FALSE)
Take one trap for the time beling to develop family tree
tb = DataTrap[DataTrap$trap_num==17, ]
tb$predecessorID = NA;
tb$successorID = NA;
timpoints = sort(unique(tb$Image_num))
plot( timpoints, 1:length(timpoints), type='l') #visual check timepoints.
Work from bottom to top to build a family tree using trace-back.
# i= 200;
for (i in length(timpoints):2){
print(paste("i=",i));
current_frame = tb[ tb$Image_num == timpoints[i], ];
previous_frame = tb[ tb$Image_num == timpoints[i-1], ];
d.tb = p.d.tb = data.frame(matrix(nrow=length(current_frame[,1]), ncol=length(previous_frame[,1]) ));
for ( i.cur in 1:length(current_frame[,1]) ) {
for ( j.pre in 1:length(previous_frame[,1]) ) {
d.tb[i.cur, j.pre] = my_dist(current_frame$cell_X[i.cur], current_frame$cell_Y[i.cur],
previous_frame$cell_X[j.pre], previous_frame$cell_Y[j.pre]);
#my_dist = function(x1, y1, x2, y2) {sqrt( (x1 - x2)^2 + (y1-y2)^2 ) };
p.d.tb[i.cur, j.pre] = p_dist(d.tb[i.cur, j.pre], moving_buffer$d )[[2]];
}
current_frame$predecessorID[i.cur] = which.max( p.d.tb[i.cur, ]); #pick max, greedy algorithm.
}
tb$predecessorID[tb$Image_num == timpoints[i]] = current_frame$predecessorID; #assume order unchanged.
}
## [1] "i= 390"
## [1] "i= 389"
## [1] "i= 388"
## [1] "i= 387"
## [1] "i= 386"
## [1] "i= 385"
## [1] "i= 384"
## [1] "i= 383"
## [1] "i= 382"
## [1] "i= 381"
## [1] "i= 380"
## [1] "i= 379"
## [1] "i= 378"
## [1] "i= 377"
## [1] "i= 376"
## [1] "i= 375"
## [1] "i= 374"
## [1] "i= 373"
## [1] "i= 372"
## [1] "i= 371"
## [1] "i= 370"
## [1] "i= 369"
## [1] "i= 368"
## [1] "i= 367"
## [1] "i= 366"
## [1] "i= 365"
## [1] "i= 364"
## [1] "i= 363"
## [1] "i= 362"
## [1] "i= 361"
## [1] "i= 360"
## [1] "i= 359"
## [1] "i= 358"
## [1] "i= 357"
## [1] "i= 356"
## [1] "i= 355"
## [1] "i= 354"
## [1] "i= 353"
## [1] "i= 352"
## [1] "i= 351"
## [1] "i= 350"
## [1] "i= 349"
## [1] "i= 348"
## [1] "i= 347"
## [1] "i= 346"
## [1] "i= 345"
## [1] "i= 344"
## [1] "i= 343"
## [1] "i= 342"
## [1] "i= 341"
## [1] "i= 340"
## [1] "i= 339"
## [1] "i= 338"
## [1] "i= 337"
## [1] "i= 336"
## [1] "i= 335"
## [1] "i= 334"
## [1] "i= 333"
## [1] "i= 332"
## [1] "i= 331"
## [1] "i= 330"
## [1] "i= 329"
## [1] "i= 328"
## [1] "i= 327"
## [1] "i= 326"
## [1] "i= 325"
## [1] "i= 324"
## [1] "i= 323"
## [1] "i= 322"
## [1] "i= 321"
## [1] "i= 320"
## [1] "i= 319"
## [1] "i= 318"
## [1] "i= 317"
## [1] "i= 316"
## [1] "i= 315"
## [1] "i= 314"
## [1] "i= 313"
## [1] "i= 312"
## [1] "i= 311"
## [1] "i= 310"
## [1] "i= 309"
## [1] "i= 308"
## [1] "i= 307"
## [1] "i= 306"
## [1] "i= 305"
## [1] "i= 304"
## [1] "i= 303"
## [1] "i= 302"
## [1] "i= 301"
## [1] "i= 300"
## [1] "i= 299"
## [1] "i= 298"
## [1] "i= 297"
## [1] "i= 296"
## [1] "i= 295"
## [1] "i= 294"
## [1] "i= 293"
## [1] "i= 292"
## [1] "i= 291"
## [1] "i= 290"
## [1] "i= 289"
## [1] "i= 288"
## [1] "i= 287"
## [1] "i= 286"
## [1] "i= 285"
## [1] "i= 284"
## [1] "i= 283"
## [1] "i= 282"
## [1] "i= 281"
## [1] "i= 280"
## [1] "i= 279"
## [1] "i= 278"
## [1] "i= 277"
## [1] "i= 276"
## [1] "i= 275"
## [1] "i= 274"
## [1] "i= 273"
## [1] "i= 272"
## [1] "i= 271"
## [1] "i= 270"
## [1] "i= 269"
## [1] "i= 268"
## [1] "i= 267"
## [1] "i= 266"
## [1] "i= 265"
## [1] "i= 264"
## [1] "i= 263"
## [1] "i= 262"
## [1] "i= 261"
## [1] "i= 260"
## [1] "i= 259"
## [1] "i= 258"
## [1] "i= 257"
## [1] "i= 256"
## [1] "i= 255"
## [1] "i= 254"
## [1] "i= 253"
## [1] "i= 252"
## [1] "i= 251"
## [1] "i= 250"
## [1] "i= 249"
## [1] "i= 248"
## [1] "i= 247"
## [1] "i= 246"
## [1] "i= 245"
## [1] "i= 244"
## [1] "i= 243"
## [1] "i= 242"
## [1] "i= 241"
## [1] "i= 240"
## [1] "i= 239"
## [1] "i= 238"
## [1] "i= 237"
## [1] "i= 236"
## [1] "i= 235"
## [1] "i= 234"
## [1] "i= 233"
## [1] "i= 232"
## [1] "i= 231"
## [1] "i= 230"
## [1] "i= 229"
## [1] "i= 228"
## [1] "i= 227"
## [1] "i= 226"
## [1] "i= 225"
## [1] "i= 224"
## [1] "i= 223"
## [1] "i= 222"
## [1] "i= 221"
## [1] "i= 220"
## [1] "i= 219"
## [1] "i= 218"
## [1] "i= 217"
## [1] "i= 216"
## [1] "i= 215"
## [1] "i= 214"
## [1] "i= 213"
## [1] "i= 212"
## [1] "i= 211"
## [1] "i= 210"
## [1] "i= 209"
## [1] "i= 208"
## [1] "i= 207"
## [1] "i= 206"
## [1] "i= 205"
## [1] "i= 204"
## [1] "i= 203"
## [1] "i= 202"
## [1] "i= 201"
## [1] "i= 200"
## [1] "i= 199"
## [1] "i= 198"
## [1] "i= 197"
## [1] "i= 196"
## [1] "i= 195"
## [1] "i= 194"
## [1] "i= 193"
## [1] "i= 192"
## [1] "i= 191"
## [1] "i= 190"
## [1] "i= 189"
## [1] "i= 188"
## [1] "i= 187"
## [1] "i= 186"
## [1] "i= 185"
## [1] "i= 184"
## [1] "i= 183"
## [1] "i= 182"
## [1] "i= 181"
## [1] "i= 180"
## [1] "i= 179"
## [1] "i= 178"
## [1] "i= 177"
## [1] "i= 176"
## [1] "i= 175"
## [1] "i= 174"
## [1] "i= 173"
## [1] "i= 172"
## [1] "i= 171"
## [1] "i= 170"
## [1] "i= 169"
## [1] "i= 168"
## [1] "i= 167"
## [1] "i= 166"
## [1] "i= 165"
## [1] "i= 164"
## [1] "i= 163"
## [1] "i= 162"
## [1] "i= 161"
## [1] "i= 160"
## [1] "i= 159"
## [1] "i= 158"
## [1] "i= 157"
## [1] "i= 156"
## [1] "i= 155"
## [1] "i= 154"
## [1] "i= 153"
## [1] "i= 152"
## [1] "i= 151"
## [1] "i= 150"
## [1] "i= 149"
## [1] "i= 148"
## [1] "i= 147"
## [1] "i= 146"
## [1] "i= 145"
## [1] "i= 144"
## [1] "i= 143"
## [1] "i= 142"
## [1] "i= 141"
## [1] "i= 140"
## [1] "i= 139"
## [1] "i= 138"
## [1] "i= 137"
## [1] "i= 136"
## [1] "i= 135"
## [1] "i= 134"
## [1] "i= 133"
## [1] "i= 132"
## [1] "i= 131"
## [1] "i= 130"
## [1] "i= 129"
## [1] "i= 128"
## [1] "i= 127"
## [1] "i= 126"
## [1] "i= 125"
## [1] "i= 124"
## [1] "i= 123"
## [1] "i= 122"
## [1] "i= 121"
## [1] "i= 120"
## [1] "i= 119"
## [1] "i= 118"
## [1] "i= 117"
## [1] "i= 116"
## [1] "i= 115"
## [1] "i= 114"
## [1] "i= 113"
## [1] "i= 112"
## [1] "i= 111"
## [1] "i= 110"
## [1] "i= 109"
## [1] "i= 108"
## [1] "i= 107"
## [1] "i= 106"
## [1] "i= 105"
## [1] "i= 104"
## [1] "i= 103"
## [1] "i= 102"
## [1] "i= 101"
## [1] "i= 100"
## [1] "i= 99"
## [1] "i= 98"
## [1] "i= 97"
## [1] "i= 96"
## [1] "i= 95"
## [1] "i= 94"
## [1] "i= 93"
## [1] "i= 92"
## [1] "i= 91"
## [1] "i= 90"
## [1] "i= 89"
## [1] "i= 88"
## [1] "i= 87"
## [1] "i= 86"
## [1] "i= 85"
## [1] "i= 84"
## [1] "i= 83"
## [1] "i= 82"
## [1] "i= 81"
## [1] "i= 80"
## [1] "i= 79"
## [1] "i= 78"
## [1] "i= 77"
## [1] "i= 76"
## [1] "i= 75"
## [1] "i= 74"
## [1] "i= 73"
## [1] "i= 72"
## [1] "i= 71"
## [1] "i= 70"
## [1] "i= 69"
## [1] "i= 68"
## [1] "i= 67"
## [1] "i= 66"
## [1] "i= 65"
## [1] "i= 64"
## [1] "i= 63"
## [1] "i= 62"
## [1] "i= 61"
## [1] "i= 60"
## [1] "i= 59"
## [1] "i= 58"
## [1] "i= 57"
## [1] "i= 56"
## [1] "i= 55"
## [1] "i= 54"
## [1] "i= 53"
## [1] "i= 52"
## [1] "i= 51"
## [1] "i= 50"
## [1] "i= 49"
## [1] "i= 48"
## [1] "i= 47"
## [1] "i= 46"
## [1] "i= 45"
## [1] "i= 44"
## [1] "i= 43"
## [1] "i= 42"
## [1] "i= 41"
## [1] "i= 40"
## [1] "i= 39"
## [1] "i= 38"
## [1] "i= 37"
## [1] "i= 36"
## [1] "i= 35"
## [1] "i= 34"
## [1] "i= 33"
## [1] "i= 32"
## [1] "i= 31"
## [1] "i= 30"
## [1] "i= 29"
## [1] "i= 28"
## [1] "i= 27"
## [1] "i= 26"
## [1] "i= 25"
## [1] "i= 24"
## [1] "i= 23"
## [1] "i= 22"
## [1] "i= 21"
## [1] "i= 20"
## [1] "i= 19"
## [1] "i= 18"
## [1] "i= 17"
## [1] "i= 16"
## [1] "i= 15"
## [1] "i= 14"
## [1] "i= 13"
## [1] "i= 12"
## [1] "i= 11"
## [1] "i= 10"
## [1] "i= 9"
## [1] "i= 8"
## [1] "i= 7"
## [1] "i= 6"
## [1] "i= 5"
## [1] "i= 4"
## [1] "i= 3"
## [1] "i= 2"
No comments:
Post a Comment