|
1 | 1 | """Tests for CBMR meta-analytic methods."""
|
2 | 2 | import logging
|
| 3 | +import warnings |
3 | 4 |
|
4 | 5 | import pytest
|
5 |
| -import torch |
| 6 | + |
| 7 | +try: |
| 8 | + import torch |
| 9 | +except ImportError: |
| 10 | + warnings.warn("Torch not installed. CBMR tests will be skipped.") |
| 11 | + TORCH_INSTALLED = False |
| 12 | +else: |
| 13 | + TORCH_INSTALLED = True |
| 14 | + from nimare.meta import models |
| 15 | + from nimare.meta.cbmr import CBMREstimator, CBMRInference |
6 | 16 |
|
7 | 17 | import nimare
|
8 | 18 | from nimare.correct import FDRCorrector, FWECorrector
|
9 |
| -from nimare.meta import models |
10 |
| -from nimare.meta.cbmr import CBMREstimator, CBMRInference |
11 | 19 | from nimare.transforms import StandardizeField
|
12 | 20 |
|
13 | 21 | # numba has a lot of debug messages that are not useful for testing
|
|
16 | 24 | logging.getLogger("indexed_gzip").setLevel(logging.WARNING)
|
17 | 25 |
|
18 | 26 |
|
19 |
| -@pytest.fixture( |
20 |
| - scope="session", |
21 |
| - params=[ |
22 |
| - pytest.param(models.PoissonEstimator, id="Poisson"), |
23 |
| - pytest.param(models.NegativeBinomialEstimator, id="NegativeBinomial"), |
24 |
| - pytest.param(models.ClusteredNegativeBinomialEstimator, id="ClusteredNegativeBinomial"), |
25 |
| - ], |
26 |
| -) |
27 |
| -def model(request): |
28 |
| - """CBMR models.""" |
29 |
| - return request.param |
| 27 | +if TORCH_INSTALLED: |
| 28 | + |
| 29 | + @pytest.fixture( |
| 30 | + scope="session", |
| 31 | + params=[ |
| 32 | + pytest.param(models.PoissonEstimator, id="Poisson"), |
| 33 | + pytest.param(models.NegativeBinomialEstimator, id="NegativeBinomial"), |
| 34 | + pytest.param( |
| 35 | + models.ClusteredNegativeBinomialEstimator, id="ClusteredNegativeBinomial" |
| 36 | + ), |
| 37 | + ], |
| 38 | + ) |
| 39 | + def model(request): |
| 40 | + """CBMR models.""" |
| 41 | + return request.param |
| 42 | + |
| 43 | +else: |
| 44 | + model = None |
30 | 45 |
|
31 | 46 |
|
32 | 47 | @pytest.fixture(scope="session")
|
@@ -243,3 +258,37 @@ def test_StandardizeField(testdata_cbmr_simulated):
|
243 | 258 | assert dset.annotations["standardized_sample_sizes"].std() == pytest.approx(1.0, abs=1e-3)
|
244 | 259 | assert dset.annotations["standardized_avg_age"].mean() == pytest.approx(0.0, abs=1e-3)
|
245 | 260 | assert dset.annotations["standardized_avg_age"].std() == pytest.approx(1.0, abs=1e-3)
|
| 261 | + |
| 262 | + |
| 263 | +@pytest.mark.cbmr_importerror |
| 264 | +def test_cbmr_importerror(): |
| 265 | + """Test that ImportErrors are raised when torch is not installed.""" |
| 266 | + with pytest.raises(ImportError): |
| 267 | + from nimare.meta.cbmr import CBMREstimator |
| 268 | + |
| 269 | + CBMREstimator() |
| 270 | + |
| 271 | + with pytest.raises(ImportError): |
| 272 | + from nimare.meta.cbmr import CBMRInference |
| 273 | + |
| 274 | + CBMRInference() |
| 275 | + |
| 276 | + with pytest.raises(ImportError): |
| 277 | + from nimare.meta.models import GeneralLinearModelEstimator |
| 278 | + |
| 279 | + GeneralLinearModelEstimator() |
| 280 | + |
| 281 | + with pytest.raises(ImportError): |
| 282 | + from nimare.meta.models import PoissonEstimator |
| 283 | + |
| 284 | + PoissonEstimator() |
| 285 | + |
| 286 | + with pytest.raises(ImportError): |
| 287 | + from nimare.meta.models import NegativeBinomialEstimator |
| 288 | + |
| 289 | + NegativeBinomialEstimator() |
| 290 | + |
| 291 | + with pytest.raises(ImportError): |
| 292 | + from nimare.meta.models import ClusteredNegativeBinomialEstimator |
| 293 | + |
| 294 | + ClusteredNegativeBinomialEstimator() |
0 commit comments