def _select_confounds(confounds_file, selected_confounds):
    """Process and return selected confounds from the confounds file

    Parameters
    ----------
    confounds_file : str
        File that contains all usable confounds
    selected_confounds : list
        List containing all desired confounds.
        confounds can be listed as regular expressions (e.g., "motion_outlier.*")

    Returns
    -------
    desired_confounds : DataFrame
        contains all desired (processed) confounds.
    """
    import pandas as pd
    import numpy as np
    import re
    confounds_df = pd.read_csv(confounds_file, sep='\t', na_values='n/a')
    confound_expr = re.compile('|'.join(selected_confounds))
    expanded_confounds = list(filter(confound_expr.fullmatch, confounds_df.
        columns))
    imputables = ('framewise_displacement', 'std_dvars', 'dvars',
        '.*derivative1.*')
    impute_expr = re.compile('|'.join(imputables))
    expanded_imputables = list(filter(impute_expr.fullmatch,
        expanded_confounds))
    for imputable in expanded_imputables:
        vals = confounds_df[imputable].values
        if not np.isnan(vals[0]):
            continue
        confounds_df[imputable][0] = np.nanmean(vals[vals != 0])
    desired_confounds = confounds_df[expanded_confounds]
    if desired_confounds.isna().values.any():
        msg = 'The selected confounds contain nans: {conf}'.format(conf=
            expanded_confounds)
        raise ValueError(msg)
    return desired_confounds
