ld2daps/Notebooks/Split First.ipynb
2018-09-21 15:51:04 +02:00

275 lines
5.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import triskele\n",
"import ld2dap\n",
"\n",
"figsize = np.array((16, 9)) * 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Split First for JURSE\n",
"\n",
"Load raster, $n$ split along dimension $d$, describe and merge in description stack."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n = 1; d = 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"layers_files = [\n",
" '../Data/phase1_rasters/DEM+B_C123/UH17_GEM051_TR.tif',\n",
" '../Data/phase1_rasters/DEM_C123_3msr/UH17_GEG051_TR.tif',\n",
" '../Data/phase1_rasters/DEM_C123_TLI/UH17_GEG05_TR.tif',\n",
" '../Data/phase1_rasters/DSM_C12/UH17c_GEF051_TR.tif',\n",
" '../Data/phase1_rasters/Intensity_C1/UH17_GI1F051_TR.tif',\n",
" '../Data/phase1_rasters/Intensity_C2/UH17_GI2F051_TR.tif',\n",
" '../Data/phase1_rasters/Intensity_C3/UH17_GI3F051_TR.tif'\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"load = ld2dap.LoadTIFF(layers_files)\n",
"trsh = ld2dap.Treshold(1e4)\n",
"norm = ld2dap.Normalize(dtype=np.uint8)\n",
"rout = ld2dap.RawOutput()\n",
"\n",
"rout.input = norm\n",
"norm.input = trsh\n",
"trsh.input = load\n",
"\n",
"rout.run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Split"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"step = int(rout.data.shape[d] / n)\n",
"\n",
"fig, axs = plt.subplots(n, figsize=figsize * n)\n",
"\n",
"view = np.moveaxis(rout.data, d, 0)\n",
"\n",
"cuts = list()\n",
"for i in range(n):\n",
" cut = np.moveaxis(view[i*step:(i+1)*step+1], 0, d)\n",
" cuts.append(cut)\n",
" \n",
" # Disp\n",
" axs[i].imshow(cut[:,:,0], cmap=plt.get_cmap('GnBu'))\n",
" axs[i].set_title('Cut {}'.format(i))\n",
" \n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Describe"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dcuts = list()\n",
"\n",
"for cut in cuts:\n",
" rinp = ld2dap.RawInput(cut, rout.metadata)\n",
" aps = ld2dap.SelfDualAttributeProfiles(area=[100,1e3,1e4,1e5], normalize_to_dtype=False)\n",
" vout = ld2dap.RawOutput()\n",
" \n",
" vout.input = aps\n",
" aps.input = rinp\n",
" \n",
" vout.run()\n",
" \n",
" dcuts.append(vout.data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Merge"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"descriptors = np.zeros(rout.data.shape[:2] + (dcuts[0].shape[-1],))\n",
"view = np.moveaxis(descriptors, d, 0)\n",
"\n",
"for i, cut in enumerate(dcuts):\n",
" view[i*step:(i+1)*step+1] = np.moveaxis(cut, 0, d)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visual test"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t_inp = ld2dap.RawInput(descriptors, vout.metadata)\n",
"t_dsp = ld2dap.ShowFig(stack_id=3, symb=False)\n",
"\n",
"t_dsp.input = t_inp\n",
"\n",
"t_dsp.run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Split cross val"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"d = 0\n",
"s = 2\n",
"n = 3\n",
"\n",
"\n",
"step = int(descriptors.shape[d] / n)\n",
"size = descriptors.shape[d]\n",
"cfilter = (np.arange(size) - step * s) % size < step\n",
"\n",
"test_filter = np.zeros_like(descriptors[:,:,0], dtype=np.bool)\n",
"view = np.moveaxis(test_filter, d, 0)\n",
"view[cfilter] = True\n",
"\n",
"plt.imshow(test_filter)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"descriptors[test_filter].shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"descriptors.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f = np.zeros_like(test_filter, dtype=np.bool)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_filter |= f\n",
"test_filter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_filter"
]
}
],
"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
}