def run(opts):
    #check if ICA has been run
    if not os.path.exists("ICA_temp"):
        print("ICA_temp not found")
        exit()
#load mask
    if opts.bothhemi:
        lh_mask, lh_mask_data = loadmgh('ICA_temp/lh_mask.mgh')
        rh_mask, rh_mask_data = loadmgh('ICA_temp/rh_mask.mgh')
        lh_mask_index = (lh_mask_data != 0)
        rh_mask_index = (rh_mask_data != 0)
        midpoint = lh_mask_data[lh_mask_data == 1].shape[0]
    else:
        if opts.voxel:
            mask, mask_data = loadnifti('ICA_temp/mask.nii.gz')
        if opts.vertex:
            mask, mask_data = loadmgh('ICA_temp/mask.mgh')
        mask_index = mask_data > .99


#load data
    rmcomps = np.genfromtxt(opts.input[0], delimiter=",", dtype='int')
    dump_ica = pickle.load(open("ICA_temp/icasave.p", "rb"))
    fitcomps = np.load('ICA_temp/signals.npy').T
    selected = rmcomps - 1
    #zero out unwanted components
    fitcomps[:, selected] = 0
    #rebuild image
    X_rec = dump_ica.inverse_transform(fitcomps)

    #save image
    outname = opts.output[0]
    outname = outname.split('.gz', 1)[0]
    outname = outname.split('.nii', 1)[0]
    outname = outname.split('.mgh', 1)[0]

    if opts.voxel:
        savenifti(X_rec, mask, mask_index, '%s.nii.gz' % outname)
    if opts.vertex:
        savemgh(X_rec, mask, mask_index, '%s.mgh' % outname)

    if opts.bothhemi:
        savemgh(X_rec[:midpoint], lh_mask, lh_mask_index,
                'lh.%s.mgh' % outname)
        savemgh(X_rec[midpoint:], rh_mask, rh_mask_index,
                'rh.%s.mgh' % outname)

    if opts.clean:
        os.system("rm -rf ICA_temp")
def run(opts):
    if opts.inputtxt:
        txtname = opts.inputtxt[0]

        if txtname.endswith(('.mgh', '.mgz')):
            print "Error: mgh file input detected. Please use -s option."
            quit()

        surfVals = np.loadtxt(txtname, dtype=np.float, delimiter=',')
        if opts.transpose:
            print "Transposing array"
            surfVals = surfVals.T
        numVert = len(surfVals)
        print "Reading in %d subjects and %d vertices. If this is incorrect re-run the script is the --transpose option." % (
            surfVals.shape[1], surfVals.shape[0])
        if surfVals.ndim == 1:
            outsurf = np.zeros((numVert, 1, 1))
            outsurf[:, 0, 0] = surfVals
        if surfVals.ndim == 2:
            outsurf = np.zeros((numVert, 1, 1, surfVals.shape[1]))
            outsurf[:, 0, 0, :] = surfVals
        txtname = os.path.splitext(txtname)[0]
        nib.save(
            nib.freesurfer.mghformat.MGHImage(outsurf.astype(np.float32,
                                                             order="C"),
                                              affine=None), '%s.mgh' % txtname)
    if opts.inputsurface:
        surfname = opts.inputsurface[0]

        if surfname.endswith(('.txt', '.csv')):
            print "Error: text file input detected. Please use -i option."
            quit()

        img, imgdata = loadmgh(surfname)
        if imgdata.ndim == 3:
            imgdata = imgdata[:, 0, 0]
        if imgdata.ndim == 4:
            imgdata = imgdata[:, 0, 0, :]
        if opts.transpose:
            print "Transposing array"
            imgdata = imgdata.T
        surfname = os.path.splitext(surfname)[0]
        np.savetxt(("%s.csv" % surfname), imgdata, delimiter=",", fmt='%10.8f')
Example #3
0
def run(opts):

    scriptwd = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    img, data = loadmgh(opts.input[0])
    outname = str(opts.output[0])
    hemi = str(opts.hemi[0])
    fwhm = float(opts.geodesicfwhm[0])
    sigma = fwhm / np.sqrt(8 * np.log(2))

    # load distances and indices
    dist = np.load(opts.distanceslist[0])
    basenamefwhm = opts.distanceslist[0].split('_distances.npy', 1)[0]
    indices = np.load('%s_indices.npy' % basenamefwhm)

    cortex_index = np.load('%s/adjacency_sets/%s_cortex_mask_index.npy' %
                           (scriptwd, hemi))
    distmask_index_start = np.in1d(indices[:, 0], cortex_index)
    distmask_index_end = np.in1d(indices[:, 1], cortex_index)
    distmask_index = distmask_index_start & distmask_index_end

    # mask out non-cortex values
    indices_masked = indices[distmask_index]
    dist_masked = dist[distmask_index]

    if opts.correct_surface:
        multipler = opts.correct_surface[0]
        y = data[cortex_index].flatten()
        (mu, sigma) = norm.fit(y)
        cthresh = multipler * sigma + mu
        print("The upper threshold is: %1.4f" % cthresh)
        data[data > cthresh] = cthresh

    smoothed = calc_gd_fwhm(indices_masked.astype(np.int32, order='c'),
                            dist_masked.astype(np.float32, order='c'),
                            data[:, 0, 0].astype(np.float32, order="c"), sigma)

    outdata = np.zeros_like(data)
    outdata[cortex_index, 0, 0] = smoothed[cortex_index]
    nib.save(nib.freesurfer.mghformat.MGHImage(outdata, img.affine), outname)