Guest User

Untitled

a guest
Dec 17th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {
  7. "collapsed": true
  8. },
  9. "outputs": [],
  10. "source": [
  11. "import timeit\n",
  12. "import time\n",
  13. "import cProfile\n",
  14. "\n",
  15. "import networkx as nx\n",
  16. "import numpy as np\n",
  17. "import pandas as pd\n",
  18. "\n",
  19. "from functools import wraps"
  20. ]
  21. },
  22. {
  23. "cell_type": "code",
  24. "execution_count": 2,
  25. "metadata": {
  26. "collapsed": true
  27. },
  28. "outputs": [],
  29. "source": [
  30. "def nodes_by_attribute1(G,attr_val, attr='type'):\n",
  31. " \n",
  32. " # v1\n",
  33. " # get all nodes that have the attribute 'type' set\n",
  34. " nodes_attributes = nx.get_node_attributes(G, attr)\n",
  35. "\n",
  36. " # extract nodes where 'type' == attr_val\n",
  37. " nodes = [k for k, v in nodes_attributes.items() if v == attr_val]\n",
  38. " \n",
  39. " return nodes\n",
  40. "\n",
  41. "def nodes_by_attribute2(G,attr_val, attr='type'):\n",
  42. " # v2\n",
  43. " nodes = filter(lambda x: x[1][attr] == attr_val, G.node.items())\n",
  44. " nodes = list(map(lambda x: x[0],nodes))\n",
  45. " return nodes\n",
  46. "\n",
  47. "def nodes_by_attribute3(G,attr_val, attr='type'):\n",
  48. " # v3\n",
  49. " l =filter(lambda x: x[1] == attr_val, G.nodes(data=attr))\n",
  50. " nodes,l = zip(*l)\n",
  51. " return nodes\n",
  52. "\n",
  53. "def nodes_by_attribute4(G,attr_val, attr='type'):\n",
  54. " # v4\n",
  55. " temp_nodes = getattr(G,'_node')\n",
  56. " nodes = list(filter(None,map(lambda x: x if temp_nodes[x][attr] == attr_val else None,\n",
  57. " temp_nodes.keys())))\n",
  58. " return nodes\n"
  59. ]
  60. },
  61. {
  62. "cell_type": "code",
  63. "execution_count": 3,
  64. "metadata": {
  65. "collapsed": true
  66. },
  67. "outputs": [],
  68. "source": [
  69. "G=nx.Graph()\n",
  70. "G.add_nodes_from(range(1,10000,2),type='generator')\n",
  71. "G.add_nodes_from(range(2,10000,2),type='load')\n",
  72. "#output = nodes_by_attribute(G,'generator')"
  73. ]
  74. },
  75. {
  76. "cell_type": "code",
  77. "execution_count": 4,
  78. "metadata": {},
  79. "outputs": [
  80. {
  81. "name": "stdout",
  82. "output_type": "stream",
  83. "text": [
  84. " 20013 function calls in 0.019 seconds\n",
  85. "\n",
  86. " Ordered by: standard name\n",
  87. "\n",
  88. " ncalls tottime percall cumtime percall filename:lineno(function)\n",
  89. " 1 0.000 0.000 0.019 0.019 <ipython-input-2-5fee856f528d>:1(nodes_by_attribute1)\n",
  90. " 1 0.001 0.001 0.001 0.001 <ipython-input-2-5fee856f528d>:8(<listcomp>)\n",
  91. " 1 0.000 0.000 0.019 0.019 <string>:1(<module>)\n",
  92. " 1 0.000 0.000 0.000 0.000 _collections_abc.py:676(items)\n",
  93. " 1 0.000 0.000 0.000 0.000 _collections_abc.py:698(__init__)\n",
  94. " 10000 0.008 0.000 0.011 0.000 _collections_abc.py:742(__iter__)\n",
  95. " 1 0.000 0.000 0.018 0.018 function.py:657(get_node_attributes)\n",
  96. " 1 0.007 0.007 0.018 0.018 function.py:679(<dictcomp>)\n",
  97. " 1 0.000 0.000 0.000 0.000 graph.py:626(nodes)\n",
  98. " 1 0.000 0.000 0.000 0.000 reportviews.py:167(__init__)\n",
  99. " 1 0.000 0.000 0.000 0.000 reportviews.py:174(__iter__)\n",
  100. " 9999 0.003 0.000 0.003 0.000 reportviews.py:177(__getitem__)\n",
  101. " 1 0.000 0.000 0.019 0.019 {built-in method builtins.exec}\n",
  102. " 1 0.000 0.000 0.000 0.000 {built-in method builtins.iter}\n",
  103. " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n",
  104. " 1 0.000 0.000 0.000 0.000 {method 'items' of 'dict' objects}\n",
  105. "\n",
  106. "\n"
  107. ]
  108. }
  109. ],
  110. "source": [
  111. "cProfile.run(\"output = nodes_by_attribute1(G,'generator')\")"
  112. ]
  113. },
  114. {
  115. "cell_type": "code",
  116. "execution_count": 5,
  117. "metadata": {},
  118. "outputs": [
  119. {
  120. "name": "stdout",
  121. "output_type": "stream",
  122. "text": [
  123. " 35008 function calls in 0.020 seconds\n",
  124. "\n",
  125. " Ordered by: standard name\n",
  126. "\n",
  127. " ncalls tottime percall cumtime percall filename:lineno(function)\n",
  128. " 1 0.006 0.006 0.019 0.019 <ipython-input-2-5fee856f528d>:12(nodes_by_attribute2)\n",
  129. " 9999 0.004 0.000 0.004 0.000 <ipython-input-2-5fee856f528d>:14(<lambda>)\n",
  130. " 5000 0.001 0.000 0.001 0.000 <ipython-input-2-5fee856f528d>:15(<lambda>)\n",
  131. " 1 0.000 0.000 0.019 0.019 <string>:1(<module>)\n",
  132. " 1 0.000 0.000 0.000 0.000 _collections_abc.py:676(items)\n",
  133. " 1 0.000 0.000 0.000 0.000 _collections_abc.py:698(__init__)\n",
  134. " 10000 0.007 0.000 0.010 0.000 _collections_abc.py:742(__iter__)\n",
  135. " 1 0.000 0.000 0.000 0.000 graph.py:626(nodes)\n",
  136. " 1 0.000 0.000 0.000 0.000 reportviews.py:167(__init__)\n",
  137. " 1 0.000 0.000 0.000 0.000 reportviews.py:174(__iter__)\n",
  138. " 9999 0.003 0.000 0.003 0.000 reportviews.py:177(__getitem__)\n",
  139. " 1 0.000 0.000 0.020 0.020 {built-in method builtins.exec}\n",
  140. " 1 0.000 0.000 0.000 0.000 {built-in method builtins.iter}\n",
  141. " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n",
  142. "\n",
  143. "\n"
  144. ]
  145. }
  146. ],
  147. "source": [
  148. "cProfile.run(\"output = nodes_by_attribute2(G,'generator')\")"
  149. ]
  150. },
  151. {
  152. "cell_type": "code",
  153. "execution_count": 6,
  154. "metadata": {},
  155. "outputs": [
  156. {
  157. "name": "stdout",
  158. "output_type": "stream",
  159. "text": [
  160. " 20009 function calls in 0.023 seconds\n",
  161. "\n",
  162. " Ordered by: standard name\n",
  163. "\n",
  164. " ncalls tottime percall cumtime percall filename:lineno(function)\n",
  165. " 1 0.010 0.010 0.023 0.023 <ipython-input-2-5fee856f528d>:18(nodes_by_attribute3)\n",
  166. " 9999 0.004 0.000 0.004 0.000 <ipython-input-2-5fee856f528d>:20(<lambda>)\n",
  167. " 1 0.000 0.000 0.023 0.023 <string>:1(<module>)\n",
  168. " 1 0.000 0.000 0.000 0.000 graph.py:626(nodes)\n",
  169. " 1 0.000 0.000 0.000 0.000 reportviews.py:167(__init__)\n",
  170. " 1 0.000 0.000 0.000 0.000 reportviews.py:189(__call__)\n",
  171. " 1 0.000 0.000 0.000 0.000 reportviews.py:235(__init__)\n",
  172. " 1 0.000 0.000 0.000 0.000 reportviews.py:253(__iter__)\n",
  173. " 10000 0.009 0.000 0.009 0.000 reportviews.py:259(<genexpr>)\n",
  174. " 1 0.000 0.000 0.023 0.023 {built-in method builtins.exec}\n",
  175. " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n",
  176. " 1 0.000 0.000 0.000 0.000 {method 'items' of 'dict' objects}\n",
  177. "\n",
  178. "\n"
  179. ]
  180. }
  181. ],
  182. "source": [
  183. "cProfile.run(\"output = nodes_by_attribute3(G,'generator')\")"
  184. ]
  185. },
  186. {
  187. "cell_type": "code",
  188. "execution_count": 7,
  189. "metadata": {},
  190. "outputs": [
  191. {
  192. "name": "stdout",
  193. "output_type": "stream",
  194. "text": [
  195. " 10005 function calls in 0.013 seconds\n",
  196. "\n",
  197. " Ordered by: standard name\n",
  198. "\n",
  199. " ncalls tottime percall cumtime percall filename:lineno(function)\n",
  200. " 1 0.006 0.006 0.013 0.013 <ipython-input-2-5fee856f528d>:24(nodes_by_attribute4)\n",
  201. " 9999 0.007 0.000 0.007 0.000 <ipython-input-2-5fee856f528d>:27(<lambda>)\n",
  202. " 1 0.000 0.000 0.013 0.013 <string>:1(<module>)\n",
  203. " 1 0.000 0.000 0.013 0.013 {built-in method builtins.exec}\n",
  204. " 1 0.000 0.000 0.000 0.000 {built-in method builtins.getattr}\n",
  205. " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n",
  206. " 1 0.000 0.000 0.000 0.000 {method 'keys' of 'dict' objects}\n",
  207. "\n",
  208. "\n"
  209. ]
  210. }
  211. ],
  212. "source": [
  213. "cProfile.run(\"output = nodes_by_attribute4(G,'generator')\")"
  214. ]
  215. }
  216. ],
  217. "metadata": {
  218. "kernelspec": {
  219. "display_name": "Python 3",
  220. "language": "python",
  221. "name": "python3"
  222. },
  223. "language_info": {
  224. "codemirror_mode": {
  225. "name": "ipython",
  226. "version": 3
  227. },
  228. "file_extension": ".py",
  229. "mimetype": "text/x-python",
  230. "name": "python",
  231. "nbconvert_exporter": "python",
  232. "pygments_lexer": "ipython3",
  233. "version": "3.6.3"
  234. }
  235. },
  236. "nbformat": 4,
  237. "nbformat_minor": 2
  238. }
Add Comment
Please, Sign In to add comment