Skip to content

Commit dee5d0f

Browse files
Merge master into update_elementwise_docs
2 parents 3e1b522 + ffd3829 commit dee5d0f

File tree

12 files changed

+212
-53
lines changed

12 files changed

+212
-53
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
:orphan:
2+
3+
{{ fullname | escape | underline}}
4+
5+
.. currentmodule:: {{ module }}
6+
7+
attribute
8+
9+
.. auto{{ objtype }}:: {{ objname }}

doc/_templates/autosummary/base.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% if objtype == 'property' %}
2+
:orphan:
3+
{% endif %}
4+
5+
{{ fullname | escape | underline}}
6+
7+
.. currentmodule:: {{ module }}
8+
9+
{% if objtype == 'property' %}
10+
property
11+
{% endif %}
12+
13+
.. auto{{ objtype }}:: {{ objname }}

doc/_templates/autosummary/class.rst

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,27 @@
1-
{{ fullname }}
2-
{{ underline }}
3-
4-
.. currentmodule:: {{ module }}
5-
6-
.. autoclass:: {{ objname }}
7-
8-
..
9-
Methods
1+
{% extends "!autosummary/class.rst" %}
102

113
{% block methods %}
12-
13-
.. rubric:: Methods
14-
15-
..
16-
Special methods
17-
18-
{% for item in ('__getitem__', '__setitem__', '__len__', '__next__', '__iter__') %}
19-
{% if item in all_methods or item in all_attributes %}
20-
.. automethod:: {{ item }}
4+
{% if methods %}
5+
.. HACK -- the point here is that we don't want this to appear in the output, but the autosummary should still generate the pages.
6+
.. autosummary::
7+
:toctree:
8+
{% for item in all_methods %}
9+
{%- if not item.startswith('_') or item in ['__call__'] %}
10+
{{ name }}.{{ item }}
11+
{%- endif -%}
12+
{%- endfor %}
2113
{% endif %}
22-
{%- endfor %}
23-
24-
..
25-
Ordinary methods
26-
27-
{% for item in methods %}
28-
{% if item not in ('__init__',) %}
29-
.. automethod:: {{ item }}
30-
{% endif %}
31-
{%- endfor %}
32-
33-
..
34-
Special methods
14+
{% endblock %}
3515

36-
{% for item in ('__eq__', '__ne__', '__lt__', '__le__', '__gt__', '__ge__') %}
37-
{% if item in all_methods %}
38-
.. automethod:: {{ item }}
16+
{% block attributes %}
17+
{% if attributes %}
18+
.. HACK -- the point here is that we don't want this to appear in the output, but the autosummary should still generate the pages.
19+
.. autosummary::
20+
:toctree:
21+
{% for item in all_attributes %}
22+
{%- if not item.startswith('_') %}
23+
{{ name }}.{{ item }}
24+
{%- endif -%}
25+
{%- endfor %}
3926
{% endif %}
40-
{%- endfor %}
4127
{% endblock %}
42-
43-
..
44-
Attributes
45-
46-
{% block attributes %} {% if attributes %}
47-
48-
.. rubric:: Attributes
49-
50-
{% for item in attributes %}
51-
.. autoattribute:: {{ item }}
52-
{%- endfor %}
53-
{% endif %} {% endblock %}

doc/_templates/autosummary/member.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
:orphan:
2+
3+
{{ fullname | escape | underline}}
4+
5+
.. currentmodule:: {{ module }}
6+
7+
member
8+
9+
.. auto{{ objtype }}:: {{ objname }}

doc/_templates/autosummary/method.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
:orphan:
2+
3+
{{ fullname | escape | underline}}
4+
5+
.. currentmodule:: {{ module }}
6+
7+
method
8+
9+
.. auto{{ objtype }}:: {{ objname }}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{ fullname | escape | underline}}
2+
3+
.. automodule:: {{ fullname }}
4+
5+
{% block docstring %}
6+
{% endblock %}

doc/_templates/autosummary/module.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{% extends "!autosummary/module.rst" %}
2+
3+
{# This file is almost the same as the default, but adds :toctree: to the
4+
autosummary directives. The original can be found at
5+
``sphinx/ext/autosummary/templates/autosummary/module.rst``. #}
6+
7+
{% block attributes %}
8+
{% if attributes %}
9+
.. rubric:: Module Attributes
10+
11+
.. autosummary::
12+
:toctree:
13+
{% for item in attributes %}
14+
{{ item }}
15+
{%- endfor %}
16+
{% endif %}
17+
{% endblock %}
18+
19+
{% block functions %}
20+
{% if functions %}
21+
.. rubric:: Functions
22+
23+
.. autosummary::
24+
:toctree:
25+
{% for item in functions %}
26+
{{ item }}
27+
{%- endfor %}
28+
{% endif %}
29+
{% endblock %}
30+
31+
{% block classes %}
32+
{% if classes %}
33+
.. rubric:: Classes
34+
35+
.. autosummary::
36+
:toctree:
37+
{% for item in classes %}
38+
{{ item }}
39+
{%- endfor %}
40+
{% endif %}
41+
{% endblock %}

doc/conf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# full list see the documentation:
77
# http://www.sphinx-doc.org/en/master/config
88

9+
from datetime import datetime
10+
911
from sphinx.ext.autodoc import FunctionDocumenter
1012

1113
from dpnp.dpnp_algo.dpnp_elementwise_common import DPNPBinaryFunc, DPNPUnaryFunc
@@ -32,7 +34,8 @@
3234
# -- Project information -----------------------------------------------------
3335

3436
project = "Data Parallel Extension for NumPy"
35-
copyright = "2020-2024, Intel Corporation"
37+
year = datetime.now().year
38+
copyright = f"2020-{year}, Intel Corporation"
3639
author = "Intel"
3740

3841
version = dpnp.__version__.strip(".dirty")

dpnp/dpnp_iface_functional.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"""
3838

3939

40-
import numpy
4140
from dpctl.tensor._numpy_helper import (
4241
normalize_axis_index,
4342
normalize_axis_tuple,
@@ -151,8 +150,7 @@ def apply_along_axis(func1d, axis, arr, *args, **kwargs):
151150

152151
# compute indices for the iteration axes, and append a trailing ellipsis to
153152
# prevent 0d arrays decaying to scalars
154-
# TODO: replace with dpnp.ndindex
155-
inds = numpy.ndindex(inarr_view.shape[:-1])
153+
inds = dpnp.ndindex(inarr_view.shape[:-1])
156154
inds = (ind + (Ellipsis,) for ind in inds)
157155

158156
# invoke the function on the first item

dpnp/dpnp_iface_indexing.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"indices",
6565
"ix_",
6666
"mask_indices",
67+
"ndindex",
6768
"nonzero",
6869
"place",
6970
"put",
@@ -1057,6 +1058,77 @@ def mask_indices(
10571058
return nonzero(a != 0)
10581059

10591060

1061+
# pylint: disable=invalid-name
1062+
# pylint: disable=too-few-public-methods
1063+
class ndindex:
1064+
"""
1065+
An N-dimensional iterator object to index arrays.
1066+
1067+
Given the shape of an array, an :obj:`dpnp.ndindex` instance iterates over
1068+
the N-dimensional index of the array. At each iteration a tuple of indices
1069+
is returned, the last dimension is iterated over first.
1070+
1071+
For full documentation refer to :obj:`numpy.ndindex`.
1072+
1073+
Parameters
1074+
----------
1075+
shape : ints, or a single tuple of ints
1076+
The size of each dimension of the array can be passed as individual
1077+
parameters or as the elements of a tuple.
1078+
1079+
See Also
1080+
--------
1081+
:obj:`dpnp.ndenumerate` : Multidimensional index iterator.
1082+
:obj:`dpnp.flatiter` : Flat iterator object to iterate over arrays.
1083+
1084+
Examples
1085+
--------
1086+
>>> import dpnp as np
1087+
1088+
Dimensions as individual arguments
1089+
1090+
>>> for index in np.ndindex(3, 2, 1):
1091+
... print(index)
1092+
(0, 0, 0)
1093+
(0, 1, 0)
1094+
(1, 0, 0)
1095+
(1, 1, 0)
1096+
(2, 0, 0)
1097+
(2, 1, 0)
1098+
1099+
Same dimensions - but in a tuple ``(3, 2, 1)``
1100+
1101+
>>> for index in np.ndindex((3, 2, 1)):
1102+
... print(index)
1103+
(0, 0, 0)
1104+
(0, 1, 0)
1105+
(1, 0, 0)
1106+
(1, 1, 0)
1107+
(2, 0, 0)
1108+
(2, 1, 0)
1109+
1110+
"""
1111+
1112+
def __init__(self, *shape):
1113+
self.ndindex_ = numpy.ndindex(*shape)
1114+
1115+
def __iter__(self):
1116+
return self.ndindex_
1117+
1118+
def __next__(self):
1119+
"""
1120+
Standard iterator method, updates the index and returns the index tuple.
1121+
1122+
Returns
1123+
-------
1124+
val : tuple of ints
1125+
Returns a tuple containing the indices of the current iteration.
1126+
1127+
"""
1128+
1129+
return self.ndindex_.__next__()
1130+
1131+
10601132
def nonzero(a):
10611133
"""
10621134
Return the indices of the elements that are non-zero.

dpnp/dpnp_utils/dpnp_utils_pad.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,7 @@ def dpnp_pad(array, pad_width, mode="constant", **kwargs):
642642

643643
# compute indices for the iteration axes, and append a trailing
644644
# ellipsis to prevent 0d arrays decaying to scalars
645-
# TODO: replace with dpnp.ndindex when implemented
646-
inds = numpy.ndindex(view.shape[:-1])
645+
inds = dpnp.ndindex(view.shape[:-1])
647646
inds = (ind + (Ellipsis,) for ind in inds)
648647
for ind in inds:
649648
function(view[ind], pad_width[axis], axis, kwargs)

dpnp/tests/test_indexing.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,32 @@ def test_ix_error(self, xp, shape):
335335
assert_raises(ValueError, xp.ix_, xp.ones(shape))
336336

337337

338+
@pytest.mark.parametrize(
339+
"shape", [[1, 2, 3], [(1, 2, 3)], [(3,)], [3], [], [()], [0]]
340+
)
341+
class TestNdindex:
342+
def test_basic(self, shape):
343+
result = dpnp.ndindex(*shape)
344+
expected = numpy.ndindex(*shape)
345+
346+
for x, y in zip(result, expected):
347+
assert x == y
348+
349+
def test_next(self, shape):
350+
dind = dpnp.ndindex(*shape)
351+
nind = numpy.ndindex(*shape)
352+
353+
while True:
354+
try:
355+
ditem = next(dind)
356+
except StopIteration:
357+
assert_raises(StopIteration, next, nind)
358+
break # both reach ends
359+
else:
360+
nitem = next(nind)
361+
assert ditem == nitem
362+
363+
338364
class TestNonzero:
339365
@pytest.mark.parametrize("list_val", [[], [0], [1]])
340366
def test_trivial(self, list_val):

0 commit comments

Comments
 (0)