Commit c4e67457 authored by Christoph Arthofer's avatar Christoph Arthofer
Browse files

added option to calculate median image in addition to average image

parent 091bc95b
......@@ -363,34 +363,50 @@ def clampImage(img_path, out_path):
img_clamped_nib.to_filename(out_path)
def averageImages(img_paths, out_path, norm_bool=False):
def averageImages(img_paths, out_path, mod='average', norm_bool=False):
"""! Creates an average image from individual (non)normalised images.
@param img_paths: List of filepaths
@param out_path: Path to average output image
@param out_path: Path to average/median output image
@param mode: Choose between 'average' or 'median' image.
@param norm_bool: Normalise intensities of each image before averaging true or false
"""
n_exist = 0
n_imgs = len(img_paths)
for i, img_path in enumerate(img_paths):
if os.path.exists(img_path):
n_exist += 1
print(i, ' ', img_path)
img_nib = nib.load(img_path)
if norm_bool:
img_nib = fslmaths(img_nib).inm(1000).run()
if i == 0:
sum_img = img_nib
n_exist = 0
if mod == 'average':
for i, img_path in enumerate(img_paths):
if os.path.exists(img_path):
n_exist += 1
print(i, ' ', img_path)
img_nib = nib.load(img_path)
if norm_bool:
img_nib = fslmaths(img_nib).inm(1000).run()
if i == 0:
sum_img = img_nib
else:
sum_img = fslmaths(sum_img).add(img_nib).run()
else:
sum_img = fslmaths(sum_img).add(img_nib).run()
else:
print(i, ' ', img_path, ' does not exist!')
if n_exist > 0:
mean_img = fslmaths(sum_img).div(n_exist).run()
mean_img.to_filename(out_path)
print(i, ' ', img_path, ' does not exist!')
if n_exist > 0:
mean_img = fslmaths(sum_img).div(n_exist).run()
mean_.to_filename(out_path)
elif mod == 'median':
images = []
for i, img_path in enumerate(img_paths):
if os.path.exists(img_path):
n_exist += 1
print(i, ' ', img_path)
img_nib = nib.load(img_path)
if norm_bool:
img_nib = fslmaths(img_nib).inm(1000).run()
images.append(img_nib.get_fdata())
if n_exist > 0:
median_img = np.median(np.array(images),axis=-1)
median_nib = nib.Nifti1Image(median_img, affine=img_nib.affine, header=img_nib.header)
median_nib.to_filename(out_path)
assert n_exist == n_imgs, "Not all images available!"
......@@ -487,7 +503,7 @@ def submitJob(name, log_dir, queue, wait_for=[], script=None, command=None, copr
@param wait_for: List of IDs of jobs required to finish before running this job.
@param script: Path to a shell script, which contains one command per line - commands will be submitted as an array job
@param command: Alternatively a single command can be provided as a string - command will be submitted as single job
@param coprocessor_class: Coprocessor class, if not None cuda will be selected
@param coprocessor_class: Coprocessor class
@param export_var: Environment variables to be exported to the submission node
@param debug: If True, information about job will be written to output
......@@ -1006,7 +1022,7 @@ if __name__ == "__main__":
img_paths.append(tree.get('T1_to_unbiased_img'))
aff_template_path = tree.get('T1_unbiased_affine_template')
jobcmd = func_to_cmd(averageImages, args=(img_paths, aff_template_path, True), tmp_dir=script_dir, kwargs=None,
jobcmd = func_to_cmd(averageImages, args=(img_paths, aff_template_path, 'median', True), tmp_dir=script_dir, kwargs=None,
clean="never")
job_ids[8] = submitJob(tag+'_'+task_name, log_dir, command=jobcmd, queue=cpuq, wait_for=[job_ids[5]])
print('submitted: ' + task_name)
......@@ -1176,7 +1192,7 @@ if __name__ == "__main__":
img_paths.append(tree.get('T1_brain_to_MNI_img'))
aff_template_path = tree.get('T1_brain_affine_template')
jobcmd = func_to_cmd(averageImages, args=(img_paths, aff_template_path, True), tmp_dir=script_dir,
jobcmd = func_to_cmd(averageImages, args=(img_paths, aff_template_path, 'median', True), tmp_dir=script_dir,
kwargs=None, clean="never")
job_ids[21] = submitJob(tag+'_'+task_name, log_dir, command=jobcmd, queue=cpuq,
wait_for=[job_ids[15]])
......@@ -1192,7 +1208,7 @@ if __name__ == "__main__":
img_paths.append(tree.get('T1_brain_mask_to_MNI_img'))
aff_template_path = tree.get('T1_brain_mask_affine_template')
jobcmd = func_to_cmd(averageImages, args=(img_paths, aff_template_path, False), tmp_dir=script_dir,
jobcmd = func_to_cmd(averageImages, args=(img_paths, aff_template_path, 'average', False), tmp_dir=script_dir,
kwargs=None, clean="never")
job_ids[22] = submitJob(tag+'_'+task_name, log_dir, command=jobcmd, queue=cpuq,
wait_for=[job_ids[16]])
......@@ -1213,7 +1229,7 @@ if __name__ == "__main__":
img_paths.append(tree.get('T1_head_to_MNI_img'))
aff_template_path = tree.get('T1_head_affine_template')
jobcmd = func_to_cmd(averageImages, args=(img_paths, aff_template_path, True), tmp_dir=script_dir,
jobcmd = func_to_cmd(averageImages, args=(img_paths, aff_template_path, 'median', True), tmp_dir=script_dir,
kwargs=None, clean="never")
job_ids[24] = submitJob(tag+'_'+task_name, log_dir, command=jobcmd, queue=cpuq,
wait_for=[job_ids[17]])
......@@ -1229,7 +1245,7 @@ if __name__ == "__main__":
img_paths.append(tree.get('T2_head_to_MNI_img'))
aff_template_path = tree.get('T2_head_affine_template')
jobcmd = func_to_cmd(averageImages, args=(img_paths, aff_template_path, True), tmp_dir=script_dir,
jobcmd = func_to_cmd(averageImages, args=(img_paths, aff_template_path, 'median', True), tmp_dir=script_dir,
kwargs=None, clean="never")
job_ids[25] = submitJob(tag+'_'+task_name, log_dir, command=jobcmd, queue=cpuq,
wait_for=[job_ids[18]])
......@@ -1245,7 +1261,7 @@ if __name__ == "__main__":
img_paths.append(tree.get('DTI_to_MNI_img'))
aff_template_path = tree.get('DTI_affine_template')
jobcmd = func_to_cmd(averageImages, args=(img_paths, aff_template_path, True), tmp_dir=script_dir,
jobcmd = func_to_cmd(averageImages, args=(img_paths, aff_template_path, 'median', True), tmp_dir=script_dir,
kwargs=None, clean="never")
job_ids[26] = submitJob(tag+'_'+task_name, log_dir, command=jobcmd, queue=cpuq,
wait_for=[job_ids[19]])
......@@ -1403,7 +1419,7 @@ if __name__ == "__main__":
warp_paths.append(tree.get('mmorf_warp'))
avg_warp_path = tree.get('avg_warp')
jobcmd = func_to_cmd(averageImages, args=(warp_paths, avg_warp_path, False), tmp_dir=script_dir,
jobcmd = func_to_cmd(averageImages, args=(warp_paths, avg_warp_path, 'average', False), tmp_dir=script_dir,
kwargs=None, clean="never")
job_ids[29] = submitJob(tag+'_'+task_name, log_dir, command=jobcmd, queue=cpuq,
wait_for=[job_ids[28]])
......@@ -1653,7 +1669,7 @@ if __name__ == "__main__":
img_paths.append(tree.get('warped_T1brain'))
nln_template_path = tree.get('T1_brain_nln_template')
jobcmd = func_to_cmd(averageImages, args=(img_paths, nln_template_path, True), tmp_dir=script_dir,
jobcmd = func_to_cmd(averageImages, args=(img_paths, nln_template_path, 'median', True), tmp_dir=script_dir,
kwargs=None, clean="never")
job_ids[44] = submitJob(tag+'_'+task_name, log_dir, command=jobcmd, queue=cpuq,
wait_for=[job_ids[37]])
......@@ -1669,7 +1685,7 @@ if __name__ == "__main__":
img_paths.append(tree.get('warped_T1head'))
nln_template_path = tree.get('T1_head_nln_template')
jobcmd = func_to_cmd(averageImages, args=(img_paths, nln_template_path, True), tmp_dir=script_dir,
jobcmd = func_to_cmd(averageImages, args=(img_paths, nln_template_path, 'median', True), tmp_dir=script_dir,
kwargs=None, clean="never")
job_ids[45] = submitJob(tag+'_'+task_name, log_dir, command=jobcmd, queue=cpuq,
wait_for=[job_ids[39]])
......@@ -1685,7 +1701,7 @@ if __name__ == "__main__":
img_paths.append(tree.get('warped_T2head'))
nln_template_path = tree.get('T2_head_nln_template')
jobcmd = func_to_cmd(averageImages, args=(img_paths, nln_template_path, True), tmp_dir=script_dir,
jobcmd = func_to_cmd(averageImages, args=(img_paths, nln_template_path, 'median', True), tmp_dir=script_dir,
kwargs=None, clean="never")
job_ids[46] = submitJob(tag+'_'+task_name, log_dir, command=jobcmd, queue=cpuq,
wait_for=[job_ids[40]])
......@@ -1701,7 +1717,7 @@ if __name__ == "__main__":
img_paths.append(tree.get('warped_DTIscalar'))
nln_template_path = tree.get('DTI_nln_template')
jobcmd = func_to_cmd(averageImages, args=(img_paths, nln_template_path, True), tmp_dir=script_dir,
jobcmd = func_to_cmd(averageImages, args=(img_paths, nln_template_path, 'median', True), tmp_dir=script_dir,
kwargs=None, clean="never")
job_ids[47] = submitJob(tag+'_'+task_name, log_dir, command=jobcmd, queue=cpuq,
wait_for=[job_ids[42]])
......@@ -1736,7 +1752,7 @@ if __name__ == "__main__":
img_paths.append(tree.get('warped_T1brain_mask'))
nln_template_path = tree.get('T1_brain_mask_nln_template')
jobcmd = func_to_cmd(averageImages, args=(img_paths, nln_template_path, False), tmp_dir=script_dir,
jobcmd = func_to_cmd(averageImages, args=(img_paths, nln_template_path, 'average', False), tmp_dir=script_dir,
kwargs=None, clean="never")
job_ids[49] = submitJob(tag+'_'+task_name, log_dir, command=jobcmd, queue=cpuq,
wait_for=[job_ids[38]])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment