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

Esu sample probability fix #23

Open
wants to merge 4 commits into
base: main
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
33 changes: 26 additions & 7 deletions src/esu.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,45 @@
"""

import networkx as nx
import random


class ESU:
def __init__(self, G: nx.Graph):
self.G = G

def enumerate_subgraphs(self, size: int):
#currently does not allow for motif size over 10 because of default sample probability. should be fixed
def enumerate_subgraphs(self, size: int, sample_probability: list[float] = [1,1,1,1,1,1,1,1,1,1]):
"""
Enumerates all unique subgraphs of a given motif size from the input\
graph using the ESU algorithm.
Translated for networkx from https://github.com/IlyaVasUW/NEMO_SUITE/
Translated and improved for networkx from https://github.com/IlyaVasUW/NEMO_SUITE/
"""
subgraph_list = []
nodes = self.G.nodes()
node_visited = set()

for node in nodes:
sample_neighbors = self.eliminate_percentage(list(nodes), sample_probability[0])

for node in sample_neighbors:
neighbor_set = set(self.get_right_neighbors(node))
node_list = [node]
node_visited.add(node)
self.esu_recursive_helper(size, neighbor_set, node_list, subgraph_list, node_visited)
self.esu_recursive_helper(size, neighbor_set, node_list, subgraph_list, node_visited, sample_probability)

return subgraph_list

def esu_recursive_helper(self, size: int, neighbors: set, node_list: list, subgraph_list: list, nodes_visited: set):
def esu_recursive_helper(self, size: int, neighbors: set, node_list: list, subgraph_list: list, nodes_visited: set, sample_probability: list[float]):
if size == 1:
subgraph_list.append(self.G.subgraph(node_list.copy()))
return

if len(neighbors) == 0:
return

for node in neighbors:
sample_neighbors = self.eliminate_percentage(list(neighbors), sample_probability[len(node_list)])

for node in sample_neighbors:
node_list.append(node)
nodes_visited.add(node)
next_neighbors = set()
Expand All @@ -52,7 +58,7 @@ def esu_recursive_helper(self, size: int, neighbors: set, node_list: list, subgr
if neighbor not in nodes_visited:
next_neighbors.add(neighbor)

self.esu_recursive_helper(size - 1, next_neighbors, node_list, subgraph_list, nodes_visited)
self.esu_recursive_helper(size - 1, next_neighbors, node_list, subgraph_list, nodes_visited, sample_probability)

if len(node_list) > 0:
node_list.pop()
Expand All @@ -70,3 +76,16 @@ def get_right_neighbors(self, node):
right_hand_neighbors.append(n)

return iter(right_hand_neighbors)

#Removes a given percentage of elements from a list randomly.
#KEEPS PRECENTAGE INPUT. REMOVES 1 - PRECENTAGE
def eliminate_percentage(self, collection, probability: float):
num_to_remove = int(len(collection) * (1-probability))
indices_to_remove = random.sample(range(len(collection)), num_to_remove)

# Remove elements in reverse order to avoid index issues
for i in sorted(indices_to_remove, reverse=True):
collection.pop(i)

return collection

4 changes: 2 additions & 2 deletions src/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def draw_graph(self):
def draw_subgraph(self, motif_size: int):
output_dir = "drawings/subgraphs"
esu = ESU(self.G)
esu_list = esu.enumerate_subgraphs(motif_size) # FIXME: 3 is default motif size
esu_list = esu.enumerate_subgraphs(motif_size)

for i, subgraph in enumerate(esu_list):
if self.graph_type == "Directed":
Expand All @@ -102,6 +102,6 @@ def draw_subgraph(self, motif_size: int):
with open(file_name, "r") as f:
html = f.read()

st.markdown(f"### Subgraph {labelGraph(subgraph, self.graph_type)}")
st.markdown(f"### Subgraph {i}: {labelGraph(subgraph, self.graph_type)}")
components.html(html, height=600, scrolling=True)
return