Skip to content

Commit bed6b0d

Browse files
committed
# biodivMapR2 v2.4.5
## change - biodivMapR_full_classif: add possibility to set compute_beta = TRUE or FALSE - remove TODO.md - use regular expression '\\.' when searching for strings including '.':
1 parent 4c15c7f commit bed6b0d

14 files changed

+81
-74
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: biodivMapR
22
Title: biodivMapR: an R package for a- and ß-diversity mapping using remotely-sensed images
3-
Version: 2.4.4
3+
Version: 2.4.5
44
Authors@R: c(person(given = "Jean-Baptiste",
55
family = "Feret",
66
email = "jb.feret@teledetection.fr",

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# biodivMapR2 v2.4.5
2+
## change
3+
- biodivMapR_full_classif: add possibility to set compute_beta = TRUE or FALSE
4+
- remove TODO.md
5+
- use regular expression '\\.' when searching for strings including '.':
6+
17
# biodivMapR2 v2.4.4
28
## fix
39
- use lapply instead of future_lapply when calling 'functional_window_list' in 'get_raster_diversity_tile'

R/alphabeta_window_classif.R

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#' @export
1515

1616
alphabeta_window_classif <- function(SSwindow, nb_clusters,
17-
Beta_info, alpha_metrics,
17+
Beta_info = NULL, alpha_metrics,
1818
pcelim = 0.02,
1919
Hill_order = 1){
2020
# get spectral species distribution from individual pixels within a window
@@ -27,20 +27,24 @@ alphabeta_window_classif <- function(SSwindow, nb_clusters,
2727
nb_pix_sunlit = nb_pix_sunlit,
2828
pcelim = pcelim,
2929
hill_order = Hill_order)
30-
# get BETA diversity
31-
# full spectral species distribution = missing clusters set to 0
32-
ssd_full <- lapply(X = ssd, FUN = get_normalized_ssd,
33-
nb_clusters = nb_clusters, pcelim = pcelim)
34-
mat_bc <- list()
35-
pcoa_bc <- list()
36-
for (i in seq_along(ssd_full)){
37-
mat_bc_tmp <- dissUtils::diss(ssd_full[[i]], Beta_info$SSD,
38-
method = 'braycurtis')
39-
# mat_bc <- list('mat1' = ssd_full[[i]],
40-
# 'mat2' = Beta_info$SSD)
41-
# mat_bc_tmp <- compute_bc_diss(ssd_list = mat_bc, pcelim = pcelim)
42-
pcoa_bc[[i]] <- compute_nn_from_ordination(mat_bc = mat_bc_tmp, knn = 3,
43-
pcoa_train = Beta_info$BetaPCO$points)
30+
31+
pcoa_bc <- NULL
32+
if (!is.null(Beta_info)){
33+
# get BETA diversity
34+
# full spectral species distribution = missing clusters set to 0
35+
ssd_full <- lapply(X = ssd, FUN = get_normalized_ssd,
36+
nb_clusters = nb_clusters, pcelim = pcelim)
37+
mat_bc <- list()
38+
pcoa_bc <- list()
39+
for (i in seq_along(ssd_full)){
40+
mat_bc_tmp <- dissUtils::diss(ssd_full[[i]], Beta_info$SSD,
41+
method = 'braycurtis')
42+
# mat_bc <- list('mat1' = ssd_full[[i]],
43+
# 'mat2' = Beta_info$SSD)
44+
# mat_bc_tmp <- compute_bc_diss(ssd_list = mat_bc, pcelim = pcelim)
45+
pcoa_bc[[i]] <- compute_nn_from_ordination(mat_bc = mat_bc_tmp, knn = 3,
46+
pcoa_train = Beta_info$BetaPCO$points)
47+
}
4448
}
4549
return(list('richness' = unlist(lapply(alpha, '[[', 'richness')),
4650
'shannon' = unlist(lapply(alpha, '[[', 'shannon')),

R/biodivMapR_SFS.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ biodivMapR_sfs <- function(input_raster, obs_vect, obs2optimize,
3939
pcelim = 0.02, verbose = TRUE, nbWorkers = 1,
4040
nbCPU = 1){
4141

42-
FullListIndices <- c("richness", "shannon", "simpson", "hill", "BC",
43-
"FRic", "FEve", "FDiv")
42+
FullListIndices <- c('richness', 'shannon', 'simpson', 'hill', 'BC',
43+
'FRic', 'FEve', 'FDiv', 'FDis', 'FRaoq')
4444
#### Which diversity metrics should be computed?
4545
alphamet <- c('richness', 'shannon', 'simpson', 'hill')
4646
betamet <- 'BC'
47-
fmet <- c('FRic', 'FEve', 'FDiv')
47+
fmet <- c('FRic', 'FEve', 'FDiv', 'FDis', 'FRaoq')
4848
# if computation of functional metrics required
4949
alpha_metrics <- alphamet[which(alphamet %in% names(obs2optimize))]
5050
if (length(alpha_metrics)==0)
@@ -133,7 +133,7 @@ biodivMapR_sfs <- function(input_raster, obs_vect, obs2optimize,
133133
registerDoFuture()
134134
# plan(multisession, workers = nbWorkers)
135135
cl <- parallel::makeCluster(nbWorkers)
136-
with(plan("cluster", workers = cl), local = TRUE)
136+
with(plan('cluster', workers = cl), local = TRUE)
137137

138138
Corr_criterion <- EvolCorr <- CorrSFS <- AssessSFS <- list()
139139
SelectedVars <- EvolCorr$richness <- EvolCorr$shannon <- EvolCorr$simpson <-
@@ -146,7 +146,7 @@ biodivMapR_sfs <- function(input_raster, obs_vect, obs2optimize,
146146
p <- progressr::progressor(steps = nb_pcs_to_keep)
147147

148148
# pb <- progress::progress_bar$new(
149-
# format = "Perform feature selection [:bar] :percent in :elapsedfull",
149+
# format = 'Perform feature selection [:bar] :percent in :elapsedfull',
150150
# total = nb_pcs_to_keep, clear = FALSE, width= 100)
151151

152152
for (nbvars2select in seq_len(nb_pcs_to_keep)){
@@ -169,7 +169,7 @@ biodivMapR_sfs <- function(input_raster, obs_vect, obs2optimize,
169169
inputdata_cr$ID <- IDplot
170170
inputdata_cr <- inputdata_cr %>% split(.$ID)
171171
inputdata_cr <- lapply(inputdata_cr,
172-
function(x) data.frame(x[ , !(names(x) %in% "ID")]))
172+
function(x) data.frame(x[ , !(names(x) %in% 'ID')]))
173173
FunctDiv <- lapply(X = inputdata_cr,
174174
FUN = get_functional_diversity,
175175
fd_metrics = fd_metrics)
@@ -256,7 +256,7 @@ biodivMapR_sfs <- function(input_raster, obs_vect, obs2optimize,
256256
}
257257
}
258258
subSFS <- subfeatures_SFS()
259-
p(message = sprintf("Perform feature selection %g", nbvars2select))
259+
p(message = sprintf('Perform feature selection %g', nbvars2select))
260260
# pb$tick()
261261
CorrSFS[[nbvars2select]] <- list()
262262
for (ind in FullListIndices)

R/biodivMapR_full_classif.R

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#' @param input_mask_path character. path for mask file
77
#' @param site_name character. nname of site
88
#' @param pcelim numeric. minimum proportion of pixels to consider spectral species
9+
#' @param compute_beta boolean. set TRUE if beta is to be computed
910
#' @param nb_samples_beta numeric. number of samples to compute beta diversity
1011
#'
1112
#' @return diversity_maps_ground
@@ -14,7 +15,8 @@
1415

1516
biodivMapR_full_classif <- function(input_raster_path, output_dir, window_size,
1617
input_mask_path = NULL, site_name = NULL,
17-
pcelim = 0.02, nb_samples_beta = 1000){
18+
pcelim = 0.02, compute_beta = TRUE,
19+
nb_samples_beta = 1000){
1820

1921
# define all alpha metrics
2022
alpha_metrics <- c('richness', 'shannon', 'simpson', 'hill')
@@ -32,35 +34,41 @@ biodivMapR_full_classif <- function(input_raster_path, output_dir, window_size,
3234
input_mask <- terra::rast(input_mask_path)
3335

3436
# define plot selection for beta diversity and sample from raster
35-
plots_beta <- plots[sample(x = seq_along(plots), nb_samples_beta,
36-
replace = FALSE)]
37-
get_samples_from_plots <- function(x, y){
38-
x <- terra::vect(x)
39-
res <- terra::extract(x = y, y = x, raw = TRUE, ID = FALSE)
40-
res <- c(unlist(res))
41-
return(res)
42-
}
43-
samples <- lapply(X = plots_beta, FUN = get_samples_from_plots,
44-
y = terra::rast(input_raster_path))
37+
Beta_info <- NULL
38+
if (compute_beta){
39+
if (nb_samples_beta > length(plots))
40+
nb_samples_beta <- length(plots)
41+
42+
plots_beta <- plots[sample(x = seq_along(plots), nb_samples_beta,
43+
replace = FALSE)]
44+
get_samples_from_plots <- function(x, y){
45+
x <- terra::vect(x)
46+
res <- terra::extract(x = y, y = x, raw = TRUE, ID = FALSE)
47+
res <- c(unlist(res))
48+
return(res)
49+
}
50+
samples <- lapply(X = plots_beta, FUN = get_samples_from_plots,
51+
y = terra::rast(input_raster_path))
4552

46-
# compute spectral dissimilarity
47-
ssd <- lapply(X = samples,FUN = table)
48-
ssd <- lapply(X = ssd,FUN = get_normalized_ssd,
49-
nb_clusters = nb_clusters, pcelim = pcelim)
50-
ssd <- do.call(rbind,ssd)
51-
mat_bc <- dissUtils::diss(ssd, ssd, method = 'braycurtis')
52-
# ssd_list <- list(ssd, ssd)
53-
# mat_bc <- compute_bc_diss(ssd_list, pcelim)
54-
Beta_info <- list('SSD' = ssd, 'MatBC' = mat_bc)
55-
mat_bc_dist <- stats::as.dist(mat_bc, diag = FALSE, upper = FALSE)
56-
BetaPCO <- pco(mat_bc_dist, k = 3)
57-
Beta_info <- list('SSD' = ssd,
58-
'MatBC' = mat_bc,
59-
'BetaPCO' = BetaPCO)
60-
# # save spectral dissimilarity
61-
# if (is.null(Beta_info_save))
62-
# Beta_info_save <- file.path(output_dir, 'Beta_info_classif.RData')
63-
# save(Beta_info, file = Beta_info_save)
53+
# compute spectral dissimilarity
54+
ssd <- lapply(X = samples,FUN = table)
55+
ssd <- lapply(X = ssd,FUN = get_normalized_ssd,
56+
nb_clusters = nb_clusters, pcelim = pcelim)
57+
ssd <- do.call(rbind,ssd)
58+
mat_bc <- dissUtils::diss(ssd, ssd, method = 'braycurtis')
59+
# ssd_list <- list(ssd, ssd)
60+
# mat_bc <- compute_bc_diss(ssd_list, pcelim)
61+
Beta_info <- list('SSD' = ssd, 'MatBC' = mat_bc)
62+
mat_bc_dist <- stats::as.dist(mat_bc, diag = FALSE, upper = FALSE)
63+
BetaPCO <- pco(mat_bc_dist, k = 3)
64+
Beta_info <- list('SSD' = ssd,
65+
'MatBC' = mat_bc,
66+
'BetaPCO' = BetaPCO)
67+
# # save spectral dissimilarity
68+
# if (is.null(Beta_info_save))
69+
# Beta_info_save <- file.path(output_dir, 'Beta_info_classif.RData')
70+
# save(Beta_info, file = Beta_info_save)
71+
}
6472

6573
# compute alpha and beta diversity over the full image
6674
input_rast_tmp <- input_rast
@@ -91,7 +99,7 @@ biodivMapR_full_classif <- function(input_raster_path, output_dir, window_size,
9199
# save diversity maps
92100
diversity_maps_ground <- list()
93101
for (idx in names(alphabeta)){
94-
if (idx == 'PCoA_BC'){
102+
if (idx == 'PCoA_BC' & !is.null(Beta_info)){
95103
beta <- list(output_rast_tmp, output_rast_tmp, output_rast_tmp)
96104
for (i in 1:3)
97105
beta[[i]][cell_order] <- unlist(lapply(alphabeta[[idx]], '[[', i))

R/biodivMapR_tiles.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ biodivMapR_tiles <- function(feature_dir, list_features, mask_dir = NULL,
108108
} else if (biodividx %in% alpha_metrics){
109109
selfiles <- list.files(path = output_dir, pattern = biodividx)
110110
selfiles <- selfiles[grepl(x = basename(selfiles),
111-
pattern = "mean.tiff")]
111+
pattern = "mean\\.tiff")]
112112
}
113113
selfiles <- file.path(output_dir, selfiles)
114114
# create directory

R/get_plots_from_tiles.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ get_plots_from_tiles <- function(plotID, plots2sel, listfiles, feat_list,
2828
for (feat in feat_list){
2929
feat2 <- feat
3030
if (!feat == 'mask')
31-
feat2 <- paste0('_',feat, '.')
31+
feat2 <- paste0('_',feat, '\\.')
3232
# whichfeat <- which(stringr::str_detect(basename(terra::sources(rastID)), feat) )
3333
whichfeat <- which(grepl(x = basename(terra::sources(rastID)),
3434
pattern = feat2))

R/get_samples_from_tiles.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ get_samples_from_tiles <- function(plotID, pix2sel, listfiles, feat_list,
2929
for (feat in feat_list){
3030
feat2 <- feat
3131
if (!feat == 'mask')
32-
feat2 <- paste0('_',feat, '.')
32+
feat2 <- paste0('_',feat, '\\.')
3333
whichfeat <- which(grepl(x = basename(terra::sources(rastID)),
3434
pattern = feat2))
3535
names(rastID)[whichfeat] <- feat

R/radiometric_filtering.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ radiometric_filtering <- function(input_raster_path, output_dir, input_rast_wl,
3939
}
4040
mask_update <- file.path(output_dir, maskfilename)
4141
dir.create(path = output_dir,recursive = TRUE, showWarnings = FALSE)
42-
if (filetype%in%c('GTiff', 'COG') & ! grepl(x = mask_update, pattern = '.tiff'))
42+
if (filetype%in%c('GTiff', 'COG') & ! grepl(x = mask_update, pattern = '\\.tiff'))
4343
mask_update <- paste0(mask_update, '.tiff')
4444

4545
# wavelengths expected to perform filtering

R/read_ENVI_header.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
read_ENVI_header <- function(HDRpath) {
99
# header <- paste(header, collapse = "\n")
10-
if (!grepl(".hdr$", HDRpath) & !grepl(".HDR$", HDRpath))
10+
if (!grepl("\\.hdr$", HDRpath) & !grepl("\\.HDR$", HDRpath))
1111
stop("File extension should be .hdr or .HDR")
1212

1313
HDR <- readLines(HDRpath)

0 commit comments

Comments
 (0)