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

Feature/recursive members #2118

Merged
merged 11 commits into from
Aug 29, 2024

Conversation

TomAugspurger
Copy link
Contributor

@TomAugspurger TomAugspurger commented Aug 25, 2024

(This currently depends on #2117. I'll rebase once that's merged to v3)

This PR adds a recursive=True flag to Group.members, for recursively
listing the members of some hierarchy. The default is unchanged (just list immediate children).

This includes a breaking change to Group.members and Group.nmembers. Previously, these were properties, while AsyncGroup.members / nmembers were functions. I've changed the sync versions to align with the async versions (which is needed to support this keyword).

This is useful for Consolidated Metadata, which needs to recursively
inspect children. IMO, it's useful (and simple) enough to include
in the public API.

Closes #2119

TODO:

  • Add unit tests and/or doctests in docstrings
  • Add docstrings and API docs for any new/modified user-facing classes and functions
  • New/modified features documented in docs/tutorial.rst
  • Changes documented in docs/release.rst
  • GitHub Actions have all passed
  • Test coverage is 100% (Codecov passes)

Ensures that nested children are listed properly.
This PR adds a recursive=True flag to Group.members, for recursively
listing the members of some hierarhcy.

This is useful for Consolidated Metadata, which needs to recursively
inspect children. IMO, it's useful (and simple) enough to include
in the public API.
@TomAugspurger
Copy link
Contributor Author

TomAugspurger commented Aug 25, 2024

#2119 has some information on the failed hypothesis test. I think it's real.

Edit: 8bd52d0 hopefully fixes that.

@TomAugspurger
Copy link
Contributor Author

I'm pretty stumped by the failure at https://github.com/zarr-developers/zarr-python/actions/runs/10549846136/job/29225363145?pr=2118#step:6:374

The issue comes from a RuntimeError in numpy when doing something like

np.array([np.complex128(0.0, np.nan)]) == np.complex128(0)

The actual array constructed by hypothesis is a bit different though. It's more like:

In [23]: np.ndarray((1,), np.dtype("complex128"), b"\x00\x00\x00\x00\x00\x00\x00\x00\xfd\x7f\xbaUFJ\xf0\x7f")[0]
Out[23]: np.complex128(nanj)

The dtype and repr appear to be the same, but apparently they're different internally. Not sure how yet.

Finally, something in the test runner appears to elevate the RuntimeWarning to an error:

# inside the test run
array([False])
(Pdb) np.array([np.complex128(0.0, np.nan)]) == np.complex128(0)
array([False])
(Pdb) np.ndarray((1,), np.dtype("complex128"), b"\x00\x00\x00\x00\x00\x00\x00\x00\xfd\x7f\xbaUFJ\xf0\x7f") == np.complex128(0)
*** RuntimeWarning: invalid value encountered in equal

@TomAugspurger
Copy link
Contributor Author

I'm partly giving up on that second hypothesis failure. It's possible that there's still a bug in Buffer.all_equal at

def all_equal(self, other: Any) -> bool:
return bool((self._data == other).all())
not handling NaN like other values. But for this PR, we're able to work around it by filtering out that RuntimeWarning.

This should be ready for review now.

@jhamman jhamman mentioned this pull request Aug 26, 2024
6 tasks
Copy link
Member

@jhamman jhamman left a comment

Choose a reason for hiding this comment

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

This includes a breaking change to Group.members and Group.nmembers. Previously, these were properties, while AsyncGroup.members / nmembers were functions. I've changed the sync versions to align with the async versions (which is needed to support this keyword).

Agreed that this is useful enough to justify this change.

# See https://github.com/zarr-developers/zarr-python/pull/2118#issuecomment-2310280899
# Uncomment the next line to reproduce the original failure.
# @reproduce_failure('6.111.2', b'AXicY2FgZGRAB/8/eLmF7qr/C5EDADZUBRM=')
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
Copy link
Member

Choose a reason for hiding this comment

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

@dcherian - would you mind looking into this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a minefield 😬 The deepest I got was hypothesis.extra.numpy:ArrayStrategy.set_element. Something about the sequence of operations and the data passed into the ndarray made it "weird", such that array == np.complex128(0.0) raised an InvalidComparision warning. I'm not sure how to interpret the bytes at #2118 (comment).

@jhamman jhamman requested a review from d-v-b August 26, 2024 20:22
@d-v-b
Copy link
Contributor

d-v-b commented Aug 27, 2024

this looks good to me! my one question is whether there would be any utility in using an integer to limit the recursion depth to a desired number of hierarchy levels, while preserving the option to traverse the entire group. e.g., the value of 1 would denote gathering immediate sub-arrays and sub-groups, 2 would recurse once, and -1 would gather the whole hierarchy. I did this over in pydantic-zarr and it has worked well so far. Without this functionality, we are basically forcing users with very deep zarr hierarchies to choose between expensive IO (recursive=True) and writing their own semi-recursive implementations (manually calling members N times).

@TomAugspurger
Copy link
Contributor Author

I was wondering about a limit-style argument too. I'll go ahead and implement that.

@TomAugspurger
Copy link
Contributor Author

I opted to implement this as a max_depth: int | None keyword, which replaces recursive=True. By setting max_depth=-1 or max_depth=None you get the same behavior as recursive=True. A positive integer considers just that depth. The default of max_depth=0 is the same as the existing behavior on main of just considering immediate children.

@d-v-b
Copy link
Contributor

d-v-b commented Aug 28, 2024

thanks @TomAugspurger! what's the reasoning for specifying the same behavior with two distinct values -- max_depth=-1 and max_depth=None? Personally I would be fine with XOR on these options, and it would make things marginally simpler. But maybe there's an advantage to your current approach that I'm not considering.

@TomAugspurger
Copy link
Contributor Author

Purely for user convenience. Neither max_depth=-1 nor max_depth=None sounded obviously more natural to express "no depth".

If I had to choose, I'd say max_depth=None. Do you have a preference?

@d-v-b
Copy link
Contributor

d-v-b commented Aug 28, 2024

If I had to choose, I'd say max_depth=None. Do you have a preference?

I like this too!

Copy link
Contributor

@d-v-b d-v-b left a comment

Choose a reason for hiding this comment

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

this looks good, pre-commit is red but I don't think that failure is related to content in the PR. If you're all done here @TomAugspurger then I am happy to merge

@TomAugspurger
Copy link
Contributor Author

Yeah, this should be all set.

The pre-commit errors are at #2131, I think from a new version of NumPy. I'll look into them today.

@d-v-b d-v-b merged commit 0095ee6 into zarr-developers:v3 Aug 29, 2024
24 of 25 checks passed
@TomAugspurger TomAugspurger deleted the feature/recursive-members branch August 29, 2024 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flaky test / buggy handling of large datetimes
3 participants