Skip to content

Commit

Permalink
WIP: Better CLI code
Browse files Browse the repository at this point in the history
  • Loading branch information
NicerNewerCar committed Sep 13, 2023
1 parent f06d873 commit 8bfeca2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
32 changes: 17 additions & 15 deletions AutoscoperM/AutoscoperM.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,8 @@ def progressCallback(x):
IO.writeTemporyFile(volumeFName, volumeImageData)

# Execute CLI for each camera
# Note: CLI nodes are currently not executed in parallel. See https://github.com/Slicer/Slicer/pull/6723
# This just allows the UI to remain responsive while the CLI nodes are running for now.
cliModule = slicer.modules.virtualradiographgeneration
cliNodes = []
for cam in cameras:
Expand All @@ -1089,23 +1091,10 @@ def progressCallback(x):
"outputFName": os.path.join(cameraDir, "1.tif"),
}
cliNode = slicer.cli.run(cliModule, None, parameters) # run asynchronously
cliNode.AddObserver("ModifiedEvent", self.cliObserver)
cliNodes.append(cliNode)

# Note: CLI nodes are currently not executed in parallel. See https://github.com/Slicer/Slicer/pull/6723
# This just allows the UI to remain responsive while the CLI nodes are running for now.

# Wait for all the CLI nodes to finish
for i, cliNode in enumerate(cliNodes):
while cliNodes[i].GetStatusString() != "Completed":
slicer.app.processEvents()
if cliNode.GetStatus() & cliNode.ErrorsMask:
# error
errorText = cliNode.GetErrorText()
slicer.mrmlScene.RemoveNode(cliNode)
raise ValueError("CLI execution failed: " + errorText)
slicer.mrmlScene.RemoveNode(cliNode)
progress = ((i + 1) / len(cameras)) * 30 + 10
progressCallback(progress)
# Need some sort of join code here

def generateCameraCalibrationFiles(
self,
Expand Down Expand Up @@ -1151,3 +1140,16 @@ def progressCallback(x):

progress = ((idx + 1) / len(bestCameras)) * 10 + 90
progressCallback(progress)

def cliObserver(self, cliNode, event):
"""
Observer function for CLI nodes.
"""
logging.info(f"Got an event, {event}, from CLI node {cliNode.GetName()}")
if cliNode.IsA("vtkMRMLCommandLineModuleNode"):
logging.info(f"Status is {cliNode.GetStatusString()}")
if cliNode.GetStatus() & cliNode.Completed:
if cliNode.GetStatus() & cliNode.ErrorsMask:
logging.error(f"CLI execution failed: {cliNode.GetErrorText()}")
else:
logging.info("CLI execution completed")
2 changes: 1 addition & 1 deletion AutoscoperM/AutoscoperMLib/RadiographGeneration.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __str__(self) -> str:
def _createFrustumModel(cam: Camera) -> None:
model = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLModelNode")
model.CreateDefaultDisplayNodes()
model.GetDisplayNode().SetColor(1, 0, 0) # Red
model.GetDisplayNode().SetColor(1, 0, 0) # Red
model.GetDisplayNode().SetOpacity(0.3)
model.GetDisplayNode().SetVisibility(True)

Expand Down

0 comments on commit 8bfeca2

Please sign in to comment.