|
1 | 1 | import logging |
2 | | -import os |
| 2 | +import os, shutil |
3 | 3 |
|
4 | 4 | import vtk |
| 5 | + |
5 | 6 | import pathlib |
6 | 7 | from pathlib import Path |
7 | 8 | import slicer |
| 9 | + |
8 | 10 | from slicer.ScriptedLoadableModule import * |
9 | 11 | from slicer.util import VTKObservationMixin |
10 | 12 | import ctk |
@@ -227,23 +229,37 @@ def enter(self): |
227 | 229 | def overwrite_mask_clicked(self): |
228 | 230 | # overwrite self.segmentEditorWidget.segmentationNode() |
229 | 231 | 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 |
233 | 248 | self.seg_mask_status[self.current_index] = 3 |
234 | 249 | # 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)) |
243 | 257 | 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 | + |
247 | 263 |
|
248 | 264 | def joinpath(self,rootdir,targetdir): |
249 | 265 | return os.path.join(os.sep, rootdir+os.sep,targetdir) |
|
0 commit comments