Detecting 55 kHz USVs with bedding

Maternal care

Author
Published

Invalid Date

Code
cns <- consolidate(path = "/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/", dest.path = "/media/m/Backup Plus/CUIDO MATERNO/", parallel = 10)
Code
fix_wavs(samp.rate = 200, bit.depth = 16)
Code
ohun::feature_acoustic_data(path = .Options$warbleR$path)
ssf <- ohun::split_acoustic_data(sgmt.dur = 5 * 60, cores = 10, path = .Options$warbleR$path)

write.csv(ssf, file.path(.Options$warbleR$path, "/5min_clip_info.csv"), row.names = FALSE)
Code
warbleR_options(
  wav.path = "/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/consolidated_sound_files/5min_clips/"
)

detection <- energy_detector(path = .Options$warbleR$path, 
     thinning = 0.5, bp = c(35, 90),  smooth = 1, threshold = 2.5, 
     hold.time = 3, min.duration = 1, max.duration = 200, 
     cores = 5)

saveRDS(detection, "/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/consolidated_sound_files/detection_55kHz_with_bedding_CUIDO_MATERNO.RDS")

1 Random forest classification

Code
detection <- readRDS("/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/consolidated_sound_files/detection_55kHz_with_bedding_CUIDO_MATERNO.RDS")

# measure spectrographic parameters
spectral_parameters <- spectro_analysis(detection, bp = c(35, 85), fast = TRUE, ovlp = 70, parallel = 22)

# leer el modelo 
rf_model <- readRDS("/home/m/Dropbox/Projects/rat_ultrasonic_vocalizations/data/processed/random_forest_55kHz_with_bedding.RDS")

sapply(spectral_parameters, function(x) sum(is.na(x)))

detection <- detection[!is.na(spectral_parameters$meandom), ]

spectral_parameters <- spectral_parameters[!is.na(spectral_parameters$meandom), ]

write.csv(spectral_parameters, "/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/consolidated_sound_files/spectral_parameters.csv", row.names = FALSE)

# aplicarlo sobre las detecciones nuevas
detection$class <-predict(object = rf_model, data = spectral_parameters)$predictions

# remover los sonidos clasificados como ruido de fondo 
filtered_detection <- detection[detection$class == "true.positive", ]

saveRDS(filtered_detection, "/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/consolidated_sound_files/random_forest_filtered_detection_55kHz_with_bedding.RDS")  

write.csv(filtered_detection, "/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/consolidated_sound_files/filtered_detection_55kHz_with_bedding.csv", row.names = FALSE)

counts <- aggregate(selec ~ sound.files, filtered_detection, length)
names(counts)[2] <- "55khz.call.count"

write.csv(counts, "/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/consolidated_sound_files/counts_per_sound_files_detection_55kHz_with_bedding.csv", row.names = FALSE)

1.1 Summarized

1.1.1 USV counts per minute

Code
filtered_detection_55 <- readRDS("/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/consolidated_sound_files/random_forest_filtered_detection_55kHz_with_bedding.RDS")  

orgf <- read.csv("/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/consolidated_sound_files/5min_clip_info.csv")

reass_detec <- reassemble_detection(detection = filtered_detection_55, Y = orgf, pb = FALSE)

# counts per minute
count_min <- vocal_rate(X = reass_detec, time.window = 60, time.resolution = 60, path = "/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/consolidated_sound_files/", files = list.files("/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/consolidated_sound_files/", pattern = "\\.wav$"))

# convert to minute rate
count_min$minute <- count_min$start / 60 + 1

wide_count_min <- reshape(count_min[, c("counts", "minute", "sound.files")], direction = "wide", idvar = "sound.files", timevar = "minute")

names(wide_count_min) <- c("sound.files", paste("min", 1:max(count_min$minute)))

print_kable(wide_count_min)
sound.files min 1 min 2 min 3 min 4 min 5 min 6 min 7 min 8 min 9 min 10 min 11 min 12 min 13 min 14 min 15 min 16 min 17 min 18
A_Campo_Abierto_M2Cuido_Materno2023-09-27_10-14-59_0000001 id 145.wav 0 0 0 0 0 0
A_Campo_Abierto_M2Cuido_Materno2023-09-27_10-24-11_0000002 id 109.wav 0 0 0 0 0 0
A_Campo_Abierto_M2Cuido_Materno2023-09-27_10-33-21_0000003 id 92.wav 0 0 0 0 0 0
A_Campo_Abierto_M2Cuido_Materno2023-09-27_10-42-22_0000004 id 144.wav 0 0 0 0 0 0
A_Campo_Abierto_M2Cuido_Materno2023-09-27_10-51-16_0000005 id 103.wav 0 0 0 0 0 0
A_Interaccion_social_M2Cuido_Materno2023-09-27_08-04-58_0000001 ID 145.wav 0 0 77 241 125 171 190 139 138 148 93 255 122 58 168 88 48 78
A_Interaccion_social_M2Cuido_Materno2023-09-27_08-30-11_0000002 id 109.wav 3 1 165 100 59 65 32 83 63 45 15 21 37 59 34 26 6 1
A_Interaccion_social_M2Cuido_Materno2023-09-27_08-53-54_0000003 id 92.wav 22 17 95 131 121 109 68 40 88 19 101 160 73 109 85 75 86 32
A_Interaccion_social_M2Cuido_Materno2023-09-27_09-18-15_0000004 id 144.wav 1 4 150 100 130 93 68 83 58 30 42 28 33 33 35 15 18 0
A_Interaccion_social_M2Cuido_Materno2023-09-27_09-41-51_0000005 id 103.wav 0 0 96 104 85 57 32 22 61 41 28 70 16 25 27 4 2 16
B_Campo_Abierto_M2Cuido_Materno2023-09-27_10-14-59_0000001id 101.wav 0 0 0 1 3 0
B_Campo_Abierto_M2Cuido_Materno2023-09-27_10-24-11_0000002 id 115.wav 0 0 0 0 0 0
B_Campo_Abierto_M2Cuido_Materno2023-09-27_10-33-21_0000003 id 110.wav 9 1 0 0 0 0
B_Campo_Abierto_M2Cuido_Materno2023-09-27_10-42-22_0000004 id 127.wav 0 0 1 0 1 0
B_Campo_Abierto_M2Cuido_Materno2023-09-27_10-51-17_0000005 id 128.wav 0 0 0 0 0 0
B_Interaccion_social_M2Cuido_Materno2023-09-27_08-04-58_0000001 ID 101.wav 0 1 23 61 62 49 56 55 29 31 36 22 11 36 51 55 10 15
B_Interaccion_social_M2Cuido_Materno2023-09-27_08-30-11_0000002 id 115.wav 1 0 132 248 269 186 211 178 155 96 102 189 153 134 103 148 115 20
B_Interaccion_social_M2Cuido_Materno2023-09-27_08-53-54_0000003 id 110.wav 11 40 136 182 104 60 78 33 72 56 47 47 23 34 73 68 52 25
B_Interaccion_social_M2Cuido_Materno2023-09-27_09-18-15_0000004 id 127.wav 36 37 37 27 22 28 10 12 3 18 14 6 5 16 9 6 7 15
B_Interaccion_social_M2Cuido_Materno2023-09-27_09-41-51_0000005 id 128.wav 5 10 146 213 151 116 112 62 78 45 43 30 54 36 38 0 0 0
C_Campo_Abierto_M2Cuido_Materno2023-09-27_10-14-59_0000001 id 97.wav 0 0 0 0 0 0
C_Campo_Abierto_M2Cuido_Materno2023-09-27_10-24-11_0000002 id 147.wav 0 0 0 0 0 0
C_Campo_Abierto_M2Cuido_Materno2023-09-27_10-33-21_0000003 id 129.wav 11 34 18 36 11 0
C_Campo_Abierto_M2Cuido_Materno2023-09-27_10-42-22_0000004 id 122.wav 0 4 2 3 2 0
C_Campo_Abierto_M2Cuido_Materno2023-09-27_10-51-17_0000005 106.wav 0 0 0 0 0 0
C_Interaccion_social_M2Cuido_Materno2023-09-27_08-04-58_0000001 ID 97.wav 0 0 37 226 251 190 155 153 75 85 57 91 89 66 48 20 16 12
C_Interaccion_social_M2Cuido_Materno2023-09-27_08-30-11_0000002 id 147.wav 1 0 59 57 51 23 35 24 44 30 48 21 8 34 32 17 9 5
C_Interaccion_social_M2Cuido_Materno2023-09-27_08-53-54_0000003 id 129.wav 6 3 136 126 139 114 137 147 227 111 181 127 59 84 78 40 31 18
C_Interaccion_social_M2Cuido_Materno2023-09-27_09-18-15_0000004 id 122.wav 1 20 104 53 48 50 46 41 37 11 26 17 21 10 21 5 7 14
C_Interaccion_social_M2Cuido_Materno2023-09-27_09-41-51_0000005 106.wav 0 0 166 47 79 89 21 70 64 74 47 67 37 46 41 69 22 30
D_Campo_Abierto_M2Cuido_Materno2023-09-27_10-14-59_0000001 93.wav 12 1 0 0 0 0
D_Campo_Abierto_M2Cuido_Materno2023-09-27_10-24-11_0000002 id 123.wav 0 0 0 0 1 0
D_Campo_Abierto_M2Cuido_Materno2023-09-27_10-33-21_0000003 id 116.wav 1 0 0 0 0 0
D_Campo_Abierto_M2Cuido_Materno2023-09-27_10-42-22_0000004 105.wav 6 12 2 3 1 4
D_Campo_Abierto_M2Cuido_Materno2023-09-27_10-51-17_0000005.wav 0 0 0 0 0 0
D_Interaccion_social_M2Cuido_Materno2023-09-27_08-04-58_0000001 ID 93.wav 0 0 96 173 134 129 208 111 93 174 132 120 62 33 74 121 62 32
D_Interaccion_social_M2Cuido_Materno2023-09-27_08-30-11_0000002 id 123.wav 0 0 165 271 240 233 167 79 114 130 168 137 150 123 2 127 145 30
D_Interaccion_social_M2Cuido_Materno2023-09-27_08-53-54_0000003 id116.wav 0 1 238 255 175 107 185 141 138 112 88 95 48 52 70 99 42 1
D_Interaccion_social_M2Cuido_Materno2023-09-27_09-18-15_0000004 id 105.wav 118 165 146 179 142 148 155 158 150 145 103 108 124 121 76 96 113 24
D_Interaccion_social_M2Cuido_Materno2023-09-27_09-41-51_0000005.wav 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1.1.2 Minute with highest USV count

Code
vr <- vocal_rate(X = reass_detec, time.window = 60, time.resolution = 1, path = "/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/consolidated_sound_files/")

# rate per minute
vr$rate <- vr$rate * 60

# get highest per sound file
sub_vr <- vr[vr$duration == 60, ]

# get the row with the highest rate per sound file
highes_min <- do.call(rbind, lapply(split(sub_vr, sub_vr$sound.files), function(x) x[which.max(x$rate), ]))

print_kable(highes_min)
sound.files start end counts duration rate
A_Interaccion_social_M2Cuido_Materno2023-09-27_08-04-58_0000001 ID 145.wav 667 727 266 60 266
A_Interaccion_social_M2Cuido_Materno2023-09-27_08-30-11_0000002 id 109.wav 140 200 216 60 216
A_Interaccion_social_M2Cuido_Materno2023-09-27_08-53-54_0000003 id 92.wav 656 716 163 60 163
A_Interaccion_social_M2Cuido_Materno2023-09-27_09-18-15_0000004 id 144.wav 136 196 176 60 176
A_Interaccion_social_M2Cuido_Materno2023-09-27_09-41-51_0000005 id 103.wav 142 202 130 60 130
B_Campo_Abierto_M2Cuido_Materno2023-09-27_10-14-59_0000001id 101.wav 238 298 3 60 3
B_Campo_Abierto_M2Cuido_Materno2023-09-27_10-33-21_0000003 id 110.wav 3 63 10 60 10
B_Campo_Abierto_M2Cuido_Materno2023-09-27_10-42-22_0000004 id 127.wav 119 179 1 60 1
B_Interaccion_social_M2Cuido_Materno2023-09-27_08-04-58_0000001 ID 101.wav 394 454 77 60 77
B_Interaccion_social_M2Cuido_Materno2023-09-27_08-30-11_0000002 id 115.wav 220 280 280 60 280
B_Interaccion_social_M2Cuido_Materno2023-09-27_08-53-54_0000003 id 110.wav 144 204 237 60 237
B_Interaccion_social_M2Cuido_Materno2023-09-27_09-18-15_0000004 id 127.wav 18 78 54 60 54
B_Interaccion_social_M2Cuido_Materno2023-09-27_09-41-51_0000005 id 128.wav 200 260 240 60 240
C_Campo_Abierto_M2Cuido_Materno2023-09-27_10-33-21_0000003 id 129.wav 159 219 47 60 47
C_Campo_Abierto_M2Cuido_Materno2023-09-27_10-42-22_0000004 id 122.wav 51 111 4 60 4
C_Interaccion_social_M2Cuido_Materno2023-09-27_08-04-58_0000001 ID 97.wav 243 303 262 60 262
C_Interaccion_social_M2Cuido_Materno2023-09-27_08-30-11_0000002 id 147.wav 211 271 77 60 77
C_Interaccion_social_M2Cuido_Materno2023-09-27_08-53-54_0000003 id 129.wav 469 529 235 60 235
C_Interaccion_social_M2Cuido_Materno2023-09-27_09-18-15_0000004 id 122.wav 136 196 128 60 128
C_Interaccion_social_M2Cuido_Materno2023-09-27_09-41-51_0000005 106.wav 128 188 187 60 187
D_Campo_Abierto_M2Cuido_Materno2023-09-27_10-14-59_0000001 93.wav 0 60 12 60 12
D_Campo_Abierto_M2Cuido_Materno2023-09-27_10-24-11_0000002 id 123.wav 196 256 1 60 1
D_Campo_Abierto_M2Cuido_Materno2023-09-27_10-33-21_0000003 id 116.wav 0 60 1 60 1
D_Campo_Abierto_M2Cuido_Materno2023-09-27_10-42-22_0000004 105.wav 49 109 13 60 13
D_Interaccion_social_M2Cuido_Materno2023-09-27_08-04-58_0000001 ID 93.wav 361 421 209 60 209
D_Interaccion_social_M2Cuido_Materno2023-09-27_08-30-11_0000002 id 123.wav 167 227 291 60 291
D_Interaccion_social_M2Cuido_Materno2023-09-27_08-53-54_0000003 id116.wav 148 208 346 60 346
D_Interaccion_social_M2Cuido_Materno2023-09-27_09-18-15_0000004 id 105.wav 175 235 194 60 194
D_Interaccion_social_M2Cuido_Materno2023-09-27_09-41-51_0000005.wav 0 60 1 60 1

1.1.3 Counts before and after mate entry

Code
mate_entry <- read_xlsx("/media/m/Backup Plus/CUIDO MATERNO/VOCALIZACIONES M2 isoc y ca/consolidated_sound_files/Tanda ISOC.xlsx")

mate_entry <- mate_entry[!is.na(mate_entry$seconds), ]

# count before and after
out <- lapply(mate_entry$SOUND_FILES, function(x){
    
    # get the sound file
    sf <- reass_detec[reass_detec$sound.files == x, ]
    
    sf$mid.point <- (sf$end + sf$start) / 2
    
    # get the time of the mate entry
    t <- mate_entry$seconds[mate_entry$SOUND_FILES == x]

    # get the calls before and after the mate entry
    before <- sum(sf$mid.point < t)
    after <- sum(sf$mid.point >= t)
    min.before <- sum(sf$mid.point < t & sf$mid.point >= t - 60)
    min.after <- sum(sf$mid.point >= t & sf$mid.point < t + 60)
    
    return(data.frame(sound.files = x, mate.entry = t, count.before = before, count.after = after, count.min.before = min.before, count.min.after = min.after))
})
    
counts <- do.call(rbind, out)



print_kable(counts)
sound.files mate.entry count.before count.after count.min.before count.min.after
A_Interaccion_social_M2Cuido_Materno2023-09-27_08-04-58_0000001 ID 145.wav 257 363 1776 225 167
B_Interaccion_social_M2Cuido_Materno2023-09-27_08-04-58_0000001 ID 101.wav 257 96 507 56 71
C_Interaccion_social_M2Cuido_Materno2023-09-27_08-04-58_0000001 ID 97.wav 234 246 1325 224 233
D_Interaccion_social_M2Cuido_Materno2023-09-27_08-04-58_0000001 ID 93.wav 230 229 1525 158 155
A_Interaccion_social_M2Cuido_Materno2023-09-27_08-30-11_0000002 id 109.wav 184 178 637 174 92
B_Interaccion_social_M2Cuido_Materno2023-09-27_08-30-11_0000002 id 115.wav 184 159 2281 158 254
C_Interaccion_social_M2Cuido_Materno2023-09-27_08-30-11_0000002 id 147.wav 164 44 454 43 58
D_Interaccion_social_M2Cuido_Materno2023-09-27_08-30-11_0000002 id 123.wav 164 110 2171 110 273
A_Interaccion_social_M2Cuido_Materno2023-09-27_08-53-54_0000003 id 92.wav 206 179 1252 121 138
B_Interaccion_social_M2Cuido_Materno2023-09-27_08-53-54_0000003 id 110.wav 206 290 851 231 145
C_Interaccion_social_M2Cuido_Materno2023-09-27_08-53-54_0000003 id 129.wav 172 113 1651 105 139
D_Interaccion_social_M2Cuido_Materno2023-09-27_08-53-54_0000003 id116.wav 177 224 1623 223 260
A_Interaccion_social_M2Cuido_Materno2023-09-27_09-18-15_0000004 id 144.wav 207 196 725 146 140
B_Interaccion_social_M2Cuido_Materno2023-09-27_09-18-15_0000004 id 127.wav 206 121 187 41 19
C_Interaccion_social_M2Cuido_Materno2023-09-27_09-18-15_0000004 id 122.wav 180 125 407 104 53
D_Interaccion_social_M2Cuido_Materno2023-09-27_09-18-15_0000004 id 105.wav 180 429 1842 146 179
A_Interaccion_social_M2Cuido_Materno2023-09-27_09-41-51_0000005 id 103.wav 191 113 573 113 91
B_Interaccion_social_M2Cuido_Materno2023-09-27_09-41-51_0000005 id 128.wav 191 189 950 174 218
C_Interaccion_social_M2Cuido_Materno2023-09-27_09-41-51_0000005 106.wav 160 94 875 94 108

Session information

R version 4.5.0 (2025-04-11)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0  LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=es_CR.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=es_CR.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=es_CR.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=es_CR.UTF-8 LC_IDENTIFICATION=C       

time zone: America/Costa_Rica
tzcode source: system (glibc)

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] ggplot2_3.5.2           ohun_1.0.3              DT_0.33                
 [4] kableExtra_1.4.0        ranger_0.17.0           readxl_1.4.5           
 [7] svMisc_1.4.3            rfigshare_0.3.8         viridis_0.6.5          
[10] viridisLite_0.4.2       Rraven_1.0.14           pbapply_1.7-4          
[13] bioacoustics_0.2.8.9000 warbleR_1.1.35          NatureSounds_1.0.5     
[16] knitr_1.50              seewave_2.2.3           tuneR_1.4.7            
[19] devtools_2.4.5          usethis_3.1.0          

loaded via a namespace (and not attached):
 [1] DBI_1.2.3           bitops_1.0-9        gridExtra_2.3      
 [4] remotes_2.5.0       testthat_3.2.3      rlang_1.1.6        
 [7] magrittr_2.0.3      e1071_1.7-16        compiler_4.5.0     
[10] systemfonts_1.2.3   vctrs_0.6.5         stringr_1.5.1      
[13] profvis_0.4.0       pkgconfig_2.0.3     crayon_1.5.3       
[16] fastmap_1.2.0       backports_1.5.0     ellipsis_0.3.2     
[19] promises_1.3.3      rmarkdown_2.29      sessioninfo_1.2.3  
[22] purrr_1.0.4         xfun_0.52           cachem_1.1.0       
[25] jsonlite_2.0.0      later_1.4.2         R6_2.6.1           
[28] stringi_1.8.7       RColorBrewer_1.1-3  pkgload_1.4.0      
[31] brio_1.1.5          cellranger_1.1.0    Rcpp_1.1.0         
[34] httpuv_1.6.16       Matrix_1.7-3        igraph_2.1.4       
[37] tidyselect_1.2.1    rstudioapi_0.17.1   yaml_2.3.10        
[40] dtw_1.23-1          miniUI_0.1.2        curl_6.4.0         
[43] pkgbuild_1.4.8      lattice_0.22-7      tibble_3.3.0       
[46] shiny_1.10.0        withr_3.0.2         evaluate_1.0.3     
[49] signal_1.8-1        moments_0.14.1      sf_1.0-21          
[52] sketchy_1.0.5       units_0.8-7         proxy_0.4-27       
[55] urlchecker_1.0.1    xml2_1.3.8          pillar_1.11.0      
[58] packrat_0.9.2       KernSmooth_2.23-26  checkmate_2.3.2    
[61] generics_0.1.4      RCurl_1.98-1.17     scales_1.4.0       
[64] xtable_1.8-4        class_7.3-23        glue_1.8.0         
[67] tools_4.5.0         xaringanExtra_0.8.0 fs_1.6.6           
[70] XML_3.99-0.18       grid_4.5.0          RJSONIO_2.0.0      
[73] cli_3.6.5           svglite_2.1.3       dplyr_1.1.4        
[76] gtable_0.3.6        fftw_1.0-9          digest_0.6.37      
[79] classInt_0.4-11     rjson_0.2.23        htmlwidgets_1.6.4  
[82] farver_2.1.2        memoise_2.0.1       htmltools_0.5.8.1  
[85] lifecycle_1.0.4     httr_1.4.7          mime_0.13          
[88] MASS_7.3-65