Skip to content

Commit

Permalink
wrapped node names to regulate/even out the node sizes by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanovaos committed Sep 16, 2024
1 parent 7ad14a7 commit 2a5e36b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
10 changes: 5 additions & 5 deletions networkcommons/visual/_aux.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ def adjust_node_name(node_name: str,
if new_node_name != node_name:
_log(f"Replaced special characters in '{node_name}' with underscores.", level=10)

# If the modified node name is longer than the specified maximum length, truncate it
if truncate and len(new_node_name) > max_length:
new_node_name = new_node_name[:max_length] + ".."
_log(f"Truncated node name '{node_name}' to '{new_node_name}'.", level=10)

# If the wrap flag is set to True, wrap the node name
if wrap:
new_node_name = "\n".join([new_node_name[i:i + wrap_length]
for i in range(0, len(new_node_name), wrap_length)])

# If the modified node name is longer than the specified maximum length, truncate it
if truncate and len(new_node_name) > max_length:
new_node_name = new_node_name[:max_length] + "..."
_log(f"Truncated node name '{node_name}' to '{new_node_name}'.", level=10)

# If multiple underscores are present, replace them with a single underscore
new_node_name = re.sub(r'_+', '_', new_node_name)

Expand Down
33 changes: 32 additions & 1 deletion networkcommons/visual/_vis_networkx.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,38 @@ def color_edges(self):

edge_data['color'] = color

def adjust_node_labels(self,
wrap: bool = True,
truncate: bool = False,
max_length: int = 6,
wrap_length: int = 6):
"""
Adjust node labels using the adjust_node_name function.
Args:
wrap (bool): Whether to wrap the node labels.
truncate (bool): Whether to truncate the node labels.
max_length (int): The maximum length of node labels before truncation.
wrap_length (int): The length at which to wrap the node labels.
"""
for node in self.network.nodes():
node_data = self.network.nodes[node]
adjusted_name = _aux.adjust_node_name(node,
truncate=truncate,
wrap=wrap,
max_length=max_length,
wrap_length=wrap_length)
node_data['label'] = adjusted_name
print(adjusted_name)


def visualize_network_default(self,
source_dict,
target_dict,
prog='dot',
custom_style=None,
max_nodes=75):
max_nodes=75,
wrap_names=True):
"""
Visualizes the network using default styles.
Expand All @@ -243,6 +269,7 @@ def visualize_network_default(self,
prog (str, optional): Layout program to use. Defaults to 'dot'.
custom_style (dict, optional): Custom style dictionary to apply.
max_nodes (int, optional): Maximum number of nodes to visualize. Defaults to 75.
wrap_names (bool, optional): Whether to wrap node names. Defaults to True.
Returns:
A (pygraphviz.AGraph): The visualized network graph.
Expand All @@ -262,6 +289,10 @@ def visualize_network_default(self,
A = nx.nx_agraph.to_agraph(self.network)
A.graph_attr['ratio'] = '1.2'

# Adjust node labels before visualization
if wrap_names:
self.adjust_node_labels(wrap=True, truncate=True)

if source_dict:
sources = set(source_dict.keys())
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_vis_aux.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_truncate_node_name():
"VeryLongNodeName",
truncate=True,
max_length=8
) == "VeryLong.."
) == "VeryLong..."


def test_wrap_node_name():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_vis_networkx.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
def sample_network():
# Create a simple directed graph with some nodes and edges
G = nx.DiGraph()
G.add_edge(1, 2, interaction=1)
G.add_edge(2, 3, interaction=-1)
G.add_edge(3, 4, interaction=-1)
G.add_edge(4, 5, interaction=-1)
G.add_edge("1", "2", interaction=1)
G.add_edge("2", "3", interaction=-1)
G.add_edge("3", "4", interaction=-1)
G.add_edge("4", "5", interaction=-1)
return G


Expand Down

0 comments on commit 2a5e36b

Please sign in to comment.