Skip to content

Commit cda94d4

Browse files
alexfiklinducer
authored andcommitted
deprecate to_numpy and from_numpy
1 parent 70f251f commit cda94d4

File tree

2 files changed

+30
-40
lines changed

2 files changed

+30
-40
lines changed

arraycontext/container/traversal.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -881,37 +881,24 @@ def from_numpy(
881881
882882
The conversion is done using :meth:`arraycontext.ArrayContext.from_numpy`.
883883
"""
884-
def _from_numpy_with_check(subary: Union[np.ndarray, ScalarLike]) \
885-
-> ArrayOrContainerOrScalar:
886-
if isinstance(subary, np.ndarray) or np.isscalar(subary):
887-
return actx.from_numpy(subary)
888-
else:
889-
raise TypeError(f"array is not an ndarray: '{type(subary).__name__}'")
884+
warn("Calling from_numpy(ary, actx) is deprecated, call actx.from_numpy(ary)"
885+
" instead. This will stop working in 2023.",
886+
DeprecationWarning, stacklevel=2)
890887

891-
return rec_map_array_container(_from_numpy_with_check, ary)
888+
return actx.from_numpy(ary)
892889

893890

894-
def to_numpy(ary: ArrayOrContainer, actx: ArrayContext) -> Any:
891+
def to_numpy(ary: ArrayOrContainer, actx: ArrayContext) -> ArrayOrContainer:
895892
"""Convert all arrays in the :class:`~arraycontext.ArrayContainer` to
896893
:mod:`numpy` using the provided :class:`~arraycontext.ArrayContext` *actx*.
897894
898895
The conversion is done using :meth:`arraycontext.ArrayContext.to_numpy`.
899896
"""
900-
def _to_numpy_with_check(subary: Any) -> Any:
901-
if isinstance(subary, actx.array_types) or np.isscalar(subary):
902-
# NOTE: these are allowed by np.isscalar, but not here
903-
assert not isinstance(subary, (str, bytes))
904-
905-
return actx.to_numpy(subary)
906-
else:
907-
raise TypeError(
908-
f"array of type '{type(subary).__name__}' not in "
909-
f"supported types {actx.array_types}")
897+
warn("Calling to_numpy(ary, actx) is deprecated, call actx.to_numpy(ary)"
898+
" instead. This will stop working in 2023.",
899+
DeprecationWarning, stacklevel=2)
910900

911-
return rec_map_array_container(_to_numpy_with_check,
912-
# do a freeze first, if 'actx' supports
913-
# container-wide freezes
914-
actx.thaw(actx.freeze(ary)))
901+
return actx.to_numpy(ary)
915902

916903
# }}}
917904

arraycontext/context.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ def dtype(self) -> "np.dtype[Any]":
221221
"ArrayOrContainerOrScalarT",
222222
bound=ArrayOrContainerOrScalar)
223223

224+
NumpyOrContainerOrScalar = Union[np.ndarray, "ArrayContainer", ScalarLike]
225+
224226
# }}}
225227

226228

@@ -304,25 +306,28 @@ def zeros_like(self, ary: Array) -> Array:
304306

305307
@abstractmethod
306308
def from_numpy(self,
307-
array: Union["np.ndarray[Any, Any]", ScalarLike]
308-
) -> Union[Array, ScalarLike]:
309+
array: ArrayOrContainerOrScalar
310+
) -> NumpyOrContainerOrScalar:
309311
r"""
310312
:returns: the :class:`numpy.ndarray` *array* converted to the
311313
array context's array type. The returned array will be
312-
:meth:`thaw`\ ed.
314+
:meth:`thaw`\ ed. When working with array containers each leaf
315+
must be an :class:`~numpy.ndarray` or scalar, which is then converted
316+
to the context's array type leaving the container structure
317+
intact.
313318
"""
314-
pass
315319

316320
@abstractmethod
317321
def to_numpy(self,
318-
array: Union[Array, ScalarLike]
319-
) -> Union["np.ndarray[Any, Any]", ScalarLike]:
322+
array: NumpyOrContainerOrScalar
323+
) -> ArrayOrContainerOrScalar:
320324
r"""
321-
:returns: *array*, an array recognized by the context, converted
322-
to a :class:`numpy.ndarray`. *array* must be
323-
:meth:`thaw`\ ed.
325+
:returns: an :class:`numpy.ndarray` for each array recognized by the
326+
context. The input *array* must be :meth:`thaw`\ ed.
327+
When working with array containers each leaf must be one of
328+
the context's array types or a scalar, which is then converted to
329+
an :class:`~numpy.ndarray` leaving the container structure intact.
324330
"""
325-
pass
326331

327332
@abstractmethod
328333
def call_loopy(self,
@@ -351,8 +356,6 @@ def freeze(self, array: ArrayOrContainerOrScalarT) -> ArrayOrContainerOrScalarT:
351356
Freezing makes the array independent of this :class:`ArrayContext`;
352357
it is permitted to :meth:`thaw` it in a different one, as long as that
353358
context understands the array format.
354-
355-
See also :func:`arraycontext.freeze`.
356359
"""
357360

358361
@abstractmethod
@@ -366,8 +369,6 @@ def thaw(self, array: ArrayOrContainerOrScalarT) -> ArrayOrContainerOrScalarT:
366369
the data in *array*.
367370
368371
The returned array may not be used with other contexts while thawed.
369-
370-
See also :func:`arraycontext.thaw`.
371372
"""
372373

373374
def freeze_thaw(
@@ -387,10 +388,11 @@ def freeze_thaw(
387388
@abstractmethod
388389
def tag(self,
389390
tags: ToTagSetConvertible,
390-
array: ArrayT) -> ArrayT:
391+
array: ArrayOrContainerOrScalarT) -> ArrayOrContainerOrScalarT:
391392
"""If the array type used by the array context is capable of capturing
392393
metadata, return a version of *array* with the *tags* applied. *array*
393-
itself is not modified.
394+
itself is not modified. When working with array containers, the
395+
tags are applied to each leaf of the container.
394396
395397
See :ref:`metadata` as well as application-specific metadata types.
396398
@@ -400,10 +402,11 @@ def tag(self,
400402
@abstractmethod
401403
def tag_axis(self,
402404
iaxis: int, tags: ToTagSetConvertible,
403-
array: ArrayT) -> ArrayT:
405+
array: ArrayOrContainerOrScalarT) -> ArrayOrContainerOrScalarT:
404406
"""If the array type used by the array context is capable of capturing
405407
metadata, return a version of *array* in which axis number *iaxis* has
406-
the *tags* applied. *array* itself is not modified.
408+
the *tags* applied. *array* itself is not modified. When working with
409+
array containers, the tags are applied to each leaf of the container.
407410
408411
See :ref:`metadata` as well as application-specific metadata types.
409412

0 commit comments

Comments
 (0)