Skip to content

Commit

Permalink
Parallel progress bar on RandomExtinction
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikKusch committed Mar 13, 2023
1 parent 07a99ba commit 83f6f89
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ importFrom(broom,augment)
importFrom(broom,glance)
importFrom(broom,tidy)
importFrom(doParallel,registerDoParallel)
importFrom(doSNOW,registerDoSNOW)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_rows)
importFrom(dplyr,case_when)
Expand Down
21 changes: 14 additions & 7 deletions R/Extintions.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ SimulateExtinctions <- function(Network, Method, Order = NULL,
#' @param RewiringProb a numeric which identifies the threshold at which to assume rewiring potential is met.
#' @param verbose Logical. Whether to report on function progress or not.
#' @param RecalcConnect Logical or Numeric. Whether to recalculate connectedness of each node following each round of extinction simulation and subsequently update extinction order with newly mostconnected nodes.
#' @param forceFULL Logical. Whether to continue removal of nodes after initial order has been depleted. This will force the simulations to execute extinctions and check for secondary extinctions/new links until the network does not change anylonger.
#' @return exports list containing a data frame with the characteristics of the network after every extinction and a network object containing the final network. The resulting data frame contains 11 columns that incorporate the topological index, the secondary extinctions, predation release, and total extinctions of the network in each primary extinction.
#' @details When NetworkType = Trophic, secondary extinctions only occur for any predator, but not producers. If NetworkType = Mutualistic, secondary extinctions occur for all species in the network.
#'
Expand Down Expand Up @@ -570,7 +571,7 @@ ExtinctionOrder <- function(Network, Order, NetworkType = "Trophic", clust.metho
#' @param RewiringDist a numeric matrix of NxN dimension (N... number of nodes in Network). Contains, for example, phylogenetic or functional trait distances between nodes in Network which are used by the Rewiring argument to calculate rewiring probabilities. If Rewiring == function(x){x}, this matrix is expected to contain probabilities of a connection being present between species-pairs.
#' @param RewiringProb a numeric which identifies the threshold at which to assume rewiring potential is met.
#' @param verbose Logical. Whether to report on function progress or not.
#' #' @param forceFULL Logical. Whether to continue removal of nodes after initial order has been depleted. This will force the simulations to execute extinctions and check for secondary extinctions/new links until the network does not change anylonger.
#' @param forceFULL Logical. Whether to continue removal of nodes after initial order has been depleted. This will force the simulations to execute extinctions and check for secondary extinctions/new links until the network does not change anylonger.
#' @return exports list containing a data frame with the characteristics of the network after every extinction, a network object containing the final network, and a graph with the mean and 95percent interval. The resulting data frame contains 11 columns that incorporate the topological index, the secondary extinctions, predation release, and total extinctions of the network in each primary extinction.
#' @details When NetworkType = Trophic, secondary extinctions only occur for any predator, but not producers. If NetworkType = Mutualistic, secondary extinctions occur for all species in the network.
#'
Expand Down Expand Up @@ -624,6 +625,7 @@ ExtinctionOrder <- function(Network, Order, NetworkType = "Trophic", clust.metho
#' @importFrom utils txtProgressBar
#' @importFrom patchwork wrap_plots
#' @importFrom patchwork plot_annotation
#' @importFrom doSNOW registerDoSNOW


#' @author Derek Corcoran <[email protected]>
Expand All @@ -637,7 +639,7 @@ RandomExtinctions <- function(Network, nsim = 10,
parallel = FALSE, ncores,
IS = 0,
Rewiring = FALSE, RewiringDist = NULL, RewiringProb = 0.5,
verbose = TRUE){
verbose = TRUE, forceFULL = FALSE){
if(!NetworkType %in% c("Trophic", "Mutualistic")){stop("Please specify NetworkType as either 'Trophic' or 'Mutualistic'")}
## setting up objects
NumExt <- sd <- AccSecExt <- AccSecExt_95CI <- AccSecExt_mean <- Lower <- Upper <-NULL
Expand All @@ -649,21 +651,26 @@ RandomExtinctions <- function(Network, nsim = 10,
## simulations
if(verbose & !parallel){ProgBar <- txtProgressBar(max = nsim, style = 3)}
if(parallel){
cl <- makeCluster(ncores, outfile="test.txt")
registerDoParallel(cl)
cl <- makeCluster(ncores)
# registerDoParallel(cl)
registerDoSNOW(cl)
parallel::clusterExport(cl,
varlist = c("network", "SimNum", "IS", "Rewiring", "RewiringDist", "RewiringProb", "SimNum"),
varlist = c("network", "SimNum", "IS", "Rewiring", "RewiringDist", "RewiringProb", "SimNum", "forceFULL"),
envir = environment()
)
sims <- foreach(i=1:nsim, .packages = c("NetworkExtinction", "dplyr"))%dopar%{
pb <- txtProgressBar(max = nsim, style = 3)
progress <- function(i) setTxtProgressBar(pb, i)
opts <- list(progress = progress)

sims <- foreach(i=1:nsim, .options.snow = opts, .packages = c("NetworkExtinction", "dplyr"))%dopar%{
Order <- sample(1:network::network.size(network), size = SimNum)
sims1 <- try(ExtinctionOrder(Network = network,
Order = Order,
IS = IS, NetworkType = NetworkType,
Rewiring = Rewiring, RewiringDist = RewiringDist,
verbose = FALSE, RewiringProb = RewiringProb), silent = TRUE)
try({sims1$sims$simulation <- i}, silent = TRUE)
sims1
return(sims1)
}
stopCluster(cl)
}else{
Expand Down
2 changes: 2 additions & 0 deletions man/ExtinctionOrder.Rd

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

8 changes: 5 additions & 3 deletions man/RandomExtinctions.Rd

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

0 comments on commit 83f6f89

Please sign in to comment.