{ "cells": [ { "cell_type": "markdown", "id": "64cdaa1c", "metadata": {}, "source": [ "# Network creation" ] }, { "cell_type": "code", "execution_count": 1, "id": "4c908b8f", "metadata": {}, "outputs": [], "source": [ "\n", "def create_graph(\n", " output=[],\n", " directed=False,\n", " attributes=False,\n", " derived_attr=False,\n", " sheet=None,\n", " actual_pos=False,\n", " cd=False\n", "):\n", " if not output:\n", " output = get_graph(sheet=sheet)\n", "\n", " if directed:\n", " G = nx.OrderedDiGraph()\n", " else:\n", " G = nx.OrderedGraph()\n", "\n", " G.add_edges_from(output['ed_ls'])\n", " if 'xyz' in output:\n", " nodes = list(sorted(G.nodes()))\n", " assert(nodes == output['nodes'])\n", " if actual_pos:\n", " pos = OrderedDict(zip(nodes, output['xyz_actual']))\n", " else:\n", " pos = OrderedDict(zip(nodes, output['xyz']))\n", " nx.set_node_attributes(G, pos, 'pos')\n", "\n", " else: # pseudo positions\n", " logging.warning(\"Coordinate positions assigned from networkx's layout ...\")\n", " pos = nx.random_layout(G, dim=3, seed=1)\n", " nx.set_node_attributes(G, pos, 'pos')\n", "\n", " if attributes:\n", " # edges = sorted(G.edges)\n", " # assert(edges == list(zip(list(output['tail']), list(output['head']))))\n", " edges = list(zip(list(output['tail']), list(output['head'])))\n", " d = OrderedDict(zip(edges, output['d']))\n", " nx.set_edge_attributes(G, d, 'd')\n", " if cd:\n", " l = OrderedDict(zip(edges, output['cd']))\n", " else:\n", " l = OrderedDict(zip(edges, output['l']))\n", " nx.set_edge_attributes(G, l, 'l')\n", "\n", " if derived_attr:\n", " node_r = OrderedDict(zip(nodes, output['node_r']))\n", " nx.set_node_attributes(G, node_r, 'node_r')\n", " node_l = OrderedDict(zip(nodes, output['node_l']))\n", " nx.set_node_attributes(G, node_l, 'node_l')\n", "\n", " return G\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "56e6168f-59de-49b6-bf61-16d0071d62af", "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 }