{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Histogram APs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "from pathlib import Path\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "triskele_path = Path('../triskele/python/')\n", "sys.path.append(str(triskele_path.resolve()))\n", "import triskele" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Specific Utils\n", "\n", "def DFC_filter(raster):\n", " raster[raster > 1e4] = raster[raster < 1e4].max()\n", "\n", "def show(im, im_size=1, save=None):\n", " plt.figure(figsize=(16*im_size,3*im_size))\n", " plt.imshow(im)\n", " plt.colorbar()\n", " \n", " if save is not None:\n", " plt.savefig(save, bbox_inches='tight', pad_inches=1)\n", " \n", " plt.show()\n", "\n", "def mshow(Xs, titles=None, im_size=1, save=None):\n", " s = len(Xs)\n", "\n", " plt.figure(figsize=(16*im_size,3*im_size*s))\n", "\n", " for i in range(s):\n", " plt.subplot(s,1,i+1)\n", " plt.imshow(Xs[i])\n", " \n", " if titles is not None:\n", " plt.title(titles[i])\n", " \n", " plt.colorbar()\n", " \n", " if save is not None:\n", " plt.savefig(save, bbox_inches='tight', pad_inches=1)\n", " \n", " plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load a single raster" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "raster_p = Path('../Data/phase1_rasters/DEM+B_C123/UH17_GEM051_TR.tif')\n", "raster = triskele.read(raster_p)\n", "DFC_filter(raster)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show(raster)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute attributes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#area = np.array([25, 100, 500, 1e3, 5e3, 10e3, 50e3, 100e3, 150e3])\n", "area = [1e3, 1e6]\n", "moi = [.5, .7, .9]\n", "sd = [.1, .5, .9]\n", "moi = None\n", "sd = None\n", "\n", "t = triskele.Triskele(raster, verbose=False)\n", "attributes = t.filter(tree='tos-tree',\n", " area=area,\n", " moment_of_inertia=moi,\n", " standard_deviation=sd\n", " )\n", "attributes.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show(attributes[:,:,-1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "figs = list()\n", "for i in range(attributes.shape[2]):\n", " figs.append(attributes[:,:,i])\n", " \n", "mshow(figs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = attributes\n", "i = 1\n", "show(a[:,:,i].astype(np.float) - a[:,:,i+1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute patches" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "patch_size = 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time\n", "\n", "offset_stack = None\n", "def create_patches(array, patch_size=3):\n", " amp = int((patch_size - 1 ) / 2)\n", "\n", " stack = list()\n", " for i in range(-amp, amp+1):\n", " ai = i if i > 0 else None\n", " bi = i if i < 0 else None\n", " ci = -bi if bi is not None else None\n", " di = -ai if ai is not None else None\n", "\n", " for j in range(-amp, amp+1):\n", " offset = np.zeros_like(array)\n", " #offset = np.empty(array.shape)\n", " aj = j if j > 0 else None\n", " bj = j if j < 0 else None\n", " cj = -bj if bj is not None else None\n", " dj = -aj if aj is not None else None\n", " print('{}:{} {}:{} - {}:{} {}:{}'.format(ai, bi, ci, di, aj, bj, cj, dj))\n", " offset[ai:bi, aj:bj] = array[ci:di, cj:dj]\n", " stack.append(offset)\n", " return np.stack(stack, axis=-1)\n", "\n", "offset_stack = create_patches(attributes, patch_size)\n", "offset_stack.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " init | user | sys | total\n", " --- | ---: | ---: | ---:\n", " empty | 27.9 s | 15.2 s | 43.1 s\n", " zeros | 27.2 s | 14.1 s | 41.3 s\n", " zeros_like | 6.47 s | 4.01 s | 10.5 s" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%whos" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "offset_stack[-2,-3,:].reshape(-1,1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stack_std = np.std(offset_stack, axis=-1)\n", "stack_std.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "figs = list()\n", "ttls = list()\n", "for d in range(stack_std.shape[-1]):\n", " if d == 0:\n", " ttls.append('Origin')\n", " else:\n", " ttls.append('STD 5x5 from area {}'.format(area[d-1]))\n", " figs.append(stack_std[:,:,d])\n", " \n", "mshow(figs, ttls, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stack_mean = np.mean(offset_stack, axis=-1)\n", "stack_avr = np.average(offset_stack, axis=-1)\n", "stack_var = np.var(offset_stack, axis=-1)\n", "stack_min = np.min(offset_stack, axis=-1)\n", "stack_max = np.max(offset_stack, axis=-1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.std.__name__" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "create_patches.__name__" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for func in (np.mean, np.var):\n", " tmp = func(offset_stack, axis=-1)\n", " show(tmp[:,:,0])\n", " print(tmp.shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "size = '{}x{}'.format(patch_size, patch_size)\n", "figs = [raster,\n", " stack_avr,\n", " stack_mean,\n", " stack_min,\n", " stack_max,\n", " stack_var,\n", " stack_std\n", " ]\n", "ttls = ['Origin',\n", " 'Average ' + size,\n", " 'Mean ' + size,\n", " 'Minimum ' + size,\n", " 'Maximum ' + size,\n", " 'Variance ' + size,\n", " 'STD ' + size\n", " ]\n", "mshow(figs, ttls)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "size = '{}x{}'.format(patch_size, patch_size)\n", "figs = [raster,\n", " stack_avr[:,:,-1],\n", " stack_mean[:,:,-1],\n", " stack_min[:,:,-1],\n", " stack_max[:,:,-1],\n", " stack_var[:,:,-1],\n", " stack_std[:,:,-1]\n", " ]\n", "ttls = ['Origin',\n", " 'Average ' + size,\n", " 'Mean ' + size,\n", " 'Minimum ' + size,\n", " 'Maximum ' + size,\n", " 'Variance ' + size,\n", " 'STD ' + size\n", " ]\n", "mshow(figs, ttls, 2, save='mstack2.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### TMP TRISLEK DBG" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "normal = triskele.read('../triskele/build/out/test-default.tif')\n", "sd = triskele.read('../triskele/build/out/test-sd.tif')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "normal.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "areas = np.loadtxt('../triskele/data/areaThresholds.txt')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "figs = [normal[:,:,0]]\n", "ttls = ['Origin']\n", "\n", "for area, i in zip(areas, range(1,normal.shape[2])):\n", " figs.append(normal[:,:,i])\n", " ttls.append('SDAP area {}'.format(area))\n", "\n", "mshow(figs, ttls, 2, '../Res/trkldbg_triskele_ap.png')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "figs = [sd[:,:,0]]\n", "ttls = ['Origin']\n", "\n", "for area, i in zip(areas, range(1,sd.shape[2])):\n", " figs.append(sd[:,:,i])\n", " ttls.append('Triskele LFSDAP (SD) area {}'.format(area))\n", "\n", "mshow(figs, ttls, 2, '../Res/trkldbg_triskele_sd.png')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(normal == sd).all()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "offset_stack = create_patches(normal, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stack_std = np.std(offset_stack, axis=-1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "figs = [stack_std[:,:,0]]\n", "ttls = ['Origin std 5x5']\n", "\n", "for area, i in zip(areas, range(1,sd.shape[2])):\n", " figs.append(stack_std[:,:,i])\n", " ttls.append('LD2DAPs LFSDAP std 5x5 area {}'.format(area))\n", "\n", "mshow(figs, ttls, 2, '../Res/trkldbg_ld2dap_sd.png')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "figs = list()\n", "ttls = list()\n", "for d in range(stack_std.shape[-1]):\n", " if d == 0:\n", " ttls.append('Origin')\n", " else:\n", " ttls.append('STD 5x5 from area {}'.format(area[d-1]))\n", " figs.append(stack_std[:,:,d])\n", " \n", "mshow(figs, ttls, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "assert True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "assert False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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" } }, "nbformat": 4, "nbformat_minor": 2 }