-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUseBC_1.0.Rmd
168 lines (136 loc) · 5.48 KB
/
UseBC_1.0.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
---
title: "Output of Batclassify"
author: "JF Godeau"
date: "`r Sys.setlocale('LC_ALL', 'en_GB.UTF-8'); format(Sys.time(), '%d %B %Y')`"
output:
pdf_document:
toc: yes
word_document:
toc: yes
html_document:
fig_caption: yes
self_contained: no
toc: yes
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
#FILE <- file.choose()
DIR <- "/media/jf/Elements/Audiomoth/20180713_AfterLIFE_Mellier/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.nuname <- data.table(DT.RBC[,c(2,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=paste0("Species with p > ",Trsh))
```
## 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=T}
kable(DFSpMax) #If not html output
```
```{r 'Final table', eval=F, include=F, 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)
```
```{r 'Full table 05', eval=F, include=F, results='asis'}
## Full table (any > Trsh)
op <- options(gvis.plot.tag='chart') ### to export the chart/table in knitted html
Table <- gvisTable(DT.nuname)
plot(Table)
```
```{r 'Full table 05-PDF-docx', eval=F, include=F}
## Full table (any > Trsh)
kable(DT.nuname)
```