Skip to content
Snippets Groups Projects
Commit 06787ffe authored by Maria Voigt's avatar Maria Voigt
Browse files

adding verbose to print, so that script can be quiet

parent 889b9cb5
No related branches found
No related tags found
No related merge requests found
...@@ -46,7 +46,9 @@ option_list <- list ( ...@@ -46,7 +46,9 @@ option_list <- list (
dest = "year_to_predict", dest = "year_to_predict",
type = "integer", type = "integer",
help = "year of the survey years (1994:2015) to predict abundance to", help = "year of the survey years (1994:2015) to predict abundance to",
metavar = "2015") metavar = "2015"),
make_option(c("-v", "--verbose"), dest = "verbose_script", action = "store_true",
default = TRUE, help = "print all intermediate results")
) )
options <- parse_args(OptionParser(option_list=option_list)) options <- parse_args(OptionParser(option_list=option_list))
...@@ -82,22 +84,30 @@ if (!(options$year_to_predict %in% year_to_predict_possibilities)) { ...@@ -82,22 +84,30 @@ if (!(options$year_to_predict %in% year_to_predict_possibilities)) {
} }
# is verbose?
is_verbose <- options$verbose_script
# input directory # input directory
indir <- options$input_directory indir <- options$input_directory
print(paste("indir", indir)) if(is_verbose){print(paste("indir", indir))}
# input directory # input directory
indir_predictors <- options$pred_input_directory indir_predictors <- options$pred_input_directory
print(paste("indir_predictors", indir_predictors)) if(is_verbose){print(paste("indir_predictors", indir_predictors))}
# directory in which output is written # directory in which output is written
outdir <- options$output_directory outdir <- options$output_directory
print(paste("outdir", outdir)) if(is_verbose){print(paste("outdir", outdir))}
year_to_predict <- as.numeric(options$year_to_predict ) year_to_predict <- as.numeric(options$year_to_predict )
print(paste("year to predict " , year_to_predict)) if(is_verbose){print(paste("year to predict " , year_to_predict))}
exclude_year <- as.numeric(options$exclude_year)
if(is_verbose){print(paste("exclude year", exclude_year))}
if(is_verbose){print(paste(Sys.time(), "0. start run"))}
#------------------------# #------------------------#
# command line arguments # # command line arguments #
#------------------------# #------------------------#
...@@ -113,11 +123,10 @@ crs_aea <- "+proj=aea +lat_1=7 +lat_2=-32 +lat_0=-15 +lon_0=125 +x_0=0 ...@@ -113,11 +123,10 @@ crs_aea <- "+proj=aea +lat_1=7 +lat_2=-32 +lat_0=-15 +lon_0=125 +x_0=0
#-------------------------------# #-------------------------------#
# Load functions # Load functions
source(file.path(indir_fun, "generic/path.to.current.R")) source(file.path(indir_fun, "generic/path.to.current.R"))
print("function loaded")
# Load coefficients and weights # Load coefficients and weights
abundMod_results_path <- path.to.current(indir, "abundMod_results", "rds" ) abundMod_results_path <- path.to.current(indir, "abundMod_results", "rds" )
print(paste("this is abundMod_results_path:", abundMod_results_path)) if(is_verbose){print(paste("this is abundMod_results_path:", abundMod_results_path))}
abundMod_results <- readRDS(abundMod_results_path) abundMod_results <- readRDS(abundMod_results_path)
# exclude the first column, which contains models, exclude the coefficient of the # exclude the first column, which contains models, exclude the coefficient of the
# autocorellation term and the weighted aic of the model # autocorellation term and the weighted aic of the model
...@@ -151,21 +160,20 @@ predictor_names <- predictor_names_coeffs[!grepl("I(*)", predictor_names_coeffs) ...@@ -151,21 +160,20 @@ predictor_names <- predictor_names_coeffs[!grepl("I(*)", predictor_names_coeffs)
#----------------------------# #----------------------------#
# Load and prepare estimates # # Load and prepare estimates #
#----------------------------# #----------------------------#
print(paste("these are predictor names: ", predictor_names)) if(is_verbose){print(paste("these are predictor names: ", predictor_names))}
predictors_path <- path.to.current(indir_predictors, paste0("predictors_abundance_", predictors_path <- path.to.current(indir_predictors, paste0("predictors_abundance_",
year_to_predict),"rds") year_to_predict),"rds")
print(paste("this is predictors path", predictors_path)) if(is_verbose){print(paste("this is predictors path", predictors_path))}
predictors <- readRDS(predictors_path) %>% predictors <- readRDS(predictors_path) %>%
dplyr::filter(predictor %in% predictor_names) %>% dplyr::filter(predictor %in% predictor_names) %>%
dcast(id + z_year ~ predictor, value.var = "scaled_value") dcast(id + z_year ~ predictor, value.var = "scaled_value")
predictors$year <- predictors$z_year predictors$year <- predictors$z_year
predictors$z_year <- NULL predictors$z_year <- NULL
str(predictors) if(is_verbose){str(predictors)}
if(is_verbose){print(paste("this is nrow predictors", nrow(predictors)))}
print(paste("this is nrow predictors", nrow(predictors)))
#--------------------------# #--------------------------#
# PREDICTION FOR each year # # PREDICTION FOR each year #
#--------------------------# #--------------------------#
...@@ -188,7 +196,7 @@ names(predictor_estimates) <- c("intercept", predictor_names, ...@@ -188,7 +196,7 @@ names(predictor_estimates) <- c("intercept", predictor_names,
# but takes a bit longer (52s, to 35s for 100 rows) # but takes a bit longer (52s, to 35s for 100 rows)
## PLUS PAY ATTENTION, IF PREDICTIONS NOT SAME NROW--> VALUES GET RECYCLED ## PLUS PAY ATTENTION, IF PREDICTIONS NOT SAME NROW--> VALUES GET RECYCLED
print(paste("1. start pred_per_cell", Sys.time())) if(is_verbose){print(paste("1. start pred_per_cell", Sys.time()))}
pred_per_cell <- foreach(i = 1:nrow(predictor_estimates), .combine = c) %dopar% { pred_per_cell <- foreach(i = 1:nrow(predictor_estimates), .combine = c) %dopar% {
# pred_per_cell <- foreach(i = 1:100, .combine = c) %dopar% { # pred_per_cell <- foreach(i = 1:100, .combine = c) %dopar% {
t_predictor_estimates <- t( predictor_estimates[i, ]) t_predictor_estimates <- t( predictor_estimates[i, ])
...@@ -199,7 +207,7 @@ pred_estimates_sum <- apply(pred_estimates_wcoeffs, 1, sum) ...@@ -199,7 +207,7 @@ pred_estimates_sum <- apply(pred_estimates_wcoeffs, 1, sum)
return(exp(pred_estimates_calc)) return(exp(pred_estimates_calc))
} }
print(paste(Sys.time(), "2. finished dopar loop")) if(is_verbose){print(paste(Sys.time(), "2. finished dopar loop"))}
# is this correct -> ???? # is this correct -> ????
pred_per_cell <- as.data.frame(cbind(predictors$id, pred_per_cell)) pred_per_cell <- as.data.frame(cbind(predictors$id, pred_per_cell))
...@@ -208,14 +216,18 @@ names(pred_per_cell) <- c("id", "abundance_pred") ...@@ -208,14 +216,18 @@ names(pred_per_cell) <- c("id", "abundance_pred")
# exclude NAN # exclude NAN
pred_per_cell <- pred_per_cell[!is.nan(pred_per_cell$abundance_pred), ] pred_per_cell <- pred_per_cell[!is.nan(pred_per_cell$abundance_pred), ]
if (!is.na(exclude_year)){paste("year_to_exclude:", exclude_year)}
print(paste(Sys.time(), "sum predicted for ", year_to_predict, print(paste(Sys.time(), "sum predicted for ", year_to_predict,
sum(pred_per_cell$abundance_pred))) sum(pred_per_cell$abundance_pred)))
print(paste(Sys.time(), "range predicted for ", year_to_predict, print(paste(Sys.time(),
"range predicted for ", year_to_predict,
range(pred_per_cell$abundance_pred))) range(pred_per_cell$abundance_pred)))
print(paste(Sys.time(), "density predicted for ", year_to_predict, print(paste(Sys.time(),
"density predicted for ", year_to_predict,
mean(pred_per_cell$abundance_pred))) mean(pred_per_cell$abundance_pred)))
print(paste(Sys.time(), "nr of values over 10 ", year_to_predict, print(paste(Sys.time(),
"nr of values over 10 ", year_to_predict,
sum(pred_per_cell$abundance_pred > 10))) sum(pred_per_cell$abundance_pred > 10)))
saveRDS(pred_per_cell, saveRDS(pred_per_cell,
...@@ -229,7 +241,7 @@ save.image(file.path(outdir, "image_before_map.RData")) ...@@ -229,7 +241,7 @@ save.image(file.path(outdir, "image_before_map.RData"))
#-----------------------# #-----------------------#
# convert output to map # # convert output to map #
#-----------------------# #-----------------------#
print(paste(Sys.time(), "3. Start making map")) if(is_verbose){print(paste(Sys.time(), "3. Start making map"))}
geography_grid_path <- path.to.current(indir_predictors, geography_grid_path <- path.to.current(indir_predictors,
paste0("geography_", year_to_predict), "rds") paste0("geography_", year_to_predict), "rds")
...@@ -271,4 +283,4 @@ save.image(file.path(outdir, paste0("abundance_pred_image_", year_to_predict, "_ ...@@ -271,4 +283,4 @@ save.image(file.path(outdir, paste0("abundance_pred_image_", year_to_predict, "_
print(paste(Sys.time(), "4. wrote results and done :-)")) if(is_verbose){print(paste(Sys.time(), "4. wrote results and done :-)"))}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment