From 546a01c108e92c24f19f8cf2d7e0d000fcd7aebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justus=20Sagem=C3=BCller?= Date: Tue, 13 Aug 2024 16:35:49 +0200 Subject: [PATCH] Don't rely on obsolete NumPy inhomogenous arrays for boundary specification. The partition builders are quite liberal (arguably, unnecessarily) in the format of the boolean lists specifying along which dimensions the boundary should be included in the grid and along which not. Previously, these free-form lists were passed through NumPy as a first step of analysing their form, but NumPy itself is now more strict in what can be in an array (unless explicitly asked with `dtype=object`, in which case however a plain list could be used just as well). These checks can also be done in a more direct fashion, presuming that the caller actually follows the specification. Amended due to feedback by Jevgenija in https://github.com/odlgroup/odl/pull/1649#pullrequestreview-2268796987. --- odl/discr/grid.py | 4 ++-- odl/util/normalize.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/odl/discr/grid.py b/odl/discr/grid.py index ef0d9bd2c35..0317629fe93 100644 --- a/odl/discr/grid.py +++ b/odl/discr/grid.py @@ -1111,8 +1111,8 @@ def uniform_grid_fromintv(intv_prod, shape, nodes_on_bdry=True): shape = normalized_scalar_param_list(shape, intv_prod.ndim, safe_int_conv) - if np.shape(nodes_on_bdry) == (): - nodes_on_bdry = ([(bool(nodes_on_bdry), bool(nodes_on_bdry))] * + if isinstance(nodes_on_bdry, bool): + nodes_on_bdry = ([(nodes_on_bdry, nodes_on_bdry)] * intv_prod.ndim) elif intv_prod.ndim == 1 and len(nodes_on_bdry) == 2: nodes_on_bdry = [nodes_on_bdry] diff --git a/odl/util/normalize.py b/odl/util/normalize.py index e28911d32bf..27984f0640a 100644 --- a/odl/util/normalize.py +++ b/odl/util/normalize.py @@ -278,11 +278,11 @@ def normalized_nodes_on_bdry(nodes_on_bdry, length): >>> normalized_nodes_on_bdry([[True, False], False, True], length=3) [(True, False), (False, False), (True, True)] """ - shape = np.shape(nodes_on_bdry) - if shape == (): - out_list = [(bool(nodes_on_bdry), bool(nodes_on_bdry))] * length - elif length == 1 and shape == (2,): - out_list = [(bool(nodes_on_bdry[0]), bool(nodes_on_bdry[1]))] + if isinstance(nodes_on_bdry, bool): + return [(nodes_on_bdry, nodes_on_bdry)] * length + elif (length == 1 and len(nodes_on_bdry) == 2 + and all(isinstance(d, bool) for d in nodes_on_bdry)): + return [nodes_on_bdry[0], nodes_on_bdry[1]] elif len(nodes_on_bdry) == length: out_list = []