Example #1
0
def main():
    parser = _build_arg_parser()
    args = parser.parse_args()

    assert_inputs_exist(parser, args.in_bundle)
    assert_outputs_exist(parser, args, args.out_bundle, args.remaining_bundle)
    if args.alpha <= 0 or args.alpha > 1:
        parser.error('--alpha should be ]0, 1]')

    sft = load_tractogram_with_reference(parser, args, args.in_bundle)
    if len(sft) == 0:
        logging.warning("Bundle file contains no streamline")
        return

    check_tracts_same_format(
        parser, [args.in_bundle, args.out_bundle, args.remaining_bundle])
    outliers, inliers = remove_outliers(sft.streamlines, args.alpha)

    inliers_sft = sft[inliers]
    outliers_sfts = sft[outliers]

    if len(inliers) == 0:
        logging.warning("All streamlines are considered outliers."
                        "Please lower the --alpha parameter")
    else:
        save_tractogram(inliers_sft, args.out_bundle)

    if len(outliers) == 0:
        logging.warning("No outlier found. Please raise the --alpha parameter")
    elif args.remaining_bundle:
        save_tractogram(outliers_sfts, args.remaining_bundle)
Example #2
0
def main():
    parser = _build_arg_parser()
    args = parser.parse_args()

    assert_inputs_exist(parser, args.in_bundle)
    assert_outputs_exist(parser, args, args.out_bundle, args.remaining_bundle)
    if args.alpha <= 0 or args.alpha > 1:
        parser.error('--alpha should be ]0, 1]')

    tractogram = nib.streamlines.load(args.in_bundle)

    if int(tractogram.header['nb_streamlines']) == 0:
        logging.warning("Bundle file contains no streamline")
        return

    check_tracts_same_format(
        parser, [args.in_bundle, args.out_bundle, args.remaining_bundle])

    streamlines = tractogram.streamlines

    summary = outliers_removal_using_hierarchical_quickbundles(streamlines)
    outliers, inliers = prune(streamlines, args.alpha, summary)

    inliers_streamlines = tractogram.streamlines[inliers]
    inliers_data_per_streamline = tractogram.tractogram.data_per_streamline[
        inliers]
    inliers_data_per_point = tractogram.tractogram.data_per_point[inliers]

    outliers_streamlines = tractogram.streamlines[outliers]
    outliers_data_per_streamline = tractogram.tractogram.data_per_streamline[
        outliers]
    outliers_data_per_point = tractogram.tractogram.data_per_point[outliers]

    if len(inliers_streamlines) == 0:
        logging.warning("All streamlines are considered outliers."
                        "Please lower the --alpha parameter")
    else:
        inliers_tractogram = Tractogram(
            inliers_streamlines,
            affine_to_rasmm=np.eye(4),
            data_per_streamline=inliers_data_per_streamline,
            data_per_point=inliers_data_per_point)
        nib.streamlines.save(inliers_tractogram,
                             args.out_bundle,
                             header=tractogram.header)

    if len(outliers_streamlines) == 0:
        logging.warning("No outlier found. Please raise the --alpha parameter")
    elif args.remaining_bundle:
        outlier_tractogram = Tractogram(
            outliers_streamlines,
            affine_to_rasmm=np.eye(4),
            data_per_streamline=outliers_data_per_streamline,
            data_per_point=outliers_data_per_point)
        nib.streamlines.save(outlier_tractogram,
                             args.remaining_bundle,
                             header=tractogram.header)
def main():
    parser = _build_arg_parser()
    args = parser.parse_args()

    assert_inputs_exist(parser, args.in_tractogram)
    assert_outputs_exist(parser, args, args.out_tractogram,
                         optional=args.looping_tractogram)
    check_tracts_same_format(parser, [args.in_tractogram, args.out_tractogram,
                                      args.looping_tractogram])

    if args.threshold <= 0:
        parser.error('Threshold "{}" '.format(args.threshold) +
                     'must be greater than 0')

    if args.angle <= 0:
        parser.error('Angle "{}" '.format(args.angle) +
                     'must be greater than 0')

    tractogram = load_tractogram_with_reference(
        parser, args, args.in_tractogram)

    streamlines = tractogram.streamlines

    ids_c = []

    ids_l = []

    if len(streamlines) > 1:
        ids_c = remove_loops_and_sharp_turns(
            streamlines, args.angle, use_qb=args.qb,
            qb_threshold=args.threshold)
        ids_l = np.setdiff1d(np.arange(len(streamlines)), ids_c)
    else:
        parser.error(
            'Zero or one streamline in {}'.format(args.in_tractogram) +
            '. The file must have more than one streamline.')

    if len(ids_c) > 0:
        sft_c = filter_tractogram_data(tractogram, ids_c)
        save_tractogram(sft_c, args.out_tractogram)
    else:
        logging.warning(
            'No clean streamlines in {}'.format(args.in_tractogram))

    if args.display_counts:
        sc_bf = len(tractogram.streamlines)
        sc_af = len(sft_c.streamlines)
        print(json.dumps({'streamline_count_before_filtering': int(sc_bf),
                         'streamline_count_after_filtering': int(sc_af)},
                         indent=args.indent))

    if len(ids_l) == 0:
        logging.warning('No loops in {}'.format(args.in_tractogram))
    elif args.looping_tractogram:
        sft_l = filter_tractogram_data(tractogram, ids_l)
        save_tractogram(sft_l, args.looping_tractogram)
Example #4
0
def main():
    parser = _build_arg_parser()
    args = parser.parse_args()

    assert_inputs_exist(parser, args.in_tractogram)
    assert_outputs_exist(parser,
                         args,
                         args.out_tractogram,
                         optional=args.remaining_tractogram)
    check_tracts_same_format(
        parser,
        [args.in_tractogram, args.out_tractogram, args.remaining_tractogram])

    if not (-1 <= args.minU <= 1 and -1 <= args.maxU <= 1):
        parser.error('Min-Max ufactor "{},{}" '.format(args.minU, args.maxU) +
                     'must be between -1 and 1.')

    sft = load_tractogram_with_reference(parser, args, args.in_tractogram)

    ids_c = detect_ushape(sft, args.minU, args.maxU)
    ids_l = np.setdiff1d(np.arange(len(sft.streamlines)), ids_c)

    if len(ids_c) == 0:
        if args.no_empty:
            logging.debug("The file {} won't be written "
                          "(0 streamline).".format(args.out_tractogram))
            return

        logging.debug('The file {} contains 0 streamline.'.format(
            args.out_tractogram))

    save_tractogram(sft[ids_c], args.out_tractogram)

    if args.display_counts:
        sc_bf = len(sft.streamlines)
        sc_af = len(ids_c)
        print(
            json.dumps(
                {
                    'streamline_count_before_filtering': int(sc_bf),
                    'streamline_count_after_filtering': int(sc_af)
                },
                indent=args.indent))

    if args.remaining_tractogram:
        if len(ids_l) == 0:
            if args.no_empty:
                logging.debug("The file {} won't be written (0 streamline"
                              ").".format(args.remaining_tractogram))
                return

            logging.warning('No remaining streamlines.')

        save_tractogram(sft[ids_l], args.remaining_tractogram)
Example #5
0
def main():
    parser = _build_arg_parser()
    args = parser.parse_args()

    assert_inputs_exist(parser, args.in_tractogram)
    assert_outputs_exist(parser,
                         args,
                         args.out_tractogram,
                         optional=args.remaining_tractogram)
    check_tracts_same_format(
        parser,
        [args.in_tractogram, args.out_tractogram, args.remaining_tractogram])

    if args.threshold <= 0:
        parser.error('Threshold "{}" '.format(args.threshold) +
                     'must be greater than 0')

    if args.angle <= 0:
        parser.error('Angle "{}" '.format(args.angle) +
                     'must be greater than 0')

    tractogram = nib.streamlines.load(args.in_tractogram)
    streamlines = tractogram.streamlines

    streamlines_c = []
    loops = []
    if len(streamlines) > 1:
        streamlines_c, loops = remove_loops_and_sharp_turns(
            streamlines, args.angle, args.qb, args.threshold)
    else:
        parser.error(
            'Zero or one streamline in {}'.format(args.in_tractogram) +
            '. The file must have more than one streamline.')

    if len(streamlines_c) > 0:
        tractogram_c = nib.streamlines.Tractogram(streamlines_c,
                                                  affine_to_rasmm=np.eye(4))
        nib.streamlines.save(tractogram_c,
                             args.out_tractogram,
                             header=tractogram.header)
    else:
        logging.warning('No clean streamlines in {}'.format(
            args.in_tractogram))

    if len(loops) == 0:
        logging.warning('No loops in {}'.format(args.in_tractogram))
    elif args.remaining_tractogram:
        tractogram_l = nib.streamlines.Tractogram(loops,
                                                  affine_to_rasmm=np.eye(4))
        nib.streamlines.save(tractogram_l,
                             args.remaining_tractogram,
                             header=tractogram.header)
Example #6
0
def main():
    parser = _build_arg_parser()
    args = parser.parse_args()

    assert_inputs_exist(parser, args.in_tractogram)
    assert_outputs_exist(parser,
                         args,
                         args.out_tractogram,
                         optional=args.remaining_tractogram)
    check_tracts_same_format(
        parser,
        [args.in_tractogram, args.out_tractogram, args.remaining_tractogram])

    if not (-1 <= args.minU <= 1 and -1 <= args.maxU <= 1):
        parser.error('Min-Max ufactor "{},{}" '.format(args.minU, args.maxU) +
                     'must be between -1 and 1.')

    sft = load_tractogram_with_reference(parser, args, args.in_tractogram)

    ids_c = []
    ids_l = []

    if len(sft.streamlines) > 1:
        ids_c = detect_ushape(sft, args.minU, args.maxU)
        ids_l = np.setdiff1d(np.arange(len(sft.streamlines)), ids_c)
    else:
        parser.error(
            'Zero or one streamline in {}'.format(args.in_tractogram) +
            '. The file must have more than one streamline.')

    if len(ids_c) > 0:
        save_tractogram(sft[ids_c], args.out_tractogram)
    else:
        logging.warning('No u-shape streamlines in {}'.format(
            args.in_tractogram))

    if args.display_counts:
        sc_bf = len(sft.streamlines)
        sc_af = len(ids_c)
        print(
            json.dumps(
                {
                    'streamline_count_before_filtering': int(sc_bf),
                    'streamline_count_after_filtering': int(sc_af)
                },
                indent=args.indent))

    if len(ids_l) == 0:
        logging.warning('No remaining streamlines '
                        'in {}'.format(args.remaining_tractogram))
    elif args.remaining_tractogram:
        save_tractogram(sft[ids_l], args.remaining_tractogram)
def main():
    parser = _build_arg_parser()
    args = parser.parse_args()

    assert_inputs_exist(parser, [args.tracts, args.ref_anat])
    assert_outputs_exists(parser, args, [args.out])
    check_tracts_support(parser, args.tracts, False)
    check_tracts_same_format(parser, args.tracts, args.out)

    # Deactivated for now.
    # Tested implicitely with the 2 previous tracts checks.
    # if not tc.is_supported(args.out):
    #     parser.error('Format of "{0}" not supported.'.format(args.out))

    filter_points(args.tracts, args.ref_anat, args.out,
                  args.nifti_compliant_gen, args.for_nifti_compliant)
Example #8
0
def main():
    parser = _build_args_parser()
    args = parser.parse_args()

    assert_inputs_exist(parser, [args.tracts])
    assert_outputs_exists(parser, args, [args.out])
    check_tracts_support(parser, args.tracts, False)
    check_tracts_same_format(parser, args.tracts, args.out)

    if args.errorRate < 0.001 or args.errorRate > 1:
        logging.warn(
            'You are using an error rate of {}.\nWe recommend setting it '
            'between 0.001 and 1.\n0.001 will do almost nothing to the tracts '
            'while 1 will higly compress/linearize the tracts'.format(
                args.errorRate))

    compression_wrapper(args.tracts, args.out, args.errorRate)