def test_splines(): # create a helix t = np.linspace(0, 1.75 * 2 * np.pi, 100) x = np.sin(t) y = np.cos(t) z = t # add noise x += np.random.normal(scale=0.1, size=x.shape) y += np.random.normal(scale=0.1, size=y.shape) z += np.random.normal(scale=0.1, size=z.shape) xyz = np.vstack((x, y, z)).T # get the B-splines smoothed result tm.spline(xyz, 3, 2, -1)
def test_splines(): # create a helix t = np.linspace(0, 1.75*2*np.pi, 100) x = np.sin(t) y = np.cos(t) z = t # add noise x += np.random.normal(scale=0.1, size=x.shape) y += np.random.normal(scale=0.1, size=y.shape) z += np.random.normal(scale=0.1, size=z.shape) xyz = np.vstack((x, y, z)).T # get the B-splines smoothed result tm.spline(xyz, 3, 2, -1)
def convert_tck_to_trk(filename_in, filename_out, reference_affine, reference_shape, compress_err_thr=0.1, smooth=None, nr_cpus=-1, tracking_format="trk_legacy"): streamlines = nib.streamlines.load( filename_in).streamlines # Load Fibers (Tck) if smooth is not None: streamlines_smooth = [] for sl in streamlines: streamlines_smooth.append(spline(sl, s=smooth)) streamlines = streamlines_smooth #Compressing also good to remove checkerboard artefacts from tracking on peaks if compress_err_thr is not None: streamlines = compress_streamlines(streamlines, compress_err_thr, nr_cpus=nr_cpus) if tracking_format == "trk_legacy": save_streamlines_as_trk_legacy(filename_out, streamlines, reference_affine, reference_shape) else: save_streamlines(filename_out, streamlines, reference_affine, reference_shape)
def convert_tck_to_trk(filename_in, filename_out, reference_affine, compress_err_thr=0.1, smooth=None): ''' Convert tck file to trk file and compress :param filename_in: :param filename_out: :param compress_err_thr: compress fibers if setting error threshold here (default: 0.1mm) :param smooth: smooth streamlines (default: None) 10: slight smoothing, 100: very smooth from beginning to end :return: ''' #Hide large number of nipype logging outputs from nipype import config, logging config.set('execution', 'remove_unnecessary_outputs', 'true') config.set('logging', 'workflow_level', 'WARNING') config.set('logging', 'interface_level', 'WARNING') logging.update_logging(config) from nipype.interfaces.mrtrix.convert import read_mrtrix_tracks from dipy.tracking.metrics import spline hdr, streamlines = read_mrtrix_tracks(filename_in, as_generator=False) # Load Fibers (Tck) if smooth is not None: streamlines_smooth = [] for sl in streamlines: streamlines_smooth.append(spline(sl, s=smooth)) streamlines = streamlines_smooth #Compressing also good to remove checkerboard artefacts from tracking on peaks if compress_err_thr is not None: streamlines = FiberUtils.compress_streamlines(streamlines, compress_err_thr) FiberUtils.save_streamlines_as_trk(filename_out, streamlines, reference_affine)
def convert_tck_to_trk(filename_in, filename_out, reference_affine, reference_shape, compress_err_thr=0.1, smooth=None, nr_cpus=-1, tracking_format="trk_legacy"): ''' Convert tck file to trk file and compress :param filename_in: :param filename_out: :param compress_err_thr: compress fibers if setting error threshold here (default: 0.1mm) :param smooth: smooth streamlines (default: None) 10: slight smoothing, 100: very smooth from beginning to end :param nr_cpus: :return: ''' from dipy.tracking.metrics import spline streamlines = nib.streamlines.load(filename_in).streamlines # Load Fibers (Tck) if smooth is not None: streamlines_smooth = [] for sl in streamlines: streamlines_smooth.append(spline(sl, s=smooth)) streamlines = streamlines_smooth #Compressing also good to remove checkerboard artefacts from tracking on peaks if compress_err_thr is not None: streamlines = compress_streamlines(streamlines, compress_err_thr, nr_cpus=nr_cpus) if tracking_format == "trk_legacy": save_streamlines_as_trk_legacy(filename_out, streamlines, reference_affine, reference_shape) else: save_streamlines(filename_out, streamlines, reference_affine, reference_shape)
def smooth_streamlines(streamlines, smoothing_factor=10): """ Args: streamlines: smoothing_factor: 10: slight smoothing, 100: very smooth from beginning to end Returns: """ streamlines_smooth = [] for sl in streamlines: streamlines_smooth.append(spline(sl, s=smoothing_factor)) return streamlines_smooth
def convert_tck_to_trk(filename_in, filename_out, reference_affine, compress_err_thr=0.1, smooth=None): ''' Convert tck file to trk file and compress :param filename_in: :param filename_out: :param compress_err_thr: compress fibers if setting error threshold here (default: 0.1mm) :param smooth: smooth streamlines (default: None) 10: slight smoothing, 100: very smooth from beginning to end :return: ''' #Hide large number of nipype logging outputs from nipype import config, logging config.set('execution', 'remove_unnecessary_outputs', 'true') config.set('logging', 'workflow_level', 'WARNING') config.set('logging', 'interface_level', 'WARNING') logging.update_logging(config) from nipype.interfaces.mrtrix.convert import read_mrtrix_tracks from dipy.tracking.metrics import spline hdr, streamlines = read_mrtrix_tracks( filename_in, as_generator=False) # Load Fibers (Tck) if smooth is not None: streamlines_smooth = [] for sl in streamlines: streamlines_smooth.append(spline(sl, s=smooth)) streamlines = streamlines_smooth #Compressing also good to remove checkerboard artefacts from tracking on peaks if compress_err_thr is not None: streamlines = FiberUtils.compress_streamlines( streamlines, compress_err_thr) FiberUtils.save_streamlines_as_trk(filename_out, streamlines, reference_affine)