From da868559537f1082d561c5ce9ed21ae94fac0526 Mon Sep 17 00:00:00 2001
From: Alexandru Fikl <alexfikl@gmail.com>
Date: Mon, 27 Sep 2021 10:01:00 -0500
Subject: [PATCH] remove flatten_to_numpy and unflatten_from_numpy

---
 arraycontext/__init__.py            |  4 +-
 arraycontext/container/traversal.py | 68 -----------------------------
 test/test_arraycontext.py           | 29 ------------
 3 files changed, 1 insertion(+), 100 deletions(-)

diff --git a/arraycontext/__init__.py b/arraycontext/__init__.py
index 6610494..c0bca77 100644
--- a/arraycontext/__init__.py
+++ b/arraycontext/__init__.py
@@ -59,8 +59,7 @@ from .container.traversal import (
         rec_multimap_reduce_array_container,
         thaw, freeze,
         flatten, unflatten,
-        from_numpy, to_numpy,
-        flatten_to_numpy, unflatten_from_numpy)
+        from_numpy, to_numpy)
 
 from .impl.pyopencl import PyOpenCLArrayContext
 from .impl.pytato import PytatoPyOpenCLArrayContext
@@ -96,7 +95,6 @@ __all__ = (
         "thaw", "freeze",
         "flatten", "unflatten",
         "from_numpy", "to_numpy",
-        "flatten_to_numpy", "unflatten_from_numpy",
 
         "PyOpenCLArrayContext", "PytatoPyOpenCLArrayContext",
 
diff --git a/arraycontext/container/traversal.py b/arraycontext/container/traversal.py
index dc60880..06a0726 100644
--- a/arraycontext/container/traversal.py
+++ b/arraycontext/container/traversal.py
@@ -32,8 +32,6 @@ Numpy conversion
 ~~~~~~~~~~~~~~~~
 .. autofunction:: from_numpy
 .. autofunction:: to_numpy
-.. autofunction:: flatten_to_numpy
-.. autofunction:: unflatten_from_numpy
 """
 
 __copyright__ = """
@@ -619,72 +617,6 @@ def to_numpy(ary: Any, actx: ArrayContext) -> Any:
     """
     return rec_map_array_container(actx.to_numpy, ary)
 
-
-def flatten_to_numpy(ary: ArrayOrContainerT, actx: ArrayContext) -> np.ndarray:
-    """Convert all arrays in the :class:`~arraycontext.ArrayContainer`
-    to host :mod:`numpy` arrays, flatten them using :func:`~numpy.ravel`
-    and concatenate them into a single :class:`~numpy.ndarray`.
-
-    The order in which the individual leaf arrays appear in the final array is
-    dependent on the order given by :func:`~arraycontext.serialize_container`.
-    """
-    def _flatten_to_numpy(subary: ArrayOrContainerT) -> None:
-        try:
-            iterable = serialize_container(subary)
-        except TypeError:
-            result.append(actx.to_numpy(subary).ravel())
-        else:
-            for _, isubary in iterable:
-                _flatten_to_numpy(isubary)
-
-    result: List[np.ndarray] = []
-    _flatten_to_numpy(ary)
-
-    return np.concatenate(result)
-
-
-def unflatten_from_numpy(
-        template: ArrayOrContainerT, ary: np.ndarray,
-        actx: ArrayContext) -> ArrayOrContainerT:
-    """Unflatten an :class:`~numpy.ndarray` produced by :func:`flatten_to_numpy`
-    back into an :class:`~arraycontext.ArrayContainer`.
-
-    The order and sizes of each slice into *ary* are determined by the
-    array container *template*.
-    """
-    # NOTE: https://github.com/python/mypy/issues/7057
-    offset = 0
-
-    def _unflatten_from_numpy(
-            template_subary: ArrayOrContainerT) -> ArrayOrContainerT:
-        nonlocal offset
-
-        try:
-            iterable = serialize_container(template_subary)
-        except TypeError:
-            # NOTE: the max is needed to handle device scalars with size == 0
-            offset += max(1, template_subary.size)
-            if offset > ary.size:
-                raise ValueError("'template' and 'ary' sizes do not match")
-
-            # FIXME: subary can be F-contiguous and ary will always be C-contiguous
-            return actx.from_numpy(
-                    ary[offset - template_subary.size:offset]
-                    .astype(template_subary.dtype, copy=False)
-                    .reshape(template_subary.shape)
-                    )
-        else:
-            return deserialize_container(template_subary, [
-                (key, _unflatten_from_numpy(isubary)) for key, isubary in iterable
-                ])
-
-    if ary.ndim != 1:
-        raise ValueError(
-                "only one dimensional arrays can be unflattened: "
-                f"'ary' has shape {ary.shape}")
-
-    return _unflatten_from_numpy(template)
-
 # }}}
 
 # vim: foldmethod=marker
diff --git a/test/test_arraycontext.py b/test/test_arraycontext.py
index 5f9cfc7..d5fca2c 100644
--- a/test/test_arraycontext.py
+++ b/test/test_arraycontext.py
@@ -911,8 +911,6 @@ def test_numpy_conversion(actx_factory):
             enthalpy=np.array(np.random.rand()),
             )
 
-    # {{{ to/from_numpy
-
     from arraycontext import from_numpy, to_numpy
     ac_actx = from_numpy(ac, actx)
     ac_roundtrip = to_numpy(ac_actx, actx)
@@ -931,33 +929,6 @@ def test_numpy_conversion(actx_factory):
     with pytest.raises(ValueError):
         to_numpy(ac, actx)
 
-    # }}}
-
-    # {{{ un/flatten
-
-    from arraycontext import flatten_to_numpy, unflatten_from_numpy
-    ac_flat = flatten_to_numpy(ac_actx, actx)
-    assert ac_flat.shape == (nelements**2 + 3 * nelements + 1,)
-
-    ac_roundtrip = unflatten_from_numpy(ac_actx, ac_flat, actx)
-    for name in ("mass", "momentum", "enthalpy"):
-        field = getattr(ac_actx, name)
-        field_roundtrip = getattr(ac_roundtrip, name)
-
-        assert field.dtype == field_roundtrip.dtype
-        assert field.shape == field_roundtrip.shape
-        assert np.linalg.norm(
-                np.linalg.norm(to_numpy(field - field_roundtrip, actx))
-                ) < 1.0e-15
-
-    with pytest.raises(ValueError):
-        unflatten_from_numpy(ac_actx, ac_flat[:-12], actx)
-
-    with pytest.raises(ValueError):
-        unflatten_from_numpy(ac_actx, ac_flat.reshape(2, -1), actx)
-
-    # }}}
-
 # }}}
 
 
-- 
GitLab