Skip to content

Commit

Permalink
Add precheck for all functions with assertthat
Browse files Browse the repository at this point in the history
  • Loading branch information
YuhangTom committed Sep 25, 2023
1 parent c5b55a5 commit 0768e2d
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 27 deletions.
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export(x3p_vertical)
import(dplyr)
importFrom(Cairo,CairoPNG)
importFrom(assertthat,assert_that)
importFrom(assertthat,has_name)
importFrom(assertthat,is.count)
importFrom(assertthat,is.flag)
importFrom(assertthat,is.number)
importFrom(assertthat,is.string)
importFrom(assertthat,not_empty)
importFrom(bulletxtrctr,sig_align)
importFrom(concaveman,concaveman)
Expand All @@ -26,6 +31,7 @@ importFrom(dplyr,filter)
importFrom(dplyr,group_by)
importFrom(dplyr,mutate)
importFrom(dplyr,n)
importFrom(dplyr,near)
importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(dplyr,summarize)
Expand Down
6 changes: 6 additions & 0 deletions R/df_rmtrend_x3p.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @import dplyr
#' @importFrom x3ptools df_to_x3p
#' @importFrom stats lm predict
#' @importFrom assertthat assert_that has_name
#' @export
#' @examples
#' x3p <- x3p_subsamples[[1]]
Expand All @@ -18,6 +19,11 @@
#' }
#'
df_rmtrend_x3p <- function(insidepoly_df) {
assert_that(
is.data.frame(insidepoly_df),
has_name(insidepoly_df, c("x", "y", "value", "mask", "n_neighbor_val_miss", "sd_not_miss"))
)

n_neighbor_val_miss <-
value <-
x <-
Expand Down
14 changes: 10 additions & 4 deletions R/inside_polygon.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#' If center is NULL, the half ranges of x and y are used.
#' @return dataframe of x and y positions describing the inside of the area
#' described by the input x and y, variable id describes the order of the points
#' @importFrom dplyr mutate select rename arrange n
#' @importFrom dplyr mutate select rename arrange n near
#' @importFrom concaveman concaveman
#' @importFrom tidyr pivot_longer
#' @importFrom assertthat assert_that is.number
#' @export
#' @examples
#' x3p <- x3p_subsamples[[1]]
Expand All @@ -28,13 +29,18 @@
#' geom_polygon(data = polygon)
#'
inside_polygon <- function(x, y, concavity, center = NULL) {
stopifnot(concavity > 0)

assert_that(
is.numeric(x),
is.numeric(y),
is.number(concavity), concavity > 0
)
if (is.null(center)) {
center <- c(diff(range(x, na.rm = TRUE)), diff(range(y, na.rm = TRUE))) / 2
}
if (length(center) == 1) center <- rep(center, 2)
stopifnot(is.numeric(x), is.numeric(y), is.numeric(center), length(center) == 2)
assert_that(
is.numeric(center), near(length(center), 2)
)

points_inside_out <- data.frame(x, y) %>%
mutate(x = x - center[1], y = y - center[2]) %>%
Expand Down
22 changes: 19 additions & 3 deletions R/vec_align_sigs_list.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' Align signals
#'
#' Align signals with plot.
#' @param sig1 first signal vector
#' @param sig2 second signal vector
#' @param sig1 first numeric signal vector
#' @param sig2 second numeric signal vector
#' @param min.overlap additional parameter passed on to \code{bulletxtrctr::get_ccf}
#' @param ifplot whether graphs are displayed
#' @param name1 name for the first cut
Expand All @@ -12,6 +12,7 @@
#' @return list of aligned signals named \code{ccf}, \code{lag} and \code{lands} followed the output format of \code{bulletxtrctr::sig_align}
#' @importFrom ggplot2 ggplot aes geom_line labs xlab ylab ggtitle
#' @importFrom bulletxtrctr sig_align
#' @importFrom assertthat assert_that not_empty is.count is.flag is.string
#' @export
#' @examples
#' x3p <- x3p_subsamples[[1]]
Expand All @@ -28,12 +29,27 @@
vec_align_sigs_list <- function(
sig1,
sig2,
min.overlap = round(0.75 * min(length(sig1), length(sig2))),
min.overlap = NULL,
ifplot = FALSE,
name1 = "Cut1",
name2 = "Cut2",
legendname = "Signal",
titlename = NULL) {
assert_that(
is.numeric(sig1),
is.numeric(sig2),
is.flag(ifplot),
is.string(name1),
is.string(name2),
is.string(legendname)
)
if (not_empty(min.overlap)) {
is.count(min.overlap)
}
if (not_empty(titlename)) {
is.string(titlename)
}

x <- NULL

sigalign <- sig_align(sig1, sig2)
Expand Down
9 changes: 9 additions & 0 deletions R/x3p_MLE_angle_vec.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#' @importFrom raster raster
#' @importFrom imager as.cimg hough_line nfline
#' @importFrom stats loess predict
#' @importFrom assertthat assert_that is.count is.number is.flag
#' @export
#' @examples
#' x3p <- x3p_subsamples[[1]]
Expand All @@ -26,6 +27,14 @@
x3p_MLE_angle_vec <- function(x3p, ntheta = 720, min_score_cut = 0.1,
ifplot = FALSE,
loess_span = 0.2) {
assert_that(
"x3p" %in% class(x3p),
is.count(ntheta),
is.number(min_score_cut),
is.flag(ifplot),
is.number(loess_span), loess_span > 0
)

theta <-
score <-
theta_mod <-
Expand Down
7 changes: 6 additions & 1 deletion R/x3p_boundary_points.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @importFrom x3ptools x3p_sample x3p_to_df
#' @importFrom dplyr `%>%` group_by mutate filter summarize select
#' @importFrom rlang .data
#' @importFrom assertthat assert_that is.count
#' @export
#' @examples
#' x3p <- x3p_subsamples[[1]]
Expand All @@ -20,7 +21,11 @@
#' geom_point()
#'
x3p_boundary_points <- function(x3p, sample) {
stopifnot("x3p" %in% class(x3p))
assert_that(
"x3p" %in% class(x3p),
is.count(sample)
)

x3p_df <- x3p %>%
x3p_sample(m = sample) %>%
x3p_to_df()
Expand Down
9 changes: 8 additions & 1 deletion R/x3p_image_autosize.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
#' Plot x3p object as an image with auto-adjusted size based on \code{x3ptools::x3p_image}.
#' @param x3p x3p object
#' @param ifhtml logical, whether the image will be put on html
#' @param zoom numeric value indicating the amount of zoom
#' @param zoom numeric value indicating the amount of zoom in \code{x3ptools::x3p_image}
#' @param ... other parameter values except size and zoom used in \code{x3ptools::x3p_image}
#' @return rgl plot, list of tags when \code{ifhtml = TRUE}
#' @importFrom x3ptools x3p_image
#' @importFrom rgl rglwidget
#' @importFrom htmltools tagList
#' @importFrom assertthat assert_that is.flag is.number
#' @export
#' @examples
#' x3p <- x3p_subsamples[[1]]
Expand All @@ -17,6 +18,12 @@
#' }
#'
x3p_image_autosize <- function(x3p, ifhtml = FALSE, zoom = 0.6, ...) {
assert_that(
"x3p" %in% class(x3p),
is.flag(ifhtml),
is.number(zoom), zoom > 0
)

x3p_image(x3p, size = dim(x3p$surface.matrix), zoom = zoom, ...)

if (ifhtml) {
Expand Down
20 changes: 13 additions & 7 deletions R/x3p_impute.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#' @import dplyr
#' @importFrom x3ptools x3p_delete_mask x3p_extract x3p_average x3p_add_mask
#' @importFrom ggplot2 ggplot aes geom_raster scale_fill_gradient2 labs ggsave
#' @importFrom assertthat assert_that not_empty
#' @importFrom raster raster focal as.data.frame as.matrix
#' @importFrom purrr map
#' @importFrom magick image_read image_join image_animate image_write
#' @importFrom stringr str_detect
#' @importFrom assertthat assert_that is.flag not_empty is.string
#' @export
#' @examples
#' x3p <- x3p_subsamples[[1]]
Expand All @@ -27,19 +27,25 @@
#' }
#'
x3p_impute <- function(x3p, ifsave = FALSE, dir_name = NULL, ifplot = FALSE) {
assert_that(
"x3p" %in% class(x3p),
is.flag(ifsave),
is.flag(ifplot)
)
if (ifsave) {
assert_that(
not_empty(dir_name), is.string(dir_name)
)
dir.create(dir_name, showWarnings = FALSE)
}

layer <-
x <-
y <-
value <-
. <-
NULL

if (ifsave) {
assert_that(not_empty(dir_name), msg = "dir_name must be non-empty")
assert_that(is.character(dir_name), msg = "dir_name must be character")
dir.create(dir_name, showWarnings = FALSE)
}

### Convert x3p to raster
x3p_inner_nomiss_res_raster <- t(x3p$surface.matrix) %>%
raster(xmx = (x3p$header.info$sizeX - 1) * x3p$header.info$incrementX, ymx = (x3p$header.info$sizeY - 1) * x3p$header.info$incrementY)
Expand Down
9 changes: 9 additions & 0 deletions R/x3p_insidepoly_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#' @importFrom stats sd
#' @importFrom raster raster adjacent ncell
#' @importFrom ggplot2 ggplot geom_raster scale_fill_gradient2 labs ggtitle geom_boxplot
#' @importFrom assertthat assert_that is.string is.number is.count is.flag
#' @export
#' @examples
#' x3p <- x3p_subsamples[[1]]
Expand All @@ -30,6 +31,14 @@
#'
x3p_insidepoly_df <- function(x3p, mask_col = "#FF0000", concavity = 1.5, b = 10,
ifplot = FALSE) {
assert_that(
"x3p" %in% class(x3p),
is.string(mask_col),
is.number(concavity), concavity > 0,
is.count(b),
is.flag(ifplot)
)

to <-
from <-
neighbor_val <-
Expand Down
8 changes: 8 additions & 0 deletions R/x3p_quantile_angle_vec.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#' @importFrom raster raster
#' @importFrom imager as.cimg hough_line nfline
#' @importFrom stats quantile median
#' @importFrom assertthat assert_that is.count is.number is.flag
#' @export
#' @examples
#' x3p <- x3p_subsamples[[1]]
Expand All @@ -24,6 +25,13 @@
#'
x3p_quantile_angle_vec <- function(x3p, ntheta = 720, min_score_cut = 0.1,
ifplot = FALSE) {
assert_that(
"x3p" %in% class(x3p),
is.count(ntheta),
is.number(min_score_cut),
is.flag(ifplot)
)

theta <-
theta_mod <-
theta_mod_shift <-
Expand Down
7 changes: 7 additions & 0 deletions R/x3p_raw_sig_vec.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' @importFrom x3ptools x3p_to_df
#' @importFrom ggplot2 ggplot aes geom_line
#' @importFrom stats na.omit median
#' @importFrom assertthat assert_that is.flag
#' @export
#' @examples
#' x3p <- x3p_subsamples[[2]]
Expand All @@ -22,6 +23,12 @@
#' str()
#'
x3p_raw_sig_vec <- function(x3p, method = "median", ifplot = FALSE) {
assert_that(
"x3p" %in% class(x3p),
method %in% c("median", "mean"),
is.flag(ifplot)
)

x <-
value <-
value_summary <-
Expand Down
8 changes: 8 additions & 0 deletions R/x3p_shift_sig_vec.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#' @importFrom stats na.omit median lm coef approx
#' @importFrom purrr map_dbl map set_names
#' @importFrom tidyr nest unnest
#' @importFrom assertthat assert_that is.flag
#' @export
#' @examples
#' x3p <- x3p_subsamples[[2]]
Expand All @@ -28,6 +29,13 @@
#' }
#'
x3p_shift_sig_vec <- function(x3p, method = "median", ifplot = FALSE, delta = -5:5) {
assert_that(
"x3p" %in% class(x3p),
method %in% c("median", "mean"),
is.flag(ifplot),
is.numeric(delta), length(delta) >= 3
)

y <-
value_nobs <-
x <-
Expand Down
10 changes: 8 additions & 2 deletions R/x3p_surface_polygon.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @param x3p x3p file
#' @param colour colour for the polygon
#' @param sample strictly positive integer value specifying the amount of
#' downsampling to be used (see `x3p_sample`) to calculate the boundary points
#' downsampling to be used (see `x3ptools::x3p_sample`) to calculate the boundary points
#' of the polygon. Larger values of `sample` will result in faster response times
#' but results in less accurate polygons.
#' @param center point on the center of the scan. If NULL, a center will be
Expand All @@ -15,6 +15,7 @@
#' @importFrom png readPNG
#' @importFrom grDevices as.raster dev.off
#' @importFrom graphics par plot.default polygon
#' @importFrom assertthat assert_that is.string is.count is.number
#' @export
#' @examples
#' if (interactive()) {
Expand All @@ -24,7 +25,12 @@
#' }
#'
x3p_surface_polygon <- function(x3p, colour = "red", sample = 10, center = NULL, concavity = 1.5) {
stopifnot("x3p" %in% class(x3p), is.numeric(concavity), concavity > 0)
assert_that(
"x3p" %in% class(x3p),
is.string(colour),
is.count(sample),
is.number(concavity), concavity > 0
)

boundary <- x3p %>% x3p_boundary_points(sample = sample)
polygon_inside <- inside_polygon(boundary$x, boundary$y,
Expand Down
19 changes: 15 additions & 4 deletions R/x3p_vertical.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#' @return x3p object after rotation with vertical striations
#' @import dplyr
#' @importFrom x3ptools x3p_bin_stripes x3p_extract x3p_rotate
#' @importFrom assertthat assert_that is.count is.number is.flag is.number
#' @export
#' @examples
#' x3p <- x3p_subsamples[[1]]
Expand All @@ -27,10 +28,20 @@
#' }
#'
x3p_vertical <- function(x3p_inner_impute, freqs = c(0, 0.3, 0.7, 1),
method = "MLE",
ntheta = 720, min_score_cut = 0.1,
ifplot = FALSE,
loess_span = 0.2) {
method = "MLE",
ntheta = 720, min_score_cut = 0.1,
ifplot = FALSE,
loess_span = 0.2) {
assert_that(
"x3p" %in% class(x3p_inner_impute),
is.numeric(freqs), near(length(freqs), 4), near(freqs[1], 0), near(freqs[4], 1),
method %in% c("MLE", "quantile"),
is.count(ntheta),
is.number(min_score_cut),
is.flag(ifplot),
is.number(loess_span), loess_span > 0
)

x3p_bin <- x3p_inner_impute %>%
x3p_bin_stripes(
direction = "vertical",
Expand Down
Loading

0 comments on commit 0768e2d

Please sign in to comment.