Skip to content

Commit

Permalink
Merge pull request #24 from mlexchange/cleanup-controls
Browse files Browse the repository at this point in the history
Cleanup controls
  • Loading branch information
hannahker committed Jul 11, 2023
2 parents b72e7a9 + e25c3cb commit b8b30d3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 84 deletions.
39 changes: 2 additions & 37 deletions callbacks/control_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,6 @@
import dash_mantine_components as dmc
import json
from utils.data_utils import convert_hex_to_rgba, data
from tifffile import imread
import numpy as np


@callback(
Output("colormap-scale", "min"),
Output("colormap-scale", "max"),
Output("colormap-scale", "value"),
Input("image-selection-slider", "value"),
Input("project-name-src", "value"),
)
def set_color_range(image_idx, project_name):
if image_idx:
image_idx -= 1 # slider starts at 1, so subtract 1 to get the correct index
tf = data[project_name][image_idx]
min_color = np.min(tf)
max_color = np.max(tf)
return min_color, max_color, [min_color, max_color]
else:
return 0, 255, [0, 255]


@callback(
Output("image-viewer", "figure", allow_duplicate=True),
Input("annotation-opacity", "value"),
prevent_initial_call=True,
)
def annotation_opacity(opacity_value):
patched_figure = Patch()
patched_figure["layout"]["newshape"]["opacity"] = opacity_value
return patched_figure


@callback(
Expand Down Expand Up @@ -118,14 +87,10 @@ def annotation_visibility(checked, store, figure, image_idx):
@callback(
Output("figure-brightness", "value", allow_duplicate=True),
Output("figure-contrast", "value", allow_duplicate=True),
Output("colormap-scale", "value", allow_duplicate=True),
Input("filters-reset", "n_clicks"),
State("colormap-scale", "min"),
State("colormap-scale", "max"),
prevent_initial_call=True,
)
def reset_filters(n_clicks, min_color, max_color):
def reset_filters(n_clicks):
default_brightness = 100
default_contrast = 100
default_colormap_scale = [min_color, max_color]
return default_brightness, default_contrast, default_colormap_scale
return default_brightness, default_contrast
10 changes: 4 additions & 6 deletions callbacks/image_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,39 @@
Output("image-viewer", "figure"),
Input("image-selection-slider", "value"),
State("project-name-src", "value"),
Input("colormap-scale", "value"),
State("paintbrush-width", "value"),
State("annotation-opacity", "value"),
State("annotation-class-selection", "className"),
)
def render_image(
image_idx,
project_name,
zrange,
annotation_width,
annotation_opacity,
annotation_color,
):
if image_idx:
image_idx -= 1 # slider starts at 1, so subtract 1 to get the correct index
tf = data[project_name][image_idx]
else:
tf = np.zeros((500, 500))
fig = px.imshow(tf, binary_string=True, zmin=zrange[0], zmax=zrange[1])
fig = px.imshow(tf, binary_string=True)
fig.update_layout(
margin=dict(l=0, r=0, t=0, b=0),
xaxis=dict(visible=False),
yaxis=dict(visible=False),
dragmode="pan",
height=620,
width=620,
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
)
fig.update_traces(hovertemplate=None, hoverinfo="skip")

# set the default annotation style
hex_color = dmc.theme.DEFAULT_COLORS[annotation_color][7]
fig.update_layout(
newshape=dict(
line=dict(color=annotation_color, width=annotation_width),
fillcolor=convert_hex_to_rgba(hex_color, 0.3),
opacity=annotation_opacity,
)
)
return fig
Expand Down
59 changes: 19 additions & 40 deletions components/control_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,35 +62,6 @@ def layout():
"image-transformations",
children=html.Div(
[
dmc.ActionIcon(
dmc.Tooltip(
label="Reset filters",
children=[
DashIconify(
icon="fluent:arrow-reset-32-regular",
width=20,
),
],
),
size="lg",
variant="filled",
id="filters-reset",
n_clicks=0,
mb=10,
ml="auto",
),
dmc.Text("Colormap scalar range", size="sm"),
dmc.RangeSlider(
id="colormap-scale",
value=[0, 255],
min=0,
max=255,
minRange=0.00001,
step=0.00001,
color="gray",
size="sm",
),
dmc.Space(h=5),
dmc.Text("Brightness", size="sm"),
dmc.Slider(
id=f"figure-brightness",
Expand All @@ -112,6 +83,25 @@ def layout():
color="gray",
size="sm",
),
dmc.Space(h=10),
dmc.ActionIcon(
dmc.Tooltip(
label="Reset filters",
children=[
DashIconify(
icon="fluent:arrow-reset-32-regular",
width=20,
),
],
),
size="lg",
variant="filled",
id="filters-reset",
n_clicks=0,
mb=10,
ml="auto",
style={"margin": "auto"},
),
]
),
),
Expand Down Expand Up @@ -142,17 +132,6 @@ def layout():
color="gray",
size="sm",
),
dmc.Space(h=5),
dmc.Text("Annotation opacity", size="sm"),
dmc.Slider(
id="annotation-opacity",
value=1,
min=0.1,
max=1,
step=0.1,
color="gray",
size="sm",
),
dmc.Space(h=20),
dmc.Text("Annotation class", size="sm"),
dmc.Group(
Expand Down
5 changes: 4 additions & 1 deletion components/image_viewer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dash import html, dcc
import dash_mantine_components as dmc
from dash_iconify import DashIconify
from utils.plot_utils import blank_fig

COMPONENT_STYLE = {
"width": "640px",
Expand Down Expand Up @@ -85,7 +86,9 @@ def layout():
color=dmc.theme.DEFAULT_COLORS["blue"][6], variant="bars"
),
children=[
dcc.Graph(id="image-viewer", config=FIGURE_CONFIG),
dcc.Graph(
id="image-viewer", config=FIGURE_CONFIG, figure=blank_fig()
),
],
),
],
Expand Down
10 changes: 10 additions & 0 deletions utils/plot_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import plotly.graph_objects as go


def blank_fig():
fig = go.Figure(go.Scatter(x=[], y=[]))
fig.update_layout(template=None)
fig.update_xaxes(showgrid=False, showticklabels=False, zeroline=False)
fig.update_yaxes(showgrid=False, showticklabels=False, zeroline=False)

return fig

0 comments on commit b8b30d3

Please sign in to comment.