Skip to content

Commit

Permalink
Introduce naive layout algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipMathieu committed Apr 2, 2024
1 parent 9b51e22 commit bb47ac2
Show file tree
Hide file tree
Showing 3 changed files with 680 additions and 135 deletions.
746 changes: 616 additions & 130 deletions notebooks/tests.ipynb

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@
from .core.metricssuite import MetricsSuite
from .core.readabilitygraph import ReadabilityGraph

import utils.helpers as helpers
import utils.crosses_promotion as crosses_promotion
# Import layout algorithms
from .layout.layout_algorithms import naive_optimizer

# Import utils
from .utils import helpers
from .utils import crosses_promotion

# Import tests
import tests
from . import tests

__all__ = ["MetricsSuite", "ReadabilityGraph", "helpers", "crosses_promotion", "tests"]
__all__ = [
"MetricsSuite",
"ReadabilityGraph",
"naive_optimizer",
"helpers",
"crosses_promotion",
"tests",
]
50 changes: 49 additions & 1 deletion src/layout/layout_algorithms.py
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
# Module for different graph layout algorithms (to be implemented).
import networkx as nx
from ..core.metricssuite import MetricsSuite


def naive_optimizer(M_input: MetricsSuite, inplace=False):
"""Naive optimizer for the graph layout.
This optimizer generates layouts based on four NetworkX graph layout algorithms:
- spring_layout
- shell_layout
- circular_layout
- kamada_kawai_layout
Then chooses the layout that maximizes the MetricSuite score.
Parameters
----------
M : MetricSuite
The MetricSuite object to be optimized.
inplace : bool, optional
Whether to modify the MetricSuite object in place or return a new one. Default is False.
Returns
-------
MetricSuite
The optimized MetricSuite object.
"""
layouts = {
"spring": nx.spring_layout,
"shell": nx.shell_layout,
"circular": nx.circular_layout,
"kamada_kawai": nx.kamada_kawai_layout,
}

scores = {}
poss = {}
for layout_name, layout_func in layouts.items():
M = M_input.copy() if not inplace else M_input
poss[layout_name] = layout_func(M._graph)
M.apply_layout(poss[layout_name])
M.calculate_metrics()
scores[layout_name] = M.combine_metrics()

# Choose the best layout
best_layout = max(scores, key=scores.get)
M = M_input.copy() if not inplace else M_input
M.apply_layout(poss[best_layout])

print(f"Best layout: {best_layout}. Score: {scores[best_layout]}")
return M

0 comments on commit bb47ac2

Please sign in to comment.