Skip to content

Skimage2 -> Skimage1 doctest postprocessing - #8219

Merged
matthew-brett merged 27 commits into
scikit-image:mainfrom
matthew-brett:ski-ski2-doctest-shims
Jul 10, 2026
Merged

matthew-brett merged 27 commits into
scikit-image:mainfrom
matthew-brett:ski-ski2-doctest-shims

Conversation

@matthew-brett

@matthew-brett matthew-brett commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Adapt shim docstrings so skimage users see skimage doctests.

After the megamove, canonical docstrings live in _skimage2 and use internal
imports (e.g. from _skimage2 import data). This adds a shim-layer adapter so
help(), Sphinx, and skimage doctests expose skimage examples instead,
while leaving skimage2 doctests intact.

There are various things worth discussing:

  • Functions can be dealt with pretty easily, by "copying" (see _copy_callable in src/skimage/_docstring_adapters.py), and replacing the docstring.
  • Classes are a whole lot more difficult. We first identify whether any of the class or method docstrings need modification. If so, we make a new class that inherits from the old, and modify the docstrings accordingly.
  • The class fix introduced a complication for using the skimage.transform._geometric classes. The new classes inherit from the old, but this affects the inheritance line of the new classes. Consider original class B that inherits from original class A. The new class A' inherits from A, and new class B' inherits from B. But now B' does not inherit from A'. This messes up some behavior, and in particular it was messing up the behavior of the __add__ function for the ProjectiveTransform hierarchy. I used a version of a fix suggested by DeepSeek, which is to a) use ABC.register formally note the inheritance of e.g.AffineTransform from ProjectiveTransform, and b) rewrite the __add__ method of the shim versions to refer to the shim versions of ProjectiveTransform. This is somewhat of a hack, but at least the hack is local to the shims, and we're expecting to be able to remove the shims in a few years.
  • In order to make the io.collections docstring work, I needed to the docstrings to know where the in-tree data files are. They are always in the _skimage2 tree, and not in the skimage tree. So I needed a new legacy_data_dir attribute of _skimage2.data to point to the directory containing these files - _skimage2/data - so I could use this for doctests in both _skimage2 and skimage. This does add something to the API surface for both namespaces, but we do need some way of telling the user where these files are - or we need to move them somewhere - or we need to adapt data fetching somehow so the user can get the files without needed their location. Happy for advice.

AI declaration

Initially generated by Cursor and Deepseek. I have guided and refactored and reviewed, so the current code is a substantial mix of AI and I.

@greptile-apps

greptile-apps Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Too many files changed for review. (228 files found, 100 file limit)

Bypass the limit by tagging @greptile-apps to review.

@matthew-brett matthew-brett mentioned this pull request Jun 12, 2026
5 of 18 tasks
@matthew-brett
matthew-brett force-pushed the ski-ski2-doctest-shims branch 2 times, most recently from 5360d73 to 66d80dc Compare June 12, 2026 23:23
@matthew-brett
matthew-brett force-pushed the ski-ski2-doctest-shims branch 3 times, most recently from 4ab11e9 to 0ec9f56 Compare June 23, 2026 11:19
Only implicitly use own callable, and `np`, in doctests, import
everything else explicitly.
Cursor, comprehensively refactored and reviewed by MB.
@matthew-brett
matthew-brett force-pushed the ski-ski2-doctest-shims branch from 0ec9f56 to a78cd7b Compare June 23, 2026 13:01
@matthew-brett
matthew-brett marked this pull request as ready for review June 23, 2026 13:07
@matthew-brett

Copy link
Copy Markdown
Contributor Author

This one is ready for review. Notice that addition back of the skimage doctests - using skimage imports.

* origin/main:
  Ignore pyenv virtualenv env name marker (scikit-image#8233)
Always set doctest_requires markers, whether package is installed at
initial import time or not.

And add version requirement to doctest_requires for safety.

And add tests for doctest_requires machinery.
Rather than checking a possibly-varying doctest_requires element /
version.
For efficiency, but perhaps not clarity.
To allow more flexible adaptation.
Largely Deepseek with edits from MB.
@matthew-brett
matthew-brett force-pushed the ski-ski2-doctest-shims branch from c8e311e to 3865276 Compare July 3, 2026 11:27
@blink1073

Copy link
Copy Markdown
Contributor
FAILED collection.py::skimage.io.collection.ImageCollection
FAILED collection.py::skimage.io.collection.MultiImage

It looks like these two need special-casing

@@ -0,0 +1,230 @@
"""Adapt ``_skimage2`` doctest examples for the public ``skimage`` namespace.

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.

Is the plan to have a separate set of adapters in skimage2 to change the docstrings from _skimage2 -> skimage2?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well - I was thinking would be temporary, until our full release, at which point we can search replace _skimage2 in the docstrings. What do you think?

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.

That sounds reasonable for now

@matthew-brett matthew-brett changed the title Draft of skimage doctest postprocessing Skimage2 -> Skimage1 doctest postprocessing Jul 3, 2026
Only create new classes as necessary, to avoid overhead.

Refactor class-making code for clarity.
Slight refactor of original code to use class member marker as class to
return when adding instances of different types.
@matthew-brett

Copy link
Copy Markdown
Contributor Author
FAILED collection.py::skimage.io.collection.ImageCollection
FAILED collection.py::skimage.io.collection.MultiImage

It looks like these two need special-casing

Working on it!

@matthew-brett

matthew-brett commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Claude suggested a way to avoid needing shims in all of the package __init__.py modules by using a centralized meta-path loader wrapper.

Sorry - on reflection - I guess this code is to avoid the adapt_doctests boilerplate specifically? So it merely has the effect of running adapt_doctests on all the shims?

How would this deal with cases like _geometric where we have to do some post adaptation surgery?

PYTHONOPTIMIZE=2 strips docstrings, so docstrings are always None.
@matthew-brett

Copy link
Copy Markdown
Contributor Author
FAILED collection.py::skimage.io.collection.ImageCollection
FAILED collection.py::skimage.io.collection.MultiImage

It looks like these two need special-casing

See top comment in PR for discussion of legacy_data_dir fix.

@blink1073

Copy link
Copy Markdown
Contributor

I'm working on the data files refactor now in #8240.

Of relevance to this PR:

  • I opted to create data inline for the image collection examples that had relied on files being in place in the tree
  • For skimage 1.0/2.0, my proposal is that there are no data files in the wheel, but we offer a data-files extra to include a shim package to make the files available on disk, otherwise they are fetched on-demand by the helper functions (e.g. data.astronaut()).

@blink1073

Copy link
Copy Markdown
Contributor

I guess this code is to avoid the adapt_doctests boilerplate specifically?

Yes

How would this deal with cases like _geometric where we have to do some post adaptation surgery?

I think for the special cases the post adaptation would still be in __init__.py, which would be running after the generic adaptation has occurred.

@matthew-brett

Copy link
Copy Markdown
Contributor Author

I guess this code is to avoid the adapt_doctests boilerplate specifically?

Yes

How would this deal with cases like _geometric where we have to do some post adaptation surgery?

I think for the special cases the post adaptation would still be in __init__.py, which would be running after the generic adaptation has occurred.

I'm vaguely against, on the basis that you gave - that it's harder to think about what's going on. If we'd started there, I'd say yes, but given we already have all the boilerplate, my preference is to leave it. Happy to be overruled.

@blink1073

Copy link
Copy Markdown
Contributor

my preference is to leave it

Fair enough, given that this whole part of the tree will be removed. It just looked really ugly to me. 😄

_PARAMS_DEP_STOP = '2.2'


class BaseModel:

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.

It looks like we can't remove this until skimage 2.2?

@matthew-brett

matthew-brett commented Jul 4, 2026 via email

Copy link
Copy Markdown
Contributor Author

@matthew-brett

matthew-brett commented Jul 4, 2026 via email

Copy link
Copy Markdown
Contributor Author

@blink1073

blink1073 commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Ah yes I see what you mean. Perhaps we can change our minds about that?

Was this in response to my BaseModel question? The email response didn't preserve the thread

@matthew-brett

matthew-brett commented Jul 4, 2026 via email

Copy link
Copy Markdown
Contributor Author

@blink1073

Copy link
Copy Markdown
Contributor

Was there a need to remove BaseModel in this PR specifically?

@matthew-brett

matthew-brett commented Jul 4, 2026 via email

Copy link
Copy Markdown
Contributor Author

Now we aren't defining classes where there is no docstring change, we
don't need to worry about deprecationwarning in inheriting from
BaseModel in shim.
Give up on modification of
`_skimage2.transform._geometric.ProjectiveTransform` to remove any
remaining changes to `_skimage2` tree.   Apply Deepseek suggestion of
redefining `__add__` for the ProjectiveTransform classes, but do it by
hand and write my own explanations.
@matthew-brett

Copy link
Copy Markdown
Contributor Author

I moved it only because it was inconvenient to try and intercept the deprecation warning when making the skimage subclass. Can go back and do that instead. This email is fully human-source. Unless I'm quoting AI, I did not use AI for any text in this email.
…

This was in reference to BaseModel again. The problem I was trying to address by moving BaseModel was triggering the deprecation warning during adapt_doctests - but putting BaseModel back now does not retrigger that warning - probably because we are no longer creating potential sub-classes for classes without doctest examples we need to modify. So I've reverted the BaseModel change.

* origin/main:
  Convert contribution guide to Markdown format (scikit-image#8188)

@blink1073 blink1073 left a comment

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.

LGTM

@matthew-brett
matthew-brett merged commit 5d91fed into scikit-image:main Jul 10, 2026
25 checks passed
@stefanv stefanv added this to the 0.27 milestone Jul 10, 2026
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.

3 participants