def run(opts):

    scriptwd = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    surface = opts.surface[0]
    FWHM = opts.fwhm[0]

    #load surface 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]  # num_subjects
    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)

    #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 or opts.triangularmesh:
        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 range(adjac_lh.shape[0]):
            vdensity_lh[i] = len(adjac_lh[i])
        for j in range(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)

    #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")
        binmgh_lh = np.squeeze(
            nib.freesurfer.mghformat.load(opts.binmask[0]).get_data())
        binmgh_rh = np.squeeze(
            nib.freesurfer.mghformat.load(opts.binmask[1]).get_data())
        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]

    if opts.input:
        #load variables
        arg_predictor = opts.input[0]
        arg_covars = opts.input[1]
        pred_x = np.genfromtxt(arg_predictor, delimiter=',')
        covars = np.genfromtxt(arg_covars, delimiter=',')
        #step1
        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
    if opts.regressors:
        arg_predictor = opts.regressors[0]
        pred_x = np.genfromtxt(arg_predictor, delimiter=',')
        merge_y = np.hstack((data_lh.T, data_rh.T))

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

    np.save("python_temp_%s/pred_x" % (surface), pred_x)
    np.save("python_temp_%s/num_subjects" % (surface), n)
    np.save("python_temp_%s/all_vertex" % (surface), all_vertex)
    np.save("python_temp_%s/num_vertex" % (surface), num_vertex)
    np.save("python_temp_%s/num_vertex_lh" % (surface), num_vertex_lh)
    np.save("python_temp_%s/num_vertex_rh" % (surface), num_vertex_rh)
    np.save("python_temp_%s/bin_mask_lh" % (surface), bin_mask_lh)
    np.save("python_temp_%s/bin_mask_rh" % (surface), bin_mask_rh)
    np.save("python_temp_%s/affine_mask_lh" % (surface), affine_mask_lh)
    np.save("python_temp_%s/affine_mask_rh" % (surface), affine_mask_rh)
    np.save("python_temp_%s/adjac_lh" % (surface), adjac_lh)
    np.save("python_temp_%s/adjac_rh" % (surface), adjac_rh)
    np.save("python_temp_%s/merge_y" % (surface),
            merge_y.astype(np.float32, order="C"))
    np.save('python_temp_%s/optstfce' % (surface), opts.tfce)
    np.save('python_temp_%s/vdensity_lh' % (surface), vdensity_lh)
    np.save('python_temp_%s/vdensity_rh' % (surface), vdensity_rh)

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

    #step2
    X = np.column_stack([np.ones(n), pred_x])
    k = len(X.T)
    if opts.vertexregressor:
        img_x_lh = np.squeeze(
            nib.freesurfer.mghformat.load("../%s" %
                                          opts.vertexregressor[0]).get_data())
        img_x_rh = np.squeeze(
            nib.freesurfer.mghformat.load("../%s" %
                                          opts.vertexregressor[1]).get_data())
        img_x_lh = img_x_lh[bin_mask_lh]
        img_x_rh = img_x_rh[bin_mask_rh]
        img_x = np.hstack((img_x_lh.T, img_x_rh.T))
        img_x_lh = img_x_rh = None
        merge_y = np.hstack((data_lh.T, data_rh.T))
        tvals, timage = image_regression(merge_y.T.astype(np.float32),
                                         img_x.T.astype(np.float32), pred_x,
                                         covars)
        VIF = image_reg_VIF(merge_y, np.column_stack((pred_x, covars)))

        tvals = tvals.T
        timage = timage.T
        write_vertStat_img('tstat_imgcovar', timage[: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('tstat_imgcovar', timage[num_vertex_lh:],
                           outdata_mask_rh, affine_mask_rh, surface, 'rh',
                           bin_mask_rh, calcTFCE_rh, bin_mask_rh.shape[0],
                           vdensity_rh)
        write_vertStat_img('negtstat_imgcovar', -timage[: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('negtstat_imgcovar', -timage[num_vertex_lh:],
                           outdata_mask_rh, affine_mask_rh, surface, 'rh',
                           bin_mask_rh, calcTFCE_rh, bin_mask_rh.shape[0],
                           vdensity_rh)
        write_vertStat_img('VIF_imgcovar',
                           VIF[:num_vertex_lh],
                           outdata_mask_lh,
                           affine_mask_lh,
                           surface,
                           'lh',
                           bin_mask_lh,
                           calcTFCE_lh,
                           bin_mask_lh.shape[0],
                           vdensity_lh,
                           TFCE=False)
        write_vertStat_img('VIF_imgcovar',
                           VIF[num_vertex_lh:],
                           outdata_mask_rh,
                           affine_mask_rh,
                           surface,
                           'rh',
                           bin_mask_rh,
                           calcTFCE_rh,
                           bin_mask_rh.shape[0],
                           vdensity_rh,
                           TFCE=False)

    else:
        invXX = np.linalg.inv(np.dot(X.T, X))
        tvals = tval_int(X, invXX, merge_y, n, k, num_vertex)

    for j in range(k - 1):
        tnum = j + 1
        write_vertStat_img('tstat_con%d' % tnum, tvals[tnum, :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('tstat_con%d' % tnum, tvals[tnum, num_vertex_lh:],
                           outdata_mask_rh, affine_mask_rh, surface, 'rh',
                           bin_mask_rh, calcTFCE_rh, bin_mask_rh.shape[0],
                           vdensity_rh)
        write_vertStat_img('negtstat_con%d' % tnum,
                           (tvals[tnum, :num_vertex_lh] * -1), outdata_mask_lh,
                           affine_mask_lh, surface, 'lh', bin_mask_lh,
                           calcTFCE_lh, bin_mask_lh.shape[0], vdensity_lh)
        write_vertStat_img('negtstat_con%d' % tnum,
                           (tvals[tnum, num_vertex_lh:] * -1), outdata_mask_rh,
                           affine_mask_rh, surface, 'rh', bin_mask_rh,
                           calcTFCE_rh, bin_mask_rh.shape[0], vdensity_rh)
def run(opts):
    arg_perm_start = int(opts.range[0])
    arg_perm_stop = int(opts.range[1]) + 1
    surface = str(opts.surface[0])
    if opts.exchangeblock:
        block_list = np.genfromtxt(opts.exchangeblock[0], dtype=np.str)
        indexer = np.array(range(len(block_list)))

    #load variables
    ny = np.load("python_temp_%s/merge_y.npy" % (surface))
    num_vertex = np.load("python_temp_%s/num_vertex.npy" % (surface))
    num_vertex_lh = np.load("python_temp_%s/num_vertex_lh.npy" % (surface))
    all_vertex = np.load("python_temp_%s/all_vertex.npy" % (surface))
    bin_mask_lh = np.load("python_temp_%s/bin_mask_lh.npy" % (surface))
    bin_mask_rh = np.load("python_temp_%s/bin_mask_rh.npy" % (surface))
    n = np.load("python_temp_%s/num_subjects.npy" % (surface))
    pred_x = np.load("python_temp_%s/pred_x.npy" % (surface))
    adjac_lh = np.load("python_temp_%s/adjac_lh.npy" % (surface))
    adjac_rh = np.load("python_temp_%s/adjac_rh.npy" % (surface))
    optstfce = np.load('python_temp_%s/optstfce.npy' % (surface))
    vdensity_lh = np.load('python_temp_%s/vdensity_lh.npy' % (surface))
    vdensity_rh = np.load('python_temp_%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 T values and write max TFCE values
    if not os.path.exists("output_%s/perm_Tstat_%s" % (surface, surface)):
        os.mkdir("output_%s/perm_Tstat_%s" % (surface, surface))
    os.chdir("output_%s/perm_Tstat_%s" % (surface, surface))

    X = np.column_stack([np.ones(n), pred_x])
    k = len(X.T)
    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))
        if opts.specifyvars:
            start = opts.specifyvars[0]
            stop = opts.specifyvars[1] + 1
            nx = X
            nx[:, start:stop] = X[:, start:stop][np.random.permutation(
                list(range(n)))]
        elif opts.exchangeblock:
            randindex = []
            for block in np.random.permutation(list(np.unique(block_list))):
                randindex.append(
                    np.random.permutation(indexer[block_list == block]))
            randindex = np.concatenate(np.array(randindex))
            nx = X[randindex]
        else:
            nx = X[np.random.permutation(list(range(n)))]
        invXX = np.linalg.inv(np.dot(nx.T, nx))
        tvals = tval_int(nx, invXX, ny, n, k, num_vertex)
        if opts.specifyvars:
            for j in range(stop - start):
                tnum = j + 1
                write_perm_maxTFCE_vertex('tstat_con%d' % tnum, tvals[tnum],
                                          num_vertex_lh, bin_mask_lh,
                                          bin_mask_rh, calcTFCE_lh,
                                          calcTFCE_rh, vdensity_lh,
                                          vdensity_rh)
                write_perm_maxTFCE_vertex('tstat_con%d' % tnum,
                                          (tvals[tnum] * -1), num_vertex_lh,
                                          bin_mask_lh, bin_mask_rh,
                                          calcTFCE_lh, calcTFCE_rh,
                                          vdensity_lh, vdensity_rh)
        else:
            for j in range(k - 1):
                tnum = j + 1
                write_perm_maxTFCE_vertex('tstat_con%d' % tnum, tvals[tnum],
                                          num_vertex_lh, bin_mask_lh,
                                          bin_mask_rh, calcTFCE_lh,
                                          calcTFCE_rh, vdensity_lh,
                                          vdensity_rh)
                write_perm_maxTFCE_vertex('tstat_con%d' % tnum,
                                          (tvals[tnum] * -1), 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)))
Esempio n. 3
0
def run(opts):
    if not os.path.exists("python_temp"):
        print("python_temp missing!")

    #load variables
    raw_nonzero = np.load('python_temp/raw_nonzero.npy')
    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')
    n = raw_nonzero.shape[1]

    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')

    #step1
    if opts.input:
        pred_x = np.genfromtxt(opts.input[0], delimiter=',')
        covars = np.genfromtxt(opts.input[1], delimiter=',')
        x_covars = np.column_stack([np.ones(n), covars])
        y = resid_covars(x_covars, raw_nonzero)
        np.save('python_temp/covars', covars)
    if opts.regressors:
        pred_x = np.genfromtxt(opts.regressors[0], delimiter=',')
        y = raw_nonzero.T
    if opts.onesample:
        pred_x = np.ones(n)
        pred_x[:int(n / 2)] = -1
        if opts.onesample[0] != 'none':
            covars = np.genfromtxt(opts.onesample[0], delimiter=',')
            x_covars = np.column_stack([np.ones(n), covars])
            y = resid_covars(x_covars, raw_nonzero)
            np.save('python_temp/covars', covars)
        else:
            y = raw_nonzero.T

    ancova = 0
    if opts.ftest:
        ancova = 1

    #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)  # H=2, E=2, 26 neighbour connectivity

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

    if not os.path.exists('output'):
        os.mkdir('output')
    os.chdir('output')
    X = np.column_stack([np.ones(n), pred_x])
    k = len(X.T)

    if opts.onesample:
        if opts.onesample[0] == 'none':
            tvalues, _ = stats.ttest_1samp(raw_nonzero, 0, axis=1)
            write_voxelStat_img('tstat_intercept', tvalues, data_mask,
                                data_index, affine_mask, calcTFCE, imgext)
            write_voxelStat_img('negtstat_intercept', (tvalues * -1),
                                data_mask, data_index, affine_mask, calcTFCE,
                                imgext)
        else:
            tvalues = tval_int(x_covars,
                               np.linalg.inv(np.dot(x_covars.T, x_covars)),
                               raw_nonzero.T, n, len(x_covars.T), num_voxel)
            tvalues = tvalues[0]
            write_voxelStat_img('tstat_intercept', tvalues, data_mask,
                                data_index, affine_mask, calcTFCE, imgext)
            write_voxelStat_img('negtstat_intercept', (tvalues * -1),
                                data_mask, data_index, affine_mask, calcTFCE,
                                imgext)
        exit()

    if ancova == 0:

        if opts.voxelregressor:

            img_all_name = opts.voxelregressor[0]
            _, file_ext = os.path.splitext(img_all_name)
            if file_ext == '.gz':
                _, file_ext = os.path.splitext(img_all_name)
                if file_ext == '.mnc':
                    imgext = '.mnc'
                    image_x = nib.load(
                        '../%s' % img_all_name).get_data()[data_index].astype(
                            np.float32)
                else:
                    imgext = '.nii.gz'
                    os.system("zcat ../%s > temp_4d.nii" % img_all_name)
                    image_x = nib.load(
                        'temp_4d.nii').get_data()[data_index].astype(
                            np.float32)
                    os.system("rm temp_4d.nii")
            elif file_ext == '.nii':
                imgext = '.nii.gz'  # default to zipped images
                image_x = nib.load('../%s' %
                                   img_all_name).get_data()[data_index].astype(
                                       np.float32)
            else:
                print('Error filetype for %s is not supported' % img_all_name)
                quit()

            tvalues, timage = image_regression(raw_nonzero.astype(np.float32),
                                               image_x, pred_x, covars)
            write_voxelStat_img('tstat_imgcovar', timage, data_mask,
                                data_index, affine_mask, calcTFCE, imgext)
            write_voxelStat_img('negtstat_imgcovar', -timage, data_mask,
                                data_index, affine_mask, calcTFCE, imgext)
            tvalues = tvalues.T
            VIF = image_reg_VIF(image_x.T, np.column_stack((pred_x, covars)))
            write_voxelStat_img('VIF_imgcovar',
                                VIF,
                                data_mask,
                                data_index,
                                affine_mask,
                                calcTFCE,
                                imgext,
                                TFCE=False)
        else:
            #multiple regression
            invXX = np.linalg.inv(np.dot(X.T, X))
            tvalues = tval_int(X, invXX, y, n, k, num_voxel)
        tvalues[np.isnan(tvalues)] = 0  #only necessary for ANTS skeleton
        #write TFCE images
        for j in range(k - 1):
            tnum = j + 1
            write_voxelStat_img('tstat_con%d' % tnum, tvalues[tnum], data_mask,
                                data_index, affine_mask, calcTFCE, imgext)
            write_voxelStat_img('negtstat_con%d' % tnum, (tvalues[tnum] * -1),
                                data_mask, data_index, affine_mask, calcTFCE,
                                imgext)
    elif ancova == 1:
        #anova
        fvals = calcF(X, y, n, k)  # sqrt to approximate the t-distribution
        fvals[fvals < 0] = 0
        write_voxelStat_img('fstat', np.sqrt(fvals), data_mask, data_index,
                            affine_mask, calcTFCE, imgext)
    else:
        print("Error")
        exit()
Esempio n. 4
0
def run(opts):
    arg_perm_start = int(opts.range[0])
    arg_perm_stop = int(opts.range[1]) + 1
    if opts.exchangeblock:
        block_list = np.genfromtxt(opts.exchangeblock[0], dtype=np.str)
        indexer = np.array(range(len(block_list)))

    np.seterr(divide="ignore",
              invalid="ignore")  #only necessary for ANTS skeleton

    #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')
    adjac = np.load('python_temp/adjac.npy')
    ancova = np.load('python_temp/ancova.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 T values and write max TFCE values
    if not os.path.exists('output/perm_Tstat'):
        os.mkdir('output/perm_Tstat')
    os.chdir('output/perm_Tstat')

    X = np.column_stack([np.ones(n), pred_x])
    k = len(X.T)
    if ancova == 1:
        for iter_perm in range(arg_perm_start,
                               int((arg_perm_stop - 1) * 2 + 1)):
            np.random.seed(int(iter_perm * 1000 + time()))
            print("Permutation number: %d" % (iter_perm))
            nx = X[np.random.permutation(list(range(n)))]
            perm_fvals = calcF(nx, ny, n, k)
            perm_fvals[perm_fvals < 0] = 0
            perm_fvals = np.sqrt(perm_fvals)
            print(perm_fvals.max())
            print(perm_fvals.min())
            write_perm_maxTFCE_voxel('fstat', perm_fvals, calcTFCE)
    else:
        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))
            if opts.specifyvars:
                start = opts.specifyvars[0]
                stop = opts.specifyvars[1] + 1
                nx = X
                nx[:, start:stop] = X[:, start:stop][np.random.permutation(
                    list(range(n)))]
            elif opts.exchangeblock:
                randindex = []
                for block in np.random.permutation(list(
                        np.unique(block_list))):
                    randindex.append(
                        np.random.permutation(indexer[block_list == block]))
                randindex = np.concatenate(np.array(randindex))
                nx = X[randindex]
            else:
                nx = X[np.random.permutation(list(range(n)))]
            invXX = np.linalg.inv(np.dot(nx.T, nx))
            perm_tvalues = tval_int(nx, invXX, ny, n, k, num_voxel)
            perm_tvalues[np.isnan(
                perm_tvalues)] = 0  #only necessary for ANTS skeleton
            if opts.specifyvars:
                for j in range(stop - start):
                    tnum = j + 1
                    write_perm_maxTFCE_voxel('tstat_con%d' % tnum,
                                             perm_tvalues[tnum], calcTFCE)
                    write_perm_maxTFCE_voxel('tstat_con%d' % tnum,
                                             (perm_tvalues[tnum] * -1),
                                             calcTFCE)
            else:
                for j in range(k - 1):
                    tnum = j + 1
                    write_perm_maxTFCE_voxel('tstat_con%d' % tnum,
                                             perm_tvalues[tnum], calcTFCE)
                    write_perm_maxTFCE_voxel('tstat_con%d' % tnum,
                                             (perm_tvalues[tnum] * -1),
                                             calcTFCE)
    print(
        ("Finished. Randomization took %.1f seconds" % (time() - start_time)))
Esempio n. 5
0
def calculate_tfce(merge_y,
                   masking_array,
                   pred_x,
                   calcTFCE,
                   vdensity,
                   position_array,
                   fullmask,
                   perm_number=None,
                   randomise=False,
                   verbose=False,
                   no_intercept=True):
    X = np.column_stack([np.ones(merge_y.shape[0]), pred_x])
    if randomise:
        np.random.seed(perm_number + int(float(str(time())[-6:]) * 100))
        X = X[np.random.permutation(range(merge_y.shape[0]))]
    k = len(X.T)
    invXX = np.linalg.inv(np.dot(X.T, X))
    tvals = tval_int(X, invXX, merge_y, merge_y.shape[0], k, merge_y.shape[1])
    if no_intercept:
        tvals = tvals[1:, :]
    tvals = tvals.astype(np.float32, order="C")
    tfce_tvals = np.zeros_like(tvals).astype(np.float32, order="C")
    neg_tfce_tvals = np.zeros_like(tvals).astype(np.float32, order="C")

    for tstat_counter in range(tvals.shape[0]):
        tval_temp = np.zeros_like((fullmask)).astype(np.float32, order="C")
        if tvals.shape[0] == 1:
            tval_temp[fullmask == 1] = tvals[0]
        else:
            tval_temp[fullmask == 1] = tvals[tstat_counter]
        tval_temp = tval_temp.astype(np.float32, order="C")
        tfce_temp = np.zeros_like(tval_temp).astype(np.float32, order="C")
        neg_tfce_temp = np.zeros_like(tval_temp).astype(np.float32, order="C")
        calcTFCE.run(tval_temp, tfce_temp)
        calcTFCE.run((tval_temp * -1), neg_tfce_temp)
        tval_temp = tval_temp[fullmask == 1]
        tfce_temp = tfce_temp[fullmask == 1]
        neg_tfce_temp = neg_tfce_temp[fullmask == 1]
        #		position = 0
        for surf_count in range(len(masking_array)):
            start = position_array[surf_count]
            end = position_array[surf_count + 1]
            if isinstance(vdensity, int):  # check vdensity is a scalar
                tfce_tvals[tstat_counter,
                           start:end] = (tfce_temp[start:end] *
                                         (tval_temp[start:end].max() / 100) *
                                         vdensity)
                neg_tfce_tvals[tstat_counter, start:end] = (
                    neg_tfce_temp[start:end] *
                    ((tval_temp * -1)[start:end].max() / 100) * vdensity)
            else:
                tfce_tvals[tstat_counter,
                           start:end] = (tfce_temp[start:end] *
                                         (tval_temp[start:end].max() / 100) *
                                         vdensity[start:end])
                neg_tfce_tvals[tstat_counter,
                               start:end] = (neg_tfce_temp[start:end] * (
                                   (tval_temp * -1)[start:end].max() / 100) *
                                             vdensity[start:end])
            if randomise:
                os.system("echo %f >> perm_maxTFCE_surf%d_tcon%d.csv" %
                          (np.nanmax(tfce_tvals[tstat_counter, start:end]),
                           surf_count, tstat_counter + 1))
                os.system("echo %f >> perm_maxTFCE_surf%d_tcon%d.csv" %
                          (np.nanmax(neg_tfce_tvals[tstat_counter, start:end]),
                           surf_count, tstat_counter + 1))
            else:
                print "Maximum (untransformed) postive tfce value for surface %s, tcon %d: %f" % (
                    surf_count, tstat_counter + 1,
                    np.nanmax(tfce_tvals[tstat_counter, start:end]))
                print "Maximum (untransformed) negative tfce value for surface %s, tcon %d: %f" % (
                    surf_count, tstat_counter + 1,
                    np.nanmax(neg_tfce_tvals[tstat_counter, start:end]))
        if verbose:
            print "T-contrast: %d" % tstat_counter
            print "Max tfce from all surfaces = %f" % tfce_tvals[
                tstat_counter].max()
            print "Max negative tfce from all surfaces = %f" % neg_tfce_tvals[
                tstat_counter].max()
    if randomise:
        print "Interation number: %d" % perm_number
        os.system("echo %s >> perm_maxTFCE_allsurf.csv" %
                  (','.join(["%0.2f" % i for i in tfce_tvals.max(axis=1)])))
        os.system("echo %s >> perm_maxTFCE_allsurf.csv" %
                  (','.join(["%0.2f" % i
                             for i in neg_tfce_tvals.max(axis=1)])))
        tvals = None
        tfce_tvals = None
        neg_tfce_tvals = None
    tval_temp = None
    tfce_temp = None
    neg_tfce_temp = None
    del calcTFCE
    if not randomise:
        return (tvals.astype(np.float32, order="C"),
                tfce_tvals.astype(np.float32, order="C"),
                neg_tfce_tvals.astype(np.float32, order="C"))