@@ -33,9 +33,8 @@ init_PCoA <- function(input_rast, output_dir, window_size, Kmeans_info,
33
33
34
34
# if path for data required to map beta diversity provided
35
35
if (! is.null(Beta_info_read )) {
36
- if (file.exists(Beta_info_read )){
37
- load(Beta_info_read )
38
- } else {
36
+ if (file.exists(Beta_info_read )) load(Beta_info_read )
37
+ if (! file.exists(Beta_info_read )){
39
38
print_error_message(' Beta_info_file_missing' )
40
39
Beta_info_read <- NULL
41
40
}
@@ -75,46 +74,52 @@ init_PCoA <- function(input_rast, output_dir, window_size, Kmeans_info,
75
74
rast_sample2 $ ID <- rast_sample2 $ ID + nbSamples
76
75
rast_sample <- rbind(rast_sample , rast_sample2 )
77
76
}
78
- # list per plot
79
- rast_sample <- rast_sample %> % group_split(ID , .keep = F )
80
- if (verbose == T ) message(' compute spectral species from beta plots' )
81
- # compute spectral species for each plot
82
- ResDist <- lapply(X = rast_sample , FUN = apply_kmeans ,
83
- Kmeans_info = Kmeans_info ,
84
- SelectBands = SelectBands )
85
77
86
- # spectral species distribution
87
- SSdist <- list ()
88
- for (iter in names(ResDist [[1 ]])) SSdist [[iter ]] <- lapply(ResDist , ' [[' ,iter )
89
- # get nbIter and nbclusters
90
- nbIter <- length(Kmeans_info $ Centroids )
91
- nbclusters <- dim(Kmeans_info $ Centroids [[1 ]])[1 ]
92
- # compute spectral species distribution for each cluster & BC dissimilarity
93
- if (verbose == T ) message(' compute dissimilarity among plots' )
94
- # plan(multisession, workers = nbCPU)
95
- cl <- parallel :: makeCluster(nbCPU )
96
- plan(" cluster" , workers = cl )
97
- handlers(global = TRUE )
98
- handlers(" cli" )
99
- with_progress({
100
- p <- progressr :: progressor(steps = nbIter )
101
- Beta_info <- future.apply :: future_lapply(SSdist ,
102
- FUN = get_BCdiss_from_SSD ,
103
- nbclusters = nbclusters ,
104
- pcelim = pcelim , p = p )
105
- })
106
- parallel :: stopCluster(cl )
107
- plan(sequential )
108
- MatBC_iter <- lapply(Beta_info , ' [[' ,' MatBC' )
109
- SSD <- lapply(Beta_info , ' [[' ,' SSD' )
110
- MatBC <- Reduce(' +' , MatBC_iter )/ nbIter
111
- MatBCdist <- stats :: as.dist(MatBC , diag = FALSE , upper = FALSE )
112
- BetaPCO <- labdsv :: pco(MatBCdist , k = dimPCoA )
113
- # Beta_Ordination_sel <- BetaPCO$points
114
- Beta_info <- list (' SSD' = SSD , ' MatBC' = MatBC , ' BetaPCO' = BetaPCO )
115
- if (is.null(Beta_info_save )) Beta_info_save <- file.path(output_dir ,
116
- ' Beta_info.RData' )
117
- save(Beta_info , file = Beta_info_save )
78
+ Beta_info <- init_PCoA_samples(rast_sample = rast_sample , output_dir = output_dir ,
79
+ Kmeans_info = Kmeans_info , SelectBands = SelectBands ,
80
+ pcelim = pcelim , dimPCoA = dimPCoA , nbCPU = nbCPU ,
81
+ Beta_info_save = Beta_info_save , verbose = verbose )
82
+
83
+ # # list per plot
84
+ # rast_sample <- rast_sample %>% group_split(ID, .keep = F)
85
+ # if (verbose ==T) message('compute spectral species from beta plots')
86
+ # # compute spectral species for each plot
87
+ # ResDist <- lapply(X = rast_sample, FUN = apply_kmeans,
88
+ # Kmeans_info = Kmeans_info,
89
+ # SelectBands = SelectBands)
90
+ #
91
+ # # spectral species distribution
92
+ # SSdist <- list()
93
+ # for (iter in names(ResDist[[1]])) SSdist[[iter]] <- lapply(ResDist, '[[',iter)
94
+ # # get nbIter and nbclusters
95
+ # nbIter <- length(Kmeans_info$Centroids)
96
+ # nbclusters <- dim(Kmeans_info$Centroids[[1]])[1]
97
+ # # compute spectral species distribution for each cluster & BC dissimilarity
98
+ # if (verbose ==T) message('compute dissimilarity among plots')
99
+ # # plan(multisession, workers = nbCPU)
100
+ # cl <- parallel::makeCluster(nbCPU)
101
+ # plan("cluster", workers = cl)
102
+ # handlers(global = TRUE)
103
+ # handlers("cli")
104
+ # with_progress({
105
+ # p <- progressr::progressor(steps = nbIter)
106
+ # Beta_info <- future.apply::future_lapply(SSdist,
107
+ # FUN = get_BCdiss_from_SSD,
108
+ # nbclusters = nbclusters,
109
+ # pcelim = pcelim, p = p)
110
+ # })
111
+ # parallel::stopCluster(cl)
112
+ # plan(sequential)
113
+ # MatBC_iter <- lapply(Beta_info, '[[','MatBC')
114
+ # SSD <- lapply(Beta_info, '[[','SSD')
115
+ # MatBC <- Reduce('+', MatBC_iter)/nbIter
116
+ # MatBCdist <- stats::as.dist(MatBC, diag = FALSE, upper = FALSE)
117
+ # BetaPCO <- labdsv::pco(MatBCdist, k = dimPCoA)
118
+ # # Beta_Ordination_sel <- BetaPCO$points
119
+ # Beta_info <- list('SSD' = SSD, 'MatBC' = MatBC, 'BetaPCO' = BetaPCO)
120
+ # if (is.null(Beta_info_save)) Beta_info_save <- file.path(output_dir,
121
+ # 'Beta_info.RData')
122
+ # save(Beta_info, file = Beta_info_save)
118
123
}
119
124
return (Beta_info )
120
125
}
0 commit comments