Skip to content

Commit 98ea338

Browse files
committed
Validate output of R distance function to ensure a single numeric value is returned
1 parent fafff1d commit 98ea338

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

pyabc/external/r/r_rpy2.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,14 @@ def distance(self, function_name: str):
159159

160160
def distance_py(*args):
161161
args = tuple(_dict_to_named_list(d) for d in args)
162-
return float(np.array(distance(*args)))
162+
163+
res = np.asarray(distance(*args)).squeeze()
164+
if res.size != 1:
165+
raise TypeError(
166+
f"R distance function '{function_name}' must return a single "
167+
f"numeric value, but got shape {res.shape} (size={res.size})."
168+
)
169+
return float(res.item())
163170

164171
distance_py.__name__ = function_name
165172
# set reference to this class to ensure the source file is

0 commit comments

Comments
 (0)