1

I am trying to follow this bioacoustics in R guide to help me run analysis on some frog chirps. I am also looking at this MonitoR guide as they're trying to accomplish the same thing (using templates to run against sound files).

As I have over 30,000 frog sound files of about 5 MB each, I've created a "dummy" folder with 20 random frog sound files in it and 5 template files. I figured this would make things quicker and once I had my code fully working I would tweak the folder names.

I am working in R Studio and the sound files are WAV ones.

I have loaded MonitoR and WarbleR using library(monitoR, warbleR) and I think that has successfully loaded them.

Then I have set my working directory to that "dummy" folder with the 20 frog files and 5 chirp template files. I know that one has worked as when I call up list.files() they all appear.

> list.files()
 [1] "frog1.WAV"          "frog10.WAV"         "frog11.WAV"        
 [4] "frog12.WAV"         "frog13.WAV"         "frog14.WAV"        
 [7] "frog15.WAV"         "frog16.WAV"         "frog17.WAV"        
[10] "frog18.WAV"         "frog19.WAV"         "frog2.WAV"         
[13] "frog20.WAV"         "frog3.WAV"          "frog4.WAV"         
[16] "frog5.WAV"          "frog6.WAV"          "frog7.WAV"         
[19] "frog8.WAV"          "frog9.WAV"          "template_test1.WAV"
[22] "template_test2.WAV" "template_test3.WAV" "template_test4.WAV"
[25] "template_test5.WAV"

It is the second step of the first guide that I keep getting error messages on no matter how I try and tweak it.

Step 1 has this:

x<-c("warbleR", "monitoR")

That bit I understand as loading the packages, though I don't know what x and c are doing there...

if(!y %in% installed.packages()[,"Package"])  install.packages(y)
require(y, character.only = T)
 })

I have no idea what that bit does... but including it or excluding it makes no difference to the error messages in the next step.

Step 2 is creating templates and where I am getting stuck:

The guide says: # load sound files and data

data(list = c("Phae.long1", "Phae.long2", "Phae.long3", "Phae.long4", "selec.table"))

I am unsure what the selec.table bit of the code does, but again, including or excluding it makes no difference to the error messages.

My attempted code is:

data(list = c("template_test1", "template_test2", "template_test3", "template_test4", "template_test5", "selec.table"))

and it throws up the following errors:

Warning messages: 1: In data(list = c("template_test1", "template_test2", "template_test3", : data set ‘template_test1’ not found 2: In data(list = c("template_test1", "template_test2", "template_test3", : data set ‘template_test2’ not found 3: In data(list = c("template_test1", "template_test2", "template_test3", : data set ‘template_test3’ not found 4: In data(list = c("template_test1", "template_test2", "template_test3", : data set ‘template_test4’ not found 5: In data(list = c("template_test1", "template_test2", "template_test3", : data set ‘template_test5’ not found

I've tried steps suggested in both guides (with no success) and looking online for where I am going wrong, but I'm afraid I'm stumped. I've got a long road in R ahead of me for analysing this data set so any help anyone reading this has to give is greatly appreciated. Thank-you.

Phil
  • 7,287
  • 3
  • 36
  • 66
FrogDude
  • 13
  • 5
  • In step 1, surprising to have `x` and `y` : it looks if the packages in vector `y` are installed, so why put the list in `x`? – Waldi Sep 04 '22 at 19:05
  • Hi @Waldi I have no idea what x and y are doing there (or c). I can understand that it's for loading the MonitoR and WarbleR packages, but other than that I have no idea what all the other code is doing in Step 1, sorry! – FrogDude Sep 05 '22 at 16:21
  • 1
    maybe an [intro to R](https://www.w3schools.com/r/r_vectors.asp) could help – Waldi Sep 05 '22 at 17:18

1 Answers1

0
library(monitoR)
library(tuneR)
knitr::opts_chunk$set(echo = FALSE)

MonitoR

Data

First we define where to find our files...

data_path <- paste0(getwd(), "./../data/")

tpl.1 <- "template_test1.WAV"
tpl.2 <- "template_test2.WAV"
tpl.3 <- "template_test3.WAV"
tpl.4 <- "template_test4.WAV"
tpl.5 <- "template_test5.WAV"

frog1 <- "frog1.WAV"
frog2 <- "frog2.WAV"


Spectograms

Then we display the spectogram of $tpl.1$

viewSpec(paste0(data_path, tpl.1))

template1 spectogram

We see the interesting frequencies are between 1.6 and 3 khz.

viewSpec(paste0(data_path, tpl.1), frq.lim = c(1.6, 3))

spectogram filtered

Correlation Templates

wct1 <- makeCorTemplate(paste0(data_path, tpl.1), 
                        name=tpl.1,
                        t.lim=c(0.1, 0.5),
                        frq.lim=c(1.6, 3))

correlation template 1

wct2 <- makeCorTemplate(paste0(data_path, tpl.2), 
                        name=tpl.2,
                        t.lim=c(0.0, 0.35),
                        frq.lim=c(1.0, 8.0))

correlation template 2

wct3 <- makeCorTemplate(paste0(data_path, tpl.3), 
                        name=tpl.3,
                        t.lim=c(0.0, 0.35),
                        frq.lim=c(1, 8))

correlation template 3

wct4 <- makeCorTemplate(paste0(data_path, tpl.4), 
                        name=tpl.4,
                        t.lim=c(0.0, 0.3),
                        frq.lim=c(0.0, 6.0))

correlation template 4

wct5 <- makeCorTemplate(paste0(data_path, tpl.5), 
                        name=tpl.5,
                        t.lim=c(0.0, 0.2),
                        frq.lim=c(0.0, 6))

correlation template 5

ctemps <- combineCorTemplates(wct1, wct2, wct3, wct4, wct5)

Checking Frog2 recording

cscores <- corMatch(paste0(data_path, frog2), ctemps)

We see here the max corelation score is between $tpl.1$ and $frog2$ around 0.84

cscores
min.score
<dbl>
max.score
<dbl>
n.scores
<int>
template_test1.WAV  -0.15   0.84    5119    
template_test2.WAV  0.23    0.71    5124    
template_test3.WAV  0.18    0.69    5124    
template_test4.WAV  0.52    0.80    5128    
template_test5.WAV  0.42    0.72    5138    
5 rows

From the n.peaks column, we can see there are from 112 to 250 peaks per template, and from the n.detections column, we can see that the templates resulted in from 5 to 250 detections

cdetects <- findPeaks(cscores)
cdetects
n.peaks
<int>
n.detections
<int>
min.peak.score
<dbl>
max.peak.score
<dbl>
template_test1.WAV  112 5   0.1195016   0.8438077   
template_test2.WAV  128 7   0.3092445   0.7053220   
template_test3.WAV  135 6   0.2550825   0.6853129   
template_test4.WAV  156 156 0.5934782   0.8018123   
template_test5.WAV  250 250 0.4687607   0.7191635   
5 rows | 1-5 of 6 columns
head(getDetections(cdetects), 6)
template
<chr>
date.time
<S3: POSIXct>
time
<dbl>
score
<dbl>
1   template_test1.WAV  2022-09-08 17:10:03 3.317333    0.5348492
2   template_test1.WAV  2022-09-08 17:10:07 7.786667    0.5271136
3   template_test1.WAV  2022-09-08 17:10:15 15.594667   0.8438077
4   template_test1.WAV  2022-09-08 17:10:19 19.626667   0.6815375
5   template_test1.WAV  2022-09-08 17:10:23 23.797333   0.7162522
6   template_test2.WAV  2022-09-08 17:10:03 3.296000    0.5244355
6 rows

Let's plot this!

plot(cdetects)

peaks default cutoff

This plot makes no sense to me so I'll just update the cutoff to some arbitrary values.

templateCutoff(cdetects) <- c(template_test1.WAV = 0.6, 
                              template_test2.WAV = 0.6, 
                              template_test3.WAV = 0.6, 
                              template_test4.WAV = 0.7, 
                              template_test5.WAV = 0.6)

what are our peaks now?

cdetects
n.peaks
<int>
n.detections
<int>
min.peak.score
<dbl>
max.peak.score
<dbl>
template_test1.WAV  112 3   0.1195016   0.8438077   
template_test2.WAV  128 2   0.3092445   0.7053220   
template_test3.WAV  135 4   0.2550825   0.6853129   
template_test4.WAV  156 6   0.5934782   0.8018123   
template_test5.WAV  250 6   0.4687607   0.7191635   
5 rows | 1-5 of 6 columns

Looking better!

plot(cdetects)

detection results

Will
  • 910
  • 7
  • 17
  • Gosh, thank you @Will ! I really appreciate you taking the time to try and help me out. I've tried out your solution on my files and it's a lot better than the guide but does unfortunately get stuck when creating the wct1 (etc) warning messages pop for each template that say: > 1: In min(x) : no non-missing arguments to min; returning Inf > 2: In max(x) : no non-missing arguments to max; returning -Inf > 3: In min(x) : no non-missing arguments to min; returning Inf > 4: In max(x) : no non-missing arguments to max; returning -Inf I've tried to add my code but can't bc of the char limit – FrogDude Sep 05 '22 at 16:35
  • Hey. Actually your project seems pretty fun :) I’ll try to reproduce at home later this week – Will Sep 05 '22 at 20:39
  • I could reproduce using a personal file, but it's a warning only. So it doesn't mean it doesn't work... – Will Sep 05 '22 at 23:22
  • Hi @Will I saw that and a few lines of your code after the warnings work but then R throws up error messages at the `cscores` bit so I thought maybe the problem was with the `wct` bits. (I'll make another comment with that error message bc of the character limit). If you have the time I would definitely welcome any help, that's extremely kind of you, thank-you. I love this research as it's on a critically endangered species that has barely any literature on it, so it's really exciting (even though I feel a bit lost with R, but am trying my best to learn). – FrogDude Sep 06 '22 at 13:16
  • `Error in !all.equal(template@t.step, t.step, tolerance = t.step/10000) : invalid argument type In addition: Warning message: In corMatch(survey = audio, templates = ctemps, write.wav = T) : time.source was set to "filename" but file name does not have date and time info, so using "fileinfo" instead` – FrogDude Sep 06 '22 at 13:16
  • well I can try to have a look but we should use the same data otherwise it's going to be painful. are you able to share the files you are working with? – Will Sep 06 '22 at 16:05
  • That would be fantastic if you have the time, thank-you very much! I've tried to see how people share files on here and it seems to be through FileDropper. Would that be OK for you or would you prefer another method? I see there is a meta discord server too? – FrogDude Sep 07 '22 at 13:02
  • I can't answer that I don't know I've just attached screenshots in the past... Personally I don't care as long as I don't have to make any effort to dl it... – Will Sep 07 '22 at 14:39
  • The easiest way seems to be through a [DropBox link](https://www.dropbox.com/scl/fo/75itv9hf781n0wdgxbs7w/h?dl=0&rlkey=mh6av0u9tolg4czb8dxsthh9k) . Let me know if you have any issues with that. I really appreciate you having a look at it, thank-you once again :) – FrogDude Sep 08 '22 at 15:22
  • I think it should be ok. I've run the analysis on Frog2, posted the Rmarkdown file plus screenshot of the result. – Will Sep 08 '22 at 21:16
  • Hi Will! Gosh, thank-you ever so much for taking the time to have a crack at this! That's so incredibly nice of you, I'm blown away. The code looks amazing and I love that you've laid it out step by step. I'm trying to get it to work for me, though think I'm tripping myself up by being stupid at this stage: `data_path <- paste0(getwd(), "./../data/")` . If I put where my folder is in the "" it's an error, but if I C&P as is later on the files aren't found... (Btw no worries if at this stage you'd rather not reply if I'm being frustratingly dense. I really appreciate all the help you've given) – FrogDude Sep 11 '22 at 13:10
  • data_path is the path to where the data files are, so when you value it, it could be something like "C:/myfrogfilefolder/" for example... but really at this point you need to start training in R. If you think your original question has been answered please mark it as such. I would be happy to hear about how your project is going in the future. phd? – Will Sep 11 '22 at 20:13
  • Apologies for taking a few days to reply/mark solved, I've been a spot under the weather. Thank-you for your time helping me, I really really appreciate it. I'm still fiddling around with the templates but further along now. This is for my MSc :) – FrogDude Sep 18 '22 at 17:53