d2_path.py 4.95 KB
Newer Older
Yoann Dufresne's avatar
Yoann Dufresne committed
1
import networkx as nx
2
3
from collections import Counter
import sys
Yoann Dufresne's avatar
Yoann Dufresne committed
4

5

Yoann Dufresne's avatar
Yoann Dufresne committed
6
7
""" Represent an udg path into a d2g graph
"""
Yoann Dufresne's avatar
Yoann Dufresne committed
8
class Path(list):
9

Yoann Dufresne's avatar
Yoann Dufresne committed
10
    def __init__(self, d2g):
11
        super(Path, self).__init__()
Yoann Dufresne's avatar
Yoann Dufresne committed
12
        self.d2g = d2g
13
        self.covering_variables = {x: 0 for x in self.d2g.variables}
Yoann Dufresne's avatar
Yoann Dufresne committed
14
        self.covering_value = 0
15
16
17
18
        # a succession of Counter (multiset)
        self.barcode_order = []
        self.lcp_per_multiset = []
        self.barcode_score = 0
Yoann Dufresne's avatar
Yoann Dufresne committed
19
        self.size_score = 0
20

Yoann Dufresne's avatar
Yoann Dufresne committed
21
    def append(self, obj) -> None:
Yoann Dufresne's avatar
Yoann Dufresne committed
22
        lcp = self.d2g.get_lcp(obj)
Yoann Dufresne's avatar
Yoann Dufresne committed
23
24

        # Update the covering variables
25
        for edge_idx in lcp.edges:
Yoann Dufresne's avatar
Yoann Dufresne committed
26
27
28
29
            if self.covering_variables[edge_idx] == 0:
                self.covering_value += 1
            self.covering_variables[edge_idx] += 1

30
31
        self._append_barcodes(lcp)

Yoann Dufresne's avatar
Yoann Dufresne committed
32
33
        super(Path, self).append(lcp)

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    def _append_barcodes(self, lcp):
        set_idx = len(self.barcode_order) - 1
        remaining_barcodes = Counter(lcp.nodes)

        while set_idx >= 0:
            current_multiset = self.barcode_order[set_idx]
            intersection = remaining_barcodes & current_multiset

            if intersection == current_multiset:
                remaining_barcodes -= intersection
                self.lcp_per_multiset[set_idx].add(lcp)
                self.barcode_score += 1
            else:
                if len(intersection) == 0:
                    break
                # Split the multiset in two parts
                left = current_multiset - intersection
                right = intersection
                self.barcode_order[set_idx] = right
                self.barcode_order.insert(set_idx, left)
                # Split the lcp appearance
                self.lcp_per_multiset.insert(set_idx, self.lcp_per_multiset[set_idx].copy())
                self.lcp_per_multiset[set_idx+1].add(lcp)
                self.barcode_score += len(self.lcp_per_multiset[set_idx+1])
                # Update remaining barcodes
                remaining_barcodes -= intersection
                break
            set_idx -= 1

        if len(remaining_barcodes) > 0:
            self.barcode_order.append(remaining_barcodes)
            self.lcp_per_multiset.append({lcp})
            self.barcode_score += 1

Yoann Dufresne's avatar
Yoann Dufresne committed
68
    def pop(self, index=-1):
69
70
71
72
        if index != -1:
            print("Warning: pop on other values than -1 have side effects here. The code will be adapted soon", file=sys.stderr)
            exit(1)
        index = -1
Yoann Dufresne's avatar
Yoann Dufresne committed
73
        lcp = super(Path, self).pop(index)
74
        self._pop_barcodes(lcp)
Yoann Dufresne's avatar
Yoann Dufresne committed
75
76

        # Update the covering variables
77
78
79
        for e_idx in lcp.edges:
            self.covering_variables[e_idx] -= 1
            if self.covering_variables[e_idx] == 0:
Yoann Dufresne's avatar
Yoann Dufresne committed
80
81
82
83
                self.covering_value -= 1

        return lcp

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    def _pop_barcodes(self, lcp):
        set_idx = len(self.barcode_order) - 1

        while set_idx >= 0 and lcp in self.lcp_per_multiset[set_idx]:
            lcp_list = self.lcp_per_multiset[set_idx]
            lcp_list.remove(lcp)
            self.barcode_score -= 1
            if len(lcp_list) == 0:
                self.lcp_per_multiset.pop(set_idx)
                self.barcode_order.pop(set_idx)
            set_idx -= 1

        if 0 <= set_idx < len(self.lcp_per_multiset) - 1 and self.lcp_per_multiset[set_idx] == self.lcp_per_multiset[set_idx+1]:
            rmv = self.lcp_per_multiset.pop(set_idx)
            self.barcode_score -= len(rmv)
            self.barcode_order[set_idx+1] += self.barcode_order[set_idx]
            self.barcode_order.pop(set_idx)

Yoann Dufresne's avatar
Yoann Dufresne committed
102
103
104
105
106
107
108
109
110
111
112
113
    def copy(self):
        copy = Path(self.d2g)

        # Copy the list
        for x in self:
            super(Path, copy).append(x)

        # Copy the variables
        for key, val in self.covering_variables.items():
            copy.covering_variables[key] = val
        copy.covering_value = self.covering_value

114
115
116
117
118
119
120
        # Copy barcode structures
        for lcp_list in self.lcp_per_multiset:
            copy.lcp_per_multiset.append(lcp_list.copy())
        for barcodes in self.barcode_order:
            copy.barcode_order.append(barcodes.copy())
        copy.barcode_score = self.barcode_score

Yoann Dufresne's avatar
Yoann Dufresne committed
121
122
        return copy

Yoann Dufresne's avatar
Yoann Dufresne committed
123
124
125
    def add_path(self, path):
        for lcp in path:
            self.append(lcp)
Yoann Dufresne's avatar
Yoann Dufresne committed
126

Yoann Dufresne's avatar
Yoann Dufresne committed
127
128
    def covering_score(self):
        return self.covering_value / len(self.covering_variables)
Yoann Dufresne's avatar
Yoann Dufresne committed
129

Yoann Dufresne's avatar
Yoann Dufresne committed
130
    def save_gexf(self, filename):
Yoann Dufresne's avatar
Yoann Dufresne committed
131
132
133
134
135
136
137
        d2p = nx.Graph()
        # Add the nodes
        for udg in self:
            d2p.add_node(udg.idx)
            d2p.nodes[udg.idx]["center"] = udg.center
            d2p.nodes[udg.idx]["udg"] = str(udg)
            d2p.nodes[udg.idx]["score"] = f"{udg.score}/{udg.get_optimal_score()}"
138
            barcode_edges = " ".join([str(x) for x in udg.edges])
Yoann Dufresne's avatar
Yoann Dufresne committed
139
            d2p.nodes[udg.idx]["barcode_edges"] = barcode_edges
Yoann Dufresne's avatar
Yoann Dufresne committed
140
141
142
143
144
145
146
147

        # add the edges
        for idx in range(len(self)-1):
            udg1 = self[idx]
            udg2 = self[idx+1]

            d2p.add_edge(udg1.idx, udg2.idx)

Yoann Dufresne's avatar
Yoann Dufresne committed
148
149
        nx.write_gexf(d2p, filename)