diff --git a/halfpipe2bids/tests/test_utils.py b/halfpipe2bids/tests/test_utils.py new file mode 100644 index 0000000..957b33f --- /dev/null +++ b/halfpipe2bids/tests/test_utils.py @@ -0,0 +1,34 @@ +from halfpipe2bids.utils import regex_to_regressor + + +def test_regex_to_regressor(): + regex_confounds = [ + "c_comp_cor_0[0-4]", + "(trans|rot)_[xyz]", + "global_signal", + "motion_outlier[0-9]+", + ] + confounds_columns = [ + "c_comp_cor_00", + "c_comp_cor_01", + "a_comp_cor_00", + "global_signal", + "global_signal_derivative1", + "white_matter", + "trans_x", + "trans_x_derivative1", + "rot_y", + "rot_x_derivative1", + "motion_outlier1", + "motion_outlier2", + ] + matched = regex_to_regressor(regex_confounds, confounds_columns) + assert matched == [ + "c_comp_cor_00", + "c_comp_cor_01", + "global_signal", + "trans_x", + "rot_y", + "motion_outlier1", + "motion_outlier2", + ] diff --git a/halfpipe2bids/utils.py b/halfpipe2bids/utils.py index 3d9cd79..49dcf8a 100644 --- a/halfpipe2bids/utils.py +++ b/halfpipe2bids/utils.py @@ -4,6 +4,7 @@ import logging from nilearn.signal import clean from nilearn import plotting +import re hp2b_log = logging.getLogger("halfpipe2bids") hp2b_url = "https://github.com/pbergeret12/HalfPipe2Bids/" @@ -45,6 +46,22 @@ def get_strategy_confounds(spec_path): return strategy_confounds +def regex_to_regressor(regex_confounds, confounds_columns): + """ + Convert the list of regex patterns from HALFpipe to a list of regressors. + Args: + regex_confounds (list): List of regex patterns. + confounds_columns (list): List of column names from confound file. + Returns: + list: List of confound columns based on fmriprep confound file. + """ + # TODO: To be merged with get_strategy_confounds + + # Compile the regex pattern + pattern = re.compile("|".join(regex_confounds)) + return [col for col in confounds_columns if pattern.fullmatch(col)] + + def impute_and_clean(df): # TODO: documentation and what's the imputation method? row_means = df.mean(axis=1, skipna=True)