annotate IdValid.R @ 0:0e3db3a308c0 draft default tip

Uploaded
author mnhn65mo
date Mon, 06 Aug 2018 09:13:29 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
1 library(data.table)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
2
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
3 ValidHier=function(x,y) #used to write validator id over observer id
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
4 {
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
5 if(y==""){x}else{y}
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
6 }
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
7
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
8 f2p <- function(x) #get date-time data from recording file names
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
9 {
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
10 if (is.data.frame((x)[1])) {pretemps <- vector(length = nrow(x))}
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
11 op <- options(digits.secs = 3)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
12 pretemps <- paste(substr(x, nchar(x) - 18, nchar(x)-4), ".", substr(x, nchar(x) - 2, nchar(x)), sep = "")
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
13 strptime(pretemps, "%Y%m%d_%H%M%OS",tz="UTC")-7200
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
14 }
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
15
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
16 args <- commandArgs(trailingOnly = TRUE)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
17
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
18
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
19 #print(args)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
20
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
21
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
22 #for test
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
23 #inputest=list.files("C:/Users/Yves Bas/Documents/GitHub/65MO_Galaxy-E/raw_scripts/Vigie-Chiro/output_IdCorrect_2ndLayer_input_IdValid/",full.names=T)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
24 #for (i in 1:length(inputest))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
25 #{
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
26 #args=c(inputest[i],"Referentiel_seuils_C2.csv")
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
27 #args=c("5857d56d9ebce1000ed89ea7-DataCorrC2.csv","Referentiel_seuils_C2.csv")
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
28
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
29
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
30
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
31 IdCorrect=fread(args[1])
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
32 RefSeuil=fread(args[2])
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
33 #IdV=as.data.frame(subset(IdCorrect,select=observateur_taxon:validateur_probabilite))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
34
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
35 #Step 0 :compute id score from 2nd Layer
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
36 test=match("participation",names(IdCorrect))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
37 IdCorrect$IdScore=apply(as.data.frame(IdCorrect)[,(test+1):(ncol(IdCorrect)-1)],MARGIN=1,FUN=max)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
38 #compute true success probabilities according to logistic regression issued from "Referentiel_seuils"
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
39 CorrSp=match(IdCorrect$ProbEsp_C2bs,RefSeuil$Espece)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
40 PSp=RefSeuil$Pente[CorrSp]
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
41 ISp=RefSeuil$Int[CorrSp]
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
42
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
43 suppressWarnings(IdCorrect$IdProb<-mapply(FUN=function(w,x,y) if((!is.na(y))&(y>0)&(y<1000)) {(exp(y*w+x)/(1+exp(y*w+x)))}else{w} ,IdCorrect$IdScore,ISp,PSp))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
44
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
45
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
46
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
47
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
48 #Step 1 :compute id with confidence regarding a hierarchy (validator > observer)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
49 IdCorrect$IdV=mapply(ValidHier,IdCorrect$observateur_taxon,IdCorrect$validateur_taxon)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
50 IdCorrect$ConfV=mapply(ValidHier,IdCorrect$observateur_probabilite
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
51 ,IdCorrect$validateur_probabilite)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
52
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
53
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
54 #print(paste(args[1],length(subset(IdCorrect$ConfV,IdCorrect$ConfV!=""))))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
55
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
56 #Step 2: Get numerictime data
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
57 suppressWarnings(IdCorrect$Session<-NULL)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
58 suppressWarnings(IdCorrect$TimeNum<-NULL)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
59
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
60 if (substr(IdCorrect$`nom du fichier`[1],2,2)=="i") #for car/walk transects
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
61 {
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
62 FileInfo=as.data.table(tstrsplit(IdCorrect$`nom du fichier`,"-"))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
63 IdCorrect$Session=as.numeric(substr(FileInfo$V4,5,nchar(FileInfo$V4)))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
64 TimeSec=as.data.table(tstrsplit(FileInfo$V5,"_"))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
65 TimeSec=as.data.frame(TimeSec)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
66 if(sum(TimeSec[,(ncol(TimeSec)-1)]!="00000")==0) #to deal with double Kaleidoscope treatments
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
67 {
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
68 print("NOMS DE FICHIERS NON CONFORMES")
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
69 print("Vous les avez probablement traiter 2 fois par Kaleidoscope")
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
70 stop("Merci de nous signaler cette erreur par mail pour correction")
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
71 }else{
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
72 IdCorrect$TimeNum=(IdCorrect$Session*800
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
73 +as.numeric(TimeSec[,(ncol(TimeSec)-1)])
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
74 +as.numeric(TimeSec[,(ncol(TimeSec))])/1000)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
75 }
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
76
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
77 }else{
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
78 if(substr(IdCorrect$`nom du fichier`[1],2,2)=="a") #for stationary recordings
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
79 {
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
80 DateRec=as.POSIXlt(f2p(IdCorrect$`nom du fichier`))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
81 Nuit=format(as.Date(DateRec-43200*(DateRec$hour<12)),format="%d/%m/%Y")
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
82 #Nuit[is.na(Nuit)]=0
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
83 IdCorrect$Session=Nuit
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
84 IdCorrect$TimeNum=as.numeric(DateRec)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
85
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
86 }else{
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
87 print("NOMS DE FICHIERS NON CONFORMES")
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
88 stop("Ils doivent commencer par Cir (routier/pedestre) ou par Car (points fixes")
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
89 }
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
90 }
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
91
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
92
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
93
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
94
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
95 #Step 3 :treat sequentially each species identified by Tadarida-C
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
96 IdExtrap=vector() #to store the id extrapolated from validations
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
97 IdC2=IdCorrect[0,] #to store data in the right order
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
98 TypeE=vector() #to store the type of extrapolation made
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
99 for (j in 1:nlevels(as.factor(IdCorrect$ProbEsp_C2bs)))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
100 {
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
101 IdSp=subset(IdCorrect
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
102 ,IdCorrect$ProbEsp_C2bs==levels(as.factor(IdCorrect$ProbEsp_C2bs))[j])
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
103 if(sum(IdSp$IdV=="")==(nrow(IdSp))) #case 1 : no validation no change
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
104 {
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
105 IdC2=rbind(IdC2,IdSp)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
106 IdExtrap=c(IdExtrap,rep(IdSp$ProbEsp_C2bs[1],nrow(IdSp)))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
107 TypeE=c(TypeE,rep(0,nrow(IdSp)))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
108 }else{ #case 2: some validation
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
109 Vtemp=subset(IdSp,IdSp$IdV!="")
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
110 #case2A: validations are homogeneous
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
111 if(nlevels(as.factor(Vtemp$IdV))==1)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
112 {
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
113 IdC2=rbind(IdC2,IdSp)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
114 IdExtrap=c(IdExtrap,rep(Vtemp$IdV[1],nrow(IdSp)))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
115 TypeE=c(TypeE,rep(2,nrow(IdSp)))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
116 }else{
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
117 #case 2B: validations are heterogeneous
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
118 #case 2B1: some validations confirms the species identified by Tadarida and highest confidence are confirmed
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
119 subVT=subset(Vtemp,Vtemp$IdV==levels(as.factor(IdCorrect$ProbEsp_C2bs))[j])
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
120 subVF=subset(Vtemp,Vtemp$IdV!=levels(as.factor(IdCorrect$ProbEsp_C2bs))[j])
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
121 if((nrow(subVT)>0)&(max(subVT$IdProb)>max(subVF$IdProb)))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
122 {
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
123 Vtemp=Vtemp[order(Vtemp$IdProb),]
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
124 test=(Vtemp$IdV!=Vtemp$ProbEsp_C2bs)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
125 Fr1=max(which(test == TRUE)) #find the error with highest indices
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
126 Thr1=mean(Vtemp$IdProb[(Fr1):(Fr1+1)]) #define first threshold as the median confidence between the first error and the confirmed ID right over it
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
127 #id over this threshold are considered right
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
128 IdHC=subset(IdSp,IdSp$IdProb>Thr1)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
129 IdC2=rbind(IdC2,IdHC)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
130 IdExtrap=c(IdExtrap,rep(Vtemp$IdV[nrow(Vtemp)],nrow(IdHC)))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
131 TypeE=c(TypeE,rep(2,nrow(IdHC)))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
132 #id under this threshold are attributed to validated id closest in time
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
133 Vtemp=Vtemp[order(Vtemp$TimeNum),]
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
134 cuts <- c(-Inf, Vtemp$TimeNum[-1]-diff(Vtemp$TimeNum)/2, Inf)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
135 CorrV=findInterval(IdSp$TimeNum, cuts)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
136 IdE=Vtemp$IdV[CorrV]
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
137 IdEL=subset(IdE,IdSp$IdProb<=Thr1)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
138 IdLC=subset(IdSp,IdSp$IdProb<=Thr1)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
139 IdExtrap=c(IdExtrap,IdEL)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
140 TypeE=c(TypeE,rep(1,length(IdEL)))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
141 IdC2=rbind(IdC2,IdLC)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
142
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
143
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
144 }else{
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
145 #case 2B2: all validations concerns errors
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
146 #id are extrapolated on time only
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
147 Vtemp=Vtemp[order(Vtemp$TimeNum),]
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
148 cuts <- c(-Inf, Vtemp$TimeNum[-1]-diff(Vtemp$TimeNum)/2, Inf)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
149 CorrV=findInterval(IdSp$TimeNum, cuts)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
150 IdE=Vtemp$IdV[CorrV]
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
151 IdExtrap=c(IdExtrap,IdE)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
152 TypeE=c(TypeE,rep(1,length(IdE)))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
153 IdC2=rbind(IdC2,IdSp)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
154 }
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
155 }
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
156
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
157
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
158 }
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
159
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
160
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
161 }
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
162 test1=(nrow(IdC2)==length(IdExtrap))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
163 test2=(nrow(IdC2)==nrow(IdCorrect))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
164 if((test1==F)|(test2==F))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
165 {
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
166 (stop("Erreur de traitement !!!"))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
167 }
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
168
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
169 IdC2$IdExtrap=IdExtrap
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
170 IdC2$TypeE=TypeE
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
171
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
172
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
173 IdC2=IdC2[order(IdC2$IdProb,decreasing=T),]
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
174 IdC2=IdC2[order(IdC2$ConfV,decreasing=T),]
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
175 IdC2=IdC2[order(IdC2$`nom du fichier`),]
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
176 #discard duplicated species within the same files (= false positives corrected by 2nd layer)
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
177 IdC2=unique(IdC2,by=c("nom du fichier","IdExtrap"))
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
178
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
179 write.table(IdC2,"output.tabular",row.names=F,sep="\t")
0e3db3a308c0 Uploaded
mnhn65mo
parents:
diff changeset
180 #write.table(IdC2,paste0(substr(args[1],1,nchar(args[1])-15),"-IdC2.csv"),row.names=F,sep="\t")