|
55 | 55 | assert_array_equal) |
56 | 56 | import pytest |
57 | 57 |
|
58 | | -from nibabel.testing import nullcontext, assert_dt_equal, assert_allclose_safely, suppress_warnings |
| 58 | +from nibabel.testing import (assert_dt_equal, assert_allclose_safely, |
| 59 | + suppress_warnings, error_warnings) |
59 | 60 |
|
60 | 61 | pyzstd, HAVE_ZSTD, _ = optional_package("pyzstd") |
61 | 62 |
|
@@ -623,7 +624,7 @@ def test_a2f_bad_scaling(): |
623 | 624 | (0, np.nan, -np.inf, np.inf)): |
624 | 625 | arr = np.ones((2,), dtype=in_type) |
625 | 626 | fobj = BytesIO() |
626 | | - cm = nullcontext() |
| 627 | + cm = error_warnings() |
627 | 628 | if (np.issubdtype(in_type, np.complexfloating) and |
628 | 629 | not np.issubdtype(out_type, np.complexfloating)): |
629 | 630 | cm = pytest.warns(np.ComplexWarning) |
@@ -674,37 +675,37 @@ def test_a2f_nan2zero_range(): |
674 | 675 | warn_type = np.ComplexWarning if np.issubdtype(dt, np.complexfloating) else None |
675 | 676 | # No errors from explicit thresholding |
676 | 677 | # mn thresholding excluding zero |
677 | | - with pytest.warns(warn_type): |
| 678 | + with pytest.warns(warn_type) if warn_type else error_warnings(): |
678 | 679 | assert_array_equal([1, 1, 1, 0], |
679 | 680 | write_return(arr, fobj, np.int8, mn=1)) |
680 | 681 | # mx thresholding excluding zero |
681 | | - with pytest.warns(warn_type): |
| 682 | + with pytest.warns(warn_type) if warn_type else error_warnings(): |
682 | 683 | assert_array_equal([-1, -1, -1, 0], |
683 | 684 | write_return(arr, fobj, np.int8, mx=-1)) |
684 | 685 | # Errors from datatype threshold after scaling |
685 | | - with pytest.warns(warn_type): |
| 686 | + with pytest.warns(warn_type) if warn_type else error_warnings(): |
686 | 687 | back_arr = write_return(arr, fobj, np.int8, intercept=128) |
687 | 688 | assert_array_equal([-128, -128, -127, -128], back_arr) |
688 | 689 | with pytest.raises(ValueError): |
689 | 690 | write_return(arr, fobj, np.int8, intercept=129) |
690 | 691 | with pytest.raises(ValueError): |
691 | 692 | write_return(arr_no_nan, fobj, np.int8, intercept=129) |
692 | 693 | # OK with nan2zero false, but we get whatever nan casts to |
693 | | - with pytest.warns(warn_type): |
| 694 | + with pytest.warns(warn_type) if warn_type else error_warnings(): |
694 | 695 | nan_cast = np.array(np.nan, dtype=dt).astype(np.int8) |
695 | | - with pytest.warns(warn_type): |
| 696 | + with pytest.warns(warn_type) if warn_type else error_warnings(): |
696 | 697 | back_arr = write_return(arr, fobj, np.int8, intercept=129, nan2zero=False) |
697 | 698 | assert_array_equal([-128, -128, -128, nan_cast], back_arr) |
698 | 699 | # divslope |
699 | | - with pytest.warns(warn_type): |
| 700 | + with pytest.warns(warn_type) if warn_type else error_warnings(): |
700 | 701 | back_arr = write_return(arr, fobj, np.int8, intercept=256, divslope=2) |
701 | 702 | assert_array_equal([-128, -128, -128, -128], back_arr) |
702 | 703 | with pytest.raises(ValueError): |
703 | 704 | write_return(arr, fobj, np.int8, intercept=257.1, divslope=2) |
704 | 705 | with pytest.raises(ValueError): |
705 | 706 | write_return(arr_no_nan, fobj, np.int8, intercept=257.1, divslope=2) |
706 | 707 | # OK with nan2zero false |
707 | | - with pytest.warns(warn_type): |
| 708 | + with pytest.warns(warn_type) if warn_type else error_warnings(): |
708 | 709 | back_arr = write_return(arr, fobj, np.int8, |
709 | 710 | intercept=257.1, divslope=2, nan2zero=False) |
710 | 711 | assert_array_equal([-128, -128, -128, nan_cast], back_arr) |
|
0 commit comments