Skip to content

Commit

Permalink
ENH: Incorporate code review suggestions
Browse files Browse the repository at this point in the history
Co-authored-by: Jean-Christophe Fillion-Robin <[email protected]>
  • Loading branch information
sbelsk and jcfr committed Jan 13, 2025
1 parent 543ef58 commit eb36015
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
20 changes: 10 additions & 10 deletions AutoscoperM/AutoscoperM.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from slicer.util import VTKObservationMixin

from AutoscoperMLib import IO, ErrorUtils, SubVolumeExtraction
from AutoscoperMLib import IO, SubVolumeExtraction, Validation

#
# AutoscoperM
Expand Down Expand Up @@ -448,7 +448,7 @@ def onGeneratePartialVolumes(self):
modelSubDir = self.ui.modelSubDir.text
segmentationNode = self.ui.pv_SegNodeComboBox.currentNode()

ErrorUtils.validateInputs(
Validation.validateInputs(
volumeNode=volumeNode,
segmentationNode=segmentationNode,
mainOutputDir=mainOutputDir,
Expand Down Expand Up @@ -502,7 +502,7 @@ def onGenerateConfig(self):
camCalList = self.ui.camCalList

# Validate the inputs
ErrorUtils.validateInputs(
Validation.validateInputs(
volumeNode=volumeNode,
mainOutputDir=mainOutputDir,
configFileName=configFileName,
Expand All @@ -513,7 +513,7 @@ def onGenerateConfig(self):
partialVolumeList=partialVolumeList,
camCalList=camCalList,
)
ErrorUtils.validatePaths(
Validation.validatePaths(
mainOutputDir=mainOutputDir,
tiffDir=os.path.join(mainOutputDir, tiffSubDir),
radiographSubDir=os.path.join(mainOutputDir, radiographSubDir),
Expand Down Expand Up @@ -596,7 +596,7 @@ def get_checked_items(listWidget):
]

# Validate the extracted parameters
ErrorUtils.validateInputs(
Validation.validateInputs(
*trialDirs,
*partialVolumeFiles,
*camCalFiles,
Expand Down Expand Up @@ -633,12 +633,12 @@ def onImportModels(self):

volumeNode = self.ui.volumeSelector.currentNode()

ErrorUtils.validateInputs(volumeNode=volumeNode)
Validation.validateInputs(volumeNode=volumeNode)

if self.ui.segSTL_loadRadioButton.isChecked():
segmentationFileDir = self.ui.segSTL_modelsDir.currentPath

ErrorUtils.validatePaths(segmentationFileDir=segmentationFileDir)
Validation.validatePaths(segmentationFileDir=segmentationFileDir)

segmentationFiles = glob.glob(os.path.join(segmentationFileDir, "*.*"))
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
Expand Down Expand Up @@ -666,7 +666,7 @@ def onSegmentation(self):

volumeNode = self.ui.volumeSelector.currentNode()

ErrorUtils.validateInputs(volumeNode=volumeNode)
Validation.validateInputs(volumeNode=volumeNode)

if self.ui.segGen_autoRadioButton.isChecked():
currentVolumeNode = volumeNode
Expand Down Expand Up @@ -791,14 +791,14 @@ def populateListFromOutputSubDir(self, listWidget, fileSubDir, itemType="file"):
listWidget.clear()

mainOutputDir = self.ui.mainOutputSelector.currentPath
ErrorUtils.validateInputs(
Validation.validateInputs(
listWidget=listWidget,
mainOutputDir=mainOutputDir,
fileSubDir=fileSubDir,
)

fileDir = os.path.join(mainOutputDir, fileSubDir)
ErrorUtils.validatePaths(fileDir=fileDir)
Validation.validatePaths(fileDir=fileDir)

if itemType == "file":
listFiles = [f.name for f in os.scandir(fileDir) if os.path.isfile(f)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os


#
# ValueErrorsException: A custom exception class that accepts a list of errors
#
class ValueErrorsException(Exception):
"""A custom exception class that accepts a list of errors."""

def __init__(self, errors):
if not isinstance(errors, list):
raise ValueError("The errors input must be a list")
Expand All @@ -14,25 +13,18 @@ def __init__(self, errors):
super().__init__("\n".join(errors))

def __str__(self):
err_str = "Invalid value" if len(self.errors) == 1 else "Multiple invalid values"
err_str = "Invalid value{}".format("" if len(self.errors) == 0 else "s")

return err_str + ":\n" + "\n".join(self.errors)


#
# helper functions
#


def validateInputs(*args: tuple, **kwargs: dict) -> bool:
def validateInputs(*args: tuple, **kwargs: dict) -> None:
"""
Validates that the provided inputs are not None or empty.
:param args: list of inputs to validate
:param kwargs: list of inputs to validate
:param kwargs: dictionary of inputs to validate
:raises: ValueErrorsException
:return: True if all inputs are valid
"""
errors = []
for arg in args:
Expand All @@ -47,21 +39,17 @@ def validateInputs(*args: tuple, **kwargs: dict) -> bool:
if isinstance(arg, str) and arg == "":
errors.append(f"Input '{name}' is an empty string")

if len(errors) != 0:
if len(errors) > 0:
raise ValueErrorsException(errors)

return True


def validatePaths(*args: tuple, **kwargs: dict) -> bool:
def validatePaths(*args: tuple, **kwargs: dict) -> None:
"""
Checks that the provided paths exist.
:param args: list of paths to validate
:param kwargs: list of paths to validate
:raises: ValueErrorsException
:return: True if all paths exist
"""
errors = []
for arg in args:
Expand All @@ -72,7 +60,5 @@ def validatePaths(*args: tuple, **kwargs: dict) -> bool:
if not os.path.exists(path):
errors.append(f"Input path '{name}' ({path}) does not exist")

if len(errors) != 0:
if len(errors) > 0:
raise ValueErrorsException(errors)

return True
2 changes: 1 addition & 1 deletion AutoscoperM/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ set(MODULE_NAME AutoscoperM)
set(MODULE_PYTHON_SCRIPTS
${MODULE_NAME}.py
${MODULE_NAME}Lib/__init__.py
${MODULE_NAME}Lib/ErrorUtils.py
${MODULE_NAME}Lib/IO.py
${MODULE_NAME}Lib/SubVolumeExtraction.py
${MODULE_NAME}Lib/Validation.py
)

set(MODULE_PYTHON_RESOURCES
Expand Down

0 comments on commit eb36015

Please sign in to comment.