GiulioRossetti / dynetx

Dynamic Network Analysis library
http://dynetx.readthedocs.io/en/latest/
BSD 2-Clause "Simplified" License
105 stars 34 forks source link

Adjust time_slice method to return sliced graph with correct interactions #159

Open ypislon opened 7 months ago

ypislon commented 7 months ago

Problem description

When using time_slice to create a time slice of a graph, the returned time slice contains wrong timestamps for the end of an interaction (which are always one unit to small) and the time slice does not take all interactions into account for directed graphs and reverses the direction of edges (see #135).

The terminology here was a bit confusing to me: when using time_slice(t_from=0, t_to=2), I expect the graph to include all snapshots from t=0 to and including t=2. When adding an interaction add_interaction(t=0, e=2), I expect the interaction to last from t=0 to t=1 and vanish at e=2. (I hope I interpret the terminology used by the library correctly).

The changes in this branch therefore add an additional time unit when creating the time slice to take this into account.

The changes also respect the direction of edges for directed graphs when checking for seen nodes and when creating a list of interactions.

This should fix #135.

Example

G = dn.DynDiGraph()
G.add_interaction(0,1, t=0, e=2)
G.add_interaction(1,2, t=0)
G.add_interaction(2,4, t=0)
G.add_interaction(0,4, t=1)
G.add_interaction(4,5, t=1)
G.add_interaction(5,6, t=1)
G.add_interaction(7,1, t=2)
G.add_interaction(1,2, t=2)
G.add_interaction(2,3, t=2)
H = G.time_slice(0)
I = G.time_slice(0, 2)

Previous behavior:

>>> G.interactions()
[(0, 1, {'t': [[0, 1]]}), (0, 4, {'t': [[1, 1]]}), (1, 2, {'t': [[0, 0], [2, 2]]}), (1, 7, {'t': [[2, 2]]}), (2, 4, {'t': [[0, 0]]}), (2, 3, {'t': [[2, 2]]}), (4, 5, {'t': [[1, 1]]}), (5, 6, {'t': [[1, 1]]})]
>>> H.interactions()
[(0, 1, {'t': [[0, -1]]})]
>>> I.interactions()
[(0, 1, {'t': [[0, 0]]}), (2, 4, {'t': [[0, -1]]})]

Behavior after changes:

>>> G.interactions()
[(0, 1, {'t': [[0, 1]]}), (0, 4, {'t': [[1, 1]]}), (1, 2, {'t': [[0, 0], [2, 2]]}), (1, 7, {'t': [[2, 2]]}), (2, 4, {'t': [[0, 0]]}), (2, 3, {'t': [[2, 2]]}), (4, 5, {'t': [[1, 1]]}), (5, 6, {'t': [[1, 1]]})]
>>> H.interactions()
[(0, 1, {'t': [[0, 0]]}), (1, 2, {'t': [[0, 0]]}), (2, 4, {'t': [[0, 0]]})]
>>> I.interactions()
[(0, 1, {'t': [[0, 1]]}), (0, 4, {'t': [[1, 1]]}), (1, 2, {'t': [[0, 0], [2, 2]]}), (1, 7, {'t': [[2, 2]]}), (4, 2, {'t': [[0, 0]]}), (4, 5, {'t': [[1, 1]]}), (2, 3, {'t': [[2, 2]]}), (5, 6, {'t': [[1, 1]]})]