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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
"""Tests for scirs2 asynchronous operations module."""

import numpy as np
import pytest
import scirs2
import asyncio


class TestAsyncBasics:
    """Test basic async operations."""

    def test_async_compute(self):
        """Test basic async computation."""
        async def compute():
            result = await scirs2.async_compute_py(lambda x: x ** 2, 5)
            return result

        result = asyncio.run(compute())
        assert result == 25

    def test_async_map(self):
        """Test async map operation."""
        async def map_func():
            data = [1, 2, 3, 4, 5]
            results = await scirs2.async_map_py(lambda x: x * 2, data)
            return results

        results = asyncio.run(map_func())
        assert results == [2, 4, 6, 8, 10]

    def test_async_gather(self):
        """Test gathering multiple async operations."""
        async def gather_tasks():
            tasks = [
                scirs2.async_compute_py(lambda x: x + 1, 1),
                scirs2.async_compute_py(lambda x: x + 2, 2),
                scirs2.async_compute_py(lambda x: x + 3, 3)
            ]
            results = await asyncio.gather(*tasks)
            return results

        results = asyncio.run(gather_tasks())
        assert results == [2, 4, 6]


class TestAsyncArrayOperations:
    """Test async array operations."""

    def test_async_matmul(self):
        """Test async matrix multiplication."""
        async def matmul():
            A = np.random.randn(10, 10)
            B = np.random.randn(10, 10)
            C = await scirs2.async_matmul_py(A, B)
            return C

        C = asyncio.run(matmul())
        assert C.shape == (10, 10)

    def test_async_sum(self):
        """Test async array sum."""
        async def sum_array():
            arr = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
            result = await scirs2.async_sum_py(arr)
            return result

        result = asyncio.run(sum_array())
        assert result == 15.0

    def test_async_reduce(self):
        """Test async reduce operation."""
        async def reduce_array():
            arr = np.array([1, 2, 3, 4, 5])
            result = await scirs2.async_reduce_py(arr, lambda x, y: x + y)
            return result

        result = asyncio.run(reduce_array())
        assert result == 15


class TestParallelComputation:
    """Test parallel computation utilities."""

    def test_parallel_for(self):
        """Test parallel for loop."""
        def square(x):
            return x ** 2

        data = [1, 2, 3, 4, 5]
        results = scirs2.parallel_for_py(square, data, n_workers=2)

        assert results == [1, 4, 9, 16, 25]

    def test_parallel_map(self):
        """Test parallel map."""
        def increment(x):
            return x + 1

        data = np.array([1, 2, 3, 4, 5])
        results = scirs2.parallel_map_py(increment, data, n_workers=2)

        assert np.allclose(results, [2, 3, 4, 5, 6])

    def test_parallel_reduce(self):
        """Test parallel reduce."""
        def add(x, y):
            return x + y

        data = [1, 2, 3, 4, 5]
        result = scirs2.parallel_reduce_py(add, data, n_workers=2)

        assert result == 15


class TestTaskQueue:
    """Test task queue operations."""

    def test_task_queue_basic(self):
        """Test basic task queue."""
        async def process_queue():
            queue = scirs2.TaskQueue()

            await queue.put(lambda: 1 + 1)
            await queue.put(lambda: 2 + 2)
            await queue.put(lambda: 3 + 3)

            results = []
            for i in range(3):
                result = await queue.get()
                results.append(result)

            return results

        results = asyncio.run(process_queue())
        assert 2 in results or len(results) == 3

    def test_task_queue_priority(self):
        """Test priority task queue."""
        async def priority_queue():
            queue = scirs2.PriorityTaskQueue()

            await queue.put(lambda: "low", priority=3)
            await queue.put(lambda: "high", priority=1)
            await queue.put(lambda: "medium", priority=2)

            results = []
            for i in range(3):
                result = await queue.get()
                results.append(result)

            return results

        results = asyncio.run(priority_queue())
        # High priority should be first
        assert results[0] == "high" or len(results) == 3


class TestAsyncDataLoading:
    """Test async data loading."""

    def test_async_load_batch(self):
        """Test async batch loading."""
        async def load_batches():
            data = np.arange(100)
            batches = await scirs2.async_load_batches_py(data, batch_size=10)
            return batches

        batches = asyncio.run(load_batches())
        assert len(batches) == 10

    def test_async_prefetch(self):
        """Test data prefetching."""
        async def prefetch_data():
            data = [np.random.randn(10, 10) for _ in range(5)]
            iterator = scirs2.async_prefetch_py(data, buffer_size=2)

            results = []
            async for item in iterator:
                results.append(item)

            return results

        results = asyncio.run(prefetch_data())
        assert len(results) == 5


class TestAsyncFileIO:
    """Test async file I/O."""

    def test_async_read_file(self):
        """Test async file reading."""
        import tempfile
        import os

        with tempfile.NamedTemporaryFile(delete=False, mode='w') as f:
            f.write("test content")
            tmp_path = f.name

        try:
            async def read_async():
                content = await scirs2.async_read_file_py(tmp_path)
                return content

            content = asyncio.run(read_async())
            assert "test" in content or len(content) > 0
        finally:
            os.remove(tmp_path)

    def test_async_write_file(self):
        """Test async file writing."""
        import tempfile
        import os

        tmp_path = tempfile.mktemp()

        try:
            async def write_async():
                await scirs2.async_write_file_py(tmp_path, "test data")

            asyncio.run(write_async())

            with open(tmp_path, 'r') as f:
                content = f.read()
                assert "test" in content
        finally:
            if os.path.exists(tmp_path):
                os.remove(tmp_path)


class TestAsyncStreaming:
    """Test async streaming operations."""

    def test_async_stream_process(self):
        """Test async stream processing."""
        async def process_stream():
            async def data_stream():
                for i in range(10):
                    yield i

            results = []
            async for item in scirs2.async_stream_process_py(data_stream(), lambda x: x * 2):
                results.append(item)

            return results

        results = asyncio.run(process_stream())
        assert len(results) == 10
        assert results[0] == 0
        assert results[-1] == 18

    def test_async_batch_stream(self):
        """Test async batch streaming."""
        async def batch_stream():
            async def data_stream():
                for i in range(20):
                    yield i

            batches = []
            async for batch in scirs2.async_batch_stream_py(data_stream(), batch_size=5):
                batches.append(batch)

            return batches

        batches = asyncio.run(batch_stream())
        assert len(batches) == 4


class TestAsyncPooling:
    """Test async worker pools."""

    def test_async_worker_pool(self):
        """Test async worker pool."""
        async def worker_pool():
            pool = scirs2.AsyncWorkerPool(n_workers=3)

            tasks = [lambda x=i: x ** 2 for i in range(10)]
            results = await pool.map(tasks)

            return results

        results = asyncio.run(worker_pool())
        assert len(results) == 10

    def test_async_thread_pool(self):
        """Test async thread pool executor."""
        async def thread_pool():
            def cpu_bound_task(x):
                return x ** 2

            results = await scirs2.async_thread_pool_py(cpu_bound_task, list(range(5)), n_threads=2)

            return results

        results = asyncio.run(thread_pool())
        assert results == [0, 1, 4, 9, 16]


class TestAsyncSynchronization:
    """Test async synchronization primitives."""

    def test_async_lock(self):
        """Test async lock."""
        async def use_lock():
            lock = asyncio.Lock()
            counter = 0

            async def increment():
                nonlocal counter
                async with lock:
                    temp = counter
                    await asyncio.sleep(0.001)
                    counter = temp + 1

            tasks = [increment() for _ in range(10)]
            await asyncio.gather(*tasks)

            return counter

        counter = asyncio.run(use_lock())
        assert counter == 10

    def test_async_semaphore(self):
        """Test async semaphore."""
        async def use_semaphore():
            semaphore = asyncio.Semaphore(2)
            results = []

            async def limited_task(i):
                async with semaphore:
                    await asyncio.sleep(0.01)
                    results.append(i)

            tasks = [limited_task(i) for i in range(5)]
            await asyncio.gather(*tasks)

            return results

        results = asyncio.run(use_semaphore())
        assert len(results) == 5

    def test_async_event(self):
        """Test async event."""
        async def use_event():
            event = asyncio.Event()
            results = []

            async def waiter():
                await event.wait()
                results.append("done")

            async def setter():
                await asyncio.sleep(0.01)
                event.set()

            await asyncio.gather(waiter(), setter())

            return results

        results = asyncio.run(use_event())
        assert "done" in results


class TestAsyncRetry:
    """Test async retry mechanisms."""

    def test_async_retry(self):
        """Test async retry on failure."""
        async def flaky_operation():
            attempts = 0

            async def operation():
                nonlocal attempts
                attempts += 1
                if attempts < 3:
                    raise Exception("Failed")
                return "Success"

            result = await scirs2.async_retry_py(operation, max_attempts=5)

            return result, attempts

        result, attempts = asyncio.run(flaky_operation())
        assert result == "Success"
        assert attempts == 3


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

    def test_async_empty_list(self):
        """Test async operations on empty list."""
        async def map_empty():
            results = await scirs2.async_map_py(lambda x: x * 2, [])
            return results

        results = asyncio.run(map_empty())
        assert results == []

    def test_async_timeout(self):
        """Test async operation with timeout."""
        async def slow_operation():
            async def slow_task():
                await asyncio.sleep(10)
                return "done"

            try:
                result = await asyncio.wait_for(slow_task(), timeout=0.1)
            except asyncio.TimeoutError:
                result = "timeout"

            return result

        result = asyncio.run(slow_operation())
        assert result == "timeout"

    def test_async_cancellation(self):
        """Test async task cancellation."""
        async def cancellable():
            task = asyncio.create_task(asyncio.sleep(10))
            await asyncio.sleep(0.01)
            task.cancel()

            try:
                await task
            except asyncio.CancelledError:
                return "cancelled"

            return "completed"

        result = asyncio.run(cancellable())
        assert result == "cancelled"


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