From 43d36230a137f018dbfce58e2ada5400502f45d1 Mon Sep 17 00:00:00 2001 From: Karamaz0V1 Date: Fri, 30 Mar 2018 12:10:46 +0200 Subject: [PATCH] HAPs Notebook --- Notebooks/HAPs.ipynb | 233 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100644 Notebooks/HAPs.ipynb diff --git a/Notebooks/HAPs.ipynb b/Notebooks/HAPs.ipynb new file mode 100644 index 0000000..83cce89 --- /dev/null +++ b/Notebooks/HAPs.ipynb @@ -0,0 +1,233 @@ +{ + "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):\n", + " plt.figure(figsize=(16*2,3*2))\n", + " plt.imshow(im)\n", + " plt.colorbar()\n", + " plt.show()" + ] + }, + { + "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": "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": [ + "a = attributes\n", + "i = 3\n", + "show(a[:,:,i].astype(np.float) - a[:,:,i+1])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "patch_size = 5\n", + "\n", + "### \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(raster.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] = raster[ci:di, cj:dj]\n", + " stack.append(offset)\n", + "\n", + "offset_stack = np.stack(stack, axis=2)\n", + "offset_stack.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "offset_stack[-3,-3,:].reshape(-1,1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "show(offset_stack[:,:,0])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "del offset_stack" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "hist = np.stack(raster, raster[:]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "np.repeat(100,10)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pstl = list()\n", + "\n", + "for c, p in [(100, .06), (60, .1), (12, .5), (20, .3)]:\n", + " st = np.random.binomial(np.repeat(c,1e7), p)\n", + " pst = pd.DataFrame(st, columns=['{} - {}'.format(c, p)])\n", + " pstl.append(pst)\n", + " \n", + "psta = pd.concat(pstl)\n", + "pd.options.display.float_format = '{:.2f}'.format\n", + "display(psta.describe())\n", + "psta.hist(figsize=(16, 9))\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pd.concat([pst, pst], axis=1)" + ] + } + ], + "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 +}