Skip to content

Commit

Permalink
fix saving to outputdir when using dask arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
AnniekStok committed Aug 13, 2024
1 parent 1358e41 commit 93463db
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 211 deletions.
61 changes: 26 additions & 35 deletions src/napari_segmentation_correction/_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,42 +204,33 @@ def _save_labels(self) -> None:
if isinstance(self.label_manager.selected_layer.data, da.core.Array):

if self.outputdir is None:
msg = QMessageBox()
msg.setWindowTitle("No output directory selected")
msg.setText("Please specify an output directory first!")
msg.setIcon(QMessageBox.Information)
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
return False

else:
outputdir = os.path.join(
self.outputdir,
(self.label_manager.selected_layer.name + "_finalresult"),
)
if os.path.exists(outputdir):
shutil.rmtree(outputdir)
os.mkdir(outputdir)

for i in range(
self.label_manager.selected_layer.data.shape[0]
): # Loop over the first dimension
current_stack = self.label_manager.selected_layer.data[
i
].compute() # Compute the current stack
tifffile.imwrite(
os.path.join(
outputdir,
(
self.label_manager.selected_layer.name
+ "_TP"
+ str(i).zfill(4)
+ ".tif"
),
self.outputdir = QFileDialog.getExistingDirectory(self, "Select Output Folder")
outputdir = os.path.join(
self.outputdir,
(self.label_manager.selected_layer.name + "_finalresult"),
)
if os.path.exists(outputdir):
shutil.rmtree(outputdir)
os.mkdir(outputdir)

for i in range(
self.label_manager.selected_layer.data.shape[0]
): # Loop over the first dimension
current_stack = self.label_manager.selected_layer.data[
i
].compute() # Compute the current stack
tifffile.imwrite(
os.path.join(
outputdir,
(
self.label_manager.selected_layer.name
+ "_TP"
+ str(i).zfill(4)
+ ".tif"
),
np.array(current_stack, dtype="uint16"),
)
return True
),
np.array(current_stack, dtype="uint16"),
)

elif len(self.label_manager.selected_layer.data.shape) == 4:
filename, _ = QFileDialog.getSaveFileName(
Expand Down
192 changes: 90 additions & 102 deletions src/napari_segmentation_correction/erosion_dilation_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
QSpinBox,
QVBoxLayout,
QWidget,
QFileDialog
)
from scipy import ndimage
from scipy.ndimage import binary_erosion
Expand All @@ -33,6 +34,7 @@ def __init__(

self.viewer = viewer
self.label_manager = label_manager
self.outputdir = None

dil_erode_box = QGroupBox("Erode/dilate labels")
dil_erode_box_layout = QVBoxLayout()
Expand Down Expand Up @@ -88,63 +90,56 @@ def _erode_labels(self):

if isinstance(self.label_manager.selected_layer.data, da.core.Array):
if self.outputdir is None:
msg = QMessageBox()
msg.setWindowTitle("No output directory selected")
msg.setText("Please specify an output directory first!")
msg.setIcon(QMessageBox.Information)
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
return False
self.outputdir = QFileDialog.getExistingDirectory(self, "Select Output Folder")

else:
outputdir = os.path.join(
self.outputdir,
(self.label_manager.selected_layer.name + "_eroded"),
)
if os.path.exists(outputdir):
shutil.rmtree(outputdir)
os.mkdir(outputdir)
outputdir = os.path.join(
self.outputdir,
(self.label_manager.selected_layer.name + "_eroded"),
)
if os.path.exists(outputdir):
shutil.rmtree(outputdir)
os.mkdir(outputdir)

for i in range(
self.label_manager.selected_layer.data.shape[0]
): # Loop over the first dimension
current_stack = self.label_manager.selected_layer.data[
i
].compute() # Compute the current stack
mask = current_stack > 0
filled_mask = ndimage.binary_fill_holes(mask)
eroded_mask = binary_erosion(
filled_mask,
structure=structuring_element,
iterations=iterations,
)
eroded = np.where(eroded_mask, current_stack, 0)
tifffile.imwrite(
os.path.join(
outputdir,
(
self.label_manager.selected_layer.name
+ "_eroded_TP"
+ str(i).zfill(4)
+ ".tif"
),
),
np.array(eroded, dtype="uint16"),
)

file_list = [
os.path.join(outputdir, fname)
for fname in os.listdir(outputdir)
if fname.endswith(".tif")
]
self.label_manager.selected_layer = self.viewer.add_labels(
da.stack([imread(fname) for fname in sorted(file_list)]),
name=self.label_manager.selected_layer.name + "_eroded",
for i in range(
self.label_manager.selected_layer.data.shape[0]
): # Loop over the first dimension
current_stack = self.label_manager.selected_layer.data[
i
].compute() # Compute the current stack
mask = current_stack > 0
filled_mask = ndimage.binary_fill_holes(mask)
eroded_mask = binary_erosion(
filled_mask,
structure=structuring_element,
iterations=iterations,
)
self.label_manager._update_labels(
self.label_manager.selected_layer.name
eroded = np.where(eroded_mask, current_stack, 0)
tifffile.imwrite(
os.path.join(
outputdir,
(
self.label_manager.selected_layer.name
+ "_eroded_TP"
+ str(i).zfill(4)
+ ".tif"
),
),
np.array(eroded, dtype="uint16"),
)
return True

file_list = [
os.path.join(outputdir, fname)
for fname in os.listdir(outputdir)
if fname.endswith(".tif")
]
self.label_manager.selected_layer = self.viewer.add_labels(
da.stack([imread(fname) for fname in sorted(file_list)]),
name=self.label_manager.selected_layer.name + "_eroded",
)
self.label_manager._update_labels(
self.label_manager.selected_layer.name
)
return True

else:
if len(self.label_manager.selected_layer.data.shape) == 4:
Expand Down Expand Up @@ -201,59 +196,52 @@ def _dilate_labels(self):

if isinstance(self.label_manager.selected_layer.data, da.core.Array):
if self.outputdir is None:
msg = QMessageBox()
msg.setWindowTitle("No output directory selected")
msg.setText("Please specify an output directory first!")
msg.setIcon(QMessageBox.Information)
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
return False
self.outputdir = QFileDialog.getExistingDirectory(self, "Select Output Folder")

else:
outputdir = os.path.join(
self.outputdir,
(self.label_manager.selected_layer.name + "_dilated"),
)
if os.path.exists(outputdir):
shutil.rmtree(outputdir)
os.mkdir(outputdir)
outputdir = os.path.join(
self.outputdir,
(self.label_manager.selected_layer.name + "_dilated"),
)
if os.path.exists(outputdir):
shutil.rmtree(outputdir)
os.mkdir(outputdir)

for i in range(
self.label_manager.selected_layer.data.shape[0]
): # Loop over the first dimension
expanded_labels = self.label_manager.selected_layer.data[
i
].compute() # Compute the current stack
for _j in range(iterations):
expanded_labels = expand_labels(
expanded_labels, distance=diam
)
tifffile.imwrite(
os.path.join(
outputdir,
(
self.label_manager.selected_layer.name
+ "_dilated_TP"
+ str(i).zfill(4)
+ ".tif"
),
),
np.array(expanded_labels, dtype="uint16"),
for i in range(
self.label_manager.selected_layer.data.shape[0]
): # Loop over the first dimension
expanded_labels = self.label_manager.selected_layer.data[
i
].compute() # Compute the current stack
for _j in range(iterations):
expanded_labels = expand_labels(
expanded_labels, distance=diam
)

file_list = [
os.path.join(outputdir, fname)
for fname in os.listdir(outputdir)
if fname.endswith(".tif")
]
self.label_manager.selected_layer = self.viewer.add_labels(
da.stack([imread(fname) for fname in sorted(file_list)]),
name=self.label_manager.selected_layer.name + "_dilated",
)
self.label_manager._update_labels(
self.label_manager.selected_layer.name
tifffile.imwrite(
os.path.join(
outputdir,
(
self.label_manager.selected_layer.name
+ "_dilated_TP"
+ str(i).zfill(4)
+ ".tif"
),
),
np.array(expanded_labels, dtype="uint16"),
)
return True

file_list = [
os.path.join(outputdir, fname)
for fname in os.listdir(outputdir)
if fname.endswith(".tif")
]
self.label_manager.selected_layer = self.viewer.add_labels(
da.stack([imread(fname) for fname in sorted(file_list)]),
name=self.label_manager.selected_layer.name + "_dilated",
)
self.label_manager._update_labels(
self.label_manager.selected_layer.name
)
return True

else:
if len(self.label_manager.selected_layer.data.shape) == 4:
Expand Down
Loading

0 comments on commit 93463db

Please sign in to comment.