Skip to content

Commit 7dca3fc

Browse files
authored
Add script to check performance (#23)
* add script to check performance * add update_title=None * add another script to test performance for minimal example with a slider
1 parent 6e1c9e1 commit 7dca3fc

File tree

2 files changed

+270
-0
lines changed

2 files changed

+270
-0
lines changed

tests/peformance2.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""
2+
A test application to test the performance of a minimal example that allows
3+
slicing through a volume using a slider.
4+
"""
5+
6+
import plotly.graph_objects as go
7+
import dash
8+
import dash_html_components as html
9+
import dash_core_components as dcc
10+
from dash.dependencies import Input, Output, State
11+
import imageio
12+
from dash_slicer.utils import img_array_to_uri
13+
14+
15+
app = dash.Dash(__name__, update_title=None)
16+
server = app.server
17+
18+
# Read volumes and create slicer objects
19+
vol = imageio.volread("imageio:stent.npz")
20+
21+
slices_png = [img_array_to_uri(im) for im in vol]
22+
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+
html.Div(id="fps"),
50+
html.Br(),
51+
dcc.Slider(id="slider", min=0, max=256, step=1, value=128, updatemode="drag"),
52+
html.Br(),
53+
dcc.Graph(id="graph", figure=fig),
54+
dcc.Store(id="index", data=0),
55+
dcc.Store(id="data_png", data=slices_png),
56+
]
57+
)
58+
59+
##
60+
61+
62+
app.clientside_callback(
63+
"""
64+
function update_index(index) {
65+
return index;
66+
}
67+
""",
68+
Output("index", "data"),
69+
[Input("slider", "value")],
70+
)
71+
72+
73+
app.clientside_callback(
74+
"""
75+
function update_figure(index, ori_figure, data_png) {
76+
77+
// Get FPS
78+
let fps_result = dash_clientside.no_update;
79+
let now = performance.now();
80+
let etime = now - (window.lasttime || 0);
81+
if (etime > 1000) {
82+
let nframes = (window.framecount || 0) + 1;
83+
fps_result = Math.round(nframes * 1000 / etime) + " FPS";
84+
window.lasttime = now;
85+
window.framecount = 0;
86+
} else {
87+
window.framecount += 1;
88+
}
89+
90+
// Get figure
91+
let figure_result = dash_clientside.no_update;
92+
93+
if (true) {
94+
let trace = {type: 'image', source: data_png[index]};
95+
figure_result = {...ori_figure};
96+
figure_result.layout.yaxis.range = [128, 0];
97+
figure_result.data = [trace];
98+
} else {
99+
// unfavorable y-axis
100+
let trace = {type: 'image', source: data_png[index]};
101+
figure_result = {...ori_figure};
102+
figure_result.layout.yaxis.range = [0, 128];
103+
figure_result.data = [trace];
104+
}
105+
return [figure_result, fps_result];
106+
}
107+
""",
108+
[Output("graph", "figure"), Output("fps", "children")],
109+
[
110+
# Input("index", "data")],
111+
Input("slider", "value")
112+
],
113+
[
114+
State("graph", "figure"),
115+
State("data_png", "data"),
116+
],
117+
)
118+
119+
120+
if __name__ == "__main__":
121+
app.run_server(debug=True)

tests/performance1.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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

Comments
 (0)