ancklo / ChaosMagPy

ChaosMagPy is a simple Python package for evaluating the CHAOS geomagnetic field model and other models of Earth's magnetic field.
Other
22 stars 3 forks source link

Dependency on matplotlib #7

Closed eelcodoornbos closed 1 month ago

eelcodoornbos commented 1 month ago

Hi! I'm using chaosmagpy in automatic processing of Swarm data for the space weather timeline viewer. I'm now running into a problem when importing chaosmagpy, where it in turn tries to import a deprecated function from matplotlib.

Traceback (most recent call last):
  File "/Users/doornbos/devel/hapi-ingest-scripts/./process_swarm_l1b_data.py", line 154, in <module>
    import chaosmagpy
  File "/Users/doornbos/.local/share/virtualenvs/hapi-ingest-scripts-PKYMczwv/lib/python3.10/site-packages/chaosmagpy/__init__.py", line 39, in <module>
    from .chaos import (
  File "/Users/doornbos/.local/share/virtualenvs/hapi-ingest-scripts-PKYMczwv/lib/python3.10/site-packages/chaosmagpy/chaos.py", line 44, in <module>
    from . import plot_utils as pu
  File "/Users/doornbos/.local/share/virtualenvs/hapi-ingest-scripts-PKYMczwv/lib/python3.10/site-packages/chaosmagpy/plot_utils.py", line 359, in <module>
    plt.register_cmap(cmap=nio_colormap())
AttributeError: module 'matplotlib.pyplot' has no attribute 'register_cmap'

Of course I could try to install an older matplotlib, but I would prefer not to need to have a specific matplotlib installed for my script, to avoid situations like this. Would it be possible to modify chaosmagpy so that matplotlib is only imported when functions that are using it are called?

ancklo commented 1 month ago

Hi Eelco,

Thank you for making me aware of this.

Unfortunately, Matplotlib is tightly integrated into Chaosmagpy, so I'm not sure that I can easily turn it into an optional dependency of Chaosmagpy. But I will check and let you know.

As a quick fix, I could replace register_cmap with matplotlib.cm.register, which the Matplotlib documentation recommends doing. This also seems to be compatible with earlier versions of Matplotlib.

ancklo commented 1 month ago

Hi,

I have just released a minor update of ChaosMagPy where Matplotlib is an optional dependency.

Upon importing ChaosMagPy, it will also try to import Matplotlib. If it is not available, a warning will be issued, but the rest should go through as usual.

Install directly from PyPI:

python -m pip install chaosmagpy==0.13.1

I hope it works as intended. Let me know if you have any issues. Regards