From 99d984bf0ab540da82dc9e0ee795913b64da750a Mon Sep 17 00:00:00 2001 From: Moka-4444 <159596295+Moka-4444@users.noreply.github.com> Date: Mon, 23 Mar 2026 16:43:00 +0200 Subject: [PATCH] Fix #83: nearest_neighbours crashes when max_num_neighbours=1 Resolves an issue where calling connect_nodes_across_graphs with max_num_neighbours=1 caused a crash because KDTree.query returns a scalar instead of an iterable. Wrapped the output in np.atleast_1d() to ensure consistent array shape. --- src/weather_model_graphs/create/base.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/weather_model_graphs/create/base.py b/src/weather_model_graphs/create/base.py index f922b2d..a99510d 100644 --- a/src/weather_model_graphs/create/base.py +++ b/src/weather_model_graphs/create/base.py @@ -1,4 +1,4 @@ -""" +f""" Generic routines for creating the graph components used in the message-passing graph, and for connecting nodes across these component graphs. @@ -333,6 +333,8 @@ def _find_neighbour_node_idxs_in_source_mesh(xy_target): if max_num_neighbours is None: raise Exception( "to use `nearest_neighbours` you should set the max number with `max_num_neighbours`" + neigh_idxs = np.atleast_1d(neigh_idxs) + ) if max_dist is not None or rel_max_dist is not None: raise Exception( @@ -341,6 +343,7 @@ def _find_neighbour_node_idxs_in_source_mesh(xy_target): def _find_neighbour_node_idxs_in_source_mesh(xy_target): neigh_idxs = kdt_s.query(xy_target, max_num_neighbours)[1] + neigh_idxs = np.atleast_1d(neigh_idxs) return neigh_idxs elif method == "within_radius":