|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 | # |
4 | | -# Copyright (c) 2021 The WfCommons Team. |
| 4 | +# Copyright (c) 2021-2024 The WfCommons Team. |
5 | 5 | # |
6 | 6 | # This program is free software: you can redistribute it and/or modify |
7 | 7 | # it under the terms of the GNU General Public License as published by |
@@ -92,25 +92,38 @@ def duplicate(path: pathlib.Path, |
92 | 92 | f"Cannot create synthentic graph with {num_nodes} nodes from base graph with {graph.order()} nodes") |
93 | 93 |
|
94 | 94 | all_microstructures = json.loads(base_path.joinpath("microstructures.json").read_text()) |
95 | | - microstructures, freqs = map(list, zip(*[(ms, ms["frequency"]) for ms_hash, ms in all_microstructures.items()])) |
96 | | - |
97 | | - p: List[float] = (np.array(freqs) / np.sum(freqs)).tolist() |
98 | | - while graph.order() < num_nodes and microstructures: |
99 | | - i = random.choice(range(len(microstructures))) |
100 | | - ms = microstructures[i] |
101 | | - while ms["nodes"]: |
102 | | - j = random.choice(range(len(ms["nodes"]))) |
103 | | - structure = ms["nodes"][j] |
104 | | - if graph.order() + len(structure) > num_nodes: |
105 | | - del ms["nodes"][j] |
106 | | - else: |
| 95 | + try: |
| 96 | + microstructures, freqs = map(list, zip(*[(ms, ms["frequency"]) for ms_hash, ms in all_microstructures.items()])) |
| 97 | + |
| 98 | + p: List[float] = (np.array(freqs) / np.sum(freqs)).tolist() |
| 99 | + while graph.order() < num_nodes and microstructures: |
| 100 | + i = random.choice(range(len(microstructures))) |
| 101 | + ms = microstructures[i] |
| 102 | + while ms["nodes"]: |
| 103 | + j = random.choice(range(len(ms["nodes"]))) |
| 104 | + structure = ms["nodes"][j] |
| 105 | + if graph.order() + len(structure) > num_nodes: |
| 106 | + del ms["nodes"][j] |
| 107 | + else: |
| 108 | + break |
| 109 | + |
| 110 | + if not ms["nodes"]: # delete microstructure |
| 111 | + del microstructures[i] |
| 112 | + del p[i] |
| 113 | + continue |
| 114 | + |
| 115 | + duplicate_nodes(graph, structure) |
| 116 | + |
| 117 | + except ValueError as e: |
| 118 | + nodes = graph.copy().nodes |
| 119 | + while graph.order() < num_nodes: |
| 120 | + structure = set[str]() |
| 121 | + if graph.order() + len(nodes) - 2 > num_nodes: |
107 | 122 | break |
| 123 | + for node in nodes: |
| 124 | + if str(node) not in ["SRC", "DST"]: |
| 125 | + structure.add(node) |
108 | 126 |
|
109 | | - if not ms["nodes"]: # delete microstructure |
110 | | - del microstructures[i] |
111 | | - del p[i] |
112 | | - continue |
113 | | - |
114 | | - duplicate_nodes(graph, structure) |
| 127 | + duplicate_nodes(graph, structure) |
115 | 128 |
|
116 | 129 | return graph |
0 commit comments