Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Delegate matplotlib to be an optional dependency #375

Open
drewejohnson opened this issue Jan 10, 2020 · 0 comments
Open

ENH Delegate matplotlib to be an optional dependency #375

drewejohnson opened this issue Jan 10, 2020 · 0 comments
Labels
API - Compatible Changes to our API that require no user actions enhancement

Comments

@drewejohnson
Copy link
Collaborator

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 drewejohnson added enhancement API - Compatible Changes to our API that require no user actions labels Jan 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Compatible Changes to our API that require no user actions enhancement
Projects
None yet
Development

No branches or pull requests

1 participant