Skip to content

Add support for extra traces #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 14, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions examples/threshold_contour.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
An example demonstrating adding traces.

This shows a volume with a contour overlaid on top. The `extra_traces`
property is used to add scatter traces that represent the contour.
"""

import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
from dash_slicer import VolumeSlicer
import imageio
from skimage import measure


app = dash.Dash(__name__, update_title=None)
server = app.server

vol = imageio.volread("imageio:stent.npz")
mi, ma = vol.min(), vol.max()
slicer = VolumeSlicer(app, vol)


app.layout = html.Div(
[
slicer.graph,
slicer.slider,
dcc.Slider(
id="level-slider",
min=mi,
max=ma,
step=1,
value=mi + 0.2 * (ma - mi),
),
*slicer.stores,
]
)


@app.callback(
Output(slicer.extra_traces.id, "data"),
[Input("level-slider", "value"), Input(slicer.state.id, "data")],
)
def apply_levels(level, state):
if not state:
return dash.no_update
slice = vol[state["index"]]
contours = measure.find_contours(slice, level)
traces = []
for contour in contours:
traces.append(
{
"type": "scatter",
"mode": "lines",
"line": {"color": "cyan", "width": 3},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use different colors here for the different contours? It would make the example clearer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that each contour was a linepiece, but I just learned they are each one closed contour. Cool!

"x": contour[:, 1],
"y": contour[:, 0],
}
)
return traces


if __name__ == "__main__":
# Note: dev_tools_props_check negatively affects the performance of VolumeSlicer
app.run_server(debug=True, dev_tools_props_check=False)