Repository navigation
fix(rntuple): preserve optional records when writing and reading (#1713) #1779
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
Open
sumitsaraswat362
wants to merge
1
commit into
scikit-hep:main
Choose a base branch
from
sumitsaraswat362:fix-1713-rntuple-optional-records
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| # BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import awkward as ak | ||
| import pytest | ||
|
|
||
| import uproot | ||
|
|
||
|
|
||
| def test_optional_record_round_trip(tmp_path): | ||
| arr = ak.Array([{"o": {"a": 1.0}}, {"o": None}]) | ||
| path = tmp_path / "opt_record.root" | ||
|
|
||
| with uproot.recreate(path) as f: | ||
| f["t"] = arr | ||
|
|
||
| with uproot.open(path) as f: | ||
| nt = f["t"] | ||
| assert nt.arrays().tolist() == arr.tolist() | ||
| assert nt["o"].array().tolist() == arr["o"].tolist() | ||
| assert nt["o.a"].array().tolist() == [1.0, None] | ||
| assert nt.arrays(filter_name="o")["o"].tolist() == arr["o"].tolist() | ||
| assert nt["o"].typename == "" | ||
| assert nt["o"].record.type_alias == "std::optional" | ||
| assert [field.typename for field in nt.all_fields] == ["", "", "double"] | ||
|
|
||
|
|
||
| def test_optional_record_with_multiple_fields(tmp_path): | ||
| arr = ak.Array([{"o": {"a": 1.0, "b": 2}}, {"o": None}]) | ||
| path = tmp_path / "opt_multi.root" | ||
|
|
||
| with uproot.recreate(path) as f: | ||
| f["t"] = arr | ||
|
|
||
| with uproot.open(path) as f: | ||
| nt = f["t"] | ||
| assert nt.arrays().tolist() == arr.tolist() | ||
| assert nt["o.a"].array().tolist() == [1.0, None] | ||
| assert nt["o.b"].array().tolist() == [2, None] | ||
| assert nt["o"].typename == "" | ||
| assert nt["o"].record.type_alias == "std::optional" | ||
| assert [field.typename for field in nt.all_fields] == [ | ||
| "", | ||
| "", | ||
| "double", | ||
| "std::int64_t", | ||
| ] | ||
|
|
||
|
|
||
| def test_optional_record_nested_tuple(tmp_path): | ||
| arr = ak.Array([{"o": (1.0, {"x": 2})}, {"o": None}]) | ||
| path = tmp_path / "opt_tuple.root" | ||
|
|
||
| with uproot.recreate(path) as f: | ||
| f["t"] = arr | ||
|
|
||
| with uproot.open(path) as f: | ||
| nt = f["t"] | ||
| assert nt.arrays().tolist() == arr.tolist() | ||
| assert nt["o"].array().tolist() == arr["o"].tolist() | ||
| assert nt["o"].typename == "" | ||
| assert nt["o"].record.type_alias == "std::optional" | ||
| assert [field.typename for field in nt.all_fields] == [ | ||
| "", | ||
| "", | ||
| "double", | ||
| "", | ||
| "std::int64_t", | ||
| ] | ||
|
|
||
|
|
||
| def test_optional_record_nested_list(tmp_path): | ||
| arr = ak.Array([{"o": [{"a": 1.0}]}, {"o": None}]) | ||
| path = tmp_path / "opt_list.root" | ||
|
|
||
| with uproot.recreate(path) as f: | ||
| f["t"] = arr | ||
|
|
||
| with uproot.open(path) as f: | ||
| nt = f["t"] | ||
| assert nt.arrays().tolist() == arr.tolist() | ||
| assert nt["o"].array().tolist() == arr["o"].tolist() | ||
| assert nt["o"].typename == "" | ||
| assert nt["o"].record.type_alias == "std::optional" | ||
| assert [field.typename for field in nt.all_fields] == ["", "", "", "double"] | ||
|
|
||
|
|
||
| def test_optional_record_extend(tmp_path): | ||
| arr1 = ak.Array([{"o": {"a": 1.0}}, {"o": None}]) | ||
| arr2 = ak.Array([{"o": None}, {"o": {"a": 2.0}}]) | ||
| path = tmp_path / "opt_extend.root" | ||
|
|
||
| with uproot.recreate(path) as f: | ||
| f["t"] = arr1 | ||
|
|
||
| with uproot.update(path) as f: | ||
| f["t"].extend(arr2) | ||
|
|
||
| with uproot.open(path) as f: | ||
| expected = ak.concatenate([arr1, arr2]) | ||
| assert f["t"].arrays().tolist() == expected.tolist() | ||
|
|
||
|
|
||
| def test_optional_record_dask(tmp_path): | ||
| pytest.importorskip("dask_awkward") | ||
|
|
||
| arr1 = ak.Array([{"o": {"a": 1.0}}, {"o": None}]) | ||
| arr2 = ak.Array([{"o": None}, {"o": {"a": 2.0}}]) | ||
| path = tmp_path / "opt_dask.root" | ||
|
|
||
| with uproot.recreate(path) as f: | ||
| f["t"] = arr1 | ||
|
|
||
| with uproot.update(path) as f: | ||
| f["t"].extend(arr2) | ||
|
|
||
| expected = ak.concatenate([arr1, arr2]) | ||
| dask_arr = uproot.dask(str(path) + ":t") | ||
| assert dask_arr.compute().tolist() == expected.tolist() | ||
|
|
||
|
|
||
| def test_optional_record_ROOT(tmp_path, capfd): | ||
| ROOT = pytest.importorskip("ROOT") | ||
| if ROOT.gROOT.GetVersionInt() < 63400: | ||
| pytest.skip("ROOT version does not support RNTuple v1.0.0.0") | ||
|
|
||
| filepath = str(tmp_path / "opt_record_root.root") | ||
| arr = ak.Array([{"o": {"a": 1.0}}, {"o": None}]) | ||
|
|
||
| with uproot.recreate(filepath) as f: | ||
| f["ntuple"] = arr | ||
|
|
||
| if ROOT.gROOT.GetVersionInt() < 63600: | ||
| RT = ROOT.Experimental.RNTupleReader.Open("ntuple", filepath) | ||
| else: | ||
| RT = ROOT.RNTupleReader.Open("ntuple", filepath) | ||
| RT.PrintInfo() | ||
| out = capfd.readouterr().out | ||
| assert "ntuple" in out | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 AI text below 🤖
These tests only round-trip through uproot, so they pass even though ROOT can't open the file. Could you add a ROOT read-back check guarded by
pytest.importorskip("ROOT"), liketests/test_0705_rntuple_writing_metadata.pydoes? It would have caught the issue above.