Statistics
| Branch: | Revision:

iof-tools / utils / get_random_nodes.py @ a256629c

History | View | Annotate | Download (2.03 KB)

1
#!/usr/bin/env python
2

    
3
# This script selects an arbitrary number of random nodes from a
4
# topology graph. It has been used to select the nodes originating
5
# a change in the network in the "Elmokashfi" simulations.
6

    
7
from networkx import *
8
from argparse import ArgumentParser
9
from random import randint
10

    
11
if __name__ == "__main__":
12
    parser = ArgumentParser()
13

    
14
    parser.add_argument("-g", "--graph", dest="graph",required=True,
15
                        default="", action="store",
16
                        help="Graphml file")
17
    parser.add_argument("-t", "--type", dest="type", default="", action="store",
18
                        help = "Node type. Can be either T, M, CP, or C.")
19
    parser.add_argument("-n", "--num", dest="num", default=5, action="store",
20
                        help="Number of AS to extract")
21
    parser.add_argument("-l", "--leaf", dest="leaf", required=False, action = "store_true",
22
                        help = "Forces the selected node to be a leaf")
23

    
24

    
25
    args = parser.parse_args()
26

    
27
    graph = read_graphml(args.graph)
28

    
29
    num = int(args.num)
30

    
31
    if args.leaf and args.type != "C":
32
        print("Only C nodes can be leafs.")
33
        exit()
34
    if num < 1:
35
        print("Number of AS to extract too low")
36
        exit()
37

    
38
    nodes_num = graph.number_of_nodes()
39
    nodes_list = list(graph.nodes(data=True))
40

    
41
    extracted_nodes = 0
42
    while extracted_nodes < num:
43
        r = randint(0,nodes_num-1)
44

    
45
        if args.type == "":
46
            print("ID: {}".format(r))
47
            extracted_nodes += 1
48
        else:
49
            t = nodes_list[r][1]['type']
50
            #print(t)
51
            if t != args.type:
52
                continue
53
            if args.leaf:
54
                # Need to be a leaf
55
                deg = graph.degree[nodes_list[r][0]]
56
                #print(deg)
57
                if deg != 1:
58
                    continue
59
                else:
60
                    print("ID: {}".format(r))
61
                    extracted_nodes += 1
62
            else:
63
                print("ID: {}".format(r))
64
                extracted_nodes += 1
65