Skip to content

Instantly share code, notes, and snippets.

View rougier's full-sized avatar
:octocat:

Nicolas P. Rougier rougier

:octocat:
View GitHub Profile
@rougier
rougier / prefix.py
Created January 10, 2016 20:47
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
rougier / admonition.el
Created February 28, 2016 18:59
Some visible admonitions for emacs
(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
rougier / maze.py
Created April 20, 2016 20:33
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))
@rougier
rougier / numpy_find_view.py
Created November 3, 2016 18:14
Numpy find view
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
rougier / colorized_voronoi.py
Created November 13, 2016 17:38 — forked from pv/colorized_voronoi.py
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
regions.
Parameters
@rougier
rougier / convolution_matrix.py
Created February 17, 2017 14:55 — forked from jakevdp/convolution_matrix.py
Convolution Matrix
# Author: Jake VanderPlas
# LICENSE: MIT
from __future__ import division
import numpy as np
def convolution_matrix(x, N=None, mode='full'):
"""Compute the Convolution Matrix
@rougier
rougier / cellular-automata-1d.py
Last active February 24, 2017 21:14
Cellular automata 1D
# Copyright (2017) Nicolas P. Rougier - BSD license
# Twitter version (140 characters)
# --------------------------------
_='0'
R,C="{:08b}".format(30),_*9+'1'+_*9
for k in range(9):
print(C)
C = _+''.join([R[7-eval('0b'+C[i:i+3])] for i in range(len(C)-2)])+_
@rougier
rougier / main.py
Created September 16, 2017 17:38 — forked from MorganBorman/main.py
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
rougier / beta_bandit.py
Created July 7, 2018 05:44 — forked from stucchio/beta_bandit.py
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
@rougier
rougier / turbo_colormap.py
Created August 20, 2019 18:09 — forked from mikhailov-work/turbo_colormap.py
Turbo Colormap Look-up Table
# Copyright 2019 Google LLC.
# SPDX-License-Identifier: Apache-2.0
# Author: Anton Mikhailov
turbo_colormap_data = [[0.18995,0.07176,0.23217],[0.19483,0.08339,0.26149],[0.19956,0.09498,0.29024],[0.20415,0.10652,0.31844],[0.20860,0.11802,0.34607],[0.21291,0.12947,0.37314],[0.21708,0.14087,0.39964],[0.22111,0.15223,0.42558],[0.22500,0.16354,0.45096],[0.22875,0.17481,0.47578],[0.23236,0.18603,0.50004],[0.23582,0.19720,0.52373],[0.23915,0.20833,0.54686],[0.24234,0.21941,0.56942],[0.24539,0.23044,0.59142],[0.24830,0.24143,0.61286],[0.25107,0.25237,0.63374],[0.25369,0.26327,0.65406],[0.25618,0.27412,0.67381],[0.25853,0.28492,0.69300],[0.26074,0.29568,0.71162],[0.26280,0.30639,0.72968],[0.26473,0.31706,0.74718],[0.26652,0.32768,0.76412],[0.26816,0.33825,0.78050],[0.26967,0.34878,0.79631],[0.27103,0.35926,0.81156],[0.27226,0.36970,0.82624],[0.27334,0.38008,0.84037],[0.27429,0.39043,0.85393],[0.27509,0.40072,0.86692],[0.27576,0.41097,0.87936],[0.27628,0.42118,0.89123],[0.27667,0.43134,0.90254],[0.27691,0.44145,0.913