Skip to content

Commit 8fbdd81

Browse files
committed
Determine scipy floating_mean support lazily from the imported module via minversion instead of an import-time metadata probe
1 parent 9014aa3 commit 8fbdd81

4 files changed

Lines changed: 21 additions & 21 deletions

File tree

‎astropy/timeseries/periodograms/lombscargle/implementations/main.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
from .cython_impl import lombscargle_cython
1616
from .fast_impl import lombscargle_fast
1717
from .fastchi2_impl import lombscargle_fastchi2
18-
from .scipy_impl import lombscargle_scipy
18+
from .scipy_impl import _scipy_supports_floating_mean, lombscargle_scipy
1919
from .slow_impl import lombscargle_slow
20-
from .utils import SCIPY_LT_1_15
2120

2221
METHODS = {
2322
"slow": lombscargle_slow,
@@ -93,7 +92,7 @@ def validate_method( # ruff: ignore[PLR0917]
9392
"scipy" in methods
9493
and dy is None
9594
and method_kwds is None
96-
and not (fit_mean and SCIPY_LT_1_15)
95+
and (not fit_mean or _scipy_supports_floating_mean())
9796
)
9897

9998
# automatically choose the appropriate method

‎astropy/timeseries/periodograms/lombscargle/implementations/scipy_impl.py‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1+
from functools import cache
2+
13
import numpy as np
24

35
from astropy.timeseries.periodograms.lombscargle.utils import (
46
compute_chi2_ref,
57
convert_normalization,
68
)
9+
from astropy.utils import minversion
710
from astropy.utils.compat.optional_deps import HAS_SCIPY
811

9-
from .utils import SCIPY_LT_1_15
12+
13+
@cache
14+
def _scipy_supports_floating_mean():
15+
# Deferred so that scipy is only imported and its version only probed when
16+
# the answer is needed; passing the module object lets minversion fall
17+
# back to scipy.__version__ when the distribution metadata is missing
18+
# (e.g. in some frozen applications).
19+
import scipy
20+
21+
return minversion(scipy, "1.15.0.dev0")
1022

1123

1224
def lombscargle_scipy(
@@ -54,6 +66,9 @@ def lombscargle_scipy(
5466

5567
from scipy import signal
5668

69+
if fit_mean and not _scipy_supports_floating_mean():
70+
raise ValueError("fit_mean=True requires scipy 1.15 or later")
71+
5772
t, y = np.broadcast_arrays(t, y)
5873

5974
# Scipy requires floating-point input
@@ -69,9 +84,6 @@ def lombscargle_scipy(
6984
if center_data:
7085
y = y - y.mean()
7186

72-
if fit_mean and SCIPY_LT_1_15:
73-
raise ValueError("fit_mean=True requires scipy 1.15 or later")
74-
7587
kwargs = {"floating_mean": True} if fit_mean else {}
7688

7789
# Note: scipy `freqs` input is in angular frequencies

‎astropy/timeseries/periodograms/lombscargle/implementations/utils.py‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
1-
from importlib.metadata import PackageNotFoundError, version
21
from math import factorial
32

43
import numpy as np
5-
from packaging.version import Version
6-
7-
# The distribution metadata gives the scipy version without the cost of
8-
# importing scipy itself; if scipy is installed without metadata (e.g. in
9-
# some frozen applications) assume it is recent, and if it is not installed
10-
# at all this value is never consulted since HAS_SCIPY gates all uses.
11-
try:
12-
SCIPY_LT_1_15 = Version(version("scipy")) < Version("1.15.0.dev0")
13-
except PackageNotFoundError:
14-
SCIPY_LT_1_15 = False
154

165

176
def bitceil(N):

‎astropy/timeseries/periodograms/lombscargle/tests/test_lombscargle.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
from astropy.timeseries.periodograms.lombscargle._testing import (
1010
assert_not_strictly_equal,
1111
)
12-
from astropy.timeseries.periodograms.lombscargle.implementations.utils import (
13-
SCIPY_LT_1_15,
12+
from astropy.timeseries.periodograms.lombscargle.implementations.scipy_impl import (
13+
_scipy_supports_floating_mean,
1414
)
1515

1616
ALL_METHODS = LombScargle.available_methods
@@ -74,7 +74,7 @@ def test_all_methods(
7474
data, method, center_data, fit_mean, errors, with_units, normalization
7575
):
7676
if method == "scipy":
77-
if fit_mean and SCIPY_LT_1_15:
77+
if fit_mean and not _scipy_supports_floating_mean():
7878
pytest.skip("SciPy 1.15+ required for using `fit_mean=True`")
7979
if errors == "full" or (errors == "partial" and normalization == "psd"):
8080
pytest.skip("scipy method only supports uniform uncertainties dy")

0 commit comments

Comments
 (0)