Skip to content

Instantly share code, notes, and snippets.

View OlivierBinette's full-sized avatar
🧑‍💻

Olivier Binette OlivierBinette

🧑‍💻
View GitHub Profile
@OlivierBinette
OlivierBinette / makefile
Created August 8, 2022 14:43
Makefile template
## Development utilities for XX
##
## Usage:
## make <target> [<arg>=<value> ...]
##
## Targets:
## help: Show this help message.
## env: Create or update conda environment "XX"
ENV?=XX
@OlivierBinette
OlivierBinette / read_auto.py
Last active August 7, 2022 17:41
Read pandas dataframe according to file extension
import os
import pandas as pd
def read_auto(datapath):
_, ext = os.path.splitext(datapath)
if ext == ".csv":
return pd.read_csv(datapath)
elif ext == ".tsv":
return pd.read_csv(datapath, sep="\t")
#! /usr/bin/bash
repos=$(gh repo list -L=9999 | awk '{print $1}')
for r in $repos; do gh repo clone $r; done
@OlivierBinette
OlivierBinette / templates.Rmd
Last active September 29, 2021 00:33
[Stan templates] #templates #rstan
---
title: "Stan templates"
output: html_notebook
---
**Things to keep in mind:**
- Stan uses the *standard deviation* to parameterize the normal distribution. Use `normal(mu, sigma)` instead of `normal(mu, sigma^2)`.
- Leave a blank line at the end of the chunk defining Stan code.
@OlivierBinette
OlivierBinette / normalgamma.R
Last active September 29, 2021 00:33
[Normal-Gamma sampling]
library(assert)
source("multinorm.R")
rnormalgamma <- (function(k, b, Sigma, nu, kappa) {
# Sample from a Normal-Gamma distribution Attaches both `rnormalgamma` and
# `rnormalgamma.fast` to the current environment.
#
# Usage:
# rnormalgamma(b, Sigma, nu, kappa)
# rnormalgamma.fast(b, Sigma, nu, kappa)
@OlivierBinette
OlivierBinette / prettyplot.R
Last active January 15, 2020 19:47
[PrettyPlot] Base graphics wrapper #R #Plotting
#
# Work in progress.
#
cmap.seaborn <- function(n) {
colors.seaborn <-
c(rgb(0.12156862745098039, 0.4666666666666667, 0.7058823529411765),
rgb(1.0, 0.4980392156862745, 0.054901960784313725),
rgb(0.17254901960784313, 0.6274509803921569, 0.17254901960784313),
rgb(0.8392156862745098, 0.15294117647058825, 0.1568627450980392),
@OlivierBinette
OlivierBinette / rmultinorm.R
Last active September 29, 2021 00:33
[Multivariate Normal Sampling]
library(assert)
rmultinorm.fast <- function(k, mu, sigma) {
p = length(mu)
t(chol(sigma)) %*% matrix(rnorm(p*k, 0, 1), nrow=p) + mu
}
#' Sample from a multivariate normal distribution. Attaches both `rmultinorm`
#' and `rmultinorm.fast` to the current environment.
#'
@OlivierBinette
OlivierBinette / assert.R
Last active September 29, 2021 00:34
[Assert] Validate function arguments #R
assert <- function(..., msg=NULL) {
# Assert that each of the provided expression is true. Otherwise returns a
# description of each failed assertion.
#
# Args:
# ...: List of logical assertions
# msg: Optional parameter printed when one of the assertions fails.
#
# Returns:
# Nothing if all assertions pass. Otherwise throws an error describing which