Skip to content

Instantly share code, notes, and snippets.

@michaelhallquist
Created August 16, 2018 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelhallquist/530035827ba46d16900b9f5b0169fb45 to your computer and use it in GitHub Desktop.
Save michaelhallquist/530035827ba46d16900b9f5b0169fb45 to your computer and use it in GitHub Desktop.
How to use a progress bar within a foreach loop to keep an eye on the overall progress of a queue
#use row-wise iterator to avoid exporting the entire voxels x time matrix to each worker
library(tictoc)
library(utils)
message("Starting voxelwise ARIMA fitting")
pb <- txtProgressBar(0, max = nrow(toprocess), style = 3)
tic("arima fitting")
vox_results <- foreach(v=iter(toprocess, by="row"), .noexport="fmri_ts_data", .packages="forecast", .inorder=TRUE, .multicombine=TRUE, .combine=rbind) %dopar% {
it <- as.numeric(row.names(v)[1]) #use row number of matrix to set progress bar
if (it %% 100 == 0) { setTxtProgressBar(pb, it) }
vfit <- arimafit(v, search_pdq=search_pdq, max.p=max_p, max.d=max_d, max.q=max_q, fit.p=fit_p, fit.d=fit_d, fit.q=fit_q) #fit arima to voxel
arima_coef <- base_coef_vector #start with empty vector of max order for each model part
arima_coef[names(vfit$arima_coef)] <- vfit$arima_coef #populate non-zero elements of fitted ARMA coefficients
c(vfit$resid_ts, vfit$arima_order[c("p", "d", "q")], vfit$white_bg_pvals, arima_coef) #concatenating into a long vector rather than try to get 2+ objects out
}
toc()
close(pb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment