/lib/networkx-1.6/build/lib.linux-i686-2.7/networkx/algorithms/centrality/tests/test_current_flow_betweenness_centrality_subset.py

https://bitbucket.org/shenli/socialgraph
Python | 181 lines | 140 code | 26 blank | 15 comment | 19 complexity | bf641fe868135290f30c877f3ed11507 MD5 | raw file
  1. #!/usr/bin/env python
  2. from nose.tools import *
  3. from nose import SkipTest
  4. import networkx
  5. from nose.plugins.attrib import attr
  6. from networkx import edge_current_flow_betweenness_centrality \
  7. as edge_current_flow
  8. from networkx import edge_current_flow_betweenness_centrality_subset \
  9. as edge_current_flow_subset
  10. class TestFlowBetweennessCentrality(object):
  11. numpy=1 # nosetests attribute, use nosetests -a 'not numpy' to skip test
  12. @classmethod
  13. def setupClass(cls):
  14. global np
  15. try:
  16. import numpy as np
  17. import scipy
  18. except ImportError:
  19. raise SkipTest('NumPy not available.')
  20. def test_K4_normalized(self):
  21. """Betweenness centrality: K4"""
  22. G=networkx.complete_graph(4)
  23. b=networkx.current_flow_betweenness_centrality_subset(G,
  24. G.nodes(),
  25. G.nodes(),
  26. normalized=True)
  27. b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True)
  28. for n in sorted(G):
  29. assert_almost_equal(b[n],b_answer[n])
  30. def test_K4(self):
  31. """Betweenness centrality: K4"""
  32. G=networkx.complete_graph(4)
  33. b=networkx.current_flow_betweenness_centrality_subset(G,
  34. G.nodes(),
  35. G.nodes(),
  36. normalized=True)
  37. b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True)
  38. for n in sorted(G):
  39. assert_almost_equal(b[n],b_answer[n])
  40. # test weighted network
  41. G.add_edge(0,1,{'weight':0.5,'other':0.3})
  42. b=networkx.current_flow_betweenness_centrality_subset(G,
  43. G.nodes(),
  44. G.nodes(),
  45. normalized=True,
  46. weight=None)
  47. for n in sorted(G):
  48. assert_almost_equal(b[n],b_answer[n])
  49. b=networkx.current_flow_betweenness_centrality_subset(G,
  50. G.nodes(),
  51. G.nodes(),
  52. normalized=True)
  53. b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True)
  54. for n in sorted(G):
  55. assert_almost_equal(b[n],b_answer[n])
  56. b=networkx.current_flow_betweenness_centrality_subset(G,
  57. G.nodes(),
  58. G.nodes(),
  59. normalized=True,
  60. weight='other')
  61. b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True,weight='other')
  62. for n in sorted(G):
  63. assert_almost_equal(b[n],b_answer[n])
  64. def test_P4_normalized(self):
  65. """Betweenness centrality: P4 normalized"""
  66. G=networkx.path_graph(4)
  67. b=networkx.current_flow_betweenness_centrality_subset(G,
  68. G.nodes(),
  69. G.nodes(),
  70. normalized=True)
  71. b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True)
  72. for n in sorted(G):
  73. assert_almost_equal(b[n],b_answer[n])
  74. def test_P4(self):
  75. """Betweenness centrality: P4"""
  76. G=networkx.path_graph(4)
  77. b=networkx.current_flow_betweenness_centrality_subset(G,
  78. G.nodes(),
  79. G.nodes(),
  80. normalized=True)
  81. b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True)
  82. for n in sorted(G):
  83. assert_almost_equal(b[n],b_answer[n])
  84. def test_star(self):
  85. """Betweenness centrality: star """
  86. G=networkx.Graph()
  87. G.add_star(['a','b','c','d'])
  88. b=networkx.current_flow_betweenness_centrality_subset(G,
  89. G.nodes(),
  90. G.nodes(),
  91. normalized=True)
  92. b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True)
  93. for n in sorted(G):
  94. assert_almost_equal(b[n],b_answer[n])
  95. # class TestWeightedFlowBetweennessCentrality():
  96. # pass
  97. class TestEdgeFlowBetweennessCentrality(object):
  98. numpy=1 # nosetests attribute, use nosetests -a 'not numpy' to skip test
  99. @classmethod
  100. def setupClass(cls):
  101. global np
  102. try:
  103. import numpy as np
  104. import scipy
  105. except ImportError:
  106. raise SkipTest('NumPy not available.')
  107. def test_K4_normalized(self):
  108. """Betweenness centrality: K4"""
  109. G=networkx.complete_graph(4)
  110. b=edge_current_flow_subset(G,G.nodes(),G.nodes(),normalized=True)
  111. b_answer=edge_current_flow(G,normalized=True)
  112. for (s,t),v1 in b_answer.items():
  113. v2=b.get((s,t),b.get((t,s)))
  114. assert_almost_equal(v1,v2)
  115. def test_K4(self):
  116. """Betweenness centrality: K4"""
  117. G=networkx.complete_graph(4)
  118. b=edge_current_flow_subset(G,G.nodes(),G.nodes(),normalized=False)
  119. b_answer=edge_current_flow(G,normalized=False)
  120. for (s,t),v1 in b_answer.items():
  121. v2=b.get((s,t),b.get((t,s)))
  122. assert_almost_equal(v1,v2)
  123. # test weighted network
  124. G.add_edge(0,1,{'weight':0.5,'other':0.3})
  125. b=edge_current_flow_subset(G,G.nodes(),G.nodes(),normalized=False,weight=None)
  126. # weight is None => same as unweighted network
  127. for (s,t),v1 in b_answer.items():
  128. v2=b.get((s,t),b.get((t,s)))
  129. assert_almost_equal(v1,v2)
  130. b=edge_current_flow_subset(G,G.nodes(),G.nodes(),normalized=False)
  131. b_answer=edge_current_flow(G,normalized=False)
  132. for (s,t),v1 in b_answer.items():
  133. v2=b.get((s,t),b.get((t,s)))
  134. assert_almost_equal(v1,v2)
  135. b=edge_current_flow_subset(G,G.nodes(),G.nodes(),normalized=False,weight='other')
  136. b_answer=edge_current_flow(G,normalized=False,weight='other')
  137. for (s,t),v1 in b_answer.items():
  138. v2=b.get((s,t),b.get((t,s)))
  139. assert_almost_equal(v1,v2)
  140. def test_C4(self):
  141. """Edge betweenness centrality: C4"""
  142. G=networkx.cycle_graph(4)
  143. b=edge_current_flow_subset(G,G.nodes(),G.nodes(),normalized=True)
  144. b_answer=edge_current_flow(G,normalized=True)
  145. for (s,t),v1 in b_answer.items():
  146. v2=b.get((s,t),b.get((t,s)))
  147. assert_almost_equal(v1,v2)
  148. def test_P4(self):
  149. """Edge betweenness centrality: P4"""
  150. G=networkx.path_graph(4)
  151. b=edge_current_flow_subset(G,G.nodes(),G.nodes(),normalized=True)
  152. b_answer=edge_current_flow(G,normalized=True)
  153. for (s,t),v1 in b_answer.items():
  154. v2=b.get((s,t),b.get((t,s)))
  155. assert_almost_equal(v1,v2)