Skip to content

Commit

Permalink
Merge pull request #8 from TheJacksonLaboratory/dev
Browse files Browse the repository at this point in the history
Enabled navigation for labels
  • Loading branch information
fercer authored Aug 6, 2024
2 parents 03dba5e + 40340a0 commit 18a7118
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
15 changes: 14 additions & 1 deletion src/napari_activelearning/_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,16 @@ def _clear_layers_group(self):
self.layers_group_name_cmb.setEnabled(False)
self.use_as_input_chk.setEnabled(False)
self.use_as_sampling_chk.setEnabled(False)
self.edit_scale_mdspn.axes = ""
self.edit_translate_mdspn.axes = ""

def _clear_layer_channel(self):
self.display_name_lbl.setText("None selected")
self.edit_channel_spn.setValue(0)
self.edit_channel_spn.setMaximum(0)
self.edit_channel_spn.setEnabled(False)
self.edit_scale_mdspn.setEnabled(False)
self.edit_translate_mdspn.setEnabled(False)

def _fill_image_group(self):
self._clear_image_group()
Expand Down Expand Up @@ -355,13 +359,15 @@ def _fill_layer(self):
ax: ax_scl
for ax, ax_scl in zip(self._active_layer_channel.source_axes,
self._active_layer_channel.scale)
if ax != "C"
}
self.edit_scale_mdspn.setEnabled(True)

self.edit_translate_mdspn.sizes = {
ax: ax_scl
for ax, ax_scl in zip(self._active_layer_channel.source_axes,
self._active_layer_channel.translate)
if ax != "C"
}
self.edit_translate_mdspn.setEnabled(True)

Expand Down Expand Up @@ -485,6 +491,7 @@ def _update_reference_info(self):

else:
self.generate_mask_btn.setEnabled(False)
self.patch_sizes_mspn.axes = ""

def generate_mask_layer(self):
self.generate_mask_btn.setEnabled(
Expand Down Expand Up @@ -697,7 +704,8 @@ def __init__(self):
"Sampling top-left",
"Sampling bottom-right"
])
self.labels_table_tw.itemSelectionChanged.connect(self.focus_region)
# self.labels_table_tw.itemSelectionChanged.connect(self.focus_region)
self.labels_table_tw.itemPressed.connect(self.focus_region)

self.labels_table_tw.addTopLevelItem(self.labels_group_root)
self.labels_group_root.setExpanded(True)
Expand Down Expand Up @@ -769,6 +777,11 @@ def focus_region(self, label: Optional[QTreeWidgetItem] = None,
self._active_label_group is not None
)

self.prev_img_btn.setEnabled(self._active_label is not None)
self.prev_patch_btn.setEnabled(self._active_label is not None)
self.next_patch_btn.setEnabled(self._active_label is not None)
self.next_img_btn.setEnabled(self._active_label is not None)

def edit_labels(self):
editing = super(LabelsManagerWidget, self).edit_labels()
self.commit_btn.setEnabled(editing)
Expand Down
11 changes: 4 additions & 7 deletions src/napari_activelearning/_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ def navigate(self, delta_patch_index=0, delta_image_index=0):
if self._requires_commit:
self.commit()

self._active_label.setSelected(False)

self._active_label_group = self._active_label.parent()
patch_index = self._active_label_group.indexOfChild(
self._active_label
Expand Down Expand Up @@ -263,8 +261,10 @@ def navigate(self, delta_patch_index=0, delta_image_index=0):

patch_index = patch_index % self._active_label_group.childCount()

self._active_label.setSelected(False)
self._active_label = self._active_label_group.child(patch_index)
self._active_label.setSelected(True)
self.focus_region(self._active_label)

def focus_region(self, label: Optional[QTreeWidgetItem] = None,
edit_focused_label: bool = False):
Expand Down Expand Up @@ -302,18 +302,15 @@ def focus_region(self, label: Optional[QTreeWidgetItem] = None,
self._active_image_group = self._active_layers_group.parent()

current_center = [
int(pos * ax_scl)
pos * ax_scl
for pos, ax_scl in zip(self._active_label.center,
self._active_layer_channel.layer.scale)
]

viewer = napari.current_viewer()
viewer.dims.order = tuple(range(viewer.dims.ndim))
viewer.camera.center = current_center
viewer.dims.current_step = tuple(current_center)
viewer.camera.zoom = 4 / min(
self._active_layer_channel.layer.scale
)
viewer.dims.current_step = tuple(map(int, current_center))

for layer in viewer.layers:
layer.visible = False
Expand Down
3 changes: 0 additions & 3 deletions src/napari_activelearning/_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,9 +1323,6 @@ def set_active_item(self,
elif isinstance(item, ImageGroup):
self._active_image_group = item

else:
return

self.layer_scale_editor.active_layer_channel =\
self._active_layer_channel

Expand Down
19 changes: 9 additions & 10 deletions src/napari_activelearning/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,25 @@ def __init__(self):
self._channel_axis = 2
self._channels = [0, 0]

def _model_init(self, pretrained_model=None):
if (pretrained_model is not None
and not os.path.exists(pretrained_model)):
pretrained_model = None

def _model_init(self):
gpu = torch.cuda.is_available() and self._gpu
model_type = self._model_type if pretrained_model is None else None
if self._pretrained_model is None:
model_type = self._model_type
else:
model_type = None

self._model = models.CellposeModel(
gpu=gpu,
model_type=model_type,
pretrained_model=pretrained_model
pretrained_model=self._pretrained_model
)
self._model.mkldnn = False
self._model.net.mkldnn = False

self._model_dropout = models.CellposeModel(
gpu=gpu,
model_type=model_type,
pretrained_model=pretrained_model
pretrained_model=self._pretrained_model
)
self._model_dropout.mkldnn = False
self._model_dropout.net.mkldnn = False
Expand All @@ -87,7 +86,7 @@ def _model_init(self, pretrained_model=None):

def _run_pred(self, img, *args, **kwargs):
if self.refresh_model:
self._model_init(pretrained_model=self._pretrained_model)
self._model_init()

x = self._transform(img)

Expand All @@ -100,7 +99,7 @@ def _run_pred(self, img, *args, **kwargs):

def _run_eval(self, img, *args, **kwargs):
if self.refresh_model:
self._model_init(pretrained_model=self._pretrained_model)
self._model_init()

seg, _, _ = self._model.eval(img, diameter=None,
flow_threshold=None,
Expand Down
5 changes: 3 additions & 2 deletions src/napari_activelearning/_models_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def cellpose_segmentation_parameters(
"max": 2**16}] = 2,
channels: tuple[int, int] = (0, 0),
pretrained_model: Annotated[Path, {"widget_type": "FileEdit",
"mode": "r"}] = Path(""),
"visible": False,
"mode": "r"}] = Path(""),
model_type: Literal["custom",
"cyto",
"cyto2",
Expand Down Expand Up @@ -232,7 +233,7 @@ def _set_parameter(self, parameter_val, parameter_key=None):
.visible = False
self._pretrained_model = None

if getattr(self, parameter_key) != parameter_key:
if getattr(self, parameter_key) != parameter_val:
self.refresh_model = True
setattr(self, parameter_key, parameter_val)

Expand Down

0 comments on commit 18a7118

Please sign in to comment.