Repository navigation
Demo for porting a whole module into _skimage2 - #8172
matthew-brett wants to merge 14 commits into
Conversation
Here I: * `git mv src/skimage/transform src/_skimage2/` * `cp -r tests/skimage/transform tests/skimage2` * Ran `python tools/port_module.py src/_skimage2/transform` * Did various edits partly noted in `tools/port_module.py` including fixing tests/_skimage2 imports to import from _skimage2. * Fixed up the tests.
|
Reviews (1): Last reviewed commit: "Demo for porting a whole module into _sk..." | Re-trigger Greptile |
| 2 | ||
|
|
||
| """ | ||
| from skimage.feature.peak import _prominent_peaks |
There was a problem hiding this comment.
Runtime import of a private
skimage symbol inside _skimage2
_prominent_peaks is imported from skimage.feature.peak (repeated at line 370 as well). This is the same backwards-dependency issue as in _warps.py: _skimage2 code importing from skimage. Because it is a runtime lazy import inside the function body, the failure only surfaces when hough_line_peaks or hough_circle_peaks is actually called. The private helper should either be copied into _skimage2._shared or a proper _skimage2.feature.peak module dependency should be used.
| img.shape[0] + 2 * offset, | ||
| img.shape[1] + 2 * offset), dtype=np.float64) | ||
|
|
||
| from skimage.draw import circle_perimeter |
There was a problem hiding this comment.
Cython extension imports
circle_perimeter from skimage.draw
circle_perimeter is imported at runtime from skimage.draw inside the Cython _hough_circle function. This is the same backwards-dependency pattern: a _skimage2 Cython extension leaning on the old skimage namespace. When skimage.draw is eventually ported and becomes a thin wrapper itself, this import may silently re-enter _skimage2 logic or fail depending on import order.
| Examples: :class:`~skimage.transform.AffineTransform`, | ||
| :class:`~skimage.transform.ProjectiveTransform`, | ||
| :class:`~skimage.transform.EuclideanTransform`. | ||
|
|
||
| - Image resizing and rescaling: | ||
| These transforms change the size or resolution of an image. | ||
| They are useful for tasks such as down-sampling an image to | ||
| reduce its size or up-sampling an image to increase its resolution. | ||
| Examples: :func:`~skimage.transform.resize`, | ||
| :func:`~skimage.transform.rescale`. | ||
|
|
||
| - Feature detection and extraction: | ||
| These transforms identify and extract specific features or | ||
| patterns in an image. They are useful for tasks such as object | ||
| detection, image segmentation, and feature matching. | ||
| Examples: :func:`~skimage.transform.hough_circle`, | ||
| :func:`~skimage.transform.pyramid_expand`, | ||
| :func:`~skimage.transform.radon`. | ||
|
|
||
| - Image transformation: | ||
| These transforms change the appearance of an image without changing its | ||
| content. They are useful for tasks such a creating image mosaics, | ||
| applying artistic effects, and visualizing image data. | ||
| Examples: :func:`~skimage.transform.warp`, | ||
| :func:`~skimage.transform.iradon`. |
There was a problem hiding this comment.
The module docstring cross-references all point at
~skimage.transform.* instead of the _skimage2 / skimage2 namespace. Sphinx will resolve these to the old API docs rather than the new ones.
| Examples: :class:`~skimage.transform.AffineTransform`, | |
| :class:`~skimage.transform.ProjectiveTransform`, | |
| :class:`~skimage.transform.EuclideanTransform`. | |
| - Image resizing and rescaling: | |
| These transforms change the size or resolution of an image. | |
| They are useful for tasks such as down-sampling an image to | |
| reduce its size or up-sampling an image to increase its resolution. | |
| Examples: :func:`~skimage.transform.resize`, | |
| :func:`~skimage.transform.rescale`. | |
| - Feature detection and extraction: | |
| These transforms identify and extract specific features or | |
| patterns in an image. They are useful for tasks such as object | |
| detection, image segmentation, and feature matching. | |
| Examples: :func:`~skimage.transform.hough_circle`, | |
| :func:`~skimage.transform.pyramid_expand`, | |
| :func:`~skimage.transform.radon`. | |
| - Image transformation: | |
| These transforms change the appearance of an image without changing its | |
| content. They are useful for tasks such a creating image mosaics, | |
| applying artistic effects, and visualizing image data. | |
| Examples: :func:`~skimage.transform.warp`, | |
| :func:`~skimage.transform.iradon`. | |
| Examples: :class:`~skimage2.transform.AffineTransform`, | |
| :class:`~skimage2.transform.ProjectiveTransform`, | |
| :class:`~skimage2.transform.EuclideanTransform`. | |
| - Image resizing and rescaling: | |
| These transforms change the size or resolution of an image. | |
| They are useful for tasks such as down-sampling an image to | |
| reduce its size or up-sampling an image to increase its resolution. | |
| Examples: :func:`~skimage2.transform.resize`, | |
| :func:`~skimage2.transform.rescale`. | |
| - Feature detection and extraction: | |
| These transforms identify and extract specific features or | |
| patterns in an image. They are useful for tasks such as object | |
| detection, image segmentation, and feature matching. | |
| Examples: :func:`~skimage2.transform.hough_circle`, | |
| :func:`~skimage2.transform.pyramid_expand`, | |
| :func:`~skimage2.transform.radon`. | |
| - Image transformation: | |
| These transforms change the appearance of an image without changing its | |
| content. They are useful for tasks such a creating image mosaics, | |
| applying artistic effects, and visualizing image data. | |
| Examples: :func:`~skimage2.transform.warp`, | |
| :func:`~skimage2.transform.iradon`. |
|
Reviews (2): Last reviewed commit: "Bug in setting output dir" | Re-trigger Greptile |
|
Reviews (3): Last reviewed commit: "Also pull in __doc__ from original modul..." | Re-trigger Greptile |
* origin/main: 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)
|
Reviews (4): Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." | Re-trigger Greptile |
dbf0e9b to
bc8821e
Compare
|
Reviews (5): Last reviewed commit: "Fix new doctest failure" | Re-trigger Greptile |
Failure is from new Numpy reprs. See <scikit-image#8128> for a comprehensive look at this problem.
bc8821e to
d3e2d2c
Compare
|
Reviews (6): Last reviewed commit: "Add Greptile fix" | Re-trigger Greptile |
| import numpy as np | ||
| import pytest | ||
|
|
||
| from ...dependency_checks import has_mpl |
There was a problem hiding this comment.
Relative import will fail without a
conftest.py in tests/skimage2/
from ...dependency_checks import has_mpl works in tests/skimage/transform/test_radon_transform.py because tests/skimage/conftest.py establishes the package boundary that lets pytest (with --import-mode=importlib) resolve three-level-up relative imports. The equivalent tests/skimage2/ directory has no conftest.py, so pytest may import test_radon_transform.py without the required package context, raising ImportError: attempted relative import with no known parent package at collection time — failing the entire file. A minimal conftest.py (identical to tests/skimage/conftest.py, or even an empty one) in tests/skimage2/ is needed.
|
Reviews (7): Last reviewed commit: "Greptile fix" | Re-trigger Greptile |
|
Reviews (8): Last reviewed commit: "Fix globbing" | Re-trigger Greptile |
| mod_list = ' ' + '\n '.join( | ||
| f" '{str(p.name)}'," for p in parent.glob('*.py*') | ||
| ) | ||
| mod_path = '/'.join(_parts_past_src(parent)) |
There was a problem hiding this comment.
--output-dir breaks mod_path computation
_parts_past_src(parent) is called on the output directory path (line 61). When --output-dir is supplied with a path that has no src component (e.g. --output-dir ../skimage/transform or any absolute path outside the project tree), _parts_past_src raises ValueError: No 'src' string in parts, crashing the entire tool before any meson.build is written. The mod_path used in the meson template (e.g. skimage/transform) should instead be derived from the input directory's module name, translated to the output namespace (replacing _skimage2 with skimage).
5a9d2a4 to
21903db
Compare
|
Reviews (9): Last reviewed commit: "Fix globbing" | Re-trigger Greptile |
| In contrast to interpolation in `skimage.transform.resize` and | ||
| `skimage.transform.rescale` this function calculates the local mean of | ||
| elements in each block of size `factors` in the input image. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| image : (M[, ...]) ndarray | ||
| Input image. | ||
| factors : array_like | ||
| Array containing down-sampling integer factor along each axis. | ||
| cval : float, optional | ||
| Constant padding value if image is not perfectly divisible by the | ||
| integer factors. | ||
| clip : bool, optional | ||
| Unused, but kept here for API consistency with the other transforms | ||
| in this module. (The local mean will never fall outside the range | ||
| of values in the input image, assuming the provided `cval` also | ||
| falls within that range.) | ||
|
|
||
| Returns | ||
| ------- | ||
| image : ndarray | ||
| Down-sampled image with same number of dimensions as input image. | ||
| For integer inputs, the output dtype will be ``float64``. | ||
| See :func:`numpy.mean` for details. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> a = np.arange(15).reshape(3, 5) | ||
| >>> a | ||
| array([[ 0, 1, 2, 3, 4], | ||
| [ 5, 6, 7, 8, 9], | ||
| [10, 11, 12, 13, 14]]) | ||
| >>> downscale_local_mean(a, (2, 3)) | ||
| array([[3.5, 4. ], | ||
| [5.5, 4.5]]) | ||
|
|
||
| """ | ||
| from skimage.measure import block_reduce | ||
|
|
||
| return block_reduce(image, factors, np.mean, cval) | ||
|
|
||
|
|
||
| def _swirl_mapping(xy, center, rotation, strength, radius): | ||
| x, y = xy.T | ||
| x0, y0 = center | ||
| rho = np.sqrt((x - x0) ** 2 + (y - y0) ** 2) |
There was a problem hiding this comment.
downscale_local_mean silently fails when skimage.measure is unavailable
downscale_local_mean defers from skimage.measure import block_reduce to runtime inside the function body. Because _skimage2.transform._warps is the canonical implementation module, this creates a hidden runtime dependency on the skimage namespace. Any caller that imports _skimage2.transform.downscale_local_mean and invokes it will trigger a backwards cross-namespace import at call time, which is invisible until the function is actually executed. The block_reduce helper should either be copied into _skimage2._shared or satisfied from a ported _skimage2.measure dependency once available.
| ) | ||
| parent = out_fname.parent | ||
| directories.add(parent) | ||
| if not parent.exists(): | ||
| parent.mkdir(parents=True) | ||
| if mod_fname.stem == '__init__': | ||
| out_fname.write_text(mod_fname.read_text()) | ||
| else: |
There was a problem hiding this comment.
__init__ files are copied verbatim into the shim directory
When mod_fname.stem == '__init__', the tool writes the _skimage2 __init__.py verbatim to the output (skimage) directory. The _skimage2/transform/__init__.py calls _lazy.attach_stub(__name__, __file__), which means when copied to skimage/transform/, it would look for skimage/transform/__init__.pyi instead of _skimage2/transform/__init__.pyi. In this PR the skimage/transform/__init__.py was not overwritten, so there is no immediate breakage, but re-running the tool against a previously untouched __init__.py would silently produce a broken shim that attempts to use the wrong stub file for lazy loading.
|
Superseded by #8204 . |
Here I did:
git mv src/skimage/transform src/_skimage2/cp -r tests/skimage/transform tests/skimage2python tools/port_module.py src/_skimage2/transform_skimage2.__init__.pyiskimagemodules._imports to shim.pyfiles. Discoverable by running tests.On doctests
I have left the doctests in the
_skimage2/transformstree with>>> import skimage as ski.We need to think about what to do here. I suspect the best thing would be to have an automated docstring processor in the shim modules that does the relevant search and replace on the docstrings, to convert from
>>> import skimage2 as ski2etc toimport skimage as ski.On lazy loading
With: EAGER_IMPORT=0:
EAGER_IMPORT=1
This is about the same as before these changes (current main):
EAGER_IMPORT=0
EAGER_IMPORT=1
Import time for
skimagewithEAGER_IMPORT=0is the same:On main:
However, for reasons unclear, overall import time with
EAGER_IMPORT=1is longer with these changes, possibly due to the duplication of the modules with the shims:On main:
The questions for the porting procedure are: is there some reasonable chance that a) it will affect not-lazy import times significantly, and b) it will not be fixable and c) it would be fixable if we do the port in some other way?