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
1415
1516biodivMapR_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 ))
0 commit comments