Skip to content

Commit

Permalink
Add examples and fix errors from .data
Browse files Browse the repository at this point in the history
  • Loading branch information
YuhangTom committed Sep 24, 2023
1 parent 8019802 commit a5b65a8
Show file tree
Hide file tree
Showing 20 changed files with 300 additions and 38 deletions.
21 changes: 17 additions & 4 deletions R/df_rmtrend_x3p.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@
#' @import dplyr
#' @importFrom x3ptools df_to_x3p
#' @importFrom stats lm predict
#' @importFrom rlang .data
#' @export

#' @examples
#' x3p <- x3p_subsamples[[1]]
#' mask_col <- "#FF0000"
#' concavity <- 1.5
#'
#' insidepoly_df <- x3p_insidepoly_df(x3p, mask_col = mask_col, concavity = concavity)
#'
#' x3p_inner_nomiss_res <- df_rmtrend_x3p(insidepoly_df)
#' x3p_inner_nomiss_res
#'
#' if (interactive()) {
#' x3p_image_autosize(x3p_inner_nomiss_res)
#' }
#'
df_rmtrend_x3p <- function(insidepoly_df) {
n_neighbor_val_miss <-
value <-
x <-
y <-
. <-
NULL

x3p_inner_nomiss_df <- insidepoly_df %>%
Expand All @@ -22,11 +35,11 @@ df_rmtrend_x3p <- function(insidepoly_df) {
### Remove trend
x3p_inner_nomiss_lm <- lm(value ~ x + y + I(x^2) + I(y^2) + x:y, data = x3p_inner_nomiss_df)
x3p_inner_nomiss_res_df <- x3p_inner_nomiss_df %>%
mutate(value = value - predict(x3p_inner_nomiss_lm, select(.data, x, y)))
mutate(value = value - predict(x3p_inner_nomiss_lm, select(., x, y)))

### Convert df to x3p
x3p_inner_nomiss_res <- x3p_inner_nomiss_res_df %>%
left_join(insidepoly_df[, c("x", "y")], .data) %>%
left_join(insidepoly_df[, c("x", "y")], ., by = join_by(x, y)) %>%
df_to_x3p()

return(x3p_inner_nomiss_res)
Expand Down
14 changes: 13 additions & 1 deletion R/vec_align_sigs_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@
#' @importFrom ggplot2 ggplot aes geom_line labs xlab ggtitle
#' @importFrom bulletxtrctr sig_align
#' @export

#' @examples
#' x3p <- x3p_subsamples[[1]]
#' mask_col <- "#FF0000"
#' concavity <- 1.5
#'
#' insidepoly_df <- x3p_insidepoly_df(x3p, mask_col = mask_col, concavity = concavity)
#' x3p_inner_nomiss_res <- df_rmtrend_x3p(insidepoly_df)
#' x3p_inner_impute <- x3p_impute(x3p_inner_nomiss_res, x3p, mask_col = mask_col,
#' concavity = concavity, ifsave = FALSE, dir_name = NULL, ifplot = FALSE)
#'
#' x3p_bin_rotate <- x3p_vertical(x3p_inner_impute, min_score_cut = 0.1)
#' vec_align_sigs_list(x3p_raw_sig_vec(x3p_bin_rotate), x3p_shift_sig_vec(x3p_bin_rotate))
#'
vec_align_sigs_list <- function(
sig1,
sig2,
Expand Down
20 changes: 16 additions & 4 deletions R/x3p_MLE_angle_vec.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,37 @@
#' @importFrom raster raster
#' @importFrom imager as.cimg hough_line nfline
#' @importFrom stats loess predict
#' @importFrom rlang .data
#' @export

x3p_MLE_angle_vec <- function(x3p, ntheta = 720, min_score_cut = 2,
#' @examples
#' x3p <- x3p_subsamples[[1]]
#' mask_col <- "#FF0000"
#' concavity <- 1.5
#'
#' insidepoly_df <- x3p_insidepoly_df(x3p, mask_col = mask_col, concavity = concavity)
#' x3p_inner_nomiss_res <- df_rmtrend_x3p(insidepoly_df)
#' x3p_inner_impute <- x3p_impute(x3p_inner_nomiss_res, x3p, mask_col = mask_col,
#' concavity = concavity, ifsave = FALSE, dir_name = NULL, ifplot = FALSE)
#'
#' x3p_MLE_angle_vec(x3p_inner_impute, min_score_cut = 0.1)
#'
#'
x3p_MLE_angle_vec <- function(x3p, ntheta = 720, min_score_cut = 0.1,
ifplot = FALSE,
loess_span = 0.2) {
theta <-
score <-
theta_mod <-
theta_mod_shift <-
rho <-
. <-
NULL

### Change to contrast color
x3p_shift <- x3p$surface.matrix
NA_val <- -(x3p$surface.matrix %>%
c() %>%
summary() %>%
.data[c("Min.", "Max.")] %>%
.[c("Min.", "Max.")] %>%
abs() %>%
max() %>%
ceiling())
Expand Down
7 changes: 6 additions & 1 deletion R/x3p_image_autosize.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
#' @importFrom rgl rglwidget
#' @importFrom htmltools tagList
#' @export

#' @examples
#' x3p <- x3p_subsamples[[1]]
#' if (interactive()) {
#' x3p_image_autosize(x3p)
#' }
#'
x3p_image_autosize <- function(x3p, ifhtml = FALSE, zoom = 0.6, ...) {
x3p_image(x3p, size = dim(x3p$surface.matrix), zoom = zoom, ...)

Expand Down
32 changes: 24 additions & 8 deletions R/x3p_impute.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#'
#' Obtained x3p object after imputing the inner polygon.
#' @param x3p x3p object
#' @param x3p_mask x3p object for mask
#' @param mask_col colour for the polygon
#' @param concavity strictly positive value used in \code{concaveman::concaveman}
#' @param ifsave whether the imputation procedure gif is going to be saved
Expand All @@ -17,15 +18,30 @@
#' @importFrom magick image_read image_join image_animate image_write
#' @importFrom stringr str_detect
#' @importFrom wires x3p_surface_polygon
#' @importFrom rlang .data
#' @export

x3p_impute <- function(x3p, mask_col = "#FF0000", concavity = 1.5,
ifsave = FALSE, dir_name = NULL, ifplot = FALSE) {
#' @examples
#' x3p <- x3p_subsamples[[1]]
#' mask_col <- "#FF0000"
#' concavity <- 1.5
#'
#' insidepoly_df <- x3p_insidepoly_df(x3p, mask_col = mask_col, concavity = concavity)
#' x3p_inner_nomiss_res <- df_rmtrend_x3p(insidepoly_df)
#'
#' x3p_inner_impute <- x3p_impute(x3p_inner_nomiss_res, x3p, mask_col = mask_col,
#' concavity = concavity, ifsave = FALSE, dir_name = NULL, ifplot = FALSE)
#' x3p_inner_impute
#'
#' if (interactive()) {
#' x3p_image_autosize(x3p_inner_impute)
#' }
#'
x3p_impute <- function(x3p, x3p_mask, mask_col = "#FF0000",
concavity = 1.5, ifsave = FALSE, dir_name = NULL, ifplot = FALSE) {
layer <-
x <-
y <-
value <-
. <-
NULL

if (ifsave) {
Expand Down Expand Up @@ -139,21 +155,21 @@ x3p_impute <- function(x3p, mask_col = "#FF0000", concavity = 1.5,
path = dir_name,
full.names = TRUE
) %>%
.data[str_detect(.data, pattern = ".png")] %>%
.[str_detect(., pattern = ".png")] %>%
file.remove() %>%
invisible()
}

x3p_inner_focal_impute <- x3p %>%
x3p_inner_focal_impute <- x3p_mask %>%
x3p_delete_mask()
x3p_inner_focal_impute$surface.matrix <- x3p_inner_nomiss_res_focal_raster %>%
as.matrix() %>%
t()

x3p <- x3p %>%
x3p_mask <- x3p_mask %>%
x3p_surface_polygon(colour = mask_col, concavity = concavity)
### Extract inner part as x3p based on mask
x3p_inner <- x3p_extract(x3p, mask_vals = mask_col) %>%
x3p_inner <- x3p_extract(x3p_mask, mask_vals = mask_col) %>%
x3p_average(m = 3, na.rm = TRUE)
x3p_inner_focal_impute <- x3p_add_mask(x3p_inner_focal_impute, x3p_inner$mask)

Expand Down
19 changes: 14 additions & 5 deletions R/x3p_insidepoly_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@
#' @importFrom stats sd
#' @importFrom raster raster adjacent ncell
#' @importFrom wires x3p_surface_polygon
#' @importFrom rlang .data
#' @export

#' @examples
#' x3p <- x3p_subsamples[[1]]
#' mask_col <- "#FF0000"
#' concavity <- 1.5
#'
#' insidepoly_df <- x3p_insidepoly_df(x3p, mask_col = mask_col, concavity = concavity)
#' str(insidepoly_df)
#'
x3p_insidepoly_df <- function(x3p, mask_col = "#FF0000", concavity = 1.5) {
to <-
from <-
neighbor_val <-
x <-
y <-
n_neighbor_val_miss <-
. <-
NULL

x3p <- x3p %>%
Expand Down Expand Up @@ -89,10 +96,11 @@ x3p_insidepoly_df <- function(x3p, mask_col = "#FF0000", concavity = 1.5) {
y = x3p_inner_df$y,
x = as.character(x),
y = as.character(y)
)
),
by = join_by(x, y)
) %>%
full_join(
.data,
x = .,
x3p_inner_df_wide_sd_not_miss %>%
pivot_longer(
cols = everything(),
Expand All @@ -103,7 +111,8 @@ x3p_insidepoly_df <- function(x3p, mask_col = "#FF0000", concavity = 1.5) {
y = x3p_inner_df$y,
x = as.character(x),
y = as.character(y)
)
),
by = join_by(x, y)
) %>%
mutate(
x = as.numeric(x),
Expand Down
20 changes: 16 additions & 4 deletions R/x3p_quantile_angle_vec.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,28 @@
#' @importFrom raster raster
#' @importFrom imager as.cimg hough_line nfline
#' @importFrom stats quantile median
#' @importFrom rlang .data
#' @export

x3p_quantile_angle_vec <- function(x3p, ntheta = 720, min_score_cut = 2,
#' @examples
#' x3p <- x3p_subsamples[[1]]
#' mask_col <- "#FF0000"
#' concavity <- 1.5
#'
#' insidepoly_df <- x3p_insidepoly_df(x3p, mask_col = mask_col, concavity = concavity)
#' x3p_inner_nomiss_res <- df_rmtrend_x3p(insidepoly_df)
#' x3p_inner_impute <- x3p_impute(x3p_inner_nomiss_res, x3p, mask_col = mask_col,
#' concavity = concavity, ifsave = FALSE, dir_name = NULL, ifplot = FALSE)
#'
#' x3p_quantile_angle_vec(x3p_inner_impute, min_score_cut = 0.1)
#'
#'
x3p_quantile_angle_vec <- function(x3p, ntheta = 720, min_score_cut = 0.1,
ifplot = FALSE) {
theta <-
theta_mod <-
theta_mod_shift <-
score <-
rho <-
. <-
NULL

### Change to contrast color
Expand All @@ -29,7 +41,7 @@ x3p_quantile_angle_vec <- function(x3p, ntheta = 720, min_score_cut = 2,
-(x3p$surface.matrix %>%
c() %>%
summary() %>%
.data[c("Min.", "Max.")] %>%
.[c("Min.", "Max.")] %>%
abs() %>%
max() %>%
ceiling())
Expand Down
17 changes: 15 additions & 2 deletions R/x3p_raw_sig_vec.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@
#' @importFrom ggplot2 ggplot aes geom_line
#' @importFrom stats na.omit median
#' @export

#' @examples
#' x3p <- x3p_subsamples[[1]]
#' mask_col <- "#FF0000"
#' concavity <- 1.5
#'
#' insidepoly_df <- x3p_insidepoly_df(x3p, mask_col = mask_col, concavity = concavity)
#' x3p_inner_nomiss_res <- df_rmtrend_x3p(insidepoly_df)
#' x3p_inner_impute <- x3p_impute(x3p_inner_nomiss_res, x3p, mask_col = mask_col,
#' concavity = concavity, ifsave = FALSE, dir_name = NULL, ifplot = FALSE)
#'
#' x3p_bin_rotate <- x3p_vertical(x3p_inner_impute, min_score_cut = 0.1)
#' x3p_raw_sig_vec(x3p_bin_rotate) %>%
#' str()
#'
x3p_raw_sig_vec <- function(x3p, method = "median", ifplot = FALSE) {
x <-
value <-
Expand All @@ -36,5 +49,5 @@ x3p_raw_sig_vec <- function(x3p, method = "median", ifplot = FALSE) {
print()
}

return(sig)
return(sig$value_summary)
}
21 changes: 17 additions & 4 deletions R/x3p_shift_sig_vec.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,20 @@
#' @importFrom purrr map_dbl map set_names
#' @importFrom tidyr nest unnest
#' @export

#' @examples
#' x3p <- x3p_subsamples[[1]]
#' mask_col <- "#FF0000"
#' concavity <- 1.5
#'
#' insidepoly_df <- x3p_insidepoly_df(x3p, mask_col = mask_col, concavity = concavity)
#' x3p_inner_nomiss_res <- df_rmtrend_x3p(insidepoly_df)
#' x3p_inner_impute <- x3p_impute(x3p_inner_nomiss_res, x3p, mask_col = mask_col,
#' concavity = concavity, ifsave = FALSE, dir_name = NULL, ifplot = FALSE)
#'
#' x3p_bin_rotate <- x3p_vertical(x3p_inner_impute, min_score_cut = 0.1)
#' x3p_shift_sig_vec(x3p_bin_rotate) %>%
#' str()
#'
x3p_shift_sig_vec <- function(x3p, method = "median", ifplot = FALSE, delta = -5:5) {
y <-
value_nobs <-
Expand All @@ -38,7 +51,7 @@ x3p_shift_sig_vec <- function(x3p, method = "median", ifplot = FALSE, delta = -5
arrange(value_nobs)

### Sort unique y values
y_sort <- inner_join(x3p_df, x3p_df_nobs) %>%
y_sort <- inner_join(x3p_df, x3p_df_nobs, by = join_by(y)) %>%
### Filter to have at least 2 observations for approx later
filter(value_nobs >= 2) %>%
distinct(y) %>%
Expand Down Expand Up @@ -119,7 +132,7 @@ x3p_shift_sig_vec <- function(x3p, method = "median", ifplot = FALSE, delta = -5
)

### Shift x values
x3p_shift_delta_df <- inner_join(x3p_df, x_shift_delta_value_df, by = "y") %>%
x3p_shift_delta_df <- inner_join(x3p_df, x_shift_delta_value_df, by = join_by(y)) %>%
mutate(x_shift_delta = x + x_shift_delta_value)

if (ifplot) {
Expand Down Expand Up @@ -188,5 +201,5 @@ x3p_shift_sig_vec <- function(x3p, method = "median", ifplot = FALSE, delta = -5
print()
}

sig
return(sig$value_summary)
}
20 changes: 18 additions & 2 deletions R/x3p_vertical.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,26 @@
#' @import dplyr
#' @importFrom x3ptools x3p_bin_stripes x3p_extract x3p_rotate
#' @export

#' @examples
#' x3p <- x3p_subsamples[[1]]
#' mask_col <- "#FF0000"
#' concavity <- 1.5
#'
#' insidepoly_df <- x3p_insidepoly_df(x3p, mask_col = mask_col, concavity = concavity)
#' x3p_inner_nomiss_res <- df_rmtrend_x3p(insidepoly_df)
#' x3p_inner_impute <- x3p_impute(x3p_inner_nomiss_res, x3p, mask_col = mask_col,
#' concavity = concavity, ifsave = FALSE, dir_name = NULL, ifplot = FALSE)
#'
#' x3p_bin_rotate <- x3p_vertical(x3p_inner_impute, min_score_cut = 0.1)
#' x3p_bin_rotate
#'
#' if (interactive()) {
#' x3p_image_autosize(x3p_bin_rotate)
#' }
#'
x3p_vertical <- function(x3p_inner_impute, freqs = c(0, 0.3, 0.7, 1),
method = "MLE",
ntheta = 720, min_score_cut = 2,
ntheta = 720, min_score_cut = 0.1,
ifplot = FALSE,
loess_span = 0.2) {
x3p_bin <- x3p_inner_impute %>%
Expand Down
Loading

0 comments on commit a5b65a8

Please sign in to comment.