| Conditions | 6 |
| Total Lines | 53 |
| Code Lines | 22 |
| Lines | 53 |
| Ratio | 100 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | ''' |
||
| 40 | View Code Duplication | def shuffle_graph(data_graph: 'NetworkXGraphObject', shuffle_number: int, seed: int = None) -> 'NetworkXGraphObject': |
|
| 41 | ''' |
||
| 42 | Returns a new graph, shuffling the order of the nodes in the input data_graph, but the relationship between the nodes remains the same. The data_graph doesn't change. |
||
| 43 | |||
| 44 | Parameters |
||
| 45 | ---------- |
||
| 46 | data_graph : NetworkXGraphObject |
||
| 47 | A NetworkX graph object. |
||
| 48 | |||
| 49 | shuffle_number : integer |
||
| 50 | Set the number of shuffles. |
||
| 51 | |||
| 52 | seed : integer, random_state, or None (default) |
||
| 53 | Indicator of random number generation state. |
||
| 54 | |||
| 55 | Returns |
||
| 56 | ------- |
||
| 57 | new_order_data_graph : NetworkXGraphObject |
||
| 58 | Returns a new graph that shuffles the order of nodes but keeps the relationships between them the same. |
||
| 59 | |||
| 60 | Examples |
||
| 61 | -------- |
||
| 62 | >>> G = Graph({0: {1: {}}, 1: {0: {}, 2: {}}, 2: {1: {}, 3: {}}, 3: {2: {}, 4: {}}, 4: {3: {}}}) |
||
| 63 | >>> shuffle_graph(G, 1, 65535).adj #Set seed to make the results repeatable. |
||
| 64 | AdjacencyView({3: {2: {}, 4: {}}, 4: {3: {}}, 1: {0: {}, 2: {}}, 2: {3: {}, 1: {}}, 0: {1: {}}}) |
||
| 65 | ''' |
||
| 66 | |||
| 67 | import random |
||
| 68 | from networkx.classes.graph import Graph |
||
| 69 | from networkx.classes.digraph import DiGraph |
||
| 70 | from networkx.classes.multigraph import MultiGraph |
||
| 71 | from networkx.classes.multidigraph import MultiDiGraph |
||
| 72 | from networkx.convert import from_dict_of_dicts |
||
| 73 | |||
| 74 | random.seed(seed) |
||
| 75 | |||
| 76 | list_of_nodes = list(data_graph.nodes) |
||
| 77 | for _i in range(shuffle_number): |
||
| 78 | random.shuffle(list_of_nodes) |
||
| 79 | new_order_data_graph = dict() |
||
| 80 | for node in list_of_nodes: |
||
| 81 | new_order_data_graph.update({node: data_graph[node]}) |
||
| 82 | if data_graph.is_directed(): |
||
| 83 | if data_graph.is_multigraph(): |
||
| 84 | new_order_data_graph = from_dict_of_dicts(new_order_data_graph, create_using = MultiDiGraph, multigraph_input = True) |
||
| 85 | else: |
||
| 86 | new_order_data_graph = from_dict_of_dicts(new_order_data_graph, create_using = DiGraph) |
||
| 87 | else: |
||
| 88 | if data_graph.is_multigraph(): |
||
| 89 | new_order_data_graph = from_dict_of_dicts(new_order_data_graph, create_using = MultiGraph, multigraph_input = True) |
||
| 90 | else: |
||
| 91 | new_order_data_graph = from_dict_of_dicts(new_order_data_graph, create_using = Graph) |
||
| 92 | return new_order_data_graph |
||
| 93 |