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)
def test_add_empty(self): W = TimeNodeSet() W.add(TimeNode("u", 2,4)) expected = TimeNodeSet([TimeNode("u", 2, 4)]) assert(W == expected)
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
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)
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)
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)
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)
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)
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)
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
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
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)
def test_TimeNode_eq(self): w = TimeNode("u", 2, 4) w2 = TimeNode("u", 2, 4) assert(w == w2)
def test_label_simple(self, test_stream): s = test_stream label = s.label(TimeNode("u", 1, 5)) assert (label == set("abcd"))
def test_label_included(self, test_stream): s = test_stream label = s.label(TimeNode("u", 2, 4)) assert (label == set("abcd"))
def test_get_TimeNode_interval(self): w = TimeNode("u", 2, 4) assert(w.interval.b == 2 and w.interval.e == 4)
def test_TimeNode_neq(self): w = TimeNode("u", 2, 4) w2 = TimeNode("u", 1, 3) assert(w != w2)