From f5160bbc404119aad83ff1e1ccae58ec895cc01d Mon Sep 17 00:00:00 2001 From: Philipp Schlegel Date: Sun, 15 Sep 2024 16:41:44 +0200 Subject: [PATCH] plotting: warn about missing radii only once --- navis/plotting/dd.py | 16 +++++++++++++--- navis/plotting/k3d/k3d_objects.py | 13 ++++++++++++- navis/plotting/plotly/graph_objs.py | 13 ++++++++++++- navis/plotting/vispy/visuals.py | 13 ++++++++++++- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/navis/plotting/dd.py b/navis/plotting/dd.py index 8620f8f7..8630565b 100644 --- a/navis/plotting/dd.py +++ b/navis/plotting/dd.py @@ -550,7 +550,17 @@ def plot2d( settings.radius = False if isinstance(neuron, core.TreeNeuron) and settings.radius: - _neuron = conversion.tree2meshneuron(neuron) + # Warn once if more than 5% of nodes have missing radii + if not getattr(fig, '_radius_warned', False): + if ((neuron.nodes.radius.fillna(0).values <= 0).sum() / neuron.n_nodes) > 0.05: + logger.warning( + "Some skeleton nodes have radius <= 0. This may lead to " + "rendering artifacts. Set `radius=False` to plot skeletons " + "as single-width lines instead." + ) + fig._radius_warned = True + + _neuron = conversion.tree2meshneuron(neuron, warn_missing_radii=False) _neuron.connectors = neuron.connectors neuron = _neuron @@ -883,7 +893,7 @@ def _plot_connectors(neuron, color, ax, settings): inner_dict["color"] = settings.cn_colors if settings.method == "2d": - for c, this_cn in connectors.groupby('type'): + for c, this_cn in connectors.groupby("type"): x, y = _parse_view2d(this_cn[["x", "y", "z"]].values, settings.view) ax.scatter( @@ -892,7 +902,7 @@ def _plot_connectors(neuron, color, ax, settings): color=cn_layout[c]["color"], edgecolor="none", s=settings.cn_size if settings.cn_size else cn_layout["size"], - zorder=1000 + zorder=1000, ) ax.get_children()[-1].set_gid(f"CN_{neuron.id}") elif settings.method in ["3d", "3d_complex"]: diff --git a/navis/plotting/k3d/k3d_objects.py b/navis/plotting/k3d/k3d_objects.py index 129e6460..28c0693c 100644 --- a/navis/plotting/k3d/k3d_objects.py +++ b/navis/plotting/k3d/k3d_objects.py @@ -69,6 +69,7 @@ def neuron2k3d(x, colormap, settings): cn_lay.update(settings.cn_layout) trace_data = [] + _radius_warned = False for i, neuron in enumerate(x): name = str(getattr(neuron, "name", neuron.id)) color = colormap[i] @@ -106,7 +107,17 @@ def neuron2k3d(x, colormap, settings): settings.radius = False if isinstance(neuron, core.TreeNeuron) and settings.radius: - _neuron = conversion.tree2meshneuron(neuron) + # Warn once if more than 5% of nodes have missing radii + if not _radius_warned: + if ((neuron.nodes.radius.fillna(0).values <= 0).sum() / neuron.n_nodes) > 0.05: + logger.warning( + "Some skeleton nodes have radius <= 0. This may lead to " + "rendering artifacts. Set `radius=False` to plot skeletons " + "as single-width lines instead." + ) + _radius_warned = True + + _neuron = conversion.tree2meshneuron(neuron, warn_missing_radii=False) _neuron.connectors = neuron.connectors neuron = _neuron diff --git a/navis/plotting/plotly/graph_objs.py b/navis/plotting/plotly/graph_objs.py index d5bb6533..9bbbb689 100644 --- a/navis/plotting/plotly/graph_objs.py +++ b/navis/plotting/plotly/graph_objs.py @@ -98,6 +98,7 @@ def neuron2plotly(x, colormap, settings): cn_lay.update(settings.cn_layout) trace_data = [] + _radius_warned = False for i, neuron in enumerate(x): name = str(getattr(neuron, "name", neuron.id)) color = colormap[i] @@ -135,7 +136,17 @@ def neuron2plotly(x, colormap, settings): settings.radius = False if isinstance(neuron, core.TreeNeuron) and settings.radius: - _neuron = conversion.tree2meshneuron(neuron) + # Warn once if more than 5% of nodes have missing radii + if not _radius_warned: + if ((neuron.nodes.radius.fillna(0).values <= 0).sum() / neuron.n_nodes) > 0.05: + logger.warning( + "Some skeleton nodes have radius <= 0. This may lead to " + "rendering artifacts. Set `radius=False` to plot skeletons " + "as single-width lines instead." + ) + _radius_warned = True + + _neuron = conversion.tree2meshneuron(neuron, warn_missing_radii=False) _neuron.connectors = neuron.connectors neuron = _neuron diff --git a/navis/plotting/vispy/visuals.py b/navis/plotting/vispy/visuals.py index fbd4a5c6..92bcf152 100644 --- a/navis/plotting/vispy/visuals.py +++ b/navis/plotting/vispy/visuals.py @@ -251,6 +251,7 @@ def neuron2vispy(x, settings): # List to fill with vispy visuals visuals = [] + _radius_warned = False for i, neuron in enumerate(x): # Generate random ID -> we need this in case we have duplicate IDs object_id = uuid.uuid4() @@ -263,7 +264,17 @@ def neuron2vispy(x, settings): settings.radius = False if isinstance(neuron, core.TreeNeuron) and settings.radius: - _neuron = conversion.tree2meshneuron(neuron) + # Warn once if more than 5% of nodes have missing radii + if not _radius_warned: + if ((neuron.nodes.radius.fillna(0).values <= 0).sum() / neuron.n_nodes) > 0.05: + logger.warning( + "Some skeleton nodes have radius <= 0. This may lead to " + "rendering artifacts. Set `radius=False` to plot skeletons " + "as single-width lines instead." + ) + _radius_warned = True + + _neuron = conversion.tree2meshneuron(neuron, warn_missing_radii=False) _neuron.connectors = neuron.connectors neuron = _neuron