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

fixing all the is_quiet to is_verbose and verbose_script

parent 78045345
No related branches found
No related tags found
No related merge requests found
...@@ -60,26 +60,26 @@ if (!is.na(options$exclude_year) && !(options$exclude_year %in% exclude_year_pos ...@@ -60,26 +60,26 @@ if (!is.na(options$exclude_year) && !(options$exclude_year %in% exclude_year_pos
stop(paste("exclude year must be between", min(exclude_year_possibilities), "and", max(exclude_year_possibilities))) stop(paste("exclude year must be between", min(exclude_year_possibilities), "and", max(exclude_year_possibilities)))
} }
is_quiet <- options$quiet_script is_verbose <- options$verbose_script
if(is_quiet){print(paste("Reminder: Include aerial is ", options$include_aerial, ". Please if(is_verbose){print(paste("Reminder: Include aerial is ", options$include_aerial, ". Please
pay attention that the same is the case for post-processing."))} pay attention that the same is the case for post-processing."))}
# input directory # input directory
indir <- options$input_directory indir <- options$input_directory
if(is_quiet){print(paste("indir", indir))} if(is_verbose){print(paste("indir", indir))}
# directory in which output is written # directory in which output is written
outdir <- options$output_directory outdir <- options$output_directory
if(is_quiet){print(paste("outdir", outdir))} if(is_verbose){print(paste("outdir", outdir))}
do_stability <- options$stability do_stability <- options$stability
if(is_quiet){print(paste("stability", do_stability))} if(is_verbose){print(paste("stability", do_stability))}
include_aerial <- options$include_aerial include_aerial <- options$include_aerial
if(is_quiet){print(paste("include_aerial", include_aerial))} if(is_verbose){print(paste("include_aerial", include_aerial))}
exclude_year <- options$exclude_year exclude_year <- options$exclude_year
if(is_quiet){print(paste("exclude year", exclude_year))} if(is_verbose){print(paste("exclude year", exclude_year))}
...@@ -88,7 +88,7 @@ if(is_quiet){print(paste("exclude year", exclude_year))} ...@@ -88,7 +88,7 @@ if(is_quiet){print(paste("exclude year", exclude_year))}
#---------# #---------#
indir_fun <- "../functions" indir_fun <- "../functions"
if(is_quiet){print(paste("indir_fun", indir_fun))} if(is_verbose){print(paste("indir_fun", indir_fun))}
cl <- makeForkCluster(outfile = "") cl <- makeForkCluster(outfile = "")
registerDoParallel(cl) registerDoParallel(cl)
...@@ -112,16 +112,16 @@ options("scipen" = 100, "digits" = 4) ...@@ -112,16 +112,16 @@ options("scipen" = 100, "digits" = 4)
#---------------# #---------------#
geography_path <- path.to.current(indir, "geography_observation", "rds") geography_path <- path.to.current(indir, "geography_observation", "rds")
if(is_quiet){print(paste("geography-path", geography_path))} if(is_verbose){print(paste("geography-path", geography_path))}
geography <- readRDS(geography_path) geography <- readRDS(geography_path)
transects_path <- path.to.current(indir, "transects", "rds") transects_path <- path.to.current(indir, "transects", "rds")
if(is_quiet){print(paste("transect_path", transects_path))} if(is_verbose){print(paste("transect_path", transects_path))}
transects <- readRDS(transects_path) transects <- readRDS(transects_path)
predictors_path <- path.to.current(indir, "predictors_observation", "rds") predictors_path <- path.to.current(indir, "predictors_observation", "rds")
if(is_quiet){print(paste("predictors-path", predictors_path))} if(is_verbose){print(paste("predictors-path", predictors_path))}
predictors <- readRDS(predictors_path) predictors <- readRDS(predictors_path)
...@@ -134,7 +134,7 @@ predictor_names <- c("year", "temp_mean", "rain_var", "rain_dry", "dom_T_OC", ...@@ -134,7 +134,7 @@ predictor_names <- c("year", "temp_mean", "rain_var", "rain_dry", "dom_T_OC",
geography <- dplyr::select(geography, -year) geography <- dplyr::select(geography, -year)
if(is_quiet){print("how many rows with na in scaled_value") if(is_verbose){print("how many rows with na in scaled_value")
nrow(predictors[is.na(predictors$scaled_value), ])} nrow(predictors[is.na(predictors$scaled_value), ])}
# deleting is.na values here # deleting is.na values here
predictors <- predictors[!is.na(predictors$scaled_value), ] predictors <- predictors[!is.na(predictors$scaled_value), ]
...@@ -181,7 +181,7 @@ aerial_predictors_obs$nr_nests <- round(exp(4.7297 + 0.9796 * ...@@ -181,7 +181,7 @@ aerial_predictors_obs$nr_nests <- round(exp(4.7297 + 0.9796 *
predictors_obs <- aerial_predictors_obs %>% predictors_obs <- aerial_predictors_obs %>%
dplyr::select(id:length_km, nr_nests, nest_decay, ou_dens, offset_term) dplyr::select(id:length_km, nr_nests, nest_decay, ou_dens, offset_term)
if(is_quiet){ print("This has to be true:") if(is_verbose){ print("This has to be true:")
unique(names(predictors_obs) == names(other_predictors_obs))} unique(names(predictors_obs) == names(other_predictors_obs))}
# HAS TO BE TRUE # HAS TO BE TRUE
predictors_obs <- predictors_obs %>% predictors_obs <- predictors_obs %>%
...@@ -208,7 +208,7 @@ predictors_obs$y_center <- rowMeans(cbind(predictors_obs$y_start, predictors_obs ...@@ -208,7 +208,7 @@ predictors_obs$y_center <- rowMeans(cbind(predictors_obs$y_start, predictors_obs
predictors_obs$nr_ou_per_km2 <- predictors_obs$nr_nests / predictors_obs$nr_ou_per_km2 <- predictors_obs$nr_nests /
(predictors_obs$length_km * ESW * 2 * predictors_obs$nest_decay * NCS * PNB ) (predictors_obs$length_km * ESW * 2 * predictors_obs$nest_decay * NCS * PNB )
if(is_quiet){print("look at predictors_obs") if(is_verbose){print("look at predictors_obs")
str(predictors_obs) str(predictors_obs)
summary(predictors_obs)} summary(predictors_obs)}
...@@ -218,7 +218,7 @@ if (!is.na(exclude_year)){ ...@@ -218,7 +218,7 @@ if (!is.na(exclude_year)){
predictors_excluded_year <- predictors_obs[predictors_obs$unscaled_year == exclude_year, ] } predictors_excluded_year <- predictors_obs[predictors_obs$unscaled_year == exclude_year, ] }
predictors_obs <- predictors_obs[predictors_obs$unscaled_year != exclude_year, ] predictors_obs <- predictors_obs[predictors_obs$unscaled_year != exclude_year, ]
if(is_quiet){ print(paste("3. start making all_model_terms", Sys.time()))} if(is_verbose){ print(paste("3. start making all_model_terms", Sys.time()))}
# #build models needed for analysis with a function # #build models needed for analysis with a function
all_model_terms <- built.all.models(env.cov.names = all_model_terms <- built.all.models(env.cov.names =
...@@ -278,11 +278,11 @@ names(predictor_estimates) <- c("intercept", predictor_names, ...@@ -278,11 +278,11 @@ names(predictor_estimates) <- c("intercept", predictor_names,
# calculate stability of the full model if desired # calculate stability of the full model if desired
if(do_stability){ if(do_stability){
if(is_quiet){print(paste("Start stability calculation", Sys.time()))} if(is_verbose){print(paste("Start stability calculation", Sys.time()))}
full_model <- paste( full_model <- paste(
m_terms[all_model_terms[nrow(all_model_terms), ] == 1], m_terms[all_model_terms[nrow(all_model_terms), ] == 1],
collapse = "+") collapse = "+")
if(is_quiet){print(paste("This is the full-model", full_model))} if(is_verbose){print(paste("This is the full-model", full_model))}
model <- as.formula( model <- as.formula(
paste("nr_nests ~", full_model, "+ offset(offset_term)")) paste("nr_nests ~", full_model, "+ offset(offset_term)"))
...@@ -300,7 +300,7 @@ write.csv(dfbeta_frame, file.path(outdir, ...@@ -300,7 +300,7 @@ write.csv(dfbeta_frame, file.path(outdir,
} }
# #run models # #run models
if(is_quiet){print(paste("8. Start running models", Sys.time()))} if(is_verbose){print(paste("8. Start running models", Sys.time()))}
if (is.na(exclude_year)){ if (is.na(exclude_year)){
...@@ -435,4 +435,4 @@ write.csv(summary_mean_coefficients, ...@@ -435,4 +435,4 @@ write.csv(summary_mean_coefficients,
save.image(file.path(outdir, paste0("abundance_model_fitting_", save.image(file.path(outdir, paste0("abundance_model_fitting_",
name_suffix, name_suffix,
Sys.Date(), ".RData"))) Sys.Date(), ".RData")))
if(is_quiet){print(paste("11. finished script, finally, at", Sys.time()))} if(is_verbose){print(paste("11. finished script, finally, at", Sys.time()))}
...@@ -91,29 +91,29 @@ if (!(options$year_to_predict %in% year_to_predict_possibilities)) { ...@@ -91,29 +91,29 @@ if (!(options$year_to_predict %in% year_to_predict_possibilities)) {
# is quiet? # is quiet?
is_quiet <- options$quiet_script is_verbose <- options$verbose_script
# input directory # input directory
indir <- options$input_directory indir <- options$input_directory
if(is_quiet){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
if(is_quiet){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
if(is_quiet){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 )
if(is_quiet){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) exclude_year <- as.numeric(options$exclude_year)
if(is_quiet){print(paste("exclude year", exclude_year))} if(is_verbose){print(paste("exclude year", exclude_year))}
if(is_quiet){print(paste(Sys.time(), "0. start run"))} if(is_verbose){print(paste(Sys.time(), "0. start run"))}
#------------------------# #------------------------#
# command line arguments # # command line arguments #
#------------------------# #------------------------#
...@@ -132,7 +132,7 @@ source(file.path(indir_fun, "generic/path.to.current.R")) ...@@ -132,7 +132,7 @@ source(file.path(indir_fun, "generic/path.to.current.R"))
# 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" )
if(is_quiet){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
...@@ -166,19 +166,19 @@ predictor_names <- predictor_names_coeffs[!grepl("I(*)", predictor_names_coeffs) ...@@ -166,19 +166,19 @@ predictor_names <- predictor_names_coeffs[!grepl("I(*)", predictor_names_coeffs)
#----------------------------# #----------------------------#
# Load and prepare estimates # # Load and prepare estimates #
#----------------------------# #----------------------------#
if(is_quiet){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")
if(is_quiet){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
if(is_quiet){str(predictors)} if(is_verbose){str(predictors)}
if(is_quiet){print(paste("this is nrow predictors", nrow(predictors)))} if(is_verbose){print(paste("this is nrow predictors", nrow(predictors)))}
#--------------------------# #--------------------------#
# PREDICTION FOR each year # # PREDICTION FOR each year #
...@@ -202,7 +202,7 @@ names(predictor_estimates) <- c("intercept", predictor_names, ...@@ -202,7 +202,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
if(is_quiet){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, ])
...@@ -213,7 +213,7 @@ pred_estimates_sum <- apply(pred_estimates_wcoeffs, 1, sum) ...@@ -213,7 +213,7 @@ pred_estimates_sum <- apply(pred_estimates_wcoeffs, 1, sum)
return(exp(pred_estimates_calc)) return(exp(pred_estimates_calc))
} }
if(is_quiet){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))
...@@ -258,7 +258,7 @@ save.image(file.path(outdir, paste0("abundance_pred_image_", name_suffix, ...@@ -258,7 +258,7 @@ save.image(file.path(outdir, paste0("abundance_pred_image_", name_suffix,
#-----------------------# #-----------------------#
# convert output to map # # convert output to map #
#-----------------------# #-----------------------#
if(is_quiet){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")
...@@ -308,4 +308,4 @@ save.image(file.path(outdir, paste0("abundance_pred_image_", name_suffix, ...@@ -308,4 +308,4 @@ save.image(file.path(outdir, paste0("abundance_pred_image_", name_suffix,
if(is_quiet){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