Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap Julia scripts into module #9

Merged
merged 13 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
^doc$
^Meta$
^inst/julia/Manifest.toml$
^inst/julia/JlmerClusterPerm/Manifest.toml$
^format/
34 changes: 34 additions & 0 deletions .github/workflows/julia-style.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Julia-Style
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
on:
push:
branches:
- 'main'
tags: '*'
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
jobs:
format-check:
name: Style Enforcement (Julia ${{ matrix.julia-version }} - ${{ github.event_name }})
# Run on push's or non-draft PRs
if: (github.event_name == 'push') || (github.event.pull_request.draft == false)
runs-on: ubuntu-latest
strategy:
matrix:
julia-version: [1.8]
steps:
- uses: julia-actions/setup-julia@latest
with:
version: ${{ matrix.julia-version }}
- uses: actions/checkout@v1
- name: Instantiate `format` environment and format
run: |
julia --project=format -e 'using Pkg; Pkg.instantiate(); Pkg.resolve()'
julia --project=format 'format/run.jl'
- uses: reviewdog/action-suggester@v1
if: github.event_name == 'pull_request'
with:
tool_name: JuliaFormatter
fail_on_error: true
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
paths: ["**.[rR]", "**.[qrR]md", "**.[rR]markdown", "**.[rR]nw", "**.[rR]profile"]

name: Style
name: R-Style

jobs:
style:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
.httr-oauth
.DS_Store
inst/julia/Manifest.toml
inst/julia/JlmerClusterPerm/Manifest.toml
format/Manifest.toml$
^CRAN-SUBMISSION$
inst/doc
/doc/
Expand Down
51 changes: 25 additions & 26 deletions R/aaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ setup_with_progress <- function(..., verbose = TRUE) {
start_with_threads(verbose = verbose)
set_projenv(verbose = verbose)
source_jl(verbose = verbose)
invisible(TRUE)
}

start_with_threads <- function(..., max_threads = 7, verbose = TRUE) {
Expand All @@ -79,6 +80,7 @@ start_with_threads <- function(..., max_threads = 7, verbose = TRUE) {
suppressMessages(JuliaConnectoR::startJuliaServer())
}
.jlmerclusterperm$opts$nthreads <- nthreads
invisible(TRUE)
}

set_projenv <- function(..., verbose = TRUE) {
Expand All @@ -88,44 +90,41 @@ set_projenv <- function(..., verbose = TRUE) {
JuliaConnectoR::juliaCall("cd", pkgdir)
JuliaConnectoR::juliaEval("using Pkg")
JuliaConnectoR::juliaEval('Pkg.activate(".", io = devnull)')
JuliaConnectoR::juliaEval('Pkg.develop(path = "JlmerClusterPerm", io = devnull)')
JuliaConnectoR::juliaEval("Pkg.instantiate()") # io = devnull
JuliaConnectoR::juliaEval("Pkg.resolve(io = devnull)")
JuliaConnectoR::juliaCall("cd", getwd())
JuliaConnectoR::juliaEval(paste0("pg_width = ", max(1L, cli::console_width() - 44L)))
JuliaConnectoR::juliaEval("pg_io = stderr")
JuliaConnectoR::juliaEval(paste0("using Random123; const rng = Threefry2x((", seed, ", 20))"))
.jlmerclusterperm$opts$seed <- seed
.jlmerclusterperm$opts$pkgdir <- pkgdir
define_globals()
invisible(TRUE)
}

define_globals <- function(...) {
JuliaConnectoR::juliaEval(sprintf(
"pg = Dict(:width => %i, :io => stderr);
using Random123;
const rng = Threefry2x((%i, 20));",
max(1L, cli::console_width() - 44L),
as.integer(.jlmerclusterperm$opts$seed)
))
.jlmerclusterperm$get_jl_opts <- function(x) {
list(JuliaConnectoR::juliaEval("(pg = pg, rng = rng)"))
}
invisible(TRUE)
}

source_jl <- function(..., verbose = TRUE) {
jl_pkgs <- readLines(file.path(.jlmerclusterperm$opts$pkgdir, "load-pkgs.jl"))
jl_scripts <- list.files(.jlmerclusterperm$opts$pkgdir, pattern = "\\d{2}-.*\\.jl$", full.names = TRUE)
load_steps <- c(jl_pkgs, jl_scripts)
i <- 1L
if (verbose) cli::cli_progress_step("Running package setup scripts ({i}/{length(load_steps)})")
for (i in seq_along(load_steps)) {
if (verbose) cli::cli_progress_step("Running package setup scripts ({i}/{length(jl_pkgs) + 1})")
for (i in (seq_along(jl_pkgs) + 1)) {
if (verbose) cli::cli_progress_update()
jl_load <- load_steps[i]
if (grepl("^using ", jl_load)) {
JuliaConnectoR::juliaEval(jl_load)
if (i <= length(jl_pkgs)) {
JuliaConnectoR::juliaEval(jl_pkgs[i])
} else {
JuliaConnectoR::juliaCall("include", jl_load)
.jlmerclusterperm$jl <- JuliaConnectoR::juliaImport("JlmerClusterPerm")
}
}
exported_fns <- c(
"jlmer", "compute_timewise_statistics",
"extract_clusters", "permute_timewise_statistics",
"guess_shuffle_as", "permute_by_predictor"
)
for (jl_fn in exported_fns) {
.jlmerclusterperm[[jl_fn]] <- JuliaConnectoR::juliaFun(jl_fn)
}
.jlmerclusterperm$exported_fns <- exported_fns
invisible(TRUE)
}

#' @keywords internal
dev_source <- function() { # nocov start
.jlmerclusterperm$opts$pkgdir <- system.file("julia/", package = "jlmerclusterperm")
source_jl()
} # nocov end
4 changes: 2 additions & 2 deletions R/compute_timewise_statistics.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ compute_timewise_statistics <- function(jlmer_spec, family = c("gaussian", "bino


out <- JuliaConnectoR::juliaGet(do.call(
.jlmerclusterperm$compute_timewise_statistics,
c(args, term_groups$jl, statistic, is_mem, opts)
.jlmerclusterperm$jl$compute_timewise_statistics,
c(args, term_groups$jl, statistic, is_mem, .jlmerclusterperm$get_jl_opts(), opts)
))

alert_diagnostics(jlmer_spec, out)
Expand Down
4 changes: 2 additions & 2 deletions R/extract_clusters.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ extract_empirical_clusters <- function(empirical_statistics, threshold, binned =
empirical_statistics <- apply_threshold(empirical_statistics, statistic, threshold)
predictors <- rownames(empirical_statistics)
n <- as.integer(max(min(top_n, ncol(empirical_statistics)), 1))
largest_clusters <- .jlmerclusterperm$extract_clusters(empirical_statistics, binned, n)
largest_clusters <- .jlmerclusterperm$jl$extract_clusters(empirical_statistics, binned, n)
cluster_dfs <- df_from_DF(largest_clusters)
empirical_clusters <- split(cluster_dfs[, -5], predictors[cluster_dfs$id])[predictors]
empirical_clusters <- lapply(empirical_clusters, function(cluster_df) {
Expand Down Expand Up @@ -125,7 +125,7 @@ extract_null_cluster_dists <- function(null_statistics, threshold, binned = FALS
null_statistics <- apply_threshold(null_statistics, statistic, threshold)
null_cluster_dists <- apply(null_statistics, 3, function(t_matrix) {
t_matrix <- t_matrix[!is.nan(rowSums(t_matrix)), ]
largest_clusters <- df_from_DF(.jlmerclusterperm$extract_clusters(t_matrix, binned, 1L))
largest_clusters <- df_from_DF(.jlmerclusterperm$jl$extract_clusters(t_matrix, binned, 1L))
}, simplify = FALSE)
structure(null_cluster_dists,
class = "null_cluster_dists",
Expand Down
6 changes: 3 additions & 3 deletions R/interop-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ julia_progress <- function(show, width) {
show_missing <- missing(show)
both_missing <- show_missing && missing(width)
opts_is_list <- !show_missing && is.list(show) && identical(names(show), c("show", "width"))
old_opts <- JuliaConnectoR::juliaGet(JuliaConnectoR::juliaEval("(show = !(pg_io isa Base.DevNull), width = pg_width)"))
old_opts <- JuliaConnectoR::juliaGet(JuliaConnectoR::juliaEval("(show = pg[:io] != devnull, width = pg[:width])"))
if (!show_missing) {
if (opts_is_list) {
width <- show$width
show <- show$show
}
JuliaConnectoR::juliaEval(paste0("pg_io = ", if (show) "stderr" else "devnull"))
JuliaConnectoR::juliaEval(paste0("pg[:io] = ", if (show) "stderr" else "devnull"))
}
if (!missing(width)) {
if (width == "auto") {
width <- max(1L, cli::console_width() - 44L)
}
JuliaConnectoR::juliaEval(paste0("pg_width = ", width))
JuliaConnectoR::juliaEval(paste0("pg[:width] = ", width))
}
out <- strip_JLTYPE(old_opts)
if (both_missing) {
Expand Down
2 changes: 1 addition & 1 deletion R/jlmer.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ jlmer <- function(jlmer_spec, family = c("gaussian", "binomial"), ..., progress
family <- match.arg(family)
args <- prep_for_jlmer(jlmer_spec, family = family, ...)[-3]

mod <- do.call(.jlmerclusterperm$jlmer, c(args, jlmer_spec$meta$is_mem, progress = progress, ...))
mod <- do.call(.jlmerclusterperm$jl$jlmer, c(args, jlmer_spec$meta$is_mem, progress = progress, ...))
structure(mod, class = c("jlmer_mod", class(mod)))
}

Expand Down
6 changes: 4 additions & 2 deletions R/permute.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ permute_by_predictor <- function(jlmer_spec, predictors, predictor_type = c("gue
time <- jlmer_spec$meta$time
predictor_type <- match.arg(predictor_type)
if (predictor_type == "guess") {
predictor_type <- .jlmerclusterperm$guess_shuffle_as(df_jl, predictors, subject, trial)
predictor_type <- .jlmerclusterperm$jl$guess_shuffle_as(df_jl, predictors, subject, trial)
cli::cli_alert_info("Guessed {.arg predictor_type} to be {.val {predictor_type}}")
}
predictor_group <- Filter(function(x) any(predictors %in% x), jlmer_spec$meta$term_groups)
Expand All @@ -81,7 +81,9 @@ permute_by_predictor <- function(jlmer_spec, predictors, predictor_type = c("gue
}
predictors <- predictor_group
}
shuffled <- df_from_DF(.jlmerclusterperm$permute_by_predictor(df_jl, predictor_type, predictors, subject, trial, as.integer(n)))
shuffled <- df_from_DF(.jlmerclusterperm$jl$permute_by_predictor(
df_jl, predictor_type, predictors, subject, trial, as.integer(n), .jlmerclusterperm$get_jl_opts()[[1]]
))
class(shuffled) <- class(df)
shuffled
}
11 changes: 3 additions & 8 deletions R/permute_timewise_statistics.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,11 @@ permute_timewise_statistics <- function(jlmer_spec, family = c("gaussian", "bino
}

out <- JuliaConnectoR::juliaGet(do.call(
.jlmerclusterperm$permute_timewise_statistics,
c(args, nsim, participant_col, trial_col, term_groups$jl, predictors_subset, statistic, is_mem, opts)
.jlmerclusterperm$jl$permute_timewise_statistics,
c(args, nsim, participant_col, trial_col, term_groups$jl,
predictors_subset, statistic, is_mem, .jlmerclusterperm$get_jl_opts(), opts)
))

# shuffle_predictor_groups <- Filter(function(x) !identical(x, "(Intercept)"), jlmer_spec$meta$term_groups)
# counter_states <- split(out$counter_states, rep(names(shuffle_predictor_groups), each = nsim))
# counter_states[] <- lapply(names(counter_states), function(x) {
# list(predictors = shuffle_predictor_groups[[x]], counter = counter_states[[x]])
# })

dimnames(out$z_array) <- list(
Sim = as.factor(zero_pad(1:nsim)),
Time = sort(unique(jlmer_spec$data[[jlmer_spec$meta$time]])),
Expand Down
3 changes: 2 additions & 1 deletion R/threshold_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
#' null_statistics <- permute_timewise_statistics(spec, nsim = 100)
#'
#' # Test cluster mass/probability under different threshold values
#' walk_threshold_steps(empirical_statistics, null_statistics, steps = 1:3)
#' walk_threshold_steps(empirical_statistics, null_statistics, steps = 1:3,
#' progress = FALSE)
#'
#' \dontshow{
#' JuliaConnectoR::stopJulia()
Expand Down
4 changes: 2 additions & 2 deletions docs/articles/Garrison-et-al-2020.html

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

6 changes: 3 additions & 3 deletions docs/articles/Geller-et-al-2020.html

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

4 changes: 2 additions & 2 deletions docs/articles/asynchronous-cpa.html

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

7 changes: 0 additions & 7 deletions docs/articles/julia-interface.html

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

Loading