Skip to content

Commit

Permalink
Merge pull request #254 from neserende/edge-compartment-update
Browse files Browse the repository at this point in the history
Fixed the error about edge_attr dict in cases where edge is defined a…
  • Loading branch information
GiulioRossetti authored Mar 26, 2024
2 parents 84a1650 + cef60ce commit aeda373
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
45 changes: 34 additions & 11 deletions ndlib/models/compartments/EdgeCategoricalAttribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,47 @@ def __init__(

def execute(self, node, graph, status, status_map, *args, **kwargs):
neighbors = list(graph.neighbors(node))
isDiGraph = False #true if the current graph is directed

if isinstance(graph, nx.DiGraph):
neighbors = list(graph.predecessors(node))
isDiGraph = False

edge_attr = graph.get_edge_attributes(self.attribute)

if self.trigger is not None:
triggered = [
v
for v in neighbors
if status[v] == status_map[self.trigger]
and edge_attr[(min([node, v]), max([node, v]))] == self.attribute_value
]
if(isDiGraph):
triggered = [
v
for v in neighbors
if status[v] == status_map[self.trigger]
and edge_attr[(min([node, v]), max([node, v]))] == self.attribute_value
]
else:
triggered = []
for v in neighbors:
if((node, v) in edge_attr):
check = edge_attr[(node, v)]
else: #then graph.has_edge(v, node) is True because v is given as neighbor of node
check = edge_attr[(v, node)]
if status[v] == status_map[self.trigger] and check == self.attribute_value:
triggered.append(v)
else:
triggered = [
v
for v in neighbors
if edge_attr[(min([node, v]), max([node, v]))] == self.attribute_value
]
if(isDiGraph):
triggered = [
v
for v in neighbors
if edge_attr[(min([node, v]), max([node, v]))] == self.attribute_value
]
else:
triggered = []
for v in neighbors:
if((node, v) in edge_attr):
check = edge_attr[(node, v)]
else:
check = edge_attr[(v, node)]
if check == self.attribute_value:
triggered.append(v)

for _ in triggered:
p = np.random.random_sample()
Expand Down
10 changes: 8 additions & 2 deletions ndlib/models/compartments/EdgeNumericalAttribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ def execute(self, node, graph, status, status_map, *args, **kwargs):
if self.trigger is not None:
for v in neighbors:
if status[v] == status_map[self.trigger]:
val = edge_attr[(min([node, v]), max([node, v]))]
if((node, v) in edge_attr):
val = edge_attr[(node, v)]
else:
val = edge_attr[(v, node)]
if self.operator == "IN":
if self.__available_operators[self.operator][0](
val, self.attribute_range[0]
Expand All @@ -82,7 +85,10 @@ def execute(self, node, graph, status, status_map, *args, **kwargs):
triggered.append(v)
else:
for v in neighbors:
val = edge_attr[(min([node, v]), max([node, v]))]
if((node, v) in edge_attr):
val = edge_attr[(node, v)]
else:
val = edge_attr[(v, node)]
if self.operator == "IN":
if self.__available_operators[self.operator][0](
val, self.attribute_range[0]
Expand Down

0 comments on commit aeda373

Please sign in to comment.