m, n, r = volume.shape
vals = ((np.arange(m) - roi[0]) ** 2)[:, (None), (None)] + ((np.arange(n) -
    roi[1]) ** 2)[:, (None)] + (np.arange(r) - roi[2]) ** 2
mask = vals < radius ** 2
Python vectorizing nested for loops