Exemplo n.º 1
0
def process_run(rundir, subid, logger):
	"""
	1. Find raw
	2. Split func into xfm dir
	3. Align func, create mean and 4d
	4. Bias correct anat
	5. Invxfm anat2func
	6. Generate movement files
	7. Clean up
	"""
	_, run = os.path.split(rundir)
	rawdir = os.path.join(rundir, 'raw')
	exists, mvdir, xfmdir = check_make_dirs(rundir, logger)
	if exists:
		return
	anat, aparc, func = find_raw_files(rawdir, subid, logger)
	if anat is None:
		return
	funcs = split_file(func, xfmdir, subid, run, logger)
	if funcs is None:
		return
	movement, movedfuncs = align_funcs(funcs, logger)
	exists, refunc = join_files(movedfuncs, rundir, subid, run)
	if not exists:
		return
	#Write movement and displacement
	logger.info('Write movement')
	save_movement(movement, mvdir)
	#Make mean from aligned
	meanfunc = reg.make_mean(movedfuncs)
	#Bias correct anat
	b4anat = reg.n4_biascorrect(anat)
	if b4anat is None:
		logger.error('{0}: Bias correction failed'.format(anat))
		return
	#Bias correct mean
	b4meanfunc = reg.n4_biascorrect(meanfunc)
	if b4meanfunc is None:
		logger.error('{0}: Bias correction failed'.format(meanfunc))
		return
	#Reslice mean to anat space
	rb4meanfunc = reslice_mean(b4anat, b4meanfunc)	
	#Make mask by growing func (5x5x5 kernel) and mask with anat
	mask  = reg.grow_mask(rb4meanfunc, size= 3)
	#Find mapping from func to anat
	func2anat_xfm = reg.affine_register_mi(b4anat, b4meanfunc, mask= mask)
	#Apply inverse mapping to anat
	anat2func = reg.apply_transform(b4anat, '-i %s'%(func2anat_xfm), 
		target=meanfunc, use_nn = False)
	#Apply transform to aparc nearest neighbor
	aparc2func = reg.apply_transform(aparc, '-i %s'%(func2anat_xfm), 
		target=meanfunc, use_nn = True)

	#Move 
	for item in (anat2func, aparc2func):
		cmd = 'mv %s %s'%(item, rundir)
		os.system(cmd)
Exemplo n.º 2
0
def process_runs(rundir, subid, run3meanfunc, logger):
	_, run = os.path.split(rundir)
	rawdir = os.path.join(rundir, 'raw')
	exists, mvdir, xfmdir = check_make_dirs(rundir, logger)
	if exists:
		return

	_, run3 = os.path.split(run3meanfunc)
	cmd = 'ln -s %s %s'%(run3meanfunc, xfmdir)
	os.system(cmd)
	run3meanfunc = os.path.join(xfmdir, run3)

	func = os.path.join(rawdir, '{0}_4d_func.nii.gz'.format(subid))
	funcs = split_file(func, xfmdir, subid, run, logger)
	if funcs is None:
		return
	movement, affines, moved = make_affines(funcs, logger)
	logger.info('Write movement')
	save_movement(movement, mvdir)
	meanfunc = reg.make_mean(moved)
	
	for f in moved[1:]:
		os.remove(f)

	threshold_mean(meanfunc, logger, thresh=275)
	b4meanfunc = reg.n4_biascorrect(meanfunc)
	if b4meanfunc is None:
		logger.error('{0}: Bias correction failed'.format(meanfunc))
		return
	movedfuncs = align_means(funcs, b4meanfunc, run3meanfunc, affines, logger)
	exists, refunc = join_files(movedfuncs, rundir, subid, run)
	if not exists:
		return
Exemplo n.º 3
0
def bias_correct(data, outdir):
	b4data = reg.n4_biascorrect(data)
	_, b4nme = os.path.split(b4data)
	cmd = 'mv %s %s'%(b4data, outdir)
	os.system(cmd)
	b4data = os.path.join(outdir, b4nme)
	return b4data
Exemplo n.º 4
0
def bias_correct(data, logger, outdir):
	b4data = reg.n4_biascorrect(data)
	if b4data is None:
		logger.error('{0}: Bias correction failed'.format(data))
		return
	_, b4nme = os.path.split(b4data)
	cmd = 'mv %s %s'%(b4data, outdir)
	os.system(cmd)
	b4data = os.path.join(outdir, b4nme)
	return b4data
Exemplo n.º 5
0
def process_run3(rundir, subid, logger):
	"""
	1. Find raw
	2. Split func into xfm dir
	3. Align func, create mean and 4d
	4. Bias correct anat
	5. Invxfm anat2func
	6. Generate movement files
	7. Clean up
	"""
	_, run = os.path.split(rundir)
	rawdir = os.path.join(rundir, 'raw')
	exists, mvdir, xfmdir = check_make_dirs(rundir, logger)
	if exists:
		return
	anat, aparc, func = find_raw_files(rawdir, subid, logger)
	if anat is None:
		return
	funcs = split_file(func, xfmdir, subid, run, logger)
	if funcs is None:
		return
	movement, movedfuncs = align_funcs(funcs, logger)
	exists, refunc = join_files(movedfuncs, rundir, subid, run)
	if not exists:
		return
	#Write movement and displacement
	logger.info('Write movement')
	save_movement(movement, mvdir)
	#Make mean from aligned
	meanfunc = reg.make_mean(movedfuncs)
	#Bias correct anat
	b4anat = reg.n4_biascorrect(anat)
	if b4anat is None:
		logger.error('{0}: Bias correction failed'.format(anat))
		return
	_, b4anatname = os.path.split(b4anat)
	cmd = 'mv %s %s'%(b4anat, xfmdir)
	os.system(cmd)
	b4anat = os.path.join(xfmdir, b4anatname)
	# threshold mean to remove "cruft" current value == 300
	threshold_mean(meanfunc, logger, thresh=300)
	#Bias correct mean
	#b4meanfunc = reg.n4_biascorrect(meanfunc)
	#if b4meanfunc is None:
	#	logger.error('{0}: Bias correction failed'.format(meanfunc))
	#	return
	#Reslice mean to anat space
	b4meanfunc = meanfunc
	rb4meanfunc = reslice_mean(b4anat, b4meanfunc)
	#Make mask by growing func (5x5x5 kernel) and mask with anat
	# mask  = reg.grow_mask(rb4meanfunc, size= 3)
	#Find mapping from func to anat
	func2anat_xfm = reg.affine_register_mi(b4anat, b4meanfunc, mask= None)
	#Apply inverse mapping to anat
	anat2func = reg.apply_transform(b4anat, '-i %s'%(func2anat_xfm),
		target=meanfunc, use_nn = False)
	#Apply transform to aparc nearest neighbor
	aparc2func = reg.apply_transform(aparc, '-i %s'%(func2anat_xfm),
		target=meanfunc, use_nn = True)

	#Skull strip shit
	pth, aparcnme = os.path.split(aparc2func)
	aparc_mask = os.path.join(datadir, 'mask_{0}'.format(aparcnme))
	mask_from_aparc(aparc2func, aparc_mask)

	#masked_b4meanfunc = b4meanfunc.replace('n4_', 'masked_n4_')
	#mask_img(b4meanfunc, aparc_mask, masked_b4meanfunc)

	#Move
	for item in (anat2func, aparc2func):
		cmd = 'mv %s %s'%(item, rundir)
		os.system(cmd)

	return b4meanfunc
Exemplo n.º 6
0
def process_runs(rundir, subid, run3meanfunc, logger):
	_, run = os.path.split(rundir)
	rawdir = os.path.join(rundir, 'raw')
	exists, mvdir, xfmdir = check_make_dirs(rundir, logger)
	if exists:
		return

	_, run3 = os.path.split(run3meanfunc)
	cmd = 'cp %s %s'%(run3meanfunc, xfmdir)
	os.system(cmd)
	run3meanfunc = os.path.join(xfmdir, run3)

	anat, aparc, func = find_raw_files(rawdir, subid, logger)
	if anat is None:
		return
	funcs = split_file(func, xfmdir, subid, run, logger)
	if funcs is None:
		return
	movement, affines, moved = make_affines(funcs, logger)
	logger.info('Write movement')
	save_movement(movement, mvdir)
	meanfunc = reg.make_mean(moved)
	#Remove old movedfuncs
	newdir = os.path.join(xfmdir, 'first_realign')
	os.mkdir(newdir)
	for f in moved[1:]:
		cmd = 'mv %s %s'%(f, newdir)
		os.system(cmd)

	#Bias correct anat
	b4anat = reg.n4_biascorrect(anat)
	if b4anat is None:
		logger.error('{0}: Bias correction failed'.format(anat))
		return
	_, b4anatname = os.path.split(b4anat)
	cmd = 'mv %s %s'%(b4anat, xfmdir)
	os.system(cmd)
	b4anat = os.path.join(xfmdir, b4anatname)
	# threshold mean to remove "cruft" current value == 300
	threshold_mean(meanfunc, logger, thresh=300)
	#Bias correct mean
	#b4meanfunc = reg.n4_biascorrect(meanfunc)
	#if b4meanfunc is None:
	#	logger.error('{0}: Bias correction failed'.format(meanfunc))
	#	return
	b4meanfunc = meanfunc
	#Reslice mean to anat space
	rb4meanfunc = reslice_mean(b4anat, b4meanfunc)
	#Make mask by growing func (5x5x5 kernel) and mask with anat
	# mask  = reg.grow_mask(rb4meanfunc, size= 3)
	#Find mapping from func to anat
	func2anat_xfm = reg.affine_register_mi(b4anat, b4meanfunc, mask= None)
	#Apply inverse mapping to anat
	anat2func = reg.apply_transform(b4anat, '-i %s'%(func2anat_xfm),
		target=meanfunc, use_nn = False)
	#Apply transform to aparc nearest neighbor
	aparc2func = reg.apply_transform(aparc, '-i %s'%(func2anat_xfm),
		target=meanfunc, use_nn = True)

	#Skull strip shit
	pth, aparcnme = os.path.split(aparc2func)
	aparc_mask = os.path.join(rundir, 'mask_{0}'.format(aparcnme))
	mask_from_aparc(aparc2func, aparc_mask)

	#masked_b4meanfunc = b4meanfunc.replace('n4_', 'masked_n4_')
	#mask_img(b4meanfunc, aparc_mask, masked_b4meanfunc)

	#Make new moved funcs based on 2 affines
	movedfuncs = align_means(funcs, b4meanfunc, run3meanfunc, affines, logger)
	exists, refunc = join_files(movedfuncs, rundir, subid, run)
	if not exists:
		return

	#Move
	for item in (anat2func, aparc2func):
		cmd = 'mv %s %s'%(item, rundir)
		os.system(cmd)