示例#1
0
def run(opts):
    arg_perm_start = int(opts.range[0])
    arg_perm_stop = int(opts.range[1]) + 1
    medtype = str(opts.medtype[0])
    surface = str(opts.surface[0])

    #load variables
    y = np.load("python_temp_med_%s/merge_y.npy" % (surface))
    num_vertex = np.load("python_temp_med_%s/num_vertex.npy" % (surface))
    num_vertex_lh = np.load("python_temp_med_%s/num_vertex_lh.npy" % (surface))
    bin_mask_lh = np.load("python_temp_med_%s/bin_mask_lh.npy" % (surface))
    bin_mask_rh = np.load("python_temp_med_%s/bin_mask_rh.npy" % (surface))
    n = np.load("python_temp_med_%s/num_subjects.npy" % (surface))
    pred_x = np.load("python_temp_med_%s/pred_x.npy" % (surface))
    depend_y = np.load("python_temp_med_%s/depend_y.npy" % (surface))
    adjac_lh = np.load("python_temp_med_%s/adjac_lh.npy" % (surface))
    adjac_rh = np.load("python_temp_med_%s/adjac_rh.npy" % (surface))
    all_vertex = np.load("python_temp_med_%s/all_vertex.npy" % (surface))
    optstfce = np.load('python_temp_med_%s/optstfce.npy' % (surface))
    vdensity_lh = np.load('python_temp_med_%s/vdensity_lh.npy' % (surface))
    vdensity_rh = np.load('python_temp_med_%s/vdensity_rh.npy' % (surface))

    #load TFCE fucntion
    calcTFCE_lh = CreateAdjSet(float(optstfce[0]), float(optstfce[1]),
                               adjac_lh)  # H=2, E=1
    calcTFCE_rh = CreateAdjSet(float(optstfce[0]), float(optstfce[1]),
                               adjac_rh)  # H=2, E=1

    #permute Sobel Z
    if not os.path.exists("output_med_%s/perm_SobelZ_%s" % (surface, medtype)):
        os.mkdir("output_med_%s/perm_SobelZ_%s" % (surface, medtype))
    os.chdir("output_med_%s/perm_SobelZ_%s" % (surface, medtype))

    for iter_perm in range(arg_perm_start, arg_perm_stop):
        np.random.seed(int(iter_perm * 1000 + time()))
        print("Iteration number : %d" % (iter_perm))
        indices_perm = np.random.permutation(list(range(n)))
        if (medtype == 'M') or (medtype == 'I'):
            pathA_nx = pred_x[indices_perm]
            pathB_nx = depend_y
            SobelZ = calc_sobelz(medtype, pathA_nx, pathB_nx, y, n, num_vertex)
        else:
            pathA_nx = pred_x[indices_perm]
            pathB_nx = depend_y[indices_perm]
            SobelZ = calc_sobelz(medtype, pathA_nx, pathB_nx, y, n, num_vertex)
        write_perm_maxTFCE_vertex("Zstat_%s" % medtype, SobelZ, num_vertex_lh,
                                  bin_mask_lh, bin_mask_rh, calcTFCE_lh,
                                  calcTFCE_rh, vdensity_lh, vdensity_rh)
    print(
        ("Finished. Randomization took %.1f seconds" % (time() - start_time)))
def run(opts):
    arg_predictor = opts.input[0]
    arg_depend = opts.input[1]
    medtype = opts.medtype[0]

    if not os.path.exists("python_temp"):
        print("python_temp missing!")

    #load variables
    raw_nonzero = np.load('python_temp/raw_nonzero.npy')
    n = raw_nonzero.shape[1]
    affine_mask = np.load('python_temp/affine_mask.npy')
    data_mask = np.load('python_temp/data_mask.npy')
    data_index = data_mask > 0.99
    num_voxel = np.load('python_temp/num_voxel.npy')
    pred_x = np.genfromtxt(arg_predictor, delimiter=",")
    depend_y = np.genfromtxt(arg_depend, delimiter=",")

    imgext = '.nii.gz'  #default save type is nii.gz
    #	if not os.path.isfile('python_temp/imgext.npy'): # to maintain compability
    #		imgext = '.nii.gz'
    #	else:
    #		imgext = np.load('python_temp/imgext.npy')

    #TFCE
    adjac = create_adjac_voxel(data_index,
                               data_mask,
                               num_voxel,
                               dirtype=opts.tfce[2])
    calcTFCE = CreateAdjSet(
        float(opts.tfce[0]), float(opts.tfce[1]),
        adjac)  # i.e. default: H=2, E=2, 26 neighbour connectivity

    #step1
    if opts.covariates:
        arg_covars = opts.covariates[0]
        covars = np.genfromtxt(arg_covars, delimiter=",")
        x_covars = np.column_stack([np.ones(n), covars])
        y = resid_covars(x_covars, raw_nonzero)
    else:
        y = raw_nonzero.T

    #save
    np.save('python_temp/pred_x', pred_x)
    np.save('python_temp/depend_y', depend_y)
    np.save('python_temp/adjac', adjac)
    np.save('python_temp/medtype', medtype)
    np.save('python_temp/optstfce', opts.tfce)
    np.save('python_temp/raw_nonzero_corr', y.T.astype(np.float32, order="C"))

    #step2 mediation
    SobelZ = calc_sobelz(medtype, pred_x, depend_y, y, n, num_voxel)

    #write TFCE images
    if not os.path.exists("output_med_%s" % medtype):
        os.mkdir("output_med_%s" % medtype)
    os.chdir("output_med_%s" % medtype)
    write_voxelStat_img('SobelZ_%s' % medtype, SobelZ, data_mask, data_index,
                        affine_mask, calcTFCE, imgext)
示例#3
0
def calculate_mediation_tfce(medtype,
                             merge_y,
                             masking_array,
                             pred_x,
                             depend_y,
                             calcTFCE,
                             vdensity,
                             position_array,
                             fullmask,
                             perm_number=None,
                             randomise=False,
                             verbose=False,
                             no_intercept=True):
    n = len(pred_x)
    if randomise:
        np.random.seed(perm_number + int(float(str(time())[-6:]) * 100))
        indices_perm = np.random.permutation(range(merge_y.shape[0]))
    if (medtype == 'M') or (medtype == 'I'):
        if randomise:
            pathA_nx = pred_x[indices_perm]
    else:
        if randomise:
            pathA_nx = pred_x[indices_perm]
            pathB_nx = depend_y[indices_perm]
    SobelZ = calc_sobelz(medtype, pred_x, depend_y, merge_y, merge_y.shape[0],
                         merge_y.shape[1])
    SobelZ = SobelZ.astype(np.float32, order="C")
    tfce_SobelZ = np.zeros_like(SobelZ).astype(np.float32, order="C")
    zval_temp = np.zeros_like((fullmask)).astype(np.float32, order="C")
    zval_temp[fullmask == 1] = SobelZ
    zval_temp = zval_temp.astype(np.float32, order="C")
    tfce_temp = np.zeros_like(zval_temp).astype(np.float32, order="C")
    calcTFCE.run(zval_temp, tfce_temp)
    zval_temp = zval_temp[fullmask == 1]
    tfce_temp = tfce_temp[fullmask == 1]
    for surf_count in range(len(masking_array)):
        start = position_array[surf_count]
        end = position_array[surf_count + 1]
        tfce_SobelZ[:, start:end] = (tfce_temp[start:end] *
                                     (zval_temp[start:end].max() / 100) *
                                     vdensity[start:end])
        if randomise:
            os.system("echo %f >> perm_maxTFCE_surf%d_zstat.csv" %
                      (np.nanmax(tfce_SobelZ[:, start:end]), surf_count))
    if verbose:
        print "Max Zstat tfce from all surfaces = %f" % tfce_SobelZ.max()
    if randomise:
        print "Interation number: %d" % perm_number
        os.system("echo %s >> perm_maxTFCE_allsurf_zstat.csv" %
                  (','.join(["%0.2f" % i for i in tfce_SobelZ.max(axis=1)])))
        SobelZ = None
        tfce_SobelZ = None
    zval_temp = None
    tfce_temp = None
    del calcTFCE
    if not randomise:
        return (SobelZ.astype(np.float32, order="C"),
                tfce_SobelZ.astype(np.float32, order="C"))
示例#4
0
def run(opts):
    arg_perm_start = int(opts.range[0])
    arg_perm_stop = int(opts.range[1]) + 1
    medtype = str(opts.medtype[0])

    #load variables
    num_voxel = np.load('python_temp/num_voxel.npy')
    n = np.load('python_temp/num_subjects.npy')
    ny = np.load('python_temp/raw_nonzero_corr.npy').T
    pred_x = np.load('python_temp/pred_x.npy')
    depend_y = np.load("python_temp/depend_y.npy")
    adjac = np.load('python_temp/adjac.npy')
    optstfce = np.load('python_temp/optstfce.npy')

    #load TFCE fucntion
    calcTFCE = CreateAdjSet(float(optstfce[0]), float(optstfce[1]),
                            adjac)  # H=2, E=2, 26 neighbour connectivity

    #permute Sobel Z values and write max TFCE values
    if not os.path.exists("output_med_%s/perm_SobelZ" % medtype):
        os.mkdir("output_med_%s/perm_SobelZ" % medtype)
    os.chdir("output_med_%s/perm_SobelZ" % medtype)

    for iter_perm in xrange(arg_perm_start, arg_perm_stop):
        np.random.seed(int(iter_perm * 1000 + time()))
        print "Iteration number : %d" % (iter_perm)
        indices_perm = np.random.permutation(range(n))
        if (medtype == 'M') or (medtype == 'I'):
            pathA_nx = pred_x[indices_perm]
            pathB_nx = depend_y
            SobelZ = calc_sobelz(medtype, pathA_nx, pathB_nx, ny, n, num_voxel)
        else:
            pathA_nx = pred_x[indices_perm]
            pathB_nx = depend_y[indices_perm]
            SobelZ = calc_sobelz(medtype, pathA_nx, pathB_nx, ny, n, num_voxel)
        write_perm_maxTFCE_voxel('Zstat_%s' % medtype, SobelZ, calcTFCE)
    print("Finished. Randomization took %.1f seconds" % (time() - start_time))
示例#5
0
def run(opts):
    scriptwd = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    arg_predictor = opts.input[0]
    arg_depend = opts.input[1]
    surface = opts.surface[0]
    medtype = opts.medtype[0]
    FWHM = opts.fwhm[0]

    #load variables
    pred_x = np.genfromtxt(arg_predictor, delimiter=",")
    depend_y = np.genfromtxt(arg_depend, delimiter=",")

    #load data
    img_data_lh = nib.freesurfer.mghformat.load("lh.all.%s.%s.mgh" %
                                                (surface, FWHM))
    data_full_lh = img_data_lh.get_data()
    data_lh = np.squeeze(data_full_lh)
    affine_mask_lh = img_data_lh.get_affine()
    n = data_lh.shape[1]
    outdata_mask_lh = np.zeros_like(data_full_lh[:, :, :, 1])
    img_data_rh = nib.freesurfer.mghformat.load("rh.all.%s.%s.mgh" %
                                                (surface, FWHM))
    data_full_rh = img_data_rh.get_data()
    data_rh = np.squeeze(data_full_rh)
    affine_mask_rh = img_data_rh.get_affine()
    outdata_mask_rh = np.zeros_like(data_full_rh[:, :, :, 1])
    if not os.path.exists("lh.mean.%s.%s.mgh" % (surface, FWHM)):
        mean_lh = np.sum(data_lh, axis=1) / data_lh.shape[1]
        outmean_lh = np.zeros_like(data_full_lh[:, :, :, 1])
        outmean_lh[:, 0, 0] = mean_lh
        nib.save(nib.freesurfer.mghformat.MGHImage(outmean_lh, affine_mask_lh),
                 "lh.mean.%s.%s.mgh" % (surface, FWHM))
        mean_rh = np.sum(data_rh, axis=1) / data_rh.shape[1]
        outmean_rh = np.zeros_like(data_full_rh[:, :, :, 1])
        outmean_rh[:, 0, 0] = mean_rh
        nib.save(nib.freesurfer.mghformat.MGHImage(outmean_rh, affine_mask_rh),
                 "rh.mean.%s.%s.mgh" % (surface, FWHM))
    else:
        img_mean_lh = nib.freesurfer.mghformat.load("lh.mean.%s.%s.mgh" %
                                                    (surface, FWHM))
        mean_full_lh = img_mean_lh.get_data()
        mean_lh = np.squeeze(mean_full_lh)
        img_mean_rh = nib.freesurfer.mghformat.load("rh.mean.%s.%s.mgh" %
                                                    (surface, FWHM))
        mean_full_rh = img_mean_rh.get_data()
        mean_rh = np.squeeze(mean_full_rh)

    #create masks
    if opts.fmri:
        maskthresh = opts.fmri
        print("fMRI threshold mask = %2.2f" % maskthresh)
        bin_mask_lh = np.logical_or(mean_lh > maskthresh, mean_lh <
                                    (-1 * maskthresh))
        bin_mask_rh = np.logical_or(mean_rh > maskthresh, mean_rh <
                                    (-1 * maskthresh))

    elif opts.fsmask:
        label = opts.fsmask
        print("Loading fsaverage ?l.%s.label" % label)

        index_lh, _, _ = convert_fslabel("%s/fsaverage/label/lh.%s.label" %
                                         (os.environ["SUBJECTS_DIR"], label))
        index_rh, _, _ = convert_fslabel("%s/fsaverage/label/rh.%s.label" %
                                         (os.environ["SUBJECTS_DIR"], label))

        bin_mask_lh = np.zeros_like(mean_lh)
        bin_mask_lh[index_lh] = 1
        bin_mask_lh = bin_mask_lh.astype(bool)

        bin_mask_rh = np.zeros_like(mean_rh)
        bin_mask_rh[index_rh] = 1
        bin_mask_rh = bin_mask_rh.astype(bool)

    elif opts.label:
        label_lh = opts.label[0]
        label_rh = opts.label[1]

        index_lh, _, _ = convert_fslabel(label_lh)
        index_rh, _, _ = convert_fslabel(label_rh)

        bin_mask_lh = np.zeros_like(mean_lh)
        bin_mask_lh[index_lh] = 1
        bin_mask_lh = bin_mask_lh.astype(bool)

        bin_mask_rh = np.zeros_like(mean_rh)
        bin_mask_rh[index_rh] = 1
        bin_mask_rh = bin_mask_rh.astype(bool)
    elif opts.binmask:
        print("Loading masks")
        img_binmgh_lh = nib.freesurfer.mghformat.load(opts.binmask[0])
        binmgh_lh = img_binmgh_lh.get_data()
        binmgh_lh = np.squeeze(binmgh_lh)
        img_binmgh_rh = nib.freesurfer.mghformat.load(opts.binmask[1])
        binmgh_rh = img_binmgh_rh.get_data()
        binmgh_rh = np.squeeze(binmgh_rh)
        bin_mask_lh = binmgh_lh > .99
        bin_mask_rh = binmgh_rh > .99
    else:
        bin_mask_lh = mean_lh > 0
        bin_mask_rh = mean_rh > 0
    data_lh = data_lh[bin_mask_lh]
    num_vertex_lh = data_lh.shape[0]
    data_rh = data_rh[bin_mask_rh]
    num_vertex_rh = data_rh.shape[0]
    num_vertex = num_vertex_lh + num_vertex_rh
    all_vertex = data_full_lh.shape[0]

    #TFCE
    if opts.triangularmesh:
        print "Creating adjacency set"
        if opts.inputsurfs:
            # 3 Neighbour vertex connectity
            v_lh, faces_lh = nib.freesurfer.read_geometry(opts.inputsurfs[0])
            v_rh, faces_rh = nib.freesurfer.read_geometry(opts.inputsurfs[1])
        else:
            v_lh, faces_lh = nib.freesurfer.read_geometry(
                "%s/fsaverage/surf/lh.sphere" % os.environ["SUBJECTS_DIR"])
            v_rh, faces_rh = nib.freesurfer.read_geometry(
                "%s/fsaverage/surf/rh.sphere" % os.environ["SUBJECTS_DIR"])
        adjac_lh = create_adjac_vertex(v_lh, faces_lh)
        adjac_rh = create_adjac_vertex(v_rh, faces_rh)
    elif opts.adjfiles:
        print "Loading prior adjacency set"
        arg_adjac_lh = opts.adjfiles[0]
        arg_adjac_rh = opts.adjfiles[1]
        adjac_lh = np.load(arg_adjac_lh)
        adjac_rh = np.load(arg_adjac_rh)
    elif opts.dist:
        print "Loading prior adjacency set for %s mm" % opts.dist[0]
        adjac_lh = np.load("%s/adjacency_sets/lh_adjacency_dist_%s.0_mm.npy" %
                           (scriptwd, str(opts.dist[0])))
        adjac_rh = np.load("%s/adjacency_sets/rh_adjacency_dist_%s.0_mm.npy" %
                           (scriptwd, str(opts.dist[0])))
    else:
        print "Error"
    if opts.noweight:
        vdensity_lh = 1
        vdensity_rh = 1
    else:
        # correction for vertex density
        vdensity_lh = np.zeros((adjac_lh.shape[0]))
        vdensity_rh = np.zeros((adjac_rh.shape[0]))
        for i in xrange(adjac_lh.shape[0]):
            vdensity_lh[i] = len(adjac_lh[i])
        for j in xrange(adjac_rh.shape[0]):
            vdensity_rh[j] = len(adjac_rh[j])
        vdensity_lh = np.array((1 - (vdensity_lh / vdensity_lh.max()) +
                                (vdensity_lh.mean() / vdensity_lh.max())),
                               dtype=np.float32)
        vdensity_rh = np.array((1 - (vdensity_rh / vdensity_rh.max()) +
                                (vdensity_rh.mean() / vdensity_rh.max())),
                               dtype=np.float32)
    calcTFCE_lh = CreateAdjSet(float(opts.tfce[0]), float(opts.tfce[1]),
                               adjac_lh)
    calcTFCE_rh = CreateAdjSet(float(opts.tfce[0]), float(opts.tfce[1]),
                               adjac_rh)

    #save variables
    if not os.path.exists("python_temp_med_%s" % surface):
        os.mkdir("python_temp_med_%s" % surface)

    np.save("python_temp_med_%s/pred_x" % surface, pred_x)
    np.save("python_temp_med_%s/depend_y" % surface, depend_y)
    np.save("python_temp_med_%s/num_subjects" % surface, n)
    np.save("python_temp_med_%s/num_vertex" % surface, num_vertex)
    np.save("python_temp_med_%s/num_vertex_lh" % (surface), num_vertex_lh)
    np.save("python_temp_med_%s/num_vertex_rh" % (surface), num_vertex_rh)
    np.save("python_temp_med_%s/all_vertex" % (surface), all_vertex)
    np.save("python_temp_med_%s/bin_mask_lh" % (surface), bin_mask_lh)
    np.save("python_temp_med_%s/bin_mask_rh" % (surface), bin_mask_rh)
    np.save("python_temp_med_%s/adjac_lh" % (surface), adjac_lh)
    np.save("python_temp_med_%s/adjac_rh" % (surface), adjac_rh)
    np.save("python_temp_med_%s/optstfce" % (surface), opts.tfce)
    np.save('python_temp_med_%s/vdensity_lh' % (surface), vdensity_lh)
    np.save('python_temp_med_%s/vdensity_rh' % (surface), vdensity_rh)

    #step1
    if opts.covariates:
        arg_covars = opts.covariates[0]
        covars = np.genfromtxt(arg_covars, delimiter=",")
        x_covars = np.column_stack([np.ones(n), covars])
        y_lh = resid_covars(x_covars, data_lh)
        y_rh = resid_covars(x_covars, data_rh)
        merge_y = np.hstack((y_lh, y_rh))
        del y_lh
        del y_rh
    else:
        #no covariates
        merge_y = np.hstack((data_lh.T, data_rh.T))
    del data_lh
    del data_rh
    np.save("python_temp_med_%s/merge_y" % (surface),
            merge_y.astype(np.float32, order="C"))

    #step2 mediation
    SobelZ = calc_sobelz(medtype, pred_x, depend_y, merge_y, n, num_vertex)

    #write TFCE images
    if not os.path.exists("output_med_%s" % surface):
        os.mkdir("output_med_%s" % surface)
    os.chdir("output_med_%s" % surface)

    write_vertStat_img('SobelZ_%s' % (medtype), SobelZ[:num_vertex_lh],
                       outdata_mask_lh, affine_mask_lh, surface, 'lh',
                       bin_mask_lh, calcTFCE_lh, bin_mask_lh.shape[0],
                       vdensity_lh)
    write_vertStat_img('SobelZ_%s' % (medtype), SobelZ[num_vertex_lh:],
                       outdata_mask_rh, affine_mask_rh, surface, 'rh',
                       bin_mask_rh, calcTFCE_rh, bin_mask_rh.shape[0],
                       vdensity_rh)