A volumetric data set read from a tif image is sliced, and succesive slices are displayed by Plotly animation.
import plotly
plotly.__version__
'4.2.0'
import numpy as np
from skimage import io
import plotly.graph_objects as go
volume = (io.imread("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/attention-mri.tif")).T
volume.shape
pl_bone=[[0.0, 'rgb(0, 0, 0)'],
[0.05, 'rgb(10, 10, 14)'],
[0.1, 'rgb(21, 21, 30)'],
[0.15, 'rgb(33, 33, 46)'],
[0.2, 'rgb(44, 44, 62)'],
[0.25, 'rgb(56, 55, 77)'],
[0.3, 'rgb(66, 66, 92)'],
[0.35, 'rgb(77, 77, 108)'],
[0.4, 'rgb(89, 92, 121)'],
[0.45, 'rgb(100, 107, 132)'],
[0.5, 'rgb(112, 123, 143)'],
[0.55, 'rgb(122, 137, 154)'],
[0.6, 'rgb(133, 153, 165)'],
[0.65, 'rgb(145, 169, 177)'],
[0.7, 'rgb(156, 184, 188)'],
[0.75, 'rgb(168, 199, 199)'],
[0.8, 'rgb(185, 210, 210)'],
[0.85, 'rgb(203, 221, 221)'],
[0.9, 'rgb(220, 233, 233)'],
[0.95, 'rgb(238, 244, 244)'],
[1.0, 'rgb(255, 255, 255)']]
r, c = volume[0].shape
n_slices = volume.shape[0]
height = (volume.shape[0]-1) / 10
grid = np.linspace(0, height, n_slices)
slice_step = grid[1] - grid[0]
height
initial_slice = go.Surface(
z=height*np.ones((r,c)),
surfacecolor=np.flipud(volume[-1]),
colorscale=pl_bone,
showscale=False)
frames = [go.Frame(data=[dict(type='surface',
z=(height-k*slice_step)*np.ones((r,c)),
surfacecolor=np.flipud(volume[-1-k]))],
name=f'frame{k+1}') for k in range(1, n_slices)]
sliders = [dict(steps = [dict(method= 'animate',
args= [[f'frame{k+1}'],
dict(mode= 'immediate',
frame= dict(duration=40, redraw= True),
transition=dict(duration= 0))
],
label=f'{k+1}'
) for k in range(n_slices)],
active=17,
transition= dict(duration= 0 ),
x=0, # slider starting position
y=0,
currentvalue=dict(font=dict(size=12),
prefix='slice: ',
visible=True,
xanchor= 'center'
),
len=1.0) #slider length
]
layout3d = dict(title_text='Head Scanning', title_x=0.5,
width=600,
height=600,
scene_zaxis_range= [-0.1, 6.8],
sliders=sliders,
)
fig = go.Figure(data=[initial_slice], layout=layout3d, frames=frames)
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
init_notebook_mode(connected=True)
iplot(fig) #wait a few seconds until the plot is displayed
Moving the slider to right or left we can inspect each slice.
The gif file created from the above animation:
%%html
<img src='head-scanning.gif'>