Skip to content

Instantly share code, notes, and snippets.


Nicolas P. Rougier rougier

View GitHub Profile
rougier /
Created Jan 10, 2016
SI and IEC prefix for printing human readable numbers
# Copyright (c) 2016 Nicolas P. Rougier - BSD License
# SI prefixes as name:value
SI_prefix_name_value = {
"yocto": 10e-24, "zepto": 10e-21, "atto": 10e-18, "femto": 10e-15,
"pico": 10e-12, "nano": 10e-9, "micro": 10e-6, "milli": 10e-3,
"centi": 10e-2, "deci": 10e-1, "deca": 10e1, "hecto": 10e2,
"kilo": 10e3, "mega": 10e6, "giga": 10e9, "tera": 10e12,
"peta": 10e15, "exa": 10e18, "zetta": 10e21, "yotta": 10e24 }
rougier / admonition.el
Created Feb 28, 2016
Some visible admonitions for emacs
View admonition.el
(make-face 'face-admonition)
(set-face-attribute 'face-admonition nil
:height 120
:weight 'regular
:foreground "white"
:background "light gray"
:box '(:line-width 1 :color "light gray"))
(defvar face-admonition 'face-admonition)
(make-face 'face-admonition-note)
rougier /
Created Apr 20, 2016
Generate a maze with customizable complexity and density
import numpy as np
import matplotlib.pyplot as plt
def maze(shape=(64,64), complexity=.95, density = 1):
# Only odd shapes
shape = ((shape[0]//2)*2+1, (shape[1]//2)*2+1)
# Adjust complexity and density relative to maze size
complexity = int(complexity*(5*(shape[0]+shape[1])))
density = int(density*(shape[0]//2*shape[1]//2))
def find_view(base, view):
Given an array that is a `view` of a `base`, find an index such that
`base[index] is view`
if not isinstance(view, np.ndarray):
return "..."
itemsize = view.itemsize
rougier /
Created Nov 13, 2016 — forked from pv/
Colorized Voronoi diagram with Scipy, in 2D, including infinite regions
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi
def voronoi_finite_polygons_2d(vor, radius=None):
Reconstruct infinite voronoi regions in a 2D diagram to finite
rougier /
Created Nov 27, 2016
Matplotlib / individual rotation markers using a single path
# Matplotlib / individual rotation markers using a single path
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.animation import FuncAnimation
from matplotlib.collections import PathCollection
triangle = [[(-0.25, -0.5), (+0.0, +0.5), (+0.25, -0.5), (+0.0, +0.0)],
# Author: Jake VanderPlas
from __future__ import division
import numpy as np
def convolution_matrix(x, N=None, mode='full'):
"""Compute the Convolution Matrix
rougier /
Last active Feb 24, 2017
Cellular automata 1D
# Copyright (2017) Nicolas P. Rougier - BSD license
# Twitter version (140 characters)
# --------------------------------
for k in range(9):
C = _+''.join([R[7-eval('0b'+C[i:i+3])] for i in range(len(C)-2)])+_
rougier /
Created Sep 16, 2017 — forked from MorganBorman/
A short example of how to use vertex array objects in PyOpenGL
import OpenGL.GL as GL
import OpenGL.GL.shaders
import ctypes
import pygame
import numpy
vertex_shader = """
#version 330
in vec4 position;
rougier /
Created Jul 7, 2018 — forked from stucchio/
The beta-distribution based bayesian bandit algorith,.
from numpy import *
from scipy.stats import beta
class BetaBandit(object):
def __init__(self, num_options=2, prior=(1.0,1.0)):
self.trials = zeros(shape=(num_options,), dtype=int)
self.successes = zeros(shape=(num_options,), dtype=int)
self.num_options = num_options
self.prior = prior