Skip to content

Commit bc756b3

Browse files
author
Max Czapanskiy
committed
Rough draft
Former-commit-id: 3b8b997db2f0ae536a7b0ed5a850970cc33275bd
1 parent 0882b00 commit bc756b3

34 files changed

+927
-91
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
^CONTRIBUTING\.md$
88
analysis
99
^data-raw$
10+
^runtime\.txt$

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.Rdata
44
.httr-oauth
55
.DS_Store
6+
analysis/paper/paper_cache/*

DESCRIPTION

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ Authors@R:
77
role = c("aut", "cre"),
88
email = "[email protected]",
99
comment = c("ORCID-ID" = "0000-0002-6302-905X"))
10-
Description: Code and data for [Authors] [Title] [Journal] [doi]
10+
Description: Code and data for Czapanskiy et al. "Accelerometer-derived
11+
ballistocardiogram confirms blue whale (*Balaenoptera musculus*) heart
12+
rate during diving bradycardia".
1113
License: MIT + file LICENSE
12-
ByteCompile: true
13-
Encoding: UTF-8
14-
LazyData: true
15-
Roxygen: list(markdown = TRUE)
16-
RoxygenNote: 7.1.1
14+
Depends:
15+
R (>= 2.10)
1716
Imports:
18-
akima,
1917
bookdown,
2018
cowplot,
2119
dplyr,
@@ -32,12 +30,20 @@ Imports:
3230
RcppRoll,
3331
readxl,
3432
RobustLinearReg,
35-
scales,
3633
signal
3734
Suggests:
38-
tidyverse,
35+
bib2df,
36+
crul,
3937
devtools,
4038
git2r,
41-
shiny
42-
Depends:
43-
R (>= 2.10)
39+
rcrossref,
40+
shiny,
41+
stringr,
42+
tidyverse
43+
Remotes:
44+
FlukeAndFeather/crul
45+
ByteCompile: true
46+
Encoding: UTF-8
47+
LazyData: true
48+
Roxygen: list(markdown = TRUE)
49+
RoxygenNote: 7.1.1

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ export(s2n)
1212
export(shannon_entropy)
1313
export(split_dives)
1414
export(tma)
15-
import(tidyverse)
1615
importFrom(magrittr,"%>%")

R/bib.R

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#' Create crossref.bib
2+
#'
3+
#' Uses package rcrossref to look up DOIs in dois.txt and create crossref.bib.
4+
write_crossref <- function() {
5+
for (pkg in c("rcrossref", "bib2df")) {
6+
if (!requireNamespace(pkg, quietly = TRUE)) {
7+
stop(sprintf("write_crossref() requires package '%s'", pkg))
8+
}
9+
}
10+
# Fetch from crossref
11+
dois <- readLines("analysis/paper/dois.txt")
12+
refs <- rcrossref::cr_cn(dois = dois, format = "bibtex")
13+
bib_path <- "analysis/paper/crossref.bib"
14+
if (file.exists(bib_path)) {
15+
message("Overwriting crossref.bib")
16+
file.remove(bib_path)
17+
}
18+
file.create(bib_path)
19+
for (ref in refs) {
20+
write(paste0(ref, "\n"), file = bib_path, append = TRUE)
21+
}
22+
23+
# Fix entry names
24+
get_first_author <- function(author_list) {
25+
author_list[1] %>% stringr::str_match(".* ([^ ]+)$") %>% `[`(1, 2)
26+
}
27+
append_suffix <- function(keys) {
28+
if (length(keys) > 1) {
29+
paste0(keys, letters[seq(length(keys))])
30+
} else {
31+
keys
32+
}
33+
}
34+
bibdf <- bib2df::bib2df(bib_path) %>%
35+
dplyr::mutate(BIBTEXKEY = paste(purrr::map_chr(AUTHOR, get_first_author),
36+
YEAR,
37+
sep = "-")) %>%
38+
dplyr::group_by(BIBTEXKEY) %>%
39+
dplyr::mutate(BIBTEXKEY = append_suffix(BIBTEXKEY)) %>%
40+
dplyr::ungroup()
41+
bib2df::df2bib(bibdf, bib_path)
42+
}
43+
44+

R/dives.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' to individual dives. Inter-dive periods marked as NA.
1111
#' @export
1212
split_dives <- function(dt, depth, surface = 0, min_depth = 0, min_dur = 0) {
13-
dive_start <- depth >= surface & lag(depth) < surface
13+
dive_start <- depth >= surface & dplyr::lag(depth) < surface
1414
dive_start[1] <- FALSE
1515
dive_id <- cumsum(dive_start)
1616
dive_id[depth < surface | dive_id == 0] <- NA
@@ -28,7 +28,7 @@ split_dives <- function(dt, depth, surface = 0, min_depth = 0, min_dur = 0) {
2828

2929
#' Normalize dive time
3030
#'
31-
#' Normalize the time in each dive to [0, 1] where 0 is start of dive and 1 is
31+
#' Normalize the time in each dive to `[0, 1]` where 0 is start of dive and 1 is
3232
#' the end.
3333
#'
3434
#' @param dive_id A factor of dive id's, as from \code{split_dives()}.

R/find_beats.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ find_beats <- function(cue, fs_hz, minperiod) {
1414
# Skip if less than 50 peaks
1515
if (nrow(peaks) >= 50) {
1616
dist_max <- sqrt((max(peaks[, 1]) - peaks[, 1])^2 + (max(proms) - proms)^2)
17-
dist_dens <- density(dist_max)
17+
dist_dens <- stats::density(dist_max)
1818
dist_thr <- dist_dens$x[pracma::findpeaks(-dist_dens$y)[, 2]]
1919
is_beat <- peaks[, 2][dist_max <= dist_thr]
2020
} else {
@@ -57,7 +57,7 @@ filter_acc <- function(acc, fs, upper) {
5757
choose_peaks <- function(x) {
5858
x_sorted <- sort(x)
5959
steps <- diff(x_sorted)
60-
thr <- if (max(steps) > mean(steps) + 2 * sd(steps)) {
60+
thr <- if (max(steps) > mean(steps) + 2 * stats::sd(steps)) {
6161
x_sorted[which.max(steps)]
6262
} else {
6363
x_sorted[1]

R/psd.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
#' @return `[spec]` see `psd::pspectrum`
77
#' @export
88
pspectrum <- function(x, fs_hz) {
9-
suppressMessages(psd::pspectrum(ts(x, frequency = fs_hz)))
9+
suppressMessages(psd::pspectrum(stats::ts(x, frequency = fs_hz)))
1010
}
1111

1212
#' Calculate signal-to-noise ratio from power spectral density
1313
#'
1414
#' @param psd `[spec]` power spectral density
1515
#' @param signal_bpm `[numeric(2)]` signal band (bpm)
1616
#'
17-
#' @return
17+
#' @return `[numeric(1)]`
1818
#' @export
1919
s2n <- function(psd, signal_bpm) {
2020
signal_hz <- signal_bpm / 60
21-
is_signal <- between(psd$freq, signal_hz[1], signal_hz[2])
21+
is_signal <- dplyr::between(psd$freq, signal_hz[1], signal_hz[2])
2222
is_noise <- !is_signal
2323

2424
psignal <- pracma::trapz(psd$freq[is_signal], psd$spec[is_signal])

R/zzz.R

Lines changed: 0 additions & 2 deletions
This file was deleted.
-538 KB
Binary file not shown.

0 commit comments

Comments
 (0)