Skip to content

Instantly share code, notes, and snippets.

@julesghub
Created July 1, 2019 01:24
Show Gist options
  • Save julesghub/3061a941de86103a647faa4251b7c7e8 to your computer and use it in GitHub Desktop.
Save julesghub/3061a941de86103a647faa4251b7c7e8 to your computer and use it in GitHub Desktop.
2D k-d tree example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import scipy\n",
"from scipy import spatial\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.1.0'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scipy.__version__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"'''\n",
"kdtree 101:\n",
"'data' - the INPUT - the original values that are used to construct the kd tree\n",
"'grid' - the OUTPUT - the location where the oringinal values are projected\n",
"\n",
"'''\n",
"\n",
"n_data = 8\n",
"range_data = 16\n",
"\n",
"n_grid = [20,20]\n",
"range_grid = [range_data, range_data]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# create 2D set of points\n",
"points = np.random.rand(n_data,2) * range_data\n",
"\n",
"## for a function\n",
"# x = np.linspace(0,data_range, num=n_data)\n",
"# y = x + np.sin(2*np.pi*x/5)\n",
"# points = np.c_[(x,y)]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# build a KDTree\n",
"tree = spatial.cKDTree(points)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# build a query grid\n",
"tmp = np.array(n_grid) * 1j # convert to complex for mgrid[]\n",
"grid = np.mgrid[0:range_grid[0]:tmp[0], 0:range_grid[1]:tmp[1]]\n",
"test = grid.reshape(len(grid), -1).T\n",
"\n",
"# query the tree\n",
"dd, ii = tree.query(test, k=1)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAATIAAAEICAYAAADcJ3gOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4xLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvDW2N/gAAIABJREFUeJzt3Xm4HHWd7/H3x+SEkACBEIwh7C44CsqSQRRlWAQBEeY6DILLsGnGcVwY5HJBXBhG7+DGlXEZnygEUIaRQVBElkRDZBggQiCQsCgISDayELJACEnO+d4/qk7o06f7nDrd1Uv1+byep57TXVX9q19X9/n2b61SRGBmVmSvaXUGzMzq5UBmZoXnQGZmhedAZmaF50BmZoXnQGZmhedA1iSSbpV0WsZ9Z0v6eJVte0gKSSPzzWFnkLSbpBcljRhgn5D0hmbmK4s033u1Oh9F1PBAJukZScsljS1Z93FJsxt97KGQdLqkuxqVfkQcGxFXNSp9S0TEsxGxTUR0w8A/Clko8VVJiyWtSdN7a345flWa76cy5qstg3GrNKtENgL4XKMP4lJKvgYq1QwjfwucCbwHGA/cA/ykpTmyfpoVyL4JnCtp+0obJb1Z0kxJqyT9QdLJJdveL+lBSWslLZR0Ucm23mrWWZKeBWal6w+WdLek1ZIeknRYyWtOl/SUpHWSnpb0EUl/AfwQeGdavF9dJZ+zJf2LpP9JXz9D0oSS7QMdd0vJQNIISd+WtDLNw6crVBd3r3ac1JmSlkhaKunckuNsJek76bYl6eOtSt57n1Jn6S+7pCsl/bukWyS9BBxe5Rx8NX2fL0r6laQdJV2Tfkb3SdqjZP/L0s9traS5kt5Tsu0iSddL+ln6Ph+Q9PYq5/6fJX03fdwl6SVJ30yfby1pg6TxJd+JkZK+RhKAvpfm9XslSb5X0hPpZ/V9Sap0XGBP4K6IeCot5f0UeEuVfXtrIBdIelTSC5KmSxpdsv0Tkp5Mv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r7NOW8SDodOB74SBpU+8nwGdYlIpZFRHckN5T8UZX0PS2wikYEsixTEm4CenugTgJmVfsC1Sptc7sceCwiLq2yz+t62+YkHURyPnINqJLGStq29zFJ43L5FUBuAv4u7b08GFhTUu3K26lUqVY243yUKP0OnAZUulvt7cDRknZIq1pHp+tyI+kY4DzghIhYX2WfLJ9hvfkobRP9X1XS97TAahrRg0DSC/dHkh6WC9N1F5N8WSC59Md/AU8Cvwf2akAe3k1SXXkYmJcuxwGfBD6Z7vNp4BGS3p97gXc1IB97pek/lB6r93yU5kMkF5/7EzAfmNKgz2UsSWAaV7Ku4eeDJHAuBTaRtOucRdIm+lvgCeA3wPh03ynAj0tee2b6PXkSOKMB+XiSpN2p9zvS25u+M3DLQJ9hzvn4SfrZP0wSnCaV56Pa/5aX8BQlMyu+jm/sN7PO50BmZoXnQGZmhedAZmaF50BmZoXnQGZmhedAZmaF9/8Buo1rUvyr3qUAAAAASUVORK5CYII=\n",
"text/plain": [
"<Figure size 432x288 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# show the nn mapping\n",
"plt.title(\"Nearest neighbour map with {} points\".format(n_data))\n",
"plt.imshow( ii.reshape(grid.shape[1],grid.shape[2]).T, \n",
" extent=(0,range_grid[0],0,range_grid[1]), \n",
" origin='lower', interpolation='none' )\n",
"cbar = plt.colorbar()\n",
"cbar.set_label('index', rotation=90)\n",
"# show the original data\n",
"plt.scatter(points[:,0], points[:,1], c='red')\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment