Skip to content

Instantly share code, notes, and snippets.

@denis-bz
denis-bz / interp1d-splev.py
Created March 28, 2013 17:26
a little testcase for scipy splines, random-uniform X -> linspace Y: see http://imgur.com/Z9ikNCk
# interp1d != splev random-uniform -> 0 1 2 ...
# big swings, different end conditions
# http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html
# http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.splev.html
# http://stackoverflow.com/questions/6216881/python-interp1d-vs-univariatespline
# 28mar 2013
from __future__ import division
import sys
import numpy as np
@denis-bz
denis-bz / griddata-intergrid-div5.log
Created June 29, 2013 17:13
compare scipy.interpolate.griddata with Intergrid wrapper for scipy.ndimage.map_coordinates
# from: griddata-intergrid.py
# run: 29 Jun 2013 19:09 in ~bz/py/interpol/git/intergrid/test i486 mac 10.8.3
versions: numpy 1.7.1 scipy 0.12.0 python 2.7.4
--------------------------------------------------------------------------------
griddata vs. intergrid: Nx 36 Ny 36 Nz 84 subsample 2 order 1 prefilter 0
func min av max: -1 0.0025 1
griddata: 121.1 sec
Intergrid: 2.82 msec 13608 points in a (36, 36, 84) grid 3 maps order 1
av |f - griddata|: 0.44
@denis-bz
denis-bz / savgol-noise.py
Last active December 19, 2015 12:49
Compare Savitzky-Golay filter f( x + xnoise ) ~ Savgol( f( x )) + ynoise for f(x) = sin( 2 pi freq x ), to try to elucidate Press et al., Numerical Recipes p. 772.
""" Compare Savitzky-Golay filter f( x + xnoise ) ~ Savgol( f( x )) + ynoise
for f(x) = sin( 2 pi freq x )
to try to elucidate Press et al., Numerical Recipes p. 772:
"for irregularly sampled data ... one can pretend that the data points *are* equally spaced
... if change in f across the full width ..."
(Simpler: noise amplification of linear filters:
|convolve( filter, x )| <= |filter| * |x|, || = rms
|flat filter| = 1 / sqrt(N)
|Savgol| ~ 2 * that
@denis-bz
denis-bz / conda-requires
Created August 9, 2013 14:54
conda-requires: list "requires" of the 100-odd python packages in anaconda
#!/bin/sh
# conda-requires: list "requires" of the 100-odd packages in anaconda,
# http://docs.continuum.io/anaconda/pkgs.html
# gist.github.com/denis-bz
[[ $1 ]] && cd $1 && shift # .../anaconda/conda-meta
echo "# ${0##*/} $PWD `/bin/date '+%e %h %Y'` " # 9 Aug 2013
for file in *.json
@denis-bz
denis-bz / docs.md
Last active December 20, 2015 21:08
Links to html doc of some Python-NumPy programs in github.com/denis-bz

Links to html doc of a couple of Python-NumPy programs in github.com/denis-bz :

Intergrid: interpolate in an N-d box grid, uniform or non-uniform. This is just a wrapper for scipy.ndimage.map_coordinates and numpy.interp. It's fast: ~ 1M interpolations / sec in 4d.

Barypol: interpolate in an N-d box grid, using d + 1 corners of a simplex

@denis-bz
denis-bz / nearsame.py
Created January 21, 2014 17:21
nearsame.py: an easy and intuitive way to find outliers in data 2d to 20d, with kd-trees
#!/usr/bin/env python
""" nearsame.py: an easy and intuitive way to find outliers in data 2d to 20d.
-> e.g.
[50 24 15 10] % of the 38356 points have
[ 3 2 1 0] of the 3 nearest neighbors in the same class.
Here one could keep the 74 % for which the nearest 3 neighbors
are mostly in the same class, and call the rest outliers.
Intuitively, points in mixed neighborhoods, where classes overlap,
@denis-bz
denis-bz / 0-lpgen.md
Last active May 6, 2021 19:31
Test generator for sparse linear programming: m+n constraints, mn variables, 2mn non-zeros 26 Apr 2019

Test generator for sparse linear programming: m+n constraints, mn variables, 2mn non-zeros

Keywords: linear-programming, sparse, scipy, test-case-generator

Purpose

lpgen_2d generates test cases for linear programming, with parameters m and n: m+n constraints, mn variables, 2mn non-zeros. An example:

from scipy.optimize import linprog

One-line arg parse for flexible testing in Python

Many Python scripts with parameters say M and N look like this:

# parameters --
M = 10
N = 10
...

myfunc( M, N ... )

@denis-bz
denis-bz / heapq_del.py
Created April 22, 2014 16:52
heapq_del.py: heapq.py + heapchange heapdel
""" heapq_del.py: heapq.py + heapchange heapdel
see http://docs.python.org/2/library/heapq.html
c heap* but python _siftup _siftdown, slow
"""
# cf. class Aheapq: heap --> A[ Arecs with .val .heappos ... ]
from heapq import heappush, heappop, heapify, heapreplace, _siftup, _siftdown
# http://docs.python.org/2/library/heapq.html
# https://github.com/python/cpython/blob/master/Modules/_heapqmodule.c
# (note _siftup --> leaves, _siftdown --> root
@denis-bz
denis-bz / Winsorize.py
Created May 12, 2014 16:44
Trim outliers in Numpy arrays: smallest n values = the next smallest, biggest n = the next biggest
""" trim outliers: smallest n values = next smallest, biggest n = next biggest
X, smallest, biggest = winsorize( X, n )
-> X trimmed *inplace*
smallest: n+1 smallest
biggest: n+1 biggest
To winsorize the top 5 % and bottom 5 %,
winsorize( X, X.size * .05 )
X may be 2d, 3d ... (but winsorizing each dimension separately would make more sense)
See also http://en.wikipedia.org/wiki/Winsorising