Skip to content

Commit 1039fbb

Browse files
committed
feat: live branch recoloring on input change after analyis
1 parent a4aabef commit 1039fbb

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

vesskel/_napari.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
from pathlib import Path
77
from typing import TYPE_CHECKING
88

9+
import numpy as np
910
from magicgui import magicgui
1011
from magicgui.widgets import Container, Label, PushButton
11-
from napari.layers import Layer
12+
from napari.layers import Layer, Shapes
1213
from napari.utils.notifications import show_error, show_info
1314
from qtpy.QtWidgets import QFileDialog
1415

@@ -160,6 +161,7 @@ def _update_branch_color_warning(*args) -> None:
160161
self.branch_color_warning.visible = needs_radius and radius_off
161162

162163
self.branch_color_widget.changed.connect(_update_branch_color_warning)
164+
self.branch_color_widget.changed.connect(self._recolor_branch_layers)
163165

164166
# connection to include_vessel_radius_widget happens below
165167
# after that widget is created
@@ -427,6 +429,29 @@ def _on_select_output_dir(self) -> None:
427429
self._output_dir = Path(dir_path)
428430
self.select_outdir_btn.text = str(self._output_dir)
429431

432+
def _recolor_branch_layers(self, *args) -> None:
433+
"""Recolor existing branch layers without re-running analysis."""
434+
prop = self.branch_color_widget.value
435+
436+
for layer in self.viewer.layers:
437+
if not isinstance(layer, Shapes) or not layer.name.endswith("_branches"):
438+
continue
439+
440+
props = layer.properties
441+
values = props.get(prop) if props is not None else None
442+
if values is not None:
443+
numeric = np.asarray(values, dtype=float)
444+
finite = numeric[np.isfinite(numeric)]
445+
if finite.size > 1 and float(np.min(finite)) < float(np.max(finite)):
446+
vmin = float(np.min(finite))
447+
vmax = float(np.max(finite))
448+
layer.edge_color = prop
449+
layer.edge_colormap = "turbo"
450+
layer.edge_contrast_limits = (vmin, vmax)
451+
continue
452+
453+
layer.edge_color = "#30d5c8"
454+
430455
def _on_analyze(self) -> None:
431456
"""Execute analysis with current settings."""
432457
img = self.image_widget.value

0 commit comments

Comments
 (0)