-
Notifications
You must be signed in to change notification settings - Fork 11
Add resize_nodes feature to VG #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from typing import Union | ||
|
|
||
| RealNumber = Union[int, float] | ||
|
|
||
|
|
||
| def verify_radii(node_radius_min_max: tuple[RealNumber, RealNumber]) -> None: | ||
| if not isinstance(node_radius_min_max, tuple): | ||
| raise ValueError(f"`node_radius_min_max` must be a tuple of two values, but was {node_radius_min_max}") | ||
|
|
||
| if len(node_radius_min_max) != 2: | ||
| raise ValueError(f"`node_radius_min_max` must be a tuple of two values, but was {node_radius_min_max}") | ||
|
|
||
| min_size, max_size = node_radius_min_max | ||
| if not isinstance(min_size, (int, float)): | ||
| raise ValueError(f"Minimum node size must be a real number, but was of type {type(min_size)}") | ||
|
|
||
| if not isinstance(max_size, (int, float)): | ||
| raise ValueError(f"Maximum node size must be a real number, but was of type {type(max_size)}") | ||
|
|
||
| if min_size < 0: | ||
| raise ValueError(f"Minimum node size must be non-negative, but was {min_size}") | ||
|
|
||
| if max_size < 0: | ||
| raise ValueError(f"Maximum node size must be non-negative, but was {max_size}") | ||
|
|
||
| if min_size > max_size: | ||
| raise ValueError( | ||
| f"Minimum node size must be less than or equal to maximum node size, but was {min_size} > {max_size}" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import re | ||
|
|
||
| import pytest | ||
|
|
||
| from neo4j_viz import Node, VisualizationGraph | ||
| from neo4j_viz.node import NodeIdType | ||
| from neo4j_viz.node_size import RealNumber, verify_radii | ||
|
|
||
|
|
||
| def test_verify_radii() -> None: | ||
| with pytest.raises(ValueError, match="`node_radius_min_max` must be a tuple of two values, but was 3"): | ||
| verify_radii(3) # type: ignore | ||
|
|
||
| with pytest.raises( | ||
| ValueError, match=re.escape("`node_radius_min_max` must be a tuple of two values, but was (1, 2, 3)") | ||
| ): | ||
| verify_radii((1, 2, 3)) # type: ignore | ||
|
|
||
| with pytest.raises(ValueError, match="Minimum node size must be a real number, but was of type <class 'str'>"): | ||
| verify_radii(("1", 2)) # type: ignore | ||
|
|
||
| with pytest.raises(ValueError, match="Maximum node size must be a real number, but was of type <class 'str'>"): | ||
| verify_radii((1, "2")) # type: ignore | ||
|
|
||
| with pytest.raises(ValueError, match="Minimum node size must be non-negative, but was -1"): | ||
| verify_radii((-1, 2)) | ||
|
|
||
| with pytest.raises(ValueError, match="Maximum node size must be non-negative, but was -2"): | ||
| verify_radii((1, -2)) | ||
|
|
||
| with pytest.raises( | ||
| ValueError, match="Minimum node size must be less than or equal to maximum node size, but was 2 > 1" | ||
| ): | ||
| verify_radii((2, 1)) | ||
|
|
||
| # This should not raise an exception | ||
| verify_radii((1, 2)) | ||
|
|
||
|
|
||
| def test_resize_nodes_no_scaling() -> None: | ||
| nodes = [ | ||
| Node(id=42), | ||
| Node(id="1337", size=10), | ||
| ] | ||
| VG = VisualizationGraph(nodes=nodes, relationships=[]) | ||
|
|
||
| new_sizes: dict[NodeIdType, RealNumber] = {"1337": 20} | ||
| VG.resize_nodes(new_sizes, None) | ||
|
|
||
| assert VG.nodes[0].size is None | ||
| assert VG.nodes[1].size == 20 | ||
|
|
||
| new_sizes = {42: 8.1, "1337": 3} | ||
| VG.resize_nodes(new_sizes, None) | ||
|
|
||
| assert VG.nodes[0].size == 8.1 | ||
| assert VG.nodes[1].size == 3 | ||
|
|
||
| new_sizes = {42: -4.2} | ||
| with pytest.raises(ValueError, match="Size for node '42' must be non-negative, but was -4.2"): | ||
| VG.resize_nodes(new_sizes, None) | ||
|
|
||
|
|
||
| def test_resize_nodes_with_scaling_constant() -> None: | ||
| nodes = [ | ||
| Node(id=42), | ||
| Node(id="1337", size=10), | ||
| ] | ||
| VG = VisualizationGraph(nodes=nodes, relationships=[]) | ||
|
|
||
| new_sizes: dict[NodeIdType, RealNumber] = {"1337": 20} | ||
| VG.resize_nodes(new_sizes, (3, 60)) | ||
|
|
||
| assert VG.nodes[0].size is None | ||
| # Should just be the default since min == max in VG (only one node) | ||
| assert VG.nodes[1].size == 3 + (60 - 3) / 2.0 | ||
|
|
||
|
|
||
| def test_resize_nodes_with_scaling_all_sizes_provided() -> None: | ||
| nodes = [ | ||
| Node(id=42, size=10), | ||
| Node(id=43, size=10), | ||
| Node(id="1337", size=15), | ||
| ] | ||
| VG = VisualizationGraph(nodes=nodes, relationships=[]) | ||
|
|
||
| new_sizes: dict[NodeIdType, RealNumber] = {42: 18, 43: 19, "1337": 20} | ||
| VG.resize_nodes(new_sizes, (3, 60)) | ||
|
|
||
| assert VG.nodes[0].size == 3 | ||
| assert VG.nodes[1].size == 3 + (60 - 3) / 2.0 | ||
| assert VG.nodes[2].size == 60 | ||
|
|
||
|
|
||
| def test_resize_nodes_with_scaling_some_sizes_provided() -> None: | ||
| nodes = [ | ||
| Node(id=42, size=10), | ||
| Node(id="1337", size=15), | ||
| ] | ||
| VG = VisualizationGraph(nodes=nodes, relationships=[]) | ||
|
|
||
| new_sizes: dict[NodeIdType, RealNumber] = {"1337": 1} | ||
| VG.resize_nodes(new_sizes, (3, 60)) | ||
|
|
||
| assert VG.nodes[0].size == 60 | ||
| assert VG.nodes[1].size == 3 | ||
|
|
||
|
|
||
| def test_resize_nodes_with_scaling_only() -> None: | ||
| nodes = [ | ||
| Node(id=42, size=10), | ||
| Node(id=43, size=10), | ||
| Node(id="1337", size=15), | ||
| ] | ||
| VG = VisualizationGraph(nodes=nodes, relationships=[]) | ||
|
|
||
| VG.resize_nodes(node_radius_min_max=(3, 60)) | ||
|
|
||
| assert VG.nodes[0].size == 3 | ||
| assert VG.nodes[1].size == 3 | ||
| assert VG.nodes[2].size == 60 | ||
|
|
||
|
|
||
| def test_resize_nodes_no_args_failure() -> None: | ||
| VG = VisualizationGraph(nodes=[], relationships=[]) | ||
|
|
||
| with pytest.raises(ValueError, match="At least one of `sizes` and `node_radius_min_max` must be given"): | ||
| VG.resize_nodes(node_radius_min_max=None) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.