def test_rect_limits_1d(graph, rect_limits, testclass): def test(allow_graph=True): limit = testclass(rect_limits) limit2 = testclass(rect_limits=rect_limits) lower, upper = limit.rect_limits lower2, upper2 = limit2.rect_limits assert limit.has_rect_limits inside = limit.inside(2) inside2 = limit.inside(2, guarantee_limits=True) outside = limit.inside(4) if graph: equal = limit.equal(limit2, allow_graph=allow_graph) less_equal = limit.less_equal(limit2, allow_graph=allow_graph) else: equal = limit == limit2 less_equal = limit <= limit2 return lower, upper, lower2, upper2, inside, inside2, outside, equal, less_equal if graph: test = z.function(test) lower, upper, lower2, upper2, inside, inside2, outside, equal, less_equal = test( ) assert equal assert less_equal assert lower, upper == rect_limits assert lower2, upper2 == rect_limits assert inside assert inside2 assert not outside
def test_reorder_x(graph, testclass): x = tf.constant([2, 3, 4, 1], dtype=tf.float64) def test(): x_all = [] obs = ['a', 'b', 'c', 'd'] x_obs = ['b', 'c', 'd', 'a'] func_obs = [ 'd', 'a', 'b', 'c', ] coords = testclass(obs) x_all.append(coords.reorder_x(x, x_obs=x_obs)) x_all.append(coords.reorder_x(x, func_obs=func_obs)) axes = [0, 1, 2, 3] x_axes = [1, 2, 3, 0] func_axes = [3, 0, 1, 2] coords = testclass(axes=axes) x_all.append(coords.reorder_x(x, x_axes=x_axes)) x_all.append(coords.reorder_x(x, func_axes=func_axes)) coords = testclass(obs=obs, axes=axes) x_all.append(coords.reorder_x(x, x_obs=x_obs, x_axes=x_axes)) x_all.append( coords.reorder_x(x, func_obs=func_obs, func_axes=func_axes)) coords = testclass(obs=obs, axes=axes) x_all.append(coords.reorder_x(x, x_obs=x_obs)) x_all.append(coords.reorder_x(x, func_obs=func_obs)) coords = testclass(obs) x_all.append(coords.reorder_x(x, x_obs=x_obs, x_axes=x_axes)) x_all.append( coords.reorder_x(x, func_obs=func_obs, func_axes=func_axes)) with pytest.raises(ValueError): coords.reorder_x(x, x_axes=x_axes) with pytest.raises(ValueError): coords.reorder_x(x, func_axes=func_axes) coords = testclass(axes=axes) with pytest.raises(ValueError): coords.reorder_x(x, x_obs=x_obs) with pytest.raises(ValueError): coords.reorder_x(x, func_obs=func_obs) return x_all if graph: test = z.function(test) all_x = test() true_x = tf.constant([1, 2, 3, 4], dtype=tf.float64) for x in all_x: assert np.allclose(x, true_x)
def test_less_equal(graph, limit_fn, testclass): def test(limit_fn=None, allow_graph=True): limit = testclass(limit_fn=limit_fn, rect_limits=rect_limits) limit2 = testclass(limit_fn=limit_fn, rect_limits=rect_limit_enlarged) assert limit.has_rect_limits ^ bool(limit_fn) assert limit.has_limits assert limit.limits_are_set assert not limit.limits_are_false inside = limit.inside(2) inside2 = limit.inside(2, guarantee_limits=True) outside = limit.inside(4) assert limit2.has_rect_limits ^ bool(limit_fn) assert limit2.has_limits assert limit2.limits_are_set assert not limit2.limits_are_false inside21 = limit2.inside(2) inside22 = limit2.inside(2, guarantee_limits=True) outside2 = limit2.inside(4) if graph: equal = limit.equal(limit2, allow_graph=allow_graph) else: equal = limit == limit2 return inside, inside2, outside, equal, inside21, inside22, outside2 if graph: test = z.function(test) inside, inside2, outside, equal, inside21, inside22, outside2 = test( limit_fn=limit_fn) assert not ( equal ^ bool(limit_fn) ) # if a limit_fn is specified, this has precedency over the rect assert inside assert inside2 assert not outside assert inside21 assert inside22 assert bool(outside2) ^ bool(limit_fn) # if limit_fn, this is outside
def test_limits_1d(graph, testclass): def test(allow_graph=True): limit = testclass(limit_fn=inside_rect_limits, rect_limits=rect_limits) limit2 = testclass(limit_fn=inside_rect_limits, rect_limits=rect_limit_enlarged) assert not limit.has_rect_limits assert limit.has_limits assert limit.limits_are_set assert not limit.limits_are_false inside = limit.inside(2) inside2 = limit.inside(2, guarantee_limits=True) outside = limit.inside(4) assert not limit2.has_rect_limits assert limit2.has_limits assert limit2.limits_are_set assert not limit2.limits_are_false inside21 = limit2.inside(2) inside22 = limit2.inside(2, guarantee_limits=True) outside2 = limit2.inside(4) if graph: equal = limit.equal(limit2, allow_graph=allow_graph) else: equal = limit == limit2 return inside, inside2, outside, equal, inside21, inside22, outside2 if graph: test = z.function(test) inside, inside2, outside, equal, inside21, inside22, outside2 = test() assert equal assert inside assert inside2 assert not outside assert inside21 assert inside22 assert not outside2
def test_numerical_hessian(graph): param1 = zfit.Parameter('param1', 4.) param2 = zfit.Parameter('param2', 5.) param3 = zfit.Parameter('param3', 2.) def func1(): return param1 * param2**2 + param3**param1 def create_derivatives(func1, params): num_hessian_diag = numerical_hessian(func1, params=params, hessian='diag') num_hessian = numerical_hessian(func1, params=params) tf_hessian_diag = autodiff_hessian(func1, params=params, hessian='diag') tf_hessian = autodiff_hessian(func1, params=params) return num_hessian, num_hessian_diag, tf_hessian, tf_hessian_diag params = [param1, param2, param3] if graph: create_derivatives = z.function(create_derivatives) num_hessian, num_hessian_diag, tf_hessian, tf_hessian_diag = create_derivatives( func1, params) np.testing.assert_allclose(num_hessian, tf_hessian, rtol=1e-4, atol=1e-10) tf_hessian_diag_from_hessian = [ tf_hessian[i, i] for i in range(len(params)) ] np.testing.assert_allclose(tf_hessian_diag_from_hessian, tf_hessian_diag, rtol=1e-4, atol=1e-10) np.testing.assert_allclose(num_hessian_diag, tf_hessian_diag, rtol=1e-4, atol=1e-10)
def test_all_kde(kdetype, npoints, jit, request): import zfit full = request.config.getoption("--longtests") full = full or request.config.getoption("--longtests-kde") cfg = create_kde(kdetype=kdetype, npoints=npoints, cfgonly=True, full=full) print(cfg) kdetype = kdetype, npoints if jit: run_jit = z.function(_run) ( expected_integral, integral, name, prob, prob_true, rel_tol, sample, sample2, x, name, data, ) = run_jit(kdetype, full=full) else: ( expected_integral, integral, name, prob, prob_true, rel_tol, sample, sample2, x, name, data, ) = _run(kdetype, full=full) expected_integral = zfit.run(expected_integral) if not jit: import matplotlib.pyplot as plt plt.figure() kernel_used = cfg.get("kernel") if kernel_used is not None: kernel_print = f", kernel={kernel_used.__name__}" else: kernel_print = "" bandwidth_printready = cfg.get("bandwidth") if isinstance(bandwidth_printready, ZfitParameter): bandwidth_printready = f"Param({float(bandwidth_printready.value())}" if bandwidth_printready is None: bandwidth_print = "" else: bandwidth_print = f", h={bandwidth_printready}" num_grid_points = cfg.get("num_grid_points") if num_grid_points is not None: grid_print = f", grid={num_grid_points}" else: grid_print = "" binning_method = cfg.get("binning_method") if binning_method is not None: binning_print = f", binning={binning_method}" else: binning_print = "" weights = cfg.get("weights") if weights is not None: weights_print = ", weighted" else: weights_print = "" npoints_plot = cfg.get("npoints", kdetype[1]) plt.title( f"{name} with {npoints_plot} points{weights_print}{bandwidth_print}{kernel_print}{grid_print}{binning_print}", fontsize=10, ) plt.plot(x, prob, label=f"KDE") plt.plot(x, prob_true, label="true PDF") data_np = zfit.run(data) plt.hist(data_np, bins=40, density=True, alpha=0.3, label="Kernel points") plt.legend() abs_tol = 0.005 if kdetype[1] > 3000 else 0.03 tolfac = 6 if not cfg["type"] == tfp.distributions.Normal else 1 if cfg.get("binning_method") == "simple": tolfac *= 6 assert zfit.run(integral) == pytest.approx(expected_integral, abs=abs_tol * tolfac) assert tuple(sample.shape) == (1, 1) assert tuple(sample2.shape) == (1500, 1) assert prob.shape.rank == 1 assert prob.shape[0] == x.shape[0] assert np.mean(prob - prob_true) < 0.07 * tolfac # make sure that on average, most values are close assert np.mean( (prob / prob_true)[prob_true > np.mean(prob_true)]**2) == pytest.approx( 1, abs=0.1 * tolfac) rtol = 0.05 np.testing.assert_allclose(prob, prob_true, rtol=rtol, atol=0.01 * tolfac)