You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can matplotlib be made an optional dependency? #2103
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.
@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.
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:
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.
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?
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:
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?
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)
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.
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.
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.
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.
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.
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.
@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?
Activity
JDWarner commentedon May 23, 2016
@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.io. Making this optional could easily render minimal installations unable to load image data.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.rankstands alone, but you'd probably be surprised at how much of the package links directly or indirectly toskimage._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.
letmaik commentedon May 23, 2016
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.drawdepends on it directly (the draw.polygon_perimeter function of it uses polygon_clip). The following depend on ._shared._geometry indirectly via skimage.draw: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.
stefanv commentedon May 23, 2016
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:
JDWarner commentedon May 23, 2016
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.
jni commentedon May 23, 2016
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.
stefanv commentedon May 23, 2016
jni commentedon May 24, 2016
@stefanv looks like only reading for jpeg, and with a little C module:
https://bitbucket.org/darcymason/pyjpeg/src
(Hard to find, but MIT-licensed)
png:
https://github.com/drj11/pypng
(also MIT licensed)
stefanv commentedon May 24, 2016
espdev commentedon Jun 10, 2016
@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.
stefanv commentedon Jun 11, 2016
On 10 June 2016 at 15:52, Eugene Prilepin [email protected] wrote:
Currently, because imageio is GPL.
stefanv commentedon Jun 11, 2016
sciunto commentedon Jun 11, 2016
As pointed out by @soupault #2110 will introduce a new function that depends on matplotlib.
alexjc commentedon Feb 23, 2017
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
matplotliboverpillowas 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? :-)pillowon the otherhand is lightweight, self-contained and already used byscipy(although optional).I'd love to see a standalone
_geometry.pyor at the very least, a lazy import to reduce both these overheads. 👍 for this and #2281.noamraph commentedon May 30, 2017
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.
jni commentedon May 30, 2017
@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 -Uand 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.txtand into optional dependencies?soupault commentedon May 31, 2017
@jni I'd be fine with this. Although, we should agree on the way we handle optional imports first.
stefanv commentedon Jul 15, 2017
I turned this into a concrete task at #2710