{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandapower.networks as pn\n", "import pypower.api as pp\n", "import networkx as nx\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "graphs = [pp.case300(), pp.case118(), pp.case57(), pp.case39()]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def analyse_graph(pp_graph, plot=True):\n", " node_ids = pp_graph['bus'][:,0].astype(np.int64)\n", " edges = pp_graph['branch'][:,0:2].astype(np.int64)\n", "\n", " G = nx.Graph()\n", " G.add_nodes_from(node_ids)\n", " for edge in edges:\n", " G.add_edge(edge[0], edge[1])\n", "\n", " degree_sequence = sorted((d for n, d in G.degree()), reverse=True)\n", " dmax = max(degree_sequence)\n", "\n", " if plot:\n", " fig = plt.figure(\"Degree of a random graph\", figsize=(8, 8))\n", " # Create a gridspec for adding subplots of different sizes\n", " axgrid = fig.add_gridspec(5, 4)\n", "\n", " ax0 = fig.add_subplot(axgrid[0:3, :])\n", " Gcc = G.subgraph(sorted(nx.connected_components(G), key=len, reverse=True)[0])\n", " pos = nx.spring_layout(Gcc, seed=10396953)\n", " nx.draw_networkx_nodes(Gcc, pos, ax=ax0, node_size=20)\n", " nx.draw_networkx_edges(Gcc, pos, ax=ax0, alpha=0.4)\n", " ax0.set_title(\"Connected components of G\")\n", " ax0.set_axis_off()\n", "\n", " ax1 = fig.add_subplot(axgrid[3:, :2])\n", " ax1.plot(degree_sequence, \"b-\", marker=\"o\")\n", " ax1.set_title(\"Degree Rank Plot\")\n", " ax1.set_ylabel(\"Degree\")\n", " ax1.set_xlabel(\"Rank\")\n", "\n", " ax2 = fig.add_subplot(axgrid[3:, 2:])\n", " ax2.bar(*np.unique(degree_sequence, return_counts=True))\n", " ax2.set_title(\"Degree histogram\")\n", " ax2.set_xlabel(\"Degree\")\n", " ax2.set_ylabel(\"# of Nodes\")\n", "\n", " fig.tight_layout()\n", " plt.show()\n", " return np.unique(degree_sequence, return_counts=True)\n", "\n", "# (array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]), array([69, 76, 84, 42, 14, 6, 5, 2, 1, 1], dtype=int64)) \n", "\n", "# (array([1, 2, 3, 4, 5, 6, 7, 8, 9]), array([ 7, 56, 19, 15, 11, 6, 2, 1, 1], dtype=int64)) \n", "\n", "# (array([1, 2, 3, 4, 5, 6]), array([ 1, 32, 12, 7, 3, 2], dtype=int64)) \n", "\n", "# (array([1, 2, 3, 4, 5]), array([ 9, 12, 14, 3, 1], dtype=int64)) " ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.16731518 0.34241245 0.25097276 0.13035019 0.05642023 0.02723735\n", " 0.01361868 0.00583658 0.00389105 0. 0.00194553]\n" ] } ], "source": [ "def analyse_graphs(graphs):\n", " degree_frequencies = [analyse_graph(graph, plot=False) for graph in graphs]\n", " maxdegree = max([np.max(x[0]) for x in degree_frequencies])\n", " aggregated_degree_frequencies = {d: 0 for d in list(range(1, maxdegree+1))}\n", " for ds, cs in degree_frequencies:\n", " for d, c in zip(ds, cs):\n", " aggregated_degree_frequencies[d] += c\n", " frequencies = [x for x in aggregated_degree_frequencies.values()]\n", " # now we need to determine random degree sequences\n", " print(np.array(frequencies) / sum(frequencies))\n", "\n", " \n", "\n", "\n", "analyse_graphs(graphs)" ] } ], "metadata": { "kernelspec": { "display_name": "graphml_proj", "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.9.16" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }