{ "cells": [ { "cell_type": "markdown", "id": "f8504247", "metadata": {}, "source": [ "## Network topology" ] }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "import json\n", "import pandas as pd\n", "import numpy as np\n", "from openpyxl import load_workbook\n", "from typing import Dict" ], "metadata": { "collapsed": false }, "id": "716f96a9b3ced41e" }, { "cell_type": "markdown", "source": [ "##### load input data" ], "metadata": { "collapsed": false }, "id": "dcb7731bc5199c48" }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "df = pd.read_excel(open('input.xlsx', 'rb'), sheet_name=0)\n", "df = df[df.notna()]\n", "print(df.columns)" ], "metadata": { "collapsed": false }, "id": "9c995a37cd9834ce" }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "output = {} # network info" ], "metadata": { "collapsed": false }, "id": "fd82ba80087e27ae" }, { "cell_type": "markdown", "source": [ "##### topology: edge information" ], "metadata": { "collapsed": false }, "id": "72cf999a54207fd3" }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "tail = df.t\n", "head = df.h\n", "tail = tail[~np.isnan(tail)]\n", "head = head[~np.isnan(head)]\n", "tail = tail.astype(int)\n", "head = head.astype(int)\n", "ed_ls = [(t, h) for t, h in zip(tail, head)]" ], "metadata": { "collapsed": false }, "id": "3b0ea401657601b5" }, { "cell_type": "markdown", "source": [ "##### edge attributes (length and diameter)" ], "metadata": { "collapsed": false }, "id": "525c2db07f954e25" }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "d = df.d\n", "l = df.l\n", "d = d[~np.isnan(d)]\n", "l = l[~np.isnan(l)]" ], "metadata": { "collapsed": false }, "id": "169abcb4288e4298" }, { "cell_type": "markdown", "source": [ "##### save edge attributes" ], "metadata": { "collapsed": false }, "id": "8a2c42d8a124a147" }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "output['tail'] = tail\n", "output['head'] = head\n", "output['l'] = l\n", "output['d'] = d\n", "output['ed_ls'] = ed_ls" ], "metadata": { "collapsed": false }, "id": "4622c3e7c1252482" }, { "cell_type": "markdown", "source": [ "##### topology: node information" ], "metadata": { "collapsed": false }, "id": "86f0d886edc72d09" }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "nodes = df.nodes\n", "nodes = nodes[~np.isnan(nodes)]\n", "nodes = [int(n) for n in nodes]\n", "\n", "xpos = df.xpos\n", "xpos = xpos[~np.isnan(xpos)]\n", "ypos = df.ypos\n", "ypos = ypos[~np.isnan(ypos)]\n", "zpos = df.zpos\n", "zpos = zpos[~np.isnan(zpos)]\n", "xyz = [[x, y, z] for x, y, z in zip(xpos, ypos, zpos)]\n" ], "metadata": { "collapsed": false }, "id": "8ba9f71e5e72b4fe" }, { "cell_type": "markdown", "source": [ "##### save node attributes" ], "metadata": { "collapsed": false }, "id": "e1632b1928afe75f" }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "output['nodes'] = nodes\n", "output['source'] = df.hNode.dropna()\n", "output['target'] = df.tNode.dropna()\n", "output['xyz'] = xyz" ], "metadata": { "collapsed": false }, "id": "3743e0b3ebee71e9" }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "with open('input.json', 'w') as f:\n", " json.dump(output, f, indent=2)\n" ], "metadata": { "collapsed": false }, "id": "e81e2f213f0a4362" }, { "cell_type": "code", "execution_count": null, "id": "28b94ce7-7c0a-4421-ad50-fbe7a0e8c8d5", "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.11.1" } }, "nbformat": 4, "nbformat_minor": 5 }