Skip to content

Can matplotlib be made an optional dependency? #2103

Closed

Description

@letmaik

I am using only a small part of scikit-image in a non-interactive environment, currently the skimage.filter.rank functionality and it is a bit suboptimal that my package then indirectly has to depend on the quite big matplotlib dependency, especially since I don't use it. For compiling matplotlib on linux you also have to install some system level libraries like libpng and freetype which may not exist in some headless servers by default. So, at least for me it would be a lot better if I could use scikit-image without matplotlib. It would be interesting to know how many parts of scikit-image depend on matplotlib.

Activity

  1. JDWarner commented on May 23, 2016

    @JDWarner
    Contributor

    @neothemachine this is an interesting question. I looked into it and found that the vast majority of matplotlib uses are in convenience visualization functions and example code as well as the documentation. In all such cases, matplotlib imports are at the top level of the files; in such cases they could be relatively simply refactored into functions.

    However, there are a few problematic areas.

    • skimage._shared._geometry.py uses matplotlib for polygon manipulation. Not just the display, but low level functions where we depend on matplotlib's implementation.
    • matplotlib is, AFAIK, the only plugin we can count on being present for skimage.io. Making this optional could easily render minimal installations unable to load image data.
    • The tifffile plugin uses matplotlib for viewing. We ship this for convenience and try not to diverge from the original, so refactoring would be more difficult.

    As far as I can tell, there's no need for matplotlib as an install dependency. At best it would be a mostly optional runtime dependency, with some exceptions. It looks like skimage.filters.rank stands alone, but you'd probably be surprised at how much of the package links directly or indirectly to skimage._shared._geometry.

    The other challenge is the user experience. Having a package installed and then spring additional dependencies on users when they access a new part of it is not ideal.

  2. letmaik commented on May 23, 2016

    @letmaik
    Author

    I had a look at skimage._shared._geometry. So this is only polygon clipping. I tried to check how many skimage code parts depend on ._shared._geometry. Only skimage.draw depends on it directly (the draw.polygon_perimeter function of it uses polygon_clip). The following depend on ._shared._geometry indirectly via skimage.draw:

    • skimage/morphology/selem.py (draw.ellipse)
    • skimage/transform/_hough_transform.pyx (draw.circle_perimeter)
    • many doc examples

    The funny thing is, the selem.py and _hough_transform.pyx don't actually use the polygon_clip function, so they are independent of matplotlib if the code would be refactored a bit. In the end it is really just the skimage.draw.polygon_perimeter function which is not used in skimage itself, but of course, who knows which external packages use it.

    Regarding skimage.io, what about pillow? This is also a dependency and used as an io plugin, right?

    About tifffile, the imshow function of it is not exposed by the plugin, and inside the tifffile.py file matplotlib is imported lazily, so that shouldn't be a problem I think.

    I think none of the things above are real blockers. Now, the more important part, user experience. This is indeed a tricky one. However, I have a feeling that people who use scikit-image use matplotlib anyway, so I would guess that making it an optional dependency would not cause much disruption in the end.

    I would be interested what others think as well.

  3. stefanv commented on May 23, 2016

    @stefanv
    Member

    The primary reason that matplotlib is a dependency is because it provides a
    standard way of loading png/jpeg files (you run into the same problem if
    you make pillow the default--it also depends on libjpeg etc.). This is
    needed to run tests, etc., so we can't really get rid of it.

    That said, you can potentially install skimage without matplotlib, if you
    know that you won't be needing any of that functionality?

    On 23 May 2016 at 15:18, Maik Riechert [email protected] wrote:

    I had a look at skimage._shared._geometry. So this is only polygon
    clipping. I tried to check how many skimage code parts depend on
    ._shared._geometry. Only skimage.draw depends on it directly (the
    draw.polygon_perimeter function of it uses polygon_clip). The following
    depend on ._shared._geometry indirectly via skimage.draw:

    • skimage/morphology/selem.py (draw.ellipse)
    • skimage/transform/_hough_transform.pyx (draw.circle_perimeter)
    • many doc examples

    The funny thing is, the selem.py and _hough_transform.pyx don't actually
    use the polygon_clip function, so they are independent of matplotlib if the
    code would be refactored a bit. In the end it is really just the
    skimage.draw.polygon_perimeter function which is not used in skimage
    itself, but of course, who knows which external packages use it.

    Regarding skimage.io, what about pillow? This is also a dependency and
    used as an io plugin, right?

    About tifffile, the imshow function of it is not exposed
    https://github.com/scikit-image/scikit-image/blob/master/skimage/io/_plugins/tifffile_plugin.ini
    by the plugin, and inside the tifffile.py file matplotlib is imported
    lazily, so that shouldn't be a problem I think.

    I think none of the things above are real blockers. Now, the more
    important part, user experience. This is indeed a tricky one. However, I
    have a feeling that people who use scikit-image use matplotlib anyway, so I
    would guess that making it an optional dependency would not cause much
    disruption in the end.

    I would be interested what others think as well.

    —
    You are receiving this because you are subscribed to this thread.
    Reply to this email directly or view it on GitHub
    #2103 (comment)

  4. JDWarner commented on May 23, 2016

    @JDWarner
    Contributor

    I don't have any problem rearranging imports to minimize the need for matplotlib in files where it's only used in a subset of functions. In some (most?) cases splitting out a separate file is likely more elegant than moving imports into functions. There is room for improvement here.

  5. jni commented on May 23, 2016

    @jni
    Member

    I think I remember seeing a couple of BSD-licensed, pure-python JPEG and PNG readers. It might be useful to bundle those with skimage as ultimate fallbacks for IO.

  6. stefanv commented on May 23, 2016

    @stefanv
    Member
  7. jni commented on May 24, 2016

    @jni
    Member
  8. stefanv commented on May 24, 2016

    @stefanv
    Member
  9. espdev commented on Jun 10, 2016

    @espdev

    The primary reason that matplotlib is a dependency is because it provides a
    standard way of loading png/jpeg files (you run into the same problem if
    you make pillow the default--it also depends on libjpeg etc.). This is
    needed to run tests, etc., so we can't really get rid of it.

    @stefanv Why are you not using imageio library? I think it's lightweight and beautiful library for IO. imageio supports a lot of image formats and depends only from numpy.

  10. stefanv commented on Jun 11, 2016

    @stefanv
    Member

    On 10 June 2016 at 15:52, Eugene Prilepin [email protected] wrote:

    The primary reason that matplotlib is a dependency is because it provides a
    standard way of loading png/jpeg files (you run into the same problem if
    you make pillow the default--it also depends on libjpeg etc.). This is
    needed to run tests, etc., so we can't really get rid of it.

    @stefanv https://github.com/stefanv Why are you not using imageio
    http://imageio.github.io library? I think it's lightweight and
    beautiful library for IO. imageio supports many image formats
    http://Lightweight and depends only from numpy.

    Currently, because imageio is GPL.

  11. stefanv commented on Jun 11, 2016

    @stefanv
    Member
  12. added
    🔧 type: Maintenance
    on Jun 11, 2016
  13. sciunto commented on Jun 11, 2016

    @sciunto
    Member

    As pointed out by @soupault #2110 will introduce a new function that depends on matplotlib.

  14. alexjc commented on Feb 23, 2017

    @alexjc

    Since other Issues on the topic refer to this one, I thought I'd add some extra arguments in support of the original post. Picking matplotlib over pillow as default implies a ~400Mb extra installation on disk (total dependencies, measured in conda) as well as importing Qt into memory by default upfront. This is obviously sub-optimal whether you're running processes on a server or in an IPython notebook. (Who uses Qt on either? :-) pillow on the otherhand is lightweight, self-contained and already used by scipy (although optional).

    I'd love to see a standalone _geometry.py or at the very least, a lazy import to reduce both these overheads. 👍 for this and #2281.

  15. noamraph commented on May 30, 2017

    @noamraph

    I would really appreciate removing the matplotlib dependency, for all the reasons stated above.

    About image export, I don't understand why not use Pillow.

    About user experience, I think that raising an exception saying "Please install matplotlib to use this" is the best approach. The minority of users that don't yet have matplotlib installed will know immediately what to do.

  16. jni commented on May 30, 2017

    @jni
    Member

    @noamraph thanks for the ping! So by my reading, #2281 was merged (with #2596), which should mean that you can at least pip install git+https://github.com/scikit-image/scikit-image --no-deps -U and have a working installation, even without matplotlib installed.

    We now depend on Pillow, too, so that's done.

    @scikit-image/core anyone up for moving matplotlib out of requirements.txt and into optional dependencies?

  17. soupault commented on May 31, 2017

    @soupault
    Member

    @jni I'd be fine with this. Although, we should agree on the way we handle optional imports first.

  18. stefanv commented on Jul 15, 2017

    @stefanv
    Member

    I turned this into a concrete task at #2710

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    ⚖️ Needs decision🔧 type: Maintenance

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions