Пример #1
0
def select_grey_matter(fsdir, subjectid, mat_file, outdir, verbose=0):
    """ Select only the grey matter using FreeSurfer segmentation.

    Parameters
    ----------
    fsdir: str (mandatory)
        the freesurfer working directory with all the subjects.
    subjectid: str (mandatory)
        the subject identifier.
    outdir: str (mandatory)
        the folder where the computed grey matter mask image will be saved.
    verbose: int (optional, default 0)
        the verbosity level.

    Returns
    -------
    wm_file: str
        a binary segmantation of the white matter.
    affine: array (4, 4)
        the physical to physical t1 to functional transformation.
    """
    # Checks
    if not os.path.isdir(outdir):
        raise ValueError("'{0}' is not a valid folder.".format(outdir))
    # Convert the segmention to the native space
    regex = os.path.join(subjectid, "mri", "aseg.mgz")
    gm_files = mri_convert(
        fsdir, regex, reslice=True, interpolation="nearest",
        fsconfig="/i2bm/local/freesurfer/SetUpFreeSurfer.sh")
    if len(gm_files) != 1:
        raise ValueError("Expect one file '{0}'.".format(gm_files))
    gm_file = gm_files[0]

    # Get the grey matter label
    gm_image = nibabel.load(gm_file)

    gm_array = gm_image.get_data()
    gm_array_tmp = copy.deepcopy(gm_array)
    gm_array[np.where(gm_array != 3)] = 0
    gm_array[np.where(gm_array == 3)] = 1
    gm_array_tmp[np.where(gm_array_tmp != 42)] = 0
    gm_array_tmp[np.where(gm_array_tmp == 42)] = 1

    gm_array += gm_array_tmp

    # Save the ventricules images
    gm_image = nibabel.Nifti1Image(gm_array.astype(int),
                                   gm_image.get_affine())
    gm_file = os.path.join(outdir, "grey_matter.nii.gz")
    nibabel.save(gm_image, gm_file)

    # Load the affine deformation
    affine = scipy.io.loadmat(mat_file)["Affine"]
    affine = np.linalg.inv(affine)
    if verbose > 0:
        print("Affine :", affine)

    return gm_file, affine
    os.mkdir(convertdir)
elif args.erase:
    shutil.rmtree(convertdir)
    os.mkdir(convertdir)


"""
Step 1: Nifti conversions.
"""
if args.verbose > 0:
    print("[info] Start Nifti conversions...")
niftifiles = {}
for modality in ["aparc+aseg", "aparc.a2009s+aseg", "aseg", "wm", "rawavg"]:
    regex = os.path.join(args.subjectid, "mri", "{0}.mgz".format(modality))
    niftifiles[modality] = mri_convert(
        args.fsdir, regex, output_directory=None, reslice=True,
        interpolation="nearest", fsconfig=args.fsconfig)
    if args.verbose > 1:
        print("[result] {0}: {1}.".format(modality, niftifiles[modality]))


"""
Step 2: Registration matrix.
"""
if args.verbose > 0:
    print("[info] Start Registration matrix...")
regex = os.path.join(args.subjectid, "mri")
trffile = conformed_to_native_space(
    args.fsdir, regex, output_directory=None, fsconfig=args.fsconfig)
if args.verbose > 1:
    print("[result] trffile: {0}.".format(trffile))
if not os.path.isdir(convertdir):
    os.mkdir(convertdir)
elif args.erase:
    shutil.rmtree(convertdir)
    os.mkdir(convertdir)
"""
Step 1: Nifti conversions.
"""
if args.verbose > 0:
    print("[info] Start Nifti conversions...")
niftifiles = {}
for modality in ["aparc+aseg", "aparc.a2009s+aseg", "aseg", "wm", "rawavg"]:
    regex = os.path.join(args.subjectid, "mri", "{0}.mgz".format(modality))
    niftifiles[modality] = mri_convert(args.fsdir,
                                       regex,
                                       output_directory=None,
                                       reslice=True,
                                       interpolation="nearest",
                                       fsconfig=args.fsconfig)
    if args.verbose > 1:
        print("[result] {0}: {1}.".format(modality, niftifiles[modality]))
"""
Step 2: Registration matrix.
"""
if args.verbose > 0:
    print("[info] Start Registration matrix...")
regex = os.path.join(args.subjectid, "mri")
trffile = conformed_to_native_space(args.fsdir,
                                    regex,
                                    output_directory=None,
                                    fsconfig=args.fsconfig)
if args.verbose > 1: