-
Notifications
You must be signed in to change notification settings - Fork 2
/
D2_HmdCondition.R
406 lines (352 loc) · 15.6 KB
/
D2_HmdCondition.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
rm(list=ls())
# LIBRARIES ####
library(data.table)
library(ggplot2)
library(lubridate)
library(dplyr)
library(viridis)
library(tidyverse)
library(rsvd)
library(rlang)
# Custom function to calculate median and standard error
custom_fun <- function(x) {
n <- length(x)
c(median = median(x), se = sd(x) / sqrt(n))
}
quantile_25_75 <- function(x) {
quantile(x, probs = c(0.25, 0.75))
}
# GET DATA ####
siteN = "CaseStudy2"
siteN = "AU_CH01-all"
siteN = "NRS01"
gdrive = "F:\\SoundCoop\\hmd_downloadedGCP\\"
dirIn = paste0( gdrive, siteN )
filepat = "_RrpcaResults_"# "_HmdLabels_LF_"
inFilesModel = list.files( dirIn, pattern = filepat, recursive = F, full.names = T )
filepat ="_Hmd_LF_"# _HmdLabels_LF_"
inFilesData = list.files( dirIn, pattern = filepat, recursive = F, full.names = T )
dirOut = dirIn
voi= "mth" #variable of interst
moi = 10
moiN = "October"
Rout = NULL
Rmth= NULL
for (ii in 1: length (inFilesModel) ){
#original data
split_string <- strsplit(basename( inFilesData[ii]), "_")[[1]]
site <- paste0(split_string[1],split_string[2])
cat("Running..", site, ii, " of ",length (inFilesModel) )
load( inFilesData[ii] )
HmdTrim$mth = month(HmdTrim$dateTime)
HmdDets = HmdTrim
idNA = ( which(is.na(HmdDets)))
stp = which( names( HmdDets) == "1001.2")
HmdDets2 = HmdDets[,1:stp]
numeric_columns <- grep("^\\d", names( HmdDets2) )
hix = names( HmdDets2)[numeric_columns]
Nv = HmdDets2[, numeric_columns] #dB values
#low rank model
load( inFilesModel[ii])
Lr = as.data.frame(nvpcaTOL$L)
colnames(Lr) = hix
LrDB = 10*log10( Lr^2 ) #CHECK: median(LrDB$'100'), no negative values, just values without transients
colnames(LrDB) = hix
## RRPCA thresholds ####
# sum of difference across frequencies for each minute
LRdiff = as.data.frame ( rowSums( abs ( (LrDB - Nv) ) ) )
colnames(LRdiff) = 'LRdiff'
# min(LRdiff$LRdiff)
# which frequency had the max LF diff
LRfq = as.data.frame ( as.numeric ( colnames(LrDB) [apply(LrDB, 1, (which.max) )] ) )
colnames(LRfq) = 'LRfq'
# median( LRfq$LRfq )
## LABEL HMD files ####
HmdDets$LowRanK = as.numeric( as.character(LRdiff$LRdiff ) )
### scatter by month (time of interest) RRPCA values (save plot)####
HmdDets$Day = as.Date(HmdDets$dateTime)
HmdDets$mth = month(HmdDets$dateTime)
result_dplyr <- HmdDets %>%
group_by(Day, !!sym(voi)) %>%
summarise(
LowRankDiff = mean(LowRanK, na.rm = TRUE),
se = sd(LowRanK, na.rm = TRUE) / sqrt(n())
)
p = ggplot(data = result_dplyr, aes(x = Day, y = LowRankDiff, color = as.factor(!!sym(voi)))) +
geom_point(size = 1) +
geom_errorbar(aes(ymin = LowRankDiff - se, ymax = LowRankDiff + se), width = 0.2) +
ggtitle(paste0(site, " Daily Low-Frequency Residual Soundscape Condition")) +
xlab("")+ ylab("Summed difference \n low-rank and origional data") +
labs(subtitle = "Higher values indicate more transient sounds present", color = "Month") +
theme_minimal() +
#ylim(c(50,95)) +
theme(text = element_text(size = 15),
plot.subtitle = element_text(face = "italic"),
plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
axis.title.x = element_text(margin = margin(t = 10)),
axis.title.y = element_text(margin = margin(r = 10) ) )
ggsave(
filename = paste0(dirOut, "\\", site, "DailyLow-FrequencyResidual.png"),
plot = p,
width = 6, height = 4, units = "in",
bg = "white"
)
### Spectra by month (time of interest) (save plot) ####
LrDB$Day = HmdDets$Day
LrDB$mth = HmdDets$mth
LrDB$site = HmdDets$site
RSoundscape = aggregate( LrDB[,hix], by = list(Season = get(voi, LrDB)), FUN = (median) )
RSoundscape25 = aggregate(
LrDB[, hix],
by = list(Season = get(voi, LrDB)),
FUN = quantile,
probs = 0.25
)
RSoundscape75 = aggregate(
LrDB[, hix],
by = list(Season = get(voi, LrDB)),
FUN = quantile,
probs = 0.75
)
RSoundscape = as.data.frame(RSoundscape)
melted_df50 = reshape2::melt(RSoundscape, id.vars = c("Season"), measure.vars = hix )
melted_df25 = reshape2::melt(RSoundscape25, id.vars = c("Season"), measure.vars = hix )
melted_df75 = reshape2::melt(RSoundscape75, id.vars = c("Season"), measure.vars = hix )
melted_df50$Fq = as.numeric(as.character(melted_df50$variable))
melted_df25$Fq = as.numeric(as.character(melted_df25$variable))
melted_df75$Fq = as.numeric(as.character(melted_df75$variable))
p2 = ggplot()+
geom_line(data = melted_df50, aes(x = Fq, y = value, color = as.factor(Season), group = as.factor(Season)), linewidth = 1 ) +
geom_line(data = melted_df25, aes(x = Fq, y = value, color = as.factor(Season), group = as.factor(Season)), linewidth = .3, alpha = .5, linetype = "dashed") +
geom_line(data = melted_df75, aes(x = Fq, y = value, color = as.factor(Season), group = as.factor(Season)), linewidth = .3, alpha = .5, linetype = "dashed" ) +
scale_x_log10() +
#facet_wrap(~Season)+
labs(subtitle = "Low-rank representation of sound levels", color = "Month") +
theme_minimal() +
ylim(c(50,95))+
theme(
text = element_text(size = 15), # Set all text to size 16
plot.subtitle = element_text(face = "italic"),
#axis.title = element_text(size = 12), # Axis titles
#axis.text = element_text(size = 12), # Axis labels
legend.text = element_text(size = 12), # Legend text
#strip.text = element_text(size = 12) # Facet labels
) +
labs(
title = paste0(site, " Residual Soundscape Condition"),
x = "Frequency (Hz)",
y =expression(paste("Residual Sound Pressure Level (dB re: 1", mu, "Pa)"))
)
p2
ggsave(
filename = paste0(dirOut, "\\", site, "Low-FrequencyResidualSPL.png"),
plot = p2,
width = 6, height = 4, units = "in",
bg = "white"
)
p3 = ggplot()+
geom_line(data = melted_df50, aes(x = Fq, y = value), linewidth = 1 ) +
geom_line(data = melted_df25, aes(x = Fq, y = value), linewidth = .3, alpha = .5, linetype = "dashed") +
geom_line(data = melted_df75, aes(x = Fq, y = value), linewidth = .3, alpha = .5, linetype = "dashed" ) +
scale_x_log10() + facet_wrap(~Season)+
labs(subtitle = "Low-rank representation of sound levels", color = "Month") +
theme_minimal() + ylim(c(50,95))+
theme( text = element_text(size = 15), # Set all text to size 16
plot.subtitle = element_text(face = "italic") ) +
labs( title = paste0(site, " Residual Soundscape Condition"),
x = "Frequency (Hz)",
y =expression(paste("Residual Sound Pressure Level (dB re: 1", mu, "Pa)")) )
p3
ggsave(
filename = paste0(dirOut, "\\", site, "Low-FrequencyResidualSPL_mth.png"),
plot = p3,
width = 6, height = 4, units = "in",
bg = "white"
)
### Spectra site (save data) ####
RSoundscapeT = apply(LrDB[,hix], 2, quantile, probs = c(0, 0.25, 0.5, 0.75, 1))
RSoundscapeT = as.data.frame( RSoundscapeT )
RSoundscapeT$Site = site
RSoundscapeT$minutes = nrow(LrDB)
Rout = rbind(Rout, RSoundscapeT)
colnames(RSoundscapeT)
### Spectra site (save data) ####
colnames(LrDB)
LrDBT = LrDB[LrDB$mth == moi,]
RSoundscapeM = apply(LrDBT[,hix], 2, quantile, probs = c(0, 0.25, 0.5, 0.75, 1))
RSoundscapeM = as.data.frame( RSoundscapeM )
RSoundscapeM$Site = site
RSoundscapeM$mth = moi
RSoundscapeM$minutes = nrow(LrDBT)
colnames(RSoundscapeM)
Rmth = rbind(Rmth, RSoundscapeM)
}
DC = Sys.Date()
save(Rout, file = paste0(dirOut, "\\", siteN, "_RRPCA", "_", DC, ".Rda") )
save(Rmth, file = paste0(dirOut, "\\", siteN, "_RRPCA-", moi, "_", DC, ".Rda") )
# PLOT ALL SITES/years ####
Rout = as.data.frame(Rout)
rownames(Rout)
library(tibble)
Rout = rownames_to_column(Rout, var = "quantiles")
colnames(Rout)
melted_Rout = reshape2::melt(Rout, id.vars = c("Site", "quantiles"), measure.vars = hix )
melted_Rout$Fq = as.numeric(as.character(melted_Rout$variable))
melted_Rout$Quant= as.factor(as.character(sapply(strsplit(melted_Rout$quantiles, "%"), `[`, 1) ))
melted_RoutT= melted_Rout %>% filter(!Quant %in% c("0", "100", "25", "75"))
ggplot() +
geom_line(data = melted_RoutT, aes(x = Fq, y = value, group = interaction(Site, Quant),
color = Site, linetype = Quant), linewidth = 1) +
labs(subtitle = "Low-rank representation of sound levels") +
theme_minimal() + ylim(c(55,80))+
theme( text = element_text(size = 15), # Set all text to size 16
plot.subtitle = element_text(face = "italic") ) +
labs( title = "Residual Soundscape Condition",
x = "Frequency (Hz)",
y =expression(paste("Residual Sound Pressure Level (dB re: 1", mu, "Pa)")) )
# CHO1- modify plot ####
# select separate decades
colnames(Rout)
#original data
Rout$yr <- as.numeric(as.character( sapply(strsplit(Rout$Site, "-"), function(x) x[3])))
melted_Rout = reshape2::melt(Rout, id.vars = c("Site", "quantiles","yr"), measure.vars = hix )
melted_Rout$Fq = as.numeric(as.character(melted_Rout$variable))
melted_Rout$Quant= as.factor(as.character(sapply(strsplit(melted_Rout$quantiles, "%"), `[`, 1) ))
melted_RoutT= melted_Rout %>% filter(!Quant %in% c("0", "100", "25", "75"))
# Filter data for each decade
melted_RoutT$decade <- "2000"
melted_RoutT$decade[melted_RoutT$yr > 2012] <- "2010"
data_2000 <- melted_RoutT[melted_RoutT$decade == "2000", ]
data_2010 <- melted_RoutT[melted_RoutT$decade == "2010", ]
# Plot for 2000s decade
plot_2000 <- ggplot(data_2000) +
geom_line(aes(x = Fq, y = value, group = interaction(Site, Quant),
color = Site, linetype = Quant), linewidth = 1) +
labs(subtitle = "2000s: Low-rank representation of sound levels") +
theme_minimal() +
ylim(c(55, 85)) +
theme(text = element_text(size = 15),
plot.subtitle = element_text(face = "italic")) +
labs(title = "Residual Soundscape Condition (2000s)",
x = "Frequency (Hz)",
y = expression(paste("Residual Sound Pressure Level (dB re: 1", mu, "Pa)")))
# Plot for 2010s decade
plot_2010 <- ggplot(data_2010) +
geom_line(aes(x = Fq, y = value, group = interaction(Site, Quant),
color = Site, linetype = Quant), linewidth = 1) +
labs(subtitle = "2010s: Low-rank representation of sound levels") +
theme_minimal() +
ylim(c(55, 85)) +
theme(text = element_text(size = 15),
plot.subtitle = element_text(face = "italic")) +
labs(title = "Residual Soundscape Condition (2010s)",
x = "Frequency (Hz)",
y = expression(paste("Residual Sound Pressure Level (dB re: 1", mu, "Pa)")))
# Arrange plots side by side or on top of each other
library(gridExtra)
grid.arrange(plot_2000, plot_2010, ncol = 1)
plot_2000
# CHO1- modify plot- Rmth ####
# select separate decades for just October results
Rmth = rownames_to_column(Rmth, var = "quantiles")
Rmth$yr <- as.numeric(as.character( sapply(strsplit(Rmth$Site, "-"), function(x) x[3])))
melted_Rout = reshape2::melt(Rmth, id.vars = c("Site", "quantiles","yr"), measure.vars = hix )
melted_Rout$Fq = as.numeric(as.character(melted_Rout$variable))
melted_Rout$Quant= as.factor(as.character(sapply(strsplit(melted_Rout$quantiles, "%"), `[`, 1) ))
melted_RoutT= melted_Rout %>% filter(!Quant %in% c("0", "100", "25", "75"))
# Filter data for each decade
melted_RoutT$decade <- "2000"
melted_RoutT$decade[melted_RoutT$yr > 2012] <- "2010"
data_2000 <- melted_RoutT[melted_RoutT$decade == "2000", ]
data_2010 <- melted_RoutT[melted_RoutT$decade == "2010", ]
# Plot for 2000s decade
ldata <- data.frame(
x1 = c(94, 94, 94, 94, 94),
y1 = data_2000$value[data_2000$Fq == 100],
lb = data_2000$yr[data_2000$Fq == 100]
)
plot_2000 <- ggplot() +
geom_line(data = data_2000, aes(x = Fq, y = value, group = interaction(Site, Quant),
color = Site, linetype = Quant), linewidth = 1) +
geom_text(data = ldata, aes(x = x1, y = y1, label = lb),size = 2, fontface = "bold") +
theme_minimal() +
ylim(c(55, 95)) +
theme(text = element_text(size = 15),
plot.subtitle = element_text(face = "italic")) +
labs(subtitle = paste0("2000s: Low-rank representation of sound levels-", moiN)) +
labs(title = "Residual Soundscape Condition (2000s)",
x = "Frequency (Hz)",
y = expression(paste("Residual Sound Pressure Level (dB re: 1", mu, "Pa)")))
# Plot for 2010s decade
ldata <- data.frame(
x1 = c(94, 94, 94, 94, 94),
y1 = data_2010$value[data_2010$Fq == 100],
lb = data_2010$yr[data_2010$Fq == 100]
)
plot_2010 <- ggplot() +
geom_line(data = data_2010, aes(x = Fq, y = value, group = interaction(Site, Quant),
color = Site, linetype = Quant), linewidth = 1) +
geom_text(data = ldata, aes(x = x1, y = y1, label = lb),size = 2, fontface = "bold") +
labs(subtitle = paste0("2010s: Low-rank representation of sound levels-", moiN)) +
theme_minimal() +
ylim(c(55, 95)) +
theme(text = element_text(size = 15),
plot.subtitle = element_text(face = "italic")) +
labs(title = "Residual Soundscape Condition (2010s)",
x = "Frequency (Hz)",
y = expression(paste("Residual Sound Pressure Level (dB re: 1", mu, "Pa)")))
# Arrange plots side by side or on top of each other
library(gridExtra)
grid.arrange(plot_2000, plot_2010, ncol = 1)
# NRS01- modify plot- Rmth ####
# select separate decades for just October results
Rmth = rownames_to_column(Rmth, var = "quantiles")
Rmth$yr <- as.numeric(as.character( sapply(strsplit(Rmth$Site, "-"), function(x) x[3])))
melted_Rout = reshape2::melt(Rmth, id.vars = c("Site", "quantiles","yr"), measure.vars = hix )
melted_Rout$Fq = as.numeric(as.character(melted_Rout$variable))
melted_Rout$Quant= as.factor(as.character(sapply(strsplit(melted_Rout$quantiles, "%"), `[`, 1) ))
melted_RoutT= melted_Rout %>% filter(!Quant %in% c("0", "100", "25", "75"))
# Filter data for each decade
melted_RoutT$decade <- "2000"
melted_RoutT$decade[melted_RoutT$yr > 2012] <- "2010"
data_2000 <- melted_RoutT[melted_RoutT$decade == "2000", ]
data_2010 <- melted_RoutT[melted_RoutT$decade == "2010", ]
# Plot for 2000s decade
ldata <- data.frame(
x1 = c(94, 94, 94, 94, 94),
y1 = data_2000$value[data_2000$Fq == 100],
lb = data_2000$yr[data_2000$Fq == 100]
)
plot_2000 <- ggplot() +
geom_line(data = data_2000, aes(x = Fq, y = value, group = interaction(Site, Quant),
color = Site, linetype = Quant), linewidth = 1) +
geom_text(data = ldata, aes(x = x1, y = y1, label = lb),size = 2, fontface = "bold") +
theme_minimal() +
ylim(c(55, 95)) +
theme(text = element_text(size = 15),
plot.subtitle = element_text(face = "italic")) +
labs(subtitle = paste0("2000s: Low-rank representation of sound levels-", moiN)) +
labs(title = "Residual Soundscape Condition (2000s)",
x = "Frequency (Hz)",
y = expression(paste("Residual Sound Pressure Level (dB re: 1", mu, "Pa)")))
# Plot for 2010s decade
ldata <- data.frame(
x1 = c(94, 94, 94, 94, 94),
y1 = data_2010$value[data_2010$Fq == 100],
lb = data_2010$yr[data_2010$Fq == 100]
)
plot_2010 <- ggplot() +
geom_line(data = data_2010, aes(x = Fq, y = value, group = interaction(Site, Quant),
color = Site, linetype = Quant), linewidth = 1) +
geom_text(data = ldata, aes(x = x1, y = y1, label = lb),size = 2, fontface = "bold") +
labs(subtitle = paste0("2010s: Low-rank representation of sound levels-", moiN)) +
theme_minimal() +
ylim(c(55, 95)) +
theme(text = element_text(size = 15),
plot.subtitle = element_text(face = "italic")) +
labs(title = "Residual Soundscape Condition (2010s)",
x = "Frequency (Hz)",
y = expression(paste("Residual Sound Pressure Level (dB re: 1", mu, "Pa)")))
# Arrange plots side by side or on top of each other
library(gridExtra)
grid.arrange(plot_2000, plot_2010, ncol = 1)