Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ refactoring: ak.strings_astype #1301

Merged
merged 3 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/awkward/_v2/operations/structure/ak_full_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ def action(layout, **kwargs):

out = layout.recursively_apply(action)
if dtype is not None:
out = ak._v2.operations.structure.strings_astype(out, dtype)
out = ak._v2.operations.structure.values_astype(out, dtype)
out = ak._v2.operations.structure.strings_astype(
out, dtype, highlevel, behavior
)
out = ak._v2.operations.structure.values_astype(out, dtype, highlevel, behavior)
return out
return ak._v2._util.wrap(out, behavior, highlevel)
134 changes: 66 additions & 68 deletions src/awkward/_v2/operations/structure/ak_strings_astype.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,74 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE


import awkward as ak

np = ak.nplike.NumpyMetadata.instance()


def strings_astype(array, to, highlevel=True, behavior=None):
raise NotImplementedError


# """
# Args:
# array: Array whose strings should be converted to a new numeric type.
# to (dtype or dtype specifier): Type to convert the strings into.
# highlevel (bool): If True, return an #ak.Array; otherwise, return
# a low-level #ak.layout.Content subclass.
# behavior (None or dict): Custom #ak.behavior for the output array, if
# high-level.

# Converts all strings in the array to a new type, leaving the structure
# untouched.

# For example,

# >>> array = ak.Array(["1", "2", " 3 ", "00004", "-5"])
# >>> ak.strings_astype(array, np.int32)
# <Array [1, 2, 3, 4, -5] type='5 * int32'>

# and

# >>> array = ak.Array(["1.1", "2.2", " 3.3 ", "00004.4", "-5.5"])
# >>> ak.strings_astype(array, np.float64)
# <Array [1.1, 2.2, 3.3, 4.4, -5.5] type='5 * float64'>

# and finally,

# >>> array = ak.Array([["1.1", "2.2", " 3.3 "], [], ["00004.4", "-5.5"]])
# >>> ak.strings_astype(array, np.float64)
# <Array [[1.1, 2.2, 3.3], [], [4.4, -5.5]] type='3 * var * float64'>

# See also #ak.numbers_astype.
# """
# to_dtype = np.dtype(to)

# def getfunction(layout):
# if isinstance(layout, ak._v2._util.listtypes) and (
# layout.parameter("__array__") == "string"
# or layout.parameter("__array__") == "bytestring"
# ):
# layout = without_parameters(layout, highlevel=False)
# max_length = ak.max(num(layout))
# regulararray = layout.rpad_and_clip(max_length, 1)
# maskedarray = ak._v2.operations.convert.to_numpy(
# regulararray, allow_missing=True
# )
# npstrings = maskedarray.data
# if maskedarray.mask is not False:
# npstrings[maskedarray.mask] = 0
# npnumbers = (
# npstrings.reshape(-1).view("<S" + str(max_length)).astype(to_dtype)
# )
# return lambda: ak._v2.contents.NumpyArray(npnumbers)
# else:
# return None

# layout = ak._v2.operations.convert.to_layout(
# array, allow_record=False, allow_other=False
# )
# out = ak._v2._util.recursively_apply(
# layout,
# getfunction,
# pass_depth=False,
# pass_user=False,
# )
# return ak._v2._util.maybe_wrap_like(out, array, behavior, highlevel)

"""
Args:
array: Array whose strings should be converted to a new numeric type.
to (dtype or dtype specifier): Type to convert the strings into.
highlevel (bool): If True, return an #ak.Array; otherwise, return
a low-level #ak.layout.Content subclass.
behavior (None or dict): Custom #ak.behavior for the output array, if
high-level.

Converts all strings in the array to a new type, leaving the structure
untouched.

For example,

>>> array = ak.Array(["1", "2", " 3 ", "00004", "-5"])
>>> ak.strings_astype(array, np.int32)
<Array [1, 2, 3, 4, -5] type='5 * int32'>

and

>>> array = ak.Array(["1.1", "2.2", " 3.3 ", "00004.4", "-5.5"])
>>> ak.strings_astype(array, np.float64)
<Array [1.1, 2.2, 3.3, 4.4, -5.5] type='5 * float64'>

and finally,

>>> array = ak.Array([["1.1", "2.2", " 3.3 "], [], ["00004.4", "-5.5"]])
>>> ak.strings_astype(array, np.float64)
<Array [[1.1, 2.2, 3.3], [], [4.4, -5.5]] type='3 * var * float64'>

See also #ak.numbers_astype.
"""
to_dtype = np.dtype(to)

def action(layout, **kwargs):
if layout.is_ListType and (
layout.parameter("__array__") == "string"
or layout.parameter("__array__") == "bytestring"
):
layout = ak._v2.operations.structure.without_parameters(
layout, highlevel=False
)
max_length = ak._v2.operations.reducers.max(
ak._v2.operations.structure.num(layout)
)
regulararray = layout.rpad(max_length, 1)
maskedarray = ak._v2.operations.convert.to_numpy(
regulararray, allow_missing=True
)
npstrings = maskedarray.data
if maskedarray.mask is not False:
npstrings[maskedarray.mask] = 0
npnumbers = (
npstrings.reshape(-1).view("<S" + str(max_length)).astype(to_dtype)
)
return ak._v2.contents.NumpyArray(npnumbers)
else:
return None

layout = ak._v2.operations.convert.to_layout(
array, allow_record=False, allow_other=False
)
out = layout.recursively_apply(action)
return ak._v2._util.wrap(out, behavior, highlevel)
1 change: 0 additions & 1 deletion tests/v2/test_0493-zeros-ones-full-like.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def test():
]


@pytest.mark.skip(reason="Tests passing, will be enabled in the strings_astype PR.")
def test_full_like_types():

array = ak._v2.highlevel.Array(
Expand Down
92 changes: 57 additions & 35 deletions tests/v2/test_0813-full-like-dtype-arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import awkward as ak # noqa: F401


@pytest.mark.skip(reason="FIXME: ak.strings_astype not implemented")
def test():
array = ak._v2.Array(
[
Expand Down Expand Up @@ -43,39 +42,51 @@ def assert_array_type(new_array, intended_type):
assert isinstance(new_array[2][4]["y"][3], intended_type)
assert isinstance(new_array[2][4]["y"][4], intended_type)

int_type = ak._v2.operations.structure.full_like(array, 12, dtype=int)
int_type32 = ak._v2.operations.structure.full_like(array, 12, dtype="int32")
int_type64 = ak._v2.operations.structure.full_like(array, 12, dtype="int64")
float_type = ak._v2.operations.structure.full_like(array, 12, dtype=float)
assert int_type.tolist() == float_type.tolist()
assert_array_type(int_type, int)

assert int_type64.tolist() == float_type.tolist()
assert_array_type(int_type32, np.int32)
assert_array_type(int_type64, np.int64)
assert_array_type(float_type, float)

bool_type = ak._v2.operations.structure.full_like(array, 12, dtype=bool)
assert_array_type(bool_type, bool)
assert_array_type(bool_type, np.bool_)

int_type = ak._v2.operations.structure.full_like(array, -1.2, dtype=int)
int_type32 = ak._v2.operations.structure.full_like(array, -1.2, dtype="int32")
int_type64 = ak._v2.operations.structure.full_like(array, -1.2, dtype="int64")
float_type = ak._v2.operations.structure.full_like(array, -1.2, dtype=float)
bool_type = ak._v2.operations.structure.full_like(array, -1.2, dtype=bool)
assert_array_type(int_type, int)

assert_array_type(int_type32, np.int32)
assert_array_type(int_type64, np.int64)
assert_array_type(float_type, float)
assert_array_type(bool_type, bool)
assert_array_type(bool_type, np.bool_)

int_type = ak._v2.operations.structure.zeros_like(array, dtype=int)
int_type32 = ak._v2.operations.structure.zeros_like(array, dtype="int32")
int_type64 = ak._v2.operations.structure.zeros_like(array, dtype="int64")
float_type = ak._v2.operations.structure.zeros_like(array, dtype=float)
bool_type = ak._v2.operations.structure.zeros_like(array, dtype=bool)
assert int_type.tolist() == float_type.tolist()
assert int_type.tolist() == bool_type.tolist()
assert_array_type(int_type, int)

assert int_type64.tolist() == float_type.tolist()
assert int_type64.tolist() == bool_type.tolist()
assert_array_type(int_type32, np.int32)
assert_array_type(int_type64, np.int64)
assert_array_type(float_type, float)
assert_array_type(bool_type, bool)
assert_array_type(bool_type, np.bool_)

int_type = ak._v2.operations.structure.ones_like(array, dtype=int)
int_type32 = ak._v2.operations.structure.ones_like(array, dtype="int32")
int_type64 = ak._v2.operations.structure.ones_like(array, dtype="int64")
float_type = ak._v2.operations.structure.ones_like(array, dtype=float)
bool_type = ak._v2.operations.structure.ones_like(array, dtype=bool)
assert int_type.tolist() == float_type.tolist()
assert int_type.tolist() == bool_type.tolist()
assert_array_type(int_type, int)

assert int_type64.tolist() == float_type.tolist()
assert int_type64.tolist() == bool_type.tolist()
assert_array_type(int_type32, np.int32)
assert_array_type(int_type64, np.int64)
assert_array_type(float_type, float)
assert_array_type(bool_type, bool)
assert_array_type(bool_type, np.bool_)

array = ak.Array([["one", "two", "three"], [], ["four", "five"]])

Expand All @@ -87,36 +98,47 @@ def assert_array_type(new_array, intended_type):
assert isinstance(new_array[2][0], intended_type)
assert isinstance(new_array[2][1], intended_type)

int_type = ak._v2.operations.structure.full_like(array, 12, dtype=int)
int_type32 = ak._v2.operations.structure.full_like(array, 12, dtype="int32")
int_type64 = ak._v2.operations.structure.full_like(array, 12, dtype="int64")
float_type = ak._v2.operations.structure.full_like(array, 12, dtype=float)
assert int_type.tolist() == float_type.tolist()
assert_array_type(int_type, int)

assert int_type64.tolist() == float_type.tolist()
assert_array_type(int_type32, np.int32)
assert_array_type(int_type64, np.int64)
assert_array_type(float_type, float)

bool_type = ak._v2.operations.structure.full_like(array, 12, dtype=bool)
assert_array_type(bool_type, bool)
assert_array_type(bool_type, np.bool_)

int_type = ak._v2.operations.structure.full_like(array, -1.2, dtype=int)
int_type32 = ak._v2.operations.structure.full_like(array, -1.2, dtype="int32")
int_type64 = ak._v2.operations.structure.full_like(array, -1.2, dtype="int64")
float_type = ak._v2.operations.structure.full_like(array, -1.2, dtype=float)
bool_type = ak._v2.operations.structure.full_like(array, -1.2, dtype=bool)
assert_array_type(int_type, int)

assert_array_type(int_type32, np.int32)
assert_array_type(int_type64, np.int64)
assert_array_type(float_type, float)
assert_array_type(bool_type, bool)
assert_array_type(bool_type, np.bool_)

int_type = ak._v2.operations.structure.zeros_like(array, dtype=int)
int_type32 = ak._v2.operations.structure.zeros_like(array, dtype="int32")
int_type64 = ak._v2.operations.structure.zeros_like(array, dtype="int64")
float_type = ak._v2.operations.structure.zeros_like(array, dtype=float)
bool_type = ak._v2.operations.structure.zeros_like(array, dtype=bool)
assert int_type.tolist() == float_type.tolist()
assert int_type.tolist() == bool_type.tolist()
assert_array_type(int_type, int)

assert int_type64.tolist() == float_type.tolist()
assert int_type64.tolist() == bool_type.tolist()
assert_array_type(int_type32, np.int32)
assert_array_type(int_type64, np.int64)
assert_array_type(float_type, float)
assert_array_type(bool_type, bool)
assert_array_type(bool_type, np.bool_)

int_type = ak._v2.operations.structure.ones_like(array, dtype=int)
int_type32 = ak._v2.operations.structure.ones_like(array, dtype="int32")
int_type64 = ak._v2.operations.structure.ones_like(array, dtype="int64")
float_type = ak._v2.operations.structure.ones_like(array, dtype=float)
bool_type = ak._v2.operations.structure.ones_like(array, dtype=bool)
assert int_type.tolist() == float_type.tolist()
assert int_type.tolist() == bool_type.tolist()
assert_array_type(int_type, int)
assert int_type64.tolist() == float_type.tolist()
assert int_type64.tolist() == bool_type.tolist()
assert_array_type(int_type32, np.int32)
assert_array_type(int_type64, np.int64)
assert_array_type(float_type, float)
assert_array_type(bool_type, bool)
assert_array_type(bool_type, np.bool_)