{ "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/Intensity_C3/UH17_GI3F051_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", "\n", "t = triskele.Triskele(raster, verbose=False)\n", "attributes = t.filter(tree='tos-tree',\n", " area=area\n", " )\n", "attributes.shape" ] }, { "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 = 3\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": [ "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(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, 5)\n", "offset_stack.shape" ] }, { "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": [ "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')" ] } ], "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 }