How to use the dipy.io.stateful_tractogram.StatefulTractogram.from_sft function in dipy

To help you get started, we’ve selected a few dipy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scilus / scilpy / scripts / scil_decompose_connectivity.py View on Github external
_save_if_needed(raw_sft, hdf5_file, args,
                        'raw', 'raw', in_label, out_label)

        # Doing all post-processing
        if not args.no_pruning:
            valid_length_ids, invalid_length_ids = _prune_segments(
                raw_sft.streamlines,
                args.min_length,
                args.max_length,
                vox_sizes[0])

            valid_length = ArraySequence(connecting_streamlines)[
                valid_length_ids]
            invalid_length = ArraySequence(connecting_streamlines)[
                invalid_length_ids]
            invalid_length_sft = StatefulTractogram.from_sft(
                invalid_length, raw_sft,
                data_per_streamline=raw_sft.data_per_streamline[invalid_length_ids],
                data_per_point={})

            _save_if_needed(invalid_length_sft, hdf5_file, args,
                            'discarded', 'invalid_length',
                            in_label, out_label)
        else:
            valid_length = connecting_streamlines
            valid_length_ids = range(len(connecting_streamlines))

        if not len(valid_length):
            continue

        valid_length_sft = StatefulTractogram.from_sft(
            valid_length, raw_sft,
github scilus / scilpy / scripts / scil_decompose_connectivity.py View on Github external
qb_curv_ids = np.setdiff1d(np.arange(len(inliers)),
                                       no_qb_curv_ids)
            qb_curv = inliers[qb_curv_ids]

            qb_curv_sft = StatefulTractogram.from_sft(
                qb_curv, inliers_sft,
                data_per_streamline=inliers_sft.data_per_streamline[qb_curv_ids],
                data_per_point={})

            _save_if_needed(qb_curv_sft, hdf5_file, args,
                            'discarded', 'qb_curv', in_label, out_label)
        else:
            no_qb_curv = inliers
            no_qb_curv_ids = range(len(inliers))

        no_qb_curv_sft = StatefulTractogram.from_sft(
            no_qb_curv, inliers_sft,
            data_per_streamline=inliers_sft.data_per_streamline[no_qb_curv_ids],
            data_per_point={})

        _save_if_needed(no_qb_curv_sft, hdf5_file, args,
                        'final', 'final', in_label, out_label)

    hdf5_file.close()
    time2 = time.time()
    logging.info(
        '    Connections post-processing and saving took {} sec.'.format(
            round(time2 - time1, 2)))
github scilus / scilpy / scripts / scil_decompose_connectivity.py View on Github external
_save_if_needed(inliers_sft, hdf5_file, args,
                        'intermediate', 'inliers', in_label, out_label)

        if not args.no_remove_curv_dev:
            no_qb_curv_ids = remove_loops_and_sharp_turns(
                inliers,
                args.loop_max_angle,
                use_qb=True,
                qb_threshold=args.curv_qb_distance)
            no_qb_curv = inliers[no_qb_curv_ids]
            qb_curv_ids = np.setdiff1d(np.arange(len(inliers)),
                                       no_qb_curv_ids)
            qb_curv = inliers[qb_curv_ids]

            qb_curv_sft = StatefulTractogram.from_sft(
                qb_curv, inliers_sft,
                data_per_streamline=inliers_sft.data_per_streamline[qb_curv_ids],
                data_per_point={})

            _save_if_needed(qb_curv_sft, hdf5_file, args,
                            'discarded', 'qb_curv', in_label, out_label)
        else:
            no_qb_curv = inliers
            no_qb_curv_ids = range(len(inliers))

        no_qb_curv_sft = StatefulTractogram.from_sft(
            no_qb_curv, inliers_sft,
            data_per_streamline=inliers_sft.data_per_streamline[no_qb_curv_ids],
            data_per_point={})

        _save_if_needed(no_qb_curv_sft, hdf5_file, args,
github scilus / scilpy / scripts / scil_assign_color_to_trk.py View on Github external
if len(args.color) == 8:
        color_int = int(args.color, 0)
        red = color_int >> 16
        green = (color_int & 0x00FF00) >> 8
        blue = color_int & 0x0000FF
    else:
        parser.error('Hexadecimal RGB color should be formatted as "#RRGGBB"'
                     ' or 0xRRGGBB.')

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

    sft.data_per_point["color"] = [np.tile([red, green, blue],
                                           (len(i), 1)) for i in sft.streamlines]

    sft = StatefulTractogram.from_sft(sft.streamlines, sft,
                                      data_per_point=sft.data_per_point)

    save_tractogram(sft, args.out_tractogram)
github scilus / scilpy / scilpy / segment / streamlines.py View on Github external
elif filter_type == 'either_end':
        selected_by_cuboid = np.union1d(line_based_indices_1,
                                        line_based_indices_2)

    # If the 'exclude' option is used, the selection is inverted
    if is_exclude:
        selected_by_cuboid = np.setdiff1d(range(len(sft)),
                                          np.unique(selected_by_cuboid))
    line_based_indices = np.asarray(selected_by_cuboid).astype(np.int32)

    # From indices to sft
    streamlines = sft.streamlines[line_based_indices]
    data_per_streamline = sft.data_per_streamline[line_based_indices]
    data_per_point = sft.data_per_point[line_based_indices]

    new_sft = StatefulTractogram.from_sft(streamlines, sft,
                                          data_per_streamline=data_per_streamline,
                                          data_per_point=data_per_point)

    return new_sft, line_based_indices
github scilus / scilpy / scripts / scil_evaluate_bundles_pairwise_agreement_measures.py View on Github external
if disable_centroids:
            centroids = []
        else:
            centroids = qbx_and_merge(streamlines, thresholds,
                                      rng=RandomState(0),
                                      verbose=False).centroids

        # Saving tmp files to save on future computation
        nib.save(nib.Nifti1Image(density.astype(np.float32), transformation),
                 tmp_density_filename)
        nib.save(nib.Nifti1Image(endpoints_density.astype(np.int16),
                                 transformation),
                 tmp_endpoints_filename)

        # Saving in vox space and corner.
        centroids_sft = StatefulTractogram.from_sft(centroids, sft)
        save_tractogram(centroids_sft, tmp_centroids_filename)

    return density, endpoints_density, streamlines, centroids
github scilus / scilpy / scilpy / tracking / tools.py View on Github external
def _warn_and_save(new_streamlines, sft):
    """Last step of the two resample functions:
    Warn that we loose data_per_point, then create resampled SFT."""

    if sft.data_per_point is not None:
        logging.debug("Initial stateful tractogram contained data_per_point. "
                      "This information will not be carried in the final"
                      "tractogram.")
    new_sft = StatefulTractogram.from_sft(
        new_streamlines, sft, data_per_streamline=sft.data_per_streamline)

    return new_sft
github scilus / scilpy / scripts / scil_decompose_connectivity.py View on Github external
no_loops_sft = StatefulTractogram.from_sft(
            no_loops, valid_length_sft,
            data_per_streamline=valid_length_sft.data_per_streamline[no_loop_ids],
            data_per_point={})

        _save_if_needed(no_loops_sft, hdf5_file, args,
                        'intermediate', 'no_loops', in_label, out_label)

        if not args.no_remove_outliers:
            outliers_ids, inliers_ids = remove_outliers(no_loops,
                                                        args.outlier_threshold)
            outliers = no_loops[outliers_ids]
            inliers = no_loops[inliers_ids]

            outliers_sft = StatefulTractogram.from_sft(
                outliers, no_loops_sft,
                data_per_streamline=no_loops_sft.data_per_streamline[outliers_ids],
                data_per_point={})

            _save_if_needed(outliers_sft, hdf5_file, args,
                            'discarded', 'outliers', in_label, out_label)
        else:
            inliers = no_loops
            inliers_ids = range(len(no_loops))

        if not len(inliers):
            continue

        inliers_sft = StatefulTractogram.from_sft(
            inliers, no_loops_sft,
            data_per_streamline=no_loops_sft.data_per_streamline[inliers_ids],
github scilus / scilpy / scripts / scil_decompose_connectivity.py View on Github external
invalid_length_sft = StatefulTractogram.from_sft(
                invalid_length, raw_sft,
                data_per_streamline=raw_sft.data_per_streamline[invalid_length_ids],
                data_per_point={})

            _save_if_needed(invalid_length_sft, hdf5_file, args,
                            'discarded', 'invalid_length',
                            in_label, out_label)
        else:
            valid_length = connecting_streamlines
            valid_length_ids = range(len(connecting_streamlines))

        if not len(valid_length):
            continue

        valid_length_sft = StatefulTractogram.from_sft(
            valid_length, raw_sft,
            data_per_streamline=raw_sft.data_per_streamline[valid_length_ids],
            data_per_point={})

        _save_if_needed(valid_length_sft, hdf5_file, args,
                        'intermediate', 'valid_length', in_label, out_label)

        if not args.no_remove_loops:
            no_loop_ids = remove_loops_and_sharp_turns(valid_length,
                                                       args.loop_max_angle)
            no_loops = valid_length[no_loop_ids]
            loop_ids = np.setdiff1d(np.arange(len(valid_length)), no_loop_ids)
            loops = valid_length[loop_ids]

            loops_sft = StatefulTractogram.from_sft(
                loops, valid_length_sft,
github scilus / scilpy / scripts / scil_evaluate_bundles_pairwise_agreement_measures.py View on Github external
if disable_centroids:
            centroids = []
        else:
            centroids = qbx_and_merge(streamlines, thresholds,
                                      rng=RandomState(0),
                                      verbose=False).centroids

        # Saving tmp files to save on future computation
        nib.save(nib.Nifti1Image(density.astype(np.float32), transformation),
                 tmp_density_filename)
        nib.save(nib.Nifti1Image(endpoints_density.astype(np.int16),
                                 transformation),
                 tmp_endpoints_filename)

        # Saving in vox space and corner.
        centroids_sft = StatefulTractogram.from_sft(centroids, sft)
        save_tractogram(centroids_sft, tmp_centroids_filename)

    return density, endpoints_density, streamlines, centroids