forked from derek-corcoran-barrios/NetworkExtinction
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parallel progress bar on RandomExtinction
- Loading branch information
Showing
4 changed files
with
22 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
#' | ||
|
@@ -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. | ||
#' | ||
|
@@ -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]> | ||
|
@@ -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 | ||
|
@@ -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{ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.