示例#1
0
def create_peri_mask(timepoint, group, subject, na=[15]):
    in_dir = os.path.join(pt.proc_in_dir, timepoint, group, subject, 'T2w')
    out_dir = os.path.join(pt.proc_out_dir, timepoint, group, subject, 'T2w')

    if not os.path.isdir(in_dir):
        sys.exit("Error: '%s' is not an existing directory." % (in_dir, ))

    if not os.path.exists(out_dir):
        os.makedirs(out_dir)

    # input atlas labels file (NIfTI)
    #path_atlas = os.path.join(in_dir, subject + 'BiasBet_AnnorsfMRI.nii.gz')

    # input mask file (NIfTI)
    path_in_mask = os.path.join(in_dir, subject + 'Stroke_mask.nii.gz')
    if not os.path.isfile(path_in_mask):
        sys.exit("Error: '%s' is not a regular file." % (path_in_mask, ))

    # output cortex ROIs file (NIfTI)
    #path_out_rois = os.path.join(out_dir, subject + '_cortex_rois_1.nii.gz')

    mask, mask_dims = pt.read_data(path_in_mask)

    #rois, rois_dims = create_rois_1(pt.path_labels_1, path_atlas, path_rois=path_out_rois)

    for model in range(3, 4):
        for n in na:
            # output mask file (NIfTI)
            path_out_mask = os.path.join(
                out_dir, subject + '_peri_mask_m%d_n%d.nii.gz' % (model, n))

            # output masked cortex ROIs file (NIfTI)
            #path_out_rois = os.path.join(out_dir, subject + '_cortex_rois_1_m%d_n%d.nii.gz' % (model, n))

            peri = np.copy(mask)

            if (model == 1) or (model == 2):
                for k in zrange(mask.shape[2]):
                    image = mask[:, :, k]
                    if np.any(image.astype(np.bool)):
                        peri[:, :, k] = dm.dilate_repeat(image,
                                                         connectivity=model,
                                                         n=n)
            else:
                struct = dm.circle_mask(n=n)
                for k in zrange(mask.shape[2]):
                    image = mask[:, :, k]
                    if np.any(image.astype(np.bool)):
                        peri[:, :, k] = dm.dilate_struct(image, struct)

            pt.save_data(peri.astype(np.float32),
                         mask_dims,
                         path_out_mask,
                         dtype=None)
示例#2
0
def create_rois_1(path_labels, path_atlas, path_rois=None, mask=None):
    if not os.path.isfile(path_atlas):
        sys.exit("Error: '%s' is not a regular file." % (path_atlas, ))

    # create atlas labels ROIs
    labels_hdr, rois = csr.create_rois(path_labels, [path_atlas],
                                       datatype=16,
                                       preserve=True)
    voxel_dims = labels_hdr[0].get_zooms()

    rois = np.squeeze(rois)

    # apply mask to ROIs
    if mask is not None:
        rois = np.multiply(rois, mask)

    # save ROIs file (NIfTI)
    if path_rois is not None:
        pt.save_data(rois, voxel_dims, path_rois, dtype=None)

    return (rois, voxel_dims)
示例#3
0
def create_rois_2(path_labels,
                  path_atlas,
                  path_rois=None,
                  mask=None,
                  preserve=False):
    if not os.path.isfile(path_atlas):
        sys.exit("Error: '%s' is not a regular file." % (path_atlas, ))

    # create atlas labels ROIs hyperstack (4D)
    labels_hdr, rois = csr.create_rois(path_labels, [path_atlas],
                                       datatype=16,
                                       preserve=preserve)
    voxel_dims = labels_hdr[0].get_zooms()

    # apply mask to each ROI
    if mask is not None:
        for k in zrange(rois.shape[3]):
            rois[:, :, :, k] = np.multiply(rois[:, :, :, k], mask)

    # save ROIs file (NIfTI)
    if path_rois is not None:
        pt.save_data(rois, voxel_dims, path_rois, dtype=None)

    return (rois, voxel_dims)
示例#4
0
    return dilated


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='Dilate input mask.')
    parser.add_argument('in_mask', help='input mask file name')
    parser.add_argument('-o',
                        '--out_mask',
                        help='output mask file name',
                        required=True)
    args = parser.parse_args()

    # input mask file
    if not os.path.isfile(args.in_mask):
        sys.exit("Error: '%s' is not a regular file." % (args.in_mask, ))

    # read input mask data
    data, voxel_dims = pt.read_data(args.in_mask)

    struct = circle_mask()
    for k in zrange(data.shape[2]):
        image = data[:, :, k]
        if np.any(image.astype(np.bool)):
            #data[:, :, k] = dilate_repeat(image)
            data[:, :, k] = dilate_struct(image, struct)

    # save mask data as NIfTI file
    pt.save_data(data, voxel_dims, args.out_mask, dtype=None)
def xfm_T2w_rsfMRI(raw_dir, timepoint_P7, timepoint, group, subject, expno_T2w,
                   expno_rsfMRI, procno_T2w, procno_rsfMRI):
    if (expno_T2w is None) or (expno_rsfMRI is None) or (
            procno_T2w is None) or (procno_rsfMRI is None):
        return

    in_dir = os.path.join(pt.proc_in_dir, timepoint, group, subject, 'T2w')
    mask_dir = os.path.join(pt.proc_out_dir, timepoint, group, subject, 'T2w')
    out_dir = os.path.join(pt.proc_out_dir, timepoint, group, subject, 'fMRI')

    if not os.path.isdir(in_dir):
        sys.exit("Error: '%s' is not an existing directory." % (in_dir, ))

    if not os.path.isdir(mask_dir):
        sys.exit("Error: '%s' is not an existing directory." % (mask_dir, ))

    if not os.path.exists(out_dir):
        os.makedirs(out_dir)

    # input T2w atlas labels file
    path_in_anno = os.path.join(in_dir, subject + 'BiasBet_AnnorsfMRI.nii.gz')
    if not os.path.isfile(path_in_anno):
        sys.exit("Error: '%s' is not a regular file." % (path_in_anno, ))

    # input T2w stroke mask file
    if timepoint == timepoint_P7:
        path_in_mask = os.path.join(in_dir, subject + 'Stroke_mask.nii.gz')
    else:
        path_in_mask = os.path.join(mask_dir, subject + 'Stroke_mask.nii.gz')

    # input T2w peri-infarct mask file
    path_in_peri = os.path.join(mask_dir, subject + '_peri_mask_m3_n15.nii.gz')
    if not os.path.isfile(path_in_peri):
        sys.exit("Error: '%s' is not a regular file." % (path_in_peri, ))

    # output rsfMRI file
    #path_rsfMRI = os.path.join(out_dir, subject + '_rsfMRI.nii.gz')

    # output transformed T2w file
    path_T2w_rsfMRI = os.path.join(out_dir, subject + '_T2w_rsfMRI.nii.gz')

    # output transformed atlas labels file
    path_anno_rsfMRI = os.path.join(out_dir,
                                    subject + '_T2w_Anno_rsfMRI.nii.gz')

    # output transformed stroke mask file
    path_mask_rsfMRI = os.path.join(out_dir,
                                    subject + '_T2w_Stroke_mask_rsfMRI.nii.gz')

    # output transformed peri-infarct mask file
    path_peri_rsfMRI = os.path.join(out_dir,
                                    subject + '_T2w_peri_mask_rsfMRI.nii.gz')

    pvr.check_args(pt.proc_out_dir, raw_dir, subject, expno_T2w, procno_T2w)
    pvr.check_args(pt.proc_out_dir, raw_dir, subject, expno_rsfMRI,
                   procno_rsfMRI)

    # T2w data
    pv = pvr.ParaVision(os.path.join(pt.proc_out_dir, timepoint, group),
                        raw_dir, subject, expno_T2w, procno_T2w)
    pv.read_2dseq(map_raw=False,
                  map_pv6=False,
                  roll_fg=False,
                  squeeze=False,
                  compact=False,
                  swap_vd=False,
                  scale=1.0)
    #pv.save_nifti(ftype='NIFTI_GZ')
    matrix_T2w, matrix_T2w_inv = pv.get_matrix()
    data_T2w = pv.nifti_image.get_data()
    #data_dims_T2w = pv.data_dims[:3]
    #data_type_T2w = pv.data_type
    voxel_dims_T2w = pv.voxel_dims[:3]
    #voxel_unit_T2w = pv.voxel_unit

    # rsfMRI data
    pv = pvr.ParaVision(os.path.join(pt.proc_out_dir, timepoint, group),
                        raw_dir, subject, expno_rsfMRI, procno_rsfMRI)
    pv.read_2dseq(map_raw=False,
                  map_pv6=False,
                  roll_fg=False,
                  squeeze=False,
                  compact=False,
                  swap_vd=False,
                  scale=1.0)
    #pv.save_nifti(ftype='NIFTI_GZ')
    matrix_rsfMRI, matrix_rsfMRI_inv = pv.get_matrix()
    #data_rsfMRI = np.mean(pv.nifti_image.get_data(), axis=3)
    data_dims_rsfMRI = pv.data_dims[:3]
    #data_type_rsfMRI = pv.data_type
    voxel_dims_rsfMRI = pv.voxel_dims[:3]
    #voxel_unit_rsfMRI = pv.voxel_unit

    # transformation matrix
    matrix_T2w_rsfMRI = np.dot(matrix_rsfMRI_inv, matrix_T2w)
    matrix_rsfMRI_T2w = np.dot(matrix_T2w_inv, matrix_rsfMRI)
    pt.save_matrix(os.path.join(out_dir, subject + '_T2w_rsfMRI.mat'),
                   matrix_T2w_rsfMRI)
    pt.save_matrix(os.path.join(out_dir, subject + '_rsfMRI_T2w.mat'),
                   matrix_rsfMRI_T2w)

    # save rsfMRI data as NIfTI file
    #pt.save_data(np.rot90(data_rsfMRI, k=2, axes=(0, 2)), voxel_dims_rsfMRI, path_rsfMRI, dtype=None)

    # save transformed T2w data as NIfTI file
    data_T2w_rsfMRI = ax.xfm_serial(data_T2w,
                                    matrix_rsfMRI_T2w,
                                    data_dims_rsfMRI,
                                    voxel_dims_rsfMRI,
                                    voxel_dims_T2w,
                                    interp=1,
                                    inverse=True)
    pt.save_data(np.rot90(data_T2w_rsfMRI, k=2, axes=(0, 2)),
                 voxel_dims_rsfMRI,
                 path_T2w_rsfMRI,
                 dtype=None)

    # save transformed T2w atlas labels as NIfTI file
    data_anno, voxel_dims_anno = pt.read_data(path_in_anno)
    data_anno_rsfMRI = ax.xfm_serial(np.rot90(data_anno, k=2, axes=(0, 2)),
                                     matrix_rsfMRI_T2w,
                                     data_dims_rsfMRI,
                                     voxel_dims_rsfMRI,
                                     voxel_dims_anno,
                                     interp=0,
                                     inverse=True)
    pt.save_data(np.rot90(data_anno_rsfMRI, k=2, axes=(0, 2)),
                 voxel_dims_rsfMRI,
                 path_anno_rsfMRI,
                 dtype=None)

    # save transformed T2w stroke mask as NIfTI file
    if os.path.isfile(path_in_mask):
        data_mask, voxel_dims_mask = pt.read_data(path_in_mask)
        data_mask_rsfMRI = ax.xfm_serial(np.rot90(data_mask, k=2, axes=(0, 2)),
                                         matrix_rsfMRI_T2w,
                                         data_dims_rsfMRI,
                                         voxel_dims_rsfMRI,
                                         voxel_dims_mask,
                                         interp=0,
                                         inverse=True)
        pt.save_data(np.rot90(data_mask_rsfMRI, k=2, axes=(0, 2)),
                     voxel_dims_rsfMRI,
                     path_mask_rsfMRI,
                     dtype=None)

    # save transformed T2w peri-infarct mask as NIfTI file
    data_peri, voxel_dims_peri = pt.read_data(path_in_peri)
    data_peri_rsfMRI = ax.xfm_serial(np.rot90(data_peri, k=2, axes=(0, 2)),
                                     matrix_rsfMRI_T2w,
                                     data_dims_rsfMRI,
                                     voxel_dims_rsfMRI,
                                     voxel_dims_peri,
                                     interp=0,
                                     inverse=True)
    pt.save_data(np.rot90(data_peri_rsfMRI, k=2, axes=(0, 2)),
                 voxel_dims_rsfMRI,
                 path_peri_rsfMRI,
                 dtype=None)
示例#6
0
def create_rois_DTI(timepoint, group, subject, labels, labels_2, scale=10.0):
    in_dir = os.path.join(pt.proc_in_dir, timepoint, group, subject, 'DTI')
    out_dir = os.path.join(pt.proc_out_dir, timepoint, group, subject, 'DTI')
    dti_dir = os.path.join(pt.proc_out_dir, timepoint, group, subject, 'DTI',
                           'DSI_studio')

    if not os.path.isdir(in_dir):
        #sys.exit("Error: '%s' is not an existing directory." % (in_dir,))
        return

    if not os.path.isdir(out_dir):
        sys.exit("Error: '%s' is not an existing directory." % (out_dir, ))

    if not os.path.exists(dti_dir):
        os.makedirs(dti_dir)

    # input atlas labels file (NIfTI)
    #path_atlas = os.path.join(in_dir, subject + 'SmoothMicoBet_AnnoSplit_rsfMRI.nii.gz')
    path_atlas = os.path.join(out_dir, subject + '_T2w_Anno_DTI.nii.gz')
    if not os.path.isfile(path_atlas):
        #sys.exit("Error: '%s' is not a regular file." % (path_atlas,))
        return

    # input peri-infarct mask file (NIfTI)
    path_mask = os.path.join(out_dir, subject + '_T2w_peri_mask_DTI.nii.gz')
    peri_mask, _ = read_mask(path_mask)

    # output ROIs files (NIfTI)
    #path_out_rois = os.path.join(out_dir, subject + '_seed_rois.nii.gz')
    #path_out_rois_1 = os.path.join(out_dir, subject + '_cortex_rois_1.nii.gz')
    #path_out_rois_2 = os.path.join(out_dir, subject + '_cortex_rois_2.nii.gz')
    #path_out_rois_x = os.path.join(out_dir, subject + '_seed_rois_mod.nii.gz')
    path_out_rois = os.path.join(out_dir, 'Seed_ROIs_all.nii.gz')
    path_out_rois_1 = os.path.join(out_dir, subject + '_rois_peri.nii.gz')
    path_out_rois_2 = os.path.join(out_dir, 'Seed_ROIs_peri.nii.gz')
    path_out_rois_x = os.path.join(out_dir, 'Seed_ROIs_all_mod_peri.nii.gz')

    # output maximum intensity projection files (NIfTI)
    path_out_mip_rois = os.path.join(dti_dir,
                                     subject + '_T2w_Anno_DTI_scaled.nii.gz')
    path_out_mip_rois_x = os.path.join(
        dti_dir, subject + '_T2w_Anno_DTI_mod_peri_scaled.nii.gz')

    # output maximum intensity projection label names text files
    path_out_label_names = os.path.join(
        dti_dir, subject + '_T2w_Anno_DTI_scaled.nii.txt')
    path_out_label_names_x = os.path.join(
        dti_dir, subject + '_T2w_Anno_DTI_mod_peri_scaled.nii.txt')

    rois, rois_dims = create_rois_2(pt.path_labels,
                                    path_atlas,
                                    path_rois=path_out_rois,
                                    preserve=True)
    _, _ = create_rois_1(pt.path_labels_1,
                         path_atlas,
                         path_rois=path_out_rois_1,
                         mask=peri_mask)
    rois_2, _ = create_rois_2(pt.path_labels_2,
                              path_atlas,
                              path_rois=path_out_rois_2,
                              mask=peri_mask,
                              preserve=True)

    rois_x = np.copy(rois)
    replace_rois(labels, labels_2, rois_x, rois_2)
    pt.save_data(rois_x, rois_dims, path_out_rois_x, dtype=None)

    mip_rois = np.max(rois, axis=3)
    mip_rois_x = np.max(rois_x, axis=3)
    mip_rois_dims = tuple((x * scale) for x in rois_dims)
    pt.save_data(mip_rois, mip_rois_dims, path_out_mip_rois, dtype=None)
    pt.save_data(mip_rois_x, mip_rois_dims, path_out_mip_rois_x, dtype=None)

    shutil.copyfile(pt.path_label_names_2000, path_out_label_names)
    shutil.copyfile(pt.path_label_names_2000, path_out_label_names_x)
    print(path_out_label_names)
    print(path_out_label_names_x)
示例#7
0
def create_rois_rsfMRI(timepoint, group, subject, labels, labels_2,
                       label_names_2000, label_names_peri):
    in_dir = os.path.join(pt.proc_in_dir, timepoint, group, subject, 'fMRI')
    out_dir = os.path.join(pt.proc_out_dir, timepoint, group, subject, 'fMRI')
    regr_dir = os.path.join(pt.proc_out_dir, timepoint, group, subject, 'fMRI',
                            'regr')

    if not os.path.isdir(in_dir):
        sys.exit("Error: '%s' is not an existing directory." % (in_dir, ))

    if not os.path.isdir(out_dir):
        sys.exit("Error: '%s' is not an existing directory." % (out_dir, ))

    if not os.path.exists(regr_dir):
        os.makedirs(regr_dir)

    # input atlas labels file (NIfTI)
    #path_atlas = os.path.join(in_dir, subject + 'SmoothBet_AnnoSplit_rsfMRI.nii.gz')
    path_atlas = os.path.join(out_dir, subject + '_T2w_Anno_rsfMRI.nii.gz')

    # input peri-infarct mask file (NIfTI)
    path_mask = os.path.join(out_dir, subject + '_T2w_peri_mask_rsfMRI.nii.gz')
    peri_mask, _ = read_mask(path_mask)

    # input rsfMRI file (NIfTI)
    path_rsfMRI = os.path.join(in_dir, 'regr', subject + '_mcf_f_SFRGR.nii.gz')
    if not os.path.isfile(path_rsfMRI):
        sys.exit("Error: '%s' is not a regular file." % (path_rsfMRI, ))

    # output ROIs files (NIfTI)
    #path_out_rois = os.path.join(out_dir, subject + '_seed_rois.nii.gz')
    #path_out_rois_1 = os.path.join(out_dir, subject + '_cortex_rois_1.nii.gz')
    #path_out_rois_2 = os.path.join(out_dir, subject + '_cortex_rois_2.nii.gz')
    #path_out_rois_x = os.path.join(out_dir, subject + '_seed_rois_mod.nii.gz')
    path_out_rois = os.path.join(out_dir, 'Seed_ROIs_all.nii.gz')
    path_out_rois_1 = os.path.join(out_dir, subject + '_rois_peri.nii.gz')
    path_out_rois_2 = os.path.join(out_dir, 'Seed_ROIs_peri.nii.gz')
    path_out_rois_x = os.path.join(out_dir, 'Seed_ROIs_all_mod_peri.nii.gz')

    # output time series text files
    #path_out_ts = os.path.join(out_dir, subject + '_ts.txt')
    #path_out_ts_2 = os.path.join(out_dir, subject + '_ts_cortex.txt')
    #path_out_ts_x = os.path.join(out_dir, subject + '_ts_mod.txt')
    path_out_ts = os.path.join(regr_dir, 'MasksTCsSplit_GV_all.txt')
    path_out_ts_2 = os.path.join(regr_dir, 'MasksTCsSplit_GV_peri.txt')
    path_out_ts_x = os.path.join(regr_dir, 'MasksTCsSplit_GV_all_mod_peri.txt')

    rois, rois_dims = create_rois_2(pt.path_labels,
                                    path_atlas,
                                    path_rois=path_out_rois)
    _, _ = create_rois_1(pt.path_labels_1,
                         path_atlas,
                         path_rois=path_out_rois_1,
                         mask=peri_mask)
    rois_2, _ = create_rois_2(pt.path_labels_2,
                              path_atlas,
                              path_rois=path_out_rois_2,
                              mask=peri_mask)

    rois_x = np.copy(rois)
    replace_rois(labels, labels_2, rois_x, rois_2)
    pt.save_data(rois_x, rois_dims, path_out_rois_x, dtype=None)

    mts.mean_ts(path_rsfMRI, path_out_rois, path_out_ts, label_names_2000)
    mts.mean_ts(path_rsfMRI, path_out_rois_2, path_out_ts_2, label_names_peri)
    mts.mean_ts(path_rsfMRI, path_out_rois_x, path_out_ts_x, label_names_2000)
示例#8
0
    # input labels text file with atlas index and seed regions (labels) in each line
    # Atlas (1 or 2), Label 1, Label 2, ...
    if len(args.in_labels) > 0: path_labels = args.in_labels
    if not os.path.isfile(path_labels):
        sys.exit("Error: '%s' is not a regular file." % (path_labels, ))

    # input atlas labels files (NIfTI)
    if len(args.in_atlas) > 0: list_atlas = args.in_atlas
    for path_atlas in list_atlas:
        if not os.path.isfile(path_atlas):
            sys.exit("Error: '%s' is not a regular file." % (path_atlas, ))

    # output seed ROIs file
    path_rois = os.path.join(
        os.getcwd(), args.out_rois) if args.out_rois != None else os.path.join(
            os.path.dirname(list_atlas[0]), file_name)

    # get date and time
    #print(pt.get_date())

    # create atlas labels hyperstack (4D)
    labels_hdr, rois = create_rois(path_labels,
                                   path_atlas,
                                   datatype=args.datatype,
                                   preserve=args.preserve)

    # save atlas labels file
    voxel_dims = labels_hdr[0].get_zooms()
    pt.save_data(rois, voxel_dims, path_rois, dtype=None)
示例#9
0
    if not os.path.isfile(args.in_data):
        sys.exit("Error: '%s' is not a regular file." % (args.in_data,))

    # input matrix text file
    if not os.path.isfile(args.in_matrix):
        sys.exit("Error: '%s' is not a regular file." % (args.in_matrix,))

    # output shape
    out_shape = '' if args.out_shape is None else args.out_shape

    # output voxel dimensions
    out_dims = '' if args.out_dims is None else args.out_dims

    # interpolation method (0: nearestneighbour, 1: trilinear)
    interp = 1 if args.interp is None else args.interp

    # read input data
    data, voxel_dims = pt.read_data(args.in_data)

    # read matrix text file
    matrix = make_matrix(pt.read_text(args.in_matrix))

    data_dims_xfm = tuple(int(v) for v in out_shape.split(',')) if len(out_shape) > 0 else data.shape[:]
    voxel_dims_xfm = tuple(float(v) for v in out_dims.split(',')) if len(out_dims) > 0 else voxel_dims[:]

    # transform input data
    data_xfm = xfm_serial(data, matrix, data_dims_xfm, voxel_dims_xfm, voxel_dims, interp=interp, inverse=args.inverse)

    # save transformed data as NIfTI file
    pt.save_data(data_xfm, voxel_dims_xfm, args.out_data, dtype=None)