Skip to content

Commit a255708

Browse files
authored
Merge pull request #16 from pedugnat/str-ids
Use str for node ids + generate_dynamic_graphs rename
2 parents bf5da42 + f849940 commit a255708

File tree

6 files changed

+21
-17
lines changed

6 files changed

+21
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ pip install -U dynnode2vec
3131
import pickle
3232

3333
from dynnode2vec.dynnode2vec import DynNode2Vec
34-
from dynnode2vec.utils import generate_dynamic_graphs
34+
from dynnode2vec.utils import create_dynamic_graph
3535

3636
# Create random graphs
37-
graphs = generate_dynamic_graphs(
37+
graphs = create_dynamic_graph(
3838
n_base_nodes=100, n_steps=50, base_density=0.05
3939
)
4040

dynnode2vec/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from .biased_random_walk import BiasedRandomWalk, RandomWalks
99
from .dynnode2vec import DynNode2Vec, Embedding
10-
from .utils import generate_dynamic_graphs
10+
from .utils import create_dynamic_graph
1111

1212

1313
def get_version() -> str:

dynnode2vec/biased_random_walk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self, graph: nx.Graph) -> None:
2626
:param graph: graph to run walk on
2727
"""
2828
self.graph = nx.convert_node_labels_to_integers(
29-
graph, ordering="sorted", label_attribute="true_label"
29+
graph, ordering="default", label_attribute="true_label"
3030
)
3131

3232
self.mapping: Dict[int, Any] = nx.get_node_attributes(self.graph, "true_label")

dynnode2vec/utils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@
1010

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

1717

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

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

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

4041
# add some more nodes
41-
node_idx = max(graph.nodes) + 1
42+
node_idx = max(map(int, graph.nodes)) + 1
4243
for i in range(2 * change_size):
43-
graph.add_node(node_idx + i)
44+
graph.add_node(str(node_idx + i))
4445

4546
# add some edges for the new nodes
4647
for edge in zip(

tests/test_biased_random_walk.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212

1313
@pytest.fixture(name="graphs")
1414
def fixture_graphs():
15-
return dynnode2vec.utils.generate_dynamic_graphs(
15+
return dynnode2vec.utils.create_dynamic_graph(
1616
n_base_nodes=30, n_steps=5, base_density=0.02
1717
)
1818

1919

20+
def add_random_weights(graph):
21+
for *_, data in graph.edges(data=True):
22+
data["weight"] = random.random()
23+
24+
2025
def test_init(graphs):
2126
brw = dynnode2vec.biased_random_walk.BiasedRandomWalk(graphs[0])
2227

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

5156
# add random weights to the graph for the weighted case
5257
if weighted:
53-
for _, _, data in graph.edges(data=True):
54-
data["weight"] = random.random()
58+
add_random_weights(graph)
5559

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

7579
# add random weights to the graph for the weighted case
7680
if weighted:
77-
for *_, data in graph.edges(data=True):
78-
data["weight"] = random.random()
81+
add_random_weights(graph)
7982

8083
brw = dynnode2vec.biased_random_walk.BiasedRandomWalk(graph)
8184

tests/test_dynnode2vec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@pytest.fixture(name="graphs")
1414
def fixture_graphs():
15-
return dynnode2vec.utils.generate_dynamic_graphs(
15+
return dynnode2vec.utils.create_dynamic_graph(
1616
n_base_nodes=30, n_steps=5, base_density=0.02
1717
)
1818

0 commit comments

Comments
 (0)