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

25 statements  

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

1"""Test for _utils.bids module.""" 

2 

3import numpy as np 

4import pytest 

5 

6from nilearn._utils import data_gen 

7from nilearn._utils.bids import ( 

8 check_look_up_table, 

9 generate_atlas_look_up_table, 

10) 

11 

12 

13def test_generate_atlas_look_up_table(shape_3d_default, surf_three_labels_img): 

14 """Check generation of LUT directly from niimg or surface image.""" 

15 mock_regions = data_gen.generate_labeled_regions( 

16 shape_3d_default, n_regions=10 

17 ) 

18 lut = generate_atlas_look_up_table(function="unknown", index=mock_regions) 

19 check_look_up_table(lut=lut, atlas=mock_regions, strict=True) 

20 

21 lut = generate_atlas_look_up_table( 

22 function="unknown", index=surf_three_labels_img 

23 ) 

24 check_look_up_table(lut=lut, atlas=surf_three_labels_img, strict=True) 

25 

26 

27def test_generate_atlas_look_up_table_errors(): 

28 with pytest.raises( 

29 ValueError, match="'index' and 'name' cannot both be None." 

30 ): 

31 generate_atlas_look_up_table(function=None, name=None, index=None) 

32 

33 with pytest.raises( 

34 TypeError, 

35 match="must be one of: Niimg-Like, SurfaceIamge, numpy array.", 

36 ): 

37 generate_atlas_look_up_table(function=None, name=None, index=[1, 2, 3]) 

38 

39 with pytest.raises(ValueError, match="have different lengths"): 

40 generate_atlas_look_up_table( 

41 function=None, 

42 name=["a", "b"], 

43 index=np.array([1, 2, 3]), 

44 strict=True, 

45 ) 

46 

47 

48def test_check_look_up_table_errors(shape_3d_default): 

49 mock_regions = data_gen.generate_labeled_regions( 

50 shape_3d_default, n_regions=10 

51 ) 

52 lut = generate_atlas_look_up_table(function="unknown", index=mock_regions) 

53 

54 with pytest.raises( 

55 ValueError, match="missing from the atlas look-up table" 

56 ): 

57 check_look_up_table( 

58 lut=lut.drop(index=2), atlas=mock_regions, strict=True 

59 ) 

60 

61 mock_regions_with_missing_labels = data_gen.generate_labeled_regions( 

62 shape_3d_default, n_regions=8 

63 ) 

64 with pytest.raises(ValueError, match="missing from the atlas image"): 

65 check_look_up_table( 

66 lut=lut, atlas=mock_regions_with_missing_labels, strict=True 

67 )