Skip to content

Commit

Permalink
Restore and deprecate np_as_located_field
Browse files Browse the repository at this point in the history
  • Loading branch information
egparedes committed Nov 12, 2023
1 parent 0a389eb commit 80f36fc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/gt4py/next/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
NeighborTableOffsetProvider,
StridedNeighborOffsetProvider,
index_field,
np_as_located_field,
)


Expand All @@ -56,6 +57,7 @@
"NeighborTableOffsetProvider",
"StridedNeighborOffsetProvider",
"index_field",
"np_as_located_field"
# from ffront
"FieldOffset",
"field_operator",
Expand Down
22 changes: 22 additions & 0 deletions src/gt4py/next/iterator/embedded.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import itertools
import math
import sys
import warnings
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -1012,6 +1013,27 @@ def _shift_field_indices(
)


def np_as_located_field(
*axes: common.Dimension, origin: Optional[dict[common.Dimension, int]] = None
) -> Callable[[np.ndarray], common.Field]:
warnings.warn("`np_as_located_field()` is deprecated, use `gtx.as_field()`", DeprecationWarning)

origin = origin or {}

def _maker(a) -> common.Field:
if a.ndim != len(axes):
raise TypeError("ndarray.ndim incompatible with number of given dimensions")
ranges = []
for d, s in zip(axes, a.shape):
offset = origin.get(d, 0)
ranges.append(common.UnitRange(-offset, s - offset))

res = common.field(a, domain=common.Domain(dims=tuple(axes), ranges=tuple(ranges)))
return res

return _maker


@dataclasses.dataclass(frozen=True)
class IndexField(common.Field):
"""
Expand Down

0 comments on commit 80f36fc

Please sign in to comment.