def mema(stdoutdir, ss_list):
    """
    AFNI's 3dMema
    """
    lg = setLog._log('%s/mema' % stdoutdir)
    lg.info('Doing mema ------------ ')
    effects = ['Aentr', 'Ventr', 'Aentr_intxn', 'Ventr_intxn']
    for ef in effects:
        diff_set = []
        for s in ss_list:
            dat_dir = os.path.join(os.environ['avp'], 'nii',
                                   'ss%s_effects' % s)
            coeff = os.path.join(dat_dir, '%s_ss%s_coef+tlrc' % (ef, s))
            tstatf = os.path.join(dat_dir, '%s_ss%s_tstat+tlrc' % (ef, s))
            diff_set.append('%d %s %s' %
                            (s, coeff, tstatf))
        diff_set = ' '.join(diff_set)
        mask = os.path.join(os.environ['avp'], 'nii', 'group_effects_wmvnt_incl',
                            'MNI152_T1_2mm_brain_mask_dil1+tlrc.BRIK.gz')
        mema_args = split('3dMEMA -jobs 20 -prefix %s/%s_flt3_msk_mema \
                          -mask %s -set %s %s -missing_data 0 -residual_Z' %
                          (stdoutdir, ef, mask, ef, diff_set))
        lg.info('args for 3dMEMA: \n%s' % mema_args)
        lg.info('Running 3dMEMA ...')
        p = subprocess.run(mema_args, stderr=subprocess.PIPE)
        lg.info(p.stderr.decode("utf-8", "strict"))
        lg.info('3dMEMA done.')
Beispiel #2
0
def vol2surf_mni_no_pn(work_dir, mapfunc, hemi, parent, outname, logf=None):
    """
    Project to MNI surf.
    Make sure 'suma_dir' is set right
    """
    if logf:
        lg = setLog._log(logf)
    lg.info("vol2surf_mni starting")
    suma_dir = '/mnt/lnif-storage/urihas/software/AFNI2015/suma_MNI_N27'
    spec_fname = 'MNI_N27_%s.spec' % hemi
    spec = os.path.join(suma_dir, spec_fname)
    surf_a = '%s.smoothwm.gii' % hemi
    surf_b = '%s.pial.gii' % hemi
    surfvol_name = 'MNI_N27_SurfVol.nii'
    sv = os.path.join(suma_dir, surfvol_name)
    cmdargs = split('3dVol2Surf -spec %s \
                    -surf_A %s -surf_B %s \
                    -sv %s -grid_parent %s \
                    -map_func %s -f_steps 10 -f_index voxels \
                    -outcols_NSD_format -oob_index -1 -oob_value 0.0 \
                    -out_1D %s' % (spec, surf_a, surf_b, sv,
                                   parent, mapfunc, outname))
    lg.info("Command: \n%s" % cmdargs)
    p = subprocess.run(cmdargs, stderr=subprocess.PIPE)
    lg.info(p.stderr.decode("utf-8", "strict"))
    lg.info("Done with vol2surf_mni")
 def mask_neurosynth_file(self, outfile):
     """
     Get the values as mask
     from the neurosynth result
     """
     lg = setLog._log(self.logf)
     lg.info('Doing mask_neurosynth_file')
     cmd = split("3dcalc -a %s -expr 'step(a)' \
                 -prefix %s " % (self.input, outfile))
     lg.info("Command: \n%s" % cmd)
     p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
     lg.info(p.stdout.decode("utf-8", "strict"))
     lg.info("Done with mask_neurosynth_file")
def cluster(outdir, thr, clst_size, infile, outname):
    """
    Doing clustering
    """
    lg = setLog._log('%s/clustering' % outdir)
    lg.info('Doing cluster ---- ')
    cmd = split(('3dclust -prefix %s -1Dformat -nosum -1dindex 1 -1tindex 1 \
                 -2thresh -%s %s -savemask %s_mask \
                 -dxyz=1 1.44 %s %s') %
                 (outname, thr, thr, outname, clst_size, infile))
    p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    lg.info(p.stdout.decode("utf-8", "strict"))
    lg.info('cluster is done.')
Beispiel #5
0
def cluster(vx_thr, clst_thr, infile, outpref, logf=None):
    """
    do 3dclust
    """
    if logf:
        lg = setLog._log(logf)
    lg.info("doing cluster (3dclust): \n%s" % infile)
    cmd = split("3dclust -prefix %s -1Dformat -nosum \
                -1dindex 1 -1tindex 1 -2thresh -%s %s \
                -dxyz=1 1.44 %s %s" %
                (outpref, vx_thr, vx_thr, clst_thr, infile))
    lg.info("Command: \n%s" % cmd)
    p = subprocess.run(cmd, stderr=subprocess.PIPE)
    lg.info(p.stderr.decode("utf-8", "strict"))
    lg.info("Done with cluster (3dclust).")
def estimate_clustersizes(fwhmx, outdir):
    """
    Already derived estimate of fwhmx via residuals
    fwhmx: x y z of fwhm estimate
    """
    lg = setLog._log('%s/clustersize_estimates' % outdir)
    lg.info('Doing estimate_clustersizes ---- ')
    lg.info('FWHMx is: %s' % fwhmx)
    mask = os.path.join(os.environ['FSLDIR'], 'data/standard',
                        'MNI152_T1_2mm_brain_mask_dil1.nii.gz')
    cmd = split('3dClustSim -mask %s -fwhmxyz %f %f %f' % (mask, fwhmx[0], fwhmx[1], fwhmx[2]))
    p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    lg.info(p.stdout.decode("utf-8", "strict"))
    f = open('%s/clustSim_resZ_fwhmx_%f_%f_%f_out.txt' %
             (outdir, fwhmx[0], fwhmx[1], fwhmx[2]), 'w')
    f.write(p.stdout.decode("utf-8", "strict"))
    f.close()
    lg.info('estimate_clustersizes done.')
Beispiel #7
0
def ttest(stdoutdir, ss_list, ef, outpref):
    """
    direct tests on comaprisons
    """
    lg = setLog._log("ttest")
    lg.info("Doing ttest in %s" % stdoutdir)

    a_sets = []
    for ss in ss_list:
        dat_dir = os.path.join(os.environ["avp"], "nii", "ss%s_effects" % ss)
        coeff = os.path.join(dat_dir, "%s_ss%s_coef+tlrc" % (ef, ss))
        a_sets.append(coeff)
    a_sets = " ".join(a_sets)
    args = split(
        "3dttest++ -setA %s -labelA %s \
                 -mask %s/MNI152_T1_2mm_brain_mask_dil1.nii.gz \
                 -prefix %s"
        % (a_sets, ef, os.path.join(os.environ["FSLDIR"], "data/standard"), outpref)
    )
    lg.info("args: \n%s" % args)
    p = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    lg.info(p.stderr.decode("utf-8", "strict"))
    lg.info("ttest done")
Beispiel #8
0
def applywarpFNIRT(ss, input, out, coeff, interp=None, logf=None):
    """
    Warp via nonlinear transformation via fsl FNIRT
    """
    if logf:
        lg = setLog._log(logf)
    else:
        lg = '/tmp/michaeljames.andric/applywarpFNIRT'
    lg.info('Doing applywarpFNIRT for %s -- ' % ss)
    if interp is None:
        cmd = split('applywarp -i %s \
                        -r %s/data/standard/MNI152_T1_2mm.nii.gz \
                        -o %s -w %s' %
                        (input, os.environ['FSLDIR'], out, coeff))
    else:
        cmd = split('applywarp -i %s \
                        -r %s/data/standard/MNI152_T1_2mm.nii.gz \
                        -o %s -w %s --interp=%s' %
                        (input, os.environ['FSLDIR'], out, coeff, interp))
    lg.info("Command: \n%s" % cmd)
    p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    lg.info(p.stdout.decode("utf-8", "strict"))
    lg.info("Done with applywarpFNIRT")
    """
    Will return x, y, z as floats
    """
    cmds = split('3dFWHMx -mask %s -input %s -ACF' % (mask, input))
    p = Popen(cmds, stdout=PIPE).communicate()
    return map(float, p[0].decode('utf-8', 'strict').split('\n')[3].split())

subj_list = [s for s in range(1, 20)]
subj_list.remove(3)
subj_list.remove(11)
xyz_mat = np.zeros(len(subj_list)*3).reshape(len(subj_list), 3)
abc_mat = np.zeros(len(subj_list)*4).reshape(len(subj_list), 4)
decon_dir = os.path.join(os.environ['avp'],
                         'nii', 'deconvolve_outs_concat_dec')
suff = 'Powered.cleanEPI_errts_REML'
lg = setLog._log('%s/acf_est_clustsim' % decon_dir)
lg.info('Doing get_fwhm')
for i, ss in enumerate(subj_list):
    lg.info('subj %s' % ss)
    mask = os.path.join(os.environ['avp'], 'nii', '%s_preproc' % ss,
                        '%s_3' % ss, '%s_3.Powered.mask.brain+orig' % ss)
    fpref = 'decon_out.mion.%s_concat.%s' % (ss, suff)
    input_pref = os.path.join(decon_dir, fpref)
    x, y, z = get_fwhm(mask, '%s+orig' % input_pref)
    lg.info('fwhm x y z: %s %s %s ' % (x, y, z))
    xyz_mat[i, :] = [x, y, z]
#    a, b, c, cmb_fwhm = get_fwhm_acf(mask, '%s+orig' % input_pref)
#    lg.info('a b c and fwhm: %s %s %s %s' % (a, b, c, cmb_fwhm))
#    abc_mat[i, :] = [a, b, c, cmb_fwhm]
lg.info('get_fwhm done.')
"""
Created on Mon Dec 14 02:32:29 2015

@author: andric
"""

import os
import setLog
import numpy as np
import procs as pr
from shlex import split
from subprocess import Popen, PIPE

import itertools


subj_list = [s for s in range(1, 20)]
subj_list.remove(3)
subj_list.remove(11)

ones = list([it for it in itertools.repeat([1, -1], 17)])
perm_ones = [it for it in itertools.product(*ones)]

n_perms = 1000
np.random.randint(0, len(perm_ones)/2., size=n_perms)


lg = setLog._log('calc_pos_neg')

def do_calc():
    lg.info("Doing 3dcalc for pos neg")
import pandas as pd
from shlex import split
import subprocess


def mask_dump_peak(clust, coords, subj):
    dat = os.path.join(os.environ["avp"], "nii", "ss%s_effects_dec" % subj, "%s_ss%s_coef+tlrc." % (clust, subj))
    cmd = split("3dmaskdump -noijk -dbox %s %s" % (coords, dat))
    p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return float(p.stdout.decode("utf-8", "strict"))


if __name__ == "__main__":

    stdoutdir = os.path.join(os.environ["avp"], "nii", "peak_vox_from_clust")
    lg = setLog._log("%s/do_peak_voxel_data" % stdoutdir)

    clust_coords = {
        "Aentr": ("-38.0 26.0 44.0", "34.0 -50.0 30.0"),
        "Ventr": ("32.0 -32.0 28.0", "-26.0 -8.0 46.0"),
        "Aentr_intxn": ("-42.0 56.0 -12.0", "34.0 58.0 -16.0", "-26.0 -14.0 28.0"),
    }

    bad_subjs = [3, 11]
    subj_list = [s for s in range(1, 20) if s not in bad_subjs]
    clust_col_names = []

    clust_dat = pd.Series()
    for clust in clust_coords:
        ncl = len(clust_coords[clust])
        for cl in range(ncl):
Beispiel #12
0
    p = subprocess.run(cmds, stdout=subprocess.PIPE)
    lg.info(p.stdout.decode("utf-8", "strict"))
    return list(map(float, p.stdout.decode("utf-8", "strict").split()))


subj_list = [s for s in range(1, 20)]
subj_list.remove(3)
subj_list.remove(11)
effects = ['Aentr', 'Ventr', 'Aentr_intxn']
mask = os.path.join(os.environ['FSLDIR'], 'data/standard',
                    'MNI152_T1_2mm_brain_mask_dil1.nii.gz')

for block in [20, 15, 10]:
    workdir = os.path.join(os.environ['avp'], 'nii',
                           'group_effects_%sblk' % block)
    lg = setLog._log('%s/fwhm_estimation' % workdir)
    block_mat = np.empty(len(effects)*3).reshape(len(effects), 3)
    for (n, ef) in enumerate(effects):
        xyz_mat = np.empty(len(subj_list)*3)
        xyz_mat = xyz_mat.reshape(len(subj_list), 3)
        infile = os.path.join(os.environ['avp'], 'nii/group_effects_%sblk' % block,
                              '%s_flt2_%sblk_msk_mema_resZ+tlrc' % (ef, block))
        for (i, ss) in enumerate(subj_list):
            x, y, z = get_fwhm(mask, '%s\[%d]' % (infile, i))
            xyz_mat[i, :] = [x, y, z]
        avg_fwhm = xyz_mat.mean(axis=0)
        block_mat[n, :] = avg_fwhm
        lg.info('%s block %s averages: %s' % (ef, block, avg_fwhm))
    block_avg = block_mat.mean(axis=0)
    lg.info('block average: %s' % block_avg)
    out_pref = 'fwhm_est_%sblk.out' % block