|
1 | | -# add 0 if same lek and 1 if else |
2 | | -out <- lapply(1:(length(labels) - 1), function(i){ |
3 | | -sapply((i + 1):length(labels), function(j) |
4 | | -if (labels[i] == labels[j]) 0 else 1) |
5 | | -}) |
6 | | -# add to mat |
7 | | -mat[lower.tri(mat)] <- unlist(out) |
8 | | -# retunr as distance matrix |
9 | | -return(as.dist(mat)) |
10 | | -} |
11 | | -plot_detection <- function(reference, detection, lwd = 90, sep = 1, labels = detection$selec){ |
12 | | -if (!is.null(detection$detection.class)){ |
13 | | -labels <- ifelse(detection$detection.class == "true.positive", "\nTP", ifelse(detection$detection.class == "false.positive", "\nFP", ifelse(detection$detection.class == "false.negative", "\nFN", ifelse(detection$detection.class == "true.positive (split)", "\nTPs", ifelse(detection$detection.class == "true.positive (merged)", "\nTPm", ifelse(detection$detection.class == "negative (split)", "\nNs", ifelse(detection$detection.class == "negative (merged)", "\nNm", ifelse(detection$detection.class == "negative (split/merged)", "\nNsm", "\nTPsm")))))))) |
14 | | -detection$labels <- paste0(detection$selec, labels) |
15 | | -} else detection$labels <- detection$selec |
16 | | -old_par <- par() |
17 | | -old_par <- old_par[!names(old_par) %in% c("cin", "cra", "csi", "cxy", "din", "page")] |
18 | | -on.exit(par(old_par)) |
19 | | -par(mar = rep(0, 4)) |
20 | | -n_sound.files <- length(unique(c(reference$sound.files, detection$sound.files))) |
21 | | -if (n_sound.files > 1) |
22 | | -par(mfrow = c(n_sound.files, 1)) |
23 | | -for (e in unique(c(detection$sound.files, reference$sound.files))){ |
24 | | -sub_detec <- detection[detection$sound.files == e, ] |
25 | | -sub_ref <- reference[reference$sound.files == e, ] |
26 | | -plot(x = range(c(sub_detec$start, sub_detec$end, sub_ref$start, sub_ref$end)), y = c(-1, 1), col = "white", pch = 21, yaxt = "n", ylab = "", xaxt = "n", xlab = "") |
27 | | -# plot references first |
28 | | -if (nrow(sub_ref) > 0) |
29 | | -for (i in 1:nrow(sub_ref)){ |
30 | | -segments( |
31 | | -x0 = sub_ref$start[i], |
32 | | -x1 = sub_ref$end[i], |
33 | | -y0 = sep / -2, |
34 | | -y1 = sep / -2, |
35 | | -lwd = lwd, |
36 | | -col = viridis::magma(10, alpha = 0.4)[3], |
37 | | -lend = 1 |
38 | | -) |
39 | | -text(x = (sub_ref$end[i] + sub_ref$start[i]) / 2, y = sep / -2, labels = i, col = "white") |
40 | | -} |
41 | | -# add detections |
42 | | -if (nrow(sub_detec) > 0) |
43 | | -for (i in 1:nrow(sub_detec)){ |
44 | | -segments( |
45 | | -x0 = sub_detec$start[i], |
46 | | -x1 = sub_detec$end[i], |
47 | | -y0 = sep / 2, |
48 | | -y1 = sep / 2, |
49 | | -lwd = lwd, |
50 | | -col = viridis::magma(10, alpha = 0.4)[7], |
51 | | -lend = 1 |
52 | | -) |
53 | | -text(x = (sub_detec$end[i] + sub_detec$start[i]) / 2, y = sep / 2, labels = sub_detec$labels[i]) |
54 | | -} |
55 | | -} |
56 | | -} |
57 | | -### simulate annotation tables |
58 | | -# cannot have duration shorter than 2 |
59 | | -sim_annotation <- function(n = 10, n.sound.files = 1){ |
60 | | -sims_l <- lapply(1:n.sound.files, function(x){ |
61 | | -possible_start <- seq(0, 98, 2) |
62 | | -sim_ref <- data.frame(sound.files = paste0(x, ".wav"), selec = 1:(n + 1), start = sort(c(sample(possible_start, n), 100))) |
63 | | -sim_ref$end <- NA |
64 | | -for(i in 1:(n)) |
65 | | -sim_ref$end[i] <- sim_ref$start[i] + round(runif(n = 1, min = 1, max = sim_ref$start[i + 1] - sim_ref$start[i] - 0.5)) |
66 | | -sim_ref <- sim_ref[-nrow(sim_ref), ] |
67 | | -return(sim_ref) |
68 | | -}) |
69 | | -sims <- do.call(rbind, sims_l) |
70 | | -return(sims) |
71 | | -} |
72 | | -run.all() |
73 | | -df1 <- query_gbif( |
74 | | -species = 'Phaethornis anthophilus', |
75 | | -all_data = FALSE, |
76 | | -format = "image" |
77 | | -) |
78 | | -df2 <- df1[df1$key %in% c("5063794756", "5077057045"), ] |
79 | | -sxc1 <- df2[!duplicated(df2$key), ][1:2, ] |
80 | | -a <- download_media(metadata = sxc1, path = tempdir()) |
81 | | -fls <- list.files(path = tempdir(), pattern = "jpeg$") |
82 | | -a |
83 | | -fls <- list.files(path = tempdir(), pattern = "jpeg$") |
84 | | -library(warbleR) |
85 | | -open_wd(tempdir()) |
86 | | -a |
87 | | -devtools::document() |
88 | | -help("download_media") |
89 | | -all( |
90 | | -c("download_status", "downloaded_file_name", "file_size") %in% names(a) |
91 | | -) |
92 | | -# remove filess |
93 | | -unlink(file.path(tempdir(), a$downloaded_file_name)) |
94 | | -a <- download_media(metadata = sxc1, path = tempdir(), folder_by = "country") |
95 | | -a |
96 | 1 | source("~/Dropbox/R_package_testing/suwo/testing/update_citation.R") |
97 | 2 | if (!nzchar(Sys.getenv("xc_api_key"))) |
98 | 3 | Sys.setenv(xc_api_key = getPass::getPass()) |
@@ -440,73 +345,168 @@ species = 'Glaucis dohrnii', |
440 | 345 | format = "sound", |
441 | 346 | path = tempdir() |
442 | 347 | ) |
443 | | -library(hexSticker) |
444 | | -install.packages("hexSticker") |
445 | | -library(hexSticker) |
446 | | -sticker( |
447 | | -subplot = "./testing/sticker_testing/logo_v3.png", # your image |
448 | | -package = "suwo", |
449 | | -s_x = 1, |
450 | | -s_y = 0.9, |
451 | | -s_width = 1, |
452 | | -p_x = 1, |
453 | | -p_y = 0.15, |
454 | | -h_fill = "white", |
455 | | -h_color = "black", |
456 | | -p_color = "black", |
457 | | -p_size = 18, |
458 | | -filename = "suwo_sticker.png" |
| 348 | +library(devtools) |
| 349 | +load_all() |
| 350 | +wa <- query_wikiaves("Procnias verano") |
| 351 | +species <- "Procnias verano" |
| 352 | +format = getOption("suwo_format", c("image", "sound")) |
| 353 | +cores = getOption("suwo_cores", 1) |
| 354 | +pb = getOption("suwo_pb", TRUE) |
| 355 | +verbose = getOption("suwo_verbose", TRUE) |
| 356 | +all_data = getOption("suwo_all_data", FALSE) |
| 357 | +raw_data = getOption("suwo_raw_data", FALSE) |
| 358 | +!.checkconnection(verb = verbose, service = "wikiaves") |
| 359 | +source("~/Dropbox/R_package_testing/suwo/R/query_wikiaves.R") |
| 360 | +wa <- query_wikiaves("Procnias verano") |
| 361 | +# assign a value to format |
| 362 | +format <- rlang::arg_match(format, values = c("image", "sound")) |
| 363 | +wiki_format <- switch(format, sound = "s", image = "f") |
| 364 | +# initialize search with user agent |
| 365 | +request_obj <- httr2::request( |
| 366 | +"https://www.wikiaves.com.br/getTaxonsJSON.php" |
| 367 | +) |
| 368 | +request_obj <- httr2::req_url_query( |
| 369 | +request_obj, |
| 370 | +term = species |
| 371 | +) |
| 372 | +request_obj <- httr2::req_user_agent( |
| 373 | +request_obj, |
| 374 | +"suwo (https://github.com/ropensci/suwo)" |
| 375 | +) |
| 376 | +response <- httr2::req_perform(request_obj) |
| 377 | +rlang::last_trace() |
| 378 | +wa <- query_wikiaves("Procnias verano") |
| 379 | +devtools::load_all() |
| 380 | +species <- "Procnias averano" |
| 381 | +format <- "sound" |
| 382 | +cores = getOption("suwo_cores", 1) |
| 383 | +pb = getOption("suwo_pb", TRUE) |
| 384 | +verbose = getOption("suwo_verbose", TRUE) |
| 385 | +all_data = getOption("suwo_all_data", FALSE) |
| 386 | +raw_data = getOption("suwo_raw_data", FALSE) |
| 387 | +## argument checking |
| 388 | +check_results <- .check_arguments( |
| 389 | +fun = "query_wikiaves", |
| 390 | +args = list( |
| 391 | +species = species, |
| 392 | +format = format, |
| 393 | +cores = cores, |
| 394 | +pb = pb, |
| 395 | +verbose = verbose, |
| 396 | +all_data = all_data, |
| 397 | +raw_data = raw_data |
| 398 | +) |
| 399 | +) |
| 400 | +# report errors |
| 401 | +.report_assertions(check_results) |
| 402 | +if (!.checkconnection(verb = verbose, service = "wikiaves")) { |
| 403 | +return(invisible(NULL)) |
| 404 | +} |
| 405 | +# assign a value to format |
| 406 | +format <- rlang::arg_match(format, values = c("image", "sound")) |
| 407 | +wiki_format <- switch(format, sound = "s", image = "f") |
| 408 | +url <- paste0( |
| 409 | +"https://www.wikiaves.com.br/getTaxonsJSON.php?term=", |
| 410 | +utils::URLencode(species, reserved = TRUE) |
| 411 | +) |
| 412 | +utils::browseURL(url) |
| 413 | +id <- readline( |
| 414 | +"\nA browser window has been opened.\n\nCopy the JSON response (or just the species ID) and paste it here:\n> " |
| 415 | +) |
| 416 | +# User pasted just the ID |
| 417 | +if (grepl("^\\d+$", id)) { |
| 418 | +species_id <- as.integer(id) |
| 419 | +# User pasted the JSON |
| 420 | +} else { |
| 421 | +species_id <- jsonlite::fromJSON(id)$id |
| 422 | +} |
| 423 | +species_id |
| 424 | +get_ids <- data.frame( |
| 425 | +id = species_id, |
| 426 | +label = species, |
| 427 | +stringsAsFactors = FALSE |
| 428 | +) |
| 429 | +get_ids |
| 430 | +if (length(get_ids) > 1) { |
| 431 | +get_ids <- get_ids[ |
| 432 | +trimws(tolower(get_ids$label)) == trimws(tolower(species)), |
| 433 | +] |
| 434 | +} |
| 435 | +if (length(get_ids) == 0) { |
| 436 | +if (verbose) { |
| 437 | +.message("Search species not found", as = "failure") |
| 438 | +} |
| 439 | +return(invisible(NULL)) |
| 440 | +} |
| 441 | +get_ids$total_registers <- vapply( |
| 442 | +seq_len(nrow(get_ids)), |
| 443 | +function(u) { |
| 444 | +request_obj <- httr2::request( |
| 445 | +"https://www.wikiaves.com.br/getRegistrosJSON.php" |
459 | 446 | ) |
460 | | -sticker( |
461 | | -subplot = "./testing/sticker_testing/logo_v3.png", # your image |
462 | | -package = "", |
463 | | -s_x = 1, |
464 | | -s_y = 0.9, |
465 | | -s_width = 1, |
466 | | -p_x = 1, |
467 | | -p_y = 0.15, |
468 | | -h_fill = "white", |
469 | | -h_color = "black", |
470 | | -p_color = "black", |
471 | | -p_size = 18, |
472 | | -filename = "suwo_sticker.png" |
| 447 | +request_obj <- httr2::req_url_query( |
| 448 | +request_obj, |
| 449 | +tm = wiki_format, |
| 450 | +t = "s", |
| 451 | +s = get_ids$id[u], |
| 452 | +o = "mp", |
| 453 | +p = 1 |
| 454 | +) |
| 455 | +request_obj <- |
| 456 | +httr2::req_user_agent( |
| 457 | +request_obj, |
| 458 | +"suwo (https://github.com/ropensci/suwo)" |
| 459 | +) |
| 460 | +request_obj <- httr2::req_error(request_obj, is_error = function(resp) { |
| 461 | +FALSE |
| 462 | +}) |
| 463 | +response <- try(httr2::req_perform(request_obj), silent = TRUE) |
| 464 | +# if fail request return -9999 |
| 465 | +if (.is_error(response)) { |
| 466 | +return(-999) |
| 467 | +} |
| 468 | +# check if request succeeded |
| 469 | +if (httr2::resp_is_error(response)) { |
| 470 | +return(-999) |
| 471 | +} |
| 472 | +content <- httr2::resp_body_json(response) |
| 473 | +as.numeric(content$registros$total) |
| 474 | +}, |
| 475 | +numeric(1) |
473 | 476 | ) |
474 | | -sticker( |
475 | | -subplot = "./testing/sticker_testing/logo_v3.png", # your image |
476 | | -package = "", |
477 | | -s_width = 1.05, |
478 | | -h_fill = NA, |
479 | | -h_color = NA, |
480 | | -filename = "suwo_sticker.png" |
| 477 | +# let user gracefully know error when downloading metadata |
| 478 | +if ( |
| 479 | +any(vapply( |
| 480 | +get_ids$total_registers, |
| 481 | +function(x) x == -999, |
| 482 | +FUN.VALUE = logical(1) |
| 483 | +)) |
| 484 | +) { |
| 485 | +if (verbose) { |
| 486 | +.message(text = "Metadata could not be downloaded", as = "failure") |
| 487 | +} |
| 488 | +return(invisible(NULL)) |
| 489 | +} |
| 490 | +seq_len(nrow(get_ids)) |
| 491 | +u = 1 |
| 492 | +request_obj <- httr2::request( |
| 493 | +"https://www.wikiaves.com.br/getRegistrosJSON.php" |
481 | 494 | ) |
482 | | -sticker( |
483 | | -subplot = "./testing/sticker_testing/logo_v3.png", # your image |
484 | | -package = "", |
485 | | -h_fill = NA, |
486 | | -h_color = NA, |
487 | | -filename = "suwo_sticker.png" |
| 495 | +request_obj <- httr2::req_url_query( |
| 496 | +request_obj, |
| 497 | +tm = wiki_format, |
| 498 | +t = "s", |
| 499 | +s = get_ids$id[u], |
| 500 | +o = "mp", |
| 501 | +p = 1 |
488 | 502 | ) |
489 | | -library(magick) |
490 | | -img <- image_read("./testing/sticker_testing/logo_v3.png") |
491 | | -# remove outer whitespace |
492 | | -img2 <- image_trim(img) |
493 | | -img2 |
494 | | -image_write(img2, "./testing/sticker_testing/suwo_trimmed.png") |
495 | | -sticker( |
496 | | -subplot = "./testing/sticker_testing/suwo_trimmed.png", # your image |
497 | | -package = "", |
498 | | -h_fill = NA, |
499 | | -h_color = NA, |
500 | | -filename = "suwo_sticker.png" |
| 503 | +request_obj <- |
| 504 | +httr2::req_user_agent( |
| 505 | +request_obj, |
| 506 | +"suwo (https://github.com/ropensci/suwo)" |
501 | 507 | ) |
502 | | -image_write(img2, "./testing/sticker_testing/suwo_sticker.png") |
503 | | -img <- image_read("./testing/sticker_testing/logo_v3.png") |
504 | | -# remove outer whitespace |
505 | | -img2 <- image_trim(img) |
506 | | -image_write(img2, "./testing/sticker_testing/suwo_sticker.png") |
507 | | -image_write(img2, "./suwo_sticker.png") |
508 | | -img <- image_read("./testing/sticker_testing/logo_v3.png") |
509 | | -# remove outer whitespace |
510 | | -img2 <- image_trim(img) |
511 | | -image_write(img2, "./suwo_sticker.png") |
512 | | -pkgdown::build_favicons(overwrite = TRUE) |
| 508 | +request_obj <- httr2::req_error(request_obj, is_error = function(resp) { |
| 509 | +FALSE |
| 510 | +}) |
| 511 | +response <- try(httr2::req_perform(request_obj), silent = TRUE) |
| 512 | +response |
0 commit comments