def merge_blip_down_blip_up_first_temporary_window(blip_down_blip_up_temporary_window_file, \ blip_down_temporary_window_file, \ blip_up_temporary_window_file): # Merge nii files of different phase # Assuming that fslsplit correctly appended # 0000 for the first temporary window # at the end of the file name # Assuming that corresponding data # for both blip directions already # exist in output_directory process_msg_prefix = "PID %i: " % os.getpid() pre_command = 'FSLOUTPUTTYPE=NIFTI' command = 'fslmerge -t ' + '"' + blip_down_blip_up_temporary_window_file + \ '"' + ' ' + '"' + blip_down_temporary_window_file + \ '"' + ' ' + '"' + blip_up_temporary_window_file + '"' full_command = pre_command + ' && ' + command run_shell_command(full_command) print(process_msg_prefix + "Successfully merged " + \ blip_down_temporary_window_file + " with " + \ blip_up_temporary_window_file + " into the file " + \ blip_down_blip_up_temporary_window_file + \ " using fslmerge in subprocess shell call")
def topup_compute(merged_image_for_topup_compute_file, \ datain, config): process_msg_prefix = "PID %i: " % os.getpid() output_base_name = merged_image_for_topup_compute_file[:-len(".nii")] out_name = output_base_name + "_generic_out" fout_name = output_base_name + "_field" iout_name = output_base_name + "_corrected" pre_command = 'FSLOUTPUTTYPE=NIFTI' command = 'topup --imain=' + '"' + merged_image_for_topup_compute_file + \ '"' + ' ' + '--datain=' + '"' + datain + \ '"' + ' ' + '--config=' + '"' + config + \ '"' + ' ' + '--out=' + '"' + out_name + \ '"' + ' ' + '--fout=' + '"' + fout_name + \ '"' + ' ' + '--iout=' + '"' + iout_name + '"' full_command = pre_command + ' && ' + command run_shell_command(full_command) print(process_msg_prefix + "Successfully computed off-resonance field " + \ fout_name + " based on " + \ merged_image_for_topup_compute_file + " and used it to correct " + \ merged_image_for_topup_compute_file + " into " + \ iout_name) return iout_name + ".nii", out_name
def remove_first_and_last_slices_and_save(output_directory, file_name): process_msg_prefix = "PID %i: " % os.getpid() output_base = file_name[:-len(".nii")] output_file_name = output_base + '_postp' output_file = output_directory + "/" + output_file_name + ".nii" pre_command = 'cd ' + '"' + output_directory + '"' + \ ' && FSLOUTPUTTYPE=NIFTI && xdim=$(fslval ' + \ '"' + file_name + '"' + ' dim1) && ydim=$(fslval ' + \ '"' + file_name + '"' + ' dim2) && zdim=$(fslval ' + \ '"' + file_name + '"' + ' dim3)' # This command will extract the 2D lowest slice along z axis # to the file output_zmin.nii command = 'fslroi ' + '"' + file_name + '"' + \ ' ' + '"' + output_file_name + '"' + \ ' ' + '0 $xdim' + \ ' ' + '0 $ydim' + \ ' ' + '1 $((zdim-2))' # Concatenate all the commands into a one-liner command (a large string string to be evaluated in a shell environment) full_command = pre_command + ' && ' + command run_shell_command(full_command) print(process_msg_prefix + "Successfully removed " + \ "first and last z slice from" + \ output_directory + "/" + file_name + \ ", and saved to" + \ output_file) return output_file
def topup_apply(prepared_4D_file, datain, topup_out_base_name_file): process_msg_prefix = "PID %i: " % os.getpid() output_base_name = prepared_4D_file[:-len(".nii")] out_name = output_base_name + "_applytopup" pre_command = 'FSLOUTPUTTYPE=NIFTI' command = 'applytopup --imain=' + '"' + prepared_4D_file[:-len(".nii")] + \ '"' + ' ' + '--inindex=2' + \ ' ' + '--datain=' + '"' + datain + \ '"' + ' ' + '--topup=' + '"' + topup_out_base_name_file + \ '"' + ' ' + '--out=' + '"' + out_name + '" --method=jac' full_command = pre_command + ' && ' + command run_shell_command(full_command) print(process_msg_prefix + "Successfully ran applytopup on " + \ prepared_4D_file + " based on topup output" + \ topup_out_base_name_file + "*") return out_name + ".nii"
def highres_to_lowres_registration(highres_file, \ lowres_file, \ output_directory, \ registration_desc): # wraps around FreeSurfer's # mri_robust_register . # Returns the mapmov output file. process_msg_prefix = "PID %i: " % os.getpid() options = "--nosym --noinit --satit --iscale --verbose 2" lta_file = output_directory + \ "/" + \ registration_desc + \ "_lta.lta" mapmov_file = output_directory + \ "/" + \ registration_desc + \ "_mapmov.nii" mapmovhdr_file = output_directory + \ "/" + \ registration_desc + \ "_mapmovhdr.nii" weights_file = output_directory + \ "/" + \ registration_desc + \ "_weights.nii" bash_run_log_file = output_directory + \ "/" + \ registration_desc + \ "_bash_run_log.txt" bash_run_log_command = "2>&1 | tee" full_command = 'mri_robust_register ' + \ '--mov "' + highres_file + '" ' + \ '--dst "' + lowres_file + '" ' + \ '--lta "' + lta_file + '" ' + \ '--mapmov "' + mapmov_file + '" ' + \ '--mapmovhdr "' + mapmovhdr_file + '" ' + \ '--weights "' + weights_file + '" ' + \ options + ' ' + \ bash_run_log_command + ' ' + \ '"' + bash_run_log_file + '"' run_shell_command(full_command) print(process_msg_prefix + "Ran mri_robust_register " + \ "with registration_desc: " + \ registration_desc + " "\ "with --mov " + \ highres_file + " " + \ "and --dst " + \ lowres_file) return mapmov_file
def add_duplicate_slices(output_directory, file_name): process_msg_prefix = "PID %i: " % os.getpid() output_base = file_name[:-len(".nii")] output_zmin = output_base + '_zmin' output_zmax = output_base + '_zmax' output_prep = output_base + '_prep_topup' output_prep_file = output_directory + "/" + output_prep + ".nii" pre_command = 'cd ' + '"' + output_directory + '"' + \ ' && FSLOUTPUTTYPE=NIFTI && xdim=$(fslval ' + \ '"' + file_name + '"' + ' dim1) && ydim=$(fslval ' + \ '"' + file_name + '"' + ' dim2) && zdim=$(fslval ' + \ '"' + file_name + '"' + ' dim3)' # This command will extract the 2D lowest slice along z axis # to the file output_zmin.nii output_zmin_command = 'fslroi ' + '"' + file_name + '"' + \ ' ' + '"' + output_zmin + '"' + \ ' ' + '0 $xdim' + \ ' ' + '0 $ydim' + \ ' ' + '0 1' # This command will extract the 2D highest slice along z axis # to the file output_zmax.nii output_zmax_command = 'fslroi ' + '"' + file_name + '"' + \ ' ' + '"' + output_zmax + '"' + \ ' ' + '0 $xdim' + \ ' ' + '0 $ydim' + \ ' ' + '$((zdim-1)) 1' # This command will merge the two extracted 2D slices # output_zmin.nii and output_zmax.nii to file , # to the file output_prep.nii output_prep_command = 'fslmerge -z ' + '"' + output_prep + '"' + \ ' ' + '"' + output_zmin + '"' + \ ' ' + '"' + file_name + '"' + \ ' ' + '"' + output_zmax + '"' # Concatenate all the commands into a one-liner command (a large string string to be evaluated in a shell environment) full_command = pre_command + ' && ' + output_zmin_command + ' && ' + output_zmax_command + ' && ' + output_prep_command run_shell_command(full_command) print(process_msg_prefix + "Successfully merged " + \ "duplicate zmin and zmax slices to " + \ output_directory + "/" + file_name + \ ", thereby creating " + \ output_prep_file + \ " for FSL topup") return output_prep_file
def convert_mgz_to_nii(mgz_file): process_msg_prefix = "PID %i: " % os.getpid() nii_file = mgz_file[:-len(".mgz")] + ".nii" full_command = 'mri_convert ' + \ '"' + mgz_file + '"' + ' ' + \ '"' + nii_file + '"' run_shell_command(full_command) print(process_msg_prefix + "convert_mgz_to_nii: Successfully converted " + \ mgz_file + " to " + \ nii_file) return nii_file
def split_along_temporary_axis_and_save(output_directory, \ blip_file, \ blip_file_name): # blip_file is the relative path from script root + file name # blip_file_name is the file name only process_msg_prefix = "PID %i: " % os.getpid() output_base_name = output_directory + "/" + blip_file_name[:-len(".nii")] + "_" pre_command = 'FSLOUTPUTTYPE=NIFTI' command = 'fslsplit ' + '"' + blip_file + '"' + ' ' + '"' + \ output_base_name + '"' + ' -t' full_command = pre_command + ' && ' + command run_shell_command(full_command) print(process_msg_prefix + "extract_first_temporary_window_and_save: Successfully split " + \ blip_file + " into directory " + output_directory + \ " using fslsplit in subprocess shell call")
def copy_header(source_nii_file, dest_nii_file): # Make NIFTI header of dest_nii_file # equal to NIFTI header or source_nii_file process_msg_prefix = "PID %i: " % os.getpid() pre_command = 'FSLOUTPUTTYPE=NIFTI' # This command will extract the 2D lowest slice along z axis # to the file output_zmin.nii command = 'fslcpgeom ' + \ '"' + source_nii_file + '"' + ' ' + \ '"' + dest_nii_file + '"' # Concatenate all the commands into a one-liner command (a large string string to be evaluated in a shell environment) full_command = pre_command + ' && ' + command run_shell_command(full_command) print(process_msg_prefix + "Successfully replaced the geometry header info of " + \ dest_nii_file + " with the the geometry header info of " + \ source_nii_file)