Skip to content

Commit 4b47d1a

Browse files
authored
Merge pull request #124 from jfisher-usgs/master
merge with upstream
2 parents aa60bda + 928a6bf commit 4b47d1a

29 files changed

+191
-165
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
^Makefile$
66
^\.travis\.yml$
77
^appveyor\.yml$
8+
^vignettes/cache$

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/.Rhistory
33
/*.tar.gz
44
/*.zip
5+
/vignettes/cache/*

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Suggests:
4444
remotes,
4545
roxygen2,
4646
testthat
47+
SystemRequirements: pandoc - https://pandoc.org/
4748
License: CC0
4849
Copyright: This software is in the public domain because it contains materials
4950
that originally came from the USGS, an agency of the

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# inlmisc 0.4.7.9000
22

3+
- In `FindOptimalSubset` function, add `numIslands` argument, used to specify the number of islands,
4+
was previously dependent on the `parallel` argument;
5+
change default value of `elitism` argument from 0 to 5-percent of the island population.
6+
37
- Add *misc/latex-packages.txt* file: contains a list of required LaTeX packages
48
that are not included in the default installation of [TinyTeX](https://yihui.name/tinytex/);
59
use the `inlmisc:::InstallLatexPackages()` command to install these packages into TinyTeX.

R/AddPoints.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
#' quantile.breaks = TRUE, add = FALSE)
131131
#'
132132
#' z <- as.factor(rep(c("dog", "cat", "ant", "pig", "bat"),
133-
#' length.out = n))
133+
#' length.out = n))
134134
#' bg <- GetColors(nlevels(z), scheme = "bright", alpha = 0.8)
135135
#' AddPoints(x, z = z, bg = bg, add = FALSE)
136136
#'

R/BuildVignettes.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ BuildVignettes <- function(dir=getwd(), doc=file.path(dir, "inst/doc"),
3333
gs_quality=c("ebook", "printer", "screen", "none"),
3434
clean=TRUE, quiet=TRUE) {
3535

36-
checkmate::assertFileExists(file.path(dir, "DESCRIPTION"))
36+
checkmate::assertString(dir)
3737
checkmate::assertPathForOutput(doc, overwrite=TRUE)
3838
gs_quality <- match.arg(gs_quality)
3939
checkmate::assertFlag(clean)
4040
checkmate::assertFlag(quiet)
4141

42+
dir <- normalizePath(path.expand(dir), winslash="/", mustWork=FALSE)
43+
checkmate::assertFileExists(file.path(dir, "DESCRIPTION"))
44+
4245
tools::buildVignettes(dir=dir, quiet=quiet, clean=clean, tangle=TRUE)
4346

4447
v <- tools::pkgVignettes(dir=dir, output=TRUE, source=TRUE)
@@ -48,7 +51,7 @@ BuildVignettes <- function(dir=getwd(), doc=file.path(dir, "inst/doc"),
4851
if (v$dir != doc) {
4952
dir.create(doc, showWarnings=!quiet, recursive=TRUE)
5053
file.copy(c(v$docs, out), doc, overwrite=TRUE)
51-
file.remove(out)
54+
if (clean) file.remove(out)
5255
}
5356

5457
if (gs_quality != "none") {

R/FindOptimalSubset.R

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#' Additional arguments to be passed to the fitness function.
2121
#' @param popSize 'integer' count.
2222
#' Population size
23+
#' @param numIslands 'integer' count.
24+
#' Number of islands
2325
#' @param migrationRate 'numeric' number.
2426
#' Proportion of individuals that should migrate between islands.
2527
#' @param migrationInterval 'integer' count.
@@ -31,6 +33,7 @@
3133
#' Probability of mutation in a parent chromosome.
3234
#' @param elitism 'integer' count.
3335
#' Number of chromosomes to survive into the next generation.
36+
#' Defaults to 5-percent of the island population.
3437
#' @param maxiter 'integer' count.
3538
#' Maximum number of iterations to run before the GA search is halted.
3639
#' @param run 'integer' count.
@@ -41,8 +44,7 @@
4144
#' See returned \code{solution} component for a suggested value for this arugment.
4245
#' @param parallel 'logical' flag or 'integer' count.
4346
#' Whether to use parallel computing.
44-
#' This argument can also be used to specify the number of cores
45-
#' (and number of islands) to employ; by default,
47+
#' This argument can also be used to specify the number of cores to employ; by default,
4648
#' this is the number of physical CPUs/cores.
4749
#' The \pkg{parallel} and \pkg{doParallel} packages must be
4850
#' installed for parallel computing to work.
@@ -98,8 +100,9 @@
98100
#' @export
99101
#'
100102
#' @examples
101-
#' # Problem: Choose the 4 smallest numbers from a list of 100 values
102-
#' # genearated from a standard uniform distribution.
103+
#' # Problem: Choose the 4 smallest numbers from a list
104+
#' # of 100 values genearated from a standard
105+
#' # uniform distribution.
103106
#' k <- 4
104107
#' n <- 100
105108
#' seed <- 123
@@ -109,19 +112,19 @@
109112
#' -1 * sum(numbers[idxs])
110113
#' }
111114
#' \dontrun{
112-
#' out <- FindOptimalSubset(n, k, Fitness, numbers, elitism = 1,
113-
#' run = 10, monitor = GA::gaislMonitor,
114-
#' seed = seed)
115+
#' out <- FindOptimalSubset(n, k, Fitness, numbers, run = 10,
116+
#' monitor = GA::gaislMonitor, seed = seed)
115117
#' plot(out[["ga_output"]])
116118
#' summary(out[["ga_output"]])
117119
#' print(out[["solution"]])
118120
#' print(out[["ga_output"]]@fitnessValue)
119121
#' }
120122
#'
121123

122-
FindOptimalSubset <- function(n, k, Fitness, ..., popSize=100,
124+
FindOptimalSubset <- function(n, k, Fitness, ..., popSize=100, numIslands=4,
123125
migrationRate=0.1, migrationInterval=10,
124-
pcrossover=0.8, pmutation=0.1, elitism=0,
126+
pcrossover=0.8, pmutation=0.1,
127+
elitism=max(1, round(popSize/numIslands*0.05)),
125128
maxiter=1000, run=maxiter, suggestions=NULL,
126129
parallel=TRUE, monitor=NULL, seed=NULL) {
127130

@@ -130,11 +133,12 @@ FindOptimalSubset <- function(n, k, Fitness, ..., popSize=100,
130133
checkmate::assertInt(k, lower=1, upper=n - 1)
131134
checkmate::assertFunction(Fitness)
132135
checkmate::assertInt(popSize, lower=1)
136+
checkmate::assertInt(numIslands, lower=1)
133137
checkmate::assertNumber(migrationRate, lower=0, upper=1, finite=TRUE)
134138
checkmate::assertInt(migrationInterval, lower=1)
135139
checkmate::assertNumber(pcrossover, lower=0, upper=1, finite=TRUE)
136140
checkmate::assertNumber(pmutation, lower=0, upper=1, finite=TRUE)
137-
checkmate::assertInt(elitism, lower=0, upper=popSize)
141+
checkmate::assertInt(elitism, lower=1)
138142
checkmate::assertInt(maxiter, lower=1)
139143
checkmate::assertInt(run, lower=1, upper=maxiter)
140144
checkmate::assertMatrix(suggestions, min.rows=1, min.cols=1, null.ok=TRUE)
@@ -143,12 +147,6 @@ FindOptimalSubset <- function(n, k, Fitness, ..., popSize=100,
143147
if (is.null(monitor)) monitor <- FALSE
144148
checkmate::assertInt(seed, null.ok=TRUE)
145149

146-
# set number of islands
147-
if (is.logical(parallel))
148-
numIslands <- if (parallel) parallel::detectCores(logical=FALSE) else 4L
149-
else
150-
numIslands <- parallel
151-
152150
# calculate number of bits in the binary string representing the chromosome
153151
nBits <- .CountBits(n) * k
154152

R/GetRegionOfInterest.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@
5454
#' r <- sqrt(stats::runif(n, 0.25^2, 0.50^2))
5555
#' x <- sp::SpatialPoints(cbind(0.5 + r * cos(theta), 0.5 + r * sin(theta)),
5656
#' proj4string = sp::CRS("+init=epsg:32610"))
57-
#' sp::plot(GetRegionOfInterest(x, alpha = 0.1, width = 0.05), col = "green")
58-
#' sp::plot(GetRegionOfInterest(x, alpha = 0.1), col = "yellow", add = TRUE)
57+
#' sp::plot(GetRegionOfInterest(x, alpha = 0.1, width = 0.05),
58+
#' col = "green")
59+
#' sp::plot(GetRegionOfInterest(x, alpha = 0.1),
60+
#' col = "yellow", add = TRUE)
5961
#' sp::plot(x, add = TRUE)
6062
#' }
6163
#'

R/Grid2Polygons.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@
123123
#' m <- m[nrow(m):1, ncol(m):1]
124124
#' x <- seq(from = 2667405, length.out = ncol(m), by = 10)
125125
#' y <- seq(from = 6478705, length.out = nrow(m), by = 10)
126-
#' r <- raster::raster(m, xmn = min(x), xmx = max(x), ymn = min(y), ymx = max(y),
127-
#' crs = "+init=epsg:27200")
126+
#' r <- raster::raster(m, xmn = min(x), xmx = max(x), ymn = min(y),
127+
#' ymx = max(y), crs = "+init=epsg:27200")
128128
#' plys <- Grid2Polygons(r, level = TRUE)
129129
#' cols <- GetColors(length(plys), scheme = "DEM screen")
130130
#' sp::plot(plys, col = cols, border = "#515151")

R/PlotCrossSection.R

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,20 @@
8080
#' m <- m[nrow(m):1, ncol(m):1]
8181
#' x <- seq(from = 2667405, length.out = ncol(m), by = 10)
8282
#' y <- seq(from = 6478705, length.out = nrow(m), by = 10)
83-
#' r1 <- raster::raster(m, xmn = min(x), xmx = max(x), ymn = min(y), ymx = max(y),
84-
#' crs = "+init=epsg:27200")
83+
#' r1 <- raster::raster(m, xmn = min(x), xmx = max(x), ymn = min(y),
84+
#' ymx = max(y), crs = "+init=epsg:27200")
8585
#' r2 <- min(r1[]) - r1 / 10
8686
#' r3 <- r1 - r2
8787
#' rs <- raster::stack(r1, r2, r3)
8888
#' names(rs) <- c("r1", "r2", "r3")
89-
#' xy <- rbind(c(2667508, 6479501), c(2667803, 6479214), c(2667508, 6478749))
90-
#' transect <- sp::SpatialLines(list(sp::Lines(list(sp::Line(xy)), ID = "Transect")),
89+
#' xy <- rbind(c(2667508, 6479501),
90+
#' c(2667803, 6479214),
91+
#' c(2667508, 6478749))
92+
#' transect <- sp::Lines(list(sp::Line(xy)), ID = "Transect")
93+
#' transect <- sp::SpatialLines(list(transect),
9194
#' proj4string = raster::crs(rs))
92-
#' xy <- rbind(c(2667705, 6478897), c(2667430, 6479178))
95+
#' xy <- rbind(c(2667705, 6478897),
96+
#' c(2667430, 6479178))
9397
#' p <- sp::SpatialPoints(xy, proj4string = raster::crs(rs))
9498
#' d <- data.frame("label" = c("Peak", "Random"))
9599
#' features <- sp::SpatialPointsDataFrame(p, d, match.ID = TRUE)
@@ -98,10 +102,13 @@
98102
#' PlotMap(r1, bg.image = bg.image,
99103
#' pal = GetColors(scheme = "DEM screen", alpha = 0.8),
100104
#' scale.loc = "top", arrow.loc = "topright",
101-
#' contour.lines = list("col" = "#1F1F1FA6"), "useRaster" = TRUE)
105+
#' contour.lines = list("col" = "#1F1F1FA6"),
106+
#' useRaster = TRUE)
102107
#' lines(transect)
103-
#' raster::text(as(transect, "SpatialPoints"), labels = c("A", "BEND", "A'"),
104-
#' halo = TRUE, cex = 0.7, pos = c(3, 4, 1), offset = 0.1, font = 4)
108+
#' raster::text(as(transect, "SpatialPoints"),
109+
#' labels = c("A", "BEND", "A'"),
110+
#' halo = TRUE, cex = 0.7, pos = c(3, 4, 1),
111+
#' offset = 0.1, font = 4)
105112
#' points(features, pch = 19)
106113
#' raster::text(features, labels = features@data$label, halo = TRUE,
107114
#' cex = 0.7, pos = 4, offset = 0.5, font = 4)
@@ -110,16 +117,18 @@
110117
#' asp <- 5
111118
#' unit <- "METERS"
112119
#' explanation <- "Vertical thickness between layers, in meters."
113-
#' PlotCrossSection(transect, rs, geo.lays = c("r1", "r2"), val.lays = "r3",
114-
#' ylab = "Elevation", asp = asp, unit = unit,
115-
#' explanation = explanation, features = features,
116-
#' max.feature.dist = 100, bg.col = "#E1E1E1",
117-
#' bend.label = "BEND IN\nSECTION", scale.loc = NULL)
120+
#' PlotCrossSection(transect, rs, geo.lays = c("r1", "r2"),
121+
#' val.lays = "r3", ylab = "Elevation", asp = asp,
122+
#' unit = unit, explanation = explanation,
123+
#' features = features, max.feature.dist = 100,
124+
#' bg.col = "#E1E1E1", bend.label = "BEND IN\nSECTION",
125+
#' scale.loc = NULL)
118126
#' AddScaleBar(unit = unit, vert.exag = asp, inset = 0.05)
119127
#'
120-
#' val <- PlotCrossSection(transect, rs, geo.lays = c("r1", "r2"), val.lays = "r3",
121-
#' ylab = "Elevation", asp = 5, unit = "METERS",
122-
#' explanation = explanation, file = "Rplots.png")
128+
#' val <- PlotCrossSection(transect, rs, geo.lays = c("r1", "r2"),
129+
#' val.lays = "r3", ylab = "Elevation", asp = 5,
130+
#' unit = "METERS", explanation = explanation,
131+
#' file = "Rplots.png")
123132
#' print(val)
124133
#'
125134
#' graphics.off()

0 commit comments

Comments
 (0)