Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
jbferet committed Jul 25, 2023
2 parents ab4d126 + b99b17d commit df966bf
Show file tree
Hide file tree
Showing 19 changed files with 402 additions and 263 deletions.
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: biodivMapR
Title: biodivMapR: an R package for a- and ß-diversity mapping using remotely-sensed images
Version: 1.10.6
Version: 1.11.0
Authors@R: c(person(given = "Jean-Baptiste",
family = "Feret",
email = "jb.feret@teledetection.fr",
Expand All @@ -20,7 +20,6 @@ LazyData: true
Imports:
dissUtils,
data.table,
emstreeR,
fields,
future,
future.apply,
Expand All @@ -37,9 +36,10 @@ Imports:
tools,
vegan,
zip,
progress,
progressr,
cli
cli,
ape,
terra
RoxygenNote: 7.2.3
Suggests:
knitr,
Expand Down
17 changes: 12 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Generated by roxygen2: do not edit by hand

export(ENVI_type2bytes)
export(Get_FunctionalMetrics_From_Traits)
export(IQR_outliers)
export(RowToLinear)
export(WeightedCoordsNN)
export(Write_Big_Image)
export(Write_Image_NativeRes)
Expand All @@ -19,15 +21,15 @@ export(compute_ALPHA_SSD_per_window_list)
export(compute_ALPHA_per_window)
export(compute_BCdiss)
export(compute_BETA_FromPlots)
export(compute_FUNCT)
export(compute_FD)
export(compute_Functional_metrics)
export(compute_NN_from_ordination)
export(compute_SSD)
export(compute_alpha_metrics)
export(compute_beta_metrics)
export(compute_spectral_species)
export(compute_spectral_species_FieldPlots)
export(continuumRemoval)
export(coordPix_kernel)
export(create_hdr)
export(create_mask_from_threshold)
export(define_output_directory)
Expand All @@ -43,6 +45,7 @@ export(extract_samples_from_image)
export(filter_PCA)
export(filter_prior_CR)
export(getBCdiss)
export(getFD)
export(get_HDR_name)
export(get_SSpath)
export(get_Shannon)
Expand Down Expand Up @@ -78,6 +81,7 @@ export(read_ENVI_header)
export(read_bin_subset)
export(read_image_bands)
export(read_image_subset)
export(repmat)
export(revert_resolution_HDR)
export(rm_invariant_bands)
export(select_PCA_components)
Expand All @@ -97,25 +101,24 @@ export(write_raster)
import(cli)
import(stars)
import(tools)
importFrom(ape,mst)
importFrom(data.table,data.table)
importFrom(data.table,rbindlist)
importFrom(data.table,setorder)
importFrom(dissUtils,diss)
importFrom(emstreeR,ComputeMST)
importFrom(fields,rdist)
importFrom(future,multisession)
importFrom(future,plan)
importFrom(future,sequential)
importFrom(future.apply,future_lapply)
importFrom(geometry,convhulln)
importFrom(labdsv,pco)
importFrom(matlab,ndims)
importFrom(matlab,meshgrid)
importFrom(matlab,ones)
importFrom(matlab,padarray)
importFrom(matrixStats,rowAnys)
importFrom(matrixStats,rowSds)
importFrom(mmand,erode)
importFrom(progress,progress_bar)
importFrom(progressr,handlers)
importFrom(progressr,progressor)
importFrom(progressr,with_progress)
Expand Down Expand Up @@ -144,11 +147,15 @@ importFrom(stars,write_stars)
importFrom(stats,IQR)
importFrom(stats,as.dist)
importFrom(stats,cov)
importFrom(stats,dist)
importFrom(stats,kmeans)
importFrom(stats,na.omit)
importFrom(stats,prcomp)
importFrom(stats,quantile)
importFrom(stats,sd)
importFrom(stringr,str_count)
importFrom(terra,rast)
importFrom(terra,values)
importFrom(tools,file_path_sans_ext)
importFrom(utils,file.edit)
importFrom(utils,read.table)
Expand Down
34 changes: 19 additions & 15 deletions R/Lib_ContinuumRemoval.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ filter_prior_CR <- function(Minit, Spectral_Bands) {
# Minit[Minit<0] <- Minit + 100.0
Minit[Minit < 0] <- 0
# eliminate invariant spectra
SD <- rowSds(Minit)
SD <- matrixStats::rowSds(Minit)
elim <- which(SD == 0 | is.na(SD))
keep <- which(!SD == 0 & !is.na(SD))
nbi0 <- nrow(Minit)
Expand All @@ -212,27 +212,31 @@ filter_prior_CR <- function(Minit, Spectral_Bands) {
return(my_list)
}

#
# @param MM
# @param nbi
# @param nbj
#
# @return
#' get max index for each row and convert into linear index
#' @param MM numeric.
#' @param nbi numeric.
#' @param nbj numeric.
#'
#' @return MaxCont
#' @export

RowToLinear <- function(MM, nbi, nbj) {
adj <- seq(1:nbi)
MaxCont <- ((MM - 1) * (nbi)) + adj
return(MaxCont)
}

# R equivalent of repmat (matlab)
#
# @param X initial matrix
# @param m nb of replications in row dimension
# @param n nb of replications in column dimension
#
# @return
#' R equivalent of repmat (matlab)
#'
#' @param X initial matrix
#' @param m nb of replications in row dimension
#' @param n nb of replications in column dimension
#'
#' @return matrix with values replicated and tiles
#' @export

repmat <- function(X, m, n) {
mx <- dim(X)[1]
nx <- dim(X)[2]
matrix(t(matrix(X, mx, nx * n)), mx * m, nx * n, byrow = T)
return(matrix(t(matrix(X, mx, nx * n)), mx * m, nx * n, byrow = T))
}
18 changes: 7 additions & 11 deletions R/Lib_ImageProcess.R
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ extract.big_raster <- function(ImPath, rowcol, MaxRAM=.50){
#' @param kernel numeric.
#' @param MaxRAM numeric.
#'
#' @importFrom matlab ones
#' @importFrom matlab padarray meshgrid
#' @importFrom raster raster brick nbands ncell values
#' @importFrom mmand erode
#' @importFrom data.table data.table rbindlist setorder
Expand Down Expand Up @@ -603,9 +603,9 @@ get_random_subset_from_image <- function(ImPath, MaskPath, nb_partitions,
if(is.matrix(kernel)){
mask[which(is.na(c(mask)))] <- 0
# erode mask with kernel, to keep valid central pixels and neighbours
mask = matlab::padarray(mask, c(1,1), padval=0, direction='both')
mask = mmand::erode(mask, (kernel!=0)*1)
mask = mask[2:(nrow(mask)-1), 2:(ncol(mask)-1)]
mask <- matlab::padarray(mask, c(1,1), padval=0, direction='both')
mask <- mmand::erode(mask, (kernel!=0)*1)
mask <- mask[2:(nrow(mask)-1), 2:(ncol(mask)-1)]
}

# get a list of samples among unmasked pixels
Expand Down Expand Up @@ -659,7 +659,7 @@ get_random_subset_from_image <- function(ImPath, MaskPath, nb_partitions,
# remove NA and Inf pixels
if(any(is.na(Sample_Sel) | is.infinite(Sample_Sel))){
print('Removing pixels with NA values.')
rmrows <- rowAnys(is.na(Sample_Sel) | is.infinite(Sample_Sel))
rmrows <- matrixStats::rowAnys(is.na(Sample_Sel) | is.infinite(Sample_Sel))
rmpix <- unique(samplePixIndex$id[rmrows])
Sample_Sel = Sample_Sel[!(samplePixIndex$id %in% rmpix),]
samplePixIndex = samplePixIndex[!(samplePixIndex$id %in% rmpix)]
Expand Down Expand Up @@ -805,7 +805,7 @@ mean_filter <- function(Image, SizeFilt,NA_remove = FALSE) {
}
ImageSmooth <- array(0,c(nbi,nbj,dim(Image)[3]))
for (band in 1: dim(Image)[3]){
E <- padarray(Image[,,band], c(SizeFilt, SizeFilt), "symmetric", "both")
E <- matlab::padarray(Image[,,band], c(SizeFilt, SizeFilt), "symmetric", "both")
ImageSmooth_tmp <- matrix(0, nrow = nbi, ncol = nbj)
Mat2D <- MatSun <- matrix(0, nrow = ((2 * SizeFilt) + 1)^2, ncol = nbj)
spl <- split(1:nbj, 1:((2 * SizeFilt) + 1))
Expand Down Expand Up @@ -1338,7 +1338,6 @@ write_ENVI_header <- function(HDR, HDRpath) {
#' @param Image_Format list. description of data format corresponding to ENVI type
#'
#' @return None
#' @importFrom progress progress_bar
#' @export

Write_Big_Image <- function(ImgWrite,ImagePath,HDR,Image_Format){
Expand All @@ -1350,9 +1349,7 @@ Write_Big_Image <- function(ImgWrite,ImagePath,HDR,Image_Format){
)
close(fidOUT)
# for each piece of image
pb <- progress_bar$new(
format = 'Writing raster [:bar] :percent in :elapsedfull',
total = nbPieces, clear = FALSE, width= 100)
message(paste('Writing raster', ImagePath, sep = ' '))

for (i in 1:nbPieces) {
# read image and mask data
Expand Down Expand Up @@ -1382,7 +1379,6 @@ Write_Big_Image <- function(ImgWrite,ImagePath,HDR,Image_Format){
writeBin(c(as.integer(ImgChunk)), fidOUT, size = Image_Format$Bytes, endian = .Platform$endian, useBytes = FALSE)
}
close(fidOUT)
pb$tick()
}
rm(ImgWrite)
rm(ImgChunk)
Expand Down
3 changes: 2 additions & 1 deletion R/Lib_MapAlphaDiversity.R
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,9 @@ convert_PCA_to_SSD <- function(ReadWrite, Spectral_Species_Path,
#' @param Mask_Chunk numeric. 3D image chunk of mask (optional)
#
#' @return list of alpha diversity metrics for each iteration
#' @import cli
#' @importFrom vegan fisher.alpha
#' @import cli
#' @importFrom progressr progressor handlers with_progress
#' @importFrom snow splitList
#' @importFrom future plan multisession sequential
#' @importFrom future.apply future_lapply
Expand Down
2 changes: 1 addition & 1 deletion R/Lib_MapBetaDiversity.R
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ compute_BCdiss <- function(SSDList, pcelim=0.02) {
SSD <- lapply(SSDList,FUN = normalize_SSD, pcelim = pcelim)
# matrix of bray curtis dissimilarity (size = nb kernels x nb kernels)
# Here use the package "dissUtils" to compute dissimilarity matrices sequentially
MatBC <- diss(SSD[[1]], SSD[[2]], method = 'braycurtis')
MatBC <- dissUtils::diss(SSD[[1]], SSD[[2]], method = 'braycurtis')
# EDIT 06-Feb-2019: added this to fix problem when empty kernels occur, leading to NA BC value
BCNA <- which(is.na(MatBC) == TRUE)
if (length(BCNA) > 0) {
Expand Down
Loading

0 comments on commit df966bf

Please sign in to comment.