Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example notebook for showing node connectivity information #24

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
376 changes: 376 additions & 0 deletions notebooks/node_connectivity_information.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,376 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Node connectivity information\n",
"This example demonstrates some functionality related to node connectivity, such as:\n",
"- Counting number of links connected to a node\n",
"- Coutning number of enabled links connected to a node\n",
"- Getting a list of upstream/downstream links connected to a node"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Imports\n",
"import pandas as pd\n",
"from mikeplus import DataTableAccess"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<DataTableContainer>\n",
"Db major version: 2024\n",
"Db minor version: 0.0\n",
"Active model: CS_MIKE1D\n",
"Unit system: MU_CS_SI\n",
"Active simulation: Sirius_1_DEMO"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Open the database\n",
"dta = DataTableAccess(\"../tests/testdata/Db/Sirius/Sirius.sqlite\")\n",
"dta.open_database()\n",
"dta"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<DHI.Amelia.DataModule.Services.DataTables.MsmNodeTable object at 0x00000294752022C0>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get the MsmNodeTable object\n",
"msm_node = dta._datatables[\"msm_Node\"].__implementation__\n",
"msm_node"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Node_36',\n",
" 'Node_27',\n",
" 'Node_28',\n",
" 'Node_30',\n",
" 'Node_31',\n",
" 'Node_32',\n",
" 'Node_33',\n",
" 'Node_34',\n",
" 'Inflow to_WWTP_Basin',\n",
" 'PS_To_WWTP']"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get all MUIDs\n",
"muids = msm_node.GetMuids()\n",
"list(muids)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get number of links connected to Node_36\n",
"msm_node.GetNumberofLinks(\"Node_36\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get number of links connected to Node_36 that are enabled\n",
"msm_node.GetNumOfLinksEnabled(\"Node_36\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Link_29']"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get upstream link MUIDs connected to Node_36\n",
"# Note: upstream links refers to links where \"Node_36\" is the upstream node. So in practice, it includes links upstream of \"Node_36\".\n",
"upstream_links = msm_node.GetUpstreamLinks(\"Node_36\")\n",
"list(upstream_links)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get downstream link MUIDs connected to Node_36\n",
"# Note: downstream links refers to links where \"Node_36\" is the downstream node. So in practice, it includes links downstream of \"Node_36\".\n",
"downstream_links = msm_node.GetDownstreamLinks(\"Node_36\")\n",
"list(downstream_links)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"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>num_of_links</th>\n",
" <th>num_of_links_enabled</th>\n",
" <th>num_upstream_links</th>\n",
" <th>num_downstream_links</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Node_36</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Node_27</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Node_28</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Node_30</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Node_31</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Node_32</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Node_33</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Node_34</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Inflow to_WWTP_Basin</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>PS_To_WWTP</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" num_of_links num_of_links_enabled num_upstream_links \\\n",
"Node_36 1 1 1 \n",
"Node_27 2 2 1 \n",
"Node_28 2 2 1 \n",
"Node_30 2 2 1 \n",
"Node_31 2 2 1 \n",
"Node_32 2 2 1 \n",
"Node_33 2 2 1 \n",
"Node_34 2 2 1 \n",
"Inflow to_WWTP_Basin 1 1 0 \n",
"PS_To_WWTP 0 0 0 \n",
"\n",
" num_downstream_links \n",
"Node_36 0 \n",
"Node_27 1 \n",
"Node_28 1 \n",
"Node_30 1 \n",
"Node_31 1 \n",
"Node_32 1 \n",
"Node_33 1 \n",
"Node_34 1 \n",
"Inflow to_WWTP_Basin 1 \n",
"PS_To_WWTP 0 "
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Same as above, but for all nodes and visualized in a DataFrame\n",
"data = {muid: {\n",
" 'num_of_links' : msm_node.GetNumberofLinks(muid),\n",
" 'num_of_links_enabled' : msm_node.GetNumberofLinks(muid),\n",
" 'num_upstream_links' : msm_node.GetUpstreamLinks(muid).Count,\n",
" 'num_downstream_links' : msm_node.GetDownstreamLinks(muid).Count,\n",
"} for muid in msm_node.GetMuids()}\n",
"df = pd.DataFrame(data).T\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Close the database\n",
"dta.close_database()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (webinar)",
"language": "python",
"name": "webinar"
},
"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.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading