Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Entry Deviation Perk Tutor Metric
import math
import vtk
from datetime import datetime
from PythonMetricsCalculator import PerkEvaluatorMetric
class EntryDeviation( PerkEvaluatorMetric ):
# Static methods
@staticmethod
def GetMetricName():
return "Entry Deviation"
@staticmethod
def GetMetricUnit():
return "mm"
@staticmethod
def GetTransformRoles():
return [ "Needle" ]
@staticmethod
def GetAnatomyRoles():
return { "Targets": "vtkMRMLMarkupsFiducialNode" }
# Instance methods
def __init__( self ):
PerkEvaluatorMetric.__init__( self )
self.R_deviation = []
self.A_deviation = []
self.S_deviation = []
self.needleTipTargetDistance = []
self.allErrors = []
self.timeStamps = []
self.timestr = datetime.now().strftime("%Y%m%d-%H%M%S")
self.fname = "C:/Users/P/Documents/tool-Trajectory-Entry-"+self.timestr+".csv"
def SetAnatomy( self, role, node ):
if ( role == "Targets" ):
self.targets = node
return True
return False
def AddTimestamp( self, time, matrix, point, role ):
currTargetPosition = [ 0, 0, 0 ]
self.targets.GetNthFiducialPosition(0, currTargetPosition ) #Assume first fid point is the entry point
currTargetPosition_RAS = [ currTargetPosition[ 0 ], currTargetPosition[ 1 ], currTargetPosition[ 2 ] ]
# Find the needle tip in RAS
needleTip_RAS = point[0:3]
self.R_deviation.append(needleTip_RAS[0] - currTargetPosition[0])
self.A_deviation.append(needleTip_RAS[1] - currTargetPosition[1])
self.S_deviation.append(needleTip_RAS[2] - currTargetPosition[2])
self.needleTipTargetDistance.append(math.sqrt( vtk.vtkMath.Distance2BetweenPoints( currTargetPosition_RAS, needleTip_RAS ) ))
self.timeStamps.append(time)
self.allErrors.append([self.timeStamps[-1], self.R_deviation[-1], self.A_deviation[-1], self.S_deviation[-1], self.needleTipTargetDistance[-1]])
with open(self.fname, "a+") as myfile:
myfile.write(self.GetMetric()+"\n")
def GetMetric( self ):
separator = ','
return separator.join(map(str,self.allErrors[-1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment