Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions dynetx/classes/dyndigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def interactions_iter(self, nbunch=None, t=None):
A container of nodes. The container will be iterated
through once.
t : snapshot id (default=None)
If None the the method returns an iterator over the edges of the flattened graph.
If None the method returns an iterator over the edges of the flattened graph.

Returns
-------
Expand Down Expand Up @@ -477,12 +477,12 @@ def interactions_iter(self, nbunch=None, t=None):
for n, nbrs in nodes_nbrs_succ:
for nbr in nbrs:
if t is not None:
if nbr not in seen and self.__presence_test(n, nbr, t):
if (n, nbr) not in seen and self.__presence_test(n, nbr, t):
yield n, nbr, {"t": [t]}
else:
if nbr not in seen:
yield nbr, n, self._succ[n][nbr]
seen[n] = 1
if (n, nbr) not in seen:
yield n, nbr, self._succ[n][nbr]
seen[(n, nbr)] = 1

del seen

Expand Down Expand Up @@ -537,7 +537,10 @@ def add_interaction(self, u, v, t=None, e=None):
self._node[v] = {}

if type(t) != list:
t = [t, t]
if e is not None and e > t:
t = [t, e - 1]
else:
t = [t, t]

for idt in [t[0]]:
if self.has_edge(u, v) and not self.edge_removal:
Expand All @@ -550,8 +553,6 @@ def add_interaction(self, u, v, t=None, e=None):
self.time_to_edge[idt][(u, v, "+")] = None

if e is not None and self.edge_removal:

t[1] = e - 1
if e not in self.time_to_edge:
self.time_to_edge[e] = {(u, v, "-"): None}
else:
Expand Down Expand Up @@ -1258,14 +1259,15 @@ def stream_interactions(self):
yield e[0], e[1], e[2], t

def time_slice(self, t_from, t_to=None):
"""Return an new graph containing nodes and interactions present in [t_from, t_to].
"""Return a new graph containing nodes and interactions present in [t_from, t_to].

Parameters
----------

t_from : snapshot id, mandatory
t_to : snapshot id, optional (default=None)
If None t_to will be set equal to t_from
Time slice includes the interactions in snapshot t_to

Returns
-------
Expand Down Expand Up @@ -1297,7 +1299,7 @@ def time_slice(self, t_from, t_to=None):

if t_to is not None:
if t_to < t_from:
raise ValueError("Invalid range: t_to must be grater that t_from")
raise ValueError("Invalid range: t_to must be greater that t_from")
else:
t_to = t_from

Expand All @@ -1310,13 +1312,13 @@ def time_slice(self, t_from, t_to=None):
continue

if f_from >= a and i_to <= b:
H.add_interaction(u, v, f_from, i_to)
H.add_interaction(u, v, f_from, i_to + 1)
elif a >= f_from and i_to <= b:
H.add_interaction(u, v, a, i_to)
H.add_interaction(u, v, a, i_to + 1)
elif f_from >= a and b <= i_to:
H.add_interaction(u, v, f_from, b)
H.add_interaction(u, v, f_from, b + 1)
elif f_from <= a and b <= i_to:
H.add_interaction(u, v, a, b)
H.add_interaction(u, v, a, b + 1)

for n in H.nodes():
H._node[n] = self._node[n]
Expand Down
15 changes: 8 additions & 7 deletions dynetx/classes/dyngraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,10 @@ def add_interaction(self, u, v, t=None, e=None):
self._node[v] = {}

if not isinstance(t, list):
t = [t, t]
if e is not None and e > t:
t = [t, e - 1]
else:
t = [t, t]

for idt in [t[0]]:
if self.has_edge(u, v) and not self.edge_removal:
Expand All @@ -369,8 +372,6 @@ def add_interaction(self, u, v, t=None, e=None):
self.time_to_edge[idt][(u, v, "+")] = None

if e is not None and self.edge_removal:

t[1] = e - 1
if e not in self.time_to_edge:
self.time_to_edge[e] = {(u, v, "-"): None}
else:
Expand Down Expand Up @@ -1055,13 +1056,13 @@ def time_slice(self, t_from, t_to=None):
continue

if f_from >= a and i_to <= b:
H.add_interaction(u, v, f_from, i_to)
H.add_interaction(u, v, f_from, i_to + 1)
elif a >= f_from and i_to <= b:
H.add_interaction(u, v, a, i_to)
H.add_interaction(u, v, a, i_to + 1)
elif f_from>=a and b<= i_to:
H.add_interaction(u, v, f_from, b)
H.add_interaction(u, v, f_from, b + 1)
elif f_from <= a and b <= i_to:
H.add_interaction(u, v, a, b)
H.add_interaction(u, v, a, b + 1)

for n in H.nodes():
H._node[n] = self._node[n]
Expand Down