Skip to content

Commit 76d99c3

Browse files
committed
Fixed ui menus during plot update
1 parent f7639d3 commit 76d99c3

File tree

2 files changed

+98
-83
lines changed

2 files changed

+98
-83
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ requires-python = ">=3.8"
4444
[project.optional-dependencies]
4545
dev = [
4646
"coverage[toml]",
47-
"pandas",
47+
"pandas<3.0.0",
4848
"scikit-learn<1.6.0",
4949
"pytest",
5050
"pytest-asyncio",

src/tdamapper/plot_backends/plot_plotly.py

Lines changed: 97 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333

3434
_DEFAULT_SPACING = 0.25
3535

36+
MENU_DARK_MODE_NAME = "menu_dark_mode"
37+
38+
MENU_CMAP_NAME = "menu_cmap"
39+
40+
MENU_COLOR_NAME = "menu_color"
41+
42+
SLIDER_NODE_SIZE_NAME = "slider_node_size"
43+
3644
FONT_COLOR_LIGHT = "#2a3f5f"
3745

3846
FONT_COLOR_DARK = "#f2f5fa"
@@ -210,10 +218,6 @@ def __init__(self, mapper_plot, fig: Optional[go.Figure] = None):
210218
self.graph = mapper_plot.graph
211219
self.positions = mapper_plot.positions
212220
self.dim = mapper_plot.dim
213-
self.ui_menu_cmap: Dict = {}
214-
self.ui_menu_color: Dict = {}
215-
self.ui_slider_size: Dict = {}
216-
self.ui_menu_dark_mode: Dict = {}
217221

218222
def plot(
219223
self,
@@ -553,38 +557,81 @@ def set_ui(
553557
) -> None:
554558
if self.fig is None:
555559
return
560+
561+
ui_menu_dark_mode = None
562+
ui_menu_cmap = None
563+
ui_menu_color = None
564+
ui_slider_size = None
565+
for m in self.fig.layout.updatemenus:
566+
if m.name == MENU_DARK_MODE_NAME:
567+
ui_menu_dark_mode = m
568+
elif m.name == MENU_CMAP_NAME:
569+
ui_menu_cmap = m
570+
elif m.name == MENU_COLOR_NAME:
571+
ui_menu_color = m
572+
for s in self.fig.layout.sliders:
573+
if s.name == SLIDER_NODE_SIZE_NAME:
574+
ui_slider_size = s
575+
576+
ui_menu_dark_mode = self.ui_menu_dark_mode()
577+
556578
if cmaps is not None:
557579
cmaps_plotly = [PLOTLY_CMAPS.get(c.lower()) for c in cmaps]
558-
self.ui_menu_cmap = self._ui_menu_cmap(cmaps_plotly)
580+
ui_menu_cmap = self._ui_menu_cmap(cmaps_plotly)
559581

560582
if colors is not None and agg is not None and titles is not None:
561-
self.ui_menu_color = self._ui_menu_color(colors, titles, agg)
583+
ui_menu_color = self._ui_menu_color(colors, titles, agg)
562584

563585
if node_sizes is not None:
564-
self.ui_slider_size = self._ui_slider_node_size(node_sizes)
565-
566-
self.ui_menu_dark_mode = self.ui_dark_mode()
567-
568-
menus = []
569-
sliders = []
586+
ui_slider_size = self._ui_slider_node_size(node_sizes)
570587

571-
if self.ui_menu_cmap:
572-
menus.append(self.ui_menu_cmap)
573-
if self.ui_menu_color:
574-
menus.append(self.ui_menu_color)
575-
if self.ui_menu_dark_mode:
576-
menus.append(self.ui_menu_dark_mode)
577-
if self.ui_slider_size:
578-
sliders.append(self.ui_slider_size)
588+
menus = [m for m in [ui_menu_dark_mode, ui_menu_cmap, ui_menu_color] if m]
589+
sliders = [s for s in [ui_slider_size] if s]
579590

580-
# self.fig.layout.updatemenus = [self.ui_menu_dark_mode]
581-
# self.fig.layout.sliders = []
591+
self.fig.layout.updatemenus = ()
592+
self.fig.layout.sliders = ()
582593

583594
self.fig.update_layout(
584595
updatemenus=menus,
585596
sliders=sliders,
586597
)
587598

599+
def ui_menu_dark_mode(self) -> Dict:
600+
buttons = [
601+
dict(
602+
label="Light",
603+
method="relayout",
604+
args=[
605+
{
606+
"font.color": FONT_COLOR_LIGHT,
607+
"paper_bgcolor": "white",
608+
"plot_bgcolor": "white",
609+
}
610+
],
611+
),
612+
dict(
613+
label="Dark",
614+
method="relayout",
615+
args=[
616+
{
617+
"font.color": FONT_COLOR_DARK,
618+
"paper_bgcolor": "black",
619+
"plot_bgcolor": "black",
620+
}
621+
],
622+
),
623+
]
624+
return dict(
625+
name=MENU_DARK_MODE_NAME,
626+
buttons=buttons,
627+
direction="down",
628+
active=0,
629+
x=0.25,
630+
y=1.0,
631+
xanchor="center",
632+
yanchor="top",
633+
)
634+
588635
def _ui_menu_cmap(self, cmaps: List[str]) -> dict:
589636
target_traces = [1] if self.dim == 2 else [0, 1]
590637

@@ -615,6 +662,7 @@ def _update_cmap(cmap: str) -> dict:
615662
]
616663

617664
return dict(
665+
name=MENU_CMAP_NAME,
618666
buttons=buttons,
619667
direction="down",
620668
x=0.5,
@@ -623,39 +671,6 @@ def _update_cmap(cmap: str) -> dict:
623671
yanchor="top",
624672
)
625673

626-
def _ui_slider_node_size(self, node_sizes: List[float]) -> Dict:
627-
steps = [
628-
dict(
629-
method="restyle",
630-
label=f"{size}",
631-
args=[
632-
{"marker.size": [self._marker_size(size)]},
633-
[1],
634-
],
635-
)
636-
for size in node_sizes
637-
]
638-
639-
return dict(
640-
active=len(steps) // 2,
641-
currentvalue=dict(
642-
prefix="Node size: ",
643-
visible=False,
644-
xanchor="center",
645-
),
646-
steps=steps,
647-
x=0.5,
648-
y=0.0,
649-
xanchor="center",
650-
yanchor="bottom",
651-
len=0.5,
652-
lenmode="fraction",
653-
ticklen=1,
654-
pad=dict(t=1, b=1, l=1, r=1),
655-
bgcolor="rgba(1.0, 1.0, 1.0, 0.5)",
656-
activebgcolor="rgba(127, 127, 127, 0.5)",
657-
)
658-
659674
def _ui_menu_color(self, colors: np.ndarray, titles: List[str], agg) -> Dict:
660675
colors_arr = np.array(colors)
661676
colors_num = colors_arr.shape[1] if colors_arr.ndim == 2 else 1
@@ -716,6 +731,7 @@ def _update_colors(i: int) -> dict:
716731
]
717732

718733
return dict(
734+
name=MENU_COLOR_NAME,
719735
buttons=buttons,
720736
direction="down",
721737
active=0,
@@ -725,37 +741,36 @@ def _update_colors(i: int) -> dict:
725741
yanchor="top",
726742
)
727743

728-
def ui_dark_mode(self) -> Dict:
729-
buttons = [
730-
dict(
731-
label="Light",
732-
method="relayout",
733-
args=[
734-
{
735-
"font.color": FONT_COLOR_LIGHT,
736-
"paper_bgcolor": "white",
737-
"plot_bgcolor": "white",
738-
}
739-
],
740-
),
744+
def _ui_slider_node_size(self, node_sizes: List[float]) -> Dict:
745+
steps = [
741746
dict(
742-
label="Dark",
743-
method="relayout",
747+
method="restyle",
748+
label=f"{size}",
744749
args=[
745-
{
746-
"font.color": FONT_COLOR_DARK,
747-
"paper_bgcolor": "black",
748-
"plot_bgcolor": "black",
749-
}
750+
{"marker.size": [self._marker_size(size)]},
751+
[1],
750752
],
751-
),
753+
)
754+
for size in node_sizes
752755
]
756+
753757
return dict(
754-
buttons=buttons,
755-
direction="down",
756-
active=0,
757-
x=0.25,
758-
y=1.0,
758+
name=SLIDER_NODE_SIZE_NAME,
759+
active=len(steps) // 2,
760+
currentvalue=dict(
761+
prefix="Node size: ",
762+
visible=False,
763+
xanchor="center",
764+
),
765+
steps=steps,
766+
x=0.5,
767+
y=0.0,
759768
xanchor="center",
760-
yanchor="top",
769+
yanchor="bottom",
770+
len=0.5,
771+
lenmode="fraction",
772+
ticklen=1,
773+
pad=dict(t=1, b=1, l=1, r=1),
774+
bgcolor="rgba(1.0, 1.0, 1.0, 0.5)",
775+
activebgcolor="rgba(127, 127, 127, 0.5)",
761776
)

0 commit comments

Comments
 (0)