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
# Title:  Generate Compass Plots
# Author: Emma K. Costa
# Date: code compiled on 20220822
# Related publication: Andrew McKay, Emma K. Costa, and Jingxun Chen, eLife, 2022

library(ggplot2)
library(dplyr)
library(ggpubr)
library(tidyverse)
library(hrbrthemes)
library(viridis)
library(sf)
library(grid)
library(dplyr)
library(knitr)
library(plyr)
library(doMC)
library(writexl)
library(readxl)
library("gridExtra")
library('tidyr')
library(ggExtra)
library(patchwork)
library(RColorBrewer)
library(philentropy)
library(splines)
library(zoo)
library(diffdf)
library("padr")
library(scales)
library('tseries')

# ------------------------------------------------------------------
# Set up 
# ------------------------------------------------------------------
setwd('/../1_ProcessingDLCOutPut/Output/filtered_and_interpolated/')
#Source: https://stackoverflow.com/questions/39024758/how-to-use-r-package-circular-to-make-rose-plot-of-histogram-data-on-360

#make one large csv file with data from all videos
f <- list.files(pattern=c("*.csv"), full.names=T, recursive=FALSE) 
tables <- lapply(f, read.csv)
df <- do.call(rbind, tables) 

#Remove animals and videos you do not want to include
df$recording_num <- df$recording_num + 1
df <- subset(df, recording_num <= 17) #only include trials 1-17
animals.to.include <- c("1", "2", "3", "4", "5", "9","13","14","15", "16", "19", "20", "26")
df <- subset(df, fishnum %in% animals.to.include) #only include these animals

#save this aggregate CSV somewhere
setwd('/../3_SuppFig6/Output/')
#write.csv(df, "allanimals_REC1-17_fullyfilteredinterpolated_resnet50_TrainAllTheThingsJun28shuffle1_1030000.csv")
df <- read.csv('allanimals_REC1-17_fullyfilteredinterpolated_resnet50_TrainAllTheThingsJun28shuffle1_1030000.csv')




# ------------------------------------------------------------------
# Get Angles of Animal Trajectory Vectors
# ------------------------------------------------------------------
#get beginning position (t = 2sec) and end position (t = 9sec) for all animals first 7 trials and last 7 trials

#extract the coordinates at 2 secs and 9 secs
beg <- subset(df, seconds == 2)
end <- subset(df, seconds == 9)

#for each, define a vector or a line
fishIDs <- unique(df$fishnum)
totrecnum <- nrow(unique(df[c("fishnum", "recording_num")]))
recs <- unique(df[c("fishnum", "recording_num")])

recs$x1 <- NA
recs$y1 <- NA
recs$x2 <- NA
recs$y2 <- NA

recs$angle <- NA

#find the angle of the vector in 2D plane (convert to polar coord (r, theta) and report theta)
for (i in 1:totrecnum){
  r <- recs[i,]
  fish <- r$fishnum
  vid <- r$recording_num
  x1 <- subset(beg, fishnum == fish & recording_num == vid)$y2
  y1 <- subset(beg, fishnum == fish & recording_num == vid)$x2
  x2 <- subset(end, fishnum == fish & recording_num == vid)$y2
  y2 <- subset(end, fishnum == fish & recording_num == vid)$x2
  
  deltay = y2 - y1
  deltax = x2 - x1
  m = deltay/deltax
  
  recs[i,]$x1 <- y1
  recs[i,]$y1 <- x1
  recs[i,]$x2 <- y2
  recs[i,]$y2 <- x2
  
  if(deltay > 0 & deltax > 0){recs[i,]$angle <- atan(m)*180/pi}
  if(deltay < 0 & deltax < 0){recs[i,]$angle <- 180 + atan(m)*180/pi}
  if(deltay > 0 & deltax < 0){recs[i,]$angle <- 180 + atan(m)*180/pi}
  if(deltay < 0 & deltax > 0){recs[i,]$angle <- 360 + atan(m)*180/pi}
} #note there are a few videos for which no angle can be calculated as t=2 or t=9 may not have values, skip those (might need to do so manually)

#save this aggregate CSV somewhere
setwd('/../3_SuppFig6/Output/')
write.csv(recs, "2secto9sec_angles.csv")

first7 <- subset(recs, recording_num %in% c(1:7))
last7 <- subset(recs, recording_num %in% c(11:17))

setwd('/../3_SuppFig6/Output/')
write.csv(first7, "2secto9sec_angles_first7.csv")
write.csv(last7, "2secto9sec_angles_last7.csv")

first7 <- read.csv('2secto9sec_angles_first7.csv')
last7 <- read.csv('2secto9sec_angles_last7.csv')




# ------------------------------------------------------------------
#  Compass Plot
# ------------------------------------------------------------------
#bin the angles and get frequency
#bin size is 20
first7.freq = hist(first7$angle, breaks=15, include.lowest=TRUE, plot=FALSE)

first7.df <- data.frame(range = first7.freq$mids, frequency = first7.freq$counts)


c1 <- ggplot(data=first7.df,aes(x=range,y=frequency))+
  geom_bar(stat="identity")+ 
  coord_polar(start = -pi/2)+ #sets where 90deg appears on the graph
  scale_x_continuous(breaks = seq(0, 360, 30))+
  ylim(c(0,20))

setwd('/../3_SuppFig6/Plots/')
pdf("220820_compassplot_1st7trials.pdf") 
c1
dev.off()


last7.freq   = hist(last7$angle, breaks=15, include.lowest=TRUE, plot=FALSE)

last7.df <- data.frame(range = last7.freq$mids, frequency = last7.freq$counts)


c2 <- ggplot(data=last7.df,aes(x=range,y=frequency))+
  geom_bar(stat="identity")+ 
  coord_polar(start = -pi/2)+ #sets where 90deg appears on the graph
  scale_x_continuous(breaks = seq(0, 360, 30))+
  ylim(c(0,20))

setwd('/../3_SuppFig6/Plots/')
pdf("220820_compassplot_last7trials.pdf") 
c2
dev.off()