Skip to content

Commit

Permalink
Updated steps 1, 2, and 3 for the Dask tutorial.
Browse files Browse the repository at this point in the history
  • Loading branch information
mseehaber committed Jul 15, 2022
1 parent bd133fc commit ee1fa81
Show file tree
Hide file tree
Showing 3 changed files with 2,379 additions and 0 deletions.
121 changes: 121 additions & 0 deletions Step1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "e3eb5d08-8515-4cfb-9ea4-f45c12914980",
"metadata": {},
"outputs": [],
"source": [
"# loading packages \n",
"import datacube \n",
"import matplotlib.pyplot as plt\n",
"from deafrica_tools.dask import create_local_dask_cluster\n",
"from deafrica_tools.plotting import rgb, display_map"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75ff9a1c-7d8f-4c40-95bf-101bf24c277d",
"metadata": {},
"outputs": [],
"source": [
"# connecting to the datacube \n",
"dc = datacube.Datacube(app='Step1')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "46d79dd1-c90e-409d-bdad-81e6d45675c9",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# standard load function for data\n",
"data = dc.load(product='gm_s2_annual',\n",
" measurements=['red','green', 'blue', 'nir'],\n",
" x=(31.90, 32.00),\n",
" y=(30.49, 30.40),\n",
" time=('2020-01-01', '2020-12-31'))\n",
"\n",
"# return data \n",
"data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f77b995c-62c3-413f-a115-99212c813cf2",
"metadata": {},
"outputs": [],
"source": [
"# lazy loading data through dask chunks parameter\n",
"lazy_data = dc.load(product='gm_s2_annual',\n",
" measurements=['red','green', 'blue', 'nir'],\n",
" x=(31.90, 32.00),\n",
" y=(30.49, 30.40),\n",
" time=('2020-01-01', '2020-12-31'),\n",
" dask_chunks={'time':1,'x':500, 'y':500})\n",
"# return data\n",
"lazy_data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "59b47fac-97aa-4f59-aaf0-312a4a6a457f",
"metadata": {},
"outputs": [],
"source": [
"# View a rgb (true colour) image of study area\n",
"rgb(lazy_data, bands=['red', 'green', 'blue'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4db3c37d-adc5-4507-b169-c89e2da87cb7",
"metadata": {},
"outputs": [],
"source": [
"# formatting the figure \n",
"fig , axis = plt.subplots(1, 1, figsize=(6, 6))\n",
"axis.set_aspect('equal')\n",
"\n",
"lazy_data.red.plot(ax= axis)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "905a0368-e01a-4424-848b-1f1172430ed9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
160 changes: 160 additions & 0 deletions Step2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "ea78b033-efa5-4675-99f4-a70f0bbe3b3b",
"metadata": {},
"outputs": [],
"source": [
"# loading packages \n",
"import datacube \n",
"import matplotlib.pyplot as plt\n",
"from deafrica_tools.dask import create_local_dask_cluster\n",
"from deafrica_tools.plotting import rgb, display_map"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0e075b5a-4937-40ed-9e99-271f71dce937",
"metadata": {},
"outputs": [],
"source": [
"# connecting to the datacube \n",
"dc = datacube.Datacube(app='Step2')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "904dc011-c2fc-4ff5-bb68-ab06c9519da1",
"metadata": {},
"outputs": [],
"source": [
"# lazy loading data through dask chunks parameter\n",
"lazy_data = dc.load(product='gm_s2_annual',\n",
" measurements=['blue','green','red','nir'],\n",
" x=(31.90, 32.00),\n",
" y=(30.49, 30.40),\n",
" time=('2021-01-01', '2021-12-31'),\n",
" dask_chunks={'time':1,'x':500, 'y':500})\n",
"# return data\n",
"lazy_data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d39bab90-7b26-4e5a-9e24-96399467d2ba",
"metadata": {},
"outputs": [],
"source": [
"# visualise the task graph\n",
"lazy_data.red.data.visualize()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9f12c41-9b1e-47e0-ae11-6fb697e8c547",
"metadata": {},
"outputs": [],
"source": [
"# requesting small portion of red band \n",
"extract_from_red = lazy_data.red[:, 100:200, 100:200]\n",
"extract_from_red.data.visualize()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72a9cf6c-f871-44a5-b86b-7bf24c294b83",
"metadata": {},
"outputs": [],
"source": [
"# calcualte NDVI using red and nir bands from array\n",
"band_diff = lazy_data.nir - lazy_data.red\n",
"band_sum = lazy_data.nir + lazy_data.red\n",
"\n",
"# added ndvi dask array to the lazy_data dataset\n",
"lazy_data['ndvi'] = band_diff / band_sum"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e7697ac-9041-4139-a123-f99c9a94e880",
"metadata": {},
"outputs": [],
"source": [
"# return the dataset \n",
"lazy_data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d7c9f04-b722-4d2d-8f28-aa08ca6a252d",
"metadata": {},
"outputs": [],
"source": [
"lazy_data.ndvi.data.visualize()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a891d1b-e84b-4c73-91c2-d4725295a84f",
"metadata": {},
"outputs": [],
"source": [
"ndvi_compute = lazy_data.ndvi.compute()\n",
"ndvi_compute"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80fd0b17-615c-4097-a92e-791bdada40ca",
"metadata": {},
"outputs": [],
"source": [
"# formatting the figure \n",
"fig , axis = plt.subplots(1, 1, figsize=(6, 6))\n",
"axis.set_aspect('equal')\n",
"\n",
"ndvi_compute.plot(ax= axis, cmap='RdYlGn', vmin=0, vmax=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6d8a58c-68a7-484d-8d58-34a4abfa034a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit ee1fa81

Please sign in to comment.