Skip to content

Instantly share code, notes, and snippets.

View jaimefrio's full-sized avatar

Jaime jaimefrio

  • Google
  • Zurich, Switzerland
View GitHub Profile
@jaimefrio
jaimefrio / spline_interpolation.py
Created March 8, 2018 12:35
Simple spline interpolation with
import numpy as np
from scipy import ndimage
SPLINE_ORDER = 3
def b_spline(x):
"""Computes 3rd order B-spline at x."""
x = np.fabs(x)
b_spline = np.zeros_like(x, dtype=float)
mask = x < 1.0
@jaimefrio
jaimefrio / dtype_compat.py
Created January 28, 2015 22:22
Prototype of dtype compatibility check
from __future__ import division, print_function
from numpy import object_
from fractions import gcd
def get_object_offsets(dtype, base_offset=0):
offsets = []
if dtype.fields is not None:
for field in dtype.fields.values():
@jaimefrio
jaimefrio / gist:8743836
Created January 31, 2014 21:44
`np.bincount` benchmarking
-------------+---------+-----------------+-----------------
bins len | old | new (naive) | new (opt.)
-------------+---------+-----------------+-----------------
3 3 | 8.2e-07 | 8.2e-07 (1.00x) | 8.2e-07 (1.00x)
3 10 | 8.2e-07 | 8.2e-07 (1.00x) | 8.2e-07 (1.00x)
3 30 | 8.2e-07 | 8.2e-07 (1.00x) | 8.2e-07 (1.00x)
3 100 | 9.8e-07 | 8.3e-07 (1.18x) | 8.2e-07 (1.20x)
3 300 | 1.6e-06 | 1.2e-06 (1.33x) | 1.2e-06 (1.33x)
3 1000 | 3.0e-06 | 2.5e-06 (1.20x) | 2.5e-06 (1.20x)
3 3000 | 7.3e-06 | 6.3e-06 (1.16x) | 7.9e-06 (0.92x)
@jaimefrio
jaimefrio / gist:8704101
Last active August 29, 2015 13:55
`np.searchsorted` benchmarks
-----------------+------------------------+------------------------
searchsorted | floats | ints
-----------------+------------------------+------------------------
haystack needle | old new gain | old new gain
-----------------+------------------------+------------------------
5 2 | 1.60e-06 1.64e-06 1.0x | 1.64e-06 1.64e-06 1.0x
5 10 | 1.65e-06 1.65e-06 1.0x | 1.64e-06 1.64e-06 1.0x
5 50 | 2.21e-06 2.03e-06 1.1x | 2.11e-06 1.69e-06 1.2x
5 200 | 4.98e-06 3.05e-06 1.6x | 4.53e-06 2.56e-06 1.8x
5 1000 | 2.54e-05 1.03e-05 2.5x | 2.12e-05 8.39e-06 2.5x
@jaimefrio
jaimefrio / gist:8424247
Created January 14, 2014 19:33
Timings for several variants of `np.digitize` and `np.searchsorted`
from __future__ import division
import numpy as np
import timeit
import matplotlib.pyplot as plt
haystack_sizes = np.linspace(2, 250, num=20, endpoint=True)
needle_sizes = np.array([10, 20, 50, 100, 200, 500 ,1000, 10000, 20000])
codes = ['np.searchsorted(haystack, needle)',
'np.fastsearchsorted(haystack, needle)',
{
"metadata": {
"name": "What's new in numpy 1.8"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{