22from contextlib import nullcontext
33from copy import deepcopy
44from functools import partial
5+ from importlib .util import find_spec
56from threading import Lock
67
78import numpy as np
1112import gpgi
1213from gpgi ._lib import _deposit_ngp_2D
1314
15+ HAVE_PYTEST_MPL = find_spec ("pytest_mpl" ) is not None
16+
1417
1518@pytest .fixture ()
1619def sample_2D_dataset ():
@@ -120,11 +123,13 @@ def fake_dep(*args, metadata=None, **kwargs):
120123@pytest .mark .parametrize ("method" , ["ngp" , "cic" , "tsc" ])
121124@pytest .mark .mpl_image_compare
122125def test_2D_deposit (sample_2D_dataset , method ):
123- from matplotlib .figure import Figure
124-
125126 ds = sample_2D_dataset
126127 particle_density = ds .deposit ("mass" , method = method )
127128
129+ if not HAVE_PYTEST_MPL :
130+ pytest .skip (reason = "pytest-mpl is not available" )
131+ from matplotlib .figure import Figure
132+
128133 fig = Figure ()
129134 ax = fig .add_subplot ()
130135
@@ -153,8 +158,6 @@ def test_2D_deposit(sample_2D_dataset, method):
153158@pytest .mark .parametrize ("grid_type" , ["linear" , "geometric" ])
154159@pytest .mark .mpl_image_compare
155160def test_1D_deposit (method , grid_type ):
156- from matplotlib .figure import Figure
157-
158161 if grid_type == "linear" :
159162 xedges = np .linspace (1 , 2 , 6 )
160163 elif grid_type == "geometric" :
@@ -178,6 +181,11 @@ def test_1D_deposit(method, grid_type):
178181 if method == "ngp" :
179182 assert mass .sum () == ds .particles .count
180183
184+ if not HAVE_PYTEST_MPL :
185+ pytest .skip (reason = "pytest-mpl is not available" )
186+
187+ from matplotlib .figure import Figure
188+
181189 fig = Figure ()
182190 ax = fig .add_subplot ()
183191 ax .set (xlabel = "x" , ylabel = "particle mass" , title = f"Deposition method '{ method } '" )
@@ -243,8 +251,6 @@ def test_3D_deposit(method, dtype):
243251
244252@pytest .mark .mpl_image_compare
245253def test_readme_example ():
246- from matplotlib .figure import Figure
247-
248254 nx = ny = 64
249255 nparticles = 600_000
250256
@@ -271,6 +277,10 @@ def test_readme_example():
271277
272278 particle_mass = ds .deposit ("mass" , method = "nearest_grid_point" )
273279
280+ if not HAVE_PYTEST_MPL :
281+ pytest .skip (reason = "pytest-mpl is not available" )
282+ from matplotlib .figure import Figure
283+
274284 fig = Figure ()
275285 ax = fig .add_subplot ()
276286 ax .set (aspect = 1 , xlabel = "x" , ylabel = "y" )
0 commit comments