From cac9baa55d549e910be64b30ea10fe895a893e53 Mon Sep 17 00:00:00 2001 From: Sima Najafzadehkhoei Date: Tue, 10 Dec 2024 14:55:49 -0700 Subject: [PATCH] yaml is updated --- .github/workflows/R-CMD-check.yaml | 35 +---------- R/run_simulations.R | 95 +++++++++++++++--------------- vignettes/calibrate.Rmd | 2 + 3 files changed, 52 insertions(+), 80 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 3ab71de..1e16cb2 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -1,32 +1,3 @@ -jobs: - R-CMD-check: - runs-on: ${{ matrix.config.os }} - name: ${{ matrix.config.os }} (${{ matrix.config.r }}) - strategy: - fail-fast: false - matrix: - config: - - {os: macos-12, r: 'release'} - - {os: windows-latest, r: 'release', build-vignettes: false} - - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} - - {os: ubuntu-latest, r: 'release'} - - {os: ubuntu-latest, r: 'oldrel-1'} - steps: - - uses: actions/checkout@v4 - - - uses: r-lib/actions/setup-r@v2 - with: - r-version: ${{ matrix.config.r }} - - - uses: r-lib/actions/setup-r-dependencies@v2 - with: - extra-packages: any::rcmdcheck - needs: check - - - uses: r-lib/actions/check-r-package@v2 - with: - build_args: ${{ matrix.config.build-vignettes && '--compact-vignettes=gs+qpdf' || '--no-build-vignettes' }} - on: push: branches: [main] @@ -44,7 +15,7 @@ jobs: fail-fast: false matrix: config: - - {os: macos-12, r: 'release'} + - {os: macos-latest, r: 'release'} - {os: windows-latest, r: 'release'} - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} - {os: ubuntu-latest, r: 'release'} @@ -53,7 +24,8 @@ jobs: env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} R_KEEP_PKG_SOURCE: yes - steps: + + steps: - uses: actions/checkout@v4 - uses: r-lib/actions/setup-pandoc@v2 @@ -83,4 +55,3 @@ jobs: with: upload-snapshots: true build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")' - diff --git a/R/run_simulations.R b/R/run_simulations.R index 2e9e3c3..2a9c6ac 100644 --- a/R/run_simulations.R +++ b/R/run_simulations.R @@ -15,59 +15,58 @@ run_simulations <- function(N, n, ndays, ncores, theta, seeds) { library(epiworldR) - matrices <- parallel::mclapply(1:N, FUN = function(i) { - set.seed(seeds[i]) - m <- epiworldR:: ModelSIRCONN( - "mycon", - prevalence = theta$preval[i], - contact_rate = theta$crate[i], - transmission_rate = theta$ptran[i], - recovery_rate = theta$prec[i], - n = n - ) + library(parallel) - verbose_off(m) - run(m, ndays = ndays) - ans <- prepare_data(m,max_days=ndays) + # Detect the operating system + os_type <- .Platform$OS.type - return(ans) - }, mc.cores = ncores) + if (os_type == "windows") { + # Use parLapply for Windows + cl <- makeCluster(ncores) + on.exit(stopCluster(cl)) # Ensure the cluster is stopped after use - return(matrices) -} + clusterExport(cl, varlist = c("theta", "n", "ndays", "seeds", "prepare_data"), envir = environment()) + clusterEvalQ(cl, library(epiworldR)) # Load necessary libraries on workers + + matrices <- parLapply(cl, 1:N, function(i) { + set.seed(seeds[i]) + m <- epiworldR::ModelSIRCONN( + "mycon", + prevalence = theta$preval[i], + contact_rate = theta$crate[i], + transmission_rate = theta$ptran[i], + recovery_rate = theta$prec[i], + n = n + ) + + verbose_off(m) + run(m, ndays = ndays) + ans <- prepare_data(m, max_days = ndays) + return(ans) + }) + } else { + # Use mclapply for macOS/Linux + matrices <- mclapply(1:N, function(i) { + set.seed(seeds[i]) + m <- epiworldR::ModelSIRCONN( + "mycon", + prevalence = theta$preval[i], + contact_rate = theta$crate[i], + transmission_rate = theta$ptran[i], + recovery_rate = theta$prec[i], + n = n + ) -# run_simulations <- function(N, n, ndays, ncores, theta) { -# matrices <- parallel::mclapply(1:N, FUN = function(i) { -# fn <- sprintf("~/myfisrt.package/misc/simulated_data/sir-%06i.rds", i) -# -# if (file.exists(fn)) -# return(readRDS(fn)) -# seeds <- sample.int(.Machine$integer.max, N, TRUE) -# set.seed(seeds[i]) -# -# m <- ModelSIRCONN( -# "mycon", -# prevalence = theta$preval[i], -# contact_rate = theta$crate[i], -# transmission_rate = theta$ptran[i], -# recovery_rate = theta$prec[i], -# n = n -# ) -# -# verbose_off_and_run(m, ndays) -# ans <- prepare_data(m) -# saveRDS(ans, fn) -# -# return(ans) -# }, mc.cores = ncores) -# -# return(matrices) -# } -# run_simulations(2e4,5000,50,20,theta) -#path="~/myfisrt.package/misc/simulated_data/sir-%06i.rds" -# source("~/myfisrt.package/R/dataprep.R") -# Input dataset as a vector + verbose_off(m) + run(m, ndays = ndays) + ans <- prepare_data(m, max_days = ndays) + return(ans) + }, mc.cores = ncores) + } + + return(matrices) +} diff --git a/vignettes/calibrate.Rmd b/vignettes/calibrate.Rmd index 5c7660f..7f81bdc 100644 --- a/vignettes/calibrate.Rmd +++ b/vignettes/calibrate.Rmd @@ -81,6 +81,8 @@ head(seeds,5) Next, the function runs the simulations for the SIR model using the generated parameters and seeds and using epiworldR package: ```{r} +N=10 +n=5000 ndays=50 ncores=20 matrices <- run_simulations(N, n, ndays, ncores, theta, seeds)