Skip to content

Make matplotlib an optional dependency - #5990

Merged
jni merged 24 commits into
scikit-image:mainfrom
rfezzani:Make_matplotlib_optional_dep
Nov 5, 2021
Merged

jni merged 24 commits into
scikit-image:mainfrom
rfezzani:Make_matplotlib_optional_dep

Conversation

@rfezzani

@rfezzani rfezzani commented Oct 26, 2021 •

Copy link
Copy Markdown
Member

Description

This PR supersede @hmaarrfk PR #3129. It basically

  • defines skimage._shared.has_mpl flag
  • uses skimage._shared.version_requirements.require decorator on the function using matplotlib
  • Disable skimage.viewer default imports if matplotlib is not installed
  • skip tests requiring matplotlib when it is not installed.

Checklist

  • Docstrings for all functions
  • Gallery example in ./doc/examples (new features only)
  • Benchmark in ./benchmarks, if your changes aren't covered by an
    existing benchmark
  • Unit tests
  • Clean style in the spirit of PEP8
  • Descriptive commit messages (see below)

For reviewers

  • Check that the PR title is short, concise, and will make sense 1 year
    later.
  • Check that new functions are imported in corresponding __init__.py.
  • Check that new features, API changes, and deprecations are mentioned in
    doc/release/release_dev.rst.

@rfezzani rfezzani added the 🔧 type: Maintenance Refactoring and maintenance of internals label Oct 26, 2021
@pep8speaks

pep8speaks commented Oct 26, 2021 •

Copy link
Copy Markdown

Hello @rfezzani! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 6:1: E402 module level import not at top of file
Line 9:1: E402 module level import not at top of file

Comment last updated at 2021-11-03 14:28:38 UTC



@require("matplotlib", ">=3.0.3")
def _draw_polygon(ax, vertices, alpha=0.4):

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.

I think the original comment was that we didn't want to start to ship analysis code where functionality depended on the libraries installed.

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.

😕 hum, I don't remember this discussion. Is it still the case?
Most function depending on matplotlib are visualization tools/utils. The only "real" functionality is draw.polygon_perimeter 🙁

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.

I think this was roughly 3 years ago, back when i was trying to slim down the dependencies .I don't remember you on the discussions at that time!

In either case, let me try to find it.

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.

I guess the discussion was elsewhere, but my attempt was to create our own implementation of the draw polygon algorithm.

https://github.com/scikit-image/scikit-image/pull/3129/files#r354616136

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 see, OK, thank you @hmaarrfk. Let's wait to see if this is a strong requirement from @scikit-image/core members, otherwise I'll investigate more to overcome this 😉

@grlee77 grlee77 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.

The implementation here looks good. The main question is whether or not we want to do this.

The main benefit is that a somewhat large dependency becomes optional. The primary downside is that there are a small subset of functions that will not be usable if the user has not installed matplotlib. As far as I can tell, these are:

skimage.draw.polygon_clip
skimage.draw.polygon_perimeter
skimage.draw.rectangle_perimeter
skimage.filters.try_all_threshold
skimage.future.graph.show_rag
skimage.future.manual_lasso_segmentation
skimage.future.manual_polygon_segmentation

I think I am weakly in favor of having this as an optional dependency, since there are likely many users that do not need the above functions. I would guess that a majority of these will already have Matplotlib or not care if it gets pulled in to a scikit-image install. For any users who really want a minimal install of scikit-image, though, this would help them.

Comment thread requirements/optional.txt Resolved
@stefanv

stefanv commented Oct 29, 2021

Copy link
Copy Markdown
Member

The biggest advantage may be that we can release for new versions of Python earlier if we do not depend on Matplotlib wheels being available. Other than that, I am agnostic: most users will install them together anyway.

@jni jni left a comment

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.

I 👍. I was the one who raised the most fuss about fixing the draw functions to not require mpl, but, three years later, I think the advantage of shipping without mpl as a dependency outweigh the fact that we would miss those three functions. If someone needs the functions and really needs to have them without mpl, perhaps they will be able to help with that implementation. ;) Thanks @rfezzani!

@jni

jni commented Nov 3, 2021

Copy link
Copy Markdown
Member

@scikit-image/core if there are no objections in the next ~24h I will go ahead and merge this.

Comment thread skimage/feature/tests/test_util.py Resolved
Comment thread skimage/viewer/__init__.py Resolved
Comment thread skimage/viewer/tests/test_plugins.py Resolved


@testing.skipif(not has_qt, reason="Qt not installed")
@pytest.mark.skipif(not has_qt, reason="Qt not installed")

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.

It looks like instances of "Qt not installed" are more numerous than "Needs matplotlib" so maybe the consistency concern should be the other way around.

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.

Thank @mkcor for your review, I fixed it the way around: "Needs matplotlib" -> "matplotlib not installed"

@mkcor

mkcor commented Nov 3, 2021

Copy link
Copy Markdown
Member

@scikit-image/core if there are no objections in the next ~24h I will go ahead and merge this.

@jni ok, I just left minor suggestions!

@jni jni changed the title Make matplotlib optional dep Make matplotlib an optional dependency Nov 5, 2021
@jni
jni merged commit e213917 into scikit-image:main Nov 5, 2021
@rfezzani

rfezzani commented Nov 5, 2021

Copy link
Copy Markdown
Member Author

Thank you all for your comments and reviews 🎉

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.

7 participants