Exemplo n.º 1
0
    def test_add_existing(self):
        W = TimeNodeSet([TimeNode("u", 1, 3)])
        W.add(TimeNode("u", 2,4))

        expected = TimeNodeSet([TimeNode("u", 1, 4)])

        assert(W == expected)
Exemplo n.º 2
0
    def find_bicore(self, stream):

        hubs = TimeNodeSet()

        for u in stream.degrees:

            if u in stream.V["left"]:
                threshold = self.h
            if u in stream.V["right"]:
                threshold = self.a

            neighbourhood = set()
            last_t = {}

            for v, t, e_type, label in sorted(stream.degrees[u],
                                              key=operator.itemgetter(1, 2)):
                if e_type == 1:
                    neighbourhood.add(v)
                    last_t[u] = t
                    last_t[v] = t
                elif e_type == -1:
                    if len(neighbourhood) >= threshold:
                        # print(u, len(neighbourhood), last_t[u], t)
                        hubs.add(TimeNode(u, last_t[u], t))
                        # We have no idea about v's neighbourhood.
                        # hubs.add(TimeNode(v, last_t[v], t))
                    neighbourhood.remove(v)

        # print(len(hubs), len(stream.W))
        return stream.substream(hubs, hubs)
Exemplo n.º 3
0
    def test_add_empty(self):
        W = TimeNodeSet()
        W.add(TimeNode("u", 2,4))

        expected = TimeNodeSet([TimeNode("u", 2, 4)])

        assert(W == expected)
Exemplo n.º 4
0
    def test_set_intersection(self):
        W = TimeNodeSet([
                TimeNode("u", 2, 4)
            ])
        W2 = TimeNodeSet([
                TimeNode("u", 3, 5),
                TimeNode("v", 1, 6)
            ])

        expected = TimeNodeSet([
                TimeNode("u", 3, 4)
            ])

        assert(W.intersection(W2) == expected)
Exemplo n.º 5
0
def generic_interior(s, X1, X2, patterns=set()):
    """
        Slower, but works for any property that defines p_1 and p2
        Use it if the property has no self defined interior function.
    """
    stsa = s.core_property

    S1 = TimeNodeSet()
    S2 = TimeNodeSet()

    substream = s.substream(X1, X2)
    nodes = set(substream.V)  # set( list(X1.nodes()) +  list(X2.nodes()) )

    while 1:
        old_S1 = S1
        old_S2 = S2
        for u in nodes:
            p1_true, tmp = stsa.p1(u, substream)
            p2_true, tmp2 = stsa.p2(u, substream)

            if p1_true:
                for x in tmp:
                    S1.add(x)
            if p2_true:
                for x in tmp2:
                    S2.add(x)

        if S1 == old_S1 or S2 == old_S2:
            break

    return S1, S2
Exemplo n.º 6
0
    def test_set_disjoint_union(self):
        W = TimeNodeSet([
                TimeNode("u", 2, 4)
            ])
        W2 = TimeNodeSet([
                TimeNode("u", 5, 7)
            ])

        expected = TimeNodeSet([
                TimeNode("u", 2, 4),
                TimeNode("u", 5, 7)
            ])

        assert(W.union(W2) == expected)
Exemplo n.º 7
0
    def extent(self, S=None):
        """
            Returns the extent (support set) of a pattern in a stream S
        """
        q = self.lang

        # for x in S.E:
        # if len(q["left"]) > 0 and len(x["label"]["left"]) > 0:
        # assert(list(q["left"])[0][0] == list(x["label"]["left"])[0][0])
        # if len(q["right"]) > 0 and len(x["label"]["right"]) > 0:
        # assert(list(q["right"])[0][0] == list(x["label"]["right"])[0][0])

        X1 = [
            TimeNode(x["u"], x["b"], x["e"]) for x in S.E
            if q["left"].issubset(set(x["label"]["left"]))
        ]
        X2 = [
            TimeNode(x["v"], x["b"], x["e"]) for x in S.E
            if q["right"].issubset(set(x["label"]["right"]))
        ]

        X = X1 + X2
        X = TimeNodeSet(X)

        return X
Exemplo n.º 8
0
    def update_time(self, node, link):

        sub_W = TimeNodeSet(elements=[ TimeNode(x, link["b"], link["e"]) for x in s.V ])
        subs = self.s.substream(sub_W, sub_W)

        embs_time = [ self.embs[v] for v in subs.V ] + [self.embs[node]]

        return torch.mean(torch.stack(embs_time), dim=0)
Exemplo n.º 9
0
def bipattern_distance(s, p, q):
    """
        s: the stream graph on which the patterns are enumerated
        p,q: two BiPatterns
        @return: the similarity between two patterns.
    """

    top, bot = s.V["left"], s.V["right"]
    get_w = lambda p: (TimeNodeSet([
        x for x in p.support_set.W if x.node in top
    ]), TimeNodeSet([x for x in p.support_set.W if x.node in bot]))

    W1 = get_w(p)
    W2 = get_w(q)

    score_top = len(W1[0].intersection(W2[0])) / len(W1[0].union(W2[0]))
    score_bot = len(W1[1].intersection(W2[1])) / len(W1[1].union(W2[1]))

    return 1 - min(score_top, score_bot)
Exemplo n.º 10
0
    def test_substream_simple(self, test_stream):
        s = test_stream
        mini_W = TimeNodeSet([
            TimeNode("u", s.T["alpha"], s.T["omega"]),
            TimeNode("v", s.T["alpha"], s.T["omega"]),
            TimeNode("x", s.T["alpha"], s.T["omega"]),
        ])
        sub = s.substream(mini_W, mini_W)

        expected = BipartiteStream()
        expected.T = {"alpha": 0, "omega": 10}
        expected.V = {"left": set("ux"), "right": set("v")}
        expected.W = TimeNodeSet(
            [TimeNode("u", 1, 5),
             TimeNode("v", 1, 5),
             TimeNode("x", 1, 3)])
        expected.E = [{
            "b": 1,
            "e": 5,
            "u": "u",
            "v": "v",
            "label": {
                "left": set(["a", "b", "c", "d"]),
                "right": set(["w", "x"])
            }
        }, {
            "b": 1,
            "e": 3,
            "u": "x",
            "v": "v",
            "label": {
                "left": set(["a", "b", "c"]),
                "right": set(["w", "x"])
            }
        }]

        assert (sub == expected)
Exemplo n.º 11
0
def jaccard(s, u, v):
    """
        s: a Stream object
        u,v : two nodes

        @return: the Jaccard coefficient of u and v
    """

    n_u = TimeNodeSet([
        TimeNode(x, b, e) for x in s.neighbours(u)
        for b, e, l_u, l_v in s.times[frozenset([u, x])]
    ])
    n_v = TimeNodeSet([
        TimeNode(x, b, e) for x in s.neighbours(v)
        for b, e, l_u, l_v in s.times[frozenset([v, x])]
    ])

    # Union
    union = sum((x.e - x.b for x in n_u.union(n_v)))

    # Intersection
    inter = sum((x.e - x.b for x in n_u.intersection(n_v)))

    return inter / union
Exemplo n.º 12
0
def pattern_distance(s, p, q):
    """
        s: the stream graph on which the patterns are enumerated
        p,q: two Patterns
        @return: the similarity between two patterns.
    """

    nodeset = s.V
    get_w = lambda p: TimeNodeSet(
        [x for x in p.support_set.W if x.node in nodeset])

    W1 = get_w(p)
    W2 = get_w(q)

    score = len(W1.intersection(W2)) / len(W1.union(W2))

    return 1 - score
Exemplo n.º 13
0
    def interior(self, s):
        """
            Interior function for star satellite, modify to return a stream instead ?
            @param s: a stream graph
            @return: two TimeNodeSets, each containing the k-stars and k-satellites of s
        """
        THRESHOLD = self.threshold
        stars = TimeNodeSet()
        satellites = TimeNodeSet()

        for k, u in enumerate(s.degrees):
            neigh = set()
            best_neighs = set()
            last_times = {}  # {u: -1 for u in s.degrees}
            for i in sorted(s.degrees[u], key=operator.itemgetter(1, 2, -3)):
                v, t, ev_type = i[0], i[1], i[2]
                # First check if the property is true
                starsat_is_true = len(neigh) >= THRESHOLD

                if starsat_is_true:
                    best_neighs = best_neighs.union(neigh)

                if ev_type == 1:
                    neigh.add(v)
                    last_times[v] = t
                    if not starsat_is_true:
                        # While the property is true (typically, we have degree > THRESHOLD)
                        # u remains star, so we should not change the times
                        last_times[u] = t
                else:
                    neigh.remove(v)
                    if starsat_is_true:
                        stars.add(TimeNode(u, last_times[u], t))
                        for x in best_neighs:
                            # min sur le t aussi ?
                            satellites.add(
                                TimeNode(x, max(last_times[x], last_times[u]),
                                         t))
                        best_neighs = set()
        return s.substream(stars, satellites)
Exemplo n.º 14
0
    def interior_bak(self, s, X=None, Y=None):
        # Define nodesets
        if X is None and Y is None:
            X = s.V["left"]
            Y = s.V["right"]
        hub = TimeNodeSet()
        authority = TimeNodeSet()

        for k, u in enumerate(s.degrees):
            if u in X:
                threshold = self.h
            else:
                threshold = self.a

            neigh = set()
            last_times = {}  # {u: -1 for u in s.degrees}
            for i in sorted(s.degrees[u], key=operator.itemgetter(1, 2, -3)):
                v, t, ev_type = i[0], i[1], i[2]
                # First check if the property is true
                bha_is_true = len(neigh) >= threshold

                if ev_type == 1:
                    neigh.add(v)
                    last_times[v] = t
                    if not bha_is_true:
                        # While the property is true (typically, we have degree > THRESHOLD)
                        # u remains star, so we should not change the times
                        last_times[u] = t
                else:
                    neigh.remove(v)
                    if bha_is_true:
                        if u in X:
                            hub.add(TimeNode(u, last_times[u], t))
                        else:
                            authority.add(TimeNode(u, last_times[u], t))

        return s.substream(hub, authority)
Exemplo n.º 15
0
    def extent(self, S=None):
        """
            Returns the extent (support set) of a pattern in a stream S
        """
        q = self.lang

        # if S is None:
        # S = (self.support_set.E, self.support_set.E)
        # else:
        # S = (S.E, S.E)

        X1 = [
            TimeNode(x["u"], x["b"], x["e"], _label=set(x["label"]["left"]))
            for x in S.E if q.issubset(set(x["label"]["left"]))
        ]
        X2 = [
            TimeNode(x["v"], x["b"], x["e"], _label=set(x["label"]["right"]))
            for x in S.E if q.issubset(set(x["label"]["right"]))
        ]

        X = X1 + X2
        X = TimeNodeSet(X)

        return X
Exemplo n.º 16
0
 def test_len(self):
     W = TimeNodeSet()
     assert(len(W) == 0)