def seed_voxel_masked_corrz(fourd, seed, maskf, outdir): _, seedname, _ = pp.split_filename(seed) outfile = os.path.join(outdir, '%s_corrz.nii.gz'%(seedname)) seedval = np.loadtxt(seed) img = ni.load(fourd) imgdim = img.get_shape()[:3] maskdim = ni.load(maskf).get_shape() if not maskdim == imgdim: raise ValueError('dimension mismatch, mask: %s, data: %s'%(maskdim, imgdim)) dat = img.get_data() maskdat = ni.load(maskf).get_data() mask = maskdat > 0 masked_dat = dat[mask,:] steps = [x * mask.shape[0]/10 for x in range(10)] steps.append(-1) allres = np.zeros(masked_dat.shape[0]) for i, stop in enumerate(steps): start = steps[i] clump = masked_dat[start:s,:] tmpres = np.corrcoef(clump, seed) allres[start:s] = tmpres[-1,:-1] new = np.zeros(dat.shape[:3]) new[mask] = allres newimg = ni.Nifti1Image(new, img.get_affine()) newimg.to_filename(outfile) return outfile
def generate_seed_voxelcorrelation(fourd, seed, outdir): _, seedname, _ = pp.split_filename(seed) outfile = os.path.join(outdir, '%s_corr.nii.gz'%(seedname)) cmd = ' '.join(['3dfim+', '-input', fourd, '-ideal_file', seed, '-out', 'Correlation', '-bucket', outfile]) cout = pp.CommandLine(cmd).run() if not cout.runtime.returncode == 0: print cout.runtime.stderr return None return outfile
def seed_voxel_corrz(fourd, seed, outdir): """ uses numpy, nibabel to calc timeseries correlation with seed values, ztransforms and saves to file in outdir""" _, seedname, _ = pp.split_filename(seed) outfile = os.path.join(outdir, '%s_corrz.nii.gz'%(seedname)) seedval = np.loadtxt(seed) img = ni.load(fourd) dat = img.get_data() new = np.zeros(dat.shape[:3]) for i, slice in enumerate(dat): for j, part in enumerate(slice): res = np.corrcoef(part, seedval) new[i,j,:] = res[-1,:-1] new = np.arctanh(new) new[np.isnan(new)] = 0 newimg = ni.Nifti1Image(new, img.get_affine()) newimg.to_filename(outfile) return outfile
""" if __name__ == '__main__': ### Change items here#### # get structurals datadir = '/home/jagust/graph/data/spm_220' anatomicals = pp.get_files(datadir,'B*/anat/B*_anat.nii') ####################################################### # run vbm vbm8_dict = {} for anat in anatomicals: # copy file to processing dir pth, nme, ext = pp.split_filename(anat) subid = nme.split('_anat')[0] vbmdir,exists = pp.make_dir(pth, 'vbm8') canat = pp.copy_file(anat, vbmdir) if not exists: # only run if vbm directory is missing out = pp.spm_vbm8(canat) vbmd = pp.get_vbmfiles(canat) vbm8_dict.update({subid: vbmd}) # run dartel on cohort gms = pp.get_files(datadir, '*/anat/vbm8/rp1*.nii') wms = pp.get_files(datadir, '*/anat/vbm8/rp2*.nii') gms.sort() wms.sort() files = []
################## datadir = '/home/jagust/graph/data/spm_220' functionals = pp.get_files(datadir,'B*/func/B*_4d.nii*') TR = 2.2 #set to TR = None if TR in file is correct templatedir = '/home/jagust/graph/data/spm_220/template' ##### CHANGE TEMPLATE HERE!! ############ template_name = 'dartel_May_21_2013_14_27_6.nii' ################################################# template = pp.os.path.join(templatedir,template_name) ruwd = {} for func in functionals: # copy file to processing dir pth, nme, ext = pp.split_filename(func) subid = nme.split('_4d')[0] realigndir,exists = pp.make_dir(pth, 'realign_unwarp') slicetime_vars = pp.get_slicetime_vars(func, TR = TR) if not exists: # only run if realign_unwarp directory is missing cfunc = pp.copy_file(func, realigndir) if pp.is_4d(cfunc): # convert to list of 3d files cfuncs = pp.fsl_split4d(cfunc, subid) # remove 4d file cfuncs = [x for x in cfuncs if not 'gz' in x] #pp.remove_files(cfunc) else: cfuncs = cfunc