Skip to content

Commit

Permalink
tbind() -> exported rbind() S3 method
Browse files Browse the repository at this point in the history
  • Loading branch information
lschneiderbauer committed Dec 27, 2024
1 parent 8854953 commit 7db2aac
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 22 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ S3method(as.data.frame,fcwtr_scalogram)
S3method(as.matrix,fcwtr_scalogram)
S3method(plot,fcwtr_scalogram)
S3method(print,fcwtr_scalogram)
S3method(rbind,fcwtr_scalogram)
export(autoplot.fcwtr_scalogram)
export(fcwt)
export(fcwt_batch)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# fcwtr 0.2.9999

- Add S3 methods: `print()`, `[]`, `as.matrix()`.
- Add S3 methods: `print()`, `[]`, `as.matrix()`, `rbind()`.

- Include improved physical unit treatment with the 'units' package. Frequency and time parameters can now be "units" objects, created with `u()`. Allow user to adjust plot scales to use arbitrary time/freq units.

Expand Down
2 changes: 1 addition & 1 deletion R/fcwt_batch.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fcwt_batch <- function(signal,
sc_agg(w)
}
) |>
do.call(tbind, args = _)
do.call(rbind, args = _)
}

batches <- function(batch_size, signal_size, dt) {
Expand Down
54 changes: 37 additions & 17 deletions R/fcwtr_scalogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,30 +180,50 @@ sc_agg <- function(x, wnd) {
)
}

tbind <- function(..., deparse.level = 1) {
#' Combine scalograms in "time" direction
#'
#' Given two or more scalograms with identical sampling frequencies, frequency
#' scales and sigma, it can be useful to combine several into a single object
#' creating a longer time series.
#' The function errs if these conditions are not satisfied.
#'
#' The scalograms are stitched together in chronological fashion (i.e. the first
#' argument will the initial piece, etc.).
#' Time offset information is kept from the first piece.
#'
#' @param ...
#' One or more "fcwtr_scalogram" objects, generated by `fcwt()`.
#'
#' @return Returns a new time-wise combined "fcwtr_scalogram" object.
#'
#' @export
rbind.fcwtr_scalogram <- function(...) {
args <- list(...)
stopifnot(length(args) >= 1)
lapply(args, \(arg) stopifnot(inherits(arg, "fcwtr_scalogram")))

arg_attr_ident <- function(attr) {
vals <-
sapply(args, \(arg) attr(arg, attr))

all.equal(max(vals), min(vals))
}

# check if attributes are identical, otherwise combination
# does not make sense
if (length(unique(lapply(args, \(arg) round(attr(arg, "sample_freq"))))) > 1) {
stop("Sampling frequencies need to be identical.")
}
if (length(unique(lapply(args, \(arg) attr(arg, "freq_begin")))) > 1) {
stop("Frequency ranges need to be identical.")
}
if (length(unique(lapply(args, \(arg) attr(arg, "freq_end")))) > 1) {
stop("Frequency ranges need to be identical.")
}
if (length(unique(lapply(args, \(arg) attr(arg, "freq_scale")))) > 1) {
stop("Frequency scales need to be identical.")
}
if (length(unique(lapply(args, \(arg) attr(arg, "sigma")))) > 1) {
stop("Sigma parameter needs to be identical.")
}
stopifnot(
"Sampling frequencies need to be identical." = arg_attr_ident("sample_freq"),
"Frequency ranges need to be identical." = arg_attr_ident("freq_begin"),
"Frequency ranges need to be identical." = arg_attr_ident("freq_end"),
"Frequency scales need to be identical." = arg_attr_ident("freq_scale"),
"Sigma parameter needs to be identical." = arg_attr_ident("sigma")
)

x_new <- do.call(rbind, c(lapply(args, unclass), list(deparse.level = deparse.level)))
x_new <-
do.call(
rbind,
c(lapply(args, unclass))
)

new_fcwtr_scalogram(
x_new,
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ reference:
- as.data.frame.fcwtr_scalogram
- as.matrix.fcwtr_scalogram
- "`[.fcwtr_scalogram`"
- rbind.fcwtr_scalogram
- print.fcwtr_scalogram
- plot.fcwtr_scalogram
- autoplot.fcwtr_scalogram
Expand Down
25 changes: 25 additions & 0 deletions man/rbind.fcwtr_scalogram.Rd

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

6 changes: 3 additions & 3 deletions tests/testthat/test-fcwtr_scalogram.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("tbind() for fcwtr_scalogram", {
test_that("rbind() for fcwtr_scalogram", {
first <-
fcwt(
ts_sin_440[1:1000],
Expand Down Expand Up @@ -40,11 +40,11 @@ test_that("tbind() for fcwtr_scalogram", {
"fcwtr_scalogram"
)
expect_s3_class(
tbind(first, second),
rbind(first, second),
"fcwtr_scalogram"
)
expect_error(
tbind(first, third)
rbind(first, third)
)
})

Expand Down

0 comments on commit 7db2aac

Please sign in to comment.