Skip to content

Commit bd2daa2

Browse files
committed
Line length linting, CI integration.
1 parent be1b097 commit bd2daa2

21 files changed

Lines changed: 238 additions & 86 deletions

.github/workflows/R-CMD-check.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2+
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
3+
on:
4+
push:
5+
branches: [main, master]
6+
pull_request:
7+
8+
name: R-CMD-check.yaml
9+
10+
permissions: read-all
11+
12+
jobs:
13+
R-CMD-check:
14+
runs-on: ${{ matrix.config.os }}
15+
16+
name: ${{ matrix.config.os }} (${{ matrix.config.r }})
17+
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
config:
22+
- {os: macos-latest, r: 'release'}
23+
- {os: windows-latest, r: 'release'}
24+
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
25+
- {os: ubuntu-latest, r: 'release'}
26+
- {os: ubuntu-latest, r: 'oldrel-1'}
27+
28+
env:
29+
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
30+
R_KEEP_PKG_SOURCE: yes
31+
32+
steps:
33+
- uses: actions/checkout@v4
34+
35+
- uses: r-lib/actions/setup-pandoc@v2
36+
37+
- uses: r-lib/actions/setup-r@v2
38+
with:
39+
r-version: ${{ matrix.config.r }}
40+
http-user-agent: ${{ matrix.config.http-user-agent }}
41+
use-public-rspm: true
42+
43+
- uses: r-lib/actions/setup-r-dependencies@v2
44+
with:
45+
extra-packages: any::rcmdcheck
46+
needs: check
47+
48+
- uses: r-lib/actions/check-r-package@v2
49+
with:
50+
upload-snapshots: true
51+
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'

.github/workflows/pkgcheck.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: pkgcheck
2+
3+
# This will cancel running jobs once a new run is triggered
4+
concurrency:
5+
group: ${{ github.workflow }}-${{ github.head_ref }}
6+
cancel-in-progress: true
7+
8+
on:
9+
# Manually trigger the Action under Actions/pkgcheck
10+
workflow_dispatch:
11+
# Run on every push to main
12+
push:
13+
branches:
14+
- main
15+
- ropensci_submission
16+
17+
jobs:
18+
pkgcheck:
19+
runs-on: ubuntu-latest
20+
permissions:
21+
issues: write
22+
steps:
23+
- uses: ropensci-review-tools/pkgcheck-action@main

.github/workflows/test-coverage.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ jobs:
3434
clean = FALSE,
3535
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package")
3636
)
37+
print(cov)
3738
covr::to_cobertura(cov)
3839
shell: Rscript {0}
3940

40-
- uses: codecov/codecov-action@v4
41+
- uses: codecov/codecov-action@v5
4142
with:
4243
# Fail if error if not on PR, or if on PR and token is given
4344
fail_ci_if_error: ${{ github.event_name != 'pull_request' || secrets.CODECOV_TOKEN }}
44-
file: ./cobertura.xml
45-
plugin: noop
45+
files: ./cobertura.xml
46+
plugins: noop
4647
disable_search: true
4748
token: ${{ secrets.CODECOV_TOKEN }}
4849

R/as_draws.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@
4040
#' summarize_draws()
4141
#'
4242
#' # View the radial coordinate in the unit-space over the run
43-
#' dm_rad <- as_draws_rvars(ernest_run_example, units = "unit_cube", radial = TRUE)
43+
#' dm_rad <- as_draws_rvars(
44+
#' ernest_run_example,
45+
#' units = "unit_cube",
46+
#' radial = TRUE
47+
#' )
4448
#' plot(
4549
#' x = ernest_run_example$log_volume,
4650
#' y = draws_of(dm_rad$.radial),

R/calculate.R

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#' Estimate Evidence using a Nested Sampling Run
22
#'
3+
#' Computes evidence and related quantities from a nested
4+
#' sampling run, optionally by simulating the volumes of each nested
5+
#' likelihood shell.
6+
#'
37
#' @param x (ernest_run) An `ernest_run` object.
48
#' @inheritParams rlang::args_dots_empty
59
#' @param ndraws (positive integer or zero, optional) The number of log volume
@@ -22,21 +26,21 @@
2226
#' Each column is returned as an [posterior::rvar()] vector.
2327
#'
2428
#' @details
25-
#' Use `calculate()` to simulate the estimation error nested sampling runs caused
26-
#' by approximating the change in log volume between iterations. Given the
27-
#' use of ordered log likelihood restricted prior sampling, these volumes can be
28-
#' modelled as the order statistics of a uniform random variable. Ernest uses the
29-
#' following distributions to perform this simulation:
29+
#' Use `calculate()` to simulate the estimation error nested sampling runs
30+
#' caused by approximating the change in log volume between iterations. Given
31+
#' the use of ordered log likelihood restricted prior sampling, these volumes
32+
#' can be modelled as the order statistics of a uniform random variable.
33+
#' Ernest uses the following distributions to perform this simulation:
3034
#'
3135
#' * Constant number of live points: At each iteration, the point with the
3236
#' lowest likelihood is replaced by a new sample constrained to a higher
3337
#' likelihood region. Under this setup, the shrinkage in prior volume at
3438
#' iteration `i` can be shown to follow the Beta distribution.
3539
#' * Decreasing number of live points: At the end of the sampling run, the
36-
#' remaining live points are added to the dead point set. This introduces a discrete
37-
#' stepwise behavior in volume shrinkage. Ernest uses a transformation of variables
38-
#' drawn from the exponential distribution to simulate the corresponding changes
39-
#' in volume.
40+
#' remaining live points are added to the dead point set. This introduces a
41+
#' discrete stepwise behavior in volume shrinkage. Ernest uses a transformation
42+
#' of variables drawn from the exponential distribution to simulate the
43+
#' corresponding changes in volume.
4044
#'
4145
#' @references See Appendix A6 of Speagle, J. (2020).
4246
#
@@ -158,9 +162,11 @@ summary.ernest_estimate <- function(object, ...) {
158162
format.ernest_estimate <- function(x, ...) {
159163
cli::cli_format_method({
160164
cli::cli_div(theme = list(.val = list(digits = 3)))
161-
cli::cli_bullets(
162-
"An {.cls ernest_calculate}: {attr(x, 'ndraws')} draws x {length(x$log_lik)} iterations"
163-
)
165+
ndraws <- attr(x, "ndraws")
166+
iter <- length(x$log_lik)
167+
cli::cli_bullets(c(
168+
"An {.cls ernest_calculate}: {ndraws} draws x {iter} iterations"
169+
))
164170
})
165171
}
166172

R/likelihood-class.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#' Prepare a likelihood function for nested sampling
22
#'
33
#' Creates a modified version of a log. likelihood function that always returns
4-
#' either a finite value or `-Inf` for each vector of parameters that is provided.
4+
#' either a finite value or `-Inf` for each vector of parameters that is
5+
#' provided.
56
#'
67
#' @param fn (uni-variate function) The log-likelihood function (see Details).
78
#' @param error_action (case-sensitive string) Action to perform once `fn`
@@ -28,9 +29,9 @@
2829
#' cause errors.
2930
#'
3031
#' @details
31-
#' Model likelihoods should be provided as a log density function. It is expected
32-
#' that `fn` should take in exactly one argument; likelihood functions that
33-
#' take in multiple non-default arguments should be entered as anonymous
32+
#' Model likelihoods should be provided as a log density function. It is
33+
#' expected that `fn` should take in exactly one argument; likelihood functions
34+
#' that take in multiple non-default arguments should be entered as anonymous
3435
#' functions (see [rlang::as_function()]).
3536
#'
3637
#' It is expected that `fn` returns a scalar finite values or `-Inf` for each
@@ -50,7 +51,8 @@
5051
#' @srrstats {G2.14, G2.14a, G2.14b, G2.14c, G2.15} create_likelihood catches
5152
#' missing values produced during a run and acts upon them based on user-desired
5253
#' behaviour.
53-
#' @srrstats {G2.16} Value handling is also performed for other undefined values.
54+
#' @srrstats {G2.16} Value handling is also performed for other undefined
55+
#' values.
5456
#'
5557
#' @examples
5658
#' # A 3D Gaussian likelihood function
@@ -264,7 +266,7 @@ format.ernest_likelihood <- function(x, ...) {
264266
">" = "{.arg nonfinite_action} = {.val {attr(x, 'nonfinite_action')}}"
265267
))
266268
cli::cat_line()
267-
cli::cli_code(format(attr(x, 'body')))
269+
cli::cli_code(format(attr(x, "body")))
268270
})
269271
}
270272

R/lrps-class.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ ernest_lrps <- R6Class(
3838

3939
update = function(...) {
4040
private$n_call <- 0L
41-
return(self)
41+
self
4242
},
4343

4444
find_point = function(unit) {
@@ -109,7 +109,8 @@ ernest_lrps <- R6Class(
109109

110110
#' Uniform LRPS Sampler (Internal)
111111
#'
112-
#' Internal R6 subclass for uniform sampling in the unit cube under a likelihood constraint.
112+
#' Internal R6 subclass for uniform sampling in the unit cube under a likelihood
113+
#' constraint.
113114
#'
114115
#' @keywords internal
115116
#' @noRd
@@ -152,7 +153,8 @@ uniform_lrps <- R6Class(
152153

153154
#' Random Walk LRPS Sampler (Internal)
154155
#'
155-
#' Internal R6 subclass for random walk sampling in the unit cube with adaptive step size.
156+
#' Internal R6 subclass for random walk sampling in the unit cube with adaptive
157+
#' step size.
156158
#'
157159
#' @param steps Number of random walk steps.
158160
#' @param epsilon Initial step size.

R/nested_sampling.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@
2727
#'
2828
#' @return An [ernest_sampler] object, prepared for nested sampling.
2929
#'
30+
#' @details
31+
#' This function prepares an `ernest_sampler` object for performing nested
32+
#' sampling, given a log-likelihood function, prior, and sampler specification.
33+
#' It checks that the provided log-likelihood and prior are compatible by
34+
#' performing a basic sanity check by evaluating the log-likelihood at the
35+
#' center of the prior space. This check ensures that the log-likelihood
36+
#' returns a finite value or `-Inf` for typical parameter values.
37+
#'
38+
#' The sanity check may fail even if `log_lik` is well-specified, for example
39+
#' if the prior's support does not include the test point
40+
#' (the center of the prior hypercube), or if the log-likelihood is only
41+
#' defined for a subset of the parameter space. In such cases, silencing the
42+
#' warning from `nested_sampling`` may require you to adjust the prior or
43+
#' adjust the `on_warning` argument.
44+
#'
3045
#' @export
3146
#' @examples
3247
#' prior <- create_uniform_prior(n_dim = 2, lower = -1, upper = 1)

R/plot.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#'
1515
#' The plot is faceted in three frames. The horizontal axis shows values of log.
1616
#' volume: If `x` is an `ernest_run` these estimates are derived from the run,
17-
#' if `x` is an `ernest_estimate` (or `ndraws != 0`), these values are simulated.
17+
#' if `x` is an `ernest_estimate` (or `ndraws != 0`), these values are
18+
#' simulated.
19+
#'
1820
#' The three `y` axes are as follows:
1921
#'
2022
#' * **Evidence:** Estimate with a corresponding error ribbon drawn from either
@@ -28,11 +30,12 @@
2830
#' an error ribbon is drawn with the HDCI of this estimate.
2931
#'
3032
#' @note For `ernest_estimate`, `ndraws` must be sufficiently large to calculate
31-
#' HDI. If HDI calculation fails, the user will be warned and an `ernest_run` plot
32-
#' will be generated instead.
33+
#' HDI. If HDI calculation fails, the user will be warned and an `ernest_run`
34+
#' plot will be generated instead.
3335
#'
34-
#' @seealso [calculate()] for generating `ernest_estimate` objects; also, [visualize()] to
35-
#' plot the posterior distributions of the parameters from a run.
36+
#' @seealso [calculate()] for generating `ernest_estimate` objects; also,
37+
#' [visualize()] to plot the posterior distributions of the parameters from a
38+
#' run.
3639
#'
3740
#' @rdname plot.ernest
3841
#' @export

R/run-class.R

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,18 @@ new_ernest_run <- function(res, n_live, n_dead, live_order, seed) {
132132
#' @export
133133
format.ernest_run <- function(x, ...) {
134134
smry <- summary(x)
135+
log_z <- smry$log_evidence
136+
log_z_sd <- smry$log_evidence_err
135137
cli::cli_format_method({
136138
cli::cli_div(theme = list(.val = list(digits = 3)))
139+
first_line <- sprintf(
140+
"%s %s",
141+
"An {.cls ernest_run}:",
142+
"{x$n_points} points x {x$n_iter} iter x {x$n_calls} lik. calls"
143+
)
137144
cli::cli_bullets(c(
138-
"An {.cls ernest_run}: {x$n_points} points x {x$n_iter} iter x {x$n_calls} lik. calls",
139-
">" = "Log. Evidence: {.val {smry$log_evidence}} \U00B1 {.val {smry$log_evidence_err}}"
145+
first_line,
146+
">" = "Log. Evidence: {.val {log_z}} \U00B1 {.val {log_z_sd}}"
140147
))
141148
})
142149
}
@@ -157,8 +164,8 @@ print.ernest_run <- function(x, ...) {
157164
#' @return An object of class `summary.ernest_run`, a list with:
158165
#' * `n_iter`: Number of iterations (number of dead points).
159166
#' * `n_points`: Number of live points at the end of the run.
160-
#' * `log_volume`, `log_evidence`, `log_evidence_err`: The final estimates of the
161-
#' quantities performed by the run that generated `object`.
167+
#' * `log_volume`, `log_evidence`, `log_evidence_err`: The final estimates of
168+
#' the quantities performed by the run that generated `object`.
162169
#' * `run`, A tibble with `n_iter + n_points` rows, containing the vectors
163170
#' `call`, `log_lik`, `log_volume`, `log_weight`, `log_evidence`,
164171
#' `log_evidence_err`, and `information`.

0 commit comments

Comments
 (0)