Skip to content

Instantly share code, notes, and snippets.

@taruma
Created December 17, 2019 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taruma/8dd920bee9fa95cf6eba39cc9d694953 to your computer and use it in GitHub Desktop.
Save taruma/8dd920bee9fa95cf6eba39cc9d694953 to your computer and use it in GitHub Desktop.
taruma_hk99_thiessen.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "taruma_hk99_thiessen.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/taruma/8dd920bee9fa95cf6eba39cc9d694953/taruma_hk99_thiessen.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "c8y38HKGPSqX",
"colab_type": "text"
},
"source": [
"Berdasarkan isu [#99](https://github.com/taruma/hidrokit/issues/99): **buat perhitungan curah hujan dengan metode poligon Thiessen**\n",
"\n",
"Referensi isu:\n",
"- Limantara, Lily M. (2018). _Rekayasa Hidrologi_. Penerbit Andi, Yogyakarta. (hal. 57).\n",
"\n",
"Deskripsi Permasalahan:\n",
"- Menghitung nilai hujan rata-rata daerah menggunakan metode poligon Thiessen"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xsGQ3p3xQQzm",
"colab_type": "text"
},
"source": [
"# PERSIAPAN DAN DATASET"
]
},
{
"cell_type": "code",
"metadata": {
"id": "jkGkRhCqQSkb",
"colab_type": "code",
"colab": {}
},
"source": [
"import numpy as np\n",
"import pandas as pd"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "RciObn68QftA",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"outputId": "f4715cfd-067f-418c-f4ce-95725c1c52bb"
},
"source": [
"try:\n",
" import hidrokit\n",
"except ModuleNotFoundError:\n",
" !pip install git+https://github.com/taruma/hidrokit.git@latest -q\n",
" import hidrokit\n",
"print(f'hidrokit version: {hidrokit.__version__}')"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
" Building wheel for hidrokit (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
"hidrokit version: 0.3.5-beta.3\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "r_BEPY1kQU0M",
"colab_type": "code",
"colab": {}
},
"source": [
"!wget -O sample.xlsx \"https://taruma.github.io/assets/hidrokit_dataset/data_daily_sample.xlsx\" -q\n",
"dataset_path = 'sample.xlsx'"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "UZ_ZHiNXQjC6",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 357
},
"outputId": "7bc1cbe5-1aae-43d9-c795-a9f19ec69f95"
},
"source": [
"from hidrokit.contrib.taruma import hk88\n",
"\n",
"_data = hk88.read_workbook(dataset_path, ['STA_A', 'STA_B', 'STA_C'], \n",
" as_df=False)\n",
"dataset = pd.concat(_data, sort=True, axis=1).infer_objects()\n",
"dataset.info()\n",
"dataset.head()"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"DatetimeIndex: 5478 entries, 2001-01-01 to 2015-12-31\n",
"Freq: D\n",
"Data columns (total 3 columns):\n",
"STA_A 5477 non-null float64\n",
"STA_B 5470 non-null float64\n",
"STA_C 5475 non-null float64\n",
"dtypes: float64(3)\n",
"memory usage: 171.2 KB\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>STA_A</th>\n",
" <th>STA_B</th>\n",
" <th>STA_C</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2001-01-01</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2001-01-02</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.65</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2001-01-03</th>\n",
" <td>0.0</td>\n",
" <td>45.0</td>\n",
" <td>9.16</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2001-01-04</th>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2001-01-05</th>\n",
" <td>0.0</td>\n",
" <td>5.0</td>\n",
" <td>1.03</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" STA_A STA_B STA_C\n",
"2001-01-01 0.0 0.0 0.00\n",
"2001-01-02 0.0 0.0 0.65\n",
"2001-01-03 0.0 45.0 9.16\n",
"2001-01-04 0.0 0.0 0.00\n",
"2001-01-05 0.0 5.0 1.03"
]
},
"metadata": {
"tags": []
},
"execution_count": 4
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qae0mTUVQplH",
"colab_type": "text"
},
"source": [
"# KODE"
]
},
{
"cell_type": "code",
"metadata": {
"id": "roXT5pYZGiSz",
"colab_type": "code",
"colab": {}
},
"source": [
"def thiessen_weight(area):\n",
" area_val = list(area.values())\n",
" area_percent = area_val / np.sum(area_val)\n",
" key = list(area.keys())\n",
" return dict(zip(key, area_percent))\n",
"\n",
"def apply_thiessen(dataset, area, columns=None, as_df=True):\n",
" weight = thiessen_weight(area)\n",
"\n",
" columns = columns if columns is not None else dataset.columns\n",
"\n",
" val = []\n",
" for col in columns:\n",
" val.append(dataset[col].values * weight[col])\n",
" \n",
" np_val = np.stack(val, axis=1)\n",
"\n",
" if as_df:\n",
" return pd.DataFrame(\n",
" data=np_val.sum(axis=1), index=dataset.index, columns=['thiessen']\n",
" )\n",
" else:\n",
" return np_val.sum(axis=1)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "KP3mfipMQvPu",
"colab_type": "text"
},
"source": [
"# FUNGSI"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "pPIjLiDAQw3y",
"colab_type": "text"
},
"source": [
"## Fungsi `thiessen_weight()`\n",
"\n",
"Fungsi ini menerima input _dictionary_ yang berisikan besar area setiap stasiun dan memberi keluaran bobotnya dalam bentuk _dictionary_. "
]
},
{
"cell_type": "code",
"metadata": {
"id": "ZrkFWo76PRtr",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "ffe2ac11-1154-4d01-813e-1441c3b9d263"
},
"source": [
"AREA_BASIN = {\n",
" 'STA_A': 49.54,\n",
" 'STA_B': 144.39,\n",
" 'STA_C': 0\n",
"}\n",
"\n",
"weight = thiessen_weight(AREA_BASIN)\n",
"weight"
],
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'STA_A': 0.2554529985046151, 'STA_B': 0.744547001495385, 'STA_C': 0.0}"
]
},
"metadata": {
"tags": []
},
"execution_count": 6
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "p8I1hve9RfES",
"colab_type": "text"
},
"source": [
"## Fungsi `apply_thiessen()`\n",
"\n",
"Menerapkan perhitungan poligon thiessen dengan bobot masing-masing stasiun dan mengeluarkan hasil penjumlahannya.\n",
"\n",
"Fungsi ini memiliki beberapa argumen opsional yaitu `as_df` dan `columns`."
]
},
{
"cell_type": "code",
"metadata": {
"id": "lnnnqFYVRV72",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 419
},
"outputId": "0d42a0bb-0b99-4481-f393-163b96603222"
},
"source": [
"apply_thiessen(dataset, AREA_BASIN)"
],
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>thiessen</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2001-01-01</th>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2001-01-02</th>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2001-01-03</th>\n",
" <td>33.504615</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2001-01-04</th>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2001-01-05</th>\n",
" <td>3.722735</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-12-27</th>\n",
" <td>14.174496</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-12-28</th>\n",
" <td>29.190017</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-12-29</th>\n",
" <td>1.788171</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-12-30</th>\n",
" <td>1.277265</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-12-31</th>\n",
" <td>0.766359</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5478 rows × 1 columns</p>\n",
"</div>"
],
"text/plain": [
" thiessen\n",
"2001-01-01 0.000000\n",
"2001-01-02 0.000000\n",
"2001-01-03 33.504615\n",
"2001-01-04 0.000000\n",
"2001-01-05 3.722735\n",
"... ...\n",
"2015-12-27 14.174496\n",
"2015-12-28 29.190017\n",
"2015-12-29 1.788171\n",
"2015-12-30 1.277265\n",
"2015-12-31 0.766359\n",
"\n",
"[5478 rows x 1 columns]"
]
},
"metadata": {
"tags": []
},
"execution_count": 7
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "y_6OFXkCSg19",
"colab_type": "text"
},
"source": [
"### Argumen `as_df=True`\n",
"\n",
"Argumen ini memberi keluaran dalam bentuk `pandas.DataFrame` jika `as_df=True` (_default_), dan dalam bentuk `numpy.array` jika `as_df=False`."
]
},
{
"cell_type": "code",
"metadata": {
"id": "9eqlAkH-RqBu",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"outputId": "6ff61b76-ac22-45ce-958f-54f2dc1353e3"
},
"source": [
"apply_thiessen(dataset, AREA_BASIN, as_df=False)"
],
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([ 0. , 0. , 33.50461507, ..., 1.78817099,\n",
" 1.27726499, 0.766359 ])"
]
},
"metadata": {
"tags": []
},
"execution_count": 8
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wxpY9xB0S48A",
"colab_type": "text"
},
"source": [
"### Argumen `columns=None`\n",
"\n",
"Argumen ini memastikan kolom mana saja yang dihitung. Jika tidak diisi, maka fungsi memproses seluruh kolom. Jika nilai kolom tidak tersedia dalam `area`, maka akan menghasilkan error. Gunakan opsi `columns` jika dataset berisikan kolom yang tidak digunakan dalam proses perhitungan thiessen."
]
},
{
"cell_type": "code",
"metadata": {
"id": "yLlYoLhFS27L",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 419
},
"outputId": "b0b77e91-b36c-448a-ee32-dbe4a6128255"
},
"source": [
"apply_thiessen(dataset, AREA_BASIN, columns=['STA_A', 'STA_C'])"
],
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>thiessen</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2001-01-01</th>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2001-01-02</th>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2001-01-03</th>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2001-01-04</th>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2001-01-05</th>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-12-27</th>\n",
" <td>9.707214</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-12-28</th>\n",
" <td>5.364513</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-12-29</th>\n",
" <td>1.788171</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-12-30</th>\n",
" <td>1.277265</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2015-12-31</th>\n",
" <td>0.766359</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5478 rows × 1 columns</p>\n",
"</div>"
],
"text/plain": [
" thiessen\n",
"2001-01-01 0.000000\n",
"2001-01-02 0.000000\n",
"2001-01-03 0.000000\n",
"2001-01-04 0.000000\n",
"2001-01-05 0.000000\n",
"... ...\n",
"2015-12-27 9.707214\n",
"2015-12-28 5.364513\n",
"2015-12-29 1.788171\n",
"2015-12-30 1.277265\n",
"2015-12-31 0.766359\n",
"\n",
"[5478 rows x 1 columns]"
]
},
"metadata": {
"tags": []
},
"execution_count": 9
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SqREB92QTrJo",
"colab_type": "text"
},
"source": [
"# Changelog\n",
"\n",
"```\n",
"- 20191217 - 1.0.0 - Initial\n",
"```\n",
"\n",
"#### Copyright &copy; 2019 [Taruma Sakti Megariansyah](https://taruma.github.io)\n",
"\n",
"Source code in this notebook is licensed under a [MIT License](https://choosealicense.com/licenses/mit/). Data in this notebook is licensed under a [Creative Common Attribution 4.0 International](https://creativecommons.org/licenses/by/4.0/). \n"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment