Skip to content

Instantly share code, notes, and snippets.

Nick Golding goldingn

Block or report user

Report or block goldingn

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
goldingn / what_is_my_name.R
Created Nov 8, 2019
playing with letting R objects know their own names
View what_is_my_name.R
# how to make R objects know their own names?
# this works with commands run in the global environment, but not inside functions
this_call <- function() {
file1 <- tempfile("Rrawhist")
rawhist <- readLines(file1)
goldingn / gradient_box_plot.R
Created Feb 23, 2019
a box-esque plot with a greyscale gradient showing the density of data
View gradient_box_plot.R
# box-ish plot with colour gradient giving smoothed densities
# some fake values
x <- c(rnorm(150, -0.5, 1.3),
rnorm(30, -1, 0.8),
rnorm(20, 0.1, 0.6))
# how smooth the gradient is
n_levels <- 100
goldingn / required_data nodes.R
Created Sep 25, 2018
recursive function to find all the data nodes on which an operation node depends (up until some stopping nodes) - not required, but stashed here in case some of the ideas are
View required_data nodes.R
# recursively find all the data nodes that this one depends on, stopping the
# search at any nodes that are data or whose unique names are in stop_at
required_data_nodes <- function (node, stop_at = c(), data_nodes = list()) {
name <- node$unique_name
if (!name %in% stop_at) {
# if a data node, record and stop this search branch (data has no children)
goldingn / multiple_progress.R
Last active May 21, 2018
multiple progress bars on a single line, to be combined with parallel_progress.R
View multiple_progress.R
# plotting multiple progress bars on the same line, as a precursor to running
# the progress bars in parallel
library (progress)
library (future)
library (R6)
new_connection <- function () {
f <- tempfile()
goldingn / fft_gp_prototype.R
Last active Sep 13, 2018
a prototype interface to defining Gaussian processes on a computational grid efficiently with greta, using the Fast Fourier Transform
View fft_gp_prototype.R
# FFT approximation to a GP on a regular grid (defined by a raster)
# information representing a grid of points, defined by the x and y coordinates
# fft_grid <- function (x_coord, y_coord) {
# # pre calculate grid info
# dx <- x_coord[2] - x_coord[1]
# dy <- y_coord[2] - y_coord[1]
# m <- length(x_coord)
# n <- length(y_coord)
goldingn / greta_marginalise_poisson_rv.R
Last active Apr 27, 2018
prototype of helper functions for marginalising a Poisson random variable in a greta model
View greta_marginalise_poisson_rv.R
# marginalise over a Poisson random variable in a greta model
# likelihood function must be a function taking a single value of N (drawn from
# N ~ Poisson(lambda)), and returning a distribution. Lambda is a (possibly
# variable) scalar greta array for the rate of the poisson distribution. max_n
# is a scalar positive integer giving the maximum value of N to consider when
# marginalising the Poisson distribution
marginal_poisson <- function (likelihood_function, lambda, max_n) {
n_seq <- seq_len(max_n)
wt <- poisson_weights(n_seq, lambda)
goldingn / parallel_progress.R
Created Apr 14, 2018
prototype of parallel progress reporting (for processes on the same file system)
View parallel_progress.R
# progress information in parallel processes (that use the same filesystem)
# the master function sets up a tempfile for each process, spawns processes, and
# passes the corresponding tempfile location to each; each process dumps
# progress information into its tempfile; the master function polls those files
# for the progress information and returns it to the screen; the previous line
# is overwritten, as for progress bars
library (future)
# an environment to stash file info in, to hack around scoping issues. A package
goldingn / greta_ode_prototype.R
Last active Oct 2, 2018
A prototype and demonstration of solving ODEs with greta
View greta_ode_prototype.R
# prototype ODE solver function for greta
# user-facing function to export:
# derivative must be a function with the first two arguments being 'y' and 't',
# and subsequent named arguments representing (temporally static) model
# parameters
# y0 must be a greta array representing the shape of y at time 0
# times must be a column vector of times at which to evaluate y
# dots must be named greta arrays for the additional (fixed) parameters
ode_solve <- function (derivative, y0, times, ...) {
goldingn / tensorflow_hmc_hack.R
Last active Feb 20, 2018
hack greta v0.2.4 to use tensorflow HMC
View tensorflow_hmc_hack.R
# get greta working with bayesflow's HMC implementation & working via
# tensorflow's run syntax
build_function <- function (dag) {
# temporarily pass float type info to options, so it can be accessed by
# nodes on definition, without clunky explicit passing
old_float_type <- options()$greta_tf_float
on.exit(options(greta_tf_float = old_float_type))
options(greta_tf_float = dag$tf_float)
View shimming errors
# lookup table of error messages (coud be read in from a file in the package)
lookup <- cbind(from = "there is no package called ‘pineapples’",
to = "no pineapples here!")
# swap over the message if there's a better one in the lookup
swap_message <- function (message) {
idx <- match(message, lookup[, "from"])
if (length(idx == 1) && !
message <- lookup[idx, "to"]
You can’t perform that action at this time.