Skip to content

Instantly share code, notes, and snippets.

View syrte's full-sized avatar

Zhaozhou Li syrte

View GitHub Profile
@syrte
syrte / hide_cell_number.yaml
Created August 2, 2016 05:35
hide_cell_number
Type: IPython Notebook Extension
Compatibility: 3.x 4.x
Main: main.js
Name: Hide cell number
Description: "toggle number of all cells"
__all__ = ['recordtype']
import sys
from textwrap import dedent
from keyword import iskeyword
def recordtype(typename, field_names, verbose=False, **default_kwds):
'''Returns a new class with named fields.
@syrte
syrte / rand_rotmat.py
Last active July 19, 2022 18:54
> To generate uniformly distributed random rotations of a unit sphere, first perform a random rotation about the vertical axis, then rotate the north pole to a random position. James Arvo, Fast Random Rotation Matrices
def rand_rotmat(n):
# http://www.realtimerendering.com/resources/GraphicsGems/gemsiii/rand_rotation.c
import numpy as np
theta, phi, z = np.random.rand(3, n) * 2
theta *= np.pi
phi *= np.pi
r = np.sqrt(z)
Vx = np.sin(phi) * r
@syrte
syrte / steps.py
Last active October 31, 2016 10:55
Make a histogram-like step plot.
from __future__ import division, print_function, absolute_import
import numpy as np
from matplotlib import pyplot as plt
def steps(x, y, *args, **kwargs):
'''steps(x, y, *args, style='line', bottom=0, **kwargs)
Make a step plot.
The interval from x[i] to x[i+1] has level y[i]
This function is useful for show the results of np.histogram.
@syrte
syrte / ks2d1s.py
Last active November 3, 2016 18:29
from scipy.stats.mvn import mvnun as mvn_cdf
from scipy.stats import kstwobign, pearsonr
from __future__ import division
def quadct(xx, yy):
n = len(xx)
res = np.empty((n, 4), 'float')
for i in range(n):
ix1, ix2 = xx <= xx[i], yy <= yy[i]
a = np.sum( ix1 & ix2)
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
def denscatter(x, y, xsacle=1, yscale=1, sort=False, **kwargs):
kwargs.setdefault('edgecolor', 'none')
if xscale != 1:
x = x / xscale
if yscale != 1:
y = y / yscale
@syrte
syrte / clean_bibtex.py
Last active December 31, 2016 14:46
Fix the improper year in bibtex exported from zotero which is reported [here]( https://github.com/retorquere/zotero-better-bibtex/issues/621).
#!/usr/bin/env python
import sys
import bibtexparser
if len(sys.argv) == 2:
fn = sys.argv[-1]
fout = None
elif len(sys.argv) == 3 and sys.argv[1] == "-i":
fn = sys.argv[-1]
fout = fn
def plot_binquantile(x, y, bins=10, weights=None, nmin=3, nanas=None,
kwargs_sig2=None, **kwargs):
from .stats import binquantile, mid
from .helper import errorbar2
nsig = [0, -1, 1, -2, 2]
quantile = binquantile(x, y, bins=bins, weights=weights,
nsig=nsig, nmin=nmin, nanas=nanas,
shape='stats')
stats, edges, count = quantile
import numpy as np
cimport numpy as np
np.import_array()
cimport cython
from libc.stdlib cimport malloc, free
cdef extern from "numpy/arrayobject.h":
void PyArray_ENABLEFLAGS(np.ndarray arr, int flags)
# cython: boundscheck=False, wraparound=False
# distutils: extra_compile_args = -fopenmp
# distutils: extra_link_args = -fopenmp
from cython.parallel import prange
from libc.math cimport exp, sqrt
import numpy as np
cdef double pi = np.pi
cdef double tau = np.pi * 2