Update Notebooks

This commit is contained in:
Florent Guiotte 2018-03-23 19:04:32 +01:00
parent 291aa288d4
commit c110ea1d27
3 changed files with 782 additions and 6 deletions

View File

@ -0,0 +1,252 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"from pathlib import Path\n",
"import numpy as np\n",
"import libtiff\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": "markdown",
"metadata": {},
"source": [
"## List raster files"
]
},
{
"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": "markdown",
"metadata": {},
"source": [
"## Define dataset dependent raster filtering"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def DFC_filter(raster):\n",
" ## Remove extrem values\n",
" #raster[raster == raster.max()] = raster[raster != raster.max()].max()\n",
" raster[raster > 1e4] = raster[raster < 1e4].max()\n",
" #raster[raster == np.finfo(raster.dtype).max] = raster[raster != raster.max()].max()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load rasters data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"layers = list()\n",
"\n",
"for file in layers_files:\n",
" print('Loading {}'.format(file))\n",
" layer = libtiff.TIFF.open(file).read_image()\n",
" DFC_filter(layer)\n",
" layers.append(layer)\n",
"\n",
"layers_stack = np.stack(layers, axis=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Display rasters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(layers_stack.shape[2]):\n",
" plt.figure(figsize=(16*2,3*2))\n",
" plt.imshow(layers_stack[:,:,i])\n",
" plt.colorbar()\n",
" plt.title(layers_files[i])\n",
" plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Attributes filter with TRISKELE !"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t = triskele.Triskele(layers_stack[0], verbose=False)\n",
"attributes = t.filter()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"attributes.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"layers_stack.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test = libtiff.TIFF.open('../Res/test.tif', mode='w')\n",
"test.write_image(np.rollaxis(layers_stack, 2).astype(np.float32))\n",
"test = None"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"layers_stack.shape[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test = libtiff.TIFF.open('../Res/test.tif', mode='w')\n",
"test.\n",
"#test = None\n",
"#test.write_tiles(layers_stack[:,:,0].astype(np.uint8), tile_width=layers_stack.shape[1], tile_height=layers_stack.shape[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.rollaxis(layers_stack, 2).shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tiff = libtiff.TIFFimage(np.rollaxis(layers_stack, 1), description='BDQ')\n",
"tiff.write_file('../Res/test.tif')\n",
"del tiff"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test = libtiff.TIFF.open('../Res/test.tif')\n",
"for image in test.iter_images():\n",
" print(image.shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test = libtiff.TIFFimage(layers_stack)\n",
"test.write_file('../Res/test.tif')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(attributes.shape[2]):\n",
" plt.figure(figsize=(16*2,3*2))\n",
" plt.imshow(attributes[:,:,i])\n",
" plt.colorbar()\n",
" plt.title(layers_files[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
}

View File

@ -0,0 +1,431 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import gdal\n",
"from pathlib import Path\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"outfile = Path('../Res/test.tif')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"raster = np.arange(20 * 10).reshape(10, 20)\n",
"raster.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds = gdal.Open(outfile.as_posix())\n",
"ds"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cols = raster.shape[1]\n",
"rows = raster.shape[0]\n",
"\n",
"driver = gdal.GetDriverByName('GTiff')\n",
"dst_ds = driver.Create(outfile.as_posix(), cols, rows, 2, gdal.GDT_Byte)\n",
"dst_ds.GetRasterBand(1).WriteArray(raster.astype(np.uint8))\n",
"dst_ds.GetRasterBand(2).WriteArray(raster.astype(np.uint8))\n",
"dst_ds.FlushCache()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import gdal\n",
"\n",
"gdal.GDT_TypeCount"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np_to_gdt = {\n",
" np.uint8: gdal.GDT_Byte,\n",
" np.float32: gdal.GDT_Float32,\n",
" np.float64: gdal.GDT_Float64,\n",
" np.uint16: gdal.GDT_Int16,\n",
" np.uint32: gdal.GDT_Int32\n",
" }\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"raster.dtype.type"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.double"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.savetxt?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"raster.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.rollaxis(dst_ds.GetVirtualMemArray(), 0, 3)[:,:,0] == raster #[:] = np.repeat(raster, 2).reshape(2, rows, cols)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gdl_data = gdal.Open('/tmp/outfile.tif')\n",
"gdl_data.GetMetadata(), gdl_data.RasterCount"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gdl_data.GetVirtualMemArray()[0,:,:] == gdl_data.GetRasterBand(1).ReadAsArray()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gdl_data.GetRasterBand(1).ReadAsArray().shape, gdl_data.GetRasterBand(1).ReadAsArray().dtype"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"from pathlib import Path\n",
"import numpy as np\n",
"\n",
"triskele_path = Path('../triskele/python/')\n",
"sys.path.append(str(triskele_path.resolve()))\n",
"import importlib\n",
"import triskele\n",
"\n",
"importlib.reload(triskele)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triskele.read('/tmp/outfile.tif')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triskele.Triskele"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gdl = gdal.Open('/tmp/outfile.tif')\n",
"gdl.ReadAsArray()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import gdal\n",
"\n",
"def read(fname):\n",
" gdl_data = gdal.Open(str(fname))\n",
" #tmp = gdl_data.GetRasterBand(1).ReadAsArray()\n",
" return gdl_data.ReadAsArray()\n",
" #return np.rollaxis(gdl_data.GetVirtualMemArray(), 0, 3)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"qwe = gdal.Open('test.tif')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"qwe."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"read('/tmp/outfile.tif')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X = np.arange(20*12*5).reshape(12,20,5)\n",
"X.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triskele.write('../Res/test.tif', X[:,:,0].astype(np.uint8))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Y = triskele.read('../Res/test.tif')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Y.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(X.astype(np.uint8) == Y).all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(X.shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = X[:,:,0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.expand_dims(X, 2).shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triskele.write('test.tif', x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = triskele.read('../Data/phase1_rasters/DEM_C123_3msr/UH17_GEG051_TR.tif')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t = triskele.Triskele(data, verbose=True)\n",
"res = t.filter(area=np.array([10,100,1000]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = np.stack((data, data), axis=2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"res.shape, res.dtype"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triskele.write('ttest.tif', res[:,:,0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triskele.read('ttest.tif').shape"
]
}
],
"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
}

View File

@ -9,13 +9,30 @@
"import sys\n",
"from pathlib import Path\n",
"import numpy as np\n",
"import libtiff\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": [
"def DFC_filter(raster):\n",
" raster[raster > 1e4] = raster[raster < 1e4].max()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## One raster"
]
},
{
"cell_type": "code",
"execution_count": null,
@ -23,8 +40,8 @@
"outputs": [],
"source": [
"infile = Path('../Data/phase1_rasters/DSM_C12/UH17c_GEF051_TR.tif')\n",
"raster = libtiff.TIFF.open(infile).read_image()\n",
"raster[raster == raster.max()] = raster[raster != raster.max()].max()"
"raster = triskele.read(infile)\n",
"DFC_filter(raster)"
]
},
{
@ -42,9 +59,11 @@
"metadata": {},
"outputs": [],
"source": [
"area = np.array([10, 100, 1000])\n",
"area = np.array([10, 100, 1e3, 1e4, 1e5])\n",
"moi = np.array([.0, .01, .1, .2, .3, .4, .5, .9, .99, 1.])\n",
"\n",
"filtered = t.filter(tree='tos-tree', area=area)"
"filtered = t.filter(tree='tos-tree')\n",
"filtered.shape"
]
},
{
@ -53,7 +72,81 @@
"metadata": {},
"outputs": [],
"source": [
"filtered"
"plt.figure(figsize=(16*1,5*1))\n",
"plt.imshow(filtered[:,:].astype(np.float))\n",
"plt.colorbar()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## More raster"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"infile1 = Path('../Data/phase1_rasters/DSM_C12/UH17c_GEF051_TR.tif')\n",
"infile2 = Path('../Data/phase1_rasters/Intensity_C1/UH17_GI1F051_TR.tif')\n",
"raster1 = triskele.read(infile1)\n",
"raster2 = triskele.read(infile2)\n",
"DFC_filter(raster1)\n",
"DFC_filter(raster2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(16*1,5*1))\n",
"plt.imshow(raster1[:,:].astype(np.float))\n",
"plt.colorbar()\n",
"plt.show()\n",
"\n",
"plt.figure(figsize=(16*1,5*1))\n",
"plt.imshow(raster2[:,:].astype(np.float))\n",
"plt.colorbar()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = np.stack((raster1, raster2), axis=2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t = triskele.Triskele(data, verbose=False)\n",
"f = t.filter(tree='tos-tree', area=area)\n",
"f.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(f.shape[2]):\n",
" plt.figure(figsize=(16*1,5*1))\n",
" plt.imshow(f[:,:,i].astype(np.float))\n",
" plt.colorbar()\n",
" plt.show()"
]
}
],