-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support reading of custom-length RNTuple floats and suppressed …
…columns (#1347) * Started implementing reading of quantized and truncated floats * Added support for suppressed columns * Added tests * Cleaner reading of floats with 1, 2, or 3 bytes * Only support little-endian systems * Fixed bug with Numpy 1 * Improved tests * Fixed tests for Numpy 2
- Loading branch information
Showing
3 changed files
with
225 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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,167 @@ | ||
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE | ||
|
||
import skhep_testdata | ||
import numpy as np | ||
|
||
import uproot | ||
|
||
|
||
def truncate_float(value, bits): | ||
a = np.float32(value).view(np.uint32) | ||
a &= np.uint32(0xFFFFFFFF) << (32 - bits) | ||
return a.astype(np.uint32).view(np.float32) | ||
|
||
|
||
def quantize_float(value, bits, min, max): | ||
min = np.float32(min) | ||
max = np.float32(max) | ||
if value < min or value > max: | ||
raise ValueError(f"Value {value} is out of range [{min}, {max}]") | ||
scaled_value = (value - min) * (2**bits - 1) / (max - min) | ||
int_value = np.round(scaled_value) | ||
quantized_float = min + int_value * (max - min) / ((1 << bits) - 1) | ||
return quantized_float.astype(np.float32) | ||
|
||
|
||
def test_custom_floats(): | ||
filename = skhep_testdata.data_path("test_float_types_rntuple_v1-0-0-0.root") | ||
with uproot.open(filename) as f: | ||
obj = f["ntuple"] | ||
|
||
arrays = obj.arrays() | ||
|
||
min_value = -2.0 | ||
max_value = 3.0 | ||
|
||
entry = arrays[0] | ||
true_value = 1.23456789 | ||
assert entry.trunc10 == truncate_float(true_value, 10) | ||
assert entry.trunc16 == truncate_float(true_value, 16) | ||
assert entry.trunc24 == truncate_float(true_value, 24) | ||
assert entry.trunc31 == truncate_float(true_value, 31) | ||
assert np.isclose( | ||
entry.quant1, quantize_float(true_value, 1, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant8, quantize_float(true_value, 8, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant16, quantize_float(true_value, 16, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant20, quantize_float(true_value, 20, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant24, quantize_float(true_value, 24, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant25, quantize_float(true_value, 25, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant32, quantize_float(true_value, 32, min_value, max_value) | ||
) | ||
|
||
entry = arrays[1] | ||
true_value = 1.4660155e13 | ||
assert entry.trunc10 == truncate_float(true_value, 10) | ||
assert entry.trunc16 == truncate_float(true_value, 16) | ||
assert entry.trunc24 == truncate_float(true_value, 24) | ||
assert entry.trunc31 == truncate_float(true_value, 31) | ||
true_value = 1.6666666 | ||
assert np.isclose( | ||
entry.quant1, quantize_float(true_value, 1, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant8, quantize_float(true_value, 8, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant16, quantize_float(true_value, 16, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant20, quantize_float(true_value, 20, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant24, quantize_float(true_value, 24, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant25, quantize_float(true_value, 25, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant32, quantize_float(true_value, 32, min_value, max_value) | ||
) | ||
|
||
entry = arrays[2] | ||
true_value = -6.2875986e-22 | ||
assert entry.trunc10 == truncate_float(true_value, 10) | ||
assert entry.trunc16 == truncate_float(true_value, 16) | ||
assert entry.trunc24 == truncate_float(true_value, 24) | ||
assert entry.trunc31 == truncate_float(true_value, 31) | ||
assert np.isclose( | ||
entry.quant1, quantize_float(true_value, 1, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant8, quantize_float(true_value, 8, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant16, quantize_float(true_value, 16, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant20, quantize_float(true_value, 20, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant24, quantize_float(true_value, 24, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant25, | ||
quantize_float(true_value, 25, min_value, max_value), | ||
atol=2e-07, | ||
) | ||
assert np.isclose( | ||
entry.quant32, quantize_float(true_value, 32, min_value, max_value) | ||
) | ||
|
||
entry = arrays[3] | ||
true_value = -1.9060668 | ||
assert entry.trunc10 == truncate_float(true_value, 10) | ||
assert entry.trunc16 == truncate_float(true_value, 16) | ||
assert entry.trunc24 == truncate_float(true_value, 24) | ||
assert entry.trunc31 == truncate_float(true_value, 31) | ||
assert np.isclose( | ||
entry.quant1, quantize_float(true_value, 1, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant8, quantize_float(true_value, 8, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant16, quantize_float(true_value, 16, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant20, quantize_float(true_value, 20, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant24, quantize_float(true_value, 24, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant25, quantize_float(true_value, 25, min_value, max_value) | ||
) | ||
assert np.isclose( | ||
entry.quant32, quantize_float(true_value, 32, min_value, max_value) | ||
) | ||
|
||
|
||
def test_multiple_representations(): | ||
filename = skhep_testdata.data_path( | ||
"test_multiple_representations_rntuple_v1-0-0-0.root" | ||
) | ||
with uproot.open(filename) as f: | ||
obj = f["ntuple"] | ||
|
||
assert len(obj.page_list_envelopes.pagelinklist) == 3 | ||
# The zeroth representation is active in clusters 0 and 2, but not in cluster 1 | ||
assert not obj.page_list_envelopes.pagelinklist[0][0].suppressed | ||
assert obj.page_list_envelopes.pagelinklist[1][0].suppressed | ||
assert not obj.page_list_envelopes.pagelinklist[2][0].suppressed | ||
|
||
arrays = obj.arrays() | ||
|
||
assert np.allclose(arrays.real, [1, 2, 3]) |