Exemplo n.º 1
0
    def _reg_atlas(self):
        '''
        If there is no registered atlas file, register atlas to subject
        '''
        from fsl.file_prep import flirt_primary_guess, fnirt_from_atlas_2_subj, apply_fnirt_warp_on_label, os_path_2_fsl, subj_files

        if os.path.exists(self.atlas_file_name):
            print(f'{self.atlas_type} is registered to subject')
        else:

            subj_folder = self.folder_name.replace(os.sep, '/')
            mprage_file_name = subj_files(subj_folder)[0]
            subj_folder = os_path_2_fsl(subj_folder + r'/')
            out_registered = subj_folder + 'r' + mprage_file_name[:-4] + '_brain.nii'

            atlas_label = self.mni_atlas_label
            if 'yeo' in self.atlas_type:
                atlas_template = f'{self.atlas_main_folder}{os.sep}{self.atlas_type}{os.sep}Schaefer_template.nii'
            elif 'bna' in self.atlas_type:
                atlas_template = f'{self.atlas_main_folder}{os.sep}MNI152_T1_1mm_brain.nii'

            atlas_label = os_path_2_fsl(atlas_label)
            atlas_template = os_path_2_fsl(atlas_template)
            atlas_brain, atlas_registered_flirt, atlas_registered_flirt_mat = flirt_primary_guess(
                subj_folder, atlas_template, out_registered)
            warp_name = fnirt_from_atlas_2_subj(subj_folder,
                                                out_registered,
                                                atlas_brain,
                                                atlas_registered_flirt_mat,
                                                cortex_only=False)
            atlas_labels_registered = apply_fnirt_warp_on_label(
                subj_folder, atlas_label, out_registered, warp_name)

            print(f'{self.atlas_type} is registered to subject')
Exemplo n.º 2
0
def register_atlas_2_diff(subj_folder, atlas_template, atlas_label):

    subj_folder = subj_folder.replace(os.sep, '/')

    mprage_file_name, diff_file_name = hcp_subj_files(subj_folder)

    subj_folder = os_path_2_fsl(subj_folder)

    subj_mprage, out_brain = bet_4_regis_mprage(subj_folder, mprage_file_name)
    ''' Registration from MPRAGE to 1st CHARMED scan using inverse matrix of CHARMED to MPRAGE registration:
    From CHARMED to MPRAGE:'''
    subj_first_charmed, out_registered, out_registered_mat = reg_from_diff_2_mprage(subj_folder, subj_mprage, diff_file_name)

    '''Creation of inverse matrix:  '''
    inv_mat = create_inv_mat(out_registered_mat)

    '''From MPRAGE to CHARMED using the inverse matrix: '''
    out_registered = reg_from_mprage_2_chm_inv(subj_folder, mprage_file_name, out_brain, subj_first_charmed, inv_mat)

    '''Registration from atlas to regisered MPRAGE:
        flirt for atlas to registered MPRAGE for primary guess:  '''
    atlas_brain, atlas_registered_flirt, atlas_registered_flirt_mat = flirt_primary_guess(subj_folder, atlas_template,
                                                                                          out_registered)

    '''fnirt for atlas based on flirt results:    '''
    warp_name = fnirt_from_atlas_2_subj(subj_folder, out_registered, atlas_brain, atlas_registered_flirt_mat,
                                        cortex_only=False)

    '''apply fnirt warp on atlas labels:   '''
    atlas_labels_registered = apply_fnirt_warp_on_label(subj_folder, atlas_label, out_registered, warp_name)
Exemplo n.º 3
0
def basic_files_hcp(cortex_only=False):
    if cortex_only:
        atlas_label = r'F:\data\atlases\BNA\newBNA_Labels.nii'
        atlas_template = r'F:\data\atlases\BNA\MNI152_T1_1mm_brain.nii'

    else:
        atlas_label = r'F:\data\atlases\BNA\BN_Atlas_274_combined_1mm.nii'
        atlas_template = r'F:\data\atlases\BNA\MNI152_T1_1mm.nii'

    atlas_label = os_path_2_fsl(atlas_label)
    atlas_template = os_path_2_fsl(atlas_template)

    folder_name = r'F:\data\V7\HCP'
    subj = glob.glob(f'{folder_name}\*{os.sep}')

    return subj, folder_name, atlas_template, atlas_label
Exemplo n.º 4
0
def apply_flirt_on_vol(s, folder_name, vol_type, template_brain, warp_name):
    subj_name = r'/' + s + r'/'
    subj_folder = folder_name + subj_name
    subj_folder = subj_folder.replace(os.sep, '/')

    vol = vol_file(subj_folder,vol_type)

    subj_folder = os_path_2_fsl(subj_folder)
    vol = os.path.join(subj_folder,vol)

    # apply fnirt:
    vol_registered = os.path.join(subj_folder+ 'rr' + vol.split(sep="/")[-1]) ##
    cmd = 'bash -lc "flirt -in {0} -ref {1} -out {2} -applyxfm -init {3}"'.format(vol, template_brain, vol_registered, warp_name)
    cmd = cmd.replace(os.sep,'/')
    os.system(cmd)
Exemplo n.º 5
0
def apply_fnirt_on_vol(s, folder_name, vol_type, template_brain, warp_name):
    subj_name = r'/' + s + r'/'
    subj_folder = folder_name + subj_name
    subj_folder = subj_folder.replace(os.sep, '/')

    vol = vol_file(subj_folder,vol_type)

    subj_folder = os_path_2_fsl(subj_folder)
    vol = os.path.join(subj_folder,vol)

    # apply fnirt:
    vol_registered = os.path.join(subj_folder+ 'rr' + vol.split(sep="/")[-1]) ##
    cmd = 'bash -lc "applywarp --ref={0} --in={1} --out={2} --warp={3} --interp={4}"'.format(template_brain, vol, vol_registered, warp_name, 'nn')
    cmd = cmd.replace(os.sep,'/')
    os.system(cmd)
Exemplo n.º 6
0
def reg_vols_2_atlas(s, folder_name):
    subj_name = r'/' + s + r'/'
    subj_folder = folder_name + subj_name
    subj_folder = subj_folder.replace(os.sep, '/')

    # search file name:
    template_brain = template_file(subj_folder)
    diff_file = 'diff_corrected_b2000.nii'

    subj_folder = os_path_2_fsl(subj_folder)

    template_brain = os.path.join(subj_folder,template_brain)
    diff_file = os.path.join(subj_folder,diff_file)

    # flirt for primary guess:
    options = r'-bins 256 -cost corratio -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -dof 12 -interp trilinear'

    diff_flirt = os.path.join(subj_folder + diff_file.split(sep="/")[-1]+'_reg')
    diff_flirt_mat = diff_flirt[:-4] + '.mat'

    cmd = 'bash -lc "flirt -ref {0} -in {1} -out {2} -omat {3} {4}"'.format(template_brain, diff_file, diff_flirt, diff_flirt_mat, options)
    cmd = cmd.replace(os.sep,'/')
    os.system(cmd)

    # apply flirt on 4D:
    cmd = 'bash -lc "applyxfm4D {0} {1} {2} {3} -singlematrix"'.format(diff_file, template_brain, diff_flirt, diff_flirt_mat)
    cmd = cmd.replace(os.sep,'/')
    os.system(cmd)

    #warp_name = diff_file[:-4] + '_diff2template.nii'

    #cmd = 'bash -lc "fnirt --ref={0} --in={1} --aff={2} --cout={3}"'.format(template_brain, diff_file, diff_flirt_mat, warp_name)
    #cmd = cmd.replace(os.sep,'/')
    #os.system(cmd)

    return template_brain, diff_flirt_mat
Exemplo n.º 7
0
    first_vol_mask = file_name[:-4] + 'hifi_nodif_brain_mask'
    # save first direction file:
    cmd = fr'bash -lc "fslroi {out_mask}.nii {first_vol_mask}.nii 0 1"'
    os.system(cmd)


if __name__ == '__main__':

    main_path = r'F:\Hila\Ax3D_Pack\V6\v7calibration\Old_AxCaliber\MS'
    rename_folder_files(main_path)
    for sub in glob(f'{main_path}\*'):
        for subj_fol in glob(sub + '\*'):
            for file in glob(os.path.join(subj_fol, '*')):
                if file.endswith('.nii'):
                    file_name = file[:-4]
                    file_name = os_path_2_fsl(file_name)
                    file_name = file_name.replace(os.sep, '/')
                    bet_4_file(file_name)

            subj_folder = os_path_2_fsl(subj_fol)

            diff_file = subj_folder + '/data.nii'
            split_from_4D_2_3Ds(diff_file)
            diff_file_1st = subj_folder + '/data0000.nii'

            for diff_file_vol in glob(f'{subj_fol}\data00*.nii'):
                diff_file_vol = os_path_2_fsl(diff_file_vol)
                flirt_regis(diff_file_1st, diff_file_vol)

            merge_3Ds_2_4D(subj_folder, diff_file)
            delete_files(subj_fol)