예제 #1
0
    def to_DynGraphLS(self):
        """
        Convert to a linkstream

        Currently, conserve only edges
        :return:
        """
        sn_duration = self.frequency()

        by_edges = dict()
        by_nodes = dict()

        for t, g in self.snapshots().items():
            for e in g.edges():
                by_edges.setdefault(frozenset(e), []).append(t)
            for n in g.nodes():
                by_nodes.setdefault(n, []).append(t)

        by_edges = {tuple(e): v for e, v in by_edges.items()}
        by_nodes = {
            n: tn.Intervals.from_time_list(v, sn_duration)
            for n, v in by_nodes.items()
        }
        return tn.DynGraphLS(start=min(self.snapshots_timesteps()),
                             end=max(self.snapshots_timesteps()) + sn_duration,
                             edges=by_edges,
                             nodes=by_nodes,
                             frequency=sn_duration)
예제 #2
0
def read_LS(filename):
    """
    Read TS json format

    :param filename:
    :return:
    """
    g = json.load(open(filename, 'r'))
    node_dict = {i: n for i, n in enumerate(g["nodes"])}
    time_dict = {i: t for i, t in enumerate(g["times"])}
    freq = g["frequency"]
    edges = g["interactions"]
    edges = {eval(e): [time_dict[t] for t in ts] for e, ts in edges.items()}
    edges = {(node_dict[e[0]], node_dict[e[1]]): ts for e, ts in edges.items()}
    return tn.DynGraphLS(edges=edges,frequency=freq)
예제 #3
0
    def test_add_nodes_ls(self):
        dg = tn.DynGraphLS()
        dg.add_node_presence(1,(1,2))
        dg.add_node_presence(2,(1,2))
        dg.add_nodes_presence_from(3,(1,7))
        dg.add_nodes_presence_from([4,5],(1,2))
        dg.add_nodes_presence_from([6, 7], (1,3))

        dg.remove_node_presence(2,(1,2))
        dg.remove_node_presence(3, (2,3))

        self.assertEqual(set(dg.graph_at_time(1).nodes()),set([1,3,4,5,6,7]))
        self.assertEqual(set(dg.graph_at_time(2).nodes()),set([6,7]))
        self.assertEqual(set(dg.graph_at_time(3).nodes()),set([3]))
        self.assertEqual(set(dg.graph_at_time(4).nodes()),set([3]))
        self.assertEqual(set(dg.graph_at_time(6).nodes()),set([3]))
예제 #4
0
def from_pandas_interaction_list(interactions,
                                 format,
                                 frequency=1,
                                 source="n1",
                                 target="n2",
                                 time="time"):
    interactions = two_columns2unidrected_edge(interactions,
                                               source="n1",
                                               target="n2")
    if format == tn.DynGraphSN:
        all_times = set(interactions[time])
        all_graphs = {}
        for t in all_times:
            this_t = interactions[interactions["time"] == t]
            all_graphs[t] = nx.from_pandas_edgelist(this_t,
                                                    source=source,
                                                    target=target)

        return tn.DynGraphSN(all_graphs, frequency=frequency)

    if format == tn.DynGraphLS:
        #all_edges = set(interactions["e"])
        #print(len(all_edges))
        edges_time = {}
        for i, row in interactions.iterrows():
            edges_time.setdefault(row["e"], []).append(row[time])
        #for e in all_edges:
        #    edges_time[e]= list(interactions[interactions["e"]==e][time])
        to_return = tn.DynGraphLS(edges=edges_time, frequency=frequency)
        return to_return

    if format == tn.DynGraphIG:
        #all_edges = set(interactions["e"])
        edges_time = {}
        for i, row in interactions.iterrows():
            edges_time.setdefault(row["e"], []).append(row[time])
        for e, v in edges_time.items():
            edges_time[e] = tn.Intervals.from_time_list(v, frequency)
        #for e in all_edges:
        #    edges_time[e]= list(interactions[interactions["e"]==e][time])
        #    edges_time[e]=tn.Intervals.from_time_list(edges_time)
        to_return = tn.DynGraphIG(edges_time)
        return to_return
예제 #5
0
    def test_add_interactions_ls(self):
        dg = tn.DynGraphLS()
        dg.add_interaction(1,2,4)

        self.assertEqual(set(dg.graph_at_time(4).edges()),set([(1,2)]))

        dg.add_interaction(1, 2, 6)

        self.assertEqual(list(dg.edge_presence((1,2))),[4,6])


        dg.add_interactions_from({2, 3}, [6,7,8])

        self.assertEqual(list(dg.graph_at_time(6).edges()),[(1,2),(2,3)])

        dg.add_interactions_from([(5, 6),(6,7)], 9)
        self.assertEqual(list(dg.graph_at_time(9).edges()),[(5,6),(6,7)])

        dg.add_interactions_from([(1, 2),(1,3)], [10,11])
        self.assertEqual(list(dg.graph_at_time(10).edges()),[(1,2),(1,3)])
        self.assertEqual(list(dg.graph_at_time(11).edges()),[(1,2),(1,3)])
예제 #6
0
    def slice(self, start, end):
        """
        Keep only the selected period

        :param start: time of the beginning of the slice (inclusive)
        :param end: time of the end of the slice (exclusive)
        """

        to_return = tn.DynGraphLS()
        slice_time = Intervals((start, end))
        for n, presence in self.node_presence().items():
            duration = slice_time.intersection(presence)
            if duration.duration() > 0:
                to_return.add_node_presence(n, duration)

        for e, presence in self.interactions_intervals().items():
            to_return.add_interaction(e[0], e[1], presence.islice(start, end))
            to_return.add_interaction(e[0], e[1],
                                      slice_time.intersection(presence))

        return to_return