-
Notifications
You must be signed in to change notification settings - Fork 0
/
UseBC_1.1.Rmd
267 lines (206 loc) · 8.61 KB
/
UseBC_1.1.Rmd
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
---
title: "Output of Batclassify"
author: "JF Godeau"
date: "`r Sys.setlocale('LC_ALL', 'en_GB.UTF-8'); format(Sys.time(), '%d %B %Y')`"
output:
html_document:
fig_caption: yes
self_contained: no
toc: yes
pdf_document:
toc: yes
word_document:
toc: yes
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
#FILE <- file.choose()
DIR <- "/media/jf/Elements/Audiomoth/20180601_Polany/SPHINX"
FILE <- paste(DIR,"Results.csv",sep="/")
require(data.table)
require(lubridate)
require(knitr)
knitr::opts_chunk$set(echo = FALSE, message = FALSE)
#knitr::opts_knit$set(root.dir=DIR) # ???
# Threshold value of probablilities to discard
Trsh <- 0.69
```
## General info
```{r 'Reformat Date and Time'}
RBC <- read.csv(FILE)
DT.RBC <- data.table(RBC)
DT.RBC[, Date := ymd(substring(DT.RBC$FileName,1,10))]
DT.RBC[, Time := as.character(hms(substring(DT.RBC$FileName,12,19)))]
PER <- max(ymd_hms(DT.RBC$FileName, tz="Europe/Amsterdam")) - min(ymd_hms(DT.RBC$FileName, tz="Europe/Amsterdam"))
PER.h <- round(difftime(max(ymd_hms(DT.RBC$FileName, tz="Europe/Amsterdam")),
min(ymd_hms(DT.RBC$FileName, tz="Europe/Amsterdam"))
,units="hours"),1)
DT.num <- data.table(DT.RBC[,5:16], keep.rownames=T)
#DT.num[,Ppyg := NULL] ## !! Remove Ppyg data!!
LogiLine2keep <- apply(DT.num, 1, max) > Trsh
LogiLine2keepNoPyg <- apply(DT.num[,-10], 1, max) > Trsh
TM <- as.POSIXct(DT.RBC$FileName, format="%Y-%m-%d_%H_%M_%S")
TM.TSH <- data.frame(time=TM[LogiLine2keepNoPyg])
TM.TSH$valmax <- apply(DT.num, 1, max)[LogiLine2keepNoPyg]
require(ggplot2)
#ggplot(TM.TSH, aes(time, valmax)) + geom_point()
G1 <- ggplot(data.frame(TM), aes(TM, 0)) +
geom_point() +
theme_bw()+
# geom_point(data=TM.TSH, aes(time, valmax), col="red", inherit.aes = F) +
# scale_y_continuous(limits=c(min(TM.TSH$valmax), max(TM.TSH$valmax))) +
geom_histogram(data=TM.TSH, aes(x=time), stat ="bin", inherit.aes = F, alpha= 0.5, binwidth=1000) +
# scale_y_continuous(sec.axis = sec_axis(~., name = "NEW")) +
scale_x_datetime(date_breaks="2 hours") +
theme(axis.text.x = element_text(angle = 90, vjust = 1.0, hjust = 1.0))+
xlab("Time") + ylab("N samples > Threshold")
```
Name of the folder: **`r unique(DT.RBC$FilePath)`**
Number of files: **`r dim(DT.RBC)[1]`**
Number of files with at least one probability value above the threshold (defined as `r Trsh`): **`r sum(LogiLine2keep)`**
Dates recorded for these files: `r paste(unique(DT.RBC$Date), collapse="; ")`
Time difference between first and last recording: **`r round(PER,1)`** , (= `r PER.h` hours).
## Distribution of the data in time
Black dots at y=0 shows the recording period.
```{r, fig.height=3}
G1
```
## Maximal score obtained for each species
### All species of Batclassify result table
```{r "Global probability for each species"}
MxSp <- sapply(DT.num, max)
#kable(MxSp, caption="General score")
kable(data.frame(t(MxSp)), caption="General score")
```
### Only species with a probability above the threshold (`r Trsh`)
```{r "Global probability for each species Sup threshold"}
kable(data.frame(t(MxSp[MxSp > Trsh])), caption="Species with p > 0.5")
```
## Number of files where probability is at least above the threshold
### For each species
```{r 'Select the species, if any, above the threshold for each file'}
CompMax <- cbind(max.col(DT.num[LogiLine2keep,], "first"),
max.col(DT.num[LogiLine2keep,], "last"))
Follow <- apply(DT.num[LogiLine2keep,] ,
1,
function(x){ord <- order(x, decreasing = T)
paste(paste(colnames(DT.num)[ord][ x[ord] > Trsh][-1],
x[ord][ x[ord] > Trsh][-1]), collapse=" / ")
})
LogiOneMax <- CompMax[,1] - CompMax[,2] == 0
DFSpMax <- cbind(FileName=as.character(DT.RBC$FileName[LogiLine2keep]),
SpeciesMax=names(DT.num)[CompMax[,1]],
Prob=apply(DT.num, 1, max)[LogiLine2keep],
OtherSp = as.character(Follow))
if(any(!LogiOneMax)){
Idx <- which(!LogiOneMax)
if(sum(!LogiOneMax)>1){
DFSpMax[Idx,2] <- apply(matrix(names(DT.num)[CompMax],
nrow=length(LogiOneMax))[Idx,],
1,
function(x) paste(x, collapse="-"))
} else {
DFSpMax[Idx,2] <- paste(names(DT.num)[CompMax[Idx,]], collapse = "-")
}
}
DFSpMax <- data.frame(DFSpMax)
kable(sort(table(DFSpMax$SpeciesMax), decreasing = T), col.names = c("Species","N"))
```
### For each species per category of probability
```{r SepProbCat}
kable(as.data.frame.matrix(table(DFSpMax$SpeciesMax, round(as.numeric(as.character(DFSpMax$Prob)),1))),
caption="Number of files with each species for probablility categories")
```
## Fully detailed results
```{r 'Final table (PDF or docx)', include=F}
kable(DFSpMax) #If not html output
```
```{r 'Final table', eval=T, include=T, results='asis'}
suppressPackageStartupMessages(library(googleVis))
require(googleVis)
op <- options(gvis.plot.tag='chart') ### to export the chart/table in knitted html
Table <- gvisTable(DFSpMax)
plot(Table)
```
## Analyse of NSL group
```{r "NSL-metrics"}
require(bioacoustics)
require(ggpubr)
Nms <- DFSpMax$FileName[grepl("NSL", DFSpMax$SpeciesMax) | grepl("NSL", DFSpMax$OtherSp)]
#Nms <- DFSpMax$FileName[grepl("Ppyg", DFSpMax$SpeciesMax) | grepl("Ppyg", DFSpMax$OtherSp)]
Pk <- do.call("rbind",
lapply(Nms, function(x){
AUD <- read_audio(paste0(DIR,"/",x,".wav"))
Output <- try(threshold_detection(AUD,
settings=T, HPF=17000,
FFT_size = 1024, threshold = 6))
Output$event_data[,c("filename", "starting_time", "duration",
"freq_max_amp", "freq_max", "freq_min",
"bandwidth", "slope", "curve_neg", "snr")]
}))
## Logicals
Cd.NYCNOC <- Pk$freq_max_amp < 22000 & Pk$freq_min < 21000
Cd.QFC <- Pk$duration > 5 & Pk$bandwidth < 5000
DF.NSL <- data.frame(N.Signals = table(factor(Pk$filename)),
N.Sig_QFC = as.vector(table(Pk$filename[Cd.QFC])),
N.Sig_NYCNOC = as.vector(table(Pk$filename[Cd.NYCNOC])))
kable(DF.NSL[order(DF.NSL$N.Signals.Freq, decreasing = T),])
```
The samples evaluated with at least 70% of probablility of belonging to the *NSL group* are analyzed broadly.
`r length(levels(Pk$filename))` files are selected, totalizing `r dim(Pk)[1]` signals.
### Full dataset of NSL
#### Dispersion on the FME and FT scale.
```{r "NSL-plot1"}
theme_set(theme_bw())
## Plots
Gx1 <- ggplot(Pk, aes(y=freq_max_amp, x=factor(1))) +
geom_boxplot() + geom_jitter(aes(color=filename, alpha = 0.4)) +
ggtitle("FME (limit Nnoc ~ 22 kHz)") +
geom_hline(yintercept = 22000, color='red', linetype=2) +
xlab("")
Gx2 <- ggplot(Pk, aes(y=freq_min, x=factor(1))) +
geom_boxplot() + geom_jitter(aes(color=filename, alpha = 0.3)) +
ggtitle("FT (limit Nnoc ~ 21 kHz)") +
geom_hline(yintercept = 21000, color='orange', linetype=2) +
xlab("")
ggarrange(Gx1, Gx2, legend = 'none')
```
#### Plots *Barataud*
```{r "NSL-plot2"}
G1 <- ggplot(data=Pk, aes(x=freq_max_amp, y=bandwidth, color=filename)) +
geom_point() + ggtitle("LB vs. FME")
G2 <- ggplot(data=Pk, aes(x=duration, y=freq_min, color=filename)) +
geom_point() + ggtitle("FT vs. Durée")
ggarrange(G1, G2, common.legend = T, legend = 'none')
```
### Selection of possible QFC
i.e. duration > 5 ms and LB < 5 kHz
```{r "NSL-plot3"}
qfc.y1 <- c(0,6000)
qfc.x1 <- c(15000,36000)
qfc.y2 <- c(15000,36000)
qfc.x2 <- c(5,26)
table(factor(Pk$filename[Cd.QFC]))
Gqfc1 <- ggplot(data=Pk[Cd.QFC,], aes(x=freq_max_amp, y=bandwidth, color=filename)) +
geom_point() + ggtitle("LB vs. FME (QFC only!)") + ylim(qfc.y1) + xlim(qfc.x1)
Gqfc2 <- ggplot(data=Pk[Cd.QFC,], aes(x=duration, y=freq_min, color=filename)) +
geom_point() + ggtitle("FT vs. Durée (QFC only!)") + ylim(qfc.y2) + xlim(qfc.x2)
ggarrange(Gqfc1, Gqfc2, common.legend = T, legend = 'none')
```
### Selection of possible *Nyctalus noctula*
i. e. FME < 22 kHz and FT < 21 kHz
```{r "NSL-plot4"}
table(factor(Pk$filename[Cd.NYCNOC]))
GNN1 <- ggplot(data=Pk[Cd.NYCNOC,], aes(x=freq_max_amp, y=bandwidth, color=filename)) +
geom_point() + ggtitle("LB vs. FME (NYCNOC? only)")
GNN2 <- ggplot(data=Pk[Cd.NYCNOC,], aes(x=duration, y=freq_min, color=filename)) +
geom_point() + ggtitle("FT vs. Durée (NYCNOC? only)")
ggarrange(GNN1, GNN2, common.legend = T, legend = 'none')
```
## Full table (any > Trsh)
```{r 'Full table 05', eval=T, include=F, results='asis'}
op <- options(gvis.plot.tag='chart') ### to export the chart/table in knitted html
Table <- gvisTable(DT.RBC[,-c(1,3,4)])
plot(Table)
```