Skip to content

Commit

Permalink
Merge pull request #16 from pedugnat/str-ids
Browse files Browse the repository at this point in the history
Use str for node ids + generate_dynamic_graphs rename
  • Loading branch information
pedugnat committed Aug 9, 2022
2 parents bf5da42 + f849940 commit a255708
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ pip install -U dynnode2vec
import pickle

from dynnode2vec.dynnode2vec import DynNode2Vec
from dynnode2vec.utils import generate_dynamic_graphs
from dynnode2vec.utils import create_dynamic_graph

# Create random graphs
graphs = generate_dynamic_graphs(
graphs = create_dynamic_graph(
n_base_nodes=100, n_steps=50, base_density=0.05
)

Expand Down
2 changes: 1 addition & 1 deletion dynnode2vec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .biased_random_walk import BiasedRandomWalk, RandomWalks
from .dynnode2vec import DynNode2Vec, Embedding
from .utils import generate_dynamic_graphs
from .utils import create_dynamic_graph


def get_version() -> str:
Expand Down
2 changes: 1 addition & 1 deletion dynnode2vec/biased_random_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, graph: nx.Graph) -> None:
:param graph: graph to run walk on
"""
self.graph = nx.convert_node_labels_to_integers(
graph, ordering="sorted", label_attribute="true_label"
graph, ordering="default", label_attribute="true_label"
)

self.mapping: Dict[int, Any] = nx.get_node_attributes(self.graph, "true_label")
Expand Down
13 changes: 7 additions & 6 deletions dynnode2vec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@

def sample_nodes(graph: nx.Graph, k: int) -> List[int]:
"""
Sample nodes randomly from a graph.
Samples nodes randomly from a graph.
"""
return random.sample(graph.nodes, k=k)


def generate_dynamic_graphs(
def create_dynamic_graph(
n_base_nodes: int = 100, n_steps: int = 10, base_density: float = 0.01
) -> List[nx.Graph]:
"""
Generates a list of dynamic graphs, i.e. that depend on the previous graph.
Creates a list of graphs representing the evolution of a dynamic graph,
i.e. graphs that each depend on the previous graph.
"""
# Create a random graph
graph = nx.fast_gnp_random_graph(n=n_base_nodes, p=base_density)

# add one to each node to avoid the perfect case where true_ids match int_ids
graph = nx.relabel_nodes(graph, mapping={n: n + 1 for n in graph.nodes()})
graph = nx.relabel_nodes(graph, mapping={n: str(n) for n in graph.nodes()})

# initialize graphs list with first graph
graphs = [graph.copy()]
Expand All @@ -38,9 +39,9 @@ def generate_dynamic_graphs(
graph.remove_node(node)

# add some more nodes
node_idx = max(graph.nodes) + 1
node_idx = max(map(int, graph.nodes)) + 1
for i in range(2 * change_size):
graph.add_node(node_idx + i)
graph.add_node(str(node_idx + i))

# add some edges for the new nodes
for edge in zip(
Expand Down
15 changes: 9 additions & 6 deletions tests/test_biased_random_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@

@pytest.fixture(name="graphs")
def fixture_graphs():
return dynnode2vec.utils.generate_dynamic_graphs(
return dynnode2vec.utils.create_dynamic_graph(
n_base_nodes=30, n_steps=5, base_density=0.02
)


def add_random_weights(graph):
for *_, data in graph.edges(data=True):
data["weight"] = random.random()


def test_init(graphs):
brw = dynnode2vec.biased_random_walk.BiasedRandomWalk(graphs[0])

Expand Down Expand Up @@ -46,12 +51,11 @@ def test_generate_walk(graphs, ip, iq, weighted):
# pylint: disable=invalid-name
# make sure that tested node has at least one neighbor
graph = graphs[0]
graph.add_edge(0, 1, weight=0.5)
graph.add_edge("0", "1")

# add random weights to the graph for the weighted case
if weighted:
for _, _, data in graph.edges(data=True):
data["weight"] = random.random()
add_random_weights(graph)

brw = dynnode2vec.biased_random_walk.BiasedRandomWalk(graph)
rng = random.Random(0)
Expand All @@ -74,8 +78,7 @@ def test_run(graphs, p, q, weighted):

# add random weights to the graph for the weighted case
if weighted:
for *_, data in graph.edges(data=True):
data["weight"] = random.random()
add_random_weights(graph)

brw = dynnode2vec.biased_random_walk.BiasedRandomWalk(graph)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_dynnode2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@pytest.fixture(name="graphs")
def fixture_graphs():
return dynnode2vec.utils.generate_dynamic_graphs(
return dynnode2vec.utils.create_dynamic_graph(
n_base_nodes=30, n_steps=5, base_density=0.02
)

Expand Down

0 comments on commit a255708

Please sign in to comment.