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.run_lengths #1347

Merged
merged 2 commits into from
Mar 8, 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
58 changes: 31 additions & 27 deletions src/awkward/_v2/behaviors/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,44 +100,48 @@ def __str__(self):
# yield x.__bytes__()


# class StringBehavior(ak._v2.highlevel.Array):
# __name__ = "Array"

# def __iter__(self):
# for x in super(StringBehavior, self).__iter__():
# yield x.__str__()
class StringBehavior(Array):
__name__ = "Array"

def __iter__(self):
for x in super().__iter__():
yield x.__str__()

# def _string_equal(one, two):
# nplike = ak.nplike.of(one, two)
# behavior = ak._v2._util.behaviorof(one, two)

# one, two = ak.without_parameters(one).layout, ak.without_parameters(two).layout
def _string_equal(one, two):
nplike = ak.nplike.of(one, two)
behavior = ak._v2._util.behavior_of(one, two)

# # first condition: string lengths must be the same
# counts1 = nplike.asarray(one.count(axis=-1))
# counts2 = nplike.asarray(two.count(axis=-1))
one, two = (
ak._v2.operations.structure.without_parameters(one).layout,
ak._v2.operations.structure.without_parameters(two).layout,
)

# out = counts1 == counts2
# first condition: string lengths must be the same
counts1 = nplike.asarray(one.count(axis=-1))
counts2 = nplike.asarray(two.count(axis=-1))

# # only compare characters in strings that are possibly equal (same length)
# possible = nplike.logical_and(out, counts1)
# possible_counts = counts1[possible]
out = counts1 == counts2

# if len(possible_counts) > 0:
# onepossible = one[possible]
# twopossible = two[possible]
# only compare characters in strings that are possibly equal (same length)
possible = nplike.logical_and(out, counts1)
possible_counts = counts1[possible]

# reduced = ak.all(ak.Array(onepossible) == ak.Array(twopossible), axis=-1).layout
if len(possible_counts) > 0:
onepossible = one[possible]
twopossible = two[possible]

# # update same-length strings with a verdict about their characters
# out[possible] = reduced
reduced = ak._v2.operations.reducers.all(
ak._v2.Array(onepossible) == ak._v2.Array(twopossible), axis=-1
).layout
# update same-length strings with a verdict about their characters
out[possible] = reduced.data

# return ak._v2._util.wrap(ak._v2.contents.NumpyArray(out), behavior)
return ak._v2._util.wrap(ak._v2.contents.NumpyArray(out), behavior)


# def _string_notequal(one, two):
# return ~_string_equal(one, two)
def _string_notequal(one, two):
return ~_string_equal(one, two)


# def _string_broadcast(layout, offsets):
Expand Down Expand Up @@ -250,7 +254,7 @@ def register(behavior):
# behavior[ak.nplike.numpy.equal, "bytestring", "bytestring"] = _string_equal
# behavior[ak.nplike.numpy.equal, "string", "string"] = _string_equal
# behavior[ak.nplike.numpy.not_equal, "bytestring", "bytestring"] = _string_notequal
# behavior[ak.nplike.numpy.not_equal, "string", "string"] = _string_notequal
behavior[ak.nplike.numpy.not_equal, "string", "string"] = _string_notequal
Comment on lines 254 to +257
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With _string_equal implemented, it should be possible to enable all four of these. I guess only one was needed for the test. (Enabling these string and categorical behaviors is one of the cards.)


# behavior["__broadcast__", "bytestring"] = _string_broadcast
# behavior["__broadcast__", "string"] = _string_broadcast
Expand Down
Loading