Skip to content

Commit

Permalink
AsObjects.simplify, leaf-lists, and test Double32. (#33)
Browse files Browse the repository at this point in the history
* AsObjects.simplify, leaf-lists, and test Double32.

* Refactor: AsString and AsStrings take length_bytes in ('1-5', '4'), rather than two booleans.

* AsObject.simplify maps AsString -> AsStrings.

* AsObject.simplify maps AsVector(dtype) -> AsJagged(AsDtype).

* Strings in Awkward1.

* Strings in Pandas.

* Mapped leaflist onto Awkward RecordArrays.

* Mapped leaflist onto Pandas columns.

* Mapped fixed-size arrays onto Pandas columns.

* Implemented Double32 and Float16, but truncated and Float16 are not being tested.

* Implemented TLeafD32 and TLeafF16, but they're not reading back correctly yet.

* Finished TLeafD32 and TLeafF16.
  • Loading branch information
jpivarski authored Jun 27, 2020
1 parent 60f2851 commit 11e6975
Show file tree
Hide file tree
Showing 21 changed files with 928 additions and 160 deletions.
8 changes: 4 additions & 4 deletions tests/test_0014-all-ttree-versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ def test_list_streamers():
== """TString (v2)
TObject (v1)
fUniqueID: unsigned int
fBits: unsigned int
fUniqueID: unsigned int (TStreamerBasicType)
fBits: unsigned int (TStreamerBasicType)
TNamed (v1): TObject (v1)
fName: TString
fTitle: TString
fName: TString (TStreamerString)
fTitle: TString (TStreamerString)
"""
)
296 changes: 285 additions & 11 deletions tests/test_0023-more-interpretations-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,8 @@ def test_strings4():
]


@pytest.mark.skip(reason="FIXME: implement Double32")
def test_double32():
del uproot4.classes["TBranch"]
del uproot4.classes["TBranchElement"]

with uproot4.open(skhep_testdata.data_path("uproot-demo-double32.root"))["T"] as t:

print(t["fD64"].interpretation)
print(t["fF32"].interpretation)
print(t["fI32"].interpretation)
print(t["fI30"].interpretation)
print(t["fI28"].interpretation)

fD64 = t["fD64"].array(library="np")
fF32 = t["fF32"].array(library="np")
fI32 = t["fI32"].array(library="np")
Expand All @@ -110,3 +99,288 @@ def test_double32():
assert ratio_fI32.min() > 0.9999 and ratio_fI32.max() < 1.0001
assert ratio_fI30.min() > 0.9999 and ratio_fI30.max() < 1.0001
assert ratio_fI28.min() > 0.9999 and ratio_fI28.max() < 1.0001


def test_double32_2():
with uproot4.open(skhep_testdata.data_path("uproot-issue187.root"))["fTreeV0"] as t:
assert numpy.all(t["fMultiplicity"].array(library="np") == -1)
assert t["V0s.fEtaPos"].array(library="np")[-3].tolist() == [
-0.390625,
0.046875,
]


def test_double32_3():
with uproot4.open(skhep_testdata.data_path("uproot-issue232.root"))["fTreeV0"] as t:
assert t["V0Hyper.fNsigmaHe3Pos"].array(library="np")[-1].tolist() == [
19.38658905029297,
999.0,
]
assert t["V0Hyper.fDcaPos2PrimaryVertex"].array(library="np")[-1].tolist() == [
0.256,
0.256,
]


def test_double32_float16():
with uproot4.open(skhep_testdata.data_path("uproot-double32-float16.root"))[
"tree"
] as t:
assert repr(t["double32_32"].interpretation) == "AsDouble32(-2.71, 10.0, 32)"
assert repr(t["double32_30"].interpretation) == "AsDouble32(-2.71, 10.0, 30)"
assert repr(t["double32_20"].interpretation) == "AsDouble32(-2.71, 10.0, 20)"
assert repr(t["double32_10"].interpretation) == "AsDouble32(-2.71, 10.0, 10)"
assert repr(t["double32_5"].interpretation) == "AsDouble32(-2.71, 10.0, 5)"
assert repr(t["double32_3"].interpretation) == "AsDouble32(-2.71, 10.0, 3)"
assert repr(t["float16_16"].interpretation) == "AsFloat16(-2.71, 10.0, 16)"
assert repr(t["float16_10"].interpretation) == "AsFloat16(-2.71, 10.0, 10)"
assert repr(t["float16_5"].interpretation) == "AsFloat16(-2.71, 10.0, 5)"
assert repr(t["float16_3"].interpretation) == "AsFloat16(-2.71, 10.0, 3)"
assert (
repr(t["array_30"].interpretation)
== "AsDouble32(-2.71, 10.0, 30, to_dims=(3,))"
)
assert (
repr(t["array_10"].interpretation)
== "AsFloat16(-2.71, 10.0, 10, to_dims=(3,))"
)

assert t["double32_32"].array(library="np").tolist() == [
-1.9999999994342215,
-1.4999999998277052,
-1.0000000002211891,
-0.50000000061467276,
-0.10000000329688152,
-1.0081566692576871e-09,
0.10000000128056863,
0.49999999859835986,
0.99999999820487595,
2.0000000003771863,
2.9999999995902185,
3.9999999988032506,
4.9999999980162837,
5.9999999972293159,
6.9999999964423489,
7.9999999986146593,
8.9999999978276897,
]

assert t["double32_30"].array(library="np").tolist() == [
-2.0000000023934987,
-1.5000000057462601,
-0.99999999726191158,
-0.50000000061467276,
-0.10000000329688152,
-3.9674339369355494e-09,
0.10000000719912361,
0.50000000451691484,
1.0000000011641537,
1.9999999944586309,
2.9999999995902185,
4.0000000047218061,
4.9999999980162837,
6.0000000031478704,
6.9999999964423489,
8.0000000015739374,
8.9999999948684142,
]

assert t["double32_20"].array(library="np").tolist() == [
-2.0000006771087646,
-1.5000011539459228,
-1.0000016307830808,
-0.50000210762023922,
-0.10000248908996578,
-2.5844573974254104e-06,
0.099997320175170934,
0.49999693870544437,
0.99999646186828661,
1.9999955081939698,
2.9999945545196534,
4.0000057220458993,
5.0000047683715829,
6.0000038146972665,
7.0000028610229501,
8.0000019073486328,
9.0000009536743164,
]

assert t["double32_10"].array(library="np").tolist() == [
-2.0025097656249997,
-1.5060253906249998,
-0.99712890624999995,
-0.50064453124999986,
-0.10345703124999961,
-0.0041601562499997691,
0.095136718750000071,
0.50473632812500036,
1.0012207031250004,
1.9941894531250002,
2.9995703125000004,
4.0049511718750006,
4.9979199218750008,
6.0033007812500001,
6.9962695312500012,
8.0016503906250023,
8.9946191406250016,
]

assert t["double32_5"].array(library="np").tolist() == [
-1.9156249999999999,
-1.5184374999999999,
-1.1212499999999999,
-0.3268749999999998,
0.0703125,
0.0703125,
0.0703125,
0.46750000000000025,
0.8646875000000005,
2.0562500000000004,
2.850625,
4.0421875000000007,
4.8365625000000003,
6.0281250000000002,
6.8225000000000007,
8.0140625000000014,
8.8084375000000001,
]

assert t["double32_3"].array(library="np").tolist() == [
-2.71,
-1.1212499999999999,
-1.1212499999999999,
-1.1212499999999999,
0.46750000000000025,
0.46750000000000025,
0.46750000000000025,
0.46750000000000025,
0.46750000000000025,
2.0562500000000004,
3.6450000000000005,
3.6450000000000005,
5.233750000000001,
5.233750000000001,
6.822500000000001,
8.411249999999999,
8.411249999999999,
]

assert t["float16_16"].array(library="np").tolist() == [
-1.9999885559082031,
-1.5000133514404297,
-1.0000380277633667,
-0.50006270408630371,
-0.099966049194335938,
-8.7499618530273438e-05,
0.099985122680664062,
0.50008177757263184,
1.0000569820404053,
2.0000076293945312,
2.9999580383300781,
3.9999089241027832,
5.0000534057617188,
6.0000038146972656,
6.9999542236328125,
7.9999046325683594,
9.0000495910644531,
]

assert t["float16_10"].array(library="np").tolist() == [
-2.0025098323822021,
-1.5060254335403442,
-0.99712896347045898,
-0.50064444541931152,
-0.10345697402954102,
-0.0041601657867431641,
0.095136642456054688,
0.50473618507385254,
1.001220703125,
1.9941892623901367,
2.999570369720459,
4.004951000213623,
4.997920036315918,
6.003300666809082,
6.9962692260742188,
8.0016508102416992,
8.9946193695068359,
]

assert t["float16_5"].array(library="np").tolist() == [
-1.9156250953674316,
-1.5184375047683716,
-1.1212500333786011,
-0.32687497138977051,
0.0703125,
0.0703125,
0.0703125,
0.46749997138977051,
0.86468744277954102,
2.0562500953674316,
2.8506250381469727,
4.0421876907348633,
4.8365626335144043,
6.0281248092651367,
6.8225002288818359,
8.0140628814697266,
8.8084373474121094,
]

assert t["float16_3"].array(library="np").tolist() == [
-2.7100000381469727,
-1.1212500333786011,
-1.1212500333786011,
-1.1212500333786011,
0.46749997138977051,
0.46749997138977051,
0.46749997138977051,
0.46749997138977051,
0.46749997138977051,
2.0562500953674316,
3.6449999809265137,
3.6449999809265137,
5.2337498664855957,
5.2337498664855957,
6.8225002288818359,
8.411250114440918,
8.411250114440918,
]

assert t["array_30"].array(library="np").tolist() == [
[-2.0000000023934987, -2.0000000023934987, -2.0000000023934987],
[-1.5000000057462601, -1.5000000057462601, -1.5000000057462601],
[-0.99999999726191158, -0.99999999726191158, -0.99999999726191158],
[-0.50000000061467276, -0.50000000061467276, -0.50000000061467276],
[-0.10000000329688152, -0.10000000329688152, -0.10000000329688152],
[-3.9674339369355494e-09, -3.9674339369355494e-09, -3.9674339369355494e-09],
[0.10000000719912361, 0.10000000719912361, 0.10000000719912361],
[0.50000000451691484, 0.50000000451691484, 0.50000000451691484],
[1.0000000011641537, 1.0000000011641537, 1.0000000011641537],
[1.9999999944586309, 1.9999999944586309, 1.9999999944586309],
[2.9999999995902185, 2.9999999995902185, 2.9999999995902185],
[4.0000000047218061, 4.0000000047218061, 4.0000000047218061],
[4.9999999980162837, 4.9999999980162837, 4.9999999980162837],
[6.0000000031478704, 6.0000000031478704, 6.0000000031478704],
[6.9999999964423489, 6.9999999964423489, 6.9999999964423489],
[8.0000000015739374, 8.0000000015739374, 8.0000000015739374],
[8.9999999948684142, 8.9999999948684142, 8.9999999948684142],
]

assert t["array_10"].array(library="np").tolist() == [
[-2.0025098323822021, -2.0025098323822021, -2.0025098323822021],
[-1.5060254335403442, -1.5060254335403442, -1.5060254335403442],
[-0.99712896347045898, -0.99712896347045898, -0.99712896347045898],
[-0.50064444541931152, -0.50064444541931152, -0.50064444541931152],
[-0.10345697402954102, -0.10345697402954102, -0.10345697402954102],
[-0.0041601657867431641, -0.0041601657867431641, -0.0041601657867431641],
[0.095136642456054688, 0.095136642456054688, 0.095136642456054688],
[0.50473618507385254, 0.50473618507385254, 0.50473618507385254],
[1.001220703125, 1.001220703125, 1.001220703125],
[1.9941892623901367, 1.9941892623901367, 1.9941892623901367],
[2.999570369720459, 2.999570369720459, 2.999570369720459],
[4.004951000213623, 4.004951000213623, 4.004951000213623],
[4.997920036315918, 4.997920036315918, 4.997920036315918],
[6.003300666809082, 6.003300666809082, 6.003300666809082],
[6.9962692260742188, 6.9962692260742188, 6.9962692260742188],
[8.0016508102416992, 8.0016508102416992, 8.0016508102416992],
[8.9946193695068359, 8.9946193695068359, 8.9946193695068359],
]
6 changes: 3 additions & 3 deletions tests/test_0029-more-string-types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def test_parse_typename():
assert parse_typename("string") == AsString(False)
assert parse_typename("std::string") == AsString(False)
assert parse_typename("std :: string") == AsString(False)
assert parse_typename("char*") == AsString(False)
assert parse_typename("char *") == AsString(False)
assert parse_typename("char*") == AsString(False, length_bytes="4")
assert parse_typename("char *") == AsString(False, length_bytes="4")
assert parse_typename("TString") == AsString(False)
assert parse_typename("vector<TTree>") == AsVector(True, uproot4.classes["TTree"])
assert parse_typename("vector<int>") == AsVector(True, ">i4")
Expand All @@ -38,7 +38,7 @@ def test_parse_typename():
True, AsVector(False, AsString(False))
)
assert parse_typename("vector<vector<char*>>") == AsVector(
True, AsVector(False, AsString(False))
True, AsVector(False, AsString(False, length_bytes="4"))
)
assert parse_typename("set<unsigned short>") == AsSet(True, ">u2")
assert parse_typename("std::set<unsigned short>") == AsSet(True, ">u2")
Expand Down
6 changes: 3 additions & 3 deletions tests/test_0031-test-stl-containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import skhep_testdata

import uproot4
from uproot4.interpretation.numerical import AsDtype
from uproot4.interpretation.jagged import AsJagged
from uproot4.interpretation.objects import AsObjects
from uproot4.stl_containers import AsString
from uproot4.stl_containers import AsVector
Expand All @@ -21,9 +23,7 @@ def test_typename():
with uproot4.open(skhep_testdata.data_path("uproot-stl_containers.root"))[
"tree"
] as tree:
assert tree["vector_int32"].interpretation == AsObjects(
AsVector(True, numpy.dtype(">i4"))
)
assert tree["vector_int32"].interpretation == AsJagged(AsDtype(">i4"), 10)
assert tree["vector_string"].interpretation == AsObjects(
AsVector(True, AsString(False))
)
Expand Down
Loading

0 comments on commit 11e6975

Please sign in to comment.