ld2daps/Notebooks/Triskele Python Demo.ipynb

225 lines
4.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Init"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import Triskele\n",
"\n",
"I did not install triskele-python on my system folders, so this is a trick to load the module outside the triskele project."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append('../triskele/python/') # Change with your triskele install path\n",
"import triskele"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Simple test\n",
"\n",
"Just to test the python import and the triskele compilation. Create an array with random values."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"raster = np.random.randint(0, 255, (1000, 1000))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create the instance of triskele object with the array to filter."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ap_random = triskele.Triskele(raster)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Filter the raster with this method :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"filtered_raster = ap_random.filter()\n",
"print(filtered_raster)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If there is no error so far everything is okay.\n",
"\n",
"Theoretically the filtered output should be a copy of the input. (it's not, there is something going on along the edges, we should tell François !) \n",
"\n",
"## Attribute filter\n",
"\n",
"With `filter` method you can ask for specific attributes filtering. The results are stacked."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"filtered_rasters = ap_random.filter(area=[10, 100, 1e3])\n",
"print(filtered_rasters.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I don't know if you can see the signature with yout IDE : `filter(tree='max-tree', area=None, standard_deviation=None, moment_of_inertia=None)`\n",
"\n",
"The parameters are the same than apGenerator from François, I should write the doc..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Advanced test\n",
"\n",
"With a real example.\n",
"\n",
"Load a TIFF file with 4 bands :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"raster = triskele.read('../triskele/data/10m.tif')\n",
"print(raster.shape)\n",
"\n",
"plt.imshow(raster)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create the instance with some tweaks."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ap_10m = triskele.Triskele(raster, verbose=False) # Verbose False to hide apGenerator gibbering :p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Filter as you wish :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tree = 'tos-tree' # {'max-tree', 'min-tree', ...}\n",
"area = [100, 1e3, 1e4]\n",
"standard_deviation = None\n",
"moment_of_inertia = None\n",
"\n",
"filtered_rasters = ap_10m.filter(tree=tree, area=area, standard_deviation=standard_deviation, moment_of_inertia=moment_of_inertia)\n",
"print(filtered_rasters.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Display results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(filtered_rasters.shape[2]):\n",
" plt.imshow(filtered_rasters[:,:,i])\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"
}
},
"nbformat": 4,
"nbformat_minor": 2
}