Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ariostas committed Dec 19, 2024
1 parent 1f8d1c8 commit 6d8ddbd
Showing 1 changed file with 118 additions and 44 deletions.
162 changes: 118 additions & 44 deletions tests/test_1347_rntuple_floats_suppressed_cols.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,143 @@
import uproot


def truncate_float(value, bits):
a = np.float32(value).view(np.uint32)
a &= 0xFFFFFFFF << (32 - bits)
return a.astype(np.uint32).view(np.float32)


def quantize_float(value, bits, min, 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) / (2**bits - 1)
return quantized_float


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 np.isclose(entry.trunc10, true_value, rtol=0.25)
assert np.isclose(entry.trunc16, true_value, rtol=1e-3)
assert np.isclose(entry.trunc24, true_value)
assert np.isclose(entry.trunc31, true_value)
assert np.isclose(entry.quant1, true_value, rtol=2)
assert np.isclose(entry.quant8, true_value, rtol=1e-3)
assert np.isclose(entry.quant16, true_value, rtol=1e-4)
assert np.isclose(entry.quant20, true_value)
assert np.isclose(entry.quant24, true_value)
assert np.isclose(entry.quant25, true_value)
assert np.isclose(entry.quant32, true_value)
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 np.isclose(entry.trunc10, true_value, rtol=0.25)
assert np.isclose(entry.trunc16, true_value, rtol=1e-2)
assert np.isclose(entry.trunc24, true_value)
assert np.isclose(entry.trunc31, true_value)
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, true_value, rtol=2)
assert np.isclose(entry.quant8, true_value, rtol=1e-3)
assert np.isclose(entry.quant16, true_value, rtol=1e-4)
assert np.isclose(entry.quant20, true_value)
assert np.isclose(entry.quant24, true_value)
assert np.isclose(entry.quant25, true_value)
assert np.isclose(entry.quant32, true_value)
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 np.isclose(entry.trunc10, true_value, rtol=0.25)
assert np.isclose(entry.trunc16, true_value, rtol=1e-3)
assert np.isclose(entry.trunc24, true_value)
assert np.isclose(entry.trunc31, true_value)
assert np.isclose(entry.quant1, true_value, atol=2.1)
assert np.isclose(entry.quant8, true_value, rtol=1e-3)
assert np.isclose(entry.quant16, true_value, rtol=1e-4)
assert np.isclose(entry.quant20, true_value)
assert np.isclose(entry.quant24, true_value)
assert np.isclose(entry.quant25, true_value, atol=1e-6)
assert np.isclose(entry.quant32, true_value)
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[3]
true_value = -1.9060668
assert np.isclose(entry.trunc10, true_value, rtol=0.25)
assert np.isclose(entry.trunc16, true_value, rtol=1e-2)
assert np.isclose(entry.trunc24, true_value, rtol=1e-3)
assert np.isclose(entry.trunc31, true_value)
assert np.isclose(entry.quant1, true_value, rtol=2)
assert np.isclose(entry.quant8, true_value, rtol=1e-2)
assert np.isclose(entry.quant16, true_value, rtol=1e-4)
assert np.isclose(entry.quant20, true_value)
assert np.isclose(entry.quant24, true_value)
assert np.isclose(entry.quant25, true_value)
assert np.isclose(entry.quant32, true_value)
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():
Expand Down

0 comments on commit 6d8ddbd

Please sign in to comment.