-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotTrackScatterAndDensities.r
executable file
·893 lines (680 loc) · 39.6 KB
/
plotTrackScatterAndDensities.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
rfHot <- colorRampPalette(rev(brewer.pal(11,'Spectral')));
histj<- function(x,y,x.breaks,y.breaks){
c1 = as.numeric(cut(x,breaks=x.breaks));
c2 = as.numeric(cut(y,breaks=y.breaks));
mat<-matrix(0,ncol=length(y.breaks)-1,nrow=length(x.breaks)-1);
mat[cbind(c1,c2)] = 1;
return(mat)
}
## Plot Mean And Std Error Around Mean ##
plotMeanEyeV <- function(lEyeVDistMatrix,lcolour,addNewPlot=TRUE)
{
lEyeVMatrix <-lEyeVDistMatrix
nSPerX <- apply(lEyeVMatrix,2,function(x){return (NROW(x[!is.na(x)])) })
bandUpper <- apply(lEyeVMatrix,2,mean,na.rm=TRUE ) + apply(lEyeVMatrix,2,sd,na.rm=TRUE)/sqrt(nSPerX)
bandLower <- apply(lEyeVMatrix,2,mean,na.rm=TRUE ) - apply(lEyeVMatrix,2,sd,na.rm=TRUE)/sqrt(nSPerX)
##plot Only Where we Have more than 1 sample
x<- seq(0,maxDist,stepDist)[nSPerX > 1]
if (addNewPlot)
plot(x,smooth( apply(lEyeVMatrix,2,mean,na.rm=TRUE)[nSPerX > 1],kind="3R" ),
type="l",col=lcolour,lwd=3,ylim=c(0,100),xlim=c(0,4),
xlab=NA,ylab=NA,
cex.lab = FONTSZ_AXISLAB,
cex.axis=FONTSZ_AXIS)
else
lines(x,smooth(apply(lEyeVMatrix,2,mean,na.rm=TRUE)[nSPerX > 1],kind="3R" ),
type="l",col=lcolour,lwd=3,ylim=c(40,80),xlim=c(0,4),
xlab=NA,ylab=NA)
polygon(c(x, rev(x )),
c(smooth(bandUpper[nSPerX > 1]) ,
smooth(rev(bandLower[nSPerX > 1])) ),
col=lcolour,
lwd=1,ylim=c(0,100),xlab=NA,ylab=NA)
}
plotGroupMotion <- function(filtereddatAllFrames,groupStat,vexpID)
{
yTop <- 500
##Note Y plotting is inverted to match video orientation ie. yTop - posY
strDatGroup <- toString(unique(filtereddatAllFrames$group))
message("PLOT Motion Tracks of each Larva noting Hunting Episodes")
### INDIVIDUAL TRAJECTORIES - With distinct colour for each larva ####
for (i in vexpID)
{
strTrajectoryplotFileName <- paste("plots/scatter/Motion/larva/MotionTrajectories-Set-",strDatGroup,"-lID_",i,".pdf",sep="",collapse=NULL);
message(strTrajectoryplotFileName)
pdf(strTrajectoryplotFileName,width=8,height=8) #col=(as.integer(filtereddatAllFrames$expID))
par(bg="black")
par(fg="yellow")
datLarvalAllFramesHunt <- filtereddatAllFrames[filtereddatAllFrames$expID == i & filtereddatAllFrames$LEyeAngle >=G_THRESHUNTANGLE & filtereddatAllFrames$REyeAngle <= -G_THRESHUNTANGLE,]
datLarvalAllFramesAll <- filtereddatAllFrames[filtereddatAllFrames$expID == i,]
vEvent <- unique(datLarvalAllFramesAll$fileIdx)
#points(datLarvalAllFramesAll$posX,datLarvalAllFramesAll$posY,pch='.',col="white",xlim=c(80,565),ylim=c(0,500),col.axis="red")
plot(datLarvalAllFramesAll$posX,yTop-datLarvalAllFramesAll$posY,type='p',pch='.',col="white",xlim=c(80,600),ylim=c(0,yTop),col.axis="red")
points(datLarvalAllFramesHunt$posX,yTop-datLarvalAllFramesHunt$posY,pch='.',col="red",xlim=c(80,600),ylim=c(0,yTop),col.axis="red")
for (j in vEvent)
{
datEvent <- datLarvalAllFramesAll[datLarvalAllFramesAll$fileIdx==j,]
points(datEvent[1]$posX,yTop-datEvent[1]$posY,pch=12,col="red",xlim=c(80,600),ylim=c(0,500),col.axis="red")
}
sampleSize <- length(unique(datLarvalAllFramesAll$fileIdx)) #Number of Larvae Used
strtitle = paste(strCond,"Motion",collapse=NULL)
strsub = paste("#e=", sampleSize, " #F:",groupStat$totalFrames,collapse=NULL)
title(strtitle, sub = strsub, cex.main = 1.5, font.main= 1.5, col.main= "yellow", cex.sub = 1.0, font.sub = 2, col.sub = "red")
#dev.copy(jpeg,filename=paste(strTrajectoryplotFileName,"-plot.jpg",sep=""));
dev.off()
}
##### Plot ALL Larvae In The group TOgether ##
message("PLOT - Overlay All Larva of Group / noting Hunting Episodes")
strTrajectoryplotFileName <- paste("plots/scatter/Motion/group/MotionTrajectories-Set-",strDatGroup,"-All.pdf",sep="",collapse=NULL);
message(strTrajectoryplotFileName)
pdf(strTrajectoryplotFileName,width=8,height=8) #col=(as.integer(filtereddatAllFrames$expID))
par(bg="black")
par(fg="yellow")
colMap = colTraj(filtereddatAllFrames$expID);
if (length(filtereddatAllFrames$expID) == 0)
{
#plot(filtereddatAllFrames$posX,yTop-filtereddatAllFrames$posY,type='p',pch='.',lwd=1,col="grey",xlim=c(80,600),ylim=c(0,500),col.axis="red")
#plot.new()
warning(paste("No Data To plot trajectories for :",strCond) )
message(paste("No Data To plot trajectories for :",strCond) )
}
bFreshPlot = TRUE
procMotFrames = 0;
procHuntFrames = 0;
hbinXY = list(); ##List Of Binarized Trajectories
hbinHXY = list(); ##List Of Binarized Hunting Episode Trajectories
##Now PLot All Larval Tracks from the Group On the SAME PLOT ##
idx = 0
for (i in vexpID)
{
idx = idx + 1
#message(i)
datLarvalAllFramesHunt <- filtereddatAllFrames[filtereddatAllFrames$expID == i &
filtereddatAllFrames$REyeAngle <= -G_THRESHUNTANGLE &
filtereddatAllFrames$LEyeAngle >=G_THRESHUNTANGLE &
abs(filtereddatAllFrames$LEyeAngle-filtereddatAllFrames$REyeAngle) >= G_THRESHUNTVERGENCEANGLE,]
procHuntFrames = procHuntFrames + NROW(datLarvalAllFramesHunt)
datLarvalAllFramesAll <- filtereddatAllFrames[filtereddatAllFrames$expID == i,]
procMotFrames = procMotFrames + NROW(datLarvalAllFramesAll)
hbinHXY[[idx]] <- histj(datLarvalAllFramesHunt$posX,yTop-datLarvalAllFramesHunt$posY,seq(0,600,600),seq(0,yTop,20))
hbinXY[[idx]] <- histj(datLarvalAllFramesAll$posX,yTop-datLarvalAllFramesAll$posY,seq(0,640,10),seq(50,yTop,10))
#points(datLarvalAllFramesAll$posX,datLarvalAllFramesAll$posY,pch='.',col="white",xlim=c(80,565),ylim=c(0,500),col.axis="red")
if (bFreshPlot)
{
plot(datLarvalAllFramesAll$posX,yTop-datLarvalAllFramesAll$posY,type='p',pch='.',col=colMap[which(vexpID == i)],xlim=c(80,600),ylim=c(0,500),col.axis="red")
bFreshPlot = FALSE
}else
{
points(datLarvalAllFramesAll$posX,yTop-datLarvalAllFramesAll$posY,pch='.',col=colMap[which(vexpID == i)],xlim=c(80,600),ylim=c(0,500),col.axis="red")
}
points(datLarvalAllFramesHunt$posX,yTop-datLarvalAllFramesHunt$posY,pch=1,lwd=2,col="red",xlim=c(80,600),ylim=c(0,500),col.axis="red")
}##For Each Larva
sampleSize <- length(vexpID) #Number of Larvae Used
strtitle = paste(strCond,"Motion",collapse=NULL)
strsub = paste("#n=", sampleSize, " #F:",groupStat$totalFrames,
"\n #Hunts:",groupStat$groupHuntEvents,
" (mu:", format(groupStat$meanHuntingEventsPerLarva,digits =3),
" sig:",format(groupStat$stdHuntingEventsPerLarva,digits=3),") #F_h:",groupStat$huntFrames,
"R_h:", format(groupStat$groupHuntRatio,digits=2),
"(mu:",format(groupStat$meanHuntRatioPerLarva,digits=3),"sd:",format(groupStat$stdHuntRatioPerLarva,digits=3),")" ,collapse=NULL)
title(strtitle, sub = strsub, cex.main = 1.5, font.main= 1.5, col.main= "yellow", cex.sub = 1.0, font.sub = 2, col.sub = "red")
#dev.copy(device=jpeg,filename=paste(strTrajectoryplotFileName,"-plot.jpg"));
dev.off()
###### BINARIZED HISTOGRAM PER GROUP ###
## Now Sum All LArva Binarized Trajectories and Display Heat Map
hGroupbinDensity <- Reduce('+', hbinXY)
strDensityplotFileName <- paste("plots/binDensity/MotionDensity-BINSet-",strCond,".pdf",collapse=NULL,sep="");
pdf(strDensityplotFileName,width=8,height=8)
sampleSize <- length(vexpID) #Number of Larvae Used
hotMap <- c(rfHot(sampleSize),"#FF0000");
image(seq(0,640,10),seq(50,yTop,10),hGroupbinDensity,axes=TRUE,col=hotMap,xlab="Pos X",ylab="Pos Y")
title(paste(strCond,"Motion Trajectory Heatmap #n=", sampleSize, " #F:",procMotFrames),collapse=NULL);
#dev.copy(jpeg,filename=paste(strDensityplotFileName,"-plot.jpg"));
dev.off()
###
## Now Sum All LArva Binarized Hunting Episode Trajectories and Display Heat Map
hGroupbinDensity <- Reduce('+', hbinHXY)
strDensityplotFileName <- paste("plots/binDensity/MotionHuntingDensity-BINSet-",strCond,".pdf",collapse=NULL,sep="");
pdf(strDensityplotFileName,width=8,height=8)
sampleSize <- length(vexpID) #Number of Larvae Used
hotMap <- c(rfHot(sampleSize),"#FF0000");
image(seq(0,600,600),seq(0,yTop,20),hGroupbinDensity,axes=TRUE,col=hotMap,xlab="Pos X",ylab="Pos Y")
title(paste(strCond,"Motion Hunting Episode Heatmap #n=", sampleSize, " #F:",procHuntFrames),collapse=NULL);
#dev.copy(jpeg,filename=paste(strDensityplotFileName,"-plot.jpg"));
dev.off()
###
} ##End of Function
bPause <- FALSE
keydown <- function(key) {
if (key == "p")
{
bPause <- TRUE
message("KEYPRESS ")
return( bPause)
}
NULL
}
##Test PlayBack Plot Hunt Event###
renderHuntEventPlayback <- function(datHuntEventMergedFrames,preyTargetID,speed=1,saveToFolder=NA)
{
#datHuntEventMergedFrames$LEyeAngle <- meanf(datHuntEventMergedFrames$LEyeAngle,20)
#datHuntEventMergedFrames$REyeAngle <- meanf(datHuntEventMergedFrames$REyeAngle,20)
frameWidth = 610
frameHeight = 610
X_FRAME <- c(0,frameWidth)
Y_FRAME <- c(0,frameHeight)
iConeLength = 100
## (see Bianco et al. 2011) : "the functional retinal field as 163˚ after Easter and Nicola (1996)."
iConeArc = 163/2 ##Degrees Of Assumed Half FOV of Each Eye
##Eye Distance taken By Bianco As 453mum, ie 0.5mm , take tracker
EyeDist = 0.45/DIM_MMPERPX ##From Head Centre
BodyArrowLength = DIM_DISTTOMOUTH_PX
LEyecolour = "#0C0CFF2A"
REyecolour = "#FF00002A"
#display.brewer.all() to see avaulable options
Polarrfc <- colorRampPalette(rev(brewer.pal(8,'Dark2')));
datHuntEventMergedFrames <- datHuntEventMergedFrames[datHuntEventMergedFrames$posX < frameWidth &
datHuntEventMergedFrames$posY < frameHeight &
!is.na(datHuntEventMergedFrames$frameN) ,]
X11() ##Show On Screen
setGraphicsEventHandlers(prompt = "Click p to pause",
onMouseDown = NULL,
onMouseUp = NULL,
onIdle=NULL,
onKeybd = keydown,
consolePrompt="Press Key To Pause")
eventEnv <- getGraphicsEventEnv()
startFrame <- min(datHuntEventMergedFrames$frameN,na.rm =TRUE)
endFrame <- max(datHuntEventMergedFrames$frameN,na.rm =TRUE)
vPreyFrameN <- datHuntEventMergedFrames[datHuntEventMergedFrames$PreyID == preyTargetID ,]$frameN
lastPreyFrame <- max(vPreyFrameN[!is.na(vPreyFrameN)])
for (i in seq(startFrame,endFrame,speed) )
{
while (bPause)
{
key<- readline(prompt="- Press r to continue -")
if (key == 'r')
bPause <- FALSE
}
tR = (startFrame: min( c(i,endFrame ) ) )
##Multiple Copies Of Fish Can Exist As its Joined the Food Records, when tracking more than one Food Item.
## Thus When Rendering the fish Choose one of the food items that appears in the current frame range
datFishFrames <- datHuntEventMergedFrames[datHuntEventMergedFrames$frameN %in% tR,] ##in Range
vTrackedPreyIDs <- unique(datFishFrames$PreyID)
lastFrame <- i
##If this specific Frame Does not Exist In the Dat, Then Take The Last One within Range
if (NROW(datFishFrames[datFishFrames$frameN == i,]) < 1)
lastFrame <- max(datFishFrames$frameN)
##Filter The Fish Motion In the Subset PreyID Selection
#datFishFrames <- filterEyeTailNoise(datFishFrames)
recLastFishFrame <- datFishFrames[datFishFrames$frameN == lastFrame,]
##There Could Be Multiple With Thaty Frame N - Isolate Single Record ##
if (NROW(recLastFishFrame) > 1)
{
if (is.na(preyTargetID) )
#if (NROW(datFishFrames[datFishFrames$frameN == lastFrame & !is.na(datFishFrames$PreyID),]))
preyTargetID <- min(c(datFishFrames[datFishFrames$frameN == lastFrame,]$PreyID ) ) ##Choose A Prey ID found on the Last Frame The max Id F
recLastFishFrame <- datFishFrames[datFishFrames$PreyID == preyTargetID ,]
}
##Now Isolate Fish Rec, Focus on Single Prey Item
posX = recLastFishFrame$posX
posY = frameWidth-recLastFishFrame$posY
bearingRad = pi/180*(recLastFishFrame$BodyAngle-90)##+90+180 - Body Heading
TailRad <- vector()
TailRad[1] = pi/180*(recLastFishFrame$DThetaSpine_1 + recLastFishFrame$ThetaSpine_0 - 90) #Tail - bearingRad+pi
TailRad[2] = pi/180*(recLastFishFrame$DThetaSpine_2 + recLastFishFrame$ThetaSpine_0 - 90) #Tail - bearingRad+pi
TailRad[3] = pi/180*(recLastFishFrame$DThetaSpine_3 + recLastFishFrame$ThetaSpine_0 - 90) #Tail - bearingRad+pi
TailRad[4] = pi/180*(recLastFishFrame$DThetaSpine_4 + recLastFishFrame$ThetaSpine_0 - 90) #Tail - bearingRad+pi
TailRad[5] = pi/180*(recLastFishFrame$DThetaSpine_5 + recLastFishFrame$ThetaSpine_0 - 90) #Tail - bearingRad+pi
TailRad[6] = pi/180*(recLastFishFrame$DThetaSpine_6 + recLastFishFrame$ThetaSpine_0 - 90) #Tail - bearingRad+pi
TailRad[7] = pi/180*(recLastFishFrame$DThetaSpine_7 + recLastFishFrame$ThetaSpine_0 - 90) #Tail - bearingRad+pi
posVX = posX+cos(bearingRad)*BodyArrowLength
posVY = posY-sin(bearingRad)*BodyArrowLength
dev.hold()
##Plot Track
par(bg="white")
plot(datFishFrames$posX,frameWidth-datFishFrames$posY,xlim=X_FRAME,ylim=Y_FRAME,col="black",cex = .5,type='l',xlab="X",ylab="Y")
##Plot Current Frame Position
points(posX,posY,col="black",pch=16)
arrows(posX,posY,posVX,posVY)
##Draw Heading Line Of Sight In Blue
posVX2 = posX+cos(bearingRad)*BodyArrowLength*10
posVY2 = posY-sin(bearingRad)*BodyArrowLength*10
arrows(posX,posY,posVX2,posVY2,length=0.01,col="blue") ##Draw Heading Forward Arrow
##Draw Tail Segment Motion
posTX2 = posX
posTY2 = posY
for (s in 1:NROW(TailRad))
{
posTX2n <- posTX2+cos(TailRad[s])*BodyArrowLength*1
posTY2n <- posTY2-sin(TailRad[s])*BodyArrowLength*1
arrows(posTX2,posTY2,posTX2n,posTY2n,length=0.03,col="magenta") ##Draw Heading Forward Arrow
posTX2 <- posTX2n ##Next Tail Segment Is Drawn from the end of previous one
posTY2 <- posTY2n
}
##Draw Eyes
##Left Eye - Requires Inversions due to differences in How Angles Are Calculated in Tracker and In R Plots
LEyePosX <- posX-cos(bearingRad+pi/180*(45+90))*EyeDist
LEyePosY <- posY+sin(bearingRad+pi/180*(45+90))*EyeDist
#LEyeConeX <- c(LEyePosX,LEyePosX-cos(bearingRad+pi/180*(recLastFishFrame$LEyeAngle+90-iConeArc))*iConeLength, LEyePosX-cos(bearingRad+pi/180*(recLastFishFrame$LEyeAngle+90+iConeArc))*iConeLength )
#LEyeConeY <- c(LEyePosY,LEyePosY+sin(bearingRad+pi/180*(recLastFishFrame$LEyeAngle+90-iConeArc))*iConeLength, LEyePosY+sin(bearingRad+pi/180*(recLastFishFrame$LEyeAngle+90+iConeArc))*iConeLength )
nsteps = 10
rs <- seq(-iConeArc,+iConeArc,len=nsteps)
LEyeConeX <- c(LEyePosX,LEyePosX-cos(bearingRad+pi/180*(recLastFishFrame$LEyeAngle+90-rs))*iConeLength)
LEyeConeY <- c(LEyePosY, LEyePosY+sin(bearingRad+pi/180*(recLastFishFrame$LEyeAngle+90-rs))*iConeLength)
polygon(LEyeConeX,LEyeConeY,col=LEyecolour) #density=20,angle=45
##Right Eye
REyePosX <- posX-cos(bearingRad+pi/180*(-45-90))*EyeDist
REyePosY <- posY+sin(bearingRad+pi/180*(-45-90))*EyeDist
REyeConeX <- c(REyePosX,
REyePosX-cos(bearingRad+pi/180*(recLastFishFrame$REyeAngle-90-rs))*iConeLength )
REyeConeY <- c(REyePosY,
REyePosY+sin(bearingRad+pi/180*(recLastFishFrame$REyeAngle-90-rs))*iConeLength)
polygon(REyeConeX,REyeConeY,col=REyecolour) ##,density=25,angle=-45
##Draw Frame Number
#text(X_FRAME[1] + 60,frameHeight+20,labels=paste(i,"# (",i-startFrame,")",round( (i-startFrame)/(Fs/1000)),"msec" ) ,col="darkblue",cex=0.7)
mtext(side = 3,cex=1.0, line = 2.2, outer=FALSE,
paste(i,"# (",i-startFrame,")",round( (i-startFrame)/(Fs/1000)),"msec" ) ,col="darkblue" )
colR <- c(Polarrfc(NROW(vTrackedPreyIDs) ) ,"#FF0000");
###Draw Prey
nn <- 0
for (f in vTrackedPreyIDs)
{
nn <- nn + 1
lastPreyFrame <- datHuntEventMergedFrames[datHuntEventMergedFrames$frameN == lastFrame & datHuntEventMergedFrames$PreyID == f,]
rangePreyFrame <- datHuntEventMergedFrames[datHuntEventMergedFrames$frameN >= startFrame & datHuntEventMergedFrames$frameN <= lastFrame & datHuntEventMergedFrames$PreyID == f,]
if (NROW(lastPreyFrame$Prey_X) > 0 )
{
points(lastPreyFrame$Prey_X,frameWidth-lastPreyFrame$Prey_Y,col=colR[[nn]],pch=16,cex=lastPreyFrame$Prey_Radius/2)
if (lastPreyFrame$Prey_Radius < 2) ##Draw X over Prey, If it has likely Dissappeared By Now
points(lastPreyFrame$Prey_X,frameWidth-lastPreyFrame$Prey_Y,col=colR[[nn]],pch=4,cex=1.2)
lines(rangePreyFrame$Prey_X,frameWidth-rangePreyFrame$Prey_Y,col="red")
text(lastPreyFrame$Prey_X+5,frameWidth-lastPreyFrame$Prey_Y+10,labels=f,col="darkred",cex=0.8)
}
}
dev.flush()
if (!is.na(saveToFolder) )
{
dev.copy(jpeg,filename=paste(saveToFolder,"/",sprintf("%05d", i) ,".jpg",sep=""), bg="white" ,quality=80);
dev.off ();
}
} ##For Each Frame
}##RenderHunt Event
## PLot The Relative Angle Of Fish Bearing to Prey Over Time On a Polar Plot - For Each Prey Of this Hunt Event
## \returns the relative Angle Of Each Prey To The Fish;s Heading
polarPlotAngleToPrey <- function(datRenderHuntEvent)
{
### Plot Relative Angle To Each Prey ###
vTrackedPreyIDs <- unique(datRenderHuntEvent$PreyID)
Range <- ((max(datRenderHuntEvent[!is.na(datRenderHuntEvent$PreyID),]$frameN)
- min(datRenderHuntEvent[!is.na(datRenderHuntEvent$PreyID),]$frameN) ) / G_APPROXFPS)+1
relAngle <- list()
txtW <- -0.2# strwidth(parse(text=paste("270", "^o ", sep="")))
plot(1,type='n',xlim=c(-(Range+txtW),(Range+txtW)) ,ylim=c(-(Range+txtW),(Range+txtW) ),main="Angle to Prey Over Time ")
#display.brewer.all() to see avaulable options
Polarrfc <- colorRampPalette(rev(brewer.pal(8,'Dark2')));
colR <- c(Polarrfc(NROW(vTrackedPreyIDs) ) ,"#FF0000");
n <- 0
for (f in vTrackedPreyIDs)
{
n<-n+1
message(f)
message(colR[n])
if (is.na(f))
next
datRenderPrey <- datRenderHuntEvent[datRenderHuntEvent$PreyID == f,]
##Atan2 returns -180 to 180, so 1st add 180 to convert to 360, then sub the fishBody Angle, then Mod 360 to wrap in 360deg circle, then sub 180 to convert to -180 to 180 relative to fish heading angles
#relAngle[[as.character(f)]] <- ( ((360+180/pi * atan2( datRenderHuntEvent$Prey_X-datRenderHuntEvent$posX,datRenderHuntEvent$posY - datRenderHuntEvent$Prey_Y)) - datRenderHuntEvent$BodyAngle) %% 360) -180
relAngle[[as.character(f)]] <- ( ( 180 + 180/pi * atan2(datRenderPrey$Prey_X -datRenderPrey$posX,datRenderPrey$posY - datRenderPrey$Prey_Y)) -datRenderPrey$BodyAngle ) %% 360 - 180
#points(relAngle[[as.character(f)]],datRenderPrey$frameN,type='b',cex=0.2,xlim=c(-180,180))
##Convert Frames To Seconds
d <- (datRenderPrey$frameN-min(datRenderHuntEvent[!is.na(datRenderHuntEvent$PreyID),]$frameN)) / G_APPROXFPS
x <- (d)*cos(2*pi-pi/180 * relAngle[[as.character(f)]] + pi/2)
y <- (d)*sin(2*pi-pi/180 * relAngle[[as.character(f)]] + pi/2)
points(x,y,type='p',cex=0.2,xlim=c(-(Range),(Range) ) ,ylim=c(-(Range),(Range) ), main="",col=colR[n])
points(0,0,cex=0.8,col="blue")
for (i in seq(0,Range,0.5 ) )
{
lines(i*cos(pi/180 * seq(0,360,1) ),i*sin(pi/180 * seq(0,360,1) ),col="blue")
txtW <- strwidth(paste(as.character(i),"s",sep="") )/2
text(i*cos(pi/180 * 0 )+txtW,i*sin(pi/180 * 0 ),labels = paste(as.character(i),"s",sep="") ,col="blue",cex=0.7)
}
lines(c(0,0),c(0,Range+Range/30) ,col="blue")
txtW <- strwidth(parse(text=paste("270", "^o ", sep="")))/2
text((Range+txtW)*cos(pi/180 * seq(0,-270,-90) + pi/2)+Range/40,(Range+txtW)*sin(pi/180 *seq(0,-270,-90) + pi/2) ,labels = parse(text=paste(seq(0,270,90), "^o ", sep="")) ,col="blue",cex=0.8)
}
return (relAngle)
}
## Returns A list of vectors showing bearing Angle To Each Prey
calcRelativeAngleToPrey <- function(datRenderHuntEvent)
{
### Plot Relative Angle To Each Prey ###
vTrackedPreyIDs <- unique(datRenderHuntEvent$PreyID)
Range <- ((max(datRenderHuntEvent[!is.na(datRenderHuntEvent$PreyID),]$frameN) - min(datRenderHuntEvent$frameN) ) / G_APPROXFPS)+1
relAngle <- list()
n <- 0
for (f in vTrackedPreyIDs)
{
n<-n+1
#message(f)
if (is.na(f))
next
datRenderPrey <- datRenderHuntEvent[datRenderHuntEvent$PreyID == f,]
##Atan2 returns -180 to 180, so 1st add 180 to convert to 360, then sub the fishBody Angle, then Mod 360 to wrap in 360deg circle, then sub 180 to convert to -180 to 180 relative to fish heading angles
##dd Time Base As frame Number on First Column
relAngle[[as.character(f)]] <- cbind(datRenderPrey$frameN,
( ( 180 + 180/pi * atan2(datRenderPrey$Prey_X -datRenderPrey$posX,datRenderPrey$posY - datRenderPrey$Prey_Y)) -datRenderPrey$BodyAngle ) %% 360 - 180
)
}
#points(relAngle[[as.character(f)]],datRenderPrey$frameN,type='b',cex=0.2,xlim=c(-180,180))
##Convert Frames To Seconds
return (relAngle)
}
# ### DUBlicate
### Calc Relative Angle To Prey / Azimuth - Returns Vector
calcPreyAzimuth <- function(datRenderHuntEvent)
{
### Plot Relative Angle To Each Prey ###
vTrackedPreyIDs <- unique(datRenderHuntEvent$PreyID)
relAngle <- list()
n <- 0
for (f in vTrackedPreyIDs)
{
n<-n+1
message(f)
if (is.na(f))
next
datRenderPrey <- datRenderHuntEvent[datRenderHuntEvent$PreyID == f,]
##Atan2 returns -180 to 180, so 1st add 180 to convert to 360, then sub the fishBody Angle, then Mod 360 to wrap in 360deg circle, then sub 180 to convert to -180 to 180 relative to fish heading angles
#relAngle[[as.character(f)]] <- ( ((360+180/pi * atan2( datRenderHuntEvent$Prey_X-datRenderHuntEvent$posX,datRenderHuntEvent$posY - datRenderHuntEvent$Prey_Y)) - datRenderHuntEvent$BodyAngle) %% 360) -180
#points(relAngle[[as.character(f)]],datRenderPrey$frameN,type='b',cex=0.2,xlim=c(-180,180))
##Convert Frames To Seconds
bearingRad = pi/180*(datRenderPrey$BodyAngle-90)##+90+180 - Body Heading
posVX = datRenderPrey$posX + cos(bearingRad)*DIM_DISTTOMOUTH_PX
posVY = datRenderPrey$posY + sin(bearingRad)*DIM_DISTTOMOUTH_PX
##For Rel Angle Use Bladder Centroid So As to minimize angle error
##For Distance Use Estimated MouthPOint
d <- sqrt( (datRenderPrey$Prey_X -posVX )^2 + (datRenderPrey$Prey_Y - posVY)^2 )
relAngle[[n]] <- cbind(preyID=f,
distPX=d,
azimuth=( ( 180 + 180/pi * atan2(datRenderPrey$Prey_X -datRenderPrey$posX, datRenderPrey$posY - datRenderPrey$Prey_Y)) -datRenderPrey$BodyAngle ) %% 360 - 180
)
##From MouthPoint
#relAngle[[n]] <- cbind(preyID=f,distPX=d,
# azimuth=( ( 180 + 180/pi * atan2(datRenderPrey$Prey_X -posVX, posVY - datRenderPrey$Prey_Y)) -datRenderPrey$BodyAngle ) %% 360 - 180 )
x <- (d)*cos(2*pi-pi/180 * relAngle[[as.character(f)]] + pi/2)
y <- (d)*sin(2*pi-pi/180 * relAngle[[as.character(f)]] + pi/2)
}
return(relAngle )
}
############# A Linear 2 Axis Plot - Prey Vs Distance
plotAngleToPreyAndDistance <- function(datRenderHuntEvent,vDistToPrey_Fixed_FullRange,t)
{
## Angle To Prey ##
par(new = FALSE)
par(mar=c(4,4,4,4))
plot(t,vDistToPrey_Fixed_FullRange[1:NROW(t)]*DIM_MMPERPX,type='l',
xlab="(msec)",
ylab=NA,
col="purple",
main="Motion Relative Prey and Eye Angles",
asp=1,
lwd=2,ylim=c(0,5))
axis(side = 2,col="purple",cex=1.2,lwd=2)
Polarrfc <- colorRampPalette(rev(brewer.pal(8,'Dark2')));
colR <- c(Polarrfc(NROW(tblPreyRecord) ) ,"#FF0000");
n<-0
##Add Prey Angle On Separate Axis
par(new=TRUE) ##Add To Path Length Plot But On Separate Axis So it Scales Nicely
for (vAToPrey in lAngleToPrey)
{
l <- min(NROW(t),NROW(vAToPrey))
n<-n+1;
plot((vAToPrey[1:l,1]-min(datRenderHuntEvent$frameN))/(Fs/1000),filtfilt(bf_eyes,vAToPrey[1:l,2]),type='l',axes=F,col=colR[n]
,xlab=NA,ylab=NA, ylim=c(-40,40))
}
axis(side = 4,col=colR[n])
mtext(side = 4,cex=0.8, line = 2.2, expression('Angle To Prey'^degree), font=2 )
mtext(side = 2,cex=0.8, line = 2.2, expression("Distance To Prey (mm)"), font=2 )
legend("bottomleft",c(paste("Distance to Prey "),paste("Angle to Prey",names(vAToPrey)) ) , #,selectedPreyID
col=c("purple",colR),cex=0.7,box.lwd =0,lty=1,lwd=2 )
###
} ## Plot Prey Angle And Distance
## PLot The Relative Angle Of Fish Bearing to Prey Over Distance to Prey as a Polar Plot
##- Can Deal With Multiple Prey IDS,
## The Is assumed to be at the centre of the polar plot
## Colour Code According To Eye Vergence
## \Returns prey azimuth Vector
polarPlotAngleToPreyVsDistance <- function(datRenderHuntEvent,newPlot=TRUE)
{
Range <- 80 ##300 Pixels Around the prey
### Plot Relative Angle To Each Prey ###
vTrackedPreyIDs <- unique(datRenderHuntEvent$PreyID)
#display.brewer.all() to see avaulable options
##Choose Heat Map For white being Low (BG) Red High Vergence
Polarrfc <- colorRampPalette((brewer.pal(9,'YlOrRd' ))); ##Color Bling Friendly Pallet
colR <- (c(Polarrfc(100 ))); ##Assume 80 Degrees Max EyeVergence
#colR["alpha",] <- 110 ##Opacity
relAngle <- list()
#txtW <- strwidth(parse(text=paste("270", "^o ", sep=""))) ##Override as it fails When In Layout Mode
txtW <- -0.1# strwidth(parse(text=paste("270", "^o ", sep="")))
fgColor <- "white"
if (newPlot)
{
plot(1,type='n',xlim=c(-(Range+4*txtW),(Range+4*txtW)) ,
ylim=c(-(Range+4*txtW),(Range+4*txtW) ),
main="Angle to Prey Vs Distance ",
xlab=NA,ylab=NA)
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = rgb(0,0,0.3,0.99))
## Make Range Circle Llines
lines(c(0,0),c(0,Range*0.85) ,col=fgColor,lty=2,lwd=1) #V Line To 0
txtW <- strwidth(parse(text=paste("270", "^o ", sep="")))/3
text((Range+txtW/2)*cos(pi/180 * seq(0,-270,-90) + pi/2),
(Range+txtW/2)*sin(pi/180 *seq(0,-270,-90) + pi/2),
labels = parse(text=paste(seq(0,270,90), "^o ", sep="")) ,col=fgColor,cex=0.8,font=1.5)
points(0,0,cex=0.8,col="blue")
for (i in seq(0,Range,1/DIM_MMPERPX ) )
{
lines(i*cos(pi/180 * seq(0,360,1) ),i*sin(pi/180 * seq(0,360,1) ),col=fgColor)
txtW <- strwidth(paste(as.character(i*DIM_MMPERPX),"",sep="") )/2
txtH <- strheight(paste(as.character(i*DIM_MMPERPX),"",sep="") )/2
## Place the Distance Labels
text(i*cos(pi/180 * -90 ),i*sin(pi/180 * -90 )-txtH,labels = paste(as.character(i*DIM_MMPERPX),"mm",sep="") ,
col=fgColor,cex=0.7,font=1.8)
}
##Plot Heat Map Legend
x <- Range/2+(1:Range/2) ##Make Narrow 1/2 length bar
points(x,rep(-70,NROW(x) ),pch=19,col=colR,cex=1.5)
text(x[1],-78,labels=expression("0"^degree),col=fgColor,font=2.2) ##0 V Angle
txtW <- strwidth(parse(text=c(expression(),bquote( .(G_THRESHUNTVERGENCEANGLE)^degree))))/2 ##Text Width For Centre Aligment
segments(x[1]+ G_THRESHUNTVERGENCEANGLE/2-txtW,-72,x[1]+ G_THRESHUNTVERGENCEANGLE/2-txtW,-68) ##V Indicator Of Hunting Threshold
text(x[1]+ G_THRESHUNTVERGENCEANGLE/2-txtW,-78,labels=c(expression(),bquote( .(G_THRESHUNTVERGENCEANGLE)^degree)),col=fgColor,font=2.0) ##0 V Angle
text(tail(x,1)-txtW,-78,labels=expression("80"^degree),col=fgColor,font=2.2) ##0 V Angle
}
n <- 0
for (f in vTrackedPreyIDs)
{
n<-n+1
message(f)
message(colR[n])
if (is.na(f))
next
datRenderPrey <- datRenderHuntEvent[datRenderHuntEvent$PreyID == f,]
##Atan2 returns -180 to 180, so 1st add 180 to convert to 360, then sub the fishBody Angle, then Mod 360 to wrap in 360deg circle, then sub 180 to convert to -180 to 180 relative to fish heading angles
#relAngle[[as.character(f)]] <- ( ((360+180/pi * atan2( datRenderHuntEvent$Prey_X-datRenderHuntEvent$posX,datRenderHuntEvent$posY - datRenderHuntEvent$Prey_Y)) - datRenderHuntEvent$BodyAngle) %% 360) -180
EyeVergence <- datRenderPrey$LEyeAngle-datRenderPrey$REyeAngle
EyeVergence[EyeVergence<1] <- 1 ##Fix Min Value To Belong to 20 Degrees V
#points(relAngle[[as.character(f)]],datRenderPrey$frameN,type='b',cex=0.2,xlim=c(-180,180))
##Convert Frames To Seconds
#bearingRad = pi/180*(datRenderPrey$BodyAngle-90)##+90+180 - Body Heading
#posVX = datRenderPrey$posX + cos(bearingRad)*DIM_DISTTOMOUTH_PX
#posVY = datRenderPrey$posY + sin(bearingRad)*DIM_DISTTOMOUTH_PX
##For Rel Angle Use Bladder Centroid So As to minimize angle error
# this is also replicated in calcPreyAzimuth(datRenderPrey)
#relAngle[[as.character(f)]] <- ( ( 180 + 180/pi * atan2(datRenderPrey$Prey_X -datRenderPrey$posX, datRenderPrey$posY - datRenderPrey$Prey_Y)) -datRenderPrey$BodyAngle ) %% 360 - 180
polarCoord <- calcPreyAzimuth(datRenderPrey)[[n]]
##For Distance Use Estimated MouthPOint
#d <- sqrt( (datRenderPrey$Prey_X -posVX )^2 + (datRenderPrey$Prey_Y - posVY)^2 )
d <- polarCoord[,"distPX"]
x <- (d)*cos(2*pi-pi/180 * polarCoord[,"azimuth"] + pi/2)
y <- (d)*sin(2*pi-pi/180 * polarCoord[,"azimuth"] + pi/2)
points(x,y,type='p',cex=0.2,xlim=c(-(Range),(Range) ) ,ylim=c(-(Range),(Range) ), main="",
col=colR[EyeVergence]) ##Color Accourding To EyeVergence
}
return(relAngle)
} ## End of PlotAngleToPreyVsDistance
## Plot Prey Position prior to Capture Based on the validated Capture Bout Data
#### Plot Angles to Prey
getCaptureBoutPreyPosition <- function (datMotionBoutsToValidate,groupID)
{
vDistToPrey <- datMotionBoutsToValidate[datMotionBoutsToValidate$boutRank == 1 &
datMotionBoutsToValidate$groupID == groupID,]$vMotionBoutDistanceToPrey_mm
vAngleToPrey <- datMotionBoutsToValidate[datMotionBoutsToValidate$boutRank == 1 &
datMotionBoutsToValidate$groupID == groupID,]$OnSetAngleToPrey
preyY <- vDistToPrey*cos(vAngleToPrey*pi/180)
preyX <- vDistToPrey*sin(vAngleToPrey*pi/180)
#x <- (d)*cos(2*pi-pi/180 * relAngle[[as.character(f)]] + pi/2)
#y <- (d)*sin(2*pi-pi/180 * relAngle[[as.character(f)]] + pi/2)
return (data.frame( cbind(preyX,preyY,vAngleToPrey,vDistToPrey,groupID)) )
}
plotPreyAzimuthAtCapture <- function(newPlot = T)
{
datMotionBoutsToValidate <-readRDS(file=paste0(strDataExportDir,"/huntEpisodeAnalysis_MotionBoutData_ToValidate.rds") )
##LOad so as to disambiguate the GroupdID
strDataFileName <-paste("setn15-HuntEvents-SB-Updated-Merged2",sep="") ##To Which To Save After Loading
datHuntEventAllGroupToValidate <-readRDS(file=paste(strDatDir,"/LabelledSet/",strDataFileName,".rds",sep="" )) ##Save With Dataset Idx Identifier
strGroupID <- c(unique(datHuntEventAllGroupToValidate$groupID))
preyCapPos_LF <- (getCaptureBoutPreyPosition(datMotionBoutsToValidate,2))
preyCapPos_DF <- getCaptureBoutPreyPosition(datMotionBoutsToValidate,1)
preyCapPos_NF <- getCaptureBoutPreyPosition(datMotionBoutsToValidate,3)
densNF <- density(preyCapPos_NF$vAngleToPrey)
densLF <- density(preyCapPos_LF$vAngleToPrey)
densDF <- density(preyCapPos_DF$vAngleToPrey)
#plot(densDF$x,densDF$y - densLF$y)
## Quantiles - Show more than 50% of data located between -20/ +20 degrees
#plot(density(abs(preyCapPos_NF$vAngleToPrey) ) )
plot(densNF,col=colourLegL[1],lty=2,lwd=3,cex.axis=cex,main=NA,xlab=NA,ylab=NA,ylim=c(0,0.050),xlim=c(-50,50))
lines(densLF,col=colourLegL[2],lty=1,lwd=3,cex.axis=cex,main=NA,xlab=NA,ylab=NA)
lines(densDF,col=colourLegL[3],lty=3,lwd=3,cex.axis=cex,main=NA,xlab=NA,ylab=NA)
mtext(side = 1, cex=cex, line = lineAxis,expression(paste("Prey azimuth prior to capture (",theta^o,")") ))
mtext(side = 2, cex=cex, line = lineAxis,expression(paste("Density") ))
legend("topright",
legend=c( expression (),
bquote(NF ~ '#' ~ .(NROW(preyCapPos_NF) ) ),
bquote(LF ~ '#' ~ .(NROW(preyCapPos_LF)) ),
bquote(DF ~ '#' ~ .(NROW(preyCapPos_DF) ) )
#, bquote(ALL ~ '#' ~ .(ldata_ALL$N) )
), ##paste(c("DL n=","LL n=","NL n="),c(NROW(lFirstBoutPoints[["DL"]][,1]),NROW(lFirstBoutPoints[["LL"]][,1]) ,NROW(lFirstBoutPoints[["NL"]][,1] ) ) )
col=colourLegL,lty=c(2,1,3),lwd=3)
##Add the quantiles 25%-75%
#points(quantNF[2],densNF$y[ head(which(densNF$x >= quantNF[2]),1) ],col=colourLegL[1] )
#points(quantNF[4],densNF$y[ head(which(densNF$x >= quantNF[4]),1) ],col=colourLegL[1] )
#points(quantLF[2],densLF$y[ head(which(densLF$x >= quantLF[2]),1) ],col=colourLegL[2] )
#points(quantLF[4],densLF$y[ head(which(densLF$x >= quantLF[4]),1) ],col=colourLegL[2] )
#points(quantDF[2],densDF$y[ head(which(densDF$x >= quantDF[2]),1) ],col=colourLegL[3] )
#points(quantDF[4],densDF$y[ head(which(densDF$x >= quantDF[4]),1) ],col=colourLegL[3] )
## Do Stat Test
## Quantile
quantNF <- quantile(abs(preyCapPos_NF$vAngleToPrey) )
quantLF <- quantile(abs(preyCapPos_LF$vAngleToPrey) )
quantDF <- quantile(abs(preyCapPos_DF$vAngleToPrey) )
print(quantNF)
print(quantLF)
print(quantDF)
## To proceed with the verification ANOVA, we must first verify the homoskedasticity (ie test for homogeneity of variances). The software R provides two tests: the Bartlett tes
datAllGroup <- rbind(preyCapPos_NF,preyCapPos_DF,preyCapPos_LF)
datAllGroup$groupID <- factor(datAllGroup$groupID)
bartlett.test(datAllGroup$vAngleToPrey, datAllGroup$groupID)
fligner.test(datAllGroup$vAngleToPrey, datAllGroup$groupID) ##p > 0.05 Homogenious
#Can be argued that the variances are homogeneous since p-value > 0.01.
fit = lm(formula = datAllGroup$vAngleToPrey ~ datAllGroup$groupID)
anova (fit)
## p >0.05 we report as non-siginificant
}
## plot The Prey Locations Prior to Capture in relation to the head of the larvae##
## The Radar Figure With the Prey Positions Prior To Capture
plotCaptureBoutPreyPositions <- function(newPlot = T)
{
datMotionBoutsToValidate <-readRDS(file=paste0(strDataExportDir,"/huntEpisodeAnalysis_MotionBoutData_ToValidate.rds") )
##LOad so as to disambiguate the GroupdID
strDataFileName <-paste("setn15-HuntEvents-SB-Updated-Merged2",sep="") ##To Which To Save After Loading
datHuntEventAllGroupToValidate <-readRDS(file=paste(strDatDir,"/LabelledSet/",strDataFileName,".rds",sep="" )) ##Save With Dataset Idx Identifier
strGroupID <- c(unique(datHuntEventAllGroupToValidate$groupID))
preyCapPos_LF <- (getCaptureBoutPreyPosition(datMotionBoutsToValidate,2))
preyCapPos_DF <- getCaptureBoutPreyPosition(datMotionBoutsToValidate,1)
preyCapPos_NF <- getCaptureBoutPreyPosition(datMotionBoutsToValidate,3)
##Typical Fish Length in px is 80 (NF)
##Typical Eye Verged Head Width (widest point) 17px
##Bladded Width =9px
Range <- 20 ##300 Pixels Around the prey
#display.brewer.all() to see avaulable options
##Choose Heat Map For white being Low (BG) Red High Vergence
Polarrfc <- colorRampPalette((brewer.pal(9,'YlOrRd' ))); ##Color Bling Friendly Pallet
colR <- (c(Polarrfc(100 ))); ##Assume 80 Degrees Max EyeVergence
#colR["alpha",] <- 110 ##Opacity
relAngle <- list()
#txtW <- strwidth(parse(text=paste("270", "^o ", sep=""))) ##Override as it fails When In Layout Mode
txtW <- -0.1# strwidth(parse(text=paste("270", "^o ", sep="")))
fgColor <- "black"
bgColor <- rgb(1,1,1.0,1)
if (newPlot)
{
plot(1,type='n',xlim=c(-(Range+4*txtW)*DIM_MMPERPX,(Range+4*txtW)*DIM_MMPERPX ) ,
ylim=c(-(Range+4*txtW)*DIM_MMPERPX,(Range+4*txtW)*DIM_MMPERPX ),
main="Prey position prior to capture swim ",
xlab=NA,ylab=NA,xaxt='n',yaxt='n', ann=FALSE)
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = bgColor)
## Make Vertical HeadOn Lline
lines(c(0,0),c(0,Range*0.85)*DIM_MMPERPX ,col=fgColor,lty=2,lwd=1) #V Line To 0
txtW <- strwidth(parse(text=paste("270", "^o ", sep="")))/3
text(DIM_MMPERPX*(Range+txtW/2)*cos(pi/180 * seq(0,-270,-90) + pi/2),
DIM_MMPERPX*(Range+txtW/2)*sin(pi/180 *seq(0,-270,-90) + pi/2),
labels = parse(text=paste(seq(0,270,90), "^o ", sep="")) ,col=fgColor,cex=0.8,font=1.5)
points(0,0,cex=0.8,col="blue")
###Draw The Circles
for (i in seq(0,Range,1/(4*DIM_MMPERPX) ) )
{
lines(DIM_MMPERPX*i*cos(pi/180 * seq(0,360,1) ),DIM_MMPERPX*i*sin(pi/180 * seq(0,360,1) ),col=fgColor)
txtW <- strwidth(paste(as.character(i*DIM_MMPERPX),"",sep="") )/2
txtH <- strheight(paste(as.character(i*DIM_MMPERPX),"",sep="") )/2
## Place the Distance Labels
text(DIM_MMPERPX*i*cos(pi/180 * -90 ),DIM_MMPERPX*i*sin(pi/180 * -90 )-txtH,labels = paste(as.character(i*DIM_MMPERPX),"mm",sep="") ,
col=fgColor,cex=0.7,font=1.8)
}
##Plot Heat Map Legend
x <- Range/2+(1:Range/2)*DIM_MMPERPX ##Make Narrow 1/2 length bar
points(x,rep(-70,NROW(x) ),pch=19,col=colR,cex=1.5)
text(x[1],-78,labels=expression("0"^degree),col=fgColor,font=2.2) ##0 V Angle
txtW <- strwidth(parse(text=c(expression(),bquote( .(G_THRESHUNTVERGENCEANGLE)^degree))))/2 ##Text Width For Centre Aligment
segments(x[1]+ G_THRESHUNTVERGENCEANGLE/2-txtW,-72,x[1]+ G_THRESHUNTVERGENCEANGLE/2-txtW,-68) ##V Indicator Of Hunting Threshold
text(x[1]+ G_THRESHUNTVERGENCEANGLE/2-txtW,-78,labels=c(expression(),bquote( .(G_THRESHUNTVERGENCEANGLE)^degree)),col=fgColor,font=2.0) ##0 V Angle
text(tail(x,1)-txtW,-78,labels=expression("80"^degree),col=fgColor,font=2.2) ##0 V Angle
} ##If New Canvas
## plot The Prey Locations Prior to Capture ##
#points(x,y,type='p',cex=0.2,xlim=c(-(Range),(Range) ) ,ylim=c(-(Range),(Range) ), main="",
# col=colR[EyeVergence]) ##Color Accourding To EyeVergence
points(preyCapPos_NF$preyX,preyCapPos_NF$preyY,col=colourHLine [1],pch=pchL[5],cex=0.7) #xlim=c(-(Range),(Range) ) ,ylim=c(-(Range),(Range) )
points(preyCapPos_LF$preyX ,preyCapPos_LF$preyY,col=colourHLine[2],pch=pchL[6],cex=0.7)
points(preyCapPos_DF$preyX ,preyCapPos_DF$preyY,col=colourHLine[3],pch=pchL[7],cex=0.7)
##Draw Fish Bounding Rects representation
rect(-17*DIM_MMPERPX/2,-9*DIM_MMPERPX/2,+17*DIM_MMPERPX/2,-7*DIM_MMPERPX/2 )
rect(-9*DIM_MMPERPX/2,-82*DIM_MMPERPX,+9*DIM_MMPERPX/2,0 )
legend("topright",
legend=c( expression (),
bquote(NF ~ '#' ~ .(NROW(preyCapPos_NF) ) ),
bquote(LF ~ '#' ~ .(NROW(preyCapPos_LF)) ),
bquote(DF ~ '#' ~ .(NROW(preyCapPos_DF) ) )
#, bquote(ALL ~ '#' ~ .(ldata_ALL$N) )
), ##paste(c("DL n=","LL n=","NL n="),c(NROW(lFirstBoutPoints[["DL"]][,1]),NROW(lFirstBoutPoints[["LL"]][,1]) ,NROW(lFirstBoutPoints[["NL"]][,1] ) ) )
col=colourLegL,pch=c(pchL[5],pchL[6],pchL[7]))
}
############# PLot Heat Map of Movement Trajectories Across COnditions #####
# strTrajectoryDensityFileName <- paste("plots/densities/MotionDensity-Set-",strCond,".pdf",collapse=NULL);
# pdf(strTrajectoryDensityFileName,width=8,height=8)
# eGroupDens <- kde2d(filtereddatAllFrames$posX,filtereddatAllFrames$posY, n=60, lims=c(range(0,565),range(0,565)) )
# image(eGroupDens,col=r)
# #title(paste(strCond,"Group Motion Densities #n=", sampleSize, " #F:",procDatFrames),collapse=NULL);
# title(strtitle, sub = strsub, cex.main = 1.5, font.main= 1.5, cex.sub = 1.0, font.sub = 2)
# dev.off()
#############################
########