Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Check for existing PV output files before generation #164

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions AutoscoperM/AutoscoperM.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,10 @@ def setup(self):
self.ui.configGenButton.connect("clicked(bool)", self.onGenerateConfig)

# Default output directory
self.ui.mainOutputSelector.setCurrentPath(
os.path.join(slicer.mrmlScene.GetCacheManager().GetRemoteCacheDirectory(), "AutoscoperM-Pre-Processing")
self.defaultOuputDir = os.path.join(
slicer.mrmlScene.GetCacheManager().GetRemoteCacheDirectory(), "AutoscoperM-Pre-Processing"
)
self.ui.mainOutputSelector.setCurrentPath(self.defaultOuputDir)

# Make sure parameter node is initialized (needed for module reload)
self.initializeParameterNode()
Expand Down Expand Up @@ -458,13 +459,33 @@ def onGeneratePartialVolumes(self):
modelSubDir=modelSubDir,
)

# if output files already exist, confirm with user whether to overwrite
if (
mainOutputDir != self.defaultOuputDir
and self.logic.pathsExistWithFiles(
os.path.join(mainOutputDir, tiffSubDir),
os.path.join(mainOutputDir, tfmSubDir),
os.path.join(mainOutputDir, modelSubDir),
)
and not slicer.util.confirmOkCancelDisplay(
"Existing pre-processing results were detected in the specified output subdirectories.\n\n"
"Would you like to proceed and risk overwriting them?\n\n"
"Click 'OK' to proceed with partial volume generation.\n"
"Click 'Cancel' to stop the process.\n",
dontShowAgainSettingsKey="AutoscoperM/DontShowPartialVolumesGenerationOutputOverwriteWarning",
)
):
return

# if the output paths don't already exist, create them
self.logic.createPathsIfNotExists(
mainOutputDir,
os.path.join(mainOutputDir, tiffSubDir),
os.path.join(mainOutputDir, tfmSubDir),
os.path.join(mainOutputDir, trackingSubDir),
os.path.join(mainOutputDir, modelSubDir),
)

self.ui.progressBar.setValue(0)
self.ui.progressBar.setMaximum(100)
self.logic.saveSubVolumesFromSegmentation(
Expand Down Expand Up @@ -1167,6 +1188,16 @@ def showVolumeIn3D(volumeNode: slicer.vtkMRMLVolumeNode):
logic.UpdateDisplayNodeFromVolumeNode(displayNode, volumeNode)
slicer.mrmlScene.RemoveNode(slicer.util.getNode("Volume rendering ROI"))

@staticmethod
def pathsExistWithFiles(*args: tuple) -> bool:
"""
Checks if the given paths exist and if they contain any existing files.

:param args: list of paths to check
:return: True if any of the paths exist and contain files, False otherwise
"""
return any(os.path.exists(arg) and len(os.listdir(arg)) > 0 for arg in args)

@staticmethod
def createPathsIfNotExists(*args: tuple) -> None:
"""
Expand Down
Loading