Commit 2e8d757b authored by De Boissieu Florian's avatar De Boissieu Florian
Browse files

clean package

parent a2175a63
...@@ -12,12 +12,13 @@ export(Map.Spectral.Species) ...@@ -12,12 +12,13 @@ export(Map.Spectral.Species)
export(Perform.PCA.Image) export(Perform.PCA.Image)
export(Perform.Radiometric.Filtering) export(Perform.Radiometric.Filtering)
export(Select.Components) export(Select.Components)
import(future)
import(raster) import(raster)
import(tools) import(tools)
importFrom(dissUtils,diss) importFrom(dissUtils,diss)
importFrom(fields,rdist) importFrom(fields,rdist)
importFrom(future,multiprocess)
importFrom(future,plan) importFrom(future,plan)
importFrom(future,sequential)
importFrom(future.apply,future_lapply) importFrom(future.apply,future_lapply)
importFrom(labdsv,pco) importFrom(labdsv,pco)
importFrom(matlab,padarray) importFrom(matlab,padarray)
...@@ -25,4 +26,5 @@ importFrom(matrixStats,rowSds) ...@@ -25,4 +26,5 @@ importFrom(matrixStats,rowSds)
importFrom(raster,writeRaster) importFrom(raster,writeRaster)
importFrom(rgdal,readOGR) importFrom(rgdal,readOGR)
importFrom(snow,splitRows) importFrom(snow,splitRows)
importFrom(stats,kmeans)
importFrom(stringr,str_count) importFrom(stringr,str_count)
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
#' @import raster #' @import raster
#' @import tools #' @import tools
#' @export #' @export
Convert.Raster2BIL <- function(Raster.Path, Sensor = "unknown", Output.Directory = FALSE, Convert.Integer = TRUE, Multiplying.Factor = 1, Multiplying.Factor.Last = 1) { raster2BIL <- function(Raster.Path, Sensor = "unknown", Output.Directory = FALSE, Convert.Integer = TRUE, Multiplying.Factor = 1, Multiplying.Factor.Last = 1) {
# get directory and file name of original image # get directory and file name of original image
Input.File <- basename(Raster.Path) Input.File <- basename(Raster.Path)
...@@ -159,7 +159,7 @@ Convert.Raster2BIL <- function(Raster.Path, Sensor = "unknown", Output.Directory ...@@ -159,7 +159,7 @@ Convert.Raster2BIL <- function(Raster.Path, Sensor = "unknown", Output.Directory
#' @param Mask is the raster a mask? #' @param Mask is the raster a mask?
#' #'
#' @export #' @export
Check.Data.Format <- function(Raster.Path, Mask = FALSE) { check_data <- function(Raster.Path, Mask = FALSE) {
HDR.Path <- Get.HDR.Name(Raster.Path) HDR.Path <- Get.HDR.Name(Raster.Path)
# check if the hdr file exists # check if the hdr file exists
if (file.exists(HDR.Path)) { if (file.exists(HDR.Path)) {
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
# #
# @return samples from image and updated number of pixels to sampel if necessary # @return samples from image and updated number of pixels to sampel if necessary
#' @importFrom snow splitRows #' @importFrom snow splitRows
#' @import future #' @importFrom future plan multiprocess sequential
#' @importFrom future.apply future_lapply #' @importFrom future.apply future_lapply
Apply.Continuum.Removal <- function(Spectral.Data, Spectral, nbCPU = 1) { Apply.Continuum.Removal <- function(Spectral.Data, Spectral, nbCPU = 1) {
if (!length(Spectral$WaterVapor) == 0) { if (!length(Spectral$WaterVapor) == 0) {
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
#' @param Image.Path Path of the image to be processed #' @param Image.Path Path of the image to be processed
#' @param Mask.Path Path of the mask corresponding to the image #' @param Mask.Path Path of the mask corresponding to the image
#' @param Output.Dir output directory #' @param Output.Dir output directory
#' @param TypePCA Type of PCA (PCA, SPCA, NLPCA...) #' @param TypePCA Type of PCA: "PCA" or "SPCA"
#' @param NDVI.Thresh NDVI threshold applied to produce a mask (select pixels with NDVI>NDVI.Thresh) #' @param NDVI.Thresh NDVI threshold applied to produce a mask (select pixels with NDVI>NDVI.Thresh)
#' @param Blue.Thresh Blue threshold applied to produce a mask (select pixels with Blue refl < Blue.Thresh --> filter clouds) refl expected between 0 and 10000 #' @param Blue.Thresh Blue threshold applied to produce a mask (select pixels with Blue refl < Blue.Thresh --> filter clouds) refl expected between 0 and 10000
#' @param NIR.Thresh NIR threshold applied to produce a mask (select pixels with NIR refl < NIR.Thresh) refl expected between 0 and 10000 #' @param NIR.Thresh NIR threshold applied to produce a mask (select pixels with NIR refl < NIR.Thresh) refl expected between 0 and 10000
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
#' @param Index.Alpha character. Either 'Shannon', 'Simpson' or 'Fisher'. #' @param Index.Alpha character. Either 'Shannon', 'Simpson' or 'Fisher'.
#' #'
#' @export #' @export
Map.Alpha.Diversity <- function(Input.Image.File, Output.Dir, Spatial.Unit, alpha_div <- function(Input.Image.File, Output.Dir, Spatial.Unit,
TypePCA = "SPCA", nbclusters = 50, TypePCA = "SPCA", nbclusters = 50,
MinSun = 0.25, pcelim = 0.02, MinSun = 0.25, pcelim = 0.02,
Index.Alpha = "Shannon", FullRes = TRUE, Index.Alpha = "Shannon", FullRes = TRUE,
...@@ -153,7 +153,7 @@ Map.Alpha.Diversity.TestnbCluster <- function(ImNames, Output.Dir, Spatial.Unit, ...@@ -153,7 +153,7 @@ Map.Alpha.Diversity.TestnbCluster <- function(ImNames, Output.Dir, Spatial.Unit,
# @param MinSun minimum proportion of sunlit pixels required to consider plot # @param MinSun minimum proportion of sunlit pixels required to consider plot
# #
# @return list of mean and SD of alpha diversity metrics # @return list of mean and SD of alpha diversity metrics
#' @import future #' @importFrom future plan multiprocess sequential
#' @importFrom future.apply future_lapply #' @importFrom future.apply future_lapply
Compute.Alpha.Diversity <- function(Spectral.Species.Path, Spatial.Unit, nbclusters, MinSun, pcelim, nbCPU = FALSE, MaxRAM = FALSE, Index.Alpha = "Shannon") { Compute.Alpha.Diversity <- function(Spectral.Species.Path, Spatial.Unit, nbclusters, MinSun, pcelim, nbCPU = FALSE, MaxRAM = FALSE, Index.Alpha = "Shannon") {
## read SpectralSpecies file and write distribution per spatial unit ## ## read SpectralSpecies file and write distribution per spatial unit ##
......
...@@ -90,7 +90,6 @@ Map.Beta.Diversity.TestnbCluster <- function(Input.Image.File, Output.Dir, Spati ...@@ -90,7 +90,6 @@ Map.Beta.Diversity.TestnbCluster <- function(Input.Image.File, Output.Dir, Spati
# @param MinSun minimum proportion of sunlit pixels required # @param MinSun minimum proportion of sunlit pixels required
# #
# @return # @return
#' @importFrom future plan
Get.Sunlit.Pixels <- function(ImPathSunlit, MinSun) { Get.Sunlit.Pixels <- function(ImPathSunlit, MinSun) {
# Filter out spatial units showing poor illumination # Filter out spatial units showing poor illumination
...@@ -126,6 +125,7 @@ Get.Sunlit.Pixels <- function(ImPathSunlit, MinSun) { ...@@ -126,6 +125,7 @@ Get.Sunlit.Pixels <- function(ImPathSunlit, MinSun) {
# @param MatBCdist BC dissimilarity matrix # @param MatBCdist BC dissimilarity matrix
# #
# @return BetaNMDS.sel # @return BetaNMDS.sel
#' @importFrom future plan multiprocess sequential
#' @importFrom future.apply future_lapply #' @importFrom future.apply future_lapply
Compute.NMDS <- function(MatBCdist) { Compute.NMDS <- function(MatBCdist) {
nbiterNMDS <- 4 nbiterNMDS <- 4
...@@ -169,7 +169,7 @@ Compute.NMDS <- function(MatBCdist) { ...@@ -169,7 +169,7 @@ Compute.NMDS <- function(MatBCdist) {
# #
# @return Ordination.est coordinates of each spatial unit in ordination space # @return Ordination.est coordinates of each spatial unit in ordination space
#' @importFrom snow splitRows #' @importFrom snow splitRows
#' @import future #' @importFrom future plan multiprocess sequential
#' @importFrom future.apply future_lapply #' @importFrom future.apply future_lapply
Ordination.to.NN <- function(Beta.Ordination.sel, SSD.Path, Sample.Sel, coordTotSort, NbIter, nbclusters, pcelim, nbCPU = FALSE) { Ordination.to.NN <- function(Beta.Ordination.sel, SSD.Path, Sample.Sel, coordTotSort, NbIter, nbclusters, pcelim, nbCPU = FALSE) {
nb.Sunlit <- dim(coordTotSort)[1] nb.Sunlit <- dim(coordTotSort)[1]
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
#' #'
#' @param Input.Image.File Path and name of the image to be processed #' @param Input.Image.File Path and name of the image to be processed
#' @param Output.Dir output directory #' @param Output.Dir output directory
#' @param TypePCA Type of PCA (PCA, SPCA, NLPCA...) #' @param TypePCA Type of PCA: "PCA" or "SPCA"
#' @param PCA.Files path for PCA file #' @param PCA.Files path for PCA file
#' @param nbCPU numeric. Number of CPUs to use in parallel. #' @param nbCPU numeric. Number of CPUs to use in parallel.
#' @param MaxRAM numeric. MaxRAM maximum size of chunk in GB to limit RAM allocation when reading image file. #' @param MaxRAM numeric. MaxRAM maximum size of chunk in GB to limit RAM allocation when reading image file.
...@@ -80,8 +80,9 @@ Map.Spectral.Species <- function(Input.Image.File, Output.Dir, PCA.Files, TypePC ...@@ -80,8 +80,9 @@ Map.Spectral.Species <- function(Input.Image.File, Output.Dir, PCA.Files, TypePC
# @param nbclusters number of clusters used in kmeans # @param nbclusters number of clusters used in kmeans
# #
# @return list of centroids and parameters needed to center/reduce data # @return list of centroids and parameters needed to center/reduce data
#' @import future #' @importFrom future plan multiprocess sequential
#' @importFrom future.apply future_lapply #' @importFrom future.apply future_lapply
#' @importFrom stats kmeans
Init.Kmeans <- function(dataPCA, Pix.Per.Iter, NbIter, nbclusters, nbCPU = 1) { Init.Kmeans <- function(dataPCA, Pix.Per.Iter, NbIter, nbclusters, nbCPU = 1) {
m0 <- apply(dataPCA, 2, function(x) min(x)) m0 <- apply(dataPCA, 2, function(x) min(x))
M0 <- apply(dataPCA, 2, function(x) max(x)) M0 <- apply(dataPCA, 2, function(x) max(x))
...@@ -92,7 +93,8 @@ Init.Kmeans <- function(dataPCA, Pix.Per.Iter, NbIter, nbclusters, nbCPU = 1) { ...@@ -92,7 +93,8 @@ Init.Kmeans <- function(dataPCA, Pix.Per.Iter, NbIter, nbclusters, nbCPU = 1) {
# multiprocess kmeans clustering # multiprocess kmeans clustering
plan(multiprocess, workers = nbCPU) ## Parallelize using four cores plan(multiprocess, workers = nbCPU) ## Parallelize using four cores
Schedule.Per.Thread <- ceiling(length(dataPCA) / nbCPU) Schedule.Per.Thread <- ceiling(length(dataPCA) / nbCPU)
res <- future_lapply(dataPCA, FUN = kmeans, centers = nbclusters, iter.max = 50, nstart = 10, algorithm = c("Hartigan-Wong"), future.scheduling = Schedule.Per.Thread) res <- future_lapply(dataPCA, FUN = kmeans, centers = nbclusters, iter.max = 50, nstart = 10,
algorithm = c("Hartigan-Wong"), future.scheduling = Schedule.Per.Thread)
plan(sequential) plan(sequential)
Centroids <- list(NbIter) Centroids <- list(NbIter)
for (i in (1:NbIter)) { for (i in (1:NbIter)) {
...@@ -177,7 +179,7 @@ Apply.Kmeans <- function(PCA.Path, PC.Select, ImPathShade, Kmeans.info, Spectral ...@@ -177,7 +179,7 @@ Apply.Kmeans <- function(PCA.Path, PC.Select, ImPathShade, Kmeans.info, Spectral
# #
# @return # @return
#' @importFrom snow splitRows #' @importFrom snow splitRows
#' @import future #' @importFrom future plan multiprocess sequential
#' @importFrom future.apply future_lapply #' @importFrom future.apply future_lapply
Compute.Spectral.Species <- function(PCA.Path, ImPathShade, Spectral.Species.Path, Location.RW, PC.Select, Kmeans.info, nbCPU = 1) { Compute.Spectral.Species <- function(PCA.Path, ImPathShade, Spectral.Species.Path, Location.RW, PC.Select, Kmeans.info, nbCPU = 1) {
......
...@@ -11,39 +11,40 @@ ...@@ -11,39 +11,40 @@
# ============================================================================== # ==============================================================================
#' get projection of a raster or a vector #' get projection of a raster or a vector
#' @param Path.File path for a raster or vector (shapefile) #' @param file path for a raster or vector (shapefile)
#' @param Type 'raster' or 'vector' #' @param type 'raster' or 'vector'
#' @return projection #' @return projection
#' @importFrom raster raster shapefile projection
#' @importFrom rgdal readOGR #' @importFrom rgdal readOGR
#' @import tools #' @import tools
#' @export #' @export
Get.Projection = function(Path.File,Type = 'raster'){ projection.file <- function(file, type = 'raster'){
if (Type == 'raster'){ if (Type == 'raster'){
obj = raster(Path.File) obj <- raster(file)
} else if (Type == 'vector'){ } else if (Type == 'vector'){
Shp.Path = dirname(Path.File) Shp.Path = dirname(file)
Shp.Name = file_path_sans_ext(basename(Path.File)) Shp.Name = file_path_sans_ext(basename(file))
obj = readOGR(dsn = Shp.Path,layer = Shp.Name,verbose = FALSE) obj = readOGR(dsn = Shp.Path,layer = Shp.Name,verbose = FALSE)
} }
Projection = projection(obj) projstr <- projection(obj)
return(Projection) return(projstr)
} }
#' Get list of shapefiles in a directory #' Get list of shapefiles in a directory
#' #'
#' @param Path.Shp path of the directory containing shapefiles #' @param x character or list. Directory containing shapefiles
#' @return list of shapefiles names #' @return list of shapefiles names
#' @export #' @export
Get.List.Shp = function(Path.Shp){ list.shp <- function(x){
if(typeof(Path.Shp)=='list'){ if(typeof(x)=='list'){
List.Shp = c() List.Shp = c()
ii = 0 ii = 0
for (shp in Path.Shp){ for (shp in x){
ii = ii+1 ii = ii+1
List.Shp[ii] = grep(dir(shp, pattern = '.shp', full.names = TRUE, ignore.case = FALSE,include.dirs = FALSE), pattern='.xml', inv=T, value=T) List.Shp[ii] = dir(shp, pattern = '.shp$', full.names = TRUE, ignore.case = FALSE,include.dirs = FALSE)
} }
} else if(typeof(Path.Shp)=='character'){ } else if(typeof(Path.Shp)=='character'){
List.Shp = grep(dir(Path.Shp, pattern = '.shp', full.names = TRUE, ignore.case = FALSE,include.dirs = FALSE), pattern='.xml', inv=T, value=T) List.Shp = dir(Path.Shp, pattern = '.shp', full.names = TRUE, ignore.case = FALSE,include.dirs = FALSE)
} }
return(List.Shp) return(List.Shp)
} }
...@@ -162,7 +163,7 @@ Get.Alpha.Metrics = function(Distrib){ ...@@ -162,7 +163,7 @@ Get.Alpha.Metrics = function(Distrib){
#' @importFrom rgdal readOGR #' @importFrom rgdal readOGR
#' @import tools #' @import tools
#' @export #' @export
Get.Diversity.From.Plots = function(Raster, Plots,NbClusters = 50,Name.Plot = FALSE){ diversity_from_plots = function(Raster, Plots,NbClusters = 50,Name.Plot = FALSE){
# get hdr from raster # get hdr from raster
HDR = read.ENVI.header(paste(Raster,'.hdr',sep='')) HDR = read.ENVI.header(paste(Raster,'.hdr',sep=''))
nbRepetitions = HDR$bands nbRepetitions = HDR$bands
......
1- link raw data included in data-raw, or put it in a directory publicly available where the package will be proposed. # TODO
2- hide all functions except: 1- put use case data in another repository/ftp to reduce package size, include smaller data for tests
- Check.Data.Format
- Convert.Raster2BIL
- Perform.Radiometric.Filtering
- Perform.PCA.Images
- Select.Components
- Map.Spectral.Species
- Map.Alpha.Diversity
- Map.Beta.Diversity
- Get.List.Shp
- Get.Projection
- Get.Diversity.From.Plots
3- add manual help based on 'Main_DiversityMapping.R' 2- clean code (optimize):
- rename functions without upper case and dots
- reduce dependcies (matlab)
4- produce a binary/compiled packages for version beta 3- include tests
4- submit to CRAN
5- put the binary package ready to be installed in a directory publicly available where the data is stored as well.
7- CRAN & nettoyage du code
...@@ -14,7 +14,7 @@ Map.Spectral.Species(Input.Image.File, Output.Dir, PCA.Files, ...@@ -14,7 +14,7 @@ Map.Spectral.Species(Input.Image.File, Output.Dir, PCA.Files,
\item{PCA.Files}{path for PCA file} \item{PCA.Files}{path for PCA file}
\item{TypePCA}{Type of PCA (PCA, SPCA, NLPCA...)} \item{TypePCA}{Type of PCA: "PCA" or "SPCA"}
\item{nbclusters}{number of clusters defined in k-Means} \item{nbclusters}{number of clusters defined in k-Means}
......
...@@ -15,7 +15,7 @@ Perform.Radiometric.Filtering(Image.Path, Mask.Path, Output.Dir, ...@@ -15,7 +15,7 @@ Perform.Radiometric.Filtering(Image.Path, Mask.Path, Output.Dir,
\item{Output.Dir}{output directory} \item{Output.Dir}{output directory}
\item{TypePCA}{Type of PCA (PCA, SPCA, NLPCA...)} \item{TypePCA}{Type of PCA: "PCA" or "SPCA"}
\item{NDVI.Thresh}{NDVI threshold applied to produce a mask (select pixels with NDVI>NDVI.Thresh)} \item{NDVI.Thresh}{NDVI threshold applied to produce a mask (select pixels with NDVI>NDVI.Thresh)}
......
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