d2_reduction.py 1.73 KB
Newer Older
1
2
3
4
5
6
#!/usr/bin/env python3

import networkx as nx
import argparse
import sys

7
8
from deconvolution.d2graph import d2_graph as d2
from deconvolution.d2graph import d2_algorithms as d2a
9
10
11
12


def parse_arguments():
    parser = argparse.ArgumentParser(description='Reduce the number of edges of a d2 graph. The reduction is performed on edges that are redundant.')
Yoann Dufresne's avatar
Yoann Dufresne committed
13
14
15
    parser.add_argument('lcp_graph', help='lcp graph to reduce. Must be a gefx formated file.')
    parser.add_argument('--output_lcpg_name', '-o', default="", help="Output file prefix.")
    parser.add_argument('--component_min_size', '-c', type=int, default=10, help="Minimum size of a component to keep it in the lcp graph, after the edge simplifications.")
16
17

    args = parser.parse_args()
Yoann Dufresne's avatar
Yoann Dufresne committed
18
19
20
21

    if args.output_lcpg_name == "":
        args.output_lcpg_name = args.lcp_graph[:args.lcp_graph.rfind('.')] + "_reduced.gexf"

22
23
24
25
26
27
28
    return args


def main():
    # Parsing the arguments and validate them
    args = parse_arguments()

Yoann Dufresne's avatar
Yoann Dufresne committed
29
30
    lcpg_name = args.lcp_graph
    if not lcpg_name.endswith(".gexf"):
31
32
33
34
        print("Inputs file must be gexf formatted", file=sys.stderr)
        exit(1)
    
    # Loading
Yoann Dufresne's avatar
Yoann Dufresne committed
35
36
37
    print("--- lcp graph loading ---")
    lcpg = d2.D2Graph()
    lcpg.load(lcpg_name)
38
39

    # Algorithms for reduction
Yoann Dufresne's avatar
Yoann Dufresne committed
40
    print("--- 3-reduction processing ---")
Yoann Dufresne's avatar
Yoann Dufresne committed
41
    d2a.transitive_reduction(lcpg)
42

Yoann Dufresne's avatar
Yoann Dufresne committed
43
    # Take the principal component
Yoann Dufresne's avatar
Yoann Dufresne committed
44
    print("--- Extract and write the graph ---")
Yoann Dufresne's avatar
Yoann Dufresne committed
45
    components = [c for c in nx.connected_components(lcpg) if len(c) > args.component_min_size]
46
47
48
    nodes = set()
    for comp in components:
        nodes.update(comp)
Yoann Dufresne's avatar
Yoann Dufresne committed
49
    subgraph = lcpg.subgraph(nodes)
50
51

    # Write the simplified graph
Yoann Dufresne's avatar
Yoann Dufresne committed
52
    nx.write_gexf(subgraph, args.output_lcpg_name)
53
54
55
56


if __name__ == "__main__":
    main()