From cd08ca80b2ed14a8e66d1f2e3bce1d322b6f3467 Mon Sep 17 00:00:00 2001 From: MARCHAND MANON Date: Tue, 15 Oct 2024 09:26:33 +0200 Subject: [PATCH] fix: add matplotlib as optional dependency for skymaps plots --- CHANGELOG.md | 4 ++++ pyproject.toml | 3 +++ python/cdshealpix/skymap/skymap.py | 11 ++++++++++- requirements-dev.txt | 3 ++- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 367a5db..57c0f39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,10 @@ * `nested.healpix_to_lonlat`, failed into rust panic for `dx=1` or `dy=1`. This is now indicated in the documentation and is catched in a `ValueError` on the python side. +### Changed + +* `matplotlib` is now an optional dependency, to plot previews of skymaps. + ## 0.6.5 Released 2023-11-28 diff --git a/pyproject.toml b/pyproject.toml index 18aff3a..8adaf29 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,9 @@ description = "A healpix manipulation library." readme = "README.md" keywords = ["astronomy", "healpix"] +[project.optional-dependencies] +plot = ["matplotlib"] + [project.urls] repository = "https://github.com/cds-astro/cds-healpix-python" documentation = "https://cds-astro.github.io/cds-healpix-python/" diff --git a/python/cdshealpix/skymap/skymap.py b/python/cdshealpix/skymap/skymap.py index 9fb4861..642cbf8 100644 --- a/python/cdshealpix/skymap/skymap.py +++ b/python/cdshealpix/skymap/skymap.py @@ -10,7 +10,10 @@ from pathlib import Path from typing import Union -import matplotlib.pyplot as plt +try: + import matplotlib.pyplot as plt +except ImportError: + _matplotlib_missing = True import numpy as np @@ -140,6 +143,12 @@ def quick_plot(self, *, size=256, convert_to_gal=True, path=None): If different from none, the image will not only be displayed, but also saved at the given location. By default None """ + if _matplotlib_missing: + raise ModuleNotFoundError( + "matplotlib is mandatory to use 'quick_plot'. " + "See https://matplotlib.org/ for installation " + "instructions." + ) img = cdshealpix.pixels_skymap(self.values, size, convert_to_gal) fig = plt.imshow(img) plt.axis("off") diff --git a/requirements-dev.txt b/requirements-dev.txt index 0ece12f..f8da8a5 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,6 @@ astropy<5.3; python_version == '3.8' astropy; python_version > '3.8' +astropy_healpix +matplotlib pre-commit==2.21.* pytest -astropy_healpix \ No newline at end of file