For the class below, I'm getting this exception:
return probe.GetOutput().GetPointData().GetArray('ImageScalars')
SyntaxError: 'return' outside function
Why is Python considering "return" outside the function? Is it an indentation issue? Thanks.
def probeVolume(self,volumeNode,rulerNode):
# get ruler endpoints coordinates in RAS
p0ras = rulerNode.GetPolyData().GetPoint(0)+(1,)
p1ras = rulerNode.GetPolyData().GetPoint(1)+(1,)
# Convert RAS to IJK coordinates of the vtkImageData
ras2ijk = vtk.vtkMatrix4x4()
volumeNode.GetRASToIJKMatrix(ras2ijk)
p0ijk = [int(round(c)) for c in ras2ijk.MultiplyPoint(p0ras)[:3]]
p1ijk = [int(round(c)) for c in ras2ijk.MultiplyPoint(p1ras)[:3]]
# Create VTK line that will be used for sampling
line = vtk.vtkLineSource()
line.SetResolution(100)
line.SetPoint1(p0ijk[0],p0ijk[1],p0ijk[2])
line.SetPoint2(p1ijk[0],p1ijk[1],p1ijk[2])
# Create VTK probe filter and sample the image
probe = vtk.vtkProbeFilter()
probe.SetInputConnection(line.GetOutputPort())
probe.SetSourceData(volumeNode.GetImageData())
probe.Update()
# return VTK array
return probe.GetOutput().GetPointData().GetArray('ImageScalars')
答案 0 :(得分:1)
The problem is your inconsistency of indentation. Python depends on indentation to determine code blocks. To fix the problem, you'll need to indent all of the code in your probeVolume
function by the same amount.
答案 1 :(得分:1)
It is an indentation error. The first two lines of the function are double-indented.
Fix:
def probeVolume(self,volumeNode,rulerNode):
# get ruler endpoints coordinates in RAS
p0ras = rulerNode.GetPolyData().GetPoint(0)+(1,)
p1ras = rulerNode.GetPolyData().GetPoint(1)+(1,)
# Convert RAS to IJK coordinates of the vtkImageData
ras2ijk = vtk.vtkMatrix4x4()
volumeNode.GetRASToIJKMatrix(ras2ijk)
p0ijk = [int(round(c)) for c in ras2ijk.MultiplyPoint(p0ras)[:3]]
p1ijk = [int(round(c)) for c in ras2ijk.MultiplyPoint(p1ras)[:3]]
# Create VTK line that will be used for sampling
line = vtk.vtkLineSource()
line.SetResolution(100)
line.SetPoint1(p0ijk[0],p0ijk[1],p0ijk[2])
line.SetPoint2(p1ijk[0],p1ijk[1],p1ijk[2])
# Create VTK probe filter and sample the image
probe = vtk.vtkProbeFilter()
probe.SetInputConnection(line.GetOutputPort())
probe.SetSourceData(volumeNode.GetImageData())
probe.Update()
# return VTK array
return probe.GetOutput().GetPointData().GetArray('ImageScalars')
答案 2 :(得分:1)
The first two lines of the function have 4 spaces from the function
def probeVolume(self,volumeNode,rulerNode):
# get ruler endpoints coordinates in RAS
p0ras = rulerNode.GetPolyData().GetPoint(0)+(1,)
p1ras = rulerNode.GetPolyData().GetPoint(1)+(1,)
I am not 100% sure if this is the issue , but try indenting the 3rd and 4th line with 2 spaces. Example -
def probeVolume(self,volumeNode,rulerNode):
# get ruler endpoints coordinates in RAS
p0ras = rulerNode.GetPolyData().GetPoint(0)+(1,)
p1ras = rulerNode.GetPolyData().GetPoint(1)+(1,)