Skip to content

Commit

Permalink
Merge pull request #44 from MindTheGap-ERC/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
NiklasHohmann authored Sep 11, 2024
2 parents 3d9da82 + 6807026 commit 3249b0c
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 16 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ S3method(time_to_strat,phylo)
export(L_axis_lab)
export(T_axis_lab)
export(add_adm_to_multiadm)
export(anchor)
export(condensation)
export(condensation_fun)
export(flux_const)
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# admtools (development version)

* added anchoring of age-depth models via `anchor`

# admtools 0.3.1

* more options for sedimentation rate generator `sed_rate_from_matrix`

* abstracted exctraction of tie points
* abstracted extraction of tie points

* added utility functions for S3 class `sac`

Expand Down
63 changes: 63 additions & 0 deletions R/anchor.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
anchor = function(x, index = "last", t_anchor = NULL, n = 1000L){
#' @export
#'
#' @title anchor age-depth model
#'
#' @param x age-depth model
#' @param index "last" or "first", specifying at which tie point the age-depth model will be anchored
#' @param t_anchor time at which the adm is anchored. must be a function that takes no arguments and returns the timing of the tie point. see example or vignettes for details
#' @param n integer, number of samples drawn from the tie point
#'
#' @description
#' anchors a deterministic age-depth model (adm object) at a tie point that is associated with uncertainty.
#'
#'
#'
#' @returns a collection of age-depth models (a multiadm object)
#'
#' @examples
#' t_anchor = function() rnorm(1) # normally distributed uncertainty
#' x = tp_to_adm(t = c(1,2), h = c(2,3)) # simple age-depth model
#' m = anchor(x, index = "last", t_anchor = t_anchor, n = 100) # anchor age-depth model
#' plot(m)
#'
if (! index %in% c("last", "first")){
stop("can only anchor at first or last tie point")
}

if (! (is.function(t_anchor))){
stop("expect functions as inputs for t_anchor")
}
adm_list = list()
if (index == "last"){
t_ref = max_time(x)
}
if (index == "first"){
t_ref = min_time(x)
}

for (i in seq_len(n)){
t_offset = t_anchor()
t = get_T_tp(x) - t_ref + t_offset
h = get_L_tp(x)
L_unit = get_L_unit(x)
T_unit = get_T_unit(x)
adm_list[[i]] = tp_to_adm(t, h, T_unit, L_unit)
}

multiadm = list("t" = list(),
"h" = list(),
"destr" = list(),
"no_of_entries" = length(adm_list),
"T_unit" = get_T_unit(x),
"L_unit" = get_L_unit(x))

for (i in seq_along(adm_list)){
adm = adm_list[[i]]
multiadm[["t"]][[i]] = adm$t
multiadm[["h"]][[i]] = adm$h
multiadm[["destr"]][[i]] = adm$destr
}
class(multiadm) = "multiadm"
return(multiadm)
}
8 changes: 4 additions & 4 deletions R/merge_adm_to_multiadm.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ merge_adm_to_multiadm = function(...){
"L_unit" = NULL)
T_units = unlist(sapply(adm_list, get_T_unit))
L_units = unlist(sapply(adm_list, get_L_unit))
if (any(rep(T_units[1], length(adm_list)) != T_units)){
stop("Inconsistent time units, can not merge adms")

if (any(rep(T_units[1], length(adm_list)) != T_units)){
stop("Inconsistent time units, can not merge adms")
}
if (any(rep(L_units[1], length(adm_list)) != L_units)){
if (any(rep(L_units[1], length(adm_list)) != L_units)){
stop("Inconsistent length units, can not merge adms")
}

Expand Down
2 changes: 1 addition & 1 deletion R/tp_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ tp_time_det = function(times){
#'
#' @seealso [tp_height_det()] for deterministic tie points in height, [tp_time_norm()] for tie points following a normal distribution
#'
#' @returns a function for usage with _strat_cont_to_multiadm_ and_sedrate_to_mulitadm_ as t_tp input
#' @returns a function for usage with _strat_cont_to_multiadm_ and _sedrate_to_mulitadm_ as t_tp input
#'
if (is.unsorted(times, strictly = TRUE)){
stop("Need strictly increasing times")
Expand Down
8 changes: 4 additions & 4 deletions R/tp_to_adm.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ tp_to_adm = function(t, h, T_unit = NULL, L_unit = NULL){
#'
#' @param t Vector, tie points in time
#' @param h Vector, tie points in height
#' @param T_unit time unit
#' @param L_unit length unit
#' @param T_unit character, time unit
#' @param L_unit character, length unit
#'
#' @details
#' by default, intervals with no sediment accumulation are marked as destructive.
Expand All @@ -26,11 +26,11 @@ tp_to_adm = function(t, h, T_unit = NULL, L_unit = NULL){
#'
#'
#' @examples
#' \dontrun{
#'
#' my_adm = tp_to_adm(t = 1:4, h = c(1,2,2,3), T_unit = "kyr", L_unit = "m")
#' plot(my_adm)
#' # see vignette("admtools") for other examples
#' }
#'
#'
#'

Expand Down
30 changes: 30 additions & 0 deletions man/anchor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/tp_time_det.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/tp_to_adm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/testthat/test_anchor.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test_that("incorrect input for index paramter is caught", {
t_anchor = function() rnorm(n = 1)
x = tp_to_adm(c(0,1), c(0,1), "Myr", "m")
expect_no_condition(anchor(x, index = "last", t_anchor, n = 10))
expect_no_condition(anchor(x, index = "first", t_anchor, n = 10))
expect_error(anchor(x, index = 2, t_anchor, n = 10))
})

test_that("incorrect input for t_anchor parameter is caught", {
x = tp_to_adm(c(0,1), c(0,1), "Myr", "m")
t_anchor = 2
expect_error(anchor(x, "last", t_anchor, n = 10))
})
4 changes: 3 additions & 1 deletion vignettes/admtools_doc.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ The following functions transform `adm` objects into other S3 classes:

- `merge_adm_to_multiadm` into `multiamd`
- `add_adm_to_multiadm` to add `adm` to `multiadm`
- `anchor` to transform anchor `adm` at a tie point with uncertainty, resulting in a `multiadm`

### S3 class `sac`

Expand Down Expand Up @@ -177,7 +178,8 @@ The S3 class `multiadm` represents **multi**ple **a**ge **d**epth **m**odels.. S

The following functions construct `multiadm` objects:

- `merge_adm_to_multiadm` to contruct `multiadm` from `adm` objects
- `anchor` to construct `multiadm` from uncertain tie points and `adm` objects.
- `merge_adm_to_multiadm` to construct `multiadm` from `adm` objects
- `sedrate_to_multiadm` construct `multiadm` from info on sedimentation rates, see `vignette("adm_from_sedrate")`
- `strat_cont_to_multiadm` construct `multiadm` from tracer information, see `vignette("adm_from_trace_cont")`

Expand Down

0 comments on commit 3249b0c

Please sign in to comment.