Skip to content

Commit f2b830d

Browse files
committed
Edit outdated docs to pass doctests.
1 parent 7f31435 commit f2b830d

File tree

12 files changed

+60
-837
lines changed

12 files changed

+60
-837
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
pip install -e '.[tests]'
5151
- name: Run tests
5252
run: |
53-
SPARSE_BACKEND=Numba pytest --pyargs sparse --cov-report=xml:coverage_Numba.xml -n auto -vvv
53+
SPARSE_BACKEND=Numba pytest --pyargs sparse --doctest-modules --cov-report=xml:coverage_Numba.xml -n auto -vvv
5454
SPARSE_BACKEND=Finch pytest --pyargs sparse/tests --cov-report=xml:coverage_Finch.xml -n auto -vvv
5555
SPARSE_BACKEND=MLIR pytest --pyargs sparse/mlir_backend --cov-report=xml:coverage_MLIR.xml -n auto -vvv
5656
- uses: codecov/codecov-action@v4

sparse/mlir_backend/tests/__init__.py

Whitespace-only changes.

sparse/numba_backend/_common.py

Lines changed: 8 additions & 789 deletions
Large diffs are not rendered by default.

sparse/numba_backend/_compressed/compressed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ def _prune(self):
819819
--------
820820
>>> coords = np.array([[0, 1, 2, 3]])
821821
>>> data = np.array([1, 0, 1, 2])
822-
>>> s = COO(coords, data).asformat("gcxs")
822+
>>> s = COO(coords, data, shape=(4,)).asformat("gcxs")
823823
>>> s._prune()
824824
>>> s.nnz
825825
3

sparse/numba_backend/_coo/common.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ def diagonalize(a, axis=0):
901901
>>> a = sparse.random((3, 3, 3, 3, 3), density=0.3)
902902
>>> a_diag = sparse.diagonalize(a, axis=2)
903903
>>> (sparse.diagonal(a_diag, axis1=2, axis2=5) == a.transpose([0, 1, 3, 4, 2])).all()
904-
True
904+
np.True_
905905
906906
Returns
907907
-------
@@ -944,7 +944,7 @@ def isposinf(x, out=None):
944944
--------
945945
[`numpy.isposinf`][] : The NumPy equivalent
946946
"""
947-
from .core import elemwise
947+
from sparse import elemwise
948948

949949
return elemwise(lambda x, out=None, dtype=None: np.isposinf(x, out=out), x, out=out)
950950

@@ -971,7 +971,7 @@ def isneginf(x, out=None):
971971
--------
972972
[`numpy.isneginf`][] : The NumPy equivalent
973973
"""
974-
from .core import elemwise
974+
from sparse import elemwise
975975

976976
return elemwise(lambda x, out=None, dtype=None: np.isneginf(x, out=out), x, out=out)
977977

@@ -1234,7 +1234,7 @@ def unique_values(x, /):
12341234
>>> import sparse
12351235
>>> x = sparse.COO.from_numpy([1, 0, 2, 1, 2, -3])
12361236
>>> sparse.unique_values(x)
1237-
array([-3, 0, 1, 2])
1237+
array([-3, 0, 1, 2])
12381238
"""
12391239

12401240
x = _validate_coo_input(x)
@@ -1279,9 +1279,9 @@ def sort(x, /, *, axis=-1, descending=False, stable=False):
12791279
>>> import sparse
12801280
>>> x = sparse.COO.from_numpy([1, 0, 2, 0, 2, -3])
12811281
>>> sparse.sort(x).todense()
1282-
array([-3, 0, 0, 1, 2, 2])
1282+
array([-3, 0, 0, 1, 2, 2])
12831283
>>> sparse.sort(x, descending=True).todense()
1284-
array([ 2, 2, 1, 0, 0, -3])
1284+
array([ 2, 2, 1, 0, 0, -3])
12851285
12861286
"""
12871287
from .._common import moveaxis

sparse/numba_backend/_coo/core.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class COO(SparseArray, NDArrayOperatorsMixin): # lgtm [py/missing-equals]
156156
>>> rows = [0, 1, 2, 3, 4]
157157
>>> cols = [0, 0, 0, 1, 1]
158158
>>> data = [10, 20, 30, 40, 50]
159-
>>> z = COO((data, (rows, cols)))
159+
>>> z = COO((data, (rows, cols)), shape=(5, 2))
160160
>>> z.todense() # doctest: +NORMALIZE_WHITESPACE
161161
array([[10, 0],
162162
[20, 0],
@@ -168,10 +168,10 @@ class COO(SparseArray, NDArrayOperatorsMixin): # lgtm [py/missing-equals]
168168
indices imply summation:
169169
170170
>>> d = {(0, 0, 0): 1, (1, 2, 3): 2, (1, 1, 0): 3}
171-
>>> COO(d)
171+
>>> COO(d, shape=(2, 3, 4))
172172
<COO: shape=(2, 3, 4), dtype=int64, nnz=3, fill_value=0>
173173
>>> L = [((0, 0), 1), ((1, 1), 2), ((0, 0), 3)]
174-
>>> COO(L).todense() # doctest: +NORMALIZE_WHITESPACE
174+
>>> COO(L, shape=(2, 2)).todense() # doctest: +NORMALIZE_WHITESPACE
175175
array([[4, 0],
176176
[0, 2]])
177177
@@ -440,6 +440,7 @@ def from_scipy_sparse(cls, x, /, *, fill_value=None):
440440
441441
Examples
442442
--------
443+
>>> import scipy.sparse
443444
>>> x = scipy.sparse.rand(6, 3, density=0.2)
444445
>>> s = COO.from_scipy_sparse(x)
445446
>>> np.array_equal(x.todense(), s.todense())
@@ -459,7 +460,7 @@ def from_scipy_sparse(cls, x, /, *, fill_value=None):
459460
)
460461

461462
@classmethod
462-
def from_iter(cls, x, shape=None, fill_value=None, dtype=None):
463+
def from_iter(cls, x, shape, fill_value=None, dtype=None):
463464
"""
464465
Converts an iterable in certain formats to a [`sparse.COO`][] array. See examples
465466
for details.
@@ -468,7 +469,7 @@ def from_iter(cls, x, shape=None, fill_value=None, dtype=None):
468469
----------
469470
x : Iterable or Iterator
470471
The iterable to convert to [`sparse.COO`][].
471-
shape : tuple[int], optional
472+
shape : tuple[int]
472473
The shape of the array.
473474
fill_value : scalar
474475
The fill value for this array.
@@ -486,31 +487,31 @@ def from_iter(cls, x, shape=None, fill_value=None, dtype=None):
486487
Here, the first part represents the coordinate and the second part represents the value.
487488
488489
>>> x = [((0, 0), 1), ((1, 1), 1)]
489-
>>> s = COO.from_iter(x)
490+
>>> s = COO.from_iter(x, shape=(2, 2))
490491
>>> s.todense()
491492
array([[1, 0],
492493
[0, 1]])
493494
494495
You can also have a similar format with a dictionary.
495496
496497
>>> x = {(0, 0): 1, (1, 1): 1}
497-
>>> s = COO.from_iter(x)
498+
>>> s = COO.from_iter(x, shape=(2, 2))
498499
>>> s.todense()
499500
array([[1, 0],
500501
[0, 1]])
501502
502503
The third supported format is ``(data, (..., row, col))``.
503504
504505
>>> x = ([1, 1], ([0, 1], [0, 1]))
505-
>>> s = COO.from_iter(x)
506+
>>> s = COO.from_iter(x, shape=(2, 2))
506507
>>> s.todense()
507508
array([[1, 0],
508509
[0, 1]])
509510
510511
You can also pass in a [`collections.abc.Iterator`][] object.
511512
512513
>>> x = [((0, 0), 1), ((1, 1), 1)].__iter__()
513-
>>> s = COO.from_iter(x)
514+
>>> s = COO.from_iter(x, shape=(2, 2))
514515
>>> s.todense()
515516
array([[1, 0],
516517
[0, 1]])
@@ -1293,7 +1294,7 @@ def _sort_indices(self):
12931294
--------
12941295
>>> coords = np.array([[1, 2, 0]], dtype=np.uint8)
12951296
>>> data = np.array([4, 1, 3], dtype=np.uint8)
1296-
>>> s = COO(coords, data)
1297+
>>> s = COO(coords, data, shape=(3,))
12971298
>>> s._sort_indices()
12981299
>>> s.coords # doctest: +NORMALIZE_WHITESPACE
12991300
array([[0, 1, 2]], dtype=uint8)
@@ -1321,7 +1322,7 @@ def _sum_duplicates(self):
13211322
--------
13221323
>>> coords = np.array([[0, 1, 1, 2]], dtype=np.uint8)
13231324
>>> data = np.array([6, 5, 2, 2], dtype=np.uint8)
1324-
>>> s = COO(coords, data)
1325+
>>> s = COO(coords, data, shape=(3,))
13251326
>>> s._sum_duplicates()
13261327
>>> s.coords # doctest: +NORMALIZE_WHITESPACE
13271328
array([[0, 1, 2]], dtype=uint8)
@@ -1354,7 +1355,7 @@ def _prune(self):
13541355
--------
13551356
>>> coords = np.array([[0, 1, 2, 3]])
13561357
>>> data = np.array([1, 0, 1, 2])
1357-
>>> s = COO(coords, data)
1358+
>>> s = COO(coords, data, shape=(4,))
13581359
>>> s._prune()
13591360
>>> s.nnz
13601361
3

sparse/numba_backend/_coo/indexing.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,9 @@ def _get_mask_pairs(starts_old, stops_old, c, idx): # pragma: no cover
524524
--------
525525
>>> c = np.array([1, 2, 1, 2, 1, 1, 2, 2])
526526
>>> starts_old = numba.typed.List()
527-
... starts_old.append(4)
527+
>>> starts_old.append(4)
528528
>>> stops_old = numba.typed.List()
529-
... stops_old.append(8)
529+
>>> stops_old.append(8)
530530
>>> idx = np.array([1, 2, 1])
531531
>>> _get_mask_pairs(starts_old, stops_old, c, idx)
532532
(ListType[int64]([4]), ListType[int64]([6]), 2)
@@ -575,9 +575,9 @@ def _filter_pairs(starts, stops, coords, indices): # pragma: no cover
575575
--------
576576
>>> import numpy as np
577577
>>> starts = numba.typed.List()
578-
... starts.append(2)
578+
>>> starts.append(2)
579579
>>> stops = numba.typed.List()
580-
... stops.append(7)
580+
>>> stops.append(7)
581581
>>> coords = np.array([[0, 1, 2, 3, 4, 5, 6, 7]])
582582
>>> indices = np.array([[2, 8, 2]]) # Start, stop, step pairs
583583
>>> _filter_pairs(starts, stops, coords, indices)
@@ -627,11 +627,11 @@ def _join_adjacent_pairs(starts_old, stops_old): # pragma: no cover
627627
Examples
628628
--------
629629
>>> starts = numba.typed.List()
630-
... starts.append(2)
631-
... starts.append(5)
630+
>>> starts.append(2)
631+
>>> starts.append(5)
632632
>>> stops = numba.typed.List()
633-
... stops.append(5)
634-
... stops.append(7)
633+
>>> stops.append(5)
634+
>>> stops.append(7)
635635
>>> _join_adjacent_pairs(starts, stops)
636636
(ListType[int64]([2]), ListType[int64]([7]))
637637
"""

sparse/numba_backend/_dok.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def from_scipy_sparse(cls, x, /, *, fill_value=None):
149149
150150
Examples
151151
--------
152+
>>> import scipy.sparse
152153
>>> x = scipy.sparse.rand(6, 3, density=0.2)
153154
>>> s = DOK.from_scipy_sparse(x)
154155
>>> np.array_equal(x.todense(), s.todense())

sparse/numba_backend/_sparse_array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ def mean(self, axis=None, keepdims=False, dtype=None, out=None):
691691
mean along all axes.
692692
693693
>>> s.mean()
694-
0.5
694+
np.float64(0.5)
695695
"""
696696

697697
if axis is None:
@@ -771,7 +771,7 @@ def var(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
771771
variance along all axes.
772772
773773
>>> s.var()
774-
0.5
774+
np.float64(0.5)
775775
"""
776776
axis = normalize_axis(axis, self.ndim)
777777

sparse/numba_backend/_utils.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -267,19 +267,18 @@ def random(
267267
268268
Examples
269269
--------
270-
>>> from sparse import random
271270
>>> from scipy import stats
272-
>>> rvs = lambda x: stats.poisson(25, loc=10).rvs(x, random_state=np.random.RandomState(1))
273-
>>> s = random((2, 3, 4), density=0.25, random_state=np.random.RandomState(1), data_rvs=rvs)
274-
>>> s.todense() # doctest: +NORMALIZE_WHITESPACE
275-
array([[[ 0, 0, 0, 0],
276-
[34, 0, 29, 30],
271+
>>> rng = np.random.default_rng(42)
272+
>>> rvs = lambda x: stats.poisson(25, loc=10).rvs(x, random_state=rng)
273+
>>> s = sparse.random((2, 3, 4), density=0.25, random_state=rng, data_rvs=rvs)
274+
>>> s.todense()
275+
array([[[39, 0, 0, 0],
276+
[28, 33, 0, 37],
277277
[ 0, 0, 0, 0]],
278278
<BLANKLINE>
279-
[[33, 0, 0, 34],
280-
[34, 0, 0, 0],
281-
[ 0, 0, 0, 0]]])
282-
279+
[[ 0, 0, 0, 0],
280+
[ 0, 0, 34, 0],
281+
[ 0, 0, 0, 36]]])
283282
"""
284283
# Copied, in large part, from scipy.sparse.random
285284
# See https://github.com/scipy/scipy/blob/main/LICENSE.txt
@@ -422,15 +421,15 @@ def equivalent(x, y, /, loose=False):
422421
Examples
423422
--------
424423
>>> equivalent(1, 1)
425-
True
424+
np.True_
426425
>>> equivalent(np.nan, np.nan + 1)
427-
True
426+
np.True_
428427
>>> equivalent(1, 2)
429-
False
428+
np.False_
430429
>>> equivalent(np.inf, np.inf)
431-
True
432-
>>> equivalent(np.PZERO, np.NZERO)
433-
False
430+
np.True_
431+
>>> equivalent(np.float64(0.0), np.float64(-0.0))
432+
np.False_
434433
"""
435434
x = np.asarray(x)
436435
y = np.asarray(y)

sparse/numba_backend/tests/__init__.py

Whitespace-only changes.

sparse/numba_backend/tests/test_coo.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,11 +1025,14 @@ def test_invalid_attrs_error():
10251025
def test_invalid_iterable_error():
10261026
with pytest.raises(ValueError):
10271027
x = [(3, 4, 5)]
1028-
COO.from_iter(x)
1028+
COO.from_iter(x, shape=(6,))
10291029

10301030
with pytest.raises(ValueError):
10311031
x = [((2.3, 4.5), 3.2)]
1032-
COO.from_iter(x)
1032+
COO.from_iter(x, shape=(5,))
1033+
1034+
with pytest.raises(TypeError):
1035+
COO.from_iter({(1, 1): 1})
10331036

10341037

10351038
def test_prod_along_axis():

0 commit comments

Comments
 (0)