Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 336b4b1
  • /
  • Fig6_SuppFig6
  • /
  • Rscripts
  • /
  • 4_AvgVelocity_SuppFig6.R
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:fde5e2be53bdceb54ab7c34dea45a4c4b10df897
directory badge
swh:1:dir:6e518c2937180bba999c066f721dba2067b61f3d

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
4_AvgVelocity_SuppFig6.R
# Title:  Animal Average Velocities
# Author: Emma K. Costa
# Date: code compiled on 20220822
# Related publication: Andrew McKay, Emma K. Costa, and Jingxun Chen, eLife, 2022


# ------------------------------------------------------------------
# Set up
# ------------------------------------------------------------------
inputDir <- c('/../1_ProcessingDLCOutPut/Output/velocity/')
outputDir <- c('/../3_SuppFig6/Output/')

setwd(inputDir)

f <- list.files(pattern=c("*.csv"), full.names=F, recursive=FALSE) 

#make one large table
tables <- lapply(f, read.csv)
df <- do.call(rbind, tables)

#add one to the trial #'s so it's no longer 0 indexed
df$recording_num <- df$recording_num + 1


#compute rolling averages
df <- df %>%
  dplyr::arrange(fishnum) %>% 
  dplyr::group_by(fishnum,recording_num) %>%
  dplyr::mutate(velocity_x_5frame = zoo::rollmean(velocity_x, k = 5, fill = NA),  #5 frames
                velocity_x_10frame = zoo::rollmean(velocity_x, k = 10, fill = NA), #10 frames
                velocity_x_20frame = zoo::rollmean(velocity_x, k = 20, fill = NA), #20 frames
                velocity_x_40frame = zoo::rollmean(velocity_x, k = 40, fill = NA) #40 frames
  )

df <- subset(df, seconds <= 18)
df <- subset(df, recording_num <= 17)

# ------------------------------------------------------------------
# Calculate the mean velocities
# ------------------------------------------------------------------
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)

avg.vel <- data.frame('fishnum' = animals.to.include, 'avg.velocity' =  NA)

for(i in 1:length(animals.to.include)){
  animal <- animals.to.include[i] 
  df.subset <- subset(df, fishnum <= animal) #do this on an individual animal basis
  
  upper <- quantile(df.subset$velocity_x_20frame, 0.85, na.rm = T)[[1]] #thresholding top 15%
  lower <- quantile(df.subset$velocity_x_20frame, 0.15, na.rm = T)[[1]] #thresholding bottom 15%
  
  data <- df.subset %>% 
    mutate(velocity_x2 = case_when(
      velocity_x_20frame >= upper ~ upper,
      T ~ velocity_x_20frame
    )) %>%
    mutate(velocity_x3 = case_when(
      velocity_x2 <= lower ~ lower,
      T ~ velocity_x2
    ))
  
  avg.vel[i,]$avg.velocity <- mean(data$velocity_x3, na.rm = T)
}

View(avg.vel)

#save this data
setwd(paste(outputDir))
write.csv(avg.vel, "allanimals_avgvelocities.csv")

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API