Last active
December 20, 2016 16:37
-
-
Save ColinTalbert/f71a2f158ab8fb6bfde87b399e1616ba to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import matplotlib.pyplot as plt\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[5, 3, 4, ..., 5, 5, 5],\n", | |
" [4, 4, 2, ..., 2, 5, 1],\n", | |
" [1, 5, 2, ..., 5, 5, 4],\n", | |
" ..., \n", | |
" [2, 3, 2, ..., 1, 2, 2],\n", | |
" [5, 2, 3, ..., 3, 5, 2],\n", | |
" [4, 4, 2, ..., 2, 2, 2]])" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data = np.random.randint(1,6, size=(1000,1000))\n", | |
"data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"xstep=1;ystep=1; xsize=3; ysize=3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1000, 1000)" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Wall time: 49 ms\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"C:\\Anaconda3\\lib\\site-packages\\numpy\\core\\numeric.py:482: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future\n", | |
" return array(a, dtype, copy=False, order=order)\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"window_view = np.lib.stride_tricks.as_strided(data, ((data.shape[0] - xsize + 1) / xstep, (data.shape[1] - ysize + 1) / ystep, xsize, ysize), (data.strides[0] * xstep, data.strides[1] * ystep, data.strides[0], data.strides[1]))\n", | |
"window_view_flat = window_view.reshape(data.shape[0]-2, data.shape[1]-2, 9)\n", | |
"window_view_flat" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([5, 3, 4, 4, 4, 2, 1, 5, 2])" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"single_pix = window_view_flat[0][0]\n", | |
"single_pix" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([ True, True, False, False, False, True, True, True, True], dtype=bool)" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"single_pix!=single_pix[4]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([5, 3, 2, 1, 5, 2])" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"single_pix[single_pix!=single_pix[4]]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"np.unique(single_pix[single_pix!=single_pix[4]]).size" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def unique_adjacency(arr):\n", | |
" return np.unique(arr[arr!=arr[4]]).size" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Wall time: 12.8 s\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[4, 3, 3, ..., 3, 2, 3],\n", | |
" [3, 3, 2, ..., 4, 3, 4],\n", | |
" [3, 2, 3, ..., 3, 2, 3],\n", | |
" ..., \n", | |
" [4, 4, 3, ..., 3, 3, 2],\n", | |
" [4, 4, 4, ..., 3, 3, 3],\n", | |
" [3, 4, 4, ..., 3, 3, 3]])" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"np.apply_along_axis(unique_adjacency, 2, window_view_flat)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [default]", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment