Skip to content

Instantly share code, notes, and snippets.

@caffeine-potent
Created July 13, 2017 18:54
Show Gist options
  • Save caffeine-potent/f283df71ade6d317ba8f0a6a17215070 to your computer and use it in GitHub Desktop.
Save caffeine-potent/f283df71ade6d317ba8f0a6a17215070 to your computer and use it in GitHub Desktop.
Scraping for performance improvements without the use of C or CYTHON. Stretching an array for an image resolution matching issue.
def discrete_stretch(array, size):
""" Expands 2d array by some factor.
For example the following array is expanded by a factor of 3
[ 1 2 ]
[ 3 4 ]
[ 1 1 1 2 2 2]
[ 1 1 1 2 2 2]
[ 1 1 1 2 2 2]
[ 3 3 3 4 4 4]
[ 3 3 3 4 4 4]
[ 3 3 3 4 4 4]
The code is not elegant or readable but timeit shows significant performance over np.kron
for kroenecker products and marginal improvements over mixing np.tile with np.repeat
Input:
np.array - 2d array of values you want stretched
Output:
np.array - 2d np array of stretched values.
"""
return np.repeat(np.repeat(a, size, axis = 1), size, axis = 0).reshape( a.shape[0] * size, a.shape[1] * size )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment