Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas White committed Aug 30, 2023
1 parent 2d237ec commit eb9a3cc
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions R/stitch.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
#' do not overlap. Defaults to `TRUE`.
#'
#' @export
#'
#'
#' @examples
#'
#' # Simulate a UV-VIS and NIR reflectance spectrum whose wavelength regions
#' # Simulate a UV-VIS and NIR reflectance spectrum whose wavelength regions
#' # slightly overlap then stitch them together, with the overlapping
#' # regions being averaged.
#'
#'
#' # Simulate specs
#' reflect1 <- simulate_spec(wl_peak = 550, xlim = c(300, 700))
#' reflect2 <- simulate_spec(wl_inflect = 1100, xlim = c(650, 1200))
#'
#'
#' # Ensure the names of the spectra match
#' names(reflect1) <- names(reflect2) <- c('wl', 'sample_1')
#'
#'
#' # Stitch the spectra together by their wavelength column
#' full_spec <- stitch(reflect1, reflect2)
#'
#'
#' # Plot the resulting spectrum
#' plot(full_spec)
#'
Expand All @@ -39,21 +39,21 @@
#' # the spectra match, but are in a different order in the two rspec objects. This isn't
#' # an issue, as the function can match up the spectra by name irrespective of their
#' # ordering
#'
#'
#' # Simulate UV-VIS and NIR spectra
#' reflect_vis <- merge(simulate_spec(wl_peak = 550, xlim = c(300, 700)),
#' simulate_spec(wl_peak = 550, xlim = c(300, 700)))
#' reflect_nir <- merge(simulate_spec(wl_inflect = 1000, xlim = c(800, 1250)),
#' simulate_spec(wl_inflect = 1100, xlim = c(800, 1250)))
#'
#'
#' # Ensure the names of the spectra exist in each, albeit in a different order
#' names(reflect_vis) <- c('wl', 'sample_1', 'sample_2')
#' names(reflect_nir) <- c('wl', 'sample_2', 'sample_1')
#'
#'
#' # Stitch together by their wavelength column, with missing regions being
#' # interpolated
#' reflect_vis_nir <- stitch(reflect_vis, reflect_nir)
#'
#'
#' # Plot the resulting spectrum
#' plot(reflect_vis_nir)
#'
Expand All @@ -63,67 +63,67 @@
#' @seealso [as.rspec()], [merge.rspec()]

stitch <- function(rspec1, rspec2,
overlap_method = c('average', 'minimum', 'maximum'),
overlap_method = c("average", "minimum", "maximum"),
interp = TRUE) {

# Class check
if (!inherits(rspec1, 'rspec') || !inherits(rspec2, 'rspec')) {
if (!inherits(rspec1, "rspec") || !inherits(rspec2, "rspec")) {
stop("Both inputs must be of class 'rspec'")
}

# Valied overlap_method
overlap_method <- match.arg(overlap_method)

# Check that at least one spectrum has a matching name in both objects
common_cols <- intersect(names(rspec1), names(rspec2))
if (length(common_cols) <= 1) {
stop("At least one spectrum in both rspec objects must have a matching name")
}

# Identify unique spectra in both objects
unique_rspec1 <- setdiff(names(rspec1), common_cols)
unique_rspec2 <- setdiff(names(rspec2), common_cols)

# Create NA-filled columns in each rspec for unique spectra in the other
rspec1[, unique_rspec2] <- NA
rspec2[, unique_rspec1] <- NA

# Reorder columns of rspec2 to match rspec1
rspec2 <- rspec2[, names(rspec1)]

# Merge by wl
res <- rbind(rspec1, rspec2)

# Handle overlapping wl values
if (any(duplicated(res$wl))) {
if (anyDuplicated(res$wl) > 0) {
overlap_wl <- unique(res$wl[duplicated(res$wl)])

for (wl in overlap_wl) {
idx <- which(res$wl == wl)

# Replace with a switch statement
switch(overlap_method,
average = res[idx[1],-1] <- colMeans(as.matrix(res[idx,-1]), na.rm = TRUE),
minimum = res[idx[1],-1] <- apply(as.matrix(res[idx,-1]), 2, min, na.rm = TRUE),
maximum = res[idx[1],-1] <- apply(as.matrix(res[idx,-1]), 2, max, na.rm = TRUE)
average = res[idx[1], -1] <- colMeans(as.matrix(res[idx, -1]), na.rm = TRUE),
minimum = res[idx[1], -1] <- apply(as.matrix(res[idx, -1]), 2, min, na.rm = TRUE),
maximum = res[idx[1], -1] <- apply(as.matrix(res[idx, -1]), 2, max, na.rm = TRUE)
)

# Remove extra rows
if (length(idx) > 1) {
res <- res[-idx[-1], ]
}
}
}

# Interpolate missing values
if (interp) {
full_wl_range <- min(res$wl):max(res$wl)
missing_wl <- setdiff(full_wl_range, res$wl)
if(length(missing_wl) > 0) {

if (length(missing_wl) > 0) {
new_rows <- data.frame(wl = missing_wl, matrix(rep(NA, length(names(res)) - 1), ncol = length(names(res)) - 1))
names(new_rows)[-1] <- names(res)[-1]

# Interpolate only common spectra
for (col in common_cols[-1]) {
new_values <- approx(res$wl, res[, col], xout = missing_wl)$y
Expand All @@ -132,12 +132,12 @@ stitch <- function(rspec1, rspec2,
res <- rbind(res, new_rows)
}
}

# Sort stitched spec by wl
res <- res[order(res$wl), ]

# Classes
class(res) <- c("rspec", "data.frame")

res
}

0 comments on commit eb9a3cc

Please sign in to comment.