Skip to content

Commit d7dd8e2

Browse files
committed
working on utils + fixes
1 parent 79c66fd commit d7dd8e2

16 files changed

+187
-21
lines changed

docs/res/model_instance_seg.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ binary_connected
1212
binary_watershed
1313
**************************************
1414
.. autofunction:: napari_cellseg_annotator.model_instance_seg::binary_watershed
15+
16+
to_instance
17+
**************************************
18+
.. autofunction:: napari_cellseg_annotator.model_instance_seg::to_instance
19+
20+
to_semantic
21+
**************************************
22+
.. autofunction:: napari_cellseg_annotator.model_instance_seg::to_semantic

docs/res/utils.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ get_time
1313
**************************************
1414
.. autofunction:: napari_cellseg_annotator.utils::get_time
1515

16+
get_time_filepath
17+
**************************************
18+
.. autofunction:: napari_cellseg_annotator.utils::get_time_filepath
19+
1620
save_stack
1721
**************************************
1822
.. autofunction:: napari_cellseg_annotator.utils::save_stack
@@ -21,6 +25,10 @@ get_padding_dim
2125
**************************************
2226
.. autofunction:: napari_cellseg_annotator.utils::get_padding_dim
2327

28+
dice_coeff
29+
**************************************
30+
.. autofunction:: napari_cellseg_annotator.utils::dice_coeff
31+
2432
normalize_x
2533
**************************************
2634
.. autofunction:: napari_cellseg_annotator.utils::normalize_x

src/napari_cellseg_annotator/model_framework.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import napari
55
import torch
6+
67
# Qt
78
from qtpy.QtWidgets import QLineEdit
89
from qtpy.QtWidgets import QProgressBar

src/napari_cellseg_annotator/model_instance_seg.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,39 @@ def binary_watershed(
9292

9393

9494
def to_instance(image, is_file_path=True):
95+
"""Converts a **ground-truth** label to instance (unique id per object) labels. Does not remove small objects.
96+
97+
Args:
98+
image: image or path to image
99+
is_file_path: if True, will consider ``image`` to be a string containing a path to a file, if not treats it as an image data array.
100+
101+
Returns: resulting converted labels
102+
103+
"""
95104
if is_file_path:
96-
image = imread(image)
97-
image = image.compute()
105+
image = [imread(image)]
106+
# image = image.compute()
98107

99108
result = binary_watershed(
100-
image, thres_small=1, thres_seeding=0.3, rem_seed_thres=1
109+
image, thres_small=0, thres_seeding=0.3, rem_seed_thres=0
101110
) # TODO add params ?
102111

103112
return result
104113

105114

106115
def to_semantic(image, is_file_path=True):
116+
"""Converts a **ground-truth** label to semantic (binary 0/1) labels.
117+
118+
Args:
119+
image: image or path to image
120+
is_file_path: if True, will consider ``image`` to be a string containing a path to a file, if not treats it as an image data array.
121+
122+
Returns: resulting converted labels
123+
124+
"""
107125
if is_file_path:
108126
image = imread(image)
109-
image = image.compute()
127+
# image = image.compute()
110128

111129
image[image >= 1] = 1
112130
result = image.astype(np.uint16)

src/napari_cellseg_annotator/model_workers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import numpy as np
66
import torch
7+
78
# MONAI
89
from monai.data import CacheDataset
910
from monai.data import DataLoader
@@ -30,6 +31,7 @@
3031
from monai.transforms import Zoom
3132
from napari.qt.threading import GeneratorWorker
3233
from napari.qt.threading import WorkerBaseSignals
34+
3335
# Qt
3436
from qtpy.QtCore import Signal
3537
from tifffile import imwrite
@@ -305,7 +307,7 @@ def inference(self):
305307
file_path = (
306308
self.results_path
307309
+ "/"
308-
+ f"Prediction_{image_id}"
310+
+ f"Prediction_{image_id}_"
309311
+ original_filename
310312
+ "_"
311313
+ self.model_dict["name"]

src/napari_cellseg_annotator/plugin_base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ def __init__(self, viewer: "napari.viewer.Viewer", parent=None):
4242
self.label_path = ""
4343
"""str: path to label folder"""
4444

45+
self.results_path = ""
46+
"""str: path to results folder"""
47+
4548
self.filetype = ""
4649
"""str: filetype, .tif or .png"""
4750
self.as_folder = False
@@ -124,6 +127,7 @@ def load_results_path(self):
124127
self.update_default()
125128

126129
def update_default(self):
130+
"""Updates default path for smoother navigation when opening file dialogs"""
127131
self._default_path = [self.image_path, self.label_path]
128132

129133
def remove_from_viewer(self):
@@ -245,7 +249,7 @@ def load_image_dataset(self):
245249
def load_label_dataset(self):
246250
"""Show file dialog to set :py:attr:`~labels_filepaths`"""
247251
filenames = self.load_dataset_paths()
248-
if filenames != "" and filenames != [""]:
252+
if filenames != "" and filenames != [""] and filenames != []:
249253
self.labels_filepaths = filenames
250254
path = os.path.dirname(filenames[0])
251255
self.lbl_label_files.setText(path)

src/napari_cellseg_annotator/plugin_convert.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,47 +129,72 @@ def build(self):
129129

130130
def folder_to_semantic(self):
131131
"""Converts folder of labels to semantic labels"""
132+
133+
results_folder = (
134+
self.results_path
135+
+ f"/converted_to_semantic_labels_{utils.get_date_time()}"
136+
)
137+
138+
os.makedirs(results_folder, exist_ok=False)
139+
132140
for file in self.labels_filepaths:
133141

134142
image = to_semantic(file)
135143

136144
imwrite(
137-
self.results_path
138-
+ f"/converted_to_semantic_labels_{utils.get_date_time()}/"
139-
+ os.path.basename(file),
145+
results_folder + "/" + os.path.basename(file),
140146
image,
141147
)
142148

143149
def layer_to_semantic(self):
144150
"""Converts selected layer to semantic labels"""
145151

146152
im = self._viewer.layers.selection.active.data
153+
name = self._viewer.layers.selection.active.name
147154
semantic_labels = to_semantic(im, is_file_path=False)
148155

149-
self._viewer.add_labels(
150-
semantic_labels, name=f"Converted to semantic_{utils.get_time()}"
151-
)
156+
if self.results_path != "":
157+
imwrite(
158+
self.results_path
159+
+ f"/{name}_semantic_{utils.get_time_filepath()}"
160+
+ self.filetype_choice.currentText(),
161+
semantic_labels,
162+
)
163+
164+
self._viewer.add_labels(semantic_labels, name=f"converted_semantic")
152165

153166
def folder_to_instance(self):
154167
"""Converts the chosen folder to instance labels"""
155168

169+
results_folder = (
170+
self.results_path
171+
+ f"/converted_to_instance_labels_{utils.get_date_time()}"
172+
)
173+
174+
os.makedirs(results_folder, exist_ok=False)
175+
156176
for file in self.labels_filepaths:
157177

158178
image = to_instance(file)
159179

160180
imwrite(
161-
self.results_path
162-
+ f"/converted_to_instance_labels_{utils.get_date_time()}/"
163-
+ os.path.basename(file),
181+
results_folder + "/" + os.path.basename(file),
164182
image,
165183
)
166184

167185
def layer_to_instance(self):
168186
"""Converts the selected layer to instance labels"""
169187

170188
im = [self._viewer.layers.selection.active.data]
189+
name = self._viewer.layers.selection.active.name
171190
instance_labels = to_instance(im, is_file_path=False)
172191

173-
self._viewer.add_labels(
174-
instance_labels, name=f"Converted to instance_{utils.get_time()}"
175-
)
192+
if self.results_path != "":
193+
imwrite(
194+
self.results_path
195+
+ f"/{name}_instance_{utils.get_time_filepath()}"
196+
+ self.filetype_choice.currentText(),
197+
instance_labels,
198+
)
199+
200+
self._viewer.add_labels(instance_labels, name=f"converted_instance")

src/napari_cellseg_annotator/plugin_crop.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from magicgui import magicgui
77
from magicgui.widgets import Container
88
from magicgui.widgets import Slider
9+
910
# Qt
1011
from qtpy.QtWidgets import QLabel
1112
from qtpy.QtWidgets import QSizePolicy

src/napari_cellseg_annotator/plugin_dock.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
22
import warnings
3+
34
# import shutil
45
from pathlib import Path
56

67
import pandas as pd
8+
79
# Qt
810
from qtpy.QtWidgets import QVBoxLayout
911
from qtpy.QtWidgets import QWidget

src/napari_cellseg_annotator/plugin_helper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import napari
2+
23
# Qt
34
from qtpy.QtWidgets import QSizePolicy
45
from qtpy.QtWidgets import QVBoxLayout

0 commit comments

Comments
 (0)