scirs2-python 0.4.3

Python bindings for SciRS2 - A comprehensive scientific computing library in Rust (SciPy alternative)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
"""Tests for scirs2 graph algorithms module."""

import numpy as np
import pytest
import scirs2


class TestGraphCreation:
    """Test graph creation functions."""

    def test_graph_from_edges_basic(self):
        """Test creating undirected graph from edge list."""
        edges = [(0, 1), (1, 2), (2, 3)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=4)

        assert graph["num_nodes"] == 4
        assert graph["num_edges"] == 3
        assert graph["directed"] is False
        assert "adjacency" in graph

    def test_graph_from_edges_weighted(self):
        """Test creating weighted graph."""
        edges = [(0, 1, 1.5), (1, 2, 2.0), (2, 3, 0.5)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=4)

        assert graph["num_nodes"] == 4
        assert graph["num_edges"] == 3

        # Check that weights are preserved
        adj = graph["adjacency"]
        assert len(adj[0]) > 0  # Node 0 has neighbors
        assert len(adj[1]) > 0  # Node 1 has neighbors

    def test_digraph_from_edges_basic(self):
        """Test creating directed graph from edge list."""
        edges = [(0, 1), (1, 2), (2, 0)]
        graph = scirs2.digraph_from_edges_py(edges, num_nodes=3)

        assert graph["num_nodes"] == 3
        assert graph["num_edges"] == 3
        assert graph["directed"] is True

    def test_graph_from_edges_auto_nodes(self):
        """Test automatic node count detection."""
        edges = [(0, 5), (1, 3), (2, 4)]
        graph = scirs2.graph_from_edges_py(edges)

        # Should auto-detect at least 6 nodes (0-5)
        assert graph["num_nodes"] >= 6


class TestGraphGenerators:
    """Test graph generator functions."""

    def test_complete_graph(self):
        """Test complete graph generation."""
        graph = scirs2.complete_graph_py(5)

        assert graph["num_nodes"] == 5
        # Complete graph K_n has n(n-1)/2 edges
        assert graph["num_edges"] == 10  # 5*4/2

    def test_path_graph(self):
        """Test path graph generation."""
        graph = scirs2.path_graph_py(6)

        assert graph["num_nodes"] == 6
        assert graph["num_edges"] == 5  # n-1 edges in path

    def test_cycle_graph(self):
        """Test cycle graph generation."""
        graph = scirs2.cycle_graph_py(5)

        assert graph["num_nodes"] == 5
        assert graph["num_edges"] == 5  # n edges in cycle

    def test_star_graph(self):
        """Test star graph generation."""
        graph = scirs2.star_graph_py(4)

        assert graph["num_nodes"] == 5  # Center + 4 leaves
        assert graph["num_edges"] == 4

    def test_erdos_renyi_graph(self):
        """Test Erdős-Rényi random graph."""
        graph = scirs2.erdos_renyi_graph_py(10, 0.3, seed=42)

        assert graph["num_nodes"] == 10
        # With probability 0.3, expect around 13-14 edges for K_10
        assert 0 <= graph["num_edges"] <= 45  # Max possible edges

    def test_barabasi_albert_graph(self):
        """Test Barabási-Albert scale-free graph."""
        graph = scirs2.barabasi_albert_graph_py(20, 2, seed=42)

        assert graph["num_nodes"] == 20
        # Each node adds m edges (after initial nodes)
        assert graph["num_edges"] >= 18  # Approximately 2*(n-2) edges

    def test_watts_strogatz_graph(self):
        """Test Watts-Strogatz small-world graph."""
        graph = scirs2.watts_strogatz_graph_py(10, 4, 0.1, seed=42)

        assert graph["num_nodes"] == 10
        # Ring lattice with k=4 has 20 edges (10*4/2)
        assert 15 <= graph["num_edges"] <= 25


class TestShortestPaths:
    """Test shortest path algorithms."""

    def test_dijkstra_path_simple(self):
        """Test Dijkstra's shortest path on simple graph."""
        edges = [(0, 1, 1.0), (1, 2, 1.0), (0, 2, 5.0)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=3)

        result = scirs2.dijkstra_path_py(graph, 0, 2)

        assert result["success"]
        assert result["distance"] == pytest.approx(2.0, abs=1e-10)
        assert result["path"] == [0, 1, 2]

    def test_dijkstra_path_no_path(self):
        """Test Dijkstra when no path exists."""
        edges = [(0, 1), (2, 3)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=4)

        result = scirs2.dijkstra_path_py(graph, 0, 3)

        assert result["success"] is False or result["distance"] == float('inf')

    def test_floyd_warshall_basic(self):
        """Test Floyd-Warshall all-pairs shortest paths."""
        edges = [(0, 1, 1.0), (1, 2, 2.0), (0, 2, 5.0)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=3)

        result = scirs2.floyd_warshall_py(graph)

        assert "distances" in result
        distances = result["distances"]

        # Check dimensions
        assert distances.shape == (3, 3)
        # Check diagonal is zero
        assert np.allclose(np.diag(distances), [0, 0, 0])
        # Check shortest path 0->2 is through 1
        assert distances[0, 2] == pytest.approx(3.0, abs=1e-10)


class TestConnectivity:
    """Test graph connectivity algorithms."""

    def test_connected_components_single(self):
        """Test finding connected components in connected graph."""
        edges = [(0, 1), (1, 2), (2, 3)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=4)

        result = scirs2.connected_components_py(graph)

        assert result["num_components"] == 1
        assert len(result["components"]) == 1

    def test_connected_components_multiple(self):
        """Test finding multiple connected components."""
        edges = [(0, 1), (2, 3), (4, 5)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=6)

        result = scirs2.connected_components_py(graph)

        assert result["num_components"] == 3

    def test_strongly_connected_components(self):
        """Test strongly connected components in directed graph."""
        edges = [(0, 1), (1, 2), (2, 0), (3, 4)]
        graph = scirs2.digraph_from_edges_py(edges, num_nodes=5)

        result = scirs2.strongly_connected_components_py(graph)

        assert result["num_components"] >= 2
        assert len(result["components"]) >= 2

    def test_is_bipartite_true(self):
        """Test bipartite detection on bipartite graph."""
        # Path graph is bipartite
        graph = scirs2.path_graph_py(4)

        result = scirs2.is_bipartite_py(graph)

        assert result["is_bipartite"] is True

    def test_is_bipartite_false(self):
        """Test bipartite detection on non-bipartite graph."""
        # Cycle of odd length is not bipartite
        graph = scirs2.cycle_graph_py(5)

        result = scirs2.is_bipartite_py(graph)

        assert result["is_bipartite"] is False

    def test_articulation_points(self):
        """Test finding articulation points."""
        # Bridge graph: 0-1-2 where 1 is articulation point
        edges = [(0, 1), (1, 2)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=3)

        result = scirs2.articulation_points_py(graph)

        assert 1 in result["points"]

    def test_bridges(self):
        """Test finding bridges in graph."""
        edges = [(0, 1), (1, 2), (2, 3)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=4)

        result = scirs2.bridges_py(graph)

        # All edges in path are bridges
        assert len(result["bridges"]) >= 1


class TestTraversal:
    """Test graph traversal algorithms."""

    def test_breadth_first_search(self):
        """Test BFS traversal."""
        edges = [(0, 1), (0, 2), (1, 3), (2, 4)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=5)

        result = scirs2.breadth_first_search_py(graph, 0)

        assert "order" in result
        assert len(result["order"]) == 5
        assert result["order"][0] == 0  # Start node

    def test_depth_first_search(self):
        """Test DFS traversal."""
        edges = [(0, 1), (0, 2), (1, 3), (2, 4)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=5)

        result = scirs2.depth_first_search_py(graph, 0)

        assert "order" in result
        assert len(result["order"]) == 5
        assert result["order"][0] == 0  # Start node


class TestCentrality:
    """Test centrality measures."""

    def test_betweenness_centrality(self):
        """Test betweenness centrality calculation."""
        # Star graph: center has high betweenness
        graph = scirs2.star_graph_py(4)

        result = scirs2.betweenness_centrality_py(graph)

        assert "centrality" in result
        centrality = result["centrality"]
        # Center node (index 0) should have highest centrality
        assert centrality[0] > 0

    def test_closeness_centrality(self):
        """Test closeness centrality calculation."""
        edges = [(0, 1), (1, 2), (2, 3)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=4)

        result = scirs2.closeness_centrality_py(graph)

        assert "centrality" in result
        centrality = result["centrality"]
        # All nodes should have some centrality
        assert all(c >= 0 for c in centrality)

    def test_pagerank_centrality(self):
        """Test PageRank centrality."""
        edges = [(0, 1), (1, 2), (2, 0), (1, 3)]
        graph = scirs2.digraph_from_edges_py(edges, num_nodes=4)

        result = scirs2.pagerank_centrality_py(graph)

        assert "pagerank" in result
        pagerank = result["pagerank"]
        # PageRank values should sum to approximately 1
        assert 0.9 <= sum(pagerank) <= 1.1
        # All values should be positive
        assert all(p > 0 for p in pagerank)


class TestSpanningTree:
    """Test minimum spanning tree algorithms."""

    def test_minimum_spanning_tree(self):
        """Test MST calculation."""
        edges = [(0, 1, 1.0), (1, 2, 2.0), (0, 2, 5.0), (2, 3, 1.5)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=4)

        result = scirs2.minimum_spanning_tree_py(graph)

        assert "edges" in result
        assert "total_weight" in result
        # MST of n nodes has n-1 edges
        assert len(result["edges"]) == 3
        # MST weight should be minimal
        assert result["total_weight"] < 10.0


class TestCommunityDetection:
    """Test community detection algorithms."""

    def test_louvain_communities(self):
        """Test Louvain community detection."""
        # Create graph with clear communities
        edges = [
            (0, 1), (1, 2), (0, 2),  # Community 1
            (3, 4), (4, 5), (3, 5),  # Community 2
            (2, 3),  # Bridge
        ]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=6)

        result = scirs2.louvain_communities_py(graph)

        assert "communities" in result
        assert "num_communities" in result
        assert result["num_communities"] >= 1

    def test_label_propagation(self):
        """Test label propagation community detection."""
        edges = [
            (0, 1), (1, 2), (0, 2),  # Community 1
            (3, 4), (4, 5), (3, 5),  # Community 2
        ]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=6)

        result = scirs2.label_propagation_py(graph, seed=42)

        assert "communities" in result
        assert len(result["communities"]) == 6

    def test_modularity(self):
        """Test modularity calculation."""
        edges = [(0, 1), (1, 2), (3, 4)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=5)
        communities = [0, 0, 0, 1, 1]

        result = scirs2.modularity_py(graph, communities)

        assert "modularity" in result
        # Modularity should be between -1 and 1
        assert -1.0 <= result["modularity"] <= 1.0


class TestGraphMeasures:
    """Test graph property measurements."""

    def test_clustering_coefficient(self):
        """Test clustering coefficient calculation."""
        # Triangle graph has clustering coefficient of 1
        edges = [(0, 1), (1, 2), (2, 0)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=3)

        result = scirs2.clustering_coefficient_py(graph)

        assert "average" in result or "clustering" in result
        # Triangle graph should have high clustering
        if "average" in result:
            assert result["average"] > 0.5

    def test_diameter(self):
        """Test graph diameter calculation."""
        # Path graph of length 4 has diameter 3
        graph = scirs2.path_graph_py(4)

        result = scirs2.diameter_py(graph)

        assert "diameter" in result
        assert result["diameter"] == 3


class TestEdgeCases:
    """Test edge cases and error handling."""

    def test_empty_graph(self):
        """Test operations on empty graph."""
        graph = scirs2.graph_from_edges_py([], num_nodes=0)

        assert graph["num_nodes"] == 0
        assert graph["num_edges"] == 0

    def test_single_node_graph(self):
        """Test graph with single node."""
        graph = scirs2.graph_from_edges_py([], num_nodes=1)

        assert graph["num_nodes"] == 1
        assert graph["num_edges"] == 0

    def test_self_loop(self):
        """Test graph with self-loop."""
        edges = [(0, 0), (0, 1)]
        graph = scirs2.graph_from_edges_py(edges, num_nodes=2)

        assert graph["num_nodes"] == 2


if __name__ == "__main__":
    pytest.main([__file__, "-v"])