Coverage for nilearn/interfaces/fsl.py: 31%

12 statements  

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

1"""Functions for working with the FSL library.""" 

2 

3from pathlib import Path 

4 

5import numpy as np 

6import pandas as pd 

7 

8__all__ = ["get_design_from_fslmat"] 

9 

10 

11def get_design_from_fslmat(fsl_design_matrix_path, column_names=None): 

12 """Extract design matrix dataframe from FSL mat file. 

13 

14 Parameters 

15 ---------- 

16 fsl_design_matrix_path : :obj:`str` 

17 Path to the FSL design matrix file. 

18 column_names : None or :obj:`list` of :obj:`str`, default=None 

19 The names of the columns in the design matrix. 

20 

21 Returns 

22 ------- 

23 design_matrix : :obj:`pandas.DataFrame` 

24 A DataFrame containing the design matrix. 

25 """ 

26 with Path(fsl_design_matrix_path).open() as design_matrix_file: 

27 # Based on the openneuro example this seems to be the right 

28 # marker to start extracting the matrix until the end of the file 

29 # Conventions of FSL mat files should be verified in more detail for 

30 # a general case 

31 for line in design_matrix_file: 

32 if "/Matrix" in line: 

33 break 

34 

35 design_matrix = np.array( 

36 [ 

37 [float(val) for val in line.replace("\t\n", "").split("\t")] 

38 for line in design_matrix_file 

39 ] 

40 ) 

41 design_matrix = pd.DataFrame(design_matrix, columns=column_names) 

42 

43 return design_matrix