-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRFunction.R
More file actions
337 lines (297 loc) · 13.3 KB
/
Copy pathRFunction.R
File metadata and controls
337 lines (297 loc) · 13.3 KB
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
library(move2)
library(dplyr)
library(ggplot2)
library(gridExtra)
library(sf)
library(lutz)
library(rlang)
library(units)
library(lubridate)
library(grid)
# dt <- readRDS("~/Downloads/test__Workflow_Instance_001__Movebank_Location__2026-04-24_22-01-30.rds")
# data <- filter_track_data(dt, .track_id = c("Lina.ABA63..ebos.9679."))
# data <- dt
# names(data)
# summary(data)
# plot_nb_lcs <- T
# bat_attr_prov <- "eobs_battery_voltage"
# bat_attr <- NULL
# plot_fix_rate <- T
# unts_fix_rate <- "min"
# use_local_time <- T
# attr_line <- "eobs_fix_battery_voltage,eobs_speed_accuracy_estimate,eobs_temperature,eobs_used_time_to_get_fix,gpsdop,gpssatellite_count"
# attr_boxplot <- "eobs_fix_battery_voltage,eobs_speed_accuracy_estimate,eobstemperature,eobs_used_timeto_get_fix,gps_dop,gps_satellite_count"
# pdfMode <- "perAttrib" #"perTrack" "perAttrib"
# # pdf_file <- "plots_prt.pdf"
#
#
#
# dtt <- readRDS("~/Downloads/test__Workflow_Instance_002__Movebank_Location__2026-04-26_19-18-22.rds")
# data <- filter_track_data(dtt, .track_id = c("Floreana_131_11964"))
# #data <- dtt
#
# # names(data)
# # summary(data)
#
# plot_nb_lcs <- T
# bat_attr_prov <- "tag_voltage"
# plot_fix_rate <- T
# bat_attr <- NULL #"tag_voltage"#"eobs_battery_voltage"
# unts_fix_rate <- "min"
# use_local_time <- T
# attr_line <- "sigfox_rssi"
# attr_boxplot <- "sigfox_rssi"
# pdfMode <- "perAttrib" #"perTrack" "perAttrib"
# pdf_file <- "plots_per_attribute.pdf"#"plots_per_attribute.pdf" plots_per_track.pdf
# voc <- movebank_get_vocabulary()
# names(voc[grep("voltage",voc)])
#
# "tag_voltage"
# "eobs_battery_voltage"
# "solar_cell_voltage"
#
# "battery_charging_voltage"
# "eobs_fix_battery_voltage"
# "solar_voltage_percent"
# "tag_backup_voltage"
# "tinyfox_sunny_index_start_voltage"
# "tinyfox_sunny_index_voltage_increase"
# "voltage_resolution"
#
#
# names(voc[grep("mV",voc)])
## ToDo plot all tags
# dtt <- readRDS("~/Downloads/test__Workflow_Instance_002__Movebank_Location__2026-04-26_19-18-22.rds")
# dtt$date <- as.Date(mt_time(dtt))
# data_daily <- dtt %>% group_by(mt_track_id()) |> count(date, name = "n_fix") |> sf::st_drop_geometry() %>% rename(TrackId=`mt_track_id()`)
#
# ggplot() +
# geom_path(data = data_daily, aes(x = date, y=n_fix,color=TrackId), linewidth=0.1, alpha=0.8)+
# geom_point(data = data_daily, aes(x = date, y =n_fix, color=TrackId))+
# scale_y_continuous(name = "Number GPS fixes per day") +
# labs(title = "Number of GPS fixes", subtitle = paste("All track"))+
# theme_bw()+
# xlab("")
rFunction <- function(data,
plot_nb_lcs,
bat_attr_prov,
bat_attr,
plot_fix_rate,
unts_fix_rate,
attr_line,
attr_boxplot,
use_local_time,
pdfMode = c("perTrack", "perAttrib")
) {
## check bat attr name
if(bat_attr_prov!="no_selection"){
batvot_ok <- bat_attr_prov[bat_attr_prov %in% names(data)]
batvot_error <- bat_attr_prov[!bat_attr_prov %in% names(data)]
if (length(batvot_error) > 0) {logger.info(paste0("Warning! Your selected voltage attribute: ",'"',batvot_error,'"'," does not exist in the data set. It will not be plotted."))}
}
if(bat_attr_prov == "no_selection" & !is.null(bat_attr)){
batvot_ok <- bat_attr[bat_attr %in% names(data)]
batvot_error <- bat_attr[!bat_attr %in% names(data)]
if (length(batvot_error) > 0) {logger.info(paste0("Warning! Your provided voltage attribute: ",'"',batvot_error,'"'," does not exist in the data set. It will not be plotted."))}
}
if(bat_attr_prov == "no_selection" & is.null(bat_attr)){
batvot_ok <- NULL
}
## check line attr name
if(!is.null(attr_line)){
attr_line_L <- strsplit(attr_line, ",")[[1]]
attr_line_L <- gsub(" ", "", attr_line_L, fixed = TRUE)
attr_line_ok <- attr_line_L[attr_line_L %in% names(data)]
attr_line_error <- attr_line_L[!attr_line_L %in% names(data)]
if (length(attr_line_error) > 0) {logger.info(paste0("Warning! Your defined attributes: ",paste0('"',attr_line_error,'"', collapse = ", ")," do not exist in the data set. They will not be plotted."))}
}else{attr_line_ok <- NULL}
## check boxplot attr name
if(!is.null(attr_boxplot)){
attr_boxplot_L <- strsplit(attr_boxplot, ",")[[1]]
attr_boxplot_L <- gsub(" ", "", attr_boxplot_L, fixed = TRUE)
attr_boxplot_ok <- attr_boxplot_L[attr_boxplot_L %in% names(data)]
attr_boxplot_error <- attr_boxplot_L[!attr_boxplot_L %in% names(data)]
if (length(attr_boxplot_error) > 0) {logger.info(paste0("Warning! Your defined attributes: ",paste0('"',attr_boxplot_error,'"', collapse = ", ")," do not exist in the data set. They will not be plotted."))}
}else{attr_boxplot_ok <- NULL}
data_L <- split(data, mt_track_id(data))
# helper to build all plots for one track, return as named list
make_track_plots <- function(trk, id) {
if (use_local_time) {
coords <- st_coordinates(trk)
timezns <- tz_lookup_coords(lat = coords[, "Y"], lon = coords[, "X"], method = "accurate")
most_frequent_tz <- names(which.max(table(timezns)))
trk$timestamp_local <- with_tz(mt_time(trk), tzone = most_frequent_tz)
if (length(unique(timezns)) != 1) {logger.warn(paste0("There are multiple local timezones present in the track. ","The most frequent local timezone present is used: ", most_frequent_tz))}
mt_time(trk) <- "timestamp_local"
}
trk$date <- as.Date(mt_time(trk))
data_daily <- trk |> count(date, name = "n_fix") |> sf::st_drop_geometry()
if(plot_nb_lcs){
## nb_volt
if (length(batvot_ok) == 1) {
if(length(trk[[batvot_ok]][is.na(trk[[batvot_ok]])])==length(trk[[batvot_ok]]))
batvot_ok <- NULL
}
if (length(batvot_ok) == 1) {
bat_units <- units(trk[[batvot_ok]])
bat_label <- paste("Battery voltage", ifelse(is.null(bat_units), "", paste0("(", bat_units, ")")))
# ranges
bat_max <- max(as.numeric(trk[[batvot_ok]]), na.rm = TRUE)
y_max <- max(data_daily$n_fix, na.rm = TRUE) # or another chosen max
bat_min <- min(as.numeric(trk[[batvot_ok]]), na.rm = TRUE)
# if(bat_max<10000){bat_min <- 2000} else if(bat_max<10){bat_min <- 2}else{bat_min <- 0}
# map: primary = a + b * battery
b <- y_max / (bat_max - bat_min)
a <- -b * bat_min
color_voltage <- "limegreen"
nb_volt <- ggplot() +
geom_point(data = trk, aes(x = mt_time(trk), y = a + b * as.numeric(.data[[batvot_ok]])), shape = 20, size=0, colour = "white") + ## workaround to get both plotted on the same xaxis
geom_path(data = trk, aes(x = mt_time(trk)-hours(12), y = a + b * as.numeric(.data[[batvot_ok]])), colour = color_voltage, linewidth=0.1, alpha=0.8) +
geom_point(data = trk, aes(x = mt_time(trk)-hours(12), y = a + b * as.numeric(.data[[batvot_ok]])), shape = 4, colour = color_voltage) +
geom_path(data = data_daily, aes(x = date, y=n_fix), linewidth=0.1, alpha=0.8)+
geom_point(data = data_daily, aes(x = date, y =n_fix))+
scale_y_continuous(name = "Number GPS fixes per day", limits = c(0, y_max), sec.axis = sec_axis(transform = ~ ( . - a ) / b, name = bat_label)) +
labs(title = paste0("Number of GPS fixes per day and ",batvot_ok), subtitle = paste("Track: ", id))+
theme_bw()+
theme(axis.title.y.right = element_text(colour = color_voltage), axis.text.y.right=element_text(colour = color_voltage))+
xlab("")
} else {
nb_volt <- ggplot() +
geom_path(data = data_daily, aes(x = date, y=n_fix), linewidth=0.1, alpha=0.8)+
geom_point(data = data_daily, aes(x = date, y =n_fix))+
scale_y_continuous(name = "Number GPS fixes per day") +
labs(title = "Number of GPS fixes", subtitle = paste("Track: ", id))+
theme_bw()+
xlab("")
}
}else{nb_volt <- NULL}
## fix rate
if(plot_fix_rate){
fixrt <- ggplot(trk) +
geom_boxplot(aes(x = date, y = mt_time_lags(trk, unts_fix_rate), group = date), outliers = FALSE, na.rm = TRUE) +
theme_bw() +
labs(title = "Fix rate (approx)", subtitle = paste("Track: ", id))+
xlab("") +
ylab("")
}else{fixrt <- NULL}
## other attr lines
if(!is.null(attr_line_ok)){
ggtrk_all_ls <- lapply(seq_along(attr_line_ok), function(i) {
atr <- attr_line_ok[i]
ggplot(trk) +
geom_line(aes(x = mt_time(trk), y = !!sym(atr)), linewidth=0.1, alpha=0.5 , show.legend = TRUE) +
geom_point(aes(x = mt_time(trk), y = !!sym(atr)), show.legend = TRUE) +
labs(title = atr, subtitle = paste("Track: ", id))+
xlab("") +
ylab("") +
theme_bw()
})
names(ggtrk_all_ls) <- paste0(attr_line_ok, "_line")
}else{ggtrk_all_ls <- NULL}
## other attr boxplot
if(!is.null(attr_boxplot_ok)){
ggtrk_all_bx <- lapply(seq_along(attr_boxplot_ok), function(i) {
atr <- attr_boxplot_ok[i]
ggplot(trk) +
geom_boxplot(aes(x = date, y =!!sym(atr), group = date), outliers = FALSE, na.rm = TRUE) +
labs(title = atr, subtitle = paste("Track: ", id))+
xlab("") +
ylab("") +
theme_bw()
})
names(ggtrk_all_bx) <- paste0(attr_boxplot_ok, "_box")
}else{ggtrk_all_bx <- NULL}
# return as a named list
c(list(nb_volt = nb_volt, fixrt = fixrt), ggtrk_all_ls, ggtrk_all_bx)
}
# lapply over tracks
track_plots_list <- lapply(names(data_L), function(id) {
trk <- data_L[[id]]
make_track_plots(trk, id)
})
names(track_plots_list) <- names(data_L)
# remove NULL entries for tracks without plots, and plots set to FALSE
track_plots_list <- Filter(Negate(is.null), track_plots_list)
track_plots_list <- lapply(track_plots_list, function(x){if (is.list(x)) {Filter(Negate(is.null), x)} else {x}})
## add study name to top of pdf
study_name <- as.character(unique(mt_track_data(data)$name))
add_study_header <- function(page_grob, study_name) {
grid.arrange(
textGrob(
label = paste("Study: ", study_name),
x = 0.01, y = 0.99, just = c("left", "top"),
gp = gpar(cex = 1.2, fontface = "bold")
),
page_grob,
ncol = 1,
heights = c(0.08, 0.92)
)
}
study_name_valid <- gsub("[^A-Za-z0-9]+", "_", study_name)
study_name_valid <- gsub("_+", "_", study_name_valid)
# --- PDF creation logic ---------------------------------------------------
if (pdfMode == "perTrack") {
# lm_rowwise <- matrix(1:(2 * 3), nrow = 2, ncol = 3, byrow = TRUE) # create matrix to fill by row and not default by column
# one multi-page PDF; per track, plots are consecutive, 2x3 grid
all_pages1 <- lapply(names(track_plots_list), function(id) {
# list of grobs for this track, in fixed order:
# nb_volt, fixrt, then attributes
grobs_id <- track_plots_list[[id]]
# marrangeGrob will paginate if more than 6 for a track
marrangeGrob(grobs = grobs_id, nrow = 2, ncol = 3)#, layout_matrix = lm_rowwise)
})
# prepend title page
all_pages <- do.call(c, all_pages1)
pages_with_header <- lapply(all_pages, add_study_header, study_name = study_name)
final_pages <- marrangeGrob(
grobs = pages_with_header,
nrow = 1,
ncol = 1
)
ggsave(appArtifactPath(paste0(study_name_valid, "__tag_diagnostics_plots_by_Track.pdf")), final_pages, width = 21, height = 10)
} else if (pdfMode == "perAttrib") {
# collect per-attribute (including nb_volt, fixrt) across tracks
# 1) named vector of attribute keys per plot list
# nb_volt and fixrt are always the first two names
# any extra names are attributes
# build a long list of plots with tags
# lm_rowwise <- matrix(1:(2 * 3), nrow = 2, ncol = 3, byrow = TRUE) # create matrix to fill by row and not default by column
plot_long <- list()
for (id in names(track_plots_list)) {
pl <- track_plots_list[[id]]
for (nm in names(pl)) {
key <- nm # "nb_volt", "fixrt", or attribute name
plot_long[[length(plot_long) + 1]] <-
list(key = key, id = id, grob = pl[[nm]])
}
}
# unique keys to iterate over
keys <- unique(vapply(plot_long, `[[`, character(1), "key"))
# one list element per key; each element is either a list of pages or NULL
pages_by_key <- lapply(keys, function(k) {
grobs_k <- lapply(plot_long, function(x) {
if (x$key == k) x$grob else NULL
})
grobs_k <- Filter(Negate(is.null), grobs_k)
if (length(grobs_k) == 0) {
return(NULL)
}
marrangeGrob(grobs = grobs_k, nrow = 2, ncol = 3)#, layout_matrix = lm_rowwise)
})
# drop NULL keys
pages_by_key <- Filter(Negate(is.null), pages_by_key)
all_pages <- do.call(c,pages_by_key)
if (length(all_pages) > 0) {
pages_with_header <- lapply(all_pages, add_study_header, study_name = study_name)
final_pages <- marrangeGrob(
grobs = pages_with_header,
nrow = 1,
ncol = 1
)
ggsave(appArtifactPath(paste0(study_name_valid, "__tag_diagnostics_plots_by_Attribute.pdf")), final_pages, width = 21, height = 10)
} else {
warning("No plots available for perAttrib pdfMode.")
}
}
return(data)
}