dsavransky / EXOSIMS

Simulator for exoplanet direct imaging space missions
BSD 3-Clause "New" or "Revised" License
25 stars 35 forks source link

astropy 4/5/6 compatibility issue in utils/photometricModels.py #373

Closed turmon closed 2 months ago

turmon commented 3 months ago

Describe the bug Import of photometricModels.py (from, e.g., Nemati.py) fails with astropy 6.0+ / synphot 1.4+

To Reproduce

The stack trace below summarizes the issue. It's on astropy 6.0.1, synphot 1.4.0, but I assume any astropy >= 6 will do.

The import line that causes the exception is verbatim from the OpticalSystem prototype.

$ python
Python 3.10.4 (main, Mar 31 2022, 08:41:55) [GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import astropy
>>> from EXOSIMS.util.photometricModels import Box1D
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/proj/exep/rhonda/Sandbox/Python-venvs/exosims-3.2.0-temp-2024-04-26/EXOSIMS/EXOSIMS/util/photometricModels.py", line 6, in <module>
    from synphot.compat import ASTROPY_LT_5_0
ImportError: cannot import name 'ASTROPY_LT_5_0' from 'synphot.compat' (/proj/exep/rhonda/Sandbox/Python-venvs/exosims-3.2.0-temp-2024-04-26/lib/python3.10/site-packages/synphot/compat.py)
>>> astropy.__version__
'6.0.1'

Expected behavior A successful import.

JSON script: Not needed, see above

EXOSIMS version: current master (3.2.0)

Additional context I believe ASTROPY_LT_5_0 is a compatibility variable to help with the astropy 4-to-5 transition, and it's no longer present in astropy 6.0+ / synphot 1.4+ (verified by inspecting changes in the synphot/compat.py source).

The photometricModels.py code is testing this variable to use the right method when it makes a box object.

The fix I used is to trap the import exception and to define ASTROPY_LT_5_0 = False if it occurs:

diff --git a/EXOSIMS/util/photometricModels.py b/EXOSIMS/util/photometricModels.py
index 15e99c51..d99f056f 100644
--- a/EXOSIMS/util/photometricModels.py
+++ b/EXOSIMS/util/photometricModels.py
@@ -3,7 +3,10 @@ Various useful photometric models from the literature
 """
 import astropy.units as u
 from synphot.models import Box1D as Box1D_orig
-from synphot.compat import ASTROPY_LT_5_0
+try:
+  from synphot.compat import ASTROPY_LT_5_0
+except:
+  ASTROPY_LT_5_0 = False
 from functools import partial
 import numpy as np
 import warnings

Another fix would be to just assume astropy >= 5.0, and remove the if within the box construction entirely.

dsavransky commented 3 months ago

Fix steps: 1) Remove our own version of Box1D (after verifying that current synphot provided Box1D works the same way) 2) Remove all associated imports 3) Set synphot minimum version to 1.3 and astropy minimum version to 5.0

dsavransky commented 2 months ago

Closed by #377