Commit 61bb25b6 authored by Romulo Pereira Goncalves's avatar Romulo Pereira Goncalves
Browse files

Avoid re-assignments to the same variable name so we can delete the content...

Avoid re-assignments to the same variable name so we can delete the content and help the garbage collector to clean memory.
parent b85fec99
......@@ -78,8 +78,8 @@ sample_nb <- function(raster,
seed2 <- init.seed
}
oobe <- matrix(NA, nrow = n, ncol = nb_mean)
models <- list()
points <- list()
models_list <- list()
points_list <- list()
dif <- matrix(NA, nrow = nb_mean, ncol = nrow(reference))
channel <- matrix(NA, nrow = nb_mean, ncol = nrow(reference))
switch <- matrix(NA, nrow = nb_mean, ncol = nrow(reference))
......@@ -116,8 +116,8 @@ sample_nb <- function(raster,
mc.cores = cores
)
for (k in 1:nb_mean) {
points[[k]] <- res[["k" = k]][["points"]]
models[[k]] <- res[["k" = k]][["models"]]
points_list[[k]] <- res[["k" = k]][["points"]]
models_list[[k]] <- res[["k" = k]][["models"]]
oobe[, k] <- res[["k" = k]][["oobe"]][, 1]
}
} else {
......@@ -140,21 +140,22 @@ sample_nb <- function(raster,
ras_vx = ras.vx,
max_samples_per_class = max_samples_per_class
)
points[[k]] <- res$points
models[[k]] <- res$models
points_list[[k]] <- res$points
models_list[[k]] <- res$models
oobe[, k] <- res$oobe[, 1]
setTxtProgressBar(pb, k)
}
}
#which_models_null <- which(models == "NULL")
which_models_null <- vapply(models, is.not.null <- function(x){!is.null(x)}, FALSE)
if (length(models) == 0 |
length(which_models_null[which_models_null == FALSE]) == length(models)) {
which_models_null <- vapply(models_list, is.not.null <- function(x){!is.null(x)}, FALSE)
if (length(models_list) == 0 |
length(which_models_null[which_models_null == FALSE]) == length(models_list)) {
stop("No Models - would you be so kind to increase init.samples, please")
}
if (length(which_models_null[which_models_null == FALSE]) > 0) {
models <- models[which_models_null]
models <- models_list[which_models_null]
remove(models_list)
}
for (jj in 1:nrow(reference)) {
......@@ -215,11 +216,14 @@ sample_nb <- function(raster,
}
close(pb)
mod_all <- models
if (save_runs == TRUE) {
mod_all <- models
}
models <- models[ch]
print(paste("n_models =", length(models)))
switch <- switch[ch, index]
points <- points[ch]
points <- points_list[ch]
remove(points_list)
dif <- dif[2,]
##############################################################################
###Vohersage
......@@ -275,6 +279,14 @@ sample_nb <- function(raster,
seeds = 0
)
}
remove(models)
remove(points)
remove(switch)
remove(layer)
remove(mod_all)
remove(diff)
remove(seed2)
out <- list(index = index, acc = acc, obj = obj)
return(out)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment