Repository navigation
Fix/random seed unwrap - #8099
michaelbratsch wants to merge 4 commits into
Conversation
|
Last reviewed commit: "Add unit test" |
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
|
Last reviewed commit: "Cover full range of ..." |
| else: | ||
| seed = int(rng) |
There was a problem hiding this comment.
No range validation for integer seeds
seed = int(rng) succeeds for any Python int, but the C layer declares cseed as unsigned int. If rng is negative or greater than np.iinfo(np.uint32).max (4 294 967 295), Cython will raise an unhelpful OverflowError: value too large to convert to unsigned int (or a negative-value equivalent) deep inside the Cython call rather than at the Python layer with a meaningful message.
The docstring now says "If an int, it is used directly as the seed", which implies any Python int is valid — but the valid range is silently constrained to [0, 2^32-1]. Consider adding explicit validation here:
else:
seed = int(rng)
if not 0 <= seed <= np.iinfo(np.uint32).max:
raise ValueError(
f"Integer seed must be in [0, {np.iinfo(np.uint32).max}], got {seed}"
)There was a problem hiding this comment.
I think this Greptile change is reasonable - do you agree?
There was a problem hiding this comment.
Actually, I'll add a version of Greptile's suggestion - I think it is reasonable. It would also be good to have a test for this behavior.
See suggestion above for changes.
matthew-brett
left a comment
There was a problem hiding this comment.
Suggestion and suggestion on suggestion.
| Seed for the random number generator used during unwrapping. | ||
| If an int, it is used directly as the seed. If a | ||
| `numpy.random.Generator`, a seed is drawn from it. If `None`, | ||
| unwrapping relies on a random initialization and results may vary | ||
| between runs. |
There was a problem hiding this comment.
| Seed for the random number generator used during unwrapping. | |
| If an int, it is used directly as the seed. If a | |
| `numpy.random.Generator`, a seed is drawn from it. If `None`, | |
| unwrapping relies on a random initialization and results may vary | |
| between runs. | |
| Unwrapping relies on a random initialization. `rng` supplies the | |
| source of a seed for the C-level random number generator used during | |
| unwrapping. If an int, it is used directly as the seed; in that case, | |
| it should be in the range of a C unsigned integer. If a | |
| `numpy.random.Generator`, we draw a suitable integer from the generator | |
| to use as the seed. If ``None``, create a new (unseeded) | |
| `numpy.random.Generator` from which to draw the seed. |
| else: | ||
| seed = int(rng) |
There was a problem hiding this comment.
I think this Greptile change is reasonable - do you agree?
matthew-brett
left a comment
There was a problem hiding this comment.
A couple more comments on reflection.
| else: | ||
| seed = int(rng) |
There was a problem hiding this comment.
Actually, I'll add a version of Greptile's suggestion - I think it is reasonable. It would also be good to have a test for this behavior.
See suggestion above for changes.
|
And - on reflection - at the moment, with and draw a random seed from there. |
|
@michaelbratsch - sorry to press you - but do you have any thoughts here? If you're busy, I'm happy to take this one over (and give you due credit). |
matthew-brett
left a comment
There was a problem hiding this comment.
Rethinking approach a little. Always pass a seed, but perhaps from a new random generator.
|
|
||
| # convert from python types to C types so we can release the GIL | ||
| use_seed = seed is None | ||
| use_seed = seed is not None |
There was a problem hiding this comment.
I think we can drop this check with the changes below (we always specify a seed, but the seed might be random).
There was a problem hiding this comment.
And therefore remove use_seed in calls to unwrap2D and unwrap3D functions.
|
|
||
| # convert from python types to C types so we can release the GIL | ||
| use_seed = seed is None | ||
| use_seed = seed is not None |
| if isinstance(rng, np.random.Generator): | ||
| seed = int(rng.integers(np.iinfo(np.uint32).max, endpoint=True)) | ||
| elif rng is None: | ||
| seed = None | ||
| else: | ||
| seed = int(rng) |
There was a problem hiding this comment.
| if isinstance(rng, np.random.Generator): | |
| seed = int(rng.integers(np.iinfo(np.uint32).max, endpoint=True)) | |
| elif rng is None: | |
| seed = None | |
| else: | |
| seed = int(rng) | |
| if rng is None: # Use new generator to make seed. | |
| rng = np.random.default_rng() | |
| # Algorithms use C `srand(seed)` to initialize random stream from | |
| # subsequent calls to `rand()`. `srand` expects an unsigned C int. | |
| uintc_max = np.iinfo(np.uintc).max | |
| if isinstance(rng, np.random.Generator): | |
| seed = rng.integers(uintc_max, endpoint=True) | |
| else: | |
| try: | |
| seed = np.uintc(rng) | |
| except OverflowError: | |
| raise ValueError( | |
| f"Integer seed must be in [0, {uintc_max}], got {rng}" | |
| ) |
| ) | ||
| # For `None` the behavior is not deterministic, so it does not make sense to check | ||
| # for equality. At least call it to see that `None` is an accepted value. | ||
| unwrap(None) |
There was a problem hiding this comment.
Check that result differs from call to call with None?
|
Sorry for my impatience, but I also wanted to refactor for better thread safety. Take-over PR at #8142 . |
This is a continuation of #8099 It includes: - @michaelbratsch's fixes to random seeding - Port to using NumPy RNG for random number generation, for better thread-safety. - Use intptr_t for variables containing array sizes or indices. - Some variable renaming for clarity, and removal of a TODO cleared up by the variable renaming. - Refactor of common code in 2D and 3D .c files into a new unwrap_common.h header file. - Added back sentinel large initial value for pixels / voxels for which we cannot calculate unreliability scores. This is from the original code before inclusion in scikit-image. --- Co-authored-by: Michael Bratsch <[email protected]> Co-authored-by: Stefan van der Walt <[email protected]>
I honestly lost track of this merge request because there has been no feedback for quite a long time. Nice to see that it eventually has been used and got in some form into the main branch. |
Sorry for that! 🙈 |
Description
unwrapnp.random.GeneratorinunwrapRelates to: #7964
Release note
For maintainers and optionally contributors, please refer to the instructions on how to document this PR for the release notes.