|
| 1 | +""" |
| 2 | +A test application to test the performance of a few methods to display an image. |
| 3 | +""" |
| 4 | + |
| 5 | +import plotly.graph_objects as go |
| 6 | +import dash |
| 7 | +import dash_html_components as html |
| 8 | +import dash_core_components as dcc |
| 9 | +from dash.dependencies import Input, Output, State |
| 10 | +import imageio |
| 11 | +from dash_slicer.utils import img_array_to_uri |
| 12 | + |
| 13 | + |
| 14 | +app = dash.Dash(__name__, update_title=None) |
| 15 | +server = app.server |
| 16 | + |
| 17 | +# Read volumes and create slicer objects |
| 18 | +vol = imageio.volread("imageio:stent.npz") |
| 19 | + |
| 20 | +slices_png = [img_array_to_uri(im) for im in vol] |
| 21 | + |
| 22 | +OPTIONS = ["noop", "empty", "heatmap", "heatmapgl", "png", "png not reversed"] |
| 23 | + |
| 24 | +## |
| 25 | + |
| 26 | + |
| 27 | +fig = go.Figure(data=[]) |
| 28 | +fig.update_layout( |
| 29 | + template=None, |
| 30 | + margin={"l": 0, "r": 0, "b": 0, "t": 0, "pad": 4}, |
| 31 | + dragmode="pan", # user navigates by panning |
| 32 | +) |
| 33 | +fig.update_xaxes( |
| 34 | + showgrid=False, |
| 35 | + showticklabels=False, |
| 36 | + zeroline=False, |
| 37 | +) |
| 38 | +fig.update_yaxes( |
| 39 | + showgrid=False, |
| 40 | + scaleanchor="x", |
| 41 | + showticklabels=False, |
| 42 | + zeroline=False, |
| 43 | + range=[0, 128], |
| 44 | +) |
| 45 | + |
| 46 | + |
| 47 | +app.layout = html.Div( |
| 48 | + children=[ |
| 49 | + dcc.Dropdown( |
| 50 | + id="dropdown", |
| 51 | + options=[{"label": name, "value": name} for name in OPTIONS], |
| 52 | + value="noop", |
| 53 | + ), |
| 54 | + html.Div(id="fps"), |
| 55 | + html.Br(), |
| 56 | + dcc.Graph(id="graph", figure=fig), |
| 57 | + dcc.Interval(id="interval", interval=1), |
| 58 | + dcc.Store(id="index", data=0), |
| 59 | + dcc.Store(id="data_list", data=vol), |
| 60 | + dcc.Store(id="data_png", data=slices_png), |
| 61 | + ] |
| 62 | +) |
| 63 | + |
| 64 | +## |
| 65 | + |
| 66 | + |
| 67 | +app.clientside_callback( |
| 68 | + """ |
| 69 | +function update_index(_, index) { |
| 70 | + index += 1; |
| 71 | + if (index > 200) index = 50; |
| 72 | + return index; |
| 73 | +} |
| 74 | +""", |
| 75 | + Output("index", "data"), |
| 76 | + [Input("interval", "n_intervals")], |
| 77 | + [ |
| 78 | + State("index", "data"), |
| 79 | + ], |
| 80 | +) |
| 81 | + |
| 82 | + |
| 83 | +app.clientside_callback( |
| 84 | + """ |
| 85 | +function update_figure(index, option, ori_figure, data_list, data_png) { |
| 86 | +
|
| 87 | + // Get FPS |
| 88 | + let fps_result = dash_clientside.no_update; |
| 89 | + let now = performance.now(); |
| 90 | + let etime = now - (window.lasttime || 0); |
| 91 | + if (etime > 1000) { |
| 92 | + let nframes = (window.framecount || 0) + 1; |
| 93 | + fps_result = Math.round(nframes * 1000 / etime) + " FPS"; |
| 94 | + window.lasttime = now; |
| 95 | + window.framecount = 0; |
| 96 | + } else { |
| 97 | + window.framecount += 1; |
| 98 | + } |
| 99 | +
|
| 100 | + // Get figure |
| 101 | + let figure_result = dash_clientside.no_update; |
| 102 | +
|
| 103 | + if (!option || option == 'noop') { |
| 104 | + // noop |
| 105 | + } else if (option == 'sleep') { |
| 106 | + while (performance.now() < now + 500) {} |
| 107 | + } else if (option == 'empty') { |
| 108 | + figure_result = {...ori_figure}; |
| 109 | + figure_result.layout.yaxis.range = [128, 0]; |
| 110 | + figure_result.data = []; |
| 111 | + } else if (option == 'heatmap') { |
| 112 | + let trace = {type: 'heatmap', z: data_list[index]}; |
| 113 | + figure_result = {...ori_figure}; |
| 114 | + figure_result.layout.yaxis.range = [128, 0]; |
| 115 | + figure_result.data = [trace]; |
| 116 | + } else if (option == 'heatmapgl') { |
| 117 | + let trace = {type: 'heatmapgl', z: data_list[index]}; |
| 118 | + figure_result = {...ori_figure}; |
| 119 | + figure_result.layout.yaxis.range = [128, 0]; |
| 120 | + figure_result.data = [trace]; |
| 121 | + } else if (option == 'png') { |
| 122 | + let trace = {type: 'image', source: data_png[index]}; |
| 123 | + figure_result = {...ori_figure}; |
| 124 | + figure_result.layout.yaxis.range = [128, 0]; |
| 125 | + figure_result.data = [trace]; |
| 126 | + } else if (option == 'png not reversed') { |
| 127 | + let trace = {type: 'image', source: data_png[index]}; |
| 128 | + figure_result = {...ori_figure}; |
| 129 | + figure_result.layout.yaxis.range = [0, 128]; |
| 130 | + figure_result.data = [trace]; |
| 131 | + } else { |
| 132 | + fps_result = "invalid option: " + option; |
| 133 | + } |
| 134 | + return [figure_result, fps_result]; |
| 135 | +} |
| 136 | +""", |
| 137 | + [Output("graph", "figure"), Output("fps", "children")], |
| 138 | + [Input("index", "data")], |
| 139 | + [ |
| 140 | + State("dropdown", "value"), |
| 141 | + State("graph", "figure"), |
| 142 | + State("data_list", "data"), |
| 143 | + State("data_png", "data"), |
| 144 | + ], |
| 145 | +) |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + app.run_server(debug=True) |
0 commit comments