How to use the nibabel.streamlines.save function in nibabel

To help you get started, we’ve selected a few nibabel 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_outlier_rejection.py View on Github external
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)
github nipy / dipy / dipy / io / streamline.py View on Github external
header = {}
        header[Field.VOXEL_TO_RASMM] = affine.copy()
        header[Field.VOXEL_SIZES] = vox_size
        header[Field.DIMENSIONS] = shape
        header[Field.VOXEL_ORDER] = "".join(aff2axcodes(affine))

    if reduce_memory_usage and not callable(streamlines):
        sg = lambda: (s for s in streamlines)
    else:
        sg = streamlines

    tractogram_loader = LazyTractogram if reduce_memory_usage else Tractogram
    tractogram = tractogram_loader(sg)
    tractogram.affine_to_rasmm = affine
    track_file = tractogram_file(tractogram, header=header)
    nib.streamlines.save(track_file, fname)
github dPys / PyNets / pynets / diffconnectometry.py View on Github external
del dg
        try:
            del csd_fit
        except:
            pass
        try:
            del response
        except:
            pass
        try:
            del csd_model
        except:
            pass
        streamlines = Streamlines(streamline_generator, buffer_size=512)

    save_trk(Tractogram(streamlines, affine_to_rasmm=dwi_img.affine), 'prob_streamlines.trk')
    tracks = [sl for sl in streamlines if len(sl) > 1]
    labels_data = labels_img.get_data().astype('int')
    labels_affine = labels_img.affine
    conn_matrix, grouping = utils.connectivity_matrix(tracks, labels_data, affine=labels_affine,
                                                      return_mapping=True, mapping_as_streamlines=True, symmetric=True)
    conn_matrix[:3, :] = 0
    conn_matrix[:, :3] = 0

    return conn_matrix
github nipy / dipy / dipy / workflows / segment.py View on Github external
"""
        logging.info('### Labels to Bundles ###')

        io_it = self.get_io_iterator()
        for sf, lb, out_bundle in io_it:

            logging.info(sf)
            tractogram_obj = nib.streamlines.load(sf)
            streamlines = tractogram_obj.streamlines

            logging.info(lb)
            location = np.load(lb)
            logging.info('Saving output files ...')
            new_tractogram = nib.streamlines.Tractogram(streamlines[location],
                                                        affine_to_rasmm=np.eye(4))
            nib.streamlines.save(new_tractogram, out_bundle,
                                 header=tractogram_obj.header)
            logging.info(out_bundle)
github nipy / nibabel / nibabel / cmdline / tck2trk.py View on Github external
filename, _ = os.path.splitext(tractogram)
        output_filename = filename + '.trk'
        if os.path.isfile(output_filename) and not args.force:
            msg = "Skipping existing file: '{}'. Use -f to overwrite."
            print(msg.format(output_filename))
            continue

        # Build header using infos from the anatomical image.
        header = {}
        header[Field.VOXEL_TO_RASMM] = nii.affine.copy()
        header[Field.VOXEL_SIZES] = nii.header.get_zooms()[:3]
        header[Field.DIMENSIONS] = nii.shape[:3]
        header[Field.VOXEL_ORDER] = "".join(aff2axcodes(nii.affine))

        tck = nib.streamlines.load(tractogram)
        nib.streamlines.save(tck.tractogram, output_filename, header=header)
github scilus / scilpy / scripts / scil_outlier_rejection.py View on Github external
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)
github nipy / dipy / dipy / workflows / align.py View on Github external
logging.info('Saving output file {0}'.format(out_affine_file))
            np.savetxt(out_affine_file, affine)

            logging.info('Saving output file {0}'
                         .format(static_centroids_file))
            new_tractogram = nib.streamlines.Tractogram(centroids_static,
                                                        affine_to_rasmm=np.eye(4))
            nib.streamlines.save(new_tractogram, static_centroids_file,
                                 header=static_header)

            logging.info('Saving output file {0}'
                         .format(moving_centroids_file))
            new_tractogram = nib.streamlines.Tractogram(centroids_moving,
                                                        affine_to_rasmm=np.eye(4))
            nib.streamlines.save(new_tractogram, moving_centroids_file,
                                 header=moving_header)

            centroids_moved = transform_streamlines(centroids_moving, affine)

            logging.info('Saving output file {0}'
                         .format(moved_centroids_file))

            new_tractogram = nib.streamlines.Tractogram(centroids_moved,
                                                        affine_to_rasmm=np.eye(4))
            nib.streamlines.save(new_tractogram, moved_centroids_file,
                                 header=moving_header)
github scilus / scilpy / scripts / scil_compute_tracking_dipy.py View on Github external
compress_streamlines(s, args.compress)
            for s in filtered_streamlines)

    tractogram = LazyTractogram(lambda: filtered_streamlines,
                                affine_to_rasmm=seed_img.affine)

    # TODO replace with create_header_from_image after PR #9
    header = {
        Field.VOXEL_TO_RASMM: seed_img.affine.copy(),
        Field.VOXEL_SIZES: seed_img.header.get_zooms(),
        Field.DIMENSIONS: seed_img.shape,
        Field.VOXEL_ORDER: ''.join(aff2axcodes(seed_img.affine))
    }

    # Use generator to save the streamlines on-the-fly
    nib.streamlines.save(tractogram, args.output_file, header=header)