|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + notebook_metadata_filter: all |
| 5 | + text_representation: |
| 6 | + extension: .md |
| 7 | + format_name: markdown |
| 8 | + format_version: '1.1' |
| 9 | + jupytext_version: 1.2.3 |
| 10 | + kernelspec: |
| 11 | + display_name: Python 3 |
| 12 | + language: python |
| 13 | + name: python3 |
| 14 | + language_info: |
| 15 | + codemirror_mode: |
| 16 | + name: ipython |
| 17 | + version: 3 |
| 18 | + file_extension: .py |
| 19 | + mimetype: text/x-python |
| 20 | + name: python |
| 21 | + nbconvert_exporter: python |
| 22 | + pygments_lexer: ipython3 |
| 23 | + version: 3.7.3 |
| 24 | + plotly: |
| 25 | + description: How to create an plotly animation with slider that cycles through |
| 26 | + MRI cross-sections of a human brain. |
| 27 | + display_as: animations |
| 28 | + has_thumbnail: true |
| 29 | + ipynb: ~notebook_demo/190 |
| 30 | + language: python |
| 31 | + layout: user-guide |
| 32 | + name: Visualizing MRI Volume Slices |
| 33 | + order: 4 |
| 34 | + page_type: example_index |
| 35 | + permalink: python/visualizing-mri-volume-slices/ |
| 36 | + thumbnail: thumbnail/brain-mri-animation_square.gif |
| 37 | + title: Visualizing MRI Volume Slices | plotly |
| 38 | +--- |
| 39 | + |
| 40 | +#### Visualization of MRI volume slices |
| 41 | + |
| 42 | +```python |
| 43 | +# Import data |
| 44 | +import time |
| 45 | +import numpy as np |
| 46 | + |
| 47 | +from skimage import io |
| 48 | + |
| 49 | +vol = io.imread("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/attention-mri.tif") |
| 50 | +volume = vol.T |
| 51 | +r, c = volume[0].shape |
| 52 | + |
| 53 | +# Define frames |
| 54 | +import plotly.graph_objects as go |
| 55 | +nb_frames = 68 |
| 56 | + |
| 57 | +fig = go.Figure(frames=[go.Frame(data=go.Surface( |
| 58 | + z=(6.7 - k * 0.1) * np.ones((r, c)), |
| 59 | + surfacecolor=np.flipud(volume[67 - k]), |
| 60 | + cmin=0, cmax=200 |
| 61 | + ), |
| 62 | + name=str(k) # you need to name the frame for the animation to behave properly |
| 63 | + ) |
| 64 | + for k in range(nb_frames)]) |
| 65 | + |
| 66 | +# Add data to be displayed before animation starts |
| 67 | +fig.add_trace(go.Surface( |
| 68 | + z=6.7 * np.ones((r, c)), |
| 69 | + surfacecolor=np.flipud(volume[67]), |
| 70 | + colorscale='Gray', |
| 71 | + cmin=0, cmax=200, |
| 72 | + colorbar=dict(thickness=20, ticklen=4) |
| 73 | + )) |
| 74 | + |
| 75 | + |
| 76 | +def frame_args(duration): |
| 77 | + return { |
| 78 | + "frame": {"duration": duration}, |
| 79 | + "mode": "immediate", |
| 80 | + "fromcurrent": True, |
| 81 | + "transition": {"duration": duration, "easing": "linear"}, |
| 82 | + } |
| 83 | + |
| 84 | +sliders = [ |
| 85 | + { |
| 86 | + "pad": {"b": 10, "t": 60}, |
| 87 | + "len": 0.9, |
| 88 | + "x": 0.1, |
| 89 | + "y": 0, |
| 90 | + "steps": [ |
| 91 | + { |
| 92 | + "args": [[f.name], frame_args(0)], |
| 93 | + "label": str(k), |
| 94 | + "method": "animate", |
| 95 | + } |
| 96 | + for k, f in enumerate(fig.frames) |
| 97 | + ], |
| 98 | + } |
| 99 | + ] |
| 100 | + |
| 101 | +# Layout |
| 102 | +fig.update_layout( |
| 103 | + title='Slices in volumetric data', |
| 104 | + width=600, |
| 105 | + height=600, |
| 106 | + scene=dict( |
| 107 | + zaxis=dict(range=[-0.1, 6.8], autorange=False), |
| 108 | + aspectratio=dict(x=1, y=1, z=1), |
| 109 | + ), |
| 110 | + updatemenus = [ |
| 111 | + { |
| 112 | + "buttons": [ |
| 113 | + { |
| 114 | + "args": [None, frame_args(50)], |
| 115 | + "label": "▶", # play symbol |
| 116 | + "method": "animate", |
| 117 | + }, |
| 118 | + { |
| 119 | + "args": [[None], frame_args(0)], |
| 120 | + "label": "◼", # pause symbol |
| 121 | + "method": "animate", |
| 122 | + }, |
| 123 | + ], |
| 124 | + "direction": "left", |
| 125 | + "pad": {"r": 10, "t": 70}, |
| 126 | + "type": "buttons", |
| 127 | + "x": 0.1, |
| 128 | + "y": 0, |
| 129 | + } |
| 130 | + ], |
| 131 | + sliders=sliders |
| 132 | +) |
| 133 | + |
| 134 | +fig.show() |
| 135 | +``` |
| 136 | + |
| 137 | +#### Credit: |
| 138 | +All credit goes to Emilia Petrisor for this excellent animation! |
| 139 | + |
| 140 | +Here's where you can find her: |
| 141 | +- Her [Twitter](https://twitter.com/mathinpython) under the handle `@mathinpython` |
| 142 | +- Her [GitHub Page](https://github.com/empet) with Username `empet` |
| 143 | + |
| 144 | + |
| 145 | +#### Reference |
| 146 | +For additional information and help setting up a slider in an animation, see https://plot.ly/python/gapminder-example/. For more documentation on creating animations with Plotly, see https://plot.ly/python/#animations. |
| 147 | + |
0 commit comments