tp assemblage cours amine ghozlane

test_decision.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. """Tests decision"""
  2. import pytest
  3. import os
  4. import networkx as nx
  5. import statistics
  6. from .context import debruijn
  7. #from .context import debruijn_comp
  8. from debruijn import std
  9. from debruijn import path_average_weight
  10. from debruijn import remove_paths
  11. from debruijn import select_best_path
  12. from debruijn import solve_bubble
  13. from debruijn import simplify_bubbles
  14. from debruijn import solve_entry_tips
  15. from debruijn import solve_out_tips
  16. def test_std():
  17. assert round(std([9, 5, 15, 20]), 1) == 6.6
  18. def test_path_weight():
  19. graph = nx.DiGraph()
  20. graph.add_weighted_edges_from([(1, 2, 5), (3, 2, 10), (2, 4, 10), (4, 5, 3),
  21. (5, 6, 10), (5, 7, 10)])
  22. assert path_average_weight(graph, [1, 2, 4, 5] ) == 6.0
  23. def test_remove_paths():
  24. graph_1 = nx.DiGraph()
  25. graph_2 = nx.DiGraph()
  26. graph_3 = nx.DiGraph()
  27. graph_4 = nx.DiGraph()
  28. graph_1.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7)])
  29. graph_2.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7)])
  30. graph_3.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7)])
  31. graph_4.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7)])
  32. graph_1 = remove_paths(graph_1, [(1,2)], True, False)
  33. graph_2 = remove_paths(graph_2, [(5,7)], False, True)
  34. graph_3 = remove_paths(graph_3, [(2,4,5)], False, False)
  35. graph_4 = remove_paths(graph_4, [(2,4,5)], True, True)
  36. assert (1,2) not in graph_1.edges()
  37. assert (3,2) in graph_1.edges()
  38. assert (5,7) not in graph_2.edges()
  39. assert (5,6) in graph_2.edges()
  40. assert 4 not in graph_3.nodes()
  41. assert (2,4) not in graph_4.edges()
  42. assert (4, 5) not in graph_4.edges()
  43. assert 2 not in graph_4.nodes()
  44. assert 4 not in graph_4.nodes()
  45. assert 5 not in graph_4.nodes()
  46. def test_select_best_path():
  47. graph_1 = nx.DiGraph()
  48. graph_1.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7)])
  49. graph_1 = select_best_path(graph_1, [[1,2], [3,2]], [1, 1], [5, 10], delete_entry_node=True)
  50. assert (1,2) not in graph_1.edges()
  51. assert (3,2) in graph_1.edges()
  52. assert 1 not in graph_1.nodes()
  53. graph_2 = nx.DiGraph()
  54. graph_2.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7) , (7, 8)])
  55. graph_2 = select_best_path(graph_1, [[5, 6], [5, 7, 8]], [1, 2], [13, 10], delete_sink_node=True)
  56. assert (5,7) not in graph_2.edges()
  57. assert (7,8) not in graph_2.edges()
  58. assert (5,6) in graph_2.edges()
  59. assert 7 not in graph_2.nodes()
  60. assert 8 not in graph_2.nodes()
  61. #Select heavier
  62. graph_3 = nx.DiGraph()
  63. graph_3.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (2, 8), (8, 9),
  64. (9, 5), (5, 6), (5, 7)])
  65. graph_3 = select_best_path(graph_3, [[2, 4, 5], [2, 8, 9, 5]],
  66. [1, 4], [13, 10])
  67. assert (2,8) not in graph_3.edges()
  68. assert (8,9) not in graph_3.edges()
  69. assert (9,5) not in graph_3.edges()
  70. assert (2,4) in graph_3.edges()
  71. assert (4,5) in graph_3.edges()
  72. assert 8 not in graph_3.nodes()
  73. assert 9 not in graph_3.nodes()
  74. assert 2 in graph_3.nodes()
  75. assert 5 in graph_3.nodes()
  76. # Select longest
  77. graph_4 = nx.DiGraph()
  78. graph_4.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (2, 8), (8, 9),
  79. (9, 5), (5, 6), (5, 7)])
  80. graph_4 = select_best_path(graph_4, [[2, 4, 5], [2, 8, 9, 5]],
  81. [1, 4], [10, 10])
  82. assert (2,4) not in graph_4.edges()
  83. assert (4,5) not in graph_4.edges()
  84. assert (2,8) in graph_4.edges()
  85. assert (8,9) in graph_4.edges()
  86. assert (9,5) in graph_4.edges()
  87. # Select random
  88. graph_5 = nx.DiGraph()
  89. graph_5.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (2, 8), (8, 9),
  90. (9, 5), (5, 6), (5, 7)])
  91. graph_5 = select_best_path(graph_5, [[2, 4, 5], [2, 8, 9, 5]],
  92. [1, 4], [10, 10])
  93. def test_solve_bubble():
  94. graph_1 = nx.DiGraph()
  95. graph_1.add_weighted_edges_from([(1, 2, 10), (3, 2, 10), (2, 4, 15),
  96. (4, 5, 15), (2, 10,10), (10, 5,10),
  97. (2, 8, 3), (8, 9, 3), (9, 5, 3),
  98. (5, 6, 10), (5, 7, 10)])
  99. graph_1 = solve_bubble(graph_1, 2, 5)
  100. assert (2,8) not in graph_1.edges()
  101. assert (8,9) not in graph_1.edges()
  102. assert (9,5) not in graph_1.edges()
  103. assert (2,10) not in graph_1.edges()
  104. assert (10, 5) not in graph_1.edges()
  105. assert (2,4) in graph_1.edges()
  106. assert (4,5) in graph_1.edges()
  107. assert 8 not in graph_1.nodes()
  108. assert 9 not in graph_1.nodes()
  109. assert 10 not in graph_1.nodes()
  110. assert 2 in graph_1.nodes()
  111. assert 5 in graph_1.nodes()
  112. graph_2 = nx.DiGraph()
  113. graph_2.add_weighted_edges_from([(1, 2, 10), (3, 2, 10), (2, 4, 10),
  114. (4, 5, 10), (2, 10,10), (10, 5,10),
  115. (2, 8, 10), (8, 9, 10), (9, 5, 10),
  116. (5, 6, 10), (5, 7, 10)])
  117. graph_2 = solve_bubble(graph_2, 2, 5)
  118. assert (2,4) not in graph_2.edges()
  119. assert (4,5) not in graph_2.edges()
  120. assert (2,10) not in graph_1.edges()
  121. assert (10, 5) not in graph_1.edges()
  122. assert (2,8) in graph_2.edges()
  123. assert (8,9) in graph_2.edges()
  124. assert (9,5) in graph_2.edges()
  125. def test_simplify_bubbles():
  126. graph_1 = nx.DiGraph()
  127. graph_1.add_weighted_edges_from([(3, 2, 10), (2, 4, 15), (4, 5, 15),
  128. (2, 10,10), (10, 5,10), (2, 8, 3),
  129. (8, 9, 3), (9, 5, 3), (5, 6, 10),
  130. (5, 7, 10)])
  131. graph_1 = simplify_bubbles(graph_1)
  132. assert (2,8) not in graph_1.edges()
  133. assert (8,9) not in graph_1.edges()
  134. assert (9,5) not in graph_1.edges()
  135. assert (2,10) not in graph_1.edges()
  136. assert (10, 5) not in graph_1.edges()
  137. def test_solve_entry_tips():
  138. graph_1 = nx.DiGraph()
  139. graph_1.add_weighted_edges_from([(1, 2, 10), (3, 2, 2), (2, 4, 15), (4, 5, 15)])
  140. graph_1 = solve_entry_tips(graph_1, [1, 3])
  141. assert (3, 2) not in graph_1.edges()
  142. assert (1, 2) in graph_1.edges()
  143. graph_2 = nx.DiGraph()
  144. graph_2.add_weighted_edges_from([(1, 2, 2), (6, 3, 2), (3, 2, 2),
  145. (2, 4, 15), (4, 5, 15)])
  146. graph_2 = solve_entry_tips(graph_2, [1, 6])
  147. assert (1, 2) not in graph_2.edges()
  148. assert (6, 3) in graph_2.edges()
  149. assert (3, 2) in graph_2.edges()
  150. def test_solve_out_tips():
  151. graph_1 = nx.DiGraph()
  152. graph_1.add_weighted_edges_from([(1, 2, 15), (2, 3, 15), (4, 5, 15), (4, 6, 2)])
  153. graph_1 = solve_out_tips(graph_1, [5, 6])
  154. assert (4, 6) not in graph_1.edges()
  155. assert (4, 5) in graph_1.edges()
  156. graph_2 = nx.DiGraph()
  157. graph_2.add_weighted_edges_from([(1, 2, 15), (2, 3, 15), (4, 5, 2), (4, 6, 2) , (6, 7, 2)])
  158. graph_2 = solve_out_tips(graph_2, [5, 7])
  159. assert (4, 5) not in graph_2.edges()
  160. assert (6, 7) in graph_2.edges()