Skip to content

Instantly share code, notes, and snippets.

View JDWarner's full-sized avatar

Josh Warner JDWarner

View GitHub Profile
@JDWarner
JDWarner / dask_benchmarks.py
Created July 12, 2015 14:47
dask.array performance compared to NumPy arrays in color conversion
import numpy as np
import dask.array as da
# Benchmark: NumPy with array construction included
%timeit nparr0 = np.random.random(size=(10000, 1000)); \
nparr1 = np.random.random(size=(10000, 1000)); \
np.sqrt((nparr0 - nparr1) ** 2 + (nparr0 - nparr1) ** 2 + (nparr0 - nparr1) ** 2)
# Benchmark: Dask with dask.array construction included
%timeit x = da.random.random(size=(10000, 1000), chunks=(10000, 1000)); \
@JDWarner
JDWarner / flood_fill.pyx
Last active October 21, 2016 22:18
Flood fill - Cython
import numpy as np
cimport numpy as cnp
# Compiler directives
@cython.cdivision(True)
@cython.boundscheck(False)
@cython.nonecheck(False)
@cython.wraparound(False)
def flood_fill(unsigned char[:, ::1] data, tuple start_coords,
@JDWarner
JDWarner / floodfill.py
Created May 19, 2015 06:29
Flood fill - pure Python
# Pure Python, usable speed but over 10x greater runtime than Cython version
def fill(data, start_coords, fill_value):
"""
Flood fill algorithm
Parameters
----------
data : (M, N) ndarray of uint8 type
Image with flood to be filled. Modified inplace.
start_coords : tuple
@JDWarner
JDWarner / _jaccard.py
Last active April 20, 2024 01:38
Jaccard coefficient between two boolean NumPy arrays or array-like data. This is commonly used as a set similarity metric, and it is a true metric. The dimensionality of the input is completely arbitrary, but `im1.shape` and `im2.shape` much be equal. This Gist is licensed under the modified BSD license, otherwise known as the 3-clause BSD.
"""
_jaccard.py : Jaccard metric for comparing set similarity.
"""
import numpy as np
def jaccard(im1, im2):
"""
@JDWarner
JDWarner / _dice.py
Last active January 25, 2023 22:28
Dice coefficient between two boolean NumPy arrays or array-like data. This is commonly used as a set similarity measurement (though note it is not a true metric; it does not satisfy the triangle inequality). The dimensionality of the input is completely arbitrary, but `im1.shape` and `im2.shape` much be equal. This Gist is licensed under the mod…
"""
_dice.py : Dice coefficient for comparing set similarity.
"""
import numpy as np
def dice(im1, im2):
"""
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import math
from PyQt4 import QtGui, QtCore
def binomial(i, n):
#!/usr/bin/env python
"""In order to use the script you need to copy your SSH key to the target server
and also copy the server SSH public key (usually .ssh/id_rsa.pub) to .ssh/authorized_keys,
so that the computing node can ssh passwordless to the login node"""
from subprocess import Popen, PIPE, call
import sys
import webbrowser
from getopt import getopt
import time
@JDWarner
JDWarner / _pad_asymmetric.py
Created May 1, 2013 05:37
Utility function for padding an array by appending `pad_amt` zeros along any `axis`. Non-symmetric. The ideas behind this snippet led to the `np.pad` improvements introduced in NumPy 1.8. This is licensed under the modified BSD.
import numpy as np
def _pad_asymmetric_zeros(arr, pad_amt, axis=-1):
"""Pads `arr` by `pad_amt` along specified `axis`"""
if axis == -1:
axis = arr.ndim - 1
zeroshape = tuple([x if i != axis else pad_amt
for (i, x) in enumerate(arr.shape)])