-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRavenPlotter_April21.R
236 lines (139 loc) · 7.68 KB
/
RavenPlotter_April21.R
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
setwd(dir<-"C:/Users/Rusty/Amazon Drive/BOP/SOUND")
directories<-list.dirs("C:/Users/Rusty/Amazon Drive/BOP/Sound/Janelle")[-1] #Gets directories (not parent directory)
species.names<-list.dirs("C:/Users/Rusty/Amazon Drive/BOP/Sound/Janelle",full.names=FALSE)[-1]
mega<-c()
naindex<-0 #N of files for which special data assignements are missing
naughtyindex<-0 #N of files for which special data assignements are not 0/1
#Loops through all files and reads them into COMPOSITE Dataframe "Mega", omitting (and identifying)
# elements that are missing/wrong values for the 5 binary, human-classified variables
for(xx in 1:length(species.names)){ #Loop through each folder
file.names<-list.files(directories[xx],full.names=TRUE,pattern=".txt")
for(yy in 1:length(file.names)){ #Loop through text files for each species
#read in table, and provide specific classes for specific columns
table<-read.table(file.names[yy],sep="\t",header=T,row.names=NULL,colClasses=c("Freq_Modulation"="factor","Harmonics"="factor","Non.Harmonic_Struc"="factor","Impulsive"="factor","Stochastic"="factor"))
#read.csv('test.csv', colClasses=c("time"="character"))
#This line condenses the raw output of Raven from the Spectrogram and Wavelength windows into one table
# data.frame(table[seq(1,nrow(table),2),c(grep("Selection",names(table)),grep("Begin",names(table)),grep("End",names(table)),grep("Delta",names(table)),grep("Low",names(table)),grep("High",names(table)),grep("Delta.Freq",names(table)),grep("File",names(table)),grep("Note",names(table)),grep("Song",names(table)),grep("Quality",names(table)))],na.omit(table$Peak.Freq..Hz.),na.omit(table[,grep("RMS",colnames(table))]))->table
# data.frame(table[seq(2,nrow(table),2),c(grep("Selection",names(table)),grep("Begin",names(table)),grep("End",names(table)),
# grep("BW.90...Hz.",names(table)),grep("Freq.5...Hz.",names(table)),grep("Freq.95...Hz.",names(table)),
# grep("Time.5...s.",names(table)),grep("Time.95...s.",names(table)),grep("Dur.90...s.",names(table)),
# grep("File",names(table)),grep("Note",names(table)),
# grep("Song",names(table)),grep("Quality",names(table)))],na.omit(table$Peak.Freq..Hz.),
# na.omit(table[,grep("RMS",colnames(table))]))->table
table$recording<-sub("C:/Users/Rusty/Amazon Drive/BOP/Sound/Janelle/", "", (file.names[yy]))
#sub("C:/Users/Rusty/Amazon Drive/BOP/Sound/Janelle/", "", substring(as.character(file.names[yy]),61))
table$species<-species.names[xx]
chuck<-ncol(table)
table<-table[,c(chuck,chuck-1,1:(chuck-2))]
checkindex<-c("Freq_Modulation","Harmonics","Non.Harmonic_Struc","Impulsive","Stochastic")
mini<-table[,c(checkindex)] # mini[4,4]<-NA mini[1,3]<-"01" mini[8,2]<-3
table$hasNAs<-rowSums(is.na(mini))
# summary(table[,c(checkindex)])
# table[,c(checkindex)]<-factor(table[,c(checkindex)])
# table[,c(checkindex)]<-lapply(table[,c(checkindex)], function(x) as.factor((x)))
if(sum(table$hasNAs>0)){
print("NA DETECTED IN:")
print(table[which(table$hasNAs==1),c("species","recording","Selection")])
if(naindex==0){
NaErrors<-table[which(table$hasNAs==1),c("species","recording","Selection")]
} else {
NaErrors<-rbind(NaErrors,table[which(table$hasNAs==1),c("species","recording","Selection")])
}
naindex<-naindex+1
table<-table[-which(table$hasNAs==1),]
}
if(length(which(apply(mini, 1, function(r) any(r != "0" & r !="1"))))>0){
print("ISSUE DETECTED IN:")
print(table[ which(apply(mini, 1, function(r) any(r != 0 & r !=1))),c("species","recording","Selection")])
if(naughtyindex==0){
NaughtyErrors<-table[ which(apply(mini, 1, function(r) any(r != "0" & r !="1"))),c("species","recording","Selection")]
} else {
NaughtyErrors<-rbind(NaughtyErrors,table[ which(apply(mini, 1, function(r) any(r != "0" & r !="1"))),c("species","recording","Selection")])
}
naughtyindex<-naughtyindex+1
table<-table[-which(apply(mini, 1, function(r) any(r != 0 & r !=1))),]
}
subtable<-table[,c("species","recording","Selection",
"Delta.Time..s.",
"Avg.Entropy..bits.",
"Agg.Entropy..bits.",
"BW.90...Hz.",
"Min.Entropy..bits.",
"Max.Entropy..bits.",
"Peak.Freq..Hz.",
"Dur.90...s.",
"Freq.5...Hz.",
"Freq.95...Hz.",
# "Peak.Freq.Contour..Hz.",
"PFC.Avg.Slope..Hz.ms.",
"PFC.Max.Freq..Hz.",
"PFC.Max.Slope..Hz.ms.",
"PFC.Min.Freq..Hz.",
"PFC.Min.Slope..Hz.ms.",
"Freq_Modulation","Harmonics","Non.Harmonic_Struc","Impulsive","Stochastic")]
if (xx==1 & yy==1) {
Mega<-subtable
} else {
Mega<-rbind(Mega,subtable)
}
}
}
#print(NaErrors)
print(NaughtyErrors) #Prints full df of errors with identifying info
#######################################################
Mega$species<-factor(Mega$species)
#Create a custom color scale
library(RColorBrewer)
myColors <- brewer.pal(9,"Set1")
names(myColors) <- levels(Mega$species)
rawData<-Mega[,-c(1:3)]
rawData[checkindex] <- lapply(rawData[checkindex], function(x) as.numeric(as.character(x))) #uses checkindex
summary(rawData)
s.pca<-princomp(rawData)
summary(s.pca)
s.pca$loadings
#summary(s.pca$scores)
plot(s.pca)
print(s.pca)
head(s.pca$X)
library(rgl)
plot3d(s.pca$scores[,1:3], col=myColors[Mega$species])
# apply PCA - scale. = TRUE is highly
# advisable, but default is FALSE.
sound.pca <- prcomp(rawData,center = TRUE,scale. = TRUE)
print(sound.pca)
plot(sound.pca)
summary(sound.pca)
biplot(sound.pca)
head(sound.pca$x)
summary(sound.pca$x)
diff<-apply(sound.pca$x,2,function(x) range(x)[2]-range(x)[1])
diff<-diff[-20]
asds<-apply(sound.pca$x,2,function(x) sd(x))
asds<-asds[-20]
PropVar<-summary(sound.pca)$importance[2,]
PropVar<-PropVar[-20]
plot(diff~PropVar)
plot(asds~PropVar)
plot((asds)~log(PropVar))
plot(log(asds)~(PropVar))
plot(log(asds)~log(PropVar))
summary(lm(log(asds)~log(PropVar)))
#Uses agglomerative hierarchical clustering with a cut-off of 3 JNDs to cluster points in colorspace
dS22<-s.pca$scores[,1:3]
dist.matrix<-dist(dS22)
hc <-fastcluster::hclust(dist.matrix, method="ward.D2", members=NULL) #Expects squared distance values
clusterid<-cutree(hc, h = 15000)
clusters<-as.vector(unique(clusterid))
l.clusters<-length(clusters[clusters !=0])
clusterid2<-cutree(hc, h = 25000)
clusters2<-as.vector(unique(clusterid2))
l.clusters2<-length(clusters2[clusters2 !=0])
Mega$notetype<- clusterid2 #
plot3d(s.pca$scores[,1:3], col=myColors[Mega$species])
plot3d(s.pca$scores[,1:3], col=myColors[Mega$notetype])
cutree.h <- function(tree,h) {
# this line adapted from cutree(...) code
k <- nrow(tree$merge) + 2L - apply(outer(c(hc$height, Inf), h, ">"), 2, which.max)
return(cutree(tree,k=k))
}