From fa450e4005cf1b8d9cf115341a5767b56ae5bc03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlson=20B=C3=BCth?= Date: Thu, 30 Jul 2026 17:45:04 +0200 Subject: [PATCH] fix(normalisation): retry on ValueError when sampling null ensemble --- delaynet/network_analysis/_normalisation.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/delaynet/network_analysis/_normalisation.py b/delaynet/network_analysis/_normalisation.py index 5a0dc44..16e7aaa 100644 --- a/delaynet/network_analysis/_normalisation.py +++ b/delaynet/network_analysis/_normalisation.py @@ -152,11 +152,25 @@ def wrapper( x_true = metric_fn(weight_matrix, *args, **kwargs) # Sample ensemble and compute metric values + # Retry if the metric raises ValueError on a random graph (e.g., reciprocity + # rejects accidentally symmetric matrices). Cap attempts to avoid infinite loops. samples = [] - for _ in range(n_rand_val): + max_attempts = n_rand_val * 10 + for _ in range(max_attempts): + if len(samples) >= n_rand_val: + break R = _random_directed_gnm_igraph(n, m) - x_r = metric_fn(R, *args, **kwargs) + try: + x_r = metric_fn(R, *args, **kwargs) + except ValueError: + continue samples.append(np.asarray(x_r)) + else: + raise RuntimeError( + f"Could not collect {n_rand_val} valid null-distribution samples " + f"after {max_attempts} attempts. The metric {metric_fn.__name__!r} " + f"raised ValueError on every random graph generated." + ) samples_arr = np.stack(samples, axis=0) # shape: (n_random, ...) mu = samples_arr.mean(axis=0)