Coverage for nilearn/plotting/displays/_utils.py: 0%

21 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-16 12:32 +0200

1def get_create_display_fun(display_mode, class_dict): 

2 """Help for functions \ 

3 :func:`~nilearn.plotting.displays.get_slicer` and \ 

4 :func:`~nilearn.plotting.displays.get_projector`. 

5 """ 

6 try: 

7 return class_dict[display_mode].init_with_figure 

8 except KeyError: 

9 message = ( 

10 f"{display_mode} is not a valid display_mode. " 

11 f"Valid options are {sorted(class_dict.keys())}" 

12 ) 

13 raise ValueError(message) 

14 

15 

16def _get_index_from_direction(direction): 

17 """Return numerical index from direction.""" 

18 directions = ["x", "y", "z"] 

19 try: 

20 # l and r are subcases of x 

21 index = 0 if direction in "lr" else directions.index(direction) 

22 except ValueError: 

23 message = ( 

24 f"{direction} is not a valid direction. " 

25 "Allowed values are 'l', 'r', 'x', 'y' and 'z'" 

26 ) 

27 raise ValueError(message) 

28 return index 

29 

30 

31def coords_3d_to_2d(coords_3d, direction, return_direction=False): 

32 """Project 3d coordinates into 2d ones given the direction of a cut.""" 

33 index = _get_index_from_direction(direction) 

34 dimensions = [0, 1, 2] 

35 dimensions.pop(index) 

36 if return_direction: 

37 return coords_3d[:, dimensions], coords_3d[:, index] 

38 return coords_3d[:, dimensions]