Skip to content

fix: hide anonymous RNTuple fields by default #1469

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
49 changes: 31 additions & 18 deletions src/uproot/behaviors/RNTuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ def to_akform(
filter_typename=filter_typename,
filter_field=filter_field,
filter_branch=filter_branch,
include_hidden=True,
)
rntuple = self.ntuple

Expand Down Expand Up @@ -1099,6 +1100,7 @@ def keys(
recursive=True,
full_paths=True,
ignore_duplicates=False,
include_hidden=False,
# For compatibility reasons we also accepts kwargs meant for TTrees
filter_branch=unset,
):
Expand All @@ -1118,6 +1120,7 @@ def keys(
with periods (``.``); otherwise, use the descendant's name as
the output name.
ignore_duplicates (bool): If True, return a set of the keys; otherwise, return the full list of keys.
include_hidden (bool): If True, include hidden fields (starting with an underscore) in the output.
filter_branch (None or function of :doc:`uproot.models.RNTuple.RField` \u2192 bool): An alias for ``filter_field`` included
for compatibility with software that was used for :doc:`uproot.behaviors.TBranch.TBranch`. This argument should not be used
and will be removed in a future version.
Expand All @@ -1132,6 +1135,7 @@ def keys(
recursive=recursive,
full_paths=full_paths,
ignore_duplicates=ignore_duplicates,
include_hidden=include_hidden,
filter_branch=filter_branch,
)
)
Expand All @@ -1143,6 +1147,7 @@ def values(
filter_typename=no_filter,
filter_field=no_filter,
recursive=True,
include_hidden=False,
# For compatibility reasons we also accepts kwargs meant for TTrees
filter_branch=unset,
):
Expand All @@ -1158,6 +1163,7 @@ def values(
included if the function returns True, excluded if it returns False.
recursive (bool): If True, descend into any nested subfields.
If False, only return the names of the top fields.
include_hidden (bool): If True, include hidden fields (starting with an underscore) in the output.
filter_branch (None or function of :doc:`uproot.models.RNTuple.RField` \u2192 bool): An alias for ``filter_field`` included
for compatibility with software that was used for :doc:`uproot.behaviors.TBranch.TBranch`. This argument should not be used
and will be removed in a future version.
Expand All @@ -1174,6 +1180,7 @@ def values(
filter_typename=filter_typename,
filter_field=filter_field,
recursive=recursive,
include_hidden=include_hidden,
filter_branch=filter_branch,
)
)
Expand All @@ -1186,6 +1193,7 @@ def items(
filter_field=no_filter,
recursive=True,
full_paths=True,
include_hidden=False,
# For compatibility reasons we also accepts kwargs meant for TTrees
filter_branch=unset,
):
Expand All @@ -1204,6 +1212,7 @@ def items(
full_paths (bool): If True, include the full path to each subfield
with periods (``.``); otherwise, use the descendant's name as
the output name.
include_hidden (bool): If True, include hidden fields (starting with an underscore) in the output.
filter_branch (None or function of :doc:`uproot.models.RNTuple.RField` \u2192 bool): An alias for ``filter_field`` included
for compatibility with software that was used for :doc:`uproot.behaviors.TBranch.TBranch`. This argument should not be used
and will be removed in a future version.
Expand All @@ -1218,6 +1227,7 @@ def items(
filter_field=filter_field,
recursive=recursive,
full_paths=full_paths,
include_hidden=include_hidden,
filter_branch=filter_branch,
)
)
Expand All @@ -1230,6 +1240,7 @@ def typenames(
filter_field=no_filter,
recursive=True,
full_paths=True,
include_hidden=False,
# For compatibility reasons we also accepts kwargs meant for TTrees
filter_branch=unset,
):
Expand All @@ -1248,6 +1259,7 @@ def typenames(
full_paths (bool): If True, include the full path to each subfield
with periods (``.``); otherwise, use the descendant's name as
the output name.
include_hidden (bool): If True, include hidden fields (starting with an underscore) in the output.
filter_branch (None or function of :doc:`uproot.models.RNTuple.RField` \u2192 bool): An alias for ``filter_field`` included
for compatibility with software that was used for :doc:`uproot.behaviors.TBranch.TBranch`. This argument should not be used
and will be removed in a future version.
Expand All @@ -1262,6 +1274,7 @@ def typenames(
filter_field=filter_field,
recursive=recursive,
full_paths=full_paths,
include_hidden=include_hidden,
filter_branch=filter_branch,
)
)
Expand All @@ -1275,6 +1288,7 @@ def iterkeys(
recursive=True,
full_paths=True,
ignore_duplicates=False,
include_hidden=False,
# For compatibility reasons we also accepts kwargs meant for TTrees
filter_branch=unset,
):
Expand All @@ -1294,6 +1308,7 @@ def iterkeys(
with periods (``.``); otherwise, use the descendant's name as
the output name.
ignore_duplicates (bool): If True, return a set of the keys; otherwise, return the full list of keys.
include_hidden (bool): If True, include hidden fields (starting with an underscore) in the output.
filter_branch (None or function of :doc:`uproot.models.RNTuple.RField` \u2192 bool): An alias for ``filter_field`` included
for compatibility with software that was used for :doc:`uproot.behaviors.TBranch.TBranch`. This argument should not be used
and will be removed in a future version.
Expand All @@ -1308,6 +1323,7 @@ def iterkeys(
recursive=recursive,
full_paths=full_paths,
ignore_duplicates=ignore_duplicates,
include_hidden=include_hidden,
filter_branch=filter_branch,
):
yield k
Expand All @@ -1319,6 +1335,7 @@ def itervalues(
filter_typename=no_filter,
filter_field=no_filter,
recursive=True,
include_hidden=False,
# For compatibility reasons we also accepts kwargs meant for TTrees
filter_branch=unset,
):
Expand All @@ -1334,6 +1351,7 @@ def itervalues(
included if the function returns True, excluded if it returns False.
recursive (bool): If True, descend into any nested subfields.
If False, only return the names of the top fields.
include_hidden (bool): If True, include hidden fields (starting with an underscore) in the output.
filter_branch (None or function of :doc:`uproot.models.RNTuple.RField` \u2192 bool): An alias for ``filter_field`` included
for compatibility with software that was used for :doc:`uproot.behaviors.TBranch.TBranch`. This argument should not be used
and will be removed in a future version.
Expand All @@ -1350,6 +1368,7 @@ def itervalues(
filter_field=filter_field,
recursive=recursive,
full_paths=False,
include_hidden=include_hidden,
filter_branch=filter_branch,
):
yield v
Expand All @@ -1363,6 +1382,7 @@ def iteritems(
recursive=True,
full_paths=True,
ignore_duplicates=False,
include_hidden=False,
# For compatibility reasons we also accepts kwargs meant for TTrees
filter_branch=unset,
):
Expand All @@ -1382,6 +1402,7 @@ def iteritems(
with periods (``.``); otherwise, use the descendant's name as
the output name.
ignore_duplicates (bool): If True, return a set of the keys; otherwise, return the full list of keys.
include_hidden (bool): If True, include hidden fields (starting with an underscore) in the output.
filter_branch (None or function of :doc:`uproot.models.RNTuple.RField` \u2192 bool): An alias for ``filter_field`` included
for compatibility with software that was used for :doc:`uproot.behaviors.TBranch.TBranch`. This argument should not be used
and will be removed in a future version.
Expand Down Expand Up @@ -1419,6 +1440,7 @@ def iteritems(
)
and (filter_typename is no_filter or filter_typename(field.typename))
and (filter_field is no_filter or filter_field(field))
and (include_hidden or not field.name.startswith("_"))
):
if ignore_duplicates and field.name in keys_set:
pass
Expand All @@ -1433,6 +1455,7 @@ def iteritems(
filter_typename=filter_typename,
filter_field=filter_field,
full_paths=full_paths,
include_hidden=include_hidden,
):
k2 = f"{field.name}.{k1}" if full_paths else k1
if filter_name is no_filter or _filter_name_deep(
Expand All @@ -1452,6 +1475,7 @@ def itertypenames(
filter_field=no_filter,
recursive=True,
full_paths=True,
include_hidden=False,
# For compatibility reasons we also accepts kwargs meant for TTrees
filter_branch=unset,
):
Expand All @@ -1470,6 +1494,7 @@ def itertypenames(
full_paths (bool): If True, include the full path to each subfield
with periods (``.``); otherwise, use the descendant's name as
the output name.
include_hidden (bool): If True, include hidden fields (starting with an underscore) in the output.
filter_branch (None or function of :doc:`uproot.models.RNTuple.RField` \u2192 bool): An alias for ``filter_field`` included
for compatibility with software that was used for :doc:`uproot.behaviors.TBranch.TBranch`. This argument should not be used
and will be removed in a future version.
Expand All @@ -1480,9 +1505,11 @@ def itertypenames(
for k, v in self.iteritems(
filter_name=filter_name,
filter_typename=filter_typename,
filter_branch=filter_branch,
filter_field=filter_field,
recursive=recursive,
full_paths=full_paths,
include_hidden=include_hidden,
filter_branch=filter_branch,
):
yield k, v.typename

Expand Down Expand Up @@ -1612,7 +1639,7 @@ def __getitem__(self, where):
except uproot.KeyInFileError:
raise uproot.KeyInFileError(
original_where,
keys=self.keys(recursive=recursive),
keys=self.keys(recursive=recursive, include_hidden=True),
file_path=self._file.file_path, # TODO
object_path=self.object_path, # TODO
) from None
Expand All @@ -1625,15 +1652,15 @@ def __getitem__(self, where):
else:
raise uproot.KeyInFileError(
original_where,
keys=self.keys(recursive=recursive),
keys=self.keys(recursive=recursive, include_hidden=True),
file_path=self._file.file_path,
object_path=self.object_path,
)

else:
raise uproot.KeyInFileError(
original_where,
keys=self.keys(recursive=recursive),
keys=self.keys(recursive=recursive, include_hidden=True),
file_path=self._file.file_path,
object_path=self.object_path,
)
Expand Down Expand Up @@ -1823,20 +1850,6 @@ def _filter_name_deep(filter_name, hasfields, field):
return filter_name("." + name)


def _keys_deep(hasbranches):
out = set()
for branch in hasbranches.itervalues(recursive=True):
name = branch.name
out.add(name)
while branch is not hasbranches:
branch = branch.parent # noqa: PLW2901 (overwriting branch)
if branch is not hasbranches:
name = branch.name + "/" + name
out.add(name)
out.add("/" + name)
return out


def _get_recursive(hasfields, where):
if hasfields._lookup is None:
hasfields._lookup = {f.name: f for f in hasfields.fields}
Expand Down
17 changes: 12 additions & 5 deletions tests/test_1406_improved_rntuple_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,27 @@ def test_keys(tmp_path):
assert len(obj) == 5
assert len(obj.keys(recursive=False)) == 5

assert len(obj.keys()) == 29
assert len(obj.keys(full_paths=False)) == 29
assert len(obj.keys(full_paths=False, ignore_duplicates=True)) == 16
assert len(obj.keys(include_hidden=True)) == 29
assert len(obj.keys(full_paths=False, include_hidden=True)) == 29
assert (
len(obj.keys(full_paths=False, ignore_duplicates=True, include_hidden=True))
== 16
)

assert len(obj.keys()) == 22
assert len(obj.keys(full_paths=False)) == 22
assert len(obj.keys(full_paths=False, ignore_duplicates=True)) == 13

assert len(obj.keys(filter_name="x")) == 4
assert len(obj.keys(filter_name="z")) == 2
assert len(obj.keys(filter_name="do*")) == 1

assert len(obj.keys(filter_typename="std::int*_t")) == 16
assert len(obj.keys(filter_typename="std::int*_t")) == 13

assert len(obj.keys(filter_field=lambda f: f.name == "up")) == 1

assert obj["struct1"].keys() == ["x", "y"]
assert len(obj["struct4"].keys()) == 12
assert len(obj["struct4"].keys()) == 8


def test_getitem(tmp_path):
Expand Down
Loading