Created
July 13, 2017 18:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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