Skip to content

Commit

Permalink
Fixing 'no visible binding for global variable' issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zsteinmetz committed Aug 8, 2023
1 parent 2cac4a3 commit bb59de6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 42 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ S3method(as_OpenSpecy,data.frame)
S3method(as_OpenSpecy,default)
S3method(as_OpenSpecy,hyperSpec)
S3method(as_OpenSpecy,list)
S3method(as_OpenSpecy,vector)
S3method(conform_spec,OpenSpecy)
S3method(conform_spec,default)
S3method(correlate_spectra,OpenSpecy)
Expand Down
11 changes: 2 additions & 9 deletions R/as_OpenSpecy.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,8 @@ as_OpenSpecy.list <- function(x, ...) {
#'
#' @export
as_OpenSpecy.hyperSpec <- function(x, ...) {
do.call("as_OpenSpecy", list(x = x@wavelength,
spectra = as.data.table(t(x$spc)), ...))
}

#' @rdname as_OpenSpecy
#'
#' @export
as_OpenSpecy.vector <- function(x, ...) {
do.call("as_OpenSpecy", list(x = x, spectra = spectra, ...))
do.call("as_OpenSpecy", list(x = x@wavelength,
spectra = as.data.table(t(x$spc)), ...))
}

#' @rdname as_OpenSpecy
Expand Down
21 changes: 13 additions & 8 deletions R/collapse_particles.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ collapse_spectra <- function(object) {
transpose(make.names = "id")

object$metadata <- object$metadata |>
unique(by = c("particle_ids", "area", "feret_max", "centroid_y",
unique(by = c("particle_id", "area", "feret_max", "centroid_y",
"centroid_x"))

return(object)
Expand Down Expand Up @@ -72,7 +72,7 @@ characterize_particles <- function(object, particles) {
stop("Particles needs to be a character or logical vector.", call. = F)
}

object$metadata <- particles_df[setDT(object$metadata), on = .(x, y)][,particle_ids := ifelse(is.na(particle_ids), "-88", particle_ids)][,centroid_x := mean(x), by = "particle_ids"][,centroid_y := mean(y), by = "particle_ids"]
object$metadata <- particles_df[setDT(object$metadata), on = .(x, y)][,particle_id := ifelse(is.na(particle_id), "-88", particle_id)][,centroid_x := mean(x), by = "particle_id"][,centroid_y := mean(y), by = "particle_id"]

return(object)
}
Expand All @@ -81,13 +81,14 @@ characterize_particles <- function(object, particles) {
#' @importFrom stats dist
.characterize_particles <- function(x, binary, name = NULL) {
# Label connected components in the binary image
binary_matrix <- matrix(binary, ncol = max(x$metadata$y)+1, byrow = T)
labeled_image <- imager::label(imager::as.cimg(binary_matrix), high_connectivity = T)
binary_matrix <- matrix(binary, ncol = max(x$metadata$y) + 1, byrow = T)
labeled_image <- imager::label(imager::as.cimg(binary_matrix),
high_connectivity = T)

# Create a dataframe with particle IDs for each true pixel
particle_points_dt <- data.table(x = x$metadata$x,
y = x$metadata$y,
particle_ids = as.character(as.vector(t(ifelse(binary_matrix, labeled_image, -88)))))
particle_id = as.character(as.vector(t(ifelse(binary_matrix, labeled_image, -88)))))

# Apply the logic to clean components
cleaned_components <- ifelse(binary_matrix, labeled_image, -88)
Expand All @@ -114,9 +115,13 @@ characterize_particles <- function(object, particles) {
# Area
area <- sum(cleaned_components == as.integer(id))

data.table(particle_ids = id, area = area, feret_max = feret_max)
}), fill = TRUE)
data.table(particle_id = id, area = area, feret_max = feret_max)
}), fill = T)

# Join with the coordinates from the binary image
particle_points_dt[particles_dt[, particle_ids := if (!is.null(name)) paste0(name, "_", particle_ids) else particle_ids], on = "particle_ids"]
if (!is.null(name)) {
particles_dt$particle_id <- paste0(name, "_", particles_dt$particle_id)
}

particle_points_dt[particles_dt, on = "particle_id"]
}
29 changes: 15 additions & 14 deletions R/smooth_intens.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' A typical good smooth can be achieved with 11 data point window and a 3rd or
#' 4th order polynomial.
#'
#' @param object a list object of class \code{OpenSpecy}.
#' @param x an object of class \code{OpenSpecy}.
#' @param p polynomial order for the filter
#' @param n number of data points in the window, filter length (must be odd).
#' @param m the derivative order if you want to calculate the derivative. Zero (default) is no derivative.
Expand Down Expand Up @@ -40,34 +40,35 @@
#' Simplified Least Squares Procedures.” \emph{Analytical Chemistry},
#' \strong{36}(8), 1627--1639.
#'
#' @importFrom magrittr %>%
#' @importFrom signal filter sgolay
#' @importFrom data.table .SD
#' @export
smooth_intens <- function(object, ...) {
smooth_intens <- function(x, ...) {
UseMethod("smooth_intens")
}

#' @rdname smooth_intens
#'
#' @export
smooth_intens.default <- function(object, ...) {
stop("object 'x' needs to be of class 'OpenSpecy'", call. = F)
smooth_intens.default <- function(x, ...) {
stop("'x' needs to be of class 'OpenSpecy'", call. = F)
}

#' @rdname smooth_intens
#'
#' @export
smooth_intens.OpenSpecy <- function(object, p = 3, n = 11, m = 0, abs = F, make_rel = TRUE,
...) {
filt <- object$spectra[, lapply(.SD, .sgfilt, p = p, n = n, m = m, abs = abs, ...)]

if (make_rel) object$spectra <- make_rel(filt) else object$spectra <- filt
smooth_intens.OpenSpecy <- function(x, p = 3, n = 11, m = 0, abs = FALSE,
make_rel = TRUE,
...) {
filt <- x$spectra[, lapply(.SD, .sgfilt, p = p, n = n, m = m, abs = abs, ...)]
if (make_rel) x$spectra <- make_rel(filt) else x$spectra <- filt

return(object)
return(x)
}

#' @importFrom signal filter sgolay
.sgfilt <- function(y, p, n, m, abs = F, ...) {
signal::filter(filt = sgolay(p = p, n = n, m = m, ...), x = y) %>%
{if(abs) abs(.) else .}
out <- signal::filter(filt = sgolay(p = p, n = n, m = m, ...), x = y)
if(abs) out <- abs(out)

return(out)
}
3 changes: 0 additions & 3 deletions man/as_OpenSpecy.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/smooth_intens.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-collapse_particles.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("check that particles are identified when given logical", {
map$metadata$particles <- map$metadata$x == 0
identified_map <- characterize_particles(map, map$metadata$particles)
expect_true(is_OpenSpecy(identified_map))
expect_true(length(unique(identified_map$metadata$particle_ids)) == 2)
expect_true(length(unique(identified_map$metadata$particle_id)) == 2)
expect_true(max(identified_map$metadata$area, na.rm = T) == 13)
expect_true(max(identified_map$metadata$feret_max, na.rm = T) == 13)
})
Expand All @@ -14,7 +14,7 @@ test_that("check that particles are identified when given character", {
map$metadata$particles <- ifelse(map$metadata$x == 1, "particle", "not_particle")
identified_map <- characterize_particles(map, map$metadata$particles)
expect_true(is_OpenSpecy(identified_map))
expect_true(length(unique(identified_map$metadata$particle_ids)) == 3)
expect_true(length(unique(identified_map$metadata$particle_id)) == 3)
expect_true(max(identified_map$metadata$area, na.rm = T) == 182)
expect_true(round(max(identified_map$metadata$feret_max, na.rm = T)) == 19)
})
Expand Down Expand Up @@ -61,7 +61,7 @@ identified_map <- characterize_particles(map, particles)
test_collapsed <- collapse_spectra(identified_map)
expect_true(is_OpenSpecy(test_collapsed))

expect_equal(c("particle_ids", "area", "feret_max", "centroid_y", "centroid_x"), names(test_collapsed$metadata))
expect_equal(c("particle_id", "area", "feret_max", "centroid_y", "centroid_x"), names(test_collapsed$metadata))

})

Expand Down

0 comments on commit bb59de6

Please sign in to comment.