Skip to content

Demo for porting a whole module into _skimage2 - #8172

Closed
matthew-brett wants to merge 14 commits into
scikit-image:mainfrom
matthew-brett:portissimo-experiment
Closed

matthew-brett wants to merge 14 commits into
scikit-image:mainfrom
matthew-brett:portissimo-experiment

Conversation

@matthew-brett

@matthew-brett matthew-brett commented May 18, 2026 •

Copy link
Copy Markdown
Contributor

Here I did:

  • 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:
    • Modifying test imports to use _skimage2.
    • Modifying meson.build files for moved sources.
    • Adding copied source module to top-level meson.build files and __init__.pyi
    • Worked round incomplete port by local imports of needed skimage modules.
  • Fixed up the tests:
    • Add any needed _ imports to shim .py files. Discoverable by running tests.

On doctests

I have left the doctests in the _skimage2/transforms tree 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 ski2 etc to import skimage as ski.

On lazy loading

With: EAGER_IMPORT=0:

In [2]: timeit -n1 -r1 from skimage.transform import hough_line
510 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

EAGER_IMPORT=1

[ins] In [2]: timeit -n1 -r1 from skimage.transform import hough_line
37.8 μs ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

This is about the same as before these changes (current main):

EAGER_IMPORT=0

[ins] In [2]: timeit -n1 -r1 from skimage.transform import hough_line
478 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

EAGER_IMPORT=1

[ins] In [2]: timeit -n1 -r1 from skimage.transform import hough_line
31.6 μs ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

Import time for skimage with EAGER_IMPORT=0 is the same:

[ins] In [1]: timeit -n1 -r1  import skimage
48.6 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

On main:

[ins] In [1]: timeit -n1 -r1  import skimage
47.8 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

However, for reasons unclear, overall import time with EAGER_IMPORT=1 is longer with these changes, possibly due to the duplication of the modules with the shims:

[ins] In [1]: timeit -n1 -r1  import skimage
3.66 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

On main:

[ins] In [1]: timeit -n1 -r1  import skimage
2.34 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

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?

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.
@greptile-apps

greptile-apps Bot commented May 18, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "Demo for porting a whole module into _sk..." | Re-trigger Greptile

Comment thread tools/port_module.py Resolved
Comment thread src/_skimage2/transform/_warps.py Resolved
2

"""
from skimage.feature.peak import _prominent_peaks

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

Comment on lines +7 to +31
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`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

Suggested change
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`.

@greptile-apps

greptile-apps Bot commented May 18, 2026

Copy link
Copy Markdown
Contributor

Reviews (2): Last reviewed commit: "Bug in setting output dir" | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

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)
@greptile-apps

greptile-apps Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

Reviews (4): Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." | Re-trigger Greptile

Comment thread tools/port_module.py Resolved
Comment thread tools/port_module.py Resolved
Comment thread tools/port_module.py Resolved
@matthew-brett
matthew-brett force-pushed the portissimo-experiment branch from dbf0e9b to bc8821e Compare May 19, 2026 10:48
@greptile-apps

greptile-apps Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

Reviews (5): Last reviewed commit: "Fix new doctest failure" | Re-trigger Greptile

Comment thread tools/port_module.py Resolved
Failure is from new Numpy reprs.

See <scikit-image#8128> for
a comprehensive look at this problem.
@matthew-brett
matthew-brett force-pushed the portissimo-experiment branch from bc8821e to d3e2d2c Compare May 19, 2026 11:11
@greptile-apps

greptile-apps Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

Reviews (6): Last reviewed commit: "Add Greptile fix" | Re-trigger Greptile

Comment thread tools/port_module.py Resolved
import numpy as np
import pytest

from ...dependency_checks import has_mpl

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

@greptile-apps

greptile-apps Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

Reviews (7): Last reviewed commit: "Greptile fix" | Re-trigger Greptile

Comment thread tools/port_module.py Resolved
@greptile-apps

greptile-apps Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

Reviews (8): Last reviewed commit: "Fix globbing" | Re-trigger Greptile

Comment thread tools/port_module.py
mod_list = ' ' + '\n '.join(
f" '{str(p.name)}'," for p in parent.glob('*.py*')
)
mod_path = '/'.join(_parts_past_src(parent))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 --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).

@matthew-brett
matthew-brett force-pushed the portissimo-experiment branch from 5a9d2a4 to 21903db Compare May 19, 2026 12:34
@greptile-apps

greptile-apps Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

Reviews (9): Last reviewed commit: "Fix globbing" | Re-trigger Greptile

Comment on lines +477 to +523
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

Comment thread tools/port_module.py
Comment on lines +45 to +52
)
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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 __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.

@matthew-brett

Copy link
Copy Markdown
Contributor Author

Superseded by #8204 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant