Skip to content

Commit

Permalink
Reorder layers when predicting to ensure Prediction layer is on top
Browse files Browse the repository at this point in the history
  • Loading branch information
jluethi committed Sep 16, 2024
1 parent 23b01de commit e778a75
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/napari_feature_classifier/classifier_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,49 @@ def selection_changed(self):
)
self._update_export_destination(self._last_selected_label_layer)

def reorder_layers(self):
"""Reorders layers if needed to ensure Annotation & Prediction layers
are above the currently selected label layer.
"""
# Get the current order of layers
all_layers = list(self._viewer.layers)

# Determine the indices of the layers if they exist
indices_to_move = []

# Find the index of "Prediction" layer if it exists
if "Predictions" in self._viewer.layers:
indices_to_move.append(self._viewer.layers.index("Predictions"))

# Find the index of "Annotation" layer if it exists
if "Annotations" in self._viewer.layers:
indices_to_move.append(self._viewer.layers.index("Annotations"))

# Find the index of the reference_label_layer
if self._last_selected_label_layer.name in self._viewer.layers:
indices_to_move.append(
self._viewer.layers.index(self._last_selected_label_layer.name)
)

# Calculate the new order of layer indices
remaining_indices = [
i for i in range(len(all_layers)) if i not in indices_to_move
]
remaining_indices.reverse()
new_order = indices_to_move + remaining_indices
new_order.reverse()

# Reorder the layers using the move_multiple function
self._viewer.layers.move_multiple(new_order)

def _init_prediction_layer(
self, label_layer: napari.layers.Labels, ensure_layer_presence: bool = True
):
"""
Initialize the prediction layer and reset its data (to fit the input
label_layer) and its colormap
label_layer) and its colormap.
ensure_layer_presence creates the Predictions layer if it doesn't exist
yet and triggers layer reordering.
"""
# Ensure that prediction layer exists
if (
Expand All @@ -415,6 +452,16 @@ def _init_prediction_layer(
name="Predictions",
translate=self._last_selected_label_layer.translate,
)
if ensure_layer_presence:
# Ensure correct layer order: This sometimes fails with weird
# EmitLoopError & IndexError that should be ignored
try:
self.reorder_layers()
except: # noqa
pass

# Ensure that prediction layer is above the current label layer
self._last_selected_label_layer

# Check if the predict column already exists in the layer.features
if "prediction" not in label_layer.features:
Expand Down

0 comments on commit e778a75

Please sign in to comment.