Skip to content

Commit

Permalink
code cleanup, removing all use of parallel package #10
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanbeaudette committed Feb 6, 2025
1 parent 70a5a91 commit 2711fc8
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 102 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ README.md
^revdep$
^CRAN-SUBMISSION$
^cran-comments\.md$
^doc$
^Meta$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ man/.Rhistory
docs/reference/*.pdf
tests/testthat/Rplots.pdf
docs
/doc/
/Meta/
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Depends: R (>= 3.5.0)
Imports: grDevices, graphics, methods, stats, utils, aqp, ape,
soilDB, cluster, lattice,
reshape2, scales, circular, RColorBrewer, plyr,
digest, e1071, stringi, parallel, curl, grid
digest, e1071, stringi, curl, grid
Additional_repositories: https://hydromad.github.io
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
Expand Down
82 changes: 40 additions & 42 deletions R/sample.by.poly.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@


constantDensitySampling <- function(x, polygon.id='pID', parallel=FALSE, cores=NULL, n.pts.per.ac=1, min.samples=5, sampling.type='regular') {
#' @title Sample Polygons at a Fixed Density
#'
#' @description
#' Perform constant-density sampling of polygons.
#'
#'
#' @param x either `SpatVector` or object that can be coerced into one, should contain polygons registered to a planar coordinate reference system
#' @param polygon.id character, name of column which contains a unique ID for each polygon
#' @param n.pts.per.ac numeric, sampling density in "points per acre"
#' @param min.samples integer, enforced minimum number of samples per polygon
#' @param sampling.type character, sampling type passed to [terra::spatSample()]
#'
#' @return `SpatVector` of sample points
#' @keywords manip
#' @author D.E. Beaudette
#'
#' @export
#'
constantDensitySampling <- function(x, polygon.id = 'pID', n.pts.per.ac = 1, min.samples = 5, sampling.type = 'regular') {

if (!requireNamespace("terra")) {
stop('please install the `terra` package', call. = FALSE)
Expand All @@ -21,43 +39,10 @@ constantDensitySampling <- function(x, polygon.id='pID', parallel=FALSE, cores=N
if (length(x) == 0)
return(NULL)

# retain proj4 information
# retain CRS information
p4s <- terra::crs(x)

# ## see https://github.com/ncss-tech/sharpshootR/issues/10
# ## NOT ready for prime time, usually slower than sequential
# ## https://github.com/ncss-tech/sharpshootR/issues/10
# ## requires slightly more RAM
# ## significant overhead
# ## not worth doing when nrow(x) < number of cores
if (parallel) {
.Deprecated(msg = "`parallel` argument is deprecated")

# establish possible number of CPU cores if not specified
if (is.null(cores)) {
# the smaller: available CPU cores | number of polygons
cores <- min(c(parallel::detectCores(), nrow(x)))
}

# init nodes
## TODO: optimal setting for useXDR ?
## TODO: makeCluster() [platform agnostic] or makePSOCKcluster() [Windoze]
cl <- parallel::makeCluster(cores, useXDR = TRUE)

# parallel sampling
# sample and return a list, one element / valid polygon
res <- parallel::parLapply(cl = cl, X = split(x, 1:length(x)),
fun = sample.by.poly,
n.pts.per.ac = n.pts.per.ac,
min.samples = min.samples,
sampling.type = sampling.type,
p4s = p4s)

# stop nodes
parallel::stopCluster(cl)
}



# sequential processing
# sample and return a list, one element / valid polygon
res <- lapply(
Expand Down Expand Up @@ -102,11 +87,24 @@ constantDensitySampling <- function(x, polygon.id='pID', parallel=FALSE, cores=N
}


# sample by polygon, must be from a projected CRS
# p: Polygon object
# n: number of points per acre (results will be close)
# min.samples: minimum requested samples / polygon
# p4s: proj4string assigned to SpatialPoints object

#' @title Sample a Single Polygon at a Target Density
#'
#' @description
#' Sample a single `SpatVector` polygon at a target density in "samples / acre".
#'
#'
#' @param p `SpatVector` containing a single polygon feature
#' @param n.pts.per.ac numeric, sampling density in "points per acre"
#' @param min.samples integer, enforced minimum number of samples per polygon
#' @param sampling.type sampling.type character, sampling type passed to [terra::spatSample()]
#' @param p4s output from [terra::crs()]
#'
#' @return `SpatVector` of sample points
#' @keywords manip
#' @author D.E. Beaudette
#'
#' @export
sample.by.poly <- function(p,
n.pts.per.ac = 1,
min.samples = 5,
Expand Down Expand Up @@ -147,7 +145,7 @@ sample.by.poly <- function(p,
return(NULL)
}

# assign original proj4 string
# assign original CRS
if (!is.null(p4s) & !is.null(s.i))
terra::crs(s.i) <- p4s

Expand Down
2 changes: 1 addition & 1 deletion R/sampleRasterStackByMU.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

## TODO:
## 1. abstract into smaller functions
## 2. optimise for parallel execution
## 2. optimize for parallel execution
## 3. keep track of time spent on various sub-tasks

## load raster stack into memory if possible
Expand Down
1 change: 0 additions & 1 deletion R/samplingStability.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
## TODO: parallel implementation

#' Estimate Sampling Stability
#'
Expand Down
66 changes: 35 additions & 31 deletions man/constantDensitySampling.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 35 additions & 26 deletions man/sample.by.poly.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2711fc8

Please sign in to comment.