From 6c89921c8f4654ec17c2170c739daf2f1ec4d75b Mon Sep 17 00:00:00 2001 From: claraElk Date: Wed, 25 Jun 2025 11:00:43 -0400 Subject: [PATCH 1/5] Add regex_to_regressor --- halfpipe2bids/utils.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/halfpipe2bids/utils.py b/halfpipe2bids/utils.py index 3d9cd79..d40ed07 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/" @@ -44,6 +45,27 @@ def get_strategy_confounds(spec_path): return strategy_confounds +def regex_to_regressor(strategy_confounds, df_confounds): + # TODO: documentation + # TODO: To be merged with get_strategy_confounds + all_columns = df_confounds.columns.tolist() + strategy_confounds_regex = {} + + # Initialize an empty list to store the regressors + for strategies in strategy_confounds.keys(): + strategy_confounds_regex[strategies] = [] + + for regex_confounds in strategy_confounds[strategies]: + # Compile the regex pattern + pattern = re.compile(regex_confounds) + # Find all columns that match the regex + matching_columns = [ + col for col in all_columns if pattern.fullmatch(col) + ] + + strategy_confounds_regex[strategies].extend(matching_columns) + + return strategy_confounds_regex def impute_and_clean(df): # TODO: documentation and what's the imputation method? From 1514819375e8db5d559a9b0d6019825c1edb8048 Mon Sep 17 00:00:00 2001 From: claraElk Date: Wed, 25 Jun 2025 11:47:54 -0400 Subject: [PATCH 2/5] update regex_to_regressor --- halfpipe2bids/utils.py | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/halfpipe2bids/utils.py b/halfpipe2bids/utils.py index d40ed07..90f97a2 100644 --- a/halfpipe2bids/utils.py +++ b/halfpipe2bids/utils.py @@ -45,27 +45,20 @@ def get_strategy_confounds(spec_path): return strategy_confounds -def regex_to_regressor(strategy_confounds, df_confounds): - # TODO: documentation +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 fmriprep confound file. + Returns: + list: List of confound columns based on fmriprep confound file. + """ # TODO: To be merged with get_strategy_confounds - all_columns = df_confounds.columns.tolist() - strategy_confounds_regex = {} - - # Initialize an empty list to store the regressors - for strategies in strategy_confounds.keys(): - strategy_confounds_regex[strategies] = [] - - for regex_confounds in strategy_confounds[strategies]: - # Compile the regex pattern - pattern = re.compile(regex_confounds) - # Find all columns that match the regex - matching_columns = [ - col for col in all_columns if pattern.fullmatch(col) - ] - - strategy_confounds_regex[strategies].extend(matching_columns) - - return strategy_confounds_regex + + # 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? From 33b0a273ef852c50fc909b832ef2d921c05fbd93 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Jun 2025 17:00:22 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- halfpipe2bids/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/halfpipe2bids/utils.py b/halfpipe2bids/utils.py index 90f97a2..b7080bb 100644 --- a/halfpipe2bids/utils.py +++ b/halfpipe2bids/utils.py @@ -45,6 +45,7 @@ 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. @@ -57,9 +58,10 @@ def regex_to_regressor(regex_confounds, confounds_columns): # TODO: To be merged with get_strategy_confounds # Compile the regex pattern - pattern = re.compile('|'.join(regex_confounds)) + 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) From f54faa12b69d8fba5ea43f0994a651ba15a3269b Mon Sep 17 00:00:00 2001 From: claraElk Date: Wed, 2 Jul 2025 09:52:47 -0400 Subject: [PATCH 4/5] Remove some characters --- halfpipe2bids/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/halfpipe2bids/utils.py b/halfpipe2bids/utils.py index b7080bb..49dcf8a 100644 --- a/halfpipe2bids/utils.py +++ b/halfpipe2bids/utils.py @@ -51,7 +51,7 @@ 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 fmriprep confound file. + confounds_columns (list): List of column names from confound file. Returns: list: List of confound columns based on fmriprep confound file. """ From f0b7476f4e62343daad70dc8a59971faea3ab909 Mon Sep 17 00:00:00 2001 From: claraElk Date: Wed, 2 Jul 2025 11:27:39 -0400 Subject: [PATCH 5/5] Add test for regex_to_regressor --- halfpipe2bids/tests/test_utils.py | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 halfpipe2bids/tests/test_utils.py 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", + ]