teaspoon.DAF.data_assimilation fails to import on default install because of unguarded TensorFlow import
After a plain pip install teaspoon, importing anything from teaspoon.DAF.data_assimilation fails, including rafda, which does not use TensorFlow.
Reproduction
python -m venv .test
source .test/bin/activate
pip install teaspoon
python -c "from teaspoon.DAF.data_assimilation import TADA"
Output
File ".../teaspoon/DAF/data_assimilation.py", line 3, in <module>
from gudhi.tensorflow import RipsLayer
File ".../gudhi/tensorflow/__init__.py", line 1, in <module>
from .cubical_layer import CubicalLayer
File ".../gudhi/tensorflow/cubical_layer.py", line 14, in <module>
import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'
Root cause
data_assimilation.py has two TensorFlow-related imports. The second one (lines 9-13) is correctly guarded:
try:
import tensorflow as tf
from tensorflow import keras
except:
raise ImportError("TADA Requires tensorflow for optimization")
However, the first import on line 3 is unguarded and executes before the try/except block:
from gudhi.tensorflow import RipsLayer
gudhi.tensorflow internally imports TensorFlow, so it has the same dependency requirement. Because of this, the module crashes before reaching the guarded import block.
Impact
TADA is unusable on a default install.
rafda is also unusable even though it does not require TensorFlow, because the module-level import failure prevents importing anything from data_assimilation.
Suggested fix
Move the RipsLayer import inside the existing try/except block:
try:
import tensorflow as tf
from tensorflow import keras
from gudhi.tensorflow import RipsLayer
except ImportError:
raise ImportError(
"TADA requires tensorflow. "
"Install with: pip install 'teaspoon[full]'"
)
Environment
teaspoon==1.5.30
- Python 3.10.12
- Fresh virtual environment with no TensorFlow installed
teaspoon.DAF.data_assimilationfails to import on default install because of unguarded TensorFlow importAfter a plain
pip install teaspoon, importing anything fromteaspoon.DAF.data_assimilationfails, includingrafda, which does not use TensorFlow.Reproduction
Output
Root cause
data_assimilation.pyhas two TensorFlow-related imports. The second one (lines 9-13) is correctly guarded:However, the first import on line 3 is unguarded and executes before the
try/exceptblock:gudhi.tensorflowinternally imports TensorFlow, so it has the same dependency requirement. Because of this, the module crashes before reaching the guarded import block.Impact
TADAis unusable on a default install.rafdais also unusable even though it does not require TensorFlow, because the module-level import failure prevents importing anything fromdata_assimilation.Suggested fix
Move the
RipsLayerimport inside the existingtry/exceptblock:Environment
teaspoon==1.5.30