Repository navigation
Unwrap seed refactor - #8142
Unwrap seed refactor#8142
Conversation
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
* origin/main: (34 commits) Suppress tifffile DeprecationWarning in test_plugin_deprecation_on_imread (scikit-image#8138) Move tests back to doc build Fill out .PHONY targets Apply suggestions from code review Remove unused imports (apply commit hooks) Improve inserted docstring note of `deprecated_func` (scikit-image#8095) address review Update doc/source/index.rst Refactor migration doctests and run as tests Port tests for `features.canny` to skimage2 (scikit-image#8126) Remove unnecessary `grid` directive Re-add reference to migration guide Replace NumPy global RNG use in tests with local seeded RNGs (scikit-image#8084) Disable tests using matplotlib on pyodide (scikit-image#8082) Test with pythonoptimize (scikit-image#8108) Add skimage2 porting decorator (scikit-image#8114) Drop 3.13t builds from cibuildwheel configuration (scikit-image#8127) Restore wording of AI policy (scikit-image#8123) Port feature.canny to skimage2 (scikit-image#8047) address greptile review ...
When input seed was 0, we did not set the seed. However, by default, the unseeded C `rand()` assumes a seed of 1. We might not have reached that state (perhaps `rand()` was always seeded somehow), but in any case, we're always seeding now.
|
A quick and dirty benchmark suggests timing is about the same before and after the Numpy RNG refactor: import numpy as np
from skimage import data, img_as_float, color, exposure
from skimage.restoration import unwrap_phase
# Load an image as a floating-point grayscale
image = color.rgb2gray(img_as_float(data.chelsea()))
# Scale the image to [0, 4*pi]
image = exposure.rescale_intensity(image, out_range=(0, 4 * np.pi))
# Create a phase-wrapped image in the interval [-pi, pi)
image_wrapped = np.angle(np.exp(1j * image))
# Perform phase unwrapping
%timeit unwrap_phase(image_wrapped) |
|
Reviews (1): Last reviewed commit: "Refactor to use Numpy RNG in C code" | Re-trigger Greptile |
From Greptile review.
|
Reviews (2): Last reviewed commit: "Make random seeding check less sensitive..." | Re-trigger Greptile |
|
Hmm - Greptile does (I think) a correct analysis at #8142 (comment) - that the random numbers only survive for corner (always) and border (depending on I see the original code had @geggo - I think this is your code? Do you have any insight into the reasoning behind the random initialization? |
|
Here's a guess - that the In any case, let's go back to returning unsigned 32-bit integers for the random values, pending better understanding. |
|
Reviews (3): Last reviewed commit: "Revert to using uint32 for initializatio..." | Re-trigger Greptile |
|
Sorry, I have no deeper insight into the unwrapping code, I only provided the initial Python warapper. But I think the analysis of Matthew-brett is correct. |
|
Reviews (4): Last reviewed commit: "Rename variables x, y, z to k, j, i for ..." | Re-trigger Greptile |
|
@geggo - thanks much for the reply, that is very helpful - I'll keep exploring, but it does look like my AI-inspired guess is about right. |
|
Reviews (5): Last reviewed commit: "Update unwrap code to use intptr_t" | Re-trigger Greptile |
3b3b80a to
03955f2
Compare
|
Reviews (6): Last reviewed commit: "Update unwrap code to use intptr_t" | Re-trigger Greptile |
|
Further analysis by Gemini asserts that the
The // gamma function in the paper
double wrap(double pixel_value) {
double wrapped_pixel_value;
if (pixel_value > PI)
wrapped_pixel_value = pixel_value - TWOPI;
else if (pixel_value < -PI)
wrapped_pixel_value = pixel_value + TWOPI;
else
wrapped_pixel_value = pixel_value;
return wrapped_pixel_value;
}but this will leave gaps greater than From the paper, on the "gamma function":
I wonder whether // gamma function in the paper
double wrap(double pixel_value) {
double wrapped_pixel_value;
double mod_value = fmod(pixel_value, TWOPI);
if (mod_value > PI)
wrapped_pixel_value = mod_value - TWOPI;
else if (mod_value < -PI)
wrapped_pixel_value = mod_value + TWOPI;
else
wrapped_pixel_value = mod_value;
return wrapped_pixel_value;
} |
03955f2 to
e06b557
Compare
|
As discussed at meeting.
|
I've restored the "sentinel value" from the original code (before inclusion in scikit-image) - that forces all uninitialized pixels to high (un)reliability values before adding the random component. I think this is correct, as otherwise, very rarely, a low number from the 32-bit random number generation will indicate low-unreliability pixels in the borders, etc, which could disturb the unwrapping. |
|
I've put an explanatory note in |
|
Reviews (14): Last reviewed commit: "Add comments on reliability score" | Re-trigger Greptile |
|
Oh, and I refactored the common stuff out of the 2D and 3D |
for clarity.
|
From discussion with @stefanv - renamed |
|
Reviews (15): Last reviewed commit: "Rename "reliability" to "unreliability"" | Re-trigger Greptile |
|
The
|
stefanv
left a comment
There was a problem hiding this comment.
Thanks, Matthew, looks good overall!
As per AI policy, please mention if any AI tools were used (e.g., in the renaming of image_width to n_j etc.)
| #include <numpy/random/bitgen.h> | ||
|
|
||
| #ifndef M_PI | ||
| #define M_PI 3.1415926535897932384626433832795 |
There was a problem hiding this comment.
Does NumPy / Cython or, otherwise, math.h not have a built-in pi?
There was a problem hiding this comment.
I believe it is possible for M_PI to be undeclared even after #include <math.h> - https://langdev.stackexchange.com/questions/2075/what-obstacles-prevented-c-and-c-from-standardizing-π - hence the ifndef M_PI ...
| ) | ||
| ) | ||
|
|
||
| # Check that `None` is also a valid seed value, and that, with a much |
There was a problem hiding this comment.
What is the advantage of testing this?
There was a problem hiding this comment.
Only, I suppose, that we can pass None as a value to the rng argument. Should we not test that?
There was a problem hiding this comment.
That is the default behavior, so I imagine it is covered somewhere. But maybe not.
From Gemini suggestions.
As suggested by a Gemini review.
Done in : eba93c7
Done in 05d3e30 |
…o unwrap-seed-refactor * matthew-brett/unwrap-seed-refactor: Rename "reliability" to "unreliability"
|
Reviews (16): Last reviewed commit: "Merge remote-tracking branch 'matthew-br..." | Re-trigger Greptile |
From Stefanv comment.
From StefanV suggestion.
Co-authored-by: Stefan van der Walt <[email protected]>
Co-authored-by: Stefan van der Walt <[email protected]>
…o unwrap-seed-refactor * matthew-brett/unwrap-seed-refactor: Update src/skimage/restoration/unwrap.py Update src/skimage/restoration/unwrap.py
|
Final call for changes. A couple of vaguely outstanding questions remain. But could live with the current state. |
|
Reviews (17): Last reviewed commit: "Merge remote-tracking branch 'matthew-br..." | Re-trigger Greptile |
|
Thanks very much! |
* origin/main: Use standard C99 static inline instead of `__inline` (#8159) Fix structural_similarity win_size / gaussian_weights conflict (#8162) Unwrap seed refactor (#8142) ci: Remove llvm build and add ccache support on windows (#8164) Document warn_external in contributing guide (#8163) CI: Fix emscripten dependencies and clean up wheel test commands (#8147) Test only changed subpackages (#8103) Fix nightlies: update deprecated scipy call, fix underdetermined test Fix minkowski distanced invocation for SciPy >= 1.18 (#8157) Port `structural_similarly` to ski2 (#8104) make spin build requirement a lower bound (#8158)
* origin/main: (254 commits) Refactor, document migration utilities (scikit-image#8184) ci: improve handling of pr labels (scikit-image#8169) Doctest migration in PRs & on all platforms (scikit-image#8141) Update SKIP 4 (import hierarchy, backporting, testing) (scikit-image#8097) One page per function / class in HTML docs for skimage2 (scikit-image#8154) Pin minimum pytest to 9 (scikit-image#8174) docs: make source install instructions more discoverable and add build requirements to top level requirements file (scikit-image#8165) Drop `--maxfail=5` from local pytest config (scikit-image#8170) Use standard C99 static inline instead of `__inline` (scikit-image#8159) Fix structural_similarity win_size / gaussian_weights conflict (scikit-image#8162) Unwrap seed refactor (scikit-image#8142) ci: Remove llvm build and add ccache support on windows (scikit-image#8164) Document warn_external in contributing guide (scikit-image#8163) CI: Fix emscripten dependencies and clean up wheel test commands (scikit-image#8147) Test only changed subpackages (scikit-image#8103) Fix nightlies: update deprecated scipy call, fix underdetermined test Fix minkowski distanced invocation for SciPy >= 1.18 (scikit-image#8157) Port `structural_similarly` to ski2 (scikit-image#8104) make spin build requirement a lower bound (scikit-image#8158) Drop Python 3.11 Support (scikit-image#8153) ...
This is a continuation of #8099
It includes:
intptr_tfor variables containing array sizes or indices..cfiles into a newunwrap_common.hheader file.I used AI to automate some variable renaming - specifically in commit 0879160, renaming e.g.
image_widthton_jandwrap_around_xtowrap_around_j, mutatis mutandis for 2 and 3D. Then I checked the diff.