Skip to content

Instantly share code, notes, and snippets.

View johnbaums's full-sized avatar

John johnbaums

  • University of Melbourne
  • Melbourne
View GitHub Profile
@johnbaums
johnbaums / error_bands_lattice.R
Created January 13, 2020 04:15
Shaded, semi-transparent uncertainty/error bands for lattice
# based on https://stackoverflow.com/q/51209112/489704
library(lattice)
library(dplyr)
library(tidyr)
my.panel.bands <- function(x, y, upper, lower, fill, col, subscripts, ...,
font, fontface) {
upper <- upper[subscripts]
lower <- lower[subscripts]
panel.polygon(c(x, rev(x)), c(upper, rev(lower)), col=fill, border=FALSE, ...)
@johnbaums
johnbaums / kappa.R
Created December 16, 2019 03:09
Calculate Cohen's Kappa from a vector of binary observations and a vector of continuous or binary predictions
confusion <- function(obs, pred, thr=NULL) {
# obs: a vector of observed binary values (0, 1)
# pred: a vector of continuous predictions (range: 0-1)
# thr: a numeric scalar specifying the value at which to threshold
# `pred`, or NULL if pred is already binary.
if(is.null(thr) && !all(pred %in% 0:1)) {
stop('If thr=NULL, all pred must be either 0 or 1.')
}
if(any(pred > 1) || any(pred < 0)) {
stop('pred must all be between 0 and 1 (inclusive).')
@johnbaums
johnbaums / get_namespaces.R
Last active February 8, 2022 14:02
Get the unique set of namespaces specified in an R script
get_namespaces <- function(file, as_imports=FALSE) {
require(stringi)
require(dplyr)
nm <- readLines(file) %>%
stringi::stri_extract_all(regex='[\\w_.]+::[\\w_.]+') %>%
unlist %>%
setdiff(NA) %>%
sort
@johnbaums
johnbaums / awazon-linux-gdal-installation.sh
Created October 24, 2019 03:55 — forked from hervenivon/awazon-linux-gdal-installation.sh
Install GEOS, PROJ4 & GDAL on amazon linux
export PYTHON_VERSION=3.4.3
export PYTHON_SHORT_VERSION=3.4
export GEOS_VERSION=3.6.2
export GDAL_VERSION=2.2.2
export PROJ4_VERSION=4.9.3
sudo yum-config-manager --enable epel
sudo yum install gdal-python
sudo yum -y install make automake gcc gcc-c++ libcurl-devel proj-devel geos-devel
@johnbaums
johnbaums / fillnodata.R
Last active September 5, 2019 06:46
Fill no data cells with focal mean/median
# Fill nodata within n x n cell window of cells that have data.
# Note that this can result in blocks n x n homogeneous blocks
# of a particular value extending out from a corner.
library(raster)
library(magrittr)
n <- 5 # width and height of window (in cells)
# note that n = 5 means that cells within 2 cells of a data cell
# will be affected (think of the focal cell as being in the middle
@johnbaums
johnbaums / cellBoundaries.R
Last active September 3, 2019 14:01
Return a list of extents for each cell in a raster
cellBoundaries <- function(r, spatial=TRUE) {
# r: a Raster object
# spatial: return an sfc object (TRUE) or a list of extents (FALSE)
require(raster)
if(isTRUE(spatial)) require(sf)
ee <- lapply(seq_along(r), function(i) {
cat(sprintf('\r%0.2f%%', 100*i/ncell(r)))
e <- extentFromCells(r, i)
if(isTRUE(spatial)) {
e <- st_as_sfc(st_bbox(e))
@johnbaums
johnbaums / fastCellFromPolygon.R
Last active August 15, 2019 15:35
Fast raster-polygon overlays, returning proportional cell coverage. Much faster than raster::cellFromPolygon.
fastCellFromPolygon <- function(r, p, layer, xy=FALSE, where, precision=0.1, normalizeWeights=FALSE) {
# r: a Raster* object defining cell arrangement, or path to a raster file.
# p: file path to a polygon vector dataset.
# layer: Name of layer to use. Ignored if datasource has only one layer.
# xy: should xy coordinates be returned? (default = FALSE, cell numbers are returned)
# where: optional SQL style WHERE statement, e.g. 'where id="123"' (quotes are important).
# precision: disaggregation factor, multiplicative factor determining
# sub-pixel resolution at which to determine cell coverage. For equivalency
# with raster::cellFromPolygon, use 0.1.
# normalizeWeights: should proportional coverage be normalised to sum to 1
@johnbaums
johnbaums / get_bionet.R
Last active July 9, 2019 05:26
Download species sighting data using the BIONET API (https://data.bionet.nsw.gov.au/)
get_bionet <- function(x, username, password, outdir, prefix='bionet_',
filetype='csv', matchtype='startswith',
return_data=FALSE, quiet=FALSE, verbose=FALSE) {
# x: a vector of species names
# username, password (optional): credentials for BIONET service
# outdir: output directory (must exist)
# prefix: csv/rds files will be saved in outdir following the pattern
# outdir/prefix_Genus_species_yyyymmddHHMMSS.csv/rds
# filetype: must be one of csv or rds
# matchtype: must be one of 'startswith', 'contains', or 'equals'.
@johnbaums
johnbaums / exdet.R
Last active April 5, 2023 11:38
Faster extrapolation detection (ExDet) based on ecospat::ecospat.climan
exdet <- function (ref, p, mic=TRUE, tol, quiet=FALSE) {
# ref: data.frame of environments at reference locations.
# p: data.frame of environments to assess.
# mic: should the most influential covariates also be returned?
# quiet: should messages be suppressed?
# tol: tolerance, passed to mahalanobis(). See ?solve.
# =============================================================
# When passing `mic=TRUE`, a data.frame will be returned with 3
# columns giving the exdet score (D), the most influential
# covariate with respect to type 1 novelty (MIC1; equivalent to
@johnbaums
johnbaums / pbsapply.R
Last active May 27, 2019 01:27
sapply with progress text (percent complete)
pbsapply <- function(x, fun, pass_to, ...) {
if(!missing('pass_to')) {
call <- sprintf('fun(%s=x[[i]], ...)', pass_to)
} else {
call <- 'fun(x[[1]], ...) '
}
sapply(seq_along(x), function(i) {
cat(sprintf('\r%.02f%%', i/length(x)*100))
eval(parse(text=call))
})