1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
from __future__ import annotations
import copy
import ctypes
import math
import sys
from pickle import dumps, loads
import numpy as np
import pytest
from pytest import approx
import boost_histogram as bh
ftype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)
def python_convert(x):
return ftype(x)
def pickle_roundtrip_protocol_2(x):
return loads(dumps(x, 2))
def pickle_roundtrip_protocol_highest(x):
return loads(dumps(x, -1))
@pytest.fixture(
params=(
pickle_roundtrip_protocol_2,
pickle_roundtrip_protocol_highest,
copy.copy,
copy.deepcopy,
)
)
def copy_fn(request):
return request.param
@pytest.mark.parametrize(
"opts", [{}, {"growth": True}, {"underflow": True, "overflow": True}]
)
def test_options(copy_fn, opts):
orig = bh.axis.Traits(**opts)
new = copy_fn(orig)
assert new == orig
@pytest.mark.parametrize(
("accum", "args"),
[
(bh.accumulators.Sum, (12,)),
(bh.accumulators.WeightedSum, (1.5, 2.5)),
(bh.accumulators.Mean, (5, 1.5, 2.5)),
(bh.accumulators.WeightedMean, (1.5, 2.5, 3.5, 4.5)),
],
)
def test_accumulators(accum, args, copy_fn):
orig = accum(*args)
new = copy_fn(orig)
assert new == orig
axes_creations = [
(bh.axis.Regular, (4, 2, 4), {"underflow": False, "overflow": False}),
(bh.axis.Regular, (4, 2, 4), {}),
(bh.axis.Regular, (4, 2, 4), {"growth": True}),
(bh.axis.Regular, (4, 2, 4), {"transform": bh.axis.transform.log}),
(bh.axis.Regular, (4, 2, 4), {"transform": bh.axis.transform.sqrt}),
(bh.axis.Regular, (4, 2, 4), {"transform": bh.axis.transform.Pow(0.5)}),
(bh.axis.Regular, (4, 2, 4), {"circular": True}),
(bh.axis.Variable, ([1, 2, 3, 4],), {}),
(bh.axis.Variable, ([1, 2, 3, 4],), {"circular": True}),
(bh.axis.Integer, (1, 4), {}),
(bh.axis.Integer, (1, 4), {"circular": True}),
(bh.axis.IntCategory, ([1, 2, 3],), {}),
(bh.axis.IntCategory, ([1, 2, 3],), {"growth": True}),
(bh.axis.StrCategory, (["1", "2", "3"],), {}),
(bh.axis.StrCategory, (["1", "2", "3"],), {"growth": True}),
]
@pytest.mark.parametrize(("axis", "args", "opts"), axes_creations)
def test_axes(axis, args, opts, copy_fn):
orig = axis(*args, **opts)
new = copy_fn(orig)
assert new == orig
assert new.centers == approx(orig.centers)
@pytest.mark.parametrize(("axis", "args", "opts"), axes_creations)
def test_metadata_str(axis, args, opts, copy_fn):
orig = axis(*args, **opts)
orig.metadata = "foo"
new = copy_fn(orig)
assert new.metadata == orig.metadata
new.metadata = orig.metadata
assert new == orig
assert new.centers == approx(orig.centers)
# Special test: Deepcopy should change metadata id, copy should not
def test_compare_copy_axis(metadata):
orig = bh.axis.Regular(4, 0, 2, metadata=metadata)
new = copy.copy(orig)
dnew = copy.deepcopy(orig)
assert orig.metadata is new.metadata
assert orig.metadata == dnew.metadata
if metadata is not copy.copy(metadata):
assert orig.metadata is not dnew.metadata
# Special test: Deepcopy should change metadata id, copy should not
def test_compare_copy_hist(metadata):
orig = bh.Histogram(bh.axis.Regular(4, 0, 2, metadata=metadata))
new = copy.copy(orig)
dnew = copy.deepcopy(orig)
assert orig.axes[0].metadata is new.axes[0].metadata
assert orig.axes[0].metadata == dnew.axes[0].metadata
if metadata is not copy.copy(metadata):
assert orig.axes[0].metadata is not dnew.axes[0].metadata
@pytest.mark.parametrize(("axis", "args", "opts"), axes_creations)
def test_metadata_any(axis, args, opts, copy_fn):
orig = axis(*args, **opts)
orig.metadata = (1, 2, 3)
new = copy_fn(orig)
assert new.metadata == orig.metadata
new.metadata = orig.metadata
assert new == orig
@pytest.mark.benchmark(group="histogram-pickling")
@pytest.mark.parametrize(
("storage", "extra"),
[
(bh.storage.AtomicInt64, {}),
(bh.storage.Int64, {}),
(bh.storage.Unlimited, {}),
(bh.storage.Unlimited, {"weight"}),
(bh.storage.Double, {"weight"}),
(bh.storage.Weight, {"weight"}),
(bh.storage.Mean, {"sample"}),
(bh.storage.WeightedMean, {"weight", "sample"}),
(bh.storage.MultiCell, {"MultiCell"}),
],
)
def test_storage(benchmark, copy_fn, storage, extra):
n = 1000
init_list = []
if extra == {"MultiCell"}:
init_list.append(3)
hist = bh.Histogram(bh.axis.Integer(0, n), storage=storage(*init_list))
x = np.arange(2 * (n + 2)) % (n + 2) - 1
if extra == {}:
hist.fill(x)
elif extra == {"weight"}:
hist.fill(x, weight=np.arange(2 * n + 4) + 1)
elif extra == {"sample"}:
hist.fill(x, sample=np.arange(2 * n + 4) + 1)
elif extra == {"MultiCell"}:
hist.fill(x, weight=np.reshape(np.arange(3 * (2 * n + 4)) + 1, (2 * n + 4, 3)))
else:
hist.fill(x, weight=np.arange(2 * n + 4) + 1, sample=np.arange(2 * n + 4) + 1)
new = benchmark(copy_fn, hist)
assert np.asarray(hist.view(True)) == approx(np.asarray(new.view(True)))
assert new == hist
def test_histogram_regular(copy_fn):
hist = bh.Histogram(bh.axis.Regular(4, 1, 2), bh.axis.Regular(8, 3, 6))
new = copy_fn(hist)
assert hist == new
def test_histogram_fancy(copy_fn):
hist = bh.Histogram(bh.axis.Regular(4, 1, 2), bh.axis.Integer(0, 6))
new = copy_fn(hist)
assert hist == new
def test_histogram_metadata(copy_fn, metadata):
hist = bh.Histogram(bh.axis.Regular(4, 1, 2, metadata=metadata))
new = copy_fn(hist)
assert hist == new
@pytest.mark.skipif(
sys.implementation.name == "pypy",
reason="Not remotely supported on PyPY, hangs forever",
)
@pytest.mark.skipif(
sys.implementation.name == "graalpy",
reason="Not supported, throws exception",
)
@pytest.mark.parametrize("mod", [np, math])
def test_pickle_transforms(mod, copy_fn):
ax1 = bh.axis.Regular(
100,
1,
100,
transform=bh.axis.transform.Function(mod.log, mod.exp, convert=python_convert),
)
ax2 = copy_fn(ax1)
ax3 = bh.axis.Regular(100, 1, 100, transform=bh.axis.transform.log)
assert ax1 == ax2
assert ax1.centers == approx(ax2.centers)
assert ax2.centers == approx(ax3.centers)
def test_hist_axes_reference(copy_fn):
h = bh.Histogram(bh.axis.Regular(10, 0, 1, metadata=1))
h.axes[0].metadata = 2
h2 = copy_fn(h)
assert h2._hist is not h._hist
assert h2.axes[0] is not h.axes[0]
assert h2.axes[0].metadata == 2
h.axes[0].metadata = 3
assert h2._axis(0).metadata == 2
assert h2.axes[0].metadata == 2
def test_hist_axes_reference_arbitrary(copy_fn):
h = bh.Histogram(bh.axis.Regular(10, 0, 1))
h.other = 2
h2 = copy_fn(h)
assert h2._hist is not h._hist
assert h2.other == 2
h.other = 3
assert h2.other == 2
def test_hist_reference_arbitrary(copy_fn):
h = bh.Histogram(bh.axis.Regular(10, 0, 1))
h.axes[0].other = 2
h2 = copy_fn(h)
assert h2._hist is not h._hist
assert h2.axes[0] is not h.axes[0]
assert h2.axes[0].other == 2
h.axes[0].other = 3
assert h2._axis(0).other == 2
assert h2.axes[0].other == 2
def test_axis_wrapped(copy_fn):
ax = bh.axis.Regular(10, 0, 2)
ax2 = copy_fn(ax)
assert ax._ax is not ax2._ax
def test_trans_wrapped(copy_fn):
tr = bh.axis.transform.Pow(2)
tr2 = copy_fn(tr)
assert tr._this is not tr2._this
# Testing #342
def test_cloudpickle():
cloudpickle = pytest.importorskip("cloudpickle")
h = bh.Histogram(bh.axis.Regular(50, 0, 20))
h.fill([1, 2, 3, 4, 5])
h2 = loads(cloudpickle.dumps(h))
assert h == h2
assert h is not h2
|