From 70dc2567a17869f635d0b38189d2b07720717ed0 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 3 May 2024 15:16:10 +0100 Subject: [PATCH 1/7] Only update layers when selection is valid --- docs/changelog.rst | 6 ++++++ src/napari_matplotlib/base.py | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 697e483..cab6ca6 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -13,6 +13,12 @@ napari-matplotlib now adheres to `SPEC 0 None: self._update_layers ) + @property + def _valid_layer_selection(self) -> bool: + """ + Return `True` if layer selection is valid. + """ + return self.n_selected_layers in self.n_layers_input and all( + isinstance(layer, self.input_layer_types) for layer in self.layers + ) + def _update_layers(self, event: napari.utils.events.Event) -> None: """ Update the ``layers`` attribute with currently selected layers and re-draw. """ self.layers = list(self.viewer.layers.selection) self.layers = sorted(self.layers, key=lambda layer: layer.name) - self.on_update_layers() + if self._valid_layer_selection: + self.on_update_layers() self._draw() def _draw(self) -> None: @@ -243,10 +253,7 @@ def _draw(self) -> None: with mplstyle.context(self.napari_theme_style_sheet): # everything should be done in the style context self.clear() - if self.n_selected_layers in self.n_layers_input and all( - isinstance(layer, self.input_layer_types) - for layer in self.layers - ): + if self._valid_layer_selection: self.draw() self.canvas.draw() # type: ignore[no-untyped-call] @@ -269,6 +276,7 @@ def on_update_layers(self) -> None: Called when the selected layers are updated. This is a no-op, and is intended for derived classes to override. + It is only called if a selected layer is one of the input layer types. """ From 72e5791a56dfddb7c1c5646f7d1c69cfb6d1d022 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sat, 25 May 2024 09:36:29 +0100 Subject: [PATCH 2/7] Only set colors if x_axis_key is string --- src/napari_matplotlib/histogram.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/napari_matplotlib/histogram.py b/src/napari_matplotlib/histogram.py index adbbae6..560676c 100644 --- a/src/napari_matplotlib/histogram.py +++ b/src/napari_matplotlib/histogram.py @@ -209,10 +209,12 @@ def draw(self) -> None: # get the colormap from the layer depending on its type if isinstance(self.layers[0], napari.layers.Points): colormap = self.layers[0].face_colormap - self.layers[0].face_color = self.x_axis_key + if self.x_axis_key: + self.layers[0].face_color = self.x_axis_key elif isinstance(self.layers[0], napari.layers.Vectors): colormap = self.layers[0].edge_colormap - self.layers[0].edge_color = self.x_axis_key + if self.x_axis_key: + self.layers[0].edge_color = self.x_axis_key else: colormap = None From 2874081c869b32b17977c4df54a7267d59a9a286 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sat, 25 May 2024 09:44:58 +0100 Subject: [PATCH 3/7] Debugging --- src/napari_matplotlib/base.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/napari_matplotlib/base.py b/src/napari_matplotlib/base.py index 9440b74..97f4146 100644 --- a/src/napari_matplotlib/base.py +++ b/src/napari_matplotlib/base.py @@ -229,6 +229,10 @@ def _valid_layer_selection(self) -> bool: """ Return `True` if layer selection is valid. """ + print(f"{self.n_selected_layers=}") + print(f"{self.n_layers_input=}") + print(f"{self.layers=}") + print(f"{self.input_layer_types=}") return self.n_selected_layers in self.n_layers_input and all( isinstance(layer, self.input_layer_types) for layer in self.layers ) From 3ebea2fdcbe375a079a445bdbffc0d504b62cece Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sat, 25 May 2024 10:01:58 +0100 Subject: [PATCH 4/7] Put self._draw() in clause --- src/napari_matplotlib/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/napari_matplotlib/base.py b/src/napari_matplotlib/base.py index 97f4146..eb023c3 100644 --- a/src/napari_matplotlib/base.py +++ b/src/napari_matplotlib/base.py @@ -245,7 +245,7 @@ def _update_layers(self, event: napari.utils.events.Event) -> None: self.layers = sorted(self.layers, key=lambda layer: layer.name) if self._valid_layer_selection: self.on_update_layers() - self._draw() + self._draw() def _draw(self) -> None: """ From b01809f49893af89965e85a0c98fb3c0dbfc3b79 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 12 Jul 2024 15:52:02 +0200 Subject: [PATCH 5/7] Always call on_update_layers --- src/napari_matplotlib/base.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/napari_matplotlib/base.py b/src/napari_matplotlib/base.py index eb023c3..720333e 100644 --- a/src/napari_matplotlib/base.py +++ b/src/napari_matplotlib/base.py @@ -229,10 +229,6 @@ def _valid_layer_selection(self) -> bool: """ Return `True` if layer selection is valid. """ - print(f"{self.n_selected_layers=}") - print(f"{self.n_layers_input=}") - print(f"{self.layers=}") - print(f"{self.input_layer_types=}") return self.n_selected_layers in self.n_layers_input and all( isinstance(layer, self.input_layer_types) for layer in self.layers ) @@ -243,8 +239,8 @@ def _update_layers(self, event: napari.utils.events.Event) -> None: """ self.layers = list(self.viewer.layers.selection) self.layers = sorted(self.layers, key=lambda layer: layer.name) + self.on_update_layers() if self._valid_layer_selection: - self.on_update_layers() self._draw() def _draw(self) -> None: @@ -280,7 +276,6 @@ def on_update_layers(self) -> None: Called when the selected layers are updated. This is a no-op, and is intended for derived classes to override. - It is only called if a selected layer is one of the input layer types. """ From 8a545953b8de227fb4f0f79ffddf384ccadcac26 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 12 Jul 2024 16:00:36 +0200 Subject: [PATCH 6/7] Fix layer selection --- src/napari_matplotlib/histogram.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/napari_matplotlib/histogram.py b/src/napari_matplotlib/histogram.py index 560676c..2881cf7 100644 --- a/src/napari_matplotlib/histogram.py +++ b/src/napari_matplotlib/histogram.py @@ -55,8 +55,10 @@ def on_update_layers(self) -> None: Called when the selected layers are updated. """ super().on_update_layers() - for layer in self.viewer.layers: - layer.events.contrast_limits.connect(self._update_contrast_lims) + if self._valid_layer_selection: + self.layers[0].events.contrast_limits.connect( + self._update_contrast_lims + ) def _update_contrast_lims(self) -> None: for lim, line in zip( From 25b9b0ee979694d3fc33f9812115cc47b91efe35 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 12 Jul 2024 16:13:42 +0200 Subject: [PATCH 7/7] Update changelog --- docs/changelog.rst | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index cab6ca6..54e6bba 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,13 @@ Changelog ========= +2.0.3 +----- +Bug fixes +~~~~~~~~~ +- Fix an error that happened when the histogram widget was open, but a layer that doesn't support + histogramming (e.g., a labels layer) was selected. + 2.0.2 ----- Dependencies @@ -13,12 +20,6 @@ napari-matplotlib now adheres to `SPEC 0