Coverage for nilearn/_utils/tests/test_niimg.py: 0%

49 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-20 10:58 +0200

1from pathlib import Path 

2 

3import joblib 

4import numpy as np 

5import pytest 

6from nibabel import Nifti1Header, Nifti1Image, load 

7 

8from nilearn._utils import load_niimg, niimg, testing 

9from nilearn.image import get_data, new_img_like 

10 

11 

12@pytest.fixture 

13def img1(affine_eye): 

14 data = np.ones((2, 2, 2, 2)) 

15 return Nifti1Image(data, affine=affine_eye) 

16 

17 

18def test_new_img_like_side_effect(img1): 

19 hash1 = joblib.hash(img1) 

20 new_img_like( 

21 img1, np.ones((2, 2, 2, 2)), img1.affine.copy(), copy_header=True 

22 ) 

23 hash2 = joblib.hash(img1) 

24 assert hash1 == hash2 

25 

26 

27@pytest.mark.parametrize("no_int64_nifti", ["allow for this test"]) 

28def test_get_target_dtype(affine_eye): 

29 img = Nifti1Image(np.ones((2, 2, 2), dtype=np.float64), affine=affine_eye) 

30 assert get_data(img).dtype.kind == "f" 

31 dtype_kind_float = niimg._get_target_dtype( 

32 get_data(img).dtype, target_dtype="auto" 

33 ) 

34 assert dtype_kind_float == np.float32 

35 # Passing dtype or header is required when using int64 

36 # https://nipy.org/nibabel/changelog.html#api-changes-and-deprecations 

37 hdr = Nifti1Header() 

38 hdr.set_data_dtype(np.int64) 

39 data = np.ones((2, 2, 2), dtype=np.int64) 

40 img2 = Nifti1Image(data, affine=affine_eye, header=hdr) 

41 assert get_data(img2).dtype.kind == img2.get_data_dtype().kind == "i" 

42 dtype_kind_int = niimg._get_target_dtype( 

43 get_data(img2).dtype, target_dtype="auto" 

44 ) 

45 assert dtype_kind_int == np.int32 

46 

47 

48@pytest.mark.parametrize("no_int64_nifti", ["allow for this test"]) 

49def test_img_data_dtype(rng, affine_eye, tmp_path): 

50 # Ignoring complex, binary, 128+ bit, RGBA 

51 nifti1_dtypes = ( 

52 np.uint8, 

53 np.uint16, 

54 np.uint32, 

55 np.uint64, 

56 np.int8, 

57 np.int16, 

58 np.int32, 

59 np.float32, 

60 np.float64, 

61 ) 

62 dtype_matches = [] 

63 # Passing dtype or header is required when using int64 

64 # https://nipy.org/nibabel/changelog.html#api-changes-and-deprecations 

65 hdr = Nifti1Header() 

66 for logical_dtype in nifti1_dtypes: 

67 dataobj = rng.uniform(0, 255, (2, 2, 2)).astype(logical_dtype) 

68 for on_disk_dtype in nifti1_dtypes: 

69 hdr.set_data_dtype(on_disk_dtype) 

70 img = Nifti1Image(dataobj, affine_eye, header=hdr) 

71 img.to_filename(tmp_path / "test.nii") 

72 loaded = load(tmp_path / "test.nii") 

73 # To verify later that sometimes these differ meaningfully 

74 dtype_matches.append( 

75 loaded.get_data_dtype() == niimg.img_data_dtype(loaded) 

76 ) 

77 assert np.array(loaded.dataobj).dtype == niimg.img_data_dtype( 

78 loaded 

79 ) 

80 # Verify that the distinction is worth making 

81 assert any(dtype_matches) 

82 assert not all(dtype_matches) 

83 

84 

85def test_load_niimg(img1, tmp_path): 

86 filename = testing.write_imgs_to_path( 

87 img1, file_path=tmp_path, create_files=True 

88 ) 

89 filename = Path(filename) 

90 load_niimg(filename)