Пример #1
0
def mrtrix2fsl(mrtrix_filename,
               fsl_bval_filename=None,
               fsl_bvec_filename=None):
    """
    Convert a mrtrix encoding.b file to fsl dir_grad.bvec/.bval files.

    Parameters
    ----------
    mrtrix_filename : str
        path to mrtrix encoding.b file.
    fsl_bval_filename: str
        path to the output fsl bval file. Default is
        mrtrix_filename.bval.
    fsl_bvec_filename: str
        path to the output fsl bvec file. Default is
        mrtrix_filename.bvec.
    Returns
    -------
    """

    mrtrix_b = np.loadtxt(mrtrix_filename)
    if not len(mrtrix_b.shape) == 2 or not mrtrix_b.shape[1] == 4:
        raise ValueError('mrtrix file must have 4 columns')

    points = np.array([mrtrix_b[:, 0], mrtrix_b[:, 1], mrtrix_b[:, 2]])
    shells = np.array(mrtrix_b[:, 3])

    bvals = np.unique(shells).tolist()
    shell_idx = [int(np.where(bval == bvals)[0]) for bval in shells]

    save_gradient_sampling_fsl(points,
                               shell_idx,
                               bvals,
                               filename_bval=fsl_bval_filename,
                               filename_bvec=fsl_bvec_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)