|
| 1 | +# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE |
| 2 | + |
| 3 | +import skhep_testdata |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +import uproot |
| 7 | + |
| 8 | + |
| 9 | +def truncate_float(value, bits): |
| 10 | + a = np.float32(value).view(np.uint32) |
| 11 | + a &= np.uint32(0xFFFFFFFF) << (32 - bits) |
| 12 | + return a.astype(np.uint32).view(np.float32) |
| 13 | + |
| 14 | + |
| 15 | +def quantize_float(value, bits, min, max): |
| 16 | + min = np.float32(min) |
| 17 | + max = np.float32(max) |
| 18 | + if value < min or value > max: |
| 19 | + raise ValueError(f"Value {value} is out of range [{min}, {max}]") |
| 20 | + scaled_value = (value - min) * (2**bits - 1) / (max - min) |
| 21 | + int_value = np.round(scaled_value) |
| 22 | + quantized_float = min + int_value * (max - min) / ((1 << bits) - 1) |
| 23 | + return quantized_float.astype(np.float32) |
| 24 | + |
| 25 | + |
| 26 | +def test_custom_floats(): |
| 27 | + filename = skhep_testdata.data_path("test_float_types_rntuple_v1-0-0-0.root") |
| 28 | + with uproot.open(filename) as f: |
| 29 | + obj = f["ntuple"] |
| 30 | + |
| 31 | + arrays = obj.arrays() |
| 32 | + |
| 33 | + min_value = -2.0 |
| 34 | + max_value = 3.0 |
| 35 | + |
| 36 | + entry = arrays[0] |
| 37 | + true_value = 1.23456789 |
| 38 | + assert entry.trunc10 == truncate_float(true_value, 10) |
| 39 | + assert entry.trunc16 == truncate_float(true_value, 16) |
| 40 | + assert entry.trunc24 == truncate_float(true_value, 24) |
| 41 | + assert entry.trunc31 == truncate_float(true_value, 31) |
| 42 | + assert np.isclose( |
| 43 | + entry.quant1, quantize_float(true_value, 1, min_value, max_value) |
| 44 | + ) |
| 45 | + assert np.isclose( |
| 46 | + entry.quant8, quantize_float(true_value, 8, min_value, max_value) |
| 47 | + ) |
| 48 | + assert np.isclose( |
| 49 | + entry.quant16, quantize_float(true_value, 16, min_value, max_value) |
| 50 | + ) |
| 51 | + assert np.isclose( |
| 52 | + entry.quant20, quantize_float(true_value, 20, min_value, max_value) |
| 53 | + ) |
| 54 | + assert np.isclose( |
| 55 | + entry.quant24, quantize_float(true_value, 24, min_value, max_value) |
| 56 | + ) |
| 57 | + assert np.isclose( |
| 58 | + entry.quant25, quantize_float(true_value, 25, min_value, max_value) |
| 59 | + ) |
| 60 | + assert np.isclose( |
| 61 | + entry.quant32, quantize_float(true_value, 32, min_value, max_value) |
| 62 | + ) |
| 63 | + |
| 64 | + entry = arrays[1] |
| 65 | + true_value = 1.4660155e13 |
| 66 | + assert entry.trunc10 == truncate_float(true_value, 10) |
| 67 | + assert entry.trunc16 == truncate_float(true_value, 16) |
| 68 | + assert entry.trunc24 == truncate_float(true_value, 24) |
| 69 | + assert entry.trunc31 == truncate_float(true_value, 31) |
| 70 | + true_value = 1.6666666 |
| 71 | + assert np.isclose( |
| 72 | + entry.quant1, quantize_float(true_value, 1, min_value, max_value) |
| 73 | + ) |
| 74 | + assert np.isclose( |
| 75 | + entry.quant8, quantize_float(true_value, 8, min_value, max_value) |
| 76 | + ) |
| 77 | + assert np.isclose( |
| 78 | + entry.quant16, quantize_float(true_value, 16, min_value, max_value) |
| 79 | + ) |
| 80 | + assert np.isclose( |
| 81 | + entry.quant20, quantize_float(true_value, 20, min_value, max_value) |
| 82 | + ) |
| 83 | + assert np.isclose( |
| 84 | + entry.quant24, quantize_float(true_value, 24, min_value, max_value) |
| 85 | + ) |
| 86 | + assert np.isclose( |
| 87 | + entry.quant25, quantize_float(true_value, 25, min_value, max_value) |
| 88 | + ) |
| 89 | + assert np.isclose( |
| 90 | + entry.quant32, quantize_float(true_value, 32, min_value, max_value) |
| 91 | + ) |
| 92 | + |
| 93 | + entry = arrays[2] |
| 94 | + true_value = -6.2875986e-22 |
| 95 | + assert entry.trunc10 == truncate_float(true_value, 10) |
| 96 | + assert entry.trunc16 == truncate_float(true_value, 16) |
| 97 | + assert entry.trunc24 == truncate_float(true_value, 24) |
| 98 | + assert entry.trunc31 == truncate_float(true_value, 31) |
| 99 | + assert np.isclose( |
| 100 | + entry.quant1, quantize_float(true_value, 1, min_value, max_value) |
| 101 | + ) |
| 102 | + assert np.isclose( |
| 103 | + entry.quant8, quantize_float(true_value, 8, min_value, max_value) |
| 104 | + ) |
| 105 | + assert np.isclose( |
| 106 | + entry.quant16, quantize_float(true_value, 16, min_value, max_value) |
| 107 | + ) |
| 108 | + assert np.isclose( |
| 109 | + entry.quant20, quantize_float(true_value, 20, min_value, max_value) |
| 110 | + ) |
| 111 | + assert np.isclose( |
| 112 | + entry.quant24, quantize_float(true_value, 24, min_value, max_value) |
| 113 | + ) |
| 114 | + assert np.isclose( |
| 115 | + entry.quant25, |
| 116 | + quantize_float(true_value, 25, min_value, max_value), |
| 117 | + atol=2e-07, |
| 118 | + ) |
| 119 | + assert np.isclose( |
| 120 | + entry.quant32, quantize_float(true_value, 32, min_value, max_value) |
| 121 | + ) |
| 122 | + |
| 123 | + entry = arrays[3] |
| 124 | + true_value = -1.9060668 |
| 125 | + assert entry.trunc10 == truncate_float(true_value, 10) |
| 126 | + assert entry.trunc16 == truncate_float(true_value, 16) |
| 127 | + assert entry.trunc24 == truncate_float(true_value, 24) |
| 128 | + assert entry.trunc31 == truncate_float(true_value, 31) |
| 129 | + assert np.isclose( |
| 130 | + entry.quant1, quantize_float(true_value, 1, min_value, max_value) |
| 131 | + ) |
| 132 | + assert np.isclose( |
| 133 | + entry.quant8, quantize_float(true_value, 8, min_value, max_value) |
| 134 | + ) |
| 135 | + assert np.isclose( |
| 136 | + entry.quant16, quantize_float(true_value, 16, min_value, max_value) |
| 137 | + ) |
| 138 | + assert np.isclose( |
| 139 | + entry.quant20, quantize_float(true_value, 20, min_value, max_value) |
| 140 | + ) |
| 141 | + assert np.isclose( |
| 142 | + entry.quant24, quantize_float(true_value, 24, min_value, max_value) |
| 143 | + ) |
| 144 | + assert np.isclose( |
| 145 | + entry.quant25, quantize_float(true_value, 25, min_value, max_value) |
| 146 | + ) |
| 147 | + assert np.isclose( |
| 148 | + entry.quant32, quantize_float(true_value, 32, min_value, max_value) |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +def test_multiple_representations(): |
| 153 | + filename = skhep_testdata.data_path( |
| 154 | + "test_multiple_representations_rntuple_v1-0-0-0.root" |
| 155 | + ) |
| 156 | + with uproot.open(filename) as f: |
| 157 | + obj = f["ntuple"] |
| 158 | + |
| 159 | + assert len(obj.page_list_envelopes.pagelinklist) == 3 |
| 160 | + # The zeroth representation is active in clusters 0 and 2, but not in cluster 1 |
| 161 | + assert not obj.page_list_envelopes.pagelinklist[0][0].suppressed |
| 162 | + assert obj.page_list_envelopes.pagelinklist[1][0].suppressed |
| 163 | + assert not obj.page_list_envelopes.pagelinklist[2][0].suppressed |
| 164 | + |
| 165 | + arrays = obj.arrays() |
| 166 | + |
| 167 | + assert np.allclose(arrays.real, [1, 2, 3]) |
0 commit comments