Skip to content

🤖 Remove legacy NumPy print options fixture - #8128

Open
stefanv wants to merge 9 commits into
scikit-image:pre-megamove-integrationfrom
stefanv:np2-doctest-fixes
Open

stefanv wants to merge 9 commits into
scikit-image:pre-megamove-integrationfrom
stefanv:np2-doctest-fixes

Conversation

@stefanv

@stefanv stefanv commented Apr 22, 2026 •

Copy link
Copy Markdown
Member

As discussed on the community call, this is an AI-assisted PR to remove the NumPy 2.0 fixture.

First, I removed the fixture and got the doctests to pass. I noticed that there were cases in which we returned mixed types (NumPy scalars and regular scalars), and also that some of our functions retured np.True_/np.False_ instead of Python bools. The second commit takes care of the bool types, and the third of the mixed return types.

When merging, please include the individual commit messages. I included the Linux kernel recommended Assisted-by tag; should this be part of our AI policy?

Addresses #8125.

stefanv added 3 commits April 22, 2026 15:51
…Py 2.0

Remove the handle_np2 fixture from src/skimage/conftest.py that set
np.set_printoptions(legacy='1.21'). Since we now require numpy>=2.1,
the legacy mode is no longer needed.

Update all doctest expected values to reflect NumPy 2.0 scalar output:
- Scalars now display as np.float64(x), np.int64(x), np.True_, np.False_
- Tuples containing scalars show mixed types

Fixes:
- io/collection.py: Fixed variable name bug (image_col -> ic)

All 300 doctests pass with 7 skipped (for optional dependencies).

Assisted-by: MiniMax/minimax-m2.7 pi
Modify is_low_contrast to return bool(ratio < fraction_threshold) instead
of the numpy scalar, and numeric_dtype_min_max for bool dtype to return
Python (False, True) instead of numpy boolean scalars.

This reverts the docstring changes for these specific functions to show
True/False instead of np.True_/np.False_.

The remaining np.True_/np.False_ occurrences in doctests are due to
comparison expressions evaluated within the doctest itself, which cannot
be fixed without changing the doctest syntax.

Assisted-by: MiniMax/minimax-m2.7 pi
Fix _edge_generator_from_csr to yield Python int/float instead of
numpy.int32/numpy.float64.

Fix route_through_array to return Python float cost instead of
numpy.float64.

Revert doctest expected values to show clean Python types.

Assisted-by: MiniMax/minimax-m2.7 pi
@greptile-apps

greptile-apps Bot commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "Fix mixed Python/NumPy type returns in g..." | Re-trigger Greptile

Comment thread src/skimage/measure/entropy.py Resolved
Reduce floating point precision, using ellipsis to account for
variation across platforms.

Assisted-by: MiniMax/minimax-m2.7 pi
@greptile-apps

greptile-apps Bot commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Reviews (2): Last reviewed commit: "Fix shannon_entropy doctest ELLIPSIS pat..." | Re-trigger Greptile

@stefanv
stefanv requested a review from lagru April 22, 2026 23:33
@greptile-apps

greptile-apps Bot commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Reviews (3): Last reviewed commit: "Merge branch 'main' into np2-doctest-fix..." | Re-trigger Greptile

Comment thread src/_skimage2/_shared/dtype.py Outdated
elif np.issubdtype(dtype, np.dtype(bool)):
min_ = dtype.type(False)
max_ = dtype.type(True)
return False, True

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This function now returns mixed types. But this seems like a surprising thing? And this is an internal function? So no real harm in using the clunkier np.True_/np.False_ right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think we illustrate it somewhere in a docstring, but fine to keep the original here as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@stefanv, I haven't yet formed a complete opinion on when to prefer NumPy or native scalar return types. Did you follow a guideline here? If so which one(s)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, a vague intuitive one: if the result is a single scalar, I changed the docstring to what the user would see. If we returned a list of mixed types, I made the types uniform (typically Python scalar). If our internal functions returned np.bool_, I changed that to Python bool.

ratio = (limits[1] - limits[0]) / (dlimits[1] - dlimits[0])

return ratio < fraction_threshold
return bool(ratio < fraction_threshold)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Technically this breaks our API. With this change result is True and result is False behave differently?

That seems like a good argument to update the doctest instead and keep the return types the same?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Though, hopefully users are aware that in context of NumPy, they shouldn't use is. And PEP 8 also recommends against using is.

So maybe we should be pragmatic here?

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.

I agree, foo is True feels like an antipattern.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

But we should make all returns from this function consistently use Python bool.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, a vague intuitive one: if the result is a single scalar, I changed the docstring to what the user would see. If we returned a list of mixed types, I made the types uniform (typically Python scalar). If our internal functions returned np.bool_, I changed that to Python bool.

Comment thread src/skimage/io/collection.py Outdated
>>> filename = data_dir + '/no_time_for_that_tiny.gif'
>>> ic = io.ImageCollection(range(24), load_func=MultiReader(filename))
>>> len(image_col)
>>> len(ic) # doctest: +SKIP

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not sure why we have to skip this one?

>>> centroid = (M[1, 0] / M[0, 0], M[0, 1] / M[0, 0])
>>> centroid
(14.5, 15.5)
(np.float64(14.5), np.float64(15.5))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@lagru an example of where it is now consistent in type, but we could also use built-in float instead. I think it will be quite annoying to always have to guard against returning numpy floats, though.

>>> g = regular_grid(ar.shape, 8)
>>> g
(slice(1, None, 3), slice(5, None, 10), slice(5, None, 10))
(slice(np.int64(1), None, np.int64(3)), slice(np.int64(5), None, np.int64(10)), slice(np.int64(5), None, np.int64(10)))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@lagru Another case where we may want to prefer built-in type for readability?

Comment thread src/skimage/conftest.py

np.set_printoptions(legacy="1.21")
except ImportError:
pass

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think we can remove this file now.

Comment thread src/_skimage2/_shared/dtype.py Outdated
elif np.issubdtype(dtype, np.dtype(bool)):
min_ = dtype.type(False)
max_ = dtype.type(True)
return False, True

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think we illustrate it somewhere in a docstring, but fine to keep the original here as well.

@blink1073

Copy link
Copy Markdown
Contributor

My gut feeling after reading https://numpy.org/doc/2.1/reference/generated/numpy.set_printoptions.html is that we should globally use a sensible set_printoptions to get consistent output in the docstrings, that is not subject to changes in numpy version, as long as it is reasonably close to what the user would see.

@stefanv

stefanv commented Apr 24, 2026

Copy link
Copy Markdown
Member Author

Yes, I think setting that consistently to prevent future changes is fine, but the 2.0 changes are large enough to potentially confuse users reading the docstrings. I think it is also worth returning single type lists and tuples and Python bools.

@blink1073

Copy link
Copy Markdown
Contributor

Hmm, looking at SPEC0, it seems like it is time to drop numpy 1.x altogether.

- Make is_low_contrast consistently return Python bool on all paths
- Delete now-empty conftest.py (legacy NumPy print options fixture)
- Fix len(ic) in ImageCollection doctest (24, not 23); remove SKIP
- Revert numeric_dtype_min_max(bool): return np.False_/np.True_,
  matching original behavior for this internal function; update docstring

Assisted-by: Kimi/kimi-k2.6 pi
@greptile-apps

greptile-apps Bot commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Reviews (4): Last reviewed commit: "Address lagru/stefanv review comments on..." | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Reviews (5): Last reviewed commit: "Remove conftest.py from installation" | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

Reviews (6): Last reviewed commit: "Merge branch 'main' into np2-doctest-fix..." | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

Comment thread src/skimage/meson.build Resolved
@greptile-apps

greptile-apps Bot commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

Reviews (7): Last reviewed commit: "Fix borked merge" | Re-trigger Greptile

matthew-brett added a commit to matthew-brett/scikit-image that referenced this pull request May 19, 2026
Failure is from new Numpy reprs.

See <scikit-image#8128> for
a discussion of the problem.
matthew-brett added a commit to matthew-brett/scikit-image that referenced this pull request May 19, 2026
Failure is from new Numpy reprs.

See <scikit-image#8128> for
a discussion of the problem.
matthew-brett added a commit to matthew-brett/scikit-image that referenced this pull request May 19, 2026
Failure is from new Numpy reprs.

See <scikit-image#8128> for
a comprehensive look at this problem.
@lagru lagru added the 🔧 type: Maintenance Refactoring and maintenance of internals label Jun 12, 2026
@lagru lagru mentioned this pull request Jun 12, 2026
5 of 18 tasks
@matthew-brett
matthew-brett changed the base branch from main to pre-megamove-integration June 19, 2026 19:59
@matthew-brett

Copy link
Copy Markdown
Contributor

We have changed the base branch for this PR from the main branch to the pre-megamove-integration branch.

Summary - what this means for your PR

You can continue to work on your PR, now based on pre-megamove-integration. You should update your branch to the current state of pre-megamove-integration (this is what the Github "Update branch" button does). Please do not merge the scikit-image main branch into your PR branch.

Once we've merged your PR into the pre-megamove-integration branch, we'll update your changes for Megamove, and merge into the main branch.

Read on for the rationale, and more detailed instructions.

Details

Rationale

We've changed the base branch to make it easier and smoother to continue to work on your PR, while we work through a rather large change in the directory structure of the repository - a change that we have called Megamove.

See the Megamove explanation issue for more detail.

Changing the base branch means you do not see merge conflicts related to the Megamove change, and can continue to work with the repository in its original state, without worrying about porting those changes across to the new post-Megamove structure.

As you'll see in the linked issue, you can continue to work on your PR as before, and you can continue to use the Github GUI to "Update branch" - this updates from the new pre-megamove-integration branch. However, there is one caveat: please, do not try and merge the scikit-image main branch into this one, but instead, keep track of the pre-megamove-integration branch. The issue explains more.

To emphasize again, do not try and merge the scikit-image main branch. You will get Megamove related merge conflicts that are rather tricky to resolve correctly.

When we've approved your PR, we'll merge into the pre-megamove-integration branch, and then process it so we can merge it into the main branch. We'll let you know when we've merged your PR changes into main.

Any questions, please reply here.

This branch has not been deployed

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

Labels

🔧 type: Maintenance Refactoring and maintenance of internals

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants