def write_gradient_information(dwi_header, b0_header, bval_path=None, bvec_path=None, flip=None, swap=None): """ Write gradient information in present in the header. Parameters ---------- header: The header with gradient info. bval_path: Path to the bval file to be saved. bvec_path: Path to the bvec path to be saved. Return ------ None """ keys = ['bvalue', 'diff_x', 'diff_y', 'diff_z'] if all(k in dwi_header for k in keys) and \ all(k in b0_header for k in keys): if bval_path: bvals = b0_header['bvalue'] + dwi_header['bvalue'] with open(bval_path, 'w') as bval_file: bval_file.write(' '.join(str(i) for i in bvals)) if bvec_path: bvecs = np.zeros( (3, len(b0_header['diff_x'] + dwi_header['diff_x']))) bvecs[0, :] = b0_header['diff_x'] + dwi_header['diff_x'] bvecs[1, :] = b0_header['diff_y'] + dwi_header['diff_y'] bvecs[2, :] = b0_header['diff_z'] + dwi_header['diff_z'] if flip: axes = [str_to_index(axis) for axis in list(flip)] for axis in axes: bvecs[axis, :] *= -1 if swap: axes = [str_to_index(axis) for axis in list(swap)] new_bvecs = np.zeros(bvecs.shape) new_bvecs[axes[0], :] = bvecs[axes[1], :] new_bvecs[axes[1], :] = bvecs[axes[0], :] bvecs = new_bvecs np.savetxt(bvec_path, bvecs, "%.8f") else: raise Exception('Could not save gradient. Some keys are missing.')
def main(): parser = build_args_parser() args = parser.parse_args() assert_inputs_exist(parser, [args.image]) assert_outputs_exists(parser, args, [args.reordered_image_path]) indices = [str_to_index(axis) for axis in list(args.axes)] if len(indices) != 3 or {0, 1, 2} != set(indices): parser.error('The axes parameter must contain x, y and z in whatever ' 'order.') img = nib.load(args.image) data = img.get_data() swaps = [axis for index, axis in enumerate(indices) if index != axis] for i in range(len(swaps) - 1): data = np.swapaxes(data, swaps[i], swaps[i + 1]) new_zooms = np.array(img.get_header().get_zooms())[list(indices)] if len(data.shape) == 4: new_zooms = np.append(new_zooms, 1.0) img.get_header().set_zooms(new_zooms) nib.Nifti1Image(data, img.get_affine(), img.get_header()). \ to_filename(args.reordered_image_path)
def main(): parser = build_arg_parser() args = parser.parse_args() assert_inputs_exist(parser, [args.encoding_file]) assert_outputs_exist(parser, args, [args.flipped_encoding]) indices = [str_to_index(axis) for axis in list(args.axes)] if args.fsl_bvecs: flip_fsl_bvecs(args.encoding_file, args.flipped_encoding, indices) else: flip_mrtrix_encoding_scheme(args.encoding_file, args.flipped_encoding, indices)
def main(): parser = _build_arg_parser() args = parser.parse_args() assert_inputs_exist(parser, args.gradient_sampling_file) assert_outputs_exist(parser, args, args.flipped_sampling_file) indices = [str_to_index(axis) for axis in list(args.axes)] _, ext = os.path.splitext(args.gradient_sampling_file) if args.fsl_bvecs: if ext == '.bvec': flip_fsl_gradient_sampling(args.gradient_sampling_file, args.flipped_sampling_file, indices) else: parser.error('Extension for FSL format should be .bvec.') elif ext == '.b': flip_mrtrix_gradient_sampling(args.gradient_sampling_file, args.flipped_sampling_file, indices) else: parser.error('Extension for MRtrix format should .b.')