-
Notifications
You must be signed in to change notification settings - Fork 97
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
Handle skeleton encoding internally #1970
Changes from all commits
034cffb
dd4865d
f66a12d
7c48903
4a75924
57ec9d3
58b2398
8aedebd
b9973c2
4d77a68
6c96a0e
c78e9a9
5935b6a
cab40c2
2068fdd
61c7cc0
c4f5be9
839d67c
5bcea83
167ef77
28f0c61
fd14ad0
7fa9517
1d98177
949fbe6
6490d1f
c199061
345dbc0
4a8c326
c57e64d
4c8bdd6
6444378
c232ae6
2a56e88
5319e5a
b1d757b
e7fb00a
743e406
02865f8
83a2704
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,62 @@ | ||
import copy | ||
import os | ||
|
||
import pytest | ||
import json | ||
|
||
from networkx.readwrite import json_graph | ||
from sleap.skeleton import Skeleton, SkeletonDecoder | ||
from sleap.skeleton import SkeletonEncoder | ||
|
||
|
||
def test_decoded_encoded_Skeleton_from_load_json(fly_legs_skeleton_json): | ||
""" | ||
Test Skeleton decoded from SkeletonEncoder.encode matches the original Skeleton. | ||
""" | ||
# Get the skeleton from the fixture | ||
skeleton = Skeleton.load_json(fly_legs_skeleton_json) | ||
# Get the graph from the skeleton | ||
indexed_node_graph = skeleton._graph | ||
graph = json_graph.node_link_data(indexed_node_graph) | ||
|
||
# Encode the graph as a json string to test .encode method | ||
encoded_json_str = SkeletonEncoder.encode(graph) | ||
|
||
# Get the skeleton from the encoded json string | ||
decoded_skeleton = Skeleton.from_json(encoded_json_str) | ||
|
||
# Check that the decoded skeleton is the same as the original skeleton | ||
assert skeleton.matches(decoded_skeleton) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"skeleton_fixture_name", ["flies13_skeleton", "skeleton", "stickman"] | ||
) | ||
def test_decoded_encoded_Skeleton(skeleton_fixture_name, request): | ||
""" | ||
Test Skeleton decoded from SkeletonEncoder.encode matches the original Skeleton. | ||
""" | ||
# Use request.getfixturevalue to get the actual fixture value by name | ||
skeleton = request.getfixturevalue(skeleton_fixture_name) | ||
|
||
# Get the graph from the skeleton | ||
indexed_node_graph = skeleton._graph | ||
graph = json_graph.node_link_data(indexed_node_graph) | ||
|
||
# Encode the graph as a json string to test .encode method | ||
encoded_json_str = SkeletonEncoder.encode(graph) | ||
|
||
# Get the skeleton from the encoded json string | ||
decoded_skeleton = Skeleton.from_json(encoded_json_str) | ||
|
||
# Check that the decoded skeleton is the same as the original skeleton | ||
assert skeleton.matches(decoded_skeleton) | ||
|
||
# Now make everything into a JSON string | ||
skeleton_json_str = skeleton.to_json() | ||
decoded_skeleton_json_str = decoded_skeleton.to_json() | ||
|
||
# Check that the JSON strings are the same | ||
assert json.loads(skeleton_json_str) == json.loads(decoded_skeleton_json_str) | ||
Comment on lines
+31
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance parameterized test with additional assertions and error messages The test function effectively verifies that the encoded and decoded Skeleton matches the original across multiple fixtures. To further improve its robustness:
Consider enhancing the test function as follows: @pytest.mark.parametrize(
"skeleton_fixture_name", ["flies13_skeleton", "skeleton", "stickman"]
)
def test_decoded_encoded_Skeleton(skeleton_fixture_name, request):
skeleton = request.getfixturevalue(skeleton_fixture_name)
indexed_node_graph = skeleton._graph
graph = json_graph.node_link_data(indexed_node_graph)
encoded_json_str = SkeletonEncoder.encode(graph)
assert encoded_json_str, f"Encoded JSON string for {skeleton_fixture_name} should not be empty"
decoded_skeleton = Skeleton.from_json(encoded_json_str)
assert skeleton.matches(decoded_skeleton), f"Decoded {skeleton_fixture_name} should match the original"
assert len(skeleton.nodes) == len(decoded_skeleton.nodes), f"Number of nodes in {skeleton_fixture_name} should match"
assert len(skeleton.edges) == len(decoded_skeleton.edges), f"Number of edges in {skeleton_fixture_name} should match"
assert set(n.name for n in skeleton.nodes) == set(n.name for n in decoded_skeleton.nodes), f"Node names in {skeleton_fixture_name} should match"
assert set(skeleton.edge_names) == set(decoded_skeleton.edge_names), f"Edge connections in {skeleton_fixture_name} should match"
skeleton_json_str = skeleton.to_json()
decoded_skeleton_json_str = decoded_skeleton.to_json()
assert json.loads(skeleton_json_str) == json.loads(decoded_skeleton_json_str), f"JSON representations of {skeleton_fixture_name} should match" These changes will provide more detailed information if a test fails, making it easier to identify and fix issues. |
||
|
||
|
||
def test_add_dupe_node(skeleton): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance test coverage with additional assertions
The test function effectively verifies that the encoded and decoded Skeleton matches the original. However, we can improve it by adding more specific assertions:
Consider adding these assertions to strengthen the test: