def fsl2mrtrix(fsl_bval_filename, fsl_bvec_filename, mrtrix_filename): """ Convert a fsl dir_grad.bvec/.bval files to mrtrix encoding.b file. Parameters ---------- fsl_bval_filename: str path to input fsl bval file. fsl_bvec_filename: str path to input fsl bvec file. mrtrix_filename : str path to output mrtrix encoding.b file. Returns ------- """ shells = np.loadtxt(fsl_bval_filename) points = np.loadtxt(fsl_bvec_filename) bvals = np.unique(shells).tolist() if not points.shape[0] == 3: points = points.transpose() logging.warning('WARNING: Your bvecs seem transposed. ' + 'Transposing them.') shell_idx = [int(np.where(bval == bvals)[0]) for bval in shells] save_gradient_sampling_mrtrix(points, shell_idx, bvals, mrtrix_filename)
def main(): parser = _build_arg_parser() args = parser.parse_args() fsl = args.fsl mrtrix = args.mrtrix if not (fsl or mrtrix): parser.error('Select at least one save format.') return out_basename, _ = os.path.splitext(args.out_basename) if fsl: out_filename = [out_basename + '.bval', out_basename + '.bvec'] else: out_filename = out_basename + '.b' assert_outputs_exist(parser, args, out_filename) logging.basicConfig(level=logging.INFO) if args.verbose: logging.basicConfig(level=logging.DEBUG) Ks = args.nb_samples eddy = args.eddy duty = args.duty # Total number of samples K = np.sum(Ks) # Number of non-b0 shells S = len(Ks) b0_every = args.b0_every b0_end = args.b0_end b0_value = args.b0_value # Only a b0 at the beginning if (b0_every > K) or (b0_every < 0): b0_every = K + 1 # Compute bval list if args.bvals is not None: bvals = args.bvals unique_bvals = np.unique(bvals) if len(unique_bvals) != S: parser.error('You have provided {} shells '.format(S) + 'but {} unique bvals.'.format(len(unique_bvals))) elif args.b_lin_max is not None: bvals = compute_bvalue_lin_b(bmin=0.0, bmax=args.b_lin_max, nb_of_b_inside=S - 1, exclude_bmin=True) elif args.q_lin_max is not None: bvals = compute_bvalue_lin_q(bmin=0.0, bmax=args.q_lin_max, nb_of_b_inside=S - 1, exclude_bmin=True) # Add b0 b-value if b0_every != 0: bvals = add_bvalue_b0(bvals, b0_value=b0_value) # Gradient sampling generation points, shell_idx = generate_gradient_sampling( Ks, verbose=int(3 - logging.getLogger().getEffectiveLevel() // 10)) # eddy current optimization if eddy: points, shell_idx = swap_sampling_eddy(points, shell_idx) # Adding interleaved b0s if b0_every != 0: points, shell_idx = add_b0s(points, shell_idx, b0_every=b0_every, finish_b0=b0_end) # duty cycle optimization if duty: points, shell_idx = compute_min_duty_cycle_bruteforce( points, shell_idx, bvals) if fsl: save_gradient_sampling_fsl(points, shell_idx, bvals, out_filename[0], out_filename[1]) if mrtrix: if not points.shape[0] == 3: points = points.transpose() save_gradient_sampling_mrtrix(points, shell_idx, bvals, filename=out_filename)