Skip to content

Commit 7c29e2e

Browse files
Merge pull request #20 from laurentletg/saving_edited_mask_subfolder
Updated overwrite_mask_clicked method (Fixes #19)
2 parents 0a4c47a + 016db30 commit 7c29e2e

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

SegmentationReview/SegmentationReview.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import logging
2-
import os
2+
import os, shutil
33

44
import vtk
5+
56
import pathlib
67
from pathlib import Path
78
import slicer
9+
810
from slicer.ScriptedLoadableModule import *
911
from slicer.util import VTKObservationMixin
1012
import ctk
@@ -227,23 +229,37 @@ def enter(self):
227229
def overwrite_mask_clicked(self):
228230
# overwrite self.segmentEditorWidget.segmentationNode()
229231
self.segmentation_node = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLSegmentationNode')
230-
file_path = self.joinpath(self.directory,"t.seg.nrrd")
231-
# Save the segmentation node to file as nifti
232-
self.file_path_nifti = str(self.nifti_files[self.current_index]).split(".")[0]+f"_mask_{datetime.now().strftime('%Y%m%d_%H%M%S')}.nii.gz"
232+
file_path = self.segmentation_files[self.current_index]
233+
print("Overwriting mask",file_path)
234+
edited_mask_filename = str(os.path.basename(self.nifti_files[self.current_index])).split(".")[0]+f"_edited_mask_{datetime.now().strftime('%Y%m%d_%H%M%S')}.nii.gz"
235+
edited_mask_filepath = os.path.join(self.directory, edited_mask_filename)
236+
237+
# Convert the segmentation node to a labelmap volume node (https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#export-labelmap-node-from-segmentation-node)
238+
edited_mask_labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
239+
referenceVolumeNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")
240+
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(self.segmentation_node, edited_mask_labelmapVolumeNode, referenceVolumeNode)
241+
edited_mask_labelmapVolumeNode.SetName(edited_mask_filename.split(".")[0])
242+
243+
# Save the edited mask to file
244+
slicer.util.saveNode(edited_mask_labelmapVolumeNode, edited_mask_filepath)
245+
print(f"Saved edited mask to {edited_mask_filepath}")
246+
247+
# update the mask status
233248
self.seg_mask_status[self.current_index] = 3
234249
# add to the list of segmentation files
235-
self.segmentation_files[self.current_index] = self.file_path_nifti
236-
# Save the segmentation node to file
237-
slicer.util.saveNode(self.segmentation_node, file_path)
238-
img = sitk.ReadImage(file_path)
239-
240-
sitk.WriteImage(img, self.file_path_nifti)
241-
242-
#delete the temporary file
250+
self.segmentation_files[self.current_index] = edited_mask_filepath
251+
252+
# move the previous mask to a backup folder (instead of deleting it)
253+
backup_folder = os.path.join(self.directory, "backup_masks")
254+
if not os.path.exists(backup_folder):
255+
os.makedirs(backup_folder)
256+
backup_mask_filepath = os.path.join(backup_folder, os.path.basename(file_path))
243257
try:
244-
os.remove(file_path)
245-
except:
246-
pass
258+
shutil.move(file_path, backup_mask_filepath)
259+
print(f"Moved previous mask to {backup_mask_filepath}")
260+
except OSError as e:
261+
print(f"Error moving previous mask to {backup_mask_filepath}: {e}")
262+
247263

248264
def joinpath(self,rootdir,targetdir):
249265
return os.path.join(os.sep, rootdir+os.sep,targetdir)

0 commit comments

Comments
 (0)