|
| 1 | +"""Tests for vesskel.graphml GraphML export.""" |
| 2 | + |
| 3 | +import networkx as nx |
| 4 | +import numpy as np |
| 5 | +from skan import Skeleton |
| 6 | + |
| 7 | +from vesskel.graphml import build_networkx_graph, write_graphml |
| 8 | + |
| 9 | + |
| 10 | +def _junction_endpoint_count(graph: Skeleton) -> int: |
| 11 | + """Number of skan nodes that are junctions or endpoints (degree != 2).""" |
| 12 | + return int((graph.degrees != 2).sum()) |
| 13 | + |
| 14 | + |
| 15 | +class TestBuildNetworkxGraph: |
| 16 | + def test_nodes_are_junctions_or_endpoints(self, cross_graph): |
| 17 | + graph, branch_data = cross_graph |
| 18 | + G = build_networkx_graph(graph, branch_data) |
| 19 | + |
| 20 | + assert isinstance(G, nx.MultiGraph) |
| 21 | + assert G.number_of_nodes() == _junction_endpoint_count(graph) |
| 22 | + assert G.number_of_edges() == len(branch_data) |
| 23 | + assert all(int(graph.degrees[n]) != 2 for n in G.nodes()) |
| 24 | + |
| 25 | + def test_node_attributes(self, cross_graph): |
| 26 | + graph, branch_data = cross_graph |
| 27 | + G = build_networkx_graph(graph, branch_data) |
| 28 | + |
| 29 | + node_id, data = next(iter(G.nodes(data=True))) |
| 30 | + assert data["coord_0"] == int(graph.coordinates[node_id, 0]) |
| 31 | + assert data["coord_1"] == int(graph.coordinates[node_id, 1]) |
| 32 | + assert data["degree"] == int(graph.degrees[node_id]) |
| 33 | + |
| 34 | + endpoints = [data for _, data in G.nodes(data=True) if data["is_endpoint"]] |
| 35 | + assert len(endpoints) >= 2 |
| 36 | + |
| 37 | + def test_node_radius_absent_without_radius_matrix(self, cross_graph): |
| 38 | + graph, branch_data = cross_graph |
| 39 | + G = build_networkx_graph(graph, branch_data) |
| 40 | + assert all("radius" not in data for _, data in G.nodes(data=True)) |
| 41 | + |
| 42 | + def test_node_radius_sampled_when_provided(self, cross_graph, cross_skel): |
| 43 | + graph, branch_data = cross_graph |
| 44 | + radius_matrix = np.full(cross_skel.shape, 2.5, dtype=np.float64) |
| 45 | + G = build_networkx_graph(graph, branch_data, radius_matrix=radius_matrix) |
| 46 | + for node_id, data in G.nodes(data=True): |
| 47 | + assert data["radius"] == 2.5 |
| 48 | + assert data["radius"] == float( |
| 49 | + radius_matrix[tuple(graph.coordinates[node_id])] |
| 50 | + ) |
| 51 | + |
| 52 | + def test_edge_attributes_and_exclusions(self, cross_graph): |
| 53 | + graph, branch_data = cross_graph |
| 54 | + G = build_networkx_graph(graph, branch_data) |
| 55 | + |
| 56 | + _, _, data = next(iter(G.edges(data=True))) |
| 57 | + assert "branch-distance" in data |
| 58 | + assert "euclidean-distance" in data |
| 59 | + assert "node-id-src" not in data |
| 60 | + assert "node-id-dst" not in data |
| 61 | + assert not any(k.startswith("coord-") for k in data) |
| 62 | + assert not any(k.startswith("image-coord-") for k in data) |
| 63 | + |
| 64 | + def test_parallel_branches_preserved(self, loop_graph): |
| 65 | + graph, branch_data = loop_graph |
| 66 | + pairs = list(zip(branch_data["node-id-src"], branch_data["node-id-dst"])) |
| 67 | + assert len(pairs) > len(set(pairs)) # fixture has parallel branches |
| 68 | + |
| 69 | + G = build_networkx_graph(graph, branch_data) |
| 70 | + assert G.number_of_edges() == len(pairs) |
| 71 | + |
| 72 | + def test_nan_attributes_dropped(self, cross_graph): |
| 73 | + graph, branch_data = cross_graph |
| 74 | + branch_data = branch_data.copy() |
| 75 | + branch_data["tortuosity"] = np.nan |
| 76 | + branch_data["straightness"] = np.inf |
| 77 | + G = build_networkx_graph(graph, branch_data) |
| 78 | + |
| 79 | + for _, _, data in G.edges(data=True): |
| 80 | + assert "tortuosity" not in data |
| 81 | + assert "straightness" not in data |
| 82 | + |
| 83 | + def test_summary_features_attached_and_nan_dropped(self, cross_graph): |
| 84 | + graph, branch_data = cross_graph |
| 85 | + G = build_networkx_graph( |
| 86 | + graph, |
| 87 | + branch_data, |
| 88 | + summary_features={"num_nodes": 5.0, "mean_tortuosity": np.nan}, |
| 89 | + ) |
| 90 | + assert G.graph["num_nodes"] == 5.0 |
| 91 | + assert "mean_tortuosity" not in G.graph |
| 92 | + |
| 93 | + def test_3d_skeleton(self, cross_volume_graph): |
| 94 | + graph, branch_data = cross_volume_graph |
| 95 | + G = build_networkx_graph(graph, branch_data) |
| 96 | + |
| 97 | + assert G.number_of_nodes() == _junction_endpoint_count(graph) |
| 98 | + _, data = next(iter(G.nodes(data=True))) |
| 99 | + assert "coord_2" in data |
| 100 | + |
| 101 | + |
| 102 | +class TestWriteGraphml: |
| 103 | + def test_write_and_read_round_trip(self, tmp_path, cross_graph, cross_skel): |
| 104 | + graph, branch_data = cross_graph |
| 105 | + radius_matrix = np.full(cross_skel.shape, 3.0, dtype=np.float64) |
| 106 | + path = tmp_path / "img_graph.graphml" |
| 107 | + write_graphml( |
| 108 | + graph, |
| 109 | + branch_data, |
| 110 | + path, |
| 111 | + summary_features={"total_length": 42.0}, |
| 112 | + radius_matrix=radius_matrix, |
| 113 | + ) |
| 114 | + |
| 115 | + G = nx.read_graphml(str(path), node_type=int) |
| 116 | + assert G.number_of_nodes() == _junction_endpoint_count(graph) |
| 117 | + assert G.number_of_edges() == len(branch_data) |
| 118 | + assert G.graph["total_length"] == 42.0 |
| 119 | + |
| 120 | + node_id, data = next(iter(G.nodes(data=True))) |
| 121 | + assert data["coord_0"] == int(graph.coordinates[node_id, 0]) |
| 122 | + assert data["radius"] == 3.0 |
| 123 | + assert "degree" in data |
| 124 | + assert "branch-distance" in next(iter(G.edges(data=True)))[2] |
| 125 | + |
| 126 | + def test_parallel_edges_round_trip_as_multigraph(self, tmp_path, loop_graph): |
| 127 | + graph, branch_data = loop_graph |
| 128 | + path = tmp_path / "loop_graph.graphml" |
| 129 | + write_graphml(graph, branch_data, path) |
| 130 | + |
| 131 | + G = nx.read_graphml(str(path), node_type=int) |
| 132 | + assert isinstance(G, nx.MultiGraph) |
| 133 | + assert G.number_of_edges() == len(branch_data) |
| 134 | + |
| 135 | + def test_empty_branch_data(self, tmp_path, cross_graph): |
| 136 | + graph, branch_data = cross_graph |
| 137 | + path = tmp_path / "empty_graph.graphml" |
| 138 | + write_graphml(graph, branch_data.iloc[0:0], path) |
| 139 | + G = nx.read_graphml(str(path), node_type=int) |
| 140 | + assert G.number_of_nodes() == 0 |
| 141 | + assert G.number_of_edges() == 0 |
0 commit comments