serpent-tools icon indicating copy to clipboard operation
serpent-tools copied to clipboard

ENH Delegate matplotlib to be an optional dependency

Open drewejohnson opened this issue 5 years ago • 0 comments

The visualization capabilities are a strong benefit for using serpentTools and should not be negatively impacted by this feature

Is your feature request related to a problem? Please describe. matplotlib is a wonderful package. It is not always needed at the same level numpy is for this package. At the least, importing matplotlib can cause an unnecessary slow down if all I want to do is read in data, using serpentTools more like a library than a visualization tool.

Consider pandas, another wonderful package that has wonderful and broad plotting capabilities. And yet, matplotlib is not a requirement pandas:

setuptools_kwargs = {
    "install_requires": [
        "python-dateutil >= 2.6.1",
        "pytz >= 2017.2",
        f"numpy >= {min_numpy_ver}",
    ],
    "setup_requires": [f"numpy >= {min_numpy_ver}"],
    "zip_safe": False,
}

Describe the solution you'd like I see two solutions that can be incorporated separately.

  1. Delay the importing of matplotlib.pyplot until the moment a plot routine is called. Most of the plotting routines have logic like
def plot(ax=None):
    if ax is None:
        ax = pyplot.gca()

where from matplotlib import pyplot or from matplotlib.pyplot import gca is included at the top of the file. We can delay this import by using some wrappers, like

def getPlotAx(**kwargs):
    from matplotlib.pyplot import gca
    return gca(**kwargs)

then later

def plot(ax=None):
    if ax is None:
        ax = getPlotAx()

Similar interfaces can be done for figures and subplots.

  1. Create a section in the setup routine that indicates matplotlib is an extra dependency, something like
setup(
    ...
    extra_requires = {
        "plotting": ["matplotlib"],
    },
    ...
)

This allows people to install serpentTools just as a parser / library without the plotting support. If this capability is needed, one can install with `pip install serpentTools[plotting].

Describe alternatives you've considered Hard to avoid this as a user / installer.

Additional context I am conflicted with action 2. First, in my opinion, the plotting capabilities are a valuable asset of serpentTools. I am not proposing to drop or heavily alter these capabilities. Instead, I am proposing to keep the dependencies to a strict minimum, and matplotlib is not required to parse the files and obtain the data.

On the other hand, if people see our posters or examples at a conference or in a paper, install the package (without matplotlib as a dependency nor previously installed), and try to plot, they will be greeted with an error. That probably won't sit well. We can include a nice try/except statement in the guarded plot routines indicating that installing matplotlib or serpentTools[plotting] is required, but I'm not sure how well that may get received.

drewejohnson avatar Jan 10 '20 20:01 drewejohnson