Skip to content

Commit c7203a0

Browse files
committed
WIP added metrics utils + fixes + new utils.py
1 parent d7dd8e2 commit c7203a0

File tree

7 files changed

+275
-60
lines changed

7 files changed

+275
-60
lines changed

src/napari_cellseg_annotator/_tests/test_utils.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,42 @@
88
from napari_cellseg_annotator import utils
99

1010

11-
def test_get_padding_dim(make_napari_viewer):
11+
def test_align_array_sizes():
12+
13+
im = np.zeros((128, 512, 256))
14+
print(im.shape)
15+
16+
dim_1 = (64, 64, 512)
17+
ground = np.array((512, 64, 64))
18+
pred = np.array(dim_1)
19+
20+
ori, targ = utils.align_array_sizes(ground, pred)
21+
22+
im_1 = np.moveaxis(im, ori, targ)
23+
print(im_1.shape)
24+
assert im_1.shape == (512, 256, 128)
25+
26+
dim_2 = (512, 256, 128)
27+
ground = np.array((128, 512, 256))
28+
pred = np.array(dim_2)
29+
30+
ori, targ = utils.align_array_sizes(ground, pred)
31+
32+
im_2 = np.moveaxis(im, ori, targ)
33+
print(im_2.shape)
34+
assert im_2.shape == dim_2
35+
36+
dim_3 = (128, 128, 128)
37+
ground = np.array(dim_3)
38+
pred = np.array(dim_3)
39+
40+
ori, targ = utils.align_array_sizes(ground, pred)
41+
im_3 = np.moveaxis(im, ori, targ)
42+
print(im_3.shape)
43+
assert im_3.shape == im.shape
44+
45+
46+
def test_get_padding_dim():
1247

1348
tensor = torch.randn(100, 30, 40)
1449
size = tensor.size()

src/napari_cellseg_annotator/model_instance_seg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def binary_watershed(
9191
return np.array(segm)
9292

9393

94-
def to_instance(image, is_file_path=True):
94+
def to_instance(image, is_file_path=False):
9595
"""Converts a **ground-truth** label to instance (unique id per object) labels. Does not remove small objects.
9696
9797
Args:
@@ -112,7 +112,7 @@ def to_instance(image, is_file_path=True):
112112
return result
113113

114114

115-
def to_semantic(image, is_file_path=True):
115+
def to_semantic(image, is_file_path=False):
116116
"""Converts a **ground-truth** label to semantic (binary 0/1) labels.
117117
118118
Args:

src/napari_cellseg_annotator/plugin_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def load_image_dataset(self):
239239
filenames = self.load_dataset_paths()
240240
# print(filenames)
241241
if filenames != "" and filenames != [""] and filenames != []:
242-
self.images_filepaths = filenames
242+
self.images_filepaths = sorted(filenames)
243243
# print(filenames)
244244
path = os.path.dirname(filenames[0])
245245
self.lbl_image_files.setText(path)
@@ -250,7 +250,7 @@ def load_label_dataset(self):
250250
"""Show file dialog to set :py:attr:`~labels_filepaths`"""
251251
filenames = self.load_dataset_paths()
252252
if filenames != "" and filenames != [""] and filenames != []:
253-
self.labels_filepaths = filenames
253+
self.labels_filepaths = sorted(filenames)
254254
path = os.path.dirname(filenames[0])
255255
self.lbl_label_files.setText(path)
256256
self.update_default()

src/napari_cellseg_annotator/plugin_convert.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def folder_to_semantic(self):
139139

140140
for file in self.labels_filepaths:
141141

142-
image = to_semantic(file)
142+
image = to_semantic(file, is_file_path=True)
143143

144144
imwrite(
145145
results_folder + "/" + os.path.basename(file),
@@ -151,7 +151,7 @@ def layer_to_semantic(self):
151151

152152
im = self._viewer.layers.selection.active.data
153153
name = self._viewer.layers.selection.active.name
154-
semantic_labels = to_semantic(im, is_file_path=False)
154+
semantic_labels = to_semantic(im)
155155

156156
if self.results_path != "":
157157
imwrite(
@@ -175,7 +175,7 @@ def folder_to_instance(self):
175175

176176
for file in self.labels_filepaths:
177177

178-
image = to_instance(file)
178+
image = to_instance(file, is_file_path=True)
179179

180180
imwrite(
181181
results_folder + "/" + os.path.basename(file),
@@ -187,7 +187,7 @@ def layer_to_instance(self):
187187

188188
im = [self._viewer.layers.selection.active.data]
189189
name = self._viewer.layers.selection.active.name
190-
instance_labels = to_instance(im, is_file_path=False)
190+
instance_labels = to_instance(im)
191191

192192
if self.results_path != "":
193193
imwrite(

src/napari_cellseg_annotator/plugin_crop.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from magicgui import magicgui
77
from magicgui.widgets import Container
88
from magicgui.widgets import Slider
9-
109
# Qt
1110
from qtpy.QtWidgets import QLabel
1211
from qtpy.QtWidgets import QSizePolicy
@@ -73,6 +72,20 @@ def __init__(self, viewer: "napari.viewer.Viewer", parent):
7372

7473
self.build()
7574

75+
###########################################
76+
if utils.ENABLE_TEST_MODE():
77+
# TODO : remove/disable once done
78+
if self.as_folder:
79+
self.image_path = "C:/Users/Cyril/Desktop/Proj_bachelor/data/visual_png/sample"
80+
if self.crop_label_choice.isChecked():
81+
self.label_path = "C:/Users/Cyril/Desktop/Proj_bachelor/data/visual_png/sample_labels"
82+
else:
83+
self.image_path = "C:/Users/Cyril/Desktop/Proj_bachelor/data/visual_tif/volumes/images.tif"
84+
if self.crop_label_choice.isChecked():
85+
self.label_path = "C:/Users/Cyril/Desktop/Proj_bachelor/data/visual_tif/labels/testing_im.tif"
86+
87+
###########################################
88+
7689
def toggle_label_path(self):
7790
if self.crop_label_choice.isChecked():
7891
self.lbl_label.setVisible(True)
@@ -141,16 +154,20 @@ def quicksave(self):
141154
if not self.as_folder:
142155
if self.image is not None:
143156
im_filename = os.path.basename(self.image_path).split(".")[0]
157+
print(im_filename)
144158
im_dir = os.path.split(self.image_path)[0] + "/cropped"
159+
print(im_dir)
145160
os.makedirs(im_dir, exist_ok=True)
146161
viewer.layers["cropped"].save(
147162
im_dir + "/" + im_filename + "_cropped_" + time + ".tif"
148163
)
149164

150-
print(self.label)
165+
# print(self.label)
151166
if self.label is not None:
152167
im_filename = os.path.basename(self.label_path).split(".")[0]
168+
print(im_filename)
153169
im_dir = os.path.split(self.label_path)[0] + "/cropped"
170+
print(im_dir)
154171
name = (
155172
im_dir
156173
+ "/"
@@ -202,20 +219,6 @@ def start(self):
202219
self.filetype = self.filetype_choice.currentText()
203220
self.crop_labels = self.crop_label_choice.isChecked()
204221

205-
###########################################
206-
if utils.ENABLE_TEST_MODE():
207-
# TODO : remove/disable once done
208-
if self.as_folder:
209-
self.image_path = "C:/Users/Cyril/Desktop/Proj_bachelor/data/visual_png/sample"
210-
if self.crop_labels:
211-
self.label_path = "C:/Users/Cyril/Desktop/Proj_bachelor/data/visual_png/sample_labels"
212-
else:
213-
self.image_path = "C:/Users/Cyril/Desktop/Proj_bachelor/data/visual_tif/volumes/images.tif"
214-
if self.crop_labels:
215-
self.label_path = "C:/Users/Cyril/Desktop/Proj_bachelor/data/visual_tif/labels/testing_im.tif"
216-
217-
###########################################
218-
219222
if not self.check_ready():
220223
return
221224

0 commit comments

Comments
 (0)