0

I want to save each file with a different file name. I know how to do it for individual files as follows:

nib.Nifti1Image(wt, affine).to_filename(os.path.join(output_dir, data_file.root.subject_ids[data_index].decode()+"_unc_whole.nii.gz"))
nib.Nifti1Image(tc, affine).to_filename(os.path.join(output_dir,data_file.root.subject_ids[data_index].decode()+ "_unc_core.nii.gz"))
nib.Nifti1Image(et, affine).to_filename(os.path.join(output_dir,data_file.root.subject_ids[data_index].decode()+ "_unc_enhance.nii.gz")) 
nib.Nifti1Image(alea, affine).to_filename(os.path.join(output_dir, data_file.root.subject_ids[data_index].decode()+"_alea.nii.gz"))

How can I do this with a for loop so that I don't have to repeat the function for each individual file?

1 Answer 1

1

As a simple solution, you could use zip:

alist = [wt, tc, et, alea]
blist = ["_unc_whole.nii.gz", "_unc_core.nii.gz", "_unc_enhance.nii.gz", "_alea.nii.gz"]

for a, b in zip(alist, blist):
    nib.Nifti1Image(a, affine).to_filename(os.path.join(output_dir, data_file.root.subject_ids[data_index].decode() + b))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.