Example #1
0
    def test_reorient(self):
        """Test the reorient command of the CLI"""

        # Reorienting the streamlines should do nothing (they are already)
        # correctly oriented.
        output = os.path.join(self.test_dir.name, 'test-reorient-1.trk')
        reorient(
            os.path.join(self.test_dir.name, 'bundle.trk'),
            output)
        streamlines = load(os.path.join(self.test_dir.name, 'bundle.trk'))
        new_streamlines = load(output)
        for streamline, new_streamline in zip(streamlines, new_streamlines):
            np.testing.assert_array_almost_equal(new_streamline._points,
                                                 streamline._points)

        # Reorienting the bundle with the reversed first streamline
        # should reverse every streamline.
        output = os.path.join(self.test_dir.name, 'test-reorient-2.trk')
        reorient(
            os.path.join(self.test_dir.name, 'bundle-flipped.trk'),
            output)
        streamlines = load(os.path.join(self.test_dir.name, 'bundle.trk'))
        new_streamlines = load(output)
        for streamline, new_streamline in zip(streamlines, new_streamlines):
            np.testing.assert_array_almost_equal(new_streamline._points,
                                                 streamline._points)
Example #2
0
def view(filename):
    """View streamlines in interactive window"""

    # Load the streamlines to be displayed.
    streamlines = load(filename)

    # Create a new vtk renderer and rendering window.
    renderer = vtk.vtkRenderer()
    renderer.SetViewport(0.0, 0.0, 1.0, 1.0)
    renderer.SetBackground(0.0, 0.0, 0.0)
    rendering_window = vtk.vtkRenderWindow()
    rendering_window.AddRenderer(renderer)

    # Allow the user to interact with the mesh.
    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(rendering_window)
    interactor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())

    # Compute the number of points.
    nb_points = 0
    for streamline in streamlines:
        nb_points += len(streamline)

    # The vtk points and lines that represent the lines to draw.
    points = vtk.vtkPoints()
    points.SetNumberOfPoints(nb_points)
    lines = vtk.vtkCellArray()

    # Copy the point and streamline data to vtk objects.
    point_count = 0
    for streamline in streamlines:
        lines.InsertNextCell(len(streamline))
        for idx, point in enumerate(streamline):
            points.SetPoint(point_count, point[0], point[1], point[2])
            lines.InsertCellPoint(point_count)
            point_count += 1

    poly_data = vtk.vtkPolyData()
    poly_data.SetPoints(points)
    poly_data.SetLines(lines)

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputData(poly_data)

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetOpacity(0.01)
    actor.GetProperty().SetLineWidth(1)

    # Add the actor to the renderer.
    renderer.AddActor(actor)

    # Start rendering.
    rendering_window.Render()
    interactor.Start()

    # Cleanup when the user closes the window.
    rendering_window.Finalize()
    interactor.TerminateApp()
Example #3
0
def filter(input, output, **kwargs):

    # Load the input streamlines using the requested parameters.
    streamlines = load(input)
    streamlines.filter(**kwargs)

    # Save the streamlines to the output file.
    save(streamlines, output)
Example #4
0
def merge(inputs, output):

    # Load all the input streamlines and merge them.
    streamlines_list = [load(i) for i in inputs]
    streamlines = reduce(operator.iadd, streamlines_list)

    # Save the streamlines to the output file.
    save(streamlines, output)
Example #5
0
    def test_merge(self):
        """Test the merge command of the CLI"""

        # Merging the short and random streamlines should yield a tractogram
        # with 4 streamlines.
        inputs = [os.path.join(self.test_dir.name, i)
                  for i in ['short.trk', 'random.trk']]
        output = os.path.join(self.test_dir.name, 'test-merge-1.trk')
        merge(inputs, output)
        streamlines = load(output)
        self.assertEqual(len(streamlines), 4)

        # Merging the shord and empty files should yield 3 streamlines.
        inputs = [os.path.join(self.test_dir.name, i)
                  for i in ['short.trk', 'empty.trk']]
        output = os.path.join(self.test_dir.name, 'test-merge-2.trk')
        merge(inputs, output)
        streamlines = load(output)
        self.assertEqual(len(streamlines), 3)
Example #6
0
 def test_filter(self):
     """Test the filter command of the CLI"""
 
     # Filter the short streamlines. The result should be an empty tractogram.
     output = os.path.join(self.test_dir.name, 'test-filter-1.trk')
     filter(
         os.path.join(self.test_dir.name, 'short.trk'),
         output,
         min_length=1.1)
     streamlines = load(output)
     self.assertEqual(len(streamlines), 0)
Example #7
0
def info(input):

    # Load the input streamlines using the requested parameters.
    streamlines = load(input)

    # Print info about the streamlines.
    out = ''
    out += '\nNumber of streamlines: {}'.format(len(streamlines))
    out += '\nMean length: {:.2f}'.format(np.mean(streamlines.lengths))

    print(out)
Example #8
0
def filter(input_filename, output_filename, **kwargs):
    """Removes streamlines from a file based on features

    Removes streamlines from a file based on their features. For example,
    remove all streamlines with a length below 50mm using --min-length 50.

    Args:
        input_filename: The file that contains the streamlines to filter.
        output_filename: The file where the remaining streamlines will be
            saved.

    """

    # Load the input_streamlines using the requested parameters.
    streamlines = load(input_filename)
    streamlines.filter(**kwargs)

    # Save the streamlines to the output file.
    save(streamlines, output_filename)
Example #9
0
def smooth(input_filename, output_filename, **kwargs):
    """Smooths streamlines in a file

    Smooths streamlines using a least square b-spline. The distance between
    knots controls the smoothness of the output streamline with larger
    distances being smoother

    Args:
        input_filename: The file that contains the streamlines to smooth.
        output_filename: The file where the smoothed streamlines will be saved.

    """

    # Load the input streamlines using the requested parameters.
    streamlines = load(input_filename)
    streamlines.smooth(**kwargs)

    # Save the streamlines to the output file.
    save(streamlines, output_filename)
Example #10
0
def info(streamlines_filename):
    """Print information about a streamlines file

    Prints the number of streamlines in the file and the mean length of the
    streamlines.

    Args:
        streamlines_filename: The file whose info is printed.
    """

    # Load the input streamlines using the requested parameters.
    streamlines = load(streamlines_filename)

    # Print info about the streamlines.
    out = ''
    out += '\nNumber of streamlines: {}'.format(len(streamlines))
    out += '\nMean length: {:.2f}'.format(np.mean(streamlines.lengths))

    print(out)
Example #11
0
def transform(input, output):
    """Transforms streamlines from one fileformat to another"""
    streamlines = load(input)
    save(streamlines, output)