Skip to content

Commit bb47ac2

Browse files
committed
Introduce naive layout algorithm
1 parent 9b51e22 commit bb47ac2

File tree

3 files changed

+680
-135
lines changed

3 files changed

+680
-135
lines changed

notebooks/tests.ipynb

Lines changed: 616 additions & 130 deletions
Large diffs are not rendered by default.

src/__init__.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@
77
from .core.metricssuite import MetricsSuite
88
from .core.readabilitygraph import ReadabilityGraph
99

10-
import utils.helpers as helpers
11-
import utils.crosses_promotion as crosses_promotion
10+
# Import layout algorithms
11+
from .layout.layout_algorithms import naive_optimizer
12+
13+
# Import utils
14+
from .utils import helpers
15+
from .utils import crosses_promotion
1216

1317
# Import tests
14-
import tests
18+
from . import tests
1519

16-
__all__ = ["MetricsSuite", "ReadabilityGraph", "helpers", "crosses_promotion", "tests"]
20+
__all__ = [
21+
"MetricsSuite",
22+
"ReadabilityGraph",
23+
"naive_optimizer",
24+
"helpers",
25+
"crosses_promotion",
26+
"tests",
27+
]

src/layout/layout_algorithms.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,49 @@
1-
# Module for different graph layout algorithms (to be implemented).
1+
import networkx as nx
2+
from ..core.metricssuite import MetricsSuite
3+
4+
5+
def naive_optimizer(M_input: MetricsSuite, inplace=False):
6+
"""Naive optimizer for the graph layout.
7+
8+
This optimizer generates layouts based on four NetworkX graph layout algorithms:
9+
- spring_layout
10+
- shell_layout
11+
- circular_layout
12+
- kamada_kawai_layout
13+
Then chooses the layout that maximizes the MetricSuite score.
14+
15+
Parameters
16+
----------
17+
M : MetricSuite
18+
The MetricSuite object to be optimized.
19+
inplace : bool, optional
20+
Whether to modify the MetricSuite object in place or return a new one. Default is False.
21+
22+
Returns
23+
-------
24+
MetricSuite
25+
The optimized MetricSuite object.
26+
"""
27+
layouts = {
28+
"spring": nx.spring_layout,
29+
"shell": nx.shell_layout,
30+
"circular": nx.circular_layout,
31+
"kamada_kawai": nx.kamada_kawai_layout,
32+
}
33+
34+
scores = {}
35+
poss = {}
36+
for layout_name, layout_func in layouts.items():
37+
M = M_input.copy() if not inplace else M_input
38+
poss[layout_name] = layout_func(M._graph)
39+
M.apply_layout(poss[layout_name])
40+
M.calculate_metrics()
41+
scores[layout_name] = M.combine_metrics()
42+
43+
# Choose the best layout
44+
best_layout = max(scores, key=scores.get)
45+
M = M_input.copy() if not inplace else M_input
46+
M.apply_layout(poss[best_layout])
47+
48+
print(f"Best layout: {best_layout}. Score: {scores[best_layout]}")
49+
return M

0 commit comments

Comments
 (0)