From 6567394fbf1209eb1a96a2f62c53c87e7374248c Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Tue, 4 Mar 2025 23:00:52 -0800 Subject: [PATCH] [utils.curves.bezier] Fix and clarify bezier() code - Accuracy fix (incorrect +1 term) - Explicitly index last dim of `nodes` - Suppress warnings about div by zero - simplify `umul` and `udiv` calculation --- masque/utils/curves.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/masque/utils/curves.py b/masque/utils/curves.py index 41f82ad..027d205 100644 --- a/masque/utils/curves.py +++ b/masque/utils/curves.py @@ -27,19 +27,17 @@ def bezier( nn = nodes.shape[0] weights = numpy.ones(nn) if weights is None else numpy.asarray(weights) - t_half0 = tt <= 0.5 - umul = tt / (1 - tt) - udiv = 1 / umul - umul[~t_half0] = 1 - udiv[t_half0] = 1 + with numpy.errstate(divide='ignore'): + umul = (tt / (1 - tt)).clip(max=1) + udiv = ((1 - tt) / tt).clip(max=1) hh = numpy.ones((tt.size,)) - qq = nodes[None, 0] * hh[:, None] + qq = nodes[None, 0, :] * hh[:, None] for kk in range(1, nn): - hh *= umul * (nn + 1 - kk) * weights[kk] + hh *= umul * (nn - kk) * weights[kk] hh /= kk * udiv * weights[kk - 1] + hh qq *= 1.0 - hh[:, None] - qq += hh[:, None] * nodes[None, kk] + qq += hh[:, None] * nodes[None, kk, :] return qq