def consistencyCheck(ref_csv,
                     outputBshellFile=None,
                     outPutResolutionFile=None):

    try:
        ref_imgs, _ = read_imgs_masks(ref_csv)
    except:
        ref_imgs = read_imgs(ref_csv)

    if isfile(outputBshellFile) and isfile(outPutResolutionFile):
        ref_bvals = read_bvals(outputBshellFile)
        ref_res = np.load(outPutResolutionFile)
    else:
        ref_bshell_img = ref_imgs[0]
        print(f'Using {ref_bshell_img} to determine b-shells')

        inPrefix = abspath(ref_bshell_img).split('.nii')[0]
        ref_bvals = findBShells(inPrefix + '.bval', outputBshellFile)

        ref_res = load(ref_bshell_img).header['pixdim'][1:4]
        np.save(outPutResolutionFile, ref_res)

    print('b-shells are', ref_bvals)

    print('\nSite', ref_csv, '\n')

    print('Checking consistency of b-shells among subjects')
    check_bshells(ref_imgs, ref_bvals)

    print('spatial resolution is', ref_res)
    print('Checking consistency of spatial resolution among subjects')
    check_resolution(ref_imgs, ref_res)
Пример #2
0
def joinBshells(imgPath, ref_bvals_file=None, ref_bvals=None, sep_prefix=None):

    if ref_bvals_file:
        print('Reading reference b-shell file ...')
        ref_bvals = read_bvals(ref_bvals_file)

    print('Joining b-shells for', imgPath)

    imgPath = local.path(imgPath)
    img = load(imgPath._path)
    dim = img.header['dim'][1:5]

    inPrefix = abspath(imgPath).split('.nii')[0]
    directory = dirname(inPrefix)
    prefix = basename(inPrefix)

    bvalFile = inPrefix + '.bval'
    bvecFile = inPrefix + '.bvec'

    if sep_prefix:
        harmPrefix = pjoin(directory, sep_prefix + prefix)
    else:
        harmPrefix = inPrefix

    if not isfile(harmPrefix + '.bval'):
        copyfile(bvalFile, harmPrefix + '.bval')
    if not isfile(harmPrefix + '.bvec'):
        copyfile(bvecFile, harmPrefix + '.bvec')

    bvals = np.array(read_bvals(inPrefix + '.bval'))

    joinedDwi = np.zeros((dim[0], dim[1], dim[2], dim[3]), dtype='float32')

    for bval in ref_bvals:

        # ind= np.where(bval==bvals)[0]
        ind = np.where(abs(bval - bvals) <= BSHELL_MIN_DIST)[0]

        if bval == 0.:
            b0Img = load(inPrefix + '_b0.nii.gz')
            b0 = b0Img.get_data()
            for i in ind:
                joinedDwi[:, :, :, i] = b0

        else:
            b0_bshell = load(harmPrefix + f'_b{int(bval)}.nii.gz').get_data()

            joinedDwi[:, :, :, ind] = b0_bshell[:, :, :, 1:]

    if not isfile(harmPrefix + '.nii.gz'):
        save_nifti(harmPrefix + '.nii.gz', joinedDwi, b0Img.affine,
                   b0Img.header)
    else:
        print(harmPrefix + '.nii.gz', 'already exists, not overwritten.')
Пример #3
0
def separateBshells(imgPath, ref_bvals_file=None, ref_bvals=None):

    if ref_bvals_file:
        print('Reading reference b-shell file ...')
        ref_bvals = read_bvals(ref_bvals_file)

    print('Separating b-shells for', imgPath)

    imgPath = local.path(imgPath)

    img = load(imgPath._path)
    dwi = img.get_data()
    inPrefix = abspath(imgPath).split('.nii')[0]
    bvals = np.array(read_bvals(inPrefix + '.bval'))
    bvecs = np.array(read_bvecs(inPrefix + '.bvec'))

    for bval in ref_bvals:

        # ind= np.where(bval==bvals)[0]
        ind = np.where(abs(bval - bvals) <= BSHELL_MIN_DIST)[0]
        N_b = len(ind)

        bPrefix = inPrefix + f'_b{int(bval)}'

        if bval == 0.:
            b0 = find_b0(dwi, ind)

        if isfile(bPrefix + '.nii.gz'):
            continue

        if bval == 0.:
            save_nifti(bPrefix + '.nii.gz', b0, img.affine, img.header)

        else:
            b0_bshell = np.zeros(
                (dwi.shape[0], dwi.shape[1], dwi.shape[2], N_b + 1),
                dtype='float32')
            b0_bshell[:, :, :, 0] = b0
            b0_bshell[:, :, :, 1:] = dwi[:, :, :, ind]

            b0_bvals = [0.] + [bval] * N_b

            b0_bvecs = np.zeros((N_b + 1, 3), dtype='float32')
            b0_bvecs[1:, ] = bvecs[ind, :]

            save_nifti(bPrefix + '.nii.gz', b0_bshell, img.affine, img.header)
            write_bvals(bPrefix + '.bval', b0_bvals)
            write_bvecs(bPrefix + '.bvec', b0_bvecs)
Пример #4
0
def scan_cases(path):
    result = []
    # 项目种类
    project_kinds = util.safely_list_dir(path)
    for project_kind in project_kinds:
        projects = util.safely_list_dir(path, project_kind)

        # 遍历项目
        for project in projects:
            versions = util.safely_list_dir(path, project_kind, project)
            # 遍历版本
            for version in versions:
                pycharm_projects = util.safely_list_dir(
                    path, project_kind, project, version)
                # 遍历pycharm工程
                for pycharm_project in pycharm_projects:
                    case_dir = util.join(path, project_kind, project, version,
                                         pycharm_project, 'src', 'cases')
                    modules = util.safely_list_dir(case_dir)
                    for m in modules:
                        # 文件作为模块
                        if util.isfile(util.join(case_dir, m)):
                            if util.suffix(m) == '.py':
                                result.extend(
                                    parsePy2Dict(util.join(case_dir, m),
                                                 util.join(path, project),
                                                 'cases', '', util.filename(m),
                                                 [], None, version,
                                                 pycharm_project,
                                                 project_kind))
                        # 目录作为一级模块
                        else:
                            # 在目录下的__init__.py里获取模块描述
                            module_desc = ''
                            if util.isfile(
                                    util.join(case_dir, m, '__init__.py')):
                                f = open(util.join(case_dir, m, '__init__.py'))
                                p = ast.parse(f.read()).body
                                module_desc = p[0].value.s if p else ''
                                f.close()
                            for root, dirs, files in util.walk(
                                    util.join(case_dir, m)):
                                for file in files:
                                    if util.suffix(file) == '.py':
                                        modules = util.relative(
                                            util.join(root, file),
                                            case_dir)[0:-1]
                                        package = util.relative(
                                            util.join(root, file),
                                            util.dirname(case_dir))[0:-1]
                                        package = '.'.join(package)
                                        result.extend(
                                            parsePy2Dict(
                                                util.join(root, file),
                                                util.join(path,
                                                          project), package, m,
                                                util.filename(file), modules,
                                                module_desc, version,
                                                pycharm_project, project_kind))

    return result
    print('\nSite', ref_csv, '\n')

    print('Checking consistency of b-shells among subjects')
    check_bshells(ref_imgs, ref_bvals)

    print('spatial resolution is', ref_res)
    print('Checking consistency of spatial resolution among subjects')
    check_resolution(ref_imgs, ref_res)


if __name__ == '__main__':
    if len(sys.argv) == 1 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
        print(
            '''Check consistency of b-shells and spatial resolution among subjects
Usage:
consistencyCheck list.csv/txt ref_bshell_bvalues.txt ref_res_file.npy

Provide a csv/txt file with first column for dwi and 2nd column for mask: dwi1,mask1\\ndwi2,mask2\\n...
or just one column for dwi1\\ndwi2\\n...
In addition, provide ref_bshell_bvalues and ref_res_file.''')
        exit()

    ref_csv = abspath(sys.argv[1])
    outputBshellFile = abspath(sys.argv[2])
    outPutResolutionFile = abspath(sys.argv[3])
    if isfile(ref_csv):
        consistencyCheck(ref_csv, outputBshellFile, outPutResolutionFile)
    else:
        raise FileNotFoundError(f'{ref_csv} does not exists.')
Пример #6
0
    def main(self):

        if self.N_proc == '-1':
            self.N_proc = N_CPU

        # check directory existence
        check_dir(self.templatePath, self.force)

        ## check consistency of b-shells and spatial resolution
        ref_bvals_file = pjoin(self.templatePath, 'ref_bshell_bvalues.txt')
        ref_res_file = pjoin(self.templatePath, 'ref_res_file.npy')
        if self.ref_csv:
            if isfile(ref_bvals_file) and isfile(ref_res_file):
                remove(ref_bvals_file)
                remove(ref_res_file)

            consistencyCheck(self.ref_csv, ref_bvals_file, ref_res_file)

        if self.target_csv:
            consistencyCheck(self.target_csv, ref_bvals_file, ref_res_file)

        ## separate b-shells
        if self.ref_csv:
            refListOutPrefix = separateShellsWrapper(self.ref_csv,
                                                     ref_bvals_file,
                                                     self.N_proc)
        if self.target_csv:
            tarListOutPrefix = separateShellsWrapper(self.target_csv,
                                                     ref_bvals_file,
                                                     self.N_proc)

        ## define variables for template creation and data harmonization

        # variables common to all ref_bvals
        pipeline_vars = [
            '--tar_name',
            self.target,
            '--nshm',
            self.N_shm,
            '--nproc',
            self.N_proc,
            '--template',
            self.templatePath,
        ]

        if self.reference:
            pipeline_vars.append(f'--ref_name {self.reference}')
        if self.N_zero:
            pipeline_vars.append(f'--nzero {self.N_zero}')
        if self.bvalMap:
            pipeline_vars.append(f'--bvalMap {self.bvalMap}')
        if self.resample:
            pipeline_vars.append(f'--resample {self.resample}')
        if self.denoise:
            pipeline_vars.append('--denoise')
        if self.travelHeads:
            pipeline_vars.append('--travelHeads')
        if self.force:
            pipeline_vars.append('--force')
        if self.debug:
            pipeline_vars.append('--debug')
        if self.verbose:
            pipeline_vars.append('--verbose')

        # the b-shell bvalues are sorted in descending order because we want to perform registration with highest bval
        ref_bvals = read_bvals(ref_bvals_file)[::-1]
        for bval in ref_bvals[:-1]:  # pass the last bval which is 0.

            if self.create and not self.process:
                print('## template creation ##')

                check_call((' ').join([
                    pjoin(SCRIPTDIR, 'harmonization.py'), '--tar_list',
                    tarListOutPrefix + f'_b{int(bval)}.csv', '--bshell_b',
                    str(int(bval)), '--ref_list', refListOutPrefix +
                    f'_b{int(bval)}.csv', '--create'
                ] + pipeline_vars),
                           shell=True)

            elif not self.create and self.process:
                print('## data harmonization ##')

                check_call((' ').join([
                    pjoin(SCRIPTDIR, 'harmonization.py'), '--tar_list',
                    tarListOutPrefix + f'_b{int(bval)}.csv',
                    f'--ref_list {refListOutPrefix}_b{int(bval)}.csv' if self.
                    ref_csv else '', '--bshell_b',
                    str(int(bval)), '--process'
                ] + pipeline_vars),
                           shell=True)

            elif self.create and self.process:
                check_call((' ').join([
                    pjoin(SCRIPTDIR, 'harmonization.py'), '--tar_list',
                    tarListOutPrefix + f'_b{int(bval)}.csv', '--bshell_b',
                    str(int(bval)), '--ref_list', refListOutPrefix +
                    f'_b{int(bval)}.csv', '--create', '--process'
                ] + pipeline_vars),
                           shell=True)

            if '--force' in pipeline_vars:
                pipeline_vars.remove('--force')

        ## join harmonized data
        if self.process:
            joinAllBshells(self.target_csv, ref_bvals_file, 'harmonized_',
                           self.N_proc)

            if self.debug and self.ref_csv:
                joinAllBshells(self.ref_csv, ref_bvals_file, 'reconstructed_',
                               self.N_proc)