Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 = Surf(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':
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"
calcTFCE_lh = Surf(2, 1, adjac_lh) # H=2, E=1
calcTFCE_rh = Surf(2, 1, adjac_rh) # H=2, E=1
#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.mask:
label = opts.mask
print("Loading fsaverage ?l.%s.label" % label)
os.system("cat $SUBJECTS_DIR/fsaverage/label/lh.%s.label | tail -n +3 | awk '{ print $1}' > temp.lh.verticeslist; cat $SUBJECTS_DIR/fsaverage/label/rh.%s.label | tail -n +3 | awk '{ print $1}' > temp.rh.verticeslist" % (label,label))
index_lh = np.array(np.genfromtxt("temp.lh.verticeslist", delimiter=',').astype(np.int))
index_rh = np.array(np.genfromtxt("temp.rh.verticeslist", delimiter=',').astype(np.int))
bin_mask_lh = np.zeros_like(mean_lh)
bin_mask_lh[index_rh]=1
bin_mask_lh[mean_lh==0]=0
v_rh, faces_rh = nib.freesurfer.read_geometry("%s/fsaverage/surf/rh.sphere" % os.environ["SUBJECTS_DIR"])
adjac_lh = create_adjac(v_lh,faces_lh)
adjac_rh = create_adjac(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"
calcTFCE_lh = Surf(2, 1, adjac_lh) # H=2, E=1
calcTFCE_rh = Surf(2, 1, adjac_rh) # H=2, E=1
#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/covars" % surface,covars)
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)
adjac_lh = create_adjac(v_lh,faces_lh)
adjac_rh = create_adjac(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"
calcTFCE_lh = Surf(2, 1, adjac_lh) # H=2, E=1
calcTFCE_rh = Surf(2, 1, adjac_rh) # H=2, E=1
#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/covars" % surface,covars)
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)
v_rh, faces_rh = nib.freesurfer.read_geometry("%s/fsaverage/surf/rh.sphere" % os.environ["SUBJECTS_DIR"])
adjac_lh = create_adjac(v_lh,faces_lh)
adjac_rh = create_adjac(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"
calcTFCE_lh = Surf(2, 1, adjac_lh) # H=2, E=1
calcTFCE_rh = Surf(2, 1, adjac_rh) # H=2, E=1
#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.mask:
label = opts.mask
print("Loading fsaverage ?l.%s.label" % label)
os.system("cat $SUBJECTS_DIR/fsaverage/label/lh.%s.label | tail -n +3 | awk '{ print $1}' > temp.lh.verticeslist; cat $SUBJECTS_DIR/fsaverage/label/rh.%s.label | tail -n +3 | awk '{ print $1}' > temp.rh.verticeslist" % (label,label))
index_lh = np.array(np.genfromtxt("temp.lh.verticeslist", delimiter=',').astype(np.int))
index_rh = np.array(np.genfromtxt("temp.rh.verticeslist", delimiter=',').astype(np.int))
bin_mask_lh = np.zeros_like(mean_lh)
bin_mask_lh[index_rh]=1
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"
calcTFCE_lh = Surf(2, 1, adjac_lh) # H=2, E=1
calcTFCE_rh = Surf(2, 1, adjac_rh) # H=2, E=1
#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.mask:
label = opts.mask
print("Loading fsaverage ?l.%s.label" % label)
os.system("cat $SUBJECTS_DIR/fsaverage/label/lh.%s.label | tail -n +3 | awk '{ print $1}' > temp.lh.verticeslist; cat $SUBJECTS_DIR/fsaverage/label/rh.%s.label | tail -n +3 | awk '{ print $1}' > temp.rh.verticeslist" % (label,label))
index_lh = np.array(np.genfromtxt("temp.lh.verticeslist", delimiter=',').astype(np.int))
index_rh = np.array(np.genfromtxt("temp.rh.verticeslist", delimiter=',').astype(np.int))
bin_mask_lh = np.zeros_like(mean_lh)
bin_mask_lh[index_rh]=1
#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))
#load TFCE fucntion
calcTFCE_lh = Surf(2, 1, adjac_lh) # H=2, E=1
calcTFCE_rh = Surf(2, 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 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, y, n, num_vertex)
else:
pathA_nx = pred_x[indices_perm]
#load variables
raw_nonzero = np.load('python_temp/raw_nonzero.npy')
n = raw_nonzero.shape[1]
header_mask = np.load('python_temp/header_mask.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')
pred_x = np.genfromtxt(arg_predictor, delimiter=",")
covars = np.genfromtxt(arg_covars, delimiter=",")
depend_y = np.genfromtxt(arg_depend, delimiter=",")
#TFCE
adjac = create_adjac_voxel(data_index,data_mask,num_voxel,dirtype=opts.tfce[2])
calcTFCE = Surf(float(opts.tfce[0]), float(opts.tfce[1]), adjac) # i.e. default: H=2, E=2, 26 neighbour connectivity
#step1
x_covars = np.column_stack([np.ones(n),covars])
y = resid_covars(x_covars,raw_nonzero)
#save
np.save('python_temp/pred_x',pred_x)
np.save('python_temp/covars',covars)
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)
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"
calcTFCE_lh = Surf(2, 1, adjac_lh) # H=2, E=1
calcTFCE_rh = Surf(2, 1, adjac_rh) # H=2, E=1
#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/covars" % surface,covars)
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)
surface = str(opts.surface[0])
#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))
#load TFCE fucntion
calcTFCE_lh = Surf(2, 1, adjac_lh) # H=2, E=1
calcTFCE_rh = Surf(2, 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 xrange(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