Create a noisy circular graph
(n_clean=15, n_noise=5, random_seed=0)
| 55 | |
| 56 | |
| 57 | def build_noisy_circular_graph(n_clean=15, n_noise=5, random_seed=0): |
| 58 | """Create a noisy circular graph""" |
| 59 | # create clean circle |
| 60 | np.random.seed(random_seed) |
| 61 | g = nx.Graph() |
| 62 | g.add_nodes_from(np.arange(n_clean + n_noise)) |
| 63 | for i in range(n_clean): |
| 64 | g.add_node(i, weight=math.sin(2 * i * math.pi / n_clean)) |
| 65 | if i == (n_clean - 1): |
| 66 | g.add_edge(i, 0) |
| 67 | else: |
| 68 | g.add_edge(i, i + 1) |
| 69 | # add nodes out of the circle as structure noise |
| 70 | if n_noise > 0: |
| 71 | noisy_nodes = np.random.choice(np.arange(n_clean), n_noise) |
| 72 | for i, j in enumerate(noisy_nodes): |
| 73 | g.add_node(i + n_clean, weight=math.sin(2 * j * math.pi / n_clean)) |
| 74 | g.add_edge(i + n_clean, j) |
| 75 | g.add_edge(i + n_clean, (j + 1) % n_clean) |
| 76 | return g |
| 77 | |
| 78 | |
| 79 | def graph_colors(nx_graph, vmin=0, vmax=7): |
no test coverage detected