Skip to content

Instantly share code, notes, and snippets.

View Sleepingwell's full-sized avatar

Simon Knapp Sleepingwell

  • Canberra, ACT, Australia
View GitHub Profile
@Sleepingwell
Sleepingwell / Makefile
Last active January 12, 2023 03:12
A minimal example of calling a python function via a callback passed to a fortran subroutine.
.PHONY: venv
VENV_PATH=venv
TIMESTAMP_FILE=$(VENV_PATH)/.f2pytest-creation-timestamp
test: venv test.f90
. $(VENV_PATH)/bin/activate && \
python -m numpy.f2py -c test.f90 -m test
venv: $(TIMESTAMP_FILE)
@Sleepingwell
Sleepingwell / cla.md
Created July 29, 2022 00:29
CLA for apsrm

apsrm

Contributor License Agreement ("Agreement") v1.1.0

Thank you for your interest in the apsrm open source Project which is being administered by CSIRO Data61. This document describes the terms under which You may contribute software, bug fixes, configuration changes, documentation, or any other materials that you send to Us related to the Project (each a "Contribution") to the Project. This contributor license agreement ("Agreement") documents the rights granted by You to Us with respect to your Contributions.

Once agreed, these terms apply not only to your initial Contribution but also to all past Contributions and subsequent Contributions by You to the Project, unless we change the terms. When we change the terms, you will be asked to sign the new version of the agreement the next time you contribute.

We appreciate your participation in our Project, and your help in improving the Project, so we want you to understand what will be done with the Contributions. This license is for your protection

# Script for extracting various data from https://doi.org/10.1371/journal.pcbi.1005697.s002
#-------------------------------------------------------------------------------
# Contact matricies for Australia
#-------------------------------------------------------------------------------
library(openxlsx)
AusOtherLocationsContactMatrix <- as.matrix(read.xlsx(
'contact_matrices_152_countries/MUestimates_other_locations_1.xlsx',
sheet='Australia'))
save(AusOtherLocationsContactMatrix,
@Sleepingwell
Sleepingwell / git_submodule_commands.md
Created December 18, 2015 03:29
Summary of git commands for submodules

This is based on this very informative post.

Configuration settings

  • diff.submodule = log (so you get clearer container diffs when referenced submodule commits changed).
  • fetch.recurseSubmodules = on-demand (so you are confident new referenced commits for known submodules get fetched with container updates).
  • status.submoduleSummary = true (so git status gets useful again when a referenced submodule commit changed).

Adding or cloning

  • Initial add: git submodule add
@Sleepingwell
Sleepingwell / gist:8588c5ee844ce0242d05
Created November 3, 2014 03:50
Holding a large number of SEXPs in C++
#include <vector>
#include <utility>
#include <iterator>
#include <R.h>
#include <Rdefines.h>
template<typename Iter>
SEXP makePolygon(Iter b, Iter e) {
SEXP
coords;
@Sleepingwell
Sleepingwell / gist:6418936
Last active December 22, 2015 04:39
An example of using showdown to parse markdown in a web page + syntax highlighting.
<html>
<!-- based example found at: http://blog.harakys.com/blog/2012/02/21/embed-markdown-into-your-html/ -->
<header>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
<script src="https://google-code-prettify.googlecode.com/svn/loader/prettify.js"></script>
<script src="http://www.showdown.im/showdown/example/showdown.js"> </script>
<script type="text/javascript">
var generateHTML = function() {
@Sleepingwell
Sleepingwell / gist:6044051
Created July 20, 2013 06:15
Using Pi as a (very slow) random number generator.
/*
This program implements the BBP algorithm to generate a few hexadecimal
digits beginning immediately after a given position id, or in other words
beginning at position id + 1. On most systems using IEEE 64-bit floating-
point arithmetic, this code works correctly so long as d is less than
approximately 1.18 x 10^7. If 80-bit arithmetic can be employed, this limit
is significantly higher. Whatever arithmetic is used, results for a given
position id can be checked by repeating with id-1 or id+1, and verifying
that the hex digits perfectly overlap with an offset of one, except possibly
for a few trailing digits. The resulting fractions are typically accurate
@Sleepingwell
Sleepingwell / ca_rng.cpp
Last active December 19, 2015 23:49
A cellular automata random uniform number generator (with R interface).
/*
Copyright (C) 2006 Anthony M. Pasqualoni
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
@Sleepingwell
Sleepingwell / rng_tests.R
Last active December 19, 2015 23:39
Comparison of performance (speed) or R's Gausian random number generators.
test.func <- function(n.samples) {
nms <- c("Wichmann-Hill", "Marsaglia-Multicarry", "Super-Duper", "Mersenne-Twister", "Knuth-TAOCP-2002", "Knuth-TAOCP", "L'Ecuyer-CMRG")
tmp <- lapply(nms, function(unif.rng.name) {
RNGkind(unif.rng.name)
nms <- c("Kinderman-Ramage", "Ahrens-Dieter", "Box-Muller", "Inversion")
tmp <- sapply(nms, function(norm.rng.name) {
RNGkind(, norm.rng.name)
system.time(rnorm(n.samples))
})[1:3,]
# included here because I'm not sure if rziggurat uses the unif rng internally.
@Sleepingwell
Sleepingwell / gist:5843755
Last active December 18, 2015 20:59
A wrapper around Rcartogram::cartogram for shape files.
# A wrapper around Rcartogram::cartogram for shape files.
#
# @param polys: Either a SpatialPolygonsDataFrame or the path to a shape file.
# @param variable: Either a string giving the name of the variable to used in polys@data, or a vector
# of the same length as polys@polygons.
# @param nx: The number of columns in the raster used to represent the region.
# @param ny: The number of rows in the raster used to represent the region.
# @param buffer.by: The proportion width of the buffer to add to the region (see ?Rcartogram::addBoundary).
#
# @return The new boundaries as A SpatialPolygonsDataFrame with the attribute 'cartogram.scaling.var'.