From 015402f289618551f17b858cbc18557bd623a780 Mon Sep 17 00:00:00 2001 From: adigitoleo Date: Thu, 29 Feb 2024 13:55:11 +1100 Subject: [PATCH] fix: Fix IndexError resulting from cumsum(fractions)[-1] != 1 This fixes an occasional IndexError that occurs during resampling, if np.cumsum(fractions_ascending) < rng.random(), which results in np.searchsorted() returning an index outside the size of the new arrays. --- src/pydrex/stats.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/pydrex/stats.py b/src/pydrex/stats.py index 32af1f12..6414d5dc 100644 --- a/src/pydrex/stats.py +++ b/src/pydrex/stats.py @@ -52,6 +52,8 @@ def resample_orientations(orientations, fractions, n_samples=None, seed=None): # Cumulative volume fractions. frac_ascending = frac[sort_ascending] cumfrac = frac_ascending.cumsum() + # Force cumfrac[-1] to be equal to sum(frac_ascending) i.e. 1. + cumfrac[-1] = 1.0 # Number of new samples with volume less than each cumulative fraction. count_less = np.searchsorted(cumfrac, rng.random(n_samples)) out_orientations[i, ...] = orient[sort_ascending][count_less]