def noIK_6dof(cls): MAIN_PATH = pyCGM2.TEST_DATA_PATH + "CGM2\\cgm2.4\\fullBody\\" staticFilename = "PN01OP01S01STAT.c3d" gaitFilename = "PN01OP01S01STAT.c3d" markerDiameter = 14 mp = { 'Bodymass': 83.0, 'LeftLegLength': 874.0, 'RightLegLength': 876.0, 'LeftKneeWidth': 106.0, 'RightKneeWidth': 103.0, 'LeftAnkleWidth': 74.0, 'RightAnkleWidth': 72.0, 'LeftSoleDelta': 0, 'RightSoleDelta': 0, } # --- Calibration --- acqStatic = btkTools.smartReader(str(MAIN_PATH + staticFilename)) translators = files.getTranslators(MAIN_PATH, "CGM2_4.translators") acqStatic = btkTools.applyTranslators(acqStatic, translators) model = cgm2.CGM2_4() model.configure() model.addAnthropoInputParameters(mp) # ---- Calibration ---- scp = modelFilters.StaticCalibrationProcedure(model) modelFilters.ModelCalibrationFilter(scp, acqStatic, model).compute() print "----" print model.getSegment("Left Shank").getReferential( "TF").relativeMatrixAnatomic print "----" # # cgm decorator modelDecorator.HipJointCenterDecorator(model).hara() modelDecorator.KneeCalibrationDecorator(model).midCondyles( acqStatic, markerDiameter=markerDiameter, side="both") modelDecorator.AnkleCalibrationDecorator(model).midMaleolus( acqStatic, markerDiameter=markerDiameter, side="both") # # # final modelFilters.ModelCalibrationFilter( scp, acqStatic, model, markerDiameter=markerDiameter).compute() # ------ Fitting ------- acqGait = btkTools.smartReader(str(MAIN_PATH + gaitFilename)) acqGait = btkTools.applyTranslators(acqGait, translators) # Motion FILTER modMotion = modelFilters.ModelMotionFilter( scp, acqGait, model, enums.motionMethod.Sodervisk) modMotion.compute() btkTools.smartWriter(acqGait, "cgm24_noIK6dof_staticMotion.c3d")
def noIK_6dof_Garches(cls): MAIN_PATH = pyCGM2.TEST_DATA_PATH + "Datasets Tests\\didier\\08_02_18_Vincent Pere\\" staticFilename = "08_02_18_Vincent_Pere_Statique_000_MOKKA.c3d" gaitFilename = "08_02_18_Vincent_Pere_Statique_000_MOKKA.c3d" markerDiameter = 14 mp = { 'Bodymass': 70.0, 'LeftLegLength': 890.0, 'RightLegLength': 890.0, 'LeftKneeWidth': 150.0, 'RightKneeWidth': 150.0, 'LeftAnkleWidth': 88.0, 'RightAnkleWidth': 99.0, 'LeftSoleDelta': 0, 'RightSoleDelta': 0, } # --- Calibration --- acqStatic = btkTools.smartReader(str(MAIN_PATH + staticFilename)) model = cgm2.CGM2_4() model.configure() model.addAnthropoInputParameters(mp) # ---- Calibration ---- scp = modelFilters.StaticCalibrationProcedure(model) modelFilters.ModelCalibrationFilter(scp, acqStatic, model).compute() print "----" print model.getSegment("Left Shank").getReferential( "TF").relativeMatrixAnatomic print "----" # # cgm decorator modelDecorator.HipJointCenterDecorator(model).hara() modelDecorator.KneeCalibrationDecorator(model).midCondyles( acqStatic, markerDiameter=markerDiameter, side="both") modelDecorator.AnkleCalibrationDecorator(model).midMaleolus( acqStatic, markerDiameter=markerDiameter, side="both") # # # final modelFilters.ModelCalibrationFilter( scp, acqStatic, model, markerDiameter=markerDiameter).compute() # ------ Fitting ------- acqGait = btkTools.smartReader(str(MAIN_PATH + gaitFilename)) # Motion FILTER modMotion = modelFilters.ModelMotionFilter( scp, acqGait, model, enums.motionMethod.Sodervisk) modMotion.compute() btkTools.smartWriter(acqGait, "cgm24_noIK6dof_staticMotion_Garches.c3d")
def calibration_FlatFoot(cls): MAIN_PATH = pyCGM2.TEST_DATA_PATH + "CGM2\\cgm2.4\\medial\\" staticFilename = "static.c3d" markerDiameter = 14 mp = { 'Bodymass': 69.0, 'LeftLegLength': 930.0, 'RightLegLength': 930.0, 'LeftKneeWidth': 94.0, 'RightKneeWidth': 64.0, 'LeftAnkleWidth': 67.0, 'RightAnkleWidth': 62.0, 'LeftSoleDelta': 0, 'RightSoleDelta': 0, "LeftToeOffset": 0, "RightToeOffset": 0, } # --- Calibration --- acqStatic = btkTools.smartReader(str(MAIN_PATH + staticFilename)) model = cgm2.CGM2_4() model.configure() model.addAnthropoInputParameters(mp) # ---- Calibration ---- scp = modelFilters.StaticCalibrationProcedure(model) modelFilters.ModelCalibrationFilter(scp, acqStatic, model).compute() # cgm decorator modelDecorator.HipJointCenterDecorator(model).hara() modelDecorator.KneeCalibrationDecorator(model).midCondyles( acqStatic, markerDiameter=markerDiameter, side="both") modelDecorator.AnkleCalibrationDecorator(model).midMaleolus( acqStatic, markerDiameter=markerDiameter, side="both") # final modelFilters.ModelCalibrationFilter(scp, acqStatic, model, markerDiameter=markerDiameter, leftFlatFoot=True, rightFlatFoot=True).compute() # display CS csp = modelFilters.ModelCoordinateSystemProcedure(model) csf = modelFilters.CoordinateSystemDisplayFilter(csp, model, acqStatic) csf.setStatic(True) csf.display() btkTools.smartWriter(acqStatic, "cgm2.4_FlatFoot.c3d")
def calibration_GarchesFlatFoot(cls): MAIN_PATH = pyCGM2.TEST_DATA_PATH + "Datasets Tests\\didier\\08_02_18_Vincent Pere\\" staticFilename = "08_02_18_Vincent_Pere_Statique_000_MOKKA.c3d" markerDiameter = 14 mp = { 'Bodymass': 70.0, 'LeftLegLength': 890.0, 'RightLegLength': 890.0, 'LeftKneeWidth': 150.0, 'RightKneeWidth': 150.0, 'LeftAnkleWidth': 88.0, 'RightAnkleWidth': 99.0, 'LeftSoleDelta': 0, 'RightSoleDelta': 0, "LeftToeOffset": 0, "RightToeOffset": 0, } # --- Calibration --- acqStatic = btkTools.smartReader(str(MAIN_PATH + staticFilename)) model = cgm2.CGM2_4() model.configure() model.addAnthropoInputParameters(mp) # ---- Calibration ---- scp = modelFilters.StaticCalibrationProcedure(model) modelFilters.ModelCalibrationFilter(scp, acqStatic, model).compute() # cgm decorator modelDecorator.HipJointCenterDecorator(model).hara() modelDecorator.KneeCalibrationDecorator(model).midCondyles( acqStatic, markerDiameter=markerDiameter, side="both") modelDecorator.AnkleCalibrationDecorator(model).midMaleolus( acqStatic, markerDiameter=markerDiameter, side="both") # final modelFilters.ModelCalibrationFilter(scp, acqStatic, model, markerDiameter=markerDiameter, leftFlatFoot=True, rightFlatFoot=True).compute() # display CS csp = modelFilters.ModelCoordinateSystemProcedure(model) csf = modelFilters.CoordinateSystemDisplayFilter(csp, model, acqStatic) csf.setStatic(True) csf.display() btkTools.smartWriter(acqStatic, "cgm2.4_GarchesFlatFoot.c3d")
def CGM2_4_Calibration2Dof_test(cls): MAIN_PATH = pyCGM2.TEST_DATA_PATH + "CGM2\\cgm2.4\\Knee Calibration\\" staticFilename = "static.c3d" funcFilename = "functional.c3d" gaitFilename = "gait trial 01.c3d" settings = files.openJson(pyCGM2.PYCGM2_APPDATA_PATH, "CGM2_4-pyCGM2.settings") translators = settings["Translators"] markerDiameter = 14 mp = { 'Bodymass': 69.0, 'LeftLegLength': 930.0, 'RightLegLength': 930.0, 'LeftKneeWidth': 94.0, 'RightKneeWidth': 64.0, 'LeftAnkleWidth': 67.0, 'RightAnkleWidth': 62.0, 'LeftSoleDelta': 0, 'RightSoleDelta': 0, "LeftToeOffset": 0, "RightToeOffset": 0, } acqStatic = btkTools.smartReader(str(MAIN_PATH + staticFilename)) model = cgm2.CGM2_4() model.configure() model.addAnthropoInputParameters(mp) # --store calibration parameters-- model.setStaticFilename(staticFilename) model.setCalibrationProperty("leftFlatFoot", True) model.setCalibrationProperty("rightFlatFoot", True) model.setCalibrationProperty("markerDiameter", 14) # test - no joint Range model, acqFunc, side = kneeCalibration.calibration2Dof( model, MAIN_PATH, funcFilename, translators, "Left", 831, 1280, None) # test with joint range of [20-90] model, acqFunc, side = kneeCalibration.calibration2Dof( model, MAIN_PATH, funcFilename, translators, "Left", 831, 1280, [20, 90])
def CGM2_4_SARA_test(cls): MAIN_PATH = pyCGM2.TEST_DATA_PATH + "CGM2\\cgm2.4\\Knee Calibration\\" staticFilename = "static.c3d" funcFilename = "functional.c3d" gaitFilename = "gait trial 01.c3d" markerDiameter = 14 mp = { 'Bodymass': 69.0, 'LeftLegLength': 930.0, 'RightLegLength': 930.0, 'LeftKneeWidth': 94.0, 'RightKneeWidth': 64.0, 'LeftAnkleWidth': 67.0, 'RightAnkleWidth': 62.0, 'LeftSoleDelta': 0, 'RightSoleDelta': 0, "LeftToeOffset": 0, "RightToeOffset": 0, } acqStatic = btkTools.smartReader(str(MAIN_PATH + staticFilename)) model = cgm2.CGM2_4() model.configure() model.addAnthropoInputParameters(mp) # --- INITIAL CALIBRATION --- scp = modelFilters.StaticCalibrationProcedure(model) modelFilters.ModelCalibrationFilter(scp, acqStatic, model).compute() # cgm decorator modelDecorator.HipJointCenterDecorator(model).hara() modelDecorator.KneeCalibrationDecorator(model).midCondyles( acqStatic, markerDiameter=markerDiameter, side="both") modelDecorator.AnkleCalibrationDecorator(model).midMaleolus( acqStatic, markerDiameter=markerDiameter, side="both") # final modelFilters.ModelCalibrationFilter( scp, acqStatic, model, seLeftHJCnode="LHJC_Hara", useRightHJCnode="RHJC_Hara", useLeftKJCnode="LKJC_mid", useLeftAJCnode="LAJC_mid", useRightKJCnode="RKJC_mid", useRightAJCnode="RAJC_mid", markerDiameter=markerDiameter).compute() # ------ LEFT KNEE CALIBRATION ------- acqFunc = btkTools.smartReader(str(MAIN_PATH + funcFilename)) # Motion of only left modMotionLeftKnee = modelFilters.ModelMotionFilter( scp, acqFunc, model, enums.motionMethod.Sodervisk) modMotionLeftKnee.segmentalCompute(["Left Thigh", "Left Shank"]) # decorator modelDecorator.KneeCalibrationDecorator(model).sara( "Left", indexFirstFrame=831, indexLastFrame=1280) # ----add Point into the c3d---- Or_inThigh = model.getSegment("Left Thigh").getReferential( "TF").getNodeTrajectory("KneeFlexionOri") axis_inThigh = model.getSegment("Left Thigh").getReferential( "TF").getNodeTrajectory("KneeFlexionAxis") btkTools.smartAppendPoint(acqFunc, "Left" + "_KneeFlexionOri", Or_inThigh) btkTools.smartAppendPoint(acqFunc, "Left" + "_KneeFlexionAxis", axis_inThigh) # ------ RIGHT KNEE CALIBRATION ------- # Motion of only left modMotionRightKnee = modelFilters.ModelMotionFilter( scp, acqFunc, model, enums.motionMethod.Sodervisk) modMotionRightKnee.segmentalCompute(["Right Thigh", "Right Shank"]) # decorator modelDecorator.KneeCalibrationDecorator(model).sara("Right", indexFirstFrame=61, indexLastFrame=551) # ----add Point into the c3d---- Or_inThigh = model.getSegment("Right Thigh").getReferential( "TF").getNodeTrajectory("KneeFlexionOri") axis_inThigh = model.getSegment("Right Thigh").getReferential( "TF").getNodeTrajectory("KneeFlexionAxis") btkTools.smartAppendPoint(acqFunc, "Right" + "_KneeFlexionOri", Or_inThigh) btkTools.smartAppendPoint(acqFunc, "Right" + "_KneeFlexionAxis", axis_inThigh) btkTools.smartWriter(acqFunc, "acqFunc-Sara.c3d") #--- FINAL CALIBRATION --- modelFilters.ModelCalibrationFilter( scp, acqStatic, model, useLeftHJCnode="LHJC_Hara", useRightHJCnode="RHJC_Hara", useLeftKJCnode="KJC_Sara", useLeftAJCnode="LAJC_mid", useRightKJCnode="KJC_Sara", useRightAJCnode="RAJC_mid", markerDiameter=markerDiameter).compute() # save static c3d with update KJC btkTools.smartWriter(acqStatic, "Static-SARA.c3d") # print functional Offsets print model.mp_computed["LeftKneeFuncCalibrationOffset"] print model.mp_computed["RightKneeFuncCalibrationOffset"]
def full_IK(cls): MAIN_PATH = pyCGM2.TEST_DATA_PATH + "CGM2\\cgm2.4\\fullBody\\" staticFilename = "PN01OP01S01STAT.c3d" gaitFilename = "PN01OP01S01STAT.c3d" markerDiameter = 14 mp = { 'Bodymass': 83.0, 'LeftLegLength': 874.0, 'RightLegLength': 876.0, 'LeftKneeWidth': 106.0, 'RightKneeWidth': 103.0, 'LeftAnkleWidth': 74.0, 'RightAnkleWidth': 72.0, 'LeftSoleDelta': 0, 'RightSoleDelta': 0, } # --- Calibration --- acqStatic = btkTools.smartReader(str(MAIN_PATH + staticFilename)) translators = files.getTranslators(MAIN_PATH, "CGM2_4.translators") acqStatic = btkTools.applyTranslators(acqStatic, translators) model = cgm2.CGM2_4() model.configure() model.addAnthropoInputParameters(mp) # ---- Calibration ---- scp = modelFilters.StaticCalibrationProcedure(model) modelFilters.ModelCalibrationFilter(scp, acqStatic, model).compute() # cgm decorator modelDecorator.HipJointCenterDecorator(model).hara() modelDecorator.KneeCalibrationDecorator(model).midCondyles( acqStatic, markerDiameter=markerDiameter, side="both") modelDecorator.AnkleCalibrationDecorator(model).midMaleolus( acqStatic, markerDiameter=markerDiameter, side="both") # final modelFilters.ModelCalibrationFilter( scp, acqStatic, model, markerDiameter=markerDiameter).compute() # ------ Fitting ------- acqGait = btkTools.smartReader(str(MAIN_PATH + gaitFilename)) acqGait = btkTools.applyTranslators(acqGait, translators) # Motion FILTER modMotion = modelFilters.ModelMotionFilter( scp, acqGait, model, enums.motionMethod.Sodervisk) modMotion.compute() # ------- OPENSIM IK -------------------------------------- # --- osim builder --- cgmCalibrationprocedure = opensimFilters.CgmOpensimCalibrationProcedures( model) markersetFile = pyCGM2.OPENSIM_PREBUILD_MODEL_PATH + "models\\settings\\cgm2_4\\cgm2_4-markerset.xml" osimfile = pyCGM2.OPENSIM_PREBUILD_MODEL_PATH + "models\\osim\\lowerLimb_ballsJoints.osim" oscf = opensimFilters.opensimCalibrationFilter( osimfile, model, cgmCalibrationprocedure, MAIN_PATH) oscf.addMarkerSet(markersetFile) scalingOsim = oscf.build(exportOsim=False) # --- fitting --- #procedure cgmFittingProcedure = opensimFilters.CgmOpensimFittingProcedure(model) iksetupFile = pyCGM2.OPENSIM_PREBUILD_MODEL_PATH + "models\\settings\\cgm2_4\\cgm2_4-ikSetUp_template.xml" osrf = opensimFilters.opensimFittingFilter(iksetupFile, scalingOsim, cgmFittingProcedure, MAIN_PATH) acqIK = osrf.run(acqGait, str(MAIN_PATH + gaitFilename), exportSetUp=False) # -------- NEW MOTION FILTER ON IK MARKERS ------------------ modMotion_ik = modelFilters.ModelMotionFilter( scp, acqIK, model, enums.motionMethod.Sodervisk, useForMotionTest=True) modMotion_ik.compute() finalJcs = modelFilters.ModelJCSFilter(model, acqIK) finalJcs.setFilterBool(False) finalJcs.compute(description="ik", pointLabelSuffix="2_ik") # btkTools.smartWriter(acqIK, "cgm24_fullIK_staticMotion.c3d")
def calibrate(DATA_PATH,calibrateFilenameLabelled,translators,settings, required_mp,optional_mp, ik_flag,leftFlatFoot,rightFlatFoot,headFlat, markerDiameter,hjcMethod, pointSuffix,**kwargs): """ Calibration of the CGM2.4 :param DATA_PATH [str]: path to your data :param calibrateFilenameLabelled [str]: c3d file :param translators [dict]: translators to apply :param required_mp [dict]: required anthropometric data :param optional_mp [dict]: optional anthropometric data (ex: LThighOffset,...) :param ik_flag [bool]: enable the inverse kinematic solver :param leftFlatFoot [bool]: enable of the flat foot option for the left foot :param rightFlatFoot [bool]: enable of the flat foot option for the right foot :param headFlat [bool]: enable of the head flat option :param markerDiameter [double]: marker diameter (mm) :param hjcMethod [str or list of 3 float]: method for locating the hip joint centre :param pointSuffix [str]: suffix to add to model outputs """ # ---btk acquisition--- if "forceBtkAcq" in kwargs.keys(): acqStatic = kwargs["forceBtkAcq"] else: acqStatic = btkTools.smartReader(str(DATA_PATH+calibrateFilenameLabelled)) btkTools.checkMultipleSubject(acqStatic) acqStatic = btkTools.applyTranslators(acqStatic,translators) # ---check marker set used---- dcm = cgm.CGM.detectCalibrationMethods(acqStatic) # --------------------------MODEL-------------------------------------- # ---definition--- model=cgm2.CGM2_4() model.configure(acq=acqStatic,detectedCalibrationMethods=dcm) model.addAnthropoInputParameters(required_mp,optional=optional_mp) # --store calibration parameters-- model.setStaticFilename(calibrateFilenameLabelled) model.setCalibrationProperty("leftFlatFoot",leftFlatFoot) model.setCalibrationProperty("rightFlatFoot",rightFlatFoot) model.setCalibrationProperty("headFlat",headFlat) model.setCalibrationProperty("markerDiameter",markerDiameter) # --------------------------STATIC CALBRATION-------------------------- scp=modelFilters.StaticCalibrationProcedure(model) # load calibration procedure # ---initial calibration filter---- # use if all optional mp are zero modelFilters.ModelCalibrationFilter(scp,acqStatic,model, leftFlatFoot = leftFlatFoot, rightFlatFoot = rightFlatFoot, headFlat= headFlat, markerDiameter=markerDiameter, ).compute() # ---- Decorators ----- decorators.applyBasicDecorators(dcm, model,acqStatic,optional_mp,markerDiameter) decorators.applyHJCDecorators(model,hjcMethod) # ----Final Calibration filter if model previously decorated ----- if model.decoratedModel: # initial static filter modelFilters.ModelCalibrationFilter(scp,acqStatic,model, leftFlatFoot = leftFlatFoot, rightFlatFoot = rightFlatFoot, headFlat= headFlat, markerDiameter=markerDiameter).compute() # ----------------------CGM MODELLING---------------------------------- # ----motion filter---- modMotion=modelFilters.ModelMotionFilter(scp,acqStatic,model,enums.motionMethod.Sodervisk, markerDiameter=markerDiameter) modMotion.compute() if ik_flag: # ---OPENSIM IK--- # --- opensim calibration Filter --- osimfile = pyCGM2.OPENSIM_PREBUILD_MODEL_PATH + "models\\osim\\lowerLimb_ballsJoints.osim" # osimfile markersetFile = pyCGM2.OPENSIM_PREBUILD_MODEL_PATH + "models\\settings\\cgm2_4\\cgm2_4-markerset.xml" # markerset cgmCalibrationprocedure = opensimFilters.CgmOpensimCalibrationProcedures(model) # procedure oscf = opensimFilters.opensimCalibrationFilter(osimfile, model, cgmCalibrationprocedure, str(DATA_PATH)) oscf.addMarkerSet(markersetFile) scalingOsim = oscf.build() # --- opensim Fitting Filter --- iksetupFile = pyCGM2.OPENSIM_PREBUILD_MODEL_PATH + "models\\settings\\cgm2_4\\cgm2_4-ikSetUp_template.xml" # ik tool file cgmFittingProcedure = opensimFilters.CgmOpensimFittingProcedure(model) # procedure cgmFittingProcedure.updateMarkerWeight("LASI",settings["Fitting"]["Weight"]["LASI"]) cgmFittingProcedure.updateMarkerWeight("RASI",settings["Fitting"]["Weight"]["RASI"]) cgmFittingProcedure.updateMarkerWeight("LPSI",settings["Fitting"]["Weight"]["LPSI"]) cgmFittingProcedure.updateMarkerWeight("RPSI",settings["Fitting"]["Weight"]["RPSI"]) cgmFittingProcedure.updateMarkerWeight("RTHI",settings["Fitting"]["Weight"]["RTHI"]) cgmFittingProcedure.updateMarkerWeight("RKNE",settings["Fitting"]["Weight"]["RKNE"]) cgmFittingProcedure.updateMarkerWeight("RTIB",settings["Fitting"]["Weight"]["RTIB"]) cgmFittingProcedure.updateMarkerWeight("RANK",settings["Fitting"]["Weight"]["RANK"]) cgmFittingProcedure.updateMarkerWeight("RHEE",settings["Fitting"]["Weight"]["RHEE"]) cgmFittingProcedure.updateMarkerWeight("RTOE",settings["Fitting"]["Weight"]["RTOE"]) cgmFittingProcedure.updateMarkerWeight("LTHI",settings["Fitting"]["Weight"]["LTHI"]) cgmFittingProcedure.updateMarkerWeight("LKNE",settings["Fitting"]["Weight"]["LKNE"]) cgmFittingProcedure.updateMarkerWeight("LTIB",settings["Fitting"]["Weight"]["LTIB"]) cgmFittingProcedure.updateMarkerWeight("LANK",settings["Fitting"]["Weight"]["LANK"]) cgmFittingProcedure.updateMarkerWeight("LHEE",settings["Fitting"]["Weight"]["LHEE"]) cgmFittingProcedure.updateMarkerWeight("LTOE",settings["Fitting"]["Weight"]["LTOE"]) cgmFittingProcedure.updateMarkerWeight("LTHAP",settings["Fitting"]["Weight"]["LTHAP"]) cgmFittingProcedure.updateMarkerWeight("LTHAD",settings["Fitting"]["Weight"]["LTHAD"]) cgmFittingProcedure.updateMarkerWeight("LTIAP",settings["Fitting"]["Weight"]["LTIAP"]) cgmFittingProcedure.updateMarkerWeight("LTIAD",settings["Fitting"]["Weight"]["LTIAD"]) cgmFittingProcedure.updateMarkerWeight("RTHAP",settings["Fitting"]["Weight"]["RTHAP"]) cgmFittingProcedure.updateMarkerWeight("RTHAD",settings["Fitting"]["Weight"]["RTHAD"]) cgmFittingProcedure.updateMarkerWeight("RTIAP",settings["Fitting"]["Weight"]["RTIAP"]) cgmFittingProcedure.updateMarkerWeight("RTIAD",settings["Fitting"]["Weight"]["RTIAD"]) cgmFittingProcedure.updateMarkerWeight("LSMH",settings["Fitting"]["Weight"]["LSMH"]) cgmFittingProcedure.updateMarkerWeight("LFMH",settings["Fitting"]["Weight"]["LFMH"]) cgmFittingProcedure.updateMarkerWeight("LVMH",settings["Fitting"]["Weight"]["LVMH"]) cgmFittingProcedure.updateMarkerWeight("RSMH",settings["Fitting"]["Weight"]["RSMH"]) cgmFittingProcedure.updateMarkerWeight("RFMH",settings["Fitting"]["Weight"]["RFMH"]) cgmFittingProcedure.updateMarkerWeight("RVMH",settings["Fitting"]["Weight"]["RVMH"]) # cgmFittingProcedure.updateMarkerWeight("LTHL",settings["Fitting"]["Weight"]["LTHL"]) # cgmFittingProcedure.updateMarkerWeight("LTHLD",settings["Fitting"]["Weight"]["LTHLD"]) # cgmFittingProcedure.updateMarkerWeight("LPAT",settings["Fitting"]["Weight"]["LPAT"]) # cgmFittingProcedure.updateMarkerWeight("LTIBL",settings["Fitting"]["Weight"]["LTIBL"]) # cgmFittingProcedure.updateMarkerWeight("RTHL",settings["Fitting"]["Weight"]["RTHL"]) # cgmFittingProcedure.updateMarkerWeight("RTHLD",settings["Fitting"]["Weight"]["RTHLD"]) # cgmFittingProcedure.updateMarkerWeight("RPAT",settings["Fitting"]["Weight"]["RPAT"]) # cgmFittingProcedure.updateMarkerWeight("RTIBL",settings["Fitting"]["Weight"]["RTIBL"]) osrf = opensimFilters.opensimFittingFilter(iksetupFile, scalingOsim, cgmFittingProcedure, str(DATA_PATH) ) acqStaticIK = osrf.run(acqStatic,str(DATA_PATH + calibrateFilenameLabelled )) # eventual static acquisition to consider for joint kinematics finalAcqStatic = acqStaticIK if ik_flag else acqStatic # --- final pyCGM2 model motion Filter --- # use fitted markers modMotionFitted=modelFilters.ModelMotionFilter(scp,finalAcqStatic,model,enums.motionMethod.Sodervisk) modMotionFitted.compute() if "displayCoordinateSystem" in kwargs.keys() and kwargs["displayCoordinateSystem"]: csp = modelFilters.ModelCoordinateSystemProcedure(model) csdf = modelFilters.CoordinateSystemDisplayFilter(csp,model,finalAcqStatic) csdf.setStatic(False) csdf.display() #---- Joint kinematics---- # relative angles modelFilters.ModelJCSFilter(model,finalAcqStatic).compute(description="vectoriel", pointLabelSuffix=pointSuffix) # detection of traveling axis + absolute angle if model.m_bodypart != enums.BodyPart.UpperLimb: longitudinalAxis,forwardProgression,globalFrame = btkTools.findProgressionAxisFromPelvicMarkers(finalAcqStatic,["LASI","LPSI","RASI","RPSI"]) else: longitudinalAxis,forwardProgression,globalFrame = btkTools.findProgressionAxisFromLongAxis(finalAcqStatic,"C7","CLAV") if model.m_bodypart != enums.BodyPart.UpperLimb: modelFilters.ModelAbsoluteAnglesFilter(model,finalAcqStatic, segmentLabels=["Left Foot","Right Foot","Pelvis"], angleLabels=["LFootProgress", "RFootProgress","Pelvis"], eulerSequences=["TOR","TOR", "ROT"], globalFrameOrientation = globalFrame, forwardProgression = forwardProgression).compute(pointLabelSuffix=pointSuffix) if model.m_bodypart == enums.BodyPart.LowerLimbTrunk: modelFilters.ModelAbsoluteAnglesFilter(model,finalAcqStatic, segmentLabels=["Thorax"], angleLabels=["Thorax"], eulerSequences=["YXZ"], globalFrameOrientation = globalFrame, forwardProgression = forwardProgression).compute(pointLabelSuffix=pointSuffix) if model.m_bodypart == enums.BodyPart.UpperLimb or model.m_bodypart == enums.BodyPart.FullBody: modelFilters.ModelAbsoluteAnglesFilter(model,finalAcqStatic, segmentLabels=["Thorax","Head"], angleLabels=["Thorax", "Head"], eulerSequences=["YXZ","TOR"], globalFrameOrientation = globalFrame, forwardProgression = forwardProgression).compute(pointLabelSuffix=pointSuffix) # BSP model bspModel = bodySegmentParameters.Bsp(model) bspModel.compute() if model.m_bodypart == enums.BodyPart.FullBody: modelFilters.CentreOfMassFilter(model,finalAcqStatic).compute(pointLabelSuffix=pointSuffix) return model, finalAcqStatic
def calibrate(DATA_PATH,calibrateFilenameLabelled,translators,weights, required_mp,optional_mp, ik_flag,leftFlatFoot,rightFlatFoot,headFlat, markerDiameter,hjcMethod, pointSuffix,**kwargs): """ Calibration of the CGM2.4 :param DATA_PATH [str]: path to your data :param calibrateFilenameLabelled [str]: c3d file :param translators [dict]: translators to apply :param required_mp [dict]: required anthropometric data :param optional_mp [dict]: optional anthropometric data (ex: LThighOffset,...) :param ik_flag [bool]: enable the inverse kinematic solver :param leftFlatFoot [bool]: enable of the flat foot option for the left foot :param rightFlatFoot [bool]: enable of the flat foot option for the right foot :param headFlat [bool]: enable of the head flat option :param markerDiameter [double]: marker diameter (mm) :param hjcMethod [str or list of 3 float]: method for locating the hip joint centre :param pointSuffix [str]: suffix to add to model outputs """ detectAnomaly = False if "anomalyException" in kwargs.keys(): anomalyException = kwargs["anomalyException"] else: anomalyException=False # ---btk acquisition--- if "Fitting" in weights.keys(): weights = weights["Fitting"]["Weight"] if "forceBtkAcq" in kwargs.keys(): acqStatic = kwargs["forceBtkAcq"] else: acqStatic = btkTools.smartReader((DATA_PATH+calibrateFilenameLabelled)) btkTools.checkMultipleSubject(acqStatic) if btkTools.isPointExist(acqStatic,"SACR"): translators["LPSI"] = "SACR" translators["RPSI"] = "SACR" LOGGER.logger.info("[pyCGM2] Sacrum marker detected") acqStatic = btkTools.applyTranslators(acqStatic,translators) trackingMarkers = cgm2.CGM2_4.LOWERLIMB_TRACKING_MARKERS + cgm2.CGM2_4.THORAX_TRACKING_MARKERS+ cgm2.CGM2_4.UPPERLIMB_TRACKING_MARKERS actual_trackingMarkers,phatoms_trackingMarkers = btkTools.createPhantoms(acqStatic, trackingMarkers) vff = acqStatic.GetFirstFrame() vlf = acqStatic.GetLastFrame() # vff,vlf = btkTools.getFrameBoundaries(acqStatic,actual_trackingMarkers) flag = btkTools.getValidFrames(acqStatic,actual_trackingMarkers,frameBounds=[vff,vlf]) gapFlag = btkTools.checkGap(acqStatic,actual_trackingMarkers,frameBounds=[vff,vlf]) if gapFlag: raise Exception("[pyCGM2] Calibration aborted. Gap find during interval [%i-%i]. Crop your c3d " %(vff,vlf)) # --------------------ANOMALY------------------------------ # --Check MP adap = AnomalyDetectionProcedure.AnthropoDataAnomalyProcedure( required_mp) adf = AnomalyFilter.AnomalyDetectionFilter(None,None,adap) mp_anomaly = adf.run() if mp_anomaly["ErrorState"]: detectAnomaly = True # --marker presence markersets = [cgm2.CGM2_4.LOWERLIMB_TRACKING_MARKERS, cgm2.CGM2_4.THORAX_TRACKING_MARKERS, cgm2.CGM2_4.UPPERLIMB_TRACKING_MARKERS] for markerset in markersets: ipdp = InspectorProcedure.MarkerPresenceDetectionProcedure( markerset) idf = InspectorFilter.InspectorFilter(acqStatic,calibrateFilenameLabelled,ipdp) inspector = idf.run() # # --marker outliers if inspector["In"] !=[]: madp = AnomalyDetectionProcedure.MarkerAnomalyDetectionRollingProcedure(inspector["In"], plot=False, window=4,threshold = 3) adf = AnomalyFilter.AnomalyDetectionFilter(acqStatic,calibrateFilenameLabelled,madp) anomaly = adf.run() anomalyIndexes = anomaly["Output"] if anomaly["ErrorState"]: detectAnomaly = True if detectAnomaly and anomalyException: raise Exception ("Anomalies has been detected - Check Warning message of the log file") # --------------------MODELLING------------------------------ # ---check marker set used---- dcm = cgm.CGM.detectCalibrationMethods(acqStatic) # --------------------------MODEL-------------------------------------- # ---definition--- model=cgm2.CGM2_4() model.configure(detectedCalibrationMethods=dcm) model.addAnthropoInputParameters(required_mp,optional=optional_mp) if dcm["Left Knee"] == enums.JointCalibrationMethod.KAD: actual_trackingMarkers.append("LKNE") if dcm["Right Knee"] == enums.JointCalibrationMethod.KAD: actual_trackingMarkers.append("RKNE") model.setStaticTrackingMarkers(actual_trackingMarkers) # --store calibration parameters-- model.setStaticFilename(calibrateFilenameLabelled) model.setCalibrationProperty("leftFlatFoot",leftFlatFoot) model.setCalibrationProperty("rightFlatFoot",rightFlatFoot) model.setCalibrationProperty("headFlat",headFlat) model.setCalibrationProperty("markerDiameter",markerDiameter) # --------------------------STATIC CALBRATION-------------------------- scp=modelFilters.StaticCalibrationProcedure(model) # load calibration procedure # ---initial calibration filter---- # use if all optional mp are zero modelFilters.ModelCalibrationFilter(scp,acqStatic,model, leftFlatFoot = leftFlatFoot, rightFlatFoot = rightFlatFoot, headFlat= headFlat, markerDiameter=markerDiameter, ).compute() # ---- Decorators ----- decorators.applyBasicDecorators(dcm, model,acqStatic,optional_mp,markerDiameter) decorators.applyHJCDecorators(model,hjcMethod) # ----Final Calibration filter if model previously decorated ----- if model.decoratedModel: # initial static filter modelFilters.ModelCalibrationFilter(scp,acqStatic,model, leftFlatFoot = leftFlatFoot, rightFlatFoot = rightFlatFoot, headFlat= headFlat, markerDiameter=markerDiameter).compute() # ----------------------CGM MODELLING---------------------------------- # ----motion filter---- modMotion=modelFilters.ModelMotionFilter(scp,acqStatic,model,enums.motionMethod.Sodervisk, markerDiameter=markerDiameter) modMotion.compute() # ----progression Frame---- progressionFlag = False if btkTools.isPointsExist(acqStatic, ['LASI', 'RASI', 'RPSI', 'LPSI'],ignorePhantom=False): LOGGER.logger.info("[pyCGM2] - progression axis detected from Pelvic markers ") pfp = progressionFrame.PelvisProgressionFrameProcedure() pff = progressionFrame.ProgressionFrameFilter(acqStatic,pfp) pff.compute() progressionAxis = pff.outputs["progressionAxis"] globalFrame = pff.outputs["globalFrame"] forwardProgression = pff.outputs["forwardProgression"] progressionFlag = True elif btkTools.isPointsExist(acqStatic, ['C7', 'T10', 'CLAV', 'STRN'],ignorePhantom=False) and not progressionFlag: LOGGER.logger.info("[pyCGM2] - progression axis detected from Thoracic markers ") pfp = progressionFrame.ThoraxProgressionFrameProcedure() pff = progressionFrame.ProgressionFrameFilter(acqStatic,pfp) pff.compute() progressionAxis = pff.outputs["progressionAxis"] globalFrame = pff.outputs["globalFrame"] forwardProgression = pff.outputs["forwardProgression"] else: globalFrame = "XYZ" progressionAxis = "X" forwardProgression = True LOGGER.logger.error("[pyCGM2] - impossible to detect progression axis - neither pelvic nor thoracic markers are present. Progression set to +X by default ") # ----manage IK Targets---- ikTargets = list() for target in weights.keys(): if target not in actual_trackingMarkers: weights[target] = 0 LOGGER.logger.warning("[pyCGM2] - the IK targeted marker [%s] is not labelled in the acquisition [%s]"%(target,calibrateFilenameLabelled)) else: ikTargets.append(target) model.setStaticIkTargets(ikTargets) if "noKinematicsCalculation" in kwargs.keys() and kwargs["noKinematicsCalculation"]: LOGGER.logger.warning("[pyCGM2] No Kinematic calculation done for the static file") return model, acqStatic,detectAnomaly else: if ik_flag: # ---OPENSIM IK--- # --- opensim calibration Filter --- osimfile = pyCGM2.OPENSIM_PREBUILD_MODEL_PATH + "models\\osim\\lowerLimb_ballsJoints.osim" # osimfile markersetFile = pyCGM2.OPENSIM_PREBUILD_MODEL_PATH + "models\\settings\\cgm2_4\\cgm2_4-markerset.xml" # markerset cgmCalibrationprocedure = opensimFilters.CgmOpensimCalibrationProcedures(model) # procedure oscf = opensimFilters.opensimCalibrationFilter(osimfile, model, cgmCalibrationprocedure, DATA_PATH ) oscf.addMarkerSet(markersetFile) scalingOsim = oscf.build() # --- opensim Fitting Filter --- iksetupFile = pyCGM2.OPENSIM_PREBUILD_MODEL_PATH + "models\\settings\\cgm2_4\\cgm2_4-ikSetUp_template.xml" # ik tool file cgmFittingProcedure = opensimFilters.CgmOpensimFittingProcedure(model) # procedure cgmFittingProcedure.updateMarkerWeight("LASI",weights["LASI"]) cgmFittingProcedure.updateMarkerWeight("RASI",weights["RASI"]) cgmFittingProcedure.updateMarkerWeight("LPSI",weights["LPSI"]) cgmFittingProcedure.updateMarkerWeight("RPSI",weights["RPSI"]) cgmFittingProcedure.updateMarkerWeight("RTHI",weights["RTHI"]) cgmFittingProcedure.updateMarkerWeight("RKNE",weights["RKNE"]) cgmFittingProcedure.updateMarkerWeight("RTIB",weights["RTIB"]) cgmFittingProcedure.updateMarkerWeight("RANK",weights["RANK"]) cgmFittingProcedure.updateMarkerWeight("RHEE",weights["RHEE"]) cgmFittingProcedure.updateMarkerWeight("RTOE",weights["RTOE"]) cgmFittingProcedure.updateMarkerWeight("LTHI",weights["LTHI"]) cgmFittingProcedure.updateMarkerWeight("LKNE",weights["LKNE"]) cgmFittingProcedure.updateMarkerWeight("LTIB",weights["LTIB"]) cgmFittingProcedure.updateMarkerWeight("LANK",weights["LANK"]) cgmFittingProcedure.updateMarkerWeight("LHEE",weights["LHEE"]) cgmFittingProcedure.updateMarkerWeight("LTOE",weights["LTOE"]) cgmFittingProcedure.updateMarkerWeight("LTHAP",weights["LTHAP"]) cgmFittingProcedure.updateMarkerWeight("LTHAD",weights["LTHAD"]) cgmFittingProcedure.updateMarkerWeight("LTIAP",weights["LTIAP"]) cgmFittingProcedure.updateMarkerWeight("LTIAD",weights["LTIAD"]) cgmFittingProcedure.updateMarkerWeight("RTHAP",weights["RTHAP"]) cgmFittingProcedure.updateMarkerWeight("RTHAD",weights["RTHAD"]) cgmFittingProcedure.updateMarkerWeight("RTIAP",weights["RTIAP"]) cgmFittingProcedure.updateMarkerWeight("RTIAD",weights["RTIAD"]) cgmFittingProcedure.updateMarkerWeight("LSMH",weights["LSMH"]) cgmFittingProcedure.updateMarkerWeight("LFMH",weights["LFMH"]) cgmFittingProcedure.updateMarkerWeight("LVMH",weights["LVMH"]) cgmFittingProcedure.updateMarkerWeight("RSMH",weights["RSMH"]) cgmFittingProcedure.updateMarkerWeight("RFMH",weights["RFMH"]) cgmFittingProcedure.updateMarkerWeight("RVMH",weights["RVMH"]) # cgmFittingProcedure.updateMarkerWeight("LTHL",weights["LTHL"]) # cgmFittingProcedure.updateMarkerWeight("LTHLD",weights["LTHLD"]) # cgmFittingProcedure.updateMarkerWeight("LPAT",weights["LPAT"]) # cgmFittingProcedure.updateMarkerWeight("LTIBL",weights["LTIBL"]) # cgmFittingProcedure.updateMarkerWeight("RTHL",weights["RTHL"]) # cgmFittingProcedure.updateMarkerWeight("RTHLD",weights["RTHLD"]) # cgmFittingProcedure.updateMarkerWeight("RPAT",weights["RPAT"]) # cgmFittingProcedure.updateMarkerWeight("RTIBL",weights["RTIBL"]) osrf = opensimFilters.opensimFittingFilter(iksetupFile, scalingOsim, cgmFittingProcedure, DATA_PATH, acqStatic, accuracy = 1e-5) osrf.setTimeRange(acqStatic,beginFrame = vff, lastFrame=vlf) LOGGER.logger.info("-------INVERSE KINEMATICS IN PROGRESS----------") try: acqStaticIK = osrf.run(DATA_PATH + calibrateFilenameLabelled, progressionAxis = progressionAxis , forwardProgression = forwardProgression) LOGGER.logger.info("[pyCGM2] - IK solver complete") except: LOGGER.logger.error("[pyCGM2] - IK solver fails") acqStaticIK = acqStatic detectAnomaly = True LOGGER.logger.info("-----------------------------------------------") # eventual static acquisition to consider for joint kinematics finalAcqStatic = acqStaticIK if ik_flag else acqStatic # --- final pyCGM2 model motion Filter --- # use fitted markers modMotionFitted=modelFilters.ModelMotionFilter(scp,finalAcqStatic,model,enums.motionMethod.Sodervisk) modMotionFitted.compute() if "displayCoordinateSystem" in kwargs.keys() and kwargs["displayCoordinateSystem"]: csp = modelFilters.ModelCoordinateSystemProcedure(model) csdf = modelFilters.CoordinateSystemDisplayFilter(csp,model,finalAcqStatic) csdf.setStatic(False) csdf.display() #---- Joint kinematics---- # relative angles modelFilters.ModelJCSFilter(model,finalAcqStatic).compute(description="vectoriel", pointLabelSuffix=pointSuffix) modelFilters.ModelAbsoluteAnglesFilter(model,finalAcqStatic, segmentLabels=["Left Foot","Right Foot","Pelvis","Thorax","Head"], angleLabels=["LFootProgress", "RFootProgress","Pelvis","Thorax", "Head"], eulerSequences=["TOR","TOR", "ROT","YXZ","TOR"], globalFrameOrientation = globalFrame, forwardProgression = forwardProgression).compute(pointLabelSuffix=pointSuffix) bspModel = bodySegmentParameters.Bsp(model) bspModel.compute() modelFilters.CentreOfMassFilter(model,finalAcqStatic).compute(pointLabelSuffix=pointSuffix) btkTools.cleanAcq(finalAcqStatic) if detectAnomaly and not anomalyException: LOGGER.logger.error("Anomalies has been detected - Check Warning messages of the log file") return model, finalAcqStatic,detectAnomaly