aroma/utils.py
def runICA(fsl_dir, in_file, out_dir, mel_dir_in, mask, dim, TR):
    """Run MELODIC and merge the thresholded ICs into a single 4D nifti file.

    Parameters
    ----------
    fsl_dir : str
        Full path of the bin-directory of FSL
    in_file : str
        Full path to the fMRI data file (nii.gz) on which MELODIC
        should be run
    out_dir : str
        Full path of the output directory
    mel_dir_in : str or None
        Full path of the MELODIC directory in case it has been run
        before, otherwise None.
    mask : str
        Full path of the mask to be applied during MELODIC
    dim : int
        Dimensionality of ICA
    TR : float
        TR (in seconds) of the fMRI data

    Output
    ------
    melodic.ica/: MELODIC directory
    melodic_IC_thr.nii.gz: Merged file containing the mixture modeling
                           thresholded Z-statistical maps located in
                           melodic.ica/stats/
    """
    # Define the 'new' MELODIC directory and predefine some associated files
    mel_dir = op.join(out_dir, "melodic.ica")
    mel_IC = op.join(mel_dir, "melodic_IC.nii.gz")
    mel_IC_mix = op.join(mel_dir, "melodic_mix")
    mel_IC_thr = op.join(out_dir, "melodic_IC_thr.nii.gz")

    # When a MELODIC directory is specified,
    # check whether all needed files are present.
    # Otherwise... run MELODIC again
    if (
        mel_dir_in
        and op.isfile(op.join(mel_dir_in, "melodic_IC.nii.gz"))
        and op.isfile(op.join(mel_dir_in, "melodic_FTmix"))
        and op.isfile(op.join(mel_dir_in, "melodic_mix"))
    ):
        LGR.info('  - The existing/specified MELODIC directory will be used.')

        # If a 'stats' directory is present (contains thresholded spatial maps)
        # create a symbolic link to the MELODIC directory.
        # Otherwise create specific links and
        # run mixture modeling to obtain thresholded maps.
        if op.isdir(op.join(mel_dir_in, "stats")):
            os.symlink(mel_dir_in, mel_dir)
        else:
            LGR.warning("  - The MELODIC directory does not contain the "
                        "required 'stats' folder. Mixture modeling on the "
                        "Z-statistical maps will be run.")

            # Create symbolic links to the items in the specified melodic
            # directory
            os.makedirs(mel_dir)
            for item in os.listdir(mel_dir_in):
                os.symlink(op.join(mel_dir_in, item), op.join(mel_dir, item))

            # Run mixture modeling
            melodic_command = ("{0} --in={1} --ICs={1} --mix={2} "
                               "--out_dir={3} --0stats --mmthresh=0.5").format(
                                    op.join(fsl_dir, 'melodic'),
                                    mel_IC,
                                    mel_IC_mix,
                                    mel_dir,
                               )
            os.system(melodic_command)
    else:
        # If a melodic directory was specified, display that it did not
        # contain all files needed for ICA-AROMA (or that the directory
        # does not exist at all)
        if mel_dir_in:
            if not op.isdir(mel_dir_in):
                LGR.warning('  - The specified MELODIC directory does not '
                            'exist. MELODIC will be run separately.')
            else:
                LGR.warning('  - The specified MELODIC directory does not '
                            'contain the required files to run ICA-AROMA. '
                            'MELODIC will be run separately.')

        # Run MELODIC
        melodic_command = (
            "{0} --in={1} --outdir={2} --mask={3} --dim={4} "
            "--Ostats --nobet --mmthresh=0.5 --report "
            "--tr={5}"
        ).format(op.join(fsl_dir, "melodic"), in_file, mel_dir, mask, dim, TR)
        os.system(melodic_command)

    # Get number of components
    mel_IC_img = nib.load(mel_IC)
    nr_ICs = mel_IC_img.shape[3]

    # Merge mixture modeled thresholded spatial maps. Note! In case that
    # mixture modeling did not converge, the file will contain two spatial
    # maps. The latter being the results from a simple null hypothesis test.
    # In that case, this map will have to be used (first one will be empty).
    zstat_imgs = []
    for i in range(1, nr_ICs + 1):
        # Define thresholded zstat-map file
        z_temp = op.join(mel_dir, "stats", "thresh_zstat{0}.nii.gz".format(i))

        # Get number of volumes in component's thresholded image
        z_temp_img = nib.load(z_temp)
        if z_temp_img.ndim == 4:
            len_IC = z_temp_img.shape[3]
            # Extract last spatial map within the thresh_zstat file
            zstat_img = image.index_img(z_temp_img, len_IC - 1)
        else:
            zstat_img = z_temp_img

        zstat_imgs.append(zstat_img)

    # Merge to 4D
    zstat_4d_img = image.concat_imgs(zstat_imgs)

    # Apply the mask to the merged image (in case a melodic-directory was
    # predefined and run with a different mask)
    zstat_4d_img = image.math_img(
        "stat * mask[:, :, :, None]", stat=zstat_4d_img, mask=mask
    )
    zstat_4d_img.to_filename(mel_IC_thr)
Run MELODIC and merge the thresholded ICs into a single 4D nifti file.

    Parameters
    ----------
    fsl_dir : str
        Full path of the bin-directory of FSL
    in_file : str
        Full path to the fMRI data file (nii.gz) on which MELODIC
        should be run
    out_dir : str
        Full path of the output directory
    mel_dir_in : str or None
        Full path of the MELODIC directory in case it has been run
        before, otherwise None.
    mask : str
        Full path of the mask to be applied during MELODIC
    dim : int
        Dimensionality of ICA
    TR : float
        TR (in seconds) of the fMRI data

    Output
    ------
    melodic.ica/: MELODIC directory
    melodic_IC_thr.nii.gz: Merged file containing the mixture modeling
                           thresholded Z-statistical maps located in
                           melodic.ica/stats/
    
Run MELODIC and merge the thresholded ICs into a single 4D nifti file.
Parameters

fsl_dir : str
Full path of the bin-directory of FSL
in_file : str
Full path to the fMRI data file (nii.gz) on which MELODIC
should be run
out_dir : str
Full path of the output directory
mel_dir_in : str or None
Full path of the MELODIC directory in case it has been run
before, otherwise None.
mask : str
Full path of the mask to be applied during MELODIC
dim : int
Dimensionality of ICA
TR : float
TR (in seconds) of the fMRI data

Output

melodic.ica/: MELODIC directory
melodic_IC_thr.nii.gz: Merged file containing the mixture modeling
thresholded Z-statistical maps located in
melodic.ica/stats
Run MELODIC and merge the thresholded ICs into a single 4D nifti file . Parameters fsl_dir : str Full path of the bin - directory of FSL in_file : str Full path to the fMRI data file ( nii . gz ) on which MELODIC should be run out_dir : str Full path of the output directory mel_dir_in : str or None Full path of the MELODIC directory in case it has been run before otherwise None . mask : str Full path of the mask to be applied during MELODIC dim : int Dimensionality of ICA TR : float TR ( in seconds ) of the fMRI data Output melodic . ica / : MELODIC directory melodic_IC_thr . nii . gz : Merged file containing the mixture modeling thresholded Z - statistical maps located in melodic . ica / stats
def runICA(fsl_dir, in_file, out_dir, mel_dir_in, mask, dim, TR):
    mel_dir = op.join(out_dir, "melodic.ica")
    mel_IC = op.join(mel_dir, "melodic_IC.nii.gz")
    mel_IC_mix = op.join(mel_dir, "melodic_mix")
    mel_IC_thr = op.join(out_dir, "melodic_IC_thr.nii.gz")
    if (
        mel_dir_in
        and op.isfile(op.join(mel_dir_in, "melodic_IC.nii.gz"))
        and op.isfile(op.join(mel_dir_in, "melodic_FTmix"))
        and op.isfile(op.join(mel_dir_in, "melodic_mix"))
    ):
        LGR.info('  - The existing/specified MELODIC directory will be used.')
        if op.isdir(op.join(mel_dir_in, "stats")):
            os.symlink(mel_dir_in, mel_dir)
        else:
            LGR.warning("  - The MELODIC directory does not contain the "
                        "required 'stats' folder. Mixture modeling on the "
                        "Z-statistical maps will be run.")
            os.makedirs(mel_dir)
            for item in os.listdir(mel_dir_in):
                os.symlink(op.join(mel_dir_in, item), op.join(mel_dir, item))
            melodic_command = ("{0} --in={1} --ICs={1} --mix={2} "
                               "--out_dir={3} --0stats --mmthresh=0.5").format(
                                    op.join(fsl_dir, 'melodic'),
                                    mel_IC,
                                    mel_IC_mix,
                                    mel_dir,
                               )
            os.system(melodic_command)
    else:
        if mel_dir_in:
            if not op.isdir(mel_dir_in):
                LGR.warning('  - The specified MELODIC directory does not '
                            'exist. MELODIC will be run separately.')
            else:
                LGR.warning('  - The specified MELODIC directory does not '
                            'contain the required files to run ICA-AROMA. '
                            'MELODIC will be run separately.')
        melodic_command = (
            "{0} --in={1} --outdir={2} --mask={3} --dim={4} "
            "--Ostats --nobet --mmthresh=0.5 --report "
            "--tr={5}"
        ).format(op.join(fsl_dir, "melodic"), in_file, mel_dir, mask, dim, TR)
        os.system(melodic_command)
    mel_IC_img = nib.load(mel_IC)
    nr_ICs = mel_IC_img.shape[3]
    zstat_imgs = []
    for i in range(1, nr_ICs + 1):
        z_temp = op.join(mel_dir, "stats", "thresh_zstat{0}.nii.gz".format(i))
        z_temp_img = nib.load(z_temp)
        if z_temp_img.ndim == 4:
            len_IC = z_temp_img.shape[3]
            zstat_img = image.index_img(z_temp_img, len_IC - 1)
        else:
            zstat_img = z_temp_img
        zstat_imgs.append(zstat_img)
    zstat_4d_img = image.concat_imgs(zstat_imgs)
    zstat_4d_img = image.math_img(
        "stat * mask[:, :, :, None]", stat=zstat_4d_img, mask=mask
    )
    zstat_4d_img.to_filename(mel_IC_thr)
def runICA ( fsl_dir , in_file , out_dir , mel_dir_in , mask , dim , TR ) : mel_dir = op . join ( out_dir , "melodic.ica" ) mel_IC = op . join ( mel_dir , "melodic_IC.nii.gz" ) mel_IC_mix = op . join ( mel_dir , "melodic_mix" ) mel_IC_thr = op . join ( out_dir , "melodic_IC_thr.nii.gz" ) if ( mel_dir_in and op . isfile ( op . join ( mel_dir_in , "melodic_IC.nii.gz" ) ) and op . isfile ( op . join ( mel_dir_in , "melodic_FTmix" ) ) and op . isfile ( op . join ( mel_dir_in , "melodic_mix" ) ) ) : LGR . info ( '  - The existing/specified MELODIC directory will be used.' ) if op . isdir ( op . join ( mel_dir_in , "stats" ) ) : os . symlink ( mel_dir_in , mel_dir ) else : LGR . warning ( "  - The MELODIC directory does not contain the " "required 'stats' folder. Mixture modeling on the " "Z-statistical maps will be run." ) os . makedirs ( mel_dir ) for item in os . listdir ( mel_dir_in ) : os . symlink ( op . join ( mel_dir_in , item ) , op . join ( mel_dir , item ) ) melodic_command = ( "{0} --in={1} --ICs={1} --mix={2} " "--out_dir={3} --0stats --mmthresh=0.5" ) . format ( op . join ( fsl_dir , 'melodic' ) , mel_IC , mel_IC_mix , mel_dir , ) os . system ( melodic_command ) else : if mel_dir_in : if not op . isdir ( mel_dir_in ) : LGR . warning ( '  - The specified MELODIC directory does not ' 'exist. MELODIC will be run separately.' ) else : LGR . warning ( '  - The specified MELODIC directory does not ' 'contain the required files to run ICA-AROMA. ' 'MELODIC will be run separately.' ) melodic_command = ( "{0} --in={1} --outdir={2} --mask={3} --dim={4} " "--Ostats --nobet --mmthresh=0.5 --report " "--tr={5}" ) . format ( op . join ( fsl_dir , "melodic" ) , in_file , mel_dir , mask , dim , TR ) os . system ( melodic_command ) mel_IC_img = nib . load ( mel_IC ) nr_ICs = mel_IC_img . shape [ 3 ] zstat_imgs = [ ] for i in range ( 1 , nr_ICs + 1 ) : z_temp = op . join ( mel_dir , "stats" , "thresh_zstat{0}.nii.gz" . format ( i ) ) z_temp_img = nib . load ( z_temp ) if z_temp_img . ndim == 4 : len_IC = z_temp_img . shape [ 3 ] zstat_img = image . index_img ( z_temp_img , len_IC - 1 ) else : zstat_img = z_temp_img zstat_imgs . append ( zstat_img ) zstat_4d_img = image . concat_imgs ( zstat_imgs ) zstat_4d_img = image . math_img ( "stat * mask[:, :, :, None]" , stat = zstat_4d_img , mask = mask ) zstat_4d_img . to_filename ( mel_IC_thr )
Run MELODIC and merge the thresholded ICs into a single 4D nifti file.
Run MELODIC and merge the thresholded ICs into a single 4D nifti file .
"""Run MELODIC and merge the thresholded ICs into a single 4D nifti file.

    Parameters
    ----------
    fsl_dir : str
        Full path of the bin-directory of FSL
    in_file : str
        Full path to the fMRI data file (nii.gz) on which MELODIC
        should be run
    out_dir : str
        Full path of the output directory
    mel_dir_in : str or None
        Full path of the MELODIC directory in case it has been run
        before, otherwise None.
    mask : str
        Full path of the mask to be applied during MELODIC
    dim : int
        Dimensionality of ICA
    TR : float
        TR (in seconds) of the fMRI data

    Output
    ------
    melodic.ica/: MELODIC directory
    melodic_IC_thr.nii.gz: Merged file containing the mixture modeling
                           thresholded Z-statistical maps located in
                           melodic.ica/stats/
    """ # Define the 'new' MELODIC directory and predefine some associated files # When a MELODIC directory is specified, # check whether all needed files are present. # Otherwise... run MELODIC again # If a 'stats' directory is present (contains thresholded spatial maps) # create a symbolic link to the MELODIC directory. # Otherwise create specific links and # run mixture modeling to obtain thresholded maps. # Create symbolic links to the items in the specified melodic # directory # Run mixture modeling # If a melodic directory was specified, display that it did not # contain all files needed for ICA-AROMA (or that the directory # does not exist at all) # Run MELODIC # Get number of components # Merge mixture modeled thresholded spatial maps. Note! In case that # mixture modeling did not converge, the file will contain two spatial # maps. The latter being the results from a simple null hypothesis test. # In that case, this map will have to be used (first one will be empty). # Define thresholded zstat-map file # Get number of volumes in component's thresholded image # Extract last spatial map within the thresh_zstat file # Merge to 4D # Apply the mask to the merged image (in case a melodic-directory was # predefined and run with a different mask)
fsl_dir
in_file
out_dir
mel_dir_in
mask
dim
TR