Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Get the relevant slices from the template
target_template_img = nib.load(args.target_template)
x_slice = int(target_template_img.shape[0] / 2)
y_slice = int(target_template_img.shape[1] / 2)
z_slice = int(target_template_img.shape[2] / 2)
slices_choice = (x_slice, y_slice, z_slice)
subject_data = prepare_data_for_actors(args.in_bundle, args.in_anat,
args.target_template)
# Create actors from each dataset for Dipy
sft, reference_data = subject_data
streamlines = sft.streamlines
volume_actor = actor.slicer(reference_data,
affine=nib.load(args.target_template).affine,
opacity=args.anat_opacity,
interpolation='nearest')
if args.local_coloring:
colors = []
for i in streamlines:
local_color = np.gradient(i, axis=0)
local_color = np.abs(local_color)
local_color = (local_color.T / np.max(local_color, axis=1)).T
colors.append(local_color)
elif args.uniform_coloring:
colors = (args.uniform_coloring[0] / 255.0,
args.uniform_coloring[1] / 255.0,
args.uniform_coloring[2] / 255.0)
elif args.reference_coloring:
sft.to_vox()
if not world_coords:
from dipy.tracking.streamline import transform_streamlines
streamlines = transform_streamlines(streamlines, np.linalg.inv(affine))
###############################################################################
# Now we create, a ``Scene`` object and add the streamlines using the
# ``line`` function and an image plane using the ``slice`` function.
scene = window.Scene()
stream_actor = actor.line(streamlines)
if not world_coords:
image_actor_z = actor.slicer(data, affine=np.eye(4))
else:
image_actor_z = actor.slicer(data, affine)
###############################################################################
# We can also change also the opacity of the slicer.
slicer_opacity = 0.6
image_actor_z.opacity(slicer_opacity)
###############################################################################
# We can add additonal slicers by copying the original and adjusting the
# ``display_extent``.
image_actor_x = image_actor_z.copy()
x_midpoint = int(np.round(shape[0] / 2))
image_actor_x.display_extent(x_midpoint,
x_midpoint, 0,
shape[1] - 1,
target_template_img = nib.load(args.in_template)
zooms = 1 / float(target_template_img.header.get_zooms()[0])
x_slice = int(target_template_img.shape[0] / 2 + zooms*30)
y_slice = int(target_template_img.shape[1] / 2)
z_slice = int(target_template_img.shape[2] / 2)
slices_choice = (x_slice, y_slice, z_slice)
FA, evals, evecs = prepare_data_for_actors(args.in_dwi, args.in_bval,
args.in_bvec,
args.in_template,
slices_choice,
shells=args.shells)
# Create actors from each dataset for Dipy
volume_actor = actor.slicer(FA,
affine=nib.load(args.in_template).affine,
opacity=0.3,
interpolation='nearest')
peaks_actor = actor.peak_slicer(evecs,
affine=nib.load(
args.in_template).affine,
peaks_values=evals,
colors=None, linewidth=1)
# Take a snapshot of each dataset, camera setting are fixed for the
# known template, won't work with another.
display_slices(volume_actor, slices_choice,
output_filenames[0], 'sagittal',
view_position=tuple([x for x in (-125, 10, 10)]),
focal_point=tuple([x for x in (0, -10, 10)]),
peaks_actor=peaks_actor)
else:
# Select the streamlines to plot
bundle_tractogram_file = nib.streamlines.load(bundle_file)
streamlines = bundle_tractogram_file.streamlines
tubes = actor.line(streamlines)
number_streamlines = len(streamlines)
# Render
ren = window.Renderer()
zoom = args.zoom
opacity = args.opacity_background
# Structural data
slice_actor = actor.slicer(data, affine, value_range)
slice_actor.opacity(opacity)
ren.add(slice_actor)
# Streamlines
ren.add(tubes)
ren.reset_camera()
ren.zoom(zoom)
view_number = 0
set_img_in_cell(mosaic, ren, view_number,
output_paths[view_number], width, height, i)
ren.pitch(180)
ren.reset_camera()
ren.zoom(zoom)
view_number = 1
set_img_in_cell(mosaic, ren, view_number,
img = nib.load(fname_fa)
fa = img.get_data()
###############################################################################
# Notice here how the scale range is. We use FA min and max values to set it up
lut = actor.colormap_lookup_table(scale_range=(fa.min(), fa.max()),
hue_range=(0.4, 1.),
saturation_range=(1, 1.),
value_range=(0., 1.))
###############################################################################
# This is because the lookup table is applied in the slice after interpolating
# to (0, 255).
fa_actor = actor.slicer(fa, affine, lookup_colormap=lut)
scene.clear()
scene.add(fa_actor)
scene.reset_camera()
scene.zoom(1.4)
# window.show(scene, size=(600, 600), reset_camera=False)
window.record(scene, out_path='slices_lut.png', size=(600, 600),
reset_camera=False)
###############################################################################
# Now we would like to add the ability to click on a voxel and show its value
# on a panel in the window. The panel is a UI element which requires access to
# different areas of the visualization pipeline and therefore we don't
def show_template_bundles(bundles, show=True, fname=None):
renderer = window.Renderer()
template_actor = actor.slicer(static)
renderer.add(template_actor)
lines_actor = actor.streamtube(bundles, window.colors.orange,
linewidth=0.3)
renderer.add(lines_actor)
if show:
window.show(renderer)
if fname is not None:
window.record(renderer, n_frames=1, out_path=fname, size=(900, 900))
###############################################################################
# Render slices from T1 with a specific value range
# =================================================
#
# The T1 has usually a higher range of values than what can be visualized in an
# image. We can set the range that we would like to see.
mean, std = data[data > 0].mean(), data[data > 0].std()
value_range = (mean - 0.5 * std, mean + 1.5 * std)
###############################################################################
# The ``slice`` function will read data and resample the data using an affine
# transformation matrix. The default behavior of this function is to show the
# middle slice of the last dimension of the resampled data.
slice_actor = actor.slicer(data, affine, value_range)
###############################################################################
# The ``slice_actor`` contains an axial slice.
scene.add(slice_actor)
###############################################################################
# The same actor can show any different slice from the given data using its
# ``display`` function. However, if we want to show multiple slices we need to
# copy the actor first.
slice_actor2 = slice_actor.copy()
###############################################################################
# Now we have a new ``slice_actor`` which displays the middle slice of sagittal
# plane.