Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import timeit\n",
- "import time\n",
- "import cProfile\n",
- "\n",
- "import networkx as nx\n",
- "import numpy as np\n",
- "import pandas as pd\n",
- "\n",
- "from functools import wraps"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def nodes_by_attribute1(G,attr_val, attr='type'):\n",
- " \n",
- " # v1\n",
- " # get all nodes that have the attribute 'type' set\n",
- " nodes_attributes = nx.get_node_attributes(G, attr)\n",
- "\n",
- " # extract nodes where 'type' == attr_val\n",
- " nodes = [k for k, v in nodes_attributes.items() if v == attr_val]\n",
- " \n",
- " return nodes\n",
- "\n",
- "def nodes_by_attribute2(G,attr_val, attr='type'):\n",
- " # v2\n",
- " nodes = filter(lambda x: x[1][attr] == attr_val, G.node.items())\n",
- " nodes = list(map(lambda x: x[0],nodes))\n",
- " return nodes\n",
- "\n",
- "def nodes_by_attribute3(G,attr_val, attr='type'):\n",
- " # v3\n",
- " l =filter(lambda x: x[1] == attr_val, G.nodes(data=attr))\n",
- " nodes,l = zip(*l)\n",
- " return nodes\n",
- "\n",
- "def nodes_by_attribute4(G,attr_val, attr='type'):\n",
- " # v4\n",
- " temp_nodes = getattr(G,'_node')\n",
- " nodes = list(filter(None,map(lambda x: x if temp_nodes[x][attr] == attr_val else None,\n",
- " temp_nodes.keys())))\n",
- " return nodes\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "G=nx.Graph()\n",
- "G.add_nodes_from(range(1,10000,2),type='generator')\n",
- "G.add_nodes_from(range(2,10000,2),type='load')\n",
- "#output = nodes_by_attribute(G,'generator')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- " 20013 function calls in 0.019 seconds\n",
- "\n",
- " Ordered by: standard name\n",
- "\n",
- " ncalls tottime percall cumtime percall filename:lineno(function)\n",
- " 1 0.000 0.000 0.019 0.019 <ipython-input-2-5fee856f528d>:1(nodes_by_attribute1)\n",
- " 1 0.001 0.001 0.001 0.001 <ipython-input-2-5fee856f528d>:8(<listcomp>)\n",
- " 1 0.000 0.000 0.019 0.019 <string>:1(<module>)\n",
- " 1 0.000 0.000 0.000 0.000 _collections_abc.py:676(items)\n",
- " 1 0.000 0.000 0.000 0.000 _collections_abc.py:698(__init__)\n",
- " 10000 0.008 0.000 0.011 0.000 _collections_abc.py:742(__iter__)\n",
- " 1 0.000 0.000 0.018 0.018 function.py:657(get_node_attributes)\n",
- " 1 0.007 0.007 0.018 0.018 function.py:679(<dictcomp>)\n",
- " 1 0.000 0.000 0.000 0.000 graph.py:626(nodes)\n",
- " 1 0.000 0.000 0.000 0.000 reportviews.py:167(__init__)\n",
- " 1 0.000 0.000 0.000 0.000 reportviews.py:174(__iter__)\n",
- " 9999 0.003 0.000 0.003 0.000 reportviews.py:177(__getitem__)\n",
- " 1 0.000 0.000 0.019 0.019 {built-in method builtins.exec}\n",
- " 1 0.000 0.000 0.000 0.000 {built-in method builtins.iter}\n",
- " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n",
- " 1 0.000 0.000 0.000 0.000 {method 'items' of 'dict' objects}\n",
- "\n",
- "\n"
- ]
- }
- ],
- "source": [
- "cProfile.run(\"output = nodes_by_attribute1(G,'generator')\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- " 35008 function calls in 0.020 seconds\n",
- "\n",
- " Ordered by: standard name\n",
- "\n",
- " ncalls tottime percall cumtime percall filename:lineno(function)\n",
- " 1 0.006 0.006 0.019 0.019 <ipython-input-2-5fee856f528d>:12(nodes_by_attribute2)\n",
- " 9999 0.004 0.000 0.004 0.000 <ipython-input-2-5fee856f528d>:14(<lambda>)\n",
- " 5000 0.001 0.000 0.001 0.000 <ipython-input-2-5fee856f528d>:15(<lambda>)\n",
- " 1 0.000 0.000 0.019 0.019 <string>:1(<module>)\n",
- " 1 0.000 0.000 0.000 0.000 _collections_abc.py:676(items)\n",
- " 1 0.000 0.000 0.000 0.000 _collections_abc.py:698(__init__)\n",
- " 10000 0.007 0.000 0.010 0.000 _collections_abc.py:742(__iter__)\n",
- " 1 0.000 0.000 0.000 0.000 graph.py:626(nodes)\n",
- " 1 0.000 0.000 0.000 0.000 reportviews.py:167(__init__)\n",
- " 1 0.000 0.000 0.000 0.000 reportviews.py:174(__iter__)\n",
- " 9999 0.003 0.000 0.003 0.000 reportviews.py:177(__getitem__)\n",
- " 1 0.000 0.000 0.020 0.020 {built-in method builtins.exec}\n",
- " 1 0.000 0.000 0.000 0.000 {built-in method builtins.iter}\n",
- " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n",
- "\n",
- "\n"
- ]
- }
- ],
- "source": [
- "cProfile.run(\"output = nodes_by_attribute2(G,'generator')\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- " 20009 function calls in 0.023 seconds\n",
- "\n",
- " Ordered by: standard name\n",
- "\n",
- " ncalls tottime percall cumtime percall filename:lineno(function)\n",
- " 1 0.010 0.010 0.023 0.023 <ipython-input-2-5fee856f528d>:18(nodes_by_attribute3)\n",
- " 9999 0.004 0.000 0.004 0.000 <ipython-input-2-5fee856f528d>:20(<lambda>)\n",
- " 1 0.000 0.000 0.023 0.023 <string>:1(<module>)\n",
- " 1 0.000 0.000 0.000 0.000 graph.py:626(nodes)\n",
- " 1 0.000 0.000 0.000 0.000 reportviews.py:167(__init__)\n",
- " 1 0.000 0.000 0.000 0.000 reportviews.py:189(__call__)\n",
- " 1 0.000 0.000 0.000 0.000 reportviews.py:235(__init__)\n",
- " 1 0.000 0.000 0.000 0.000 reportviews.py:253(__iter__)\n",
- " 10000 0.009 0.000 0.009 0.000 reportviews.py:259(<genexpr>)\n",
- " 1 0.000 0.000 0.023 0.023 {built-in method builtins.exec}\n",
- " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n",
- " 1 0.000 0.000 0.000 0.000 {method 'items' of 'dict' objects}\n",
- "\n",
- "\n"
- ]
- }
- ],
- "source": [
- "cProfile.run(\"output = nodes_by_attribute3(G,'generator')\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- " 10005 function calls in 0.013 seconds\n",
- "\n",
- " Ordered by: standard name\n",
- "\n",
- " ncalls tottime percall cumtime percall filename:lineno(function)\n",
- " 1 0.006 0.006 0.013 0.013 <ipython-input-2-5fee856f528d>:24(nodes_by_attribute4)\n",
- " 9999 0.007 0.000 0.007 0.000 <ipython-input-2-5fee856f528d>:27(<lambda>)\n",
- " 1 0.000 0.000 0.013 0.013 <string>:1(<module>)\n",
- " 1 0.000 0.000 0.013 0.013 {built-in method builtins.exec}\n",
- " 1 0.000 0.000 0.000 0.000 {built-in method builtins.getattr}\n",
- " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n",
- " 1 0.000 0.000 0.000 0.000 {method 'keys' of 'dict' objects}\n",
- "\n",
- "\n"
- ]
- }
- ],
- "source": [
- "cProfile.run(\"output = nodes_by_attribute4(G,'generator')\")"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "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.6.3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment