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 / 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 / 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)])+_
@stucchio
stucchio / beta_bandit.py
Created April 14, 2013 15:46
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
@jakevdp
jakevdp / convolution_matrix.py
Last active March 12, 2019 09:49
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
@mdboom
mdboom / datashader-matplotlib-mashup.py
Last active April 23, 2020 10:59
The absolute bare minimum to get datashader working in matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import datashader as ds
import datashader.transfer_functions as tf
from datashader.colors import Hot
time_period = 60
@rougier
rougier / glut-cube.py
Created January 20, 2016 07:06
Rotating color cube using modern GL, glut and python
# -----------------------------------------------------------------------------
# Copyright (c) 2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
import sys
import math
import ctypes
import numpy as np
import OpenGL.GL as gl
import OpenGL.GLUT as glut
@rougier
rougier / enriched.py
Created June 20, 2020 09:21
Emacs enriched test minor mode
import json
palettes = json.load(open("material-colors.json"))
levels = [ "L50", "L100", "L200", "L300", "L400",
"L500", "L600", "L700", "L800", "L900",
"A100", "A200", "A400", "A700"]
def enriched_text(t, fg_color, bg_color, space, bold=False):
if bold: u0, u1 = "<bold>", "</bold>"
else: u0, u1 = "", ""
print(f"<x-bg-color><param>{bg_color}</param>"
@rougier
rougier / numpy-logo.svg
Created September 8, 2019 13:15
Numpy logo redesign
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@rougier
rougier / rounded-box.el
Last active October 3, 2020 13:24
SVG Rounded box for Emacs
(require 'svg)
;; Rounded boxes using SVG:
;; This could be made into a function but size of text needs to be computed
(defun tag (text &optional foreground background font-size)
(let* ((font-size (or font-size 12))
;; The char-width ratio depends on the font family
(char-width (* font-size 0.58))
(char-height (+ font-size 1))