Skip to content

Instantly share code, notes, and snippets.

@chychen
Last active August 27, 2020 15:16
Show Gist options
  • Select an option

  • Save chychen/ba7221dfe86e9b3c381e566abe936970 to your computer and use it in GitHub Desktop.

Select an option

Save chychen/ba7221dfe86e9b3c381e566abe936970 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import Numba CUDA"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from numba import cuda\n",
"import numpy as np\n",
"import math"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"data = np.load('example_data.npy')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Original (CPU-based)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def ridge_detection(f, thres):\n",
" count = np.zeros(f.shape)\n",
" for i in range(len(f)):\n",
" for j in range(len(f[i])):\n",
" if (\n",
" i > 0\n",
" and j > 0\n",
" and i < (len(f) - 1)\n",
" and j < (len(f[i]) - 1)\n",
" and f[i, j] > thres\n",
" and ~np.isnan(f[i, j])\n",
" ):\n",
" step_i = i\n",
" step_j = j\n",
" for k in range(1000):\n",
" if (\n",
" step_i == 0\n",
" or step_j == 0\n",
" or step_i == (len(f) - 1)\n",
" or step_j == (len(f[i]) - 1)\n",
" ):\n",
" break\n",
" index = np.nanargmax(\n",
" f[step_i - 1 : step_i + 2, step_j - 1 : step_j + 2].data\n",
" )\n",
" vmax = np.nanmax(\n",
" f[step_i - 1 : step_i + 2, step_j - 1 : step_j + 2].data\n",
" )\n",
" if index == 4 or vmax == f[step_i, step_j] or np.isnan(vmax):\n",
" break\n",
" row = int(index / 3)\n",
" col = index % 3\n",
" count[step_i - 1 + row, step_j - 1 + col] += 1\n",
" step_i = step_i - 1 + row\n",
" step_j = step_j - 1 + col\n",
" return count"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 6min 14s, sys: 15.9 s, total: 6min 30s\n",
"Wall time: 6min 6s\n"
]
}
],
"source": [
"%%time\n",
"results = ridge_detection(data, 0)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# %timeit -r 7 -n 1 ridge_detection(data, 0)"
]
}
],
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment