Exemple #1
0
    def test_functions(self):
        filename = pyCGM2.TEST_DATA_PATH + "LowLevel\\IO\\Hånnibøl_c3d\\gait1.c3d"
        acq = btkTools.smartReader(filename, translators=None)

        btkTools.GetMarkerNames(acq)
        btkTools.findNearestMarker(acq, 0, "LASI")
        btkTools.GetAnalogNames(acq)
        btkTools.isGap(acq, "LASI")
        btkTools.findMarkerGap(acq)
        btkTools.isPointExist(acq, "LASI")
        btkTools.isPointsExist(acq, ["LASI", "RASI"])

        btkTools.clearPoints(acq, ["LASI", "RASI"])
        btkTools.checkFirstAndLastFrame(acq, "LASI")
        btkTools.isGap_inAcq(acq, ["LASI", "RASI"])
        btkTools.findValidFrames(acq, ["LASI", "RASI"])

        btkTools.checkMultipleSubject(acq)
        btkTools.checkMarkers(acq, ["LASI", "RASI"])
        btkTools.clearEvents(acq, ["Foot Strike"])
        btkTools.modifyEventSubject(acq, "Hän")
        btkTools.modifySubject(acq, "Han")

        btkTools.getVisibleMarkersAtFrame(acq, ["LASI", "RASI"], 0)
        btkTools.isAnalogExist(acq, "emg-Hän")
        btkTools.createZeros(acq, ["LASI", "RASI"])
        btkTools.constructEmptyMarker(acq, "zéros", desc="Hän")

        btkTools.getStartEndEvents(acq, "Left")

        btkTools.changeSubjectName(acq, "Hän")
        btkTools.smartGetMetadata(acq, "SUBJECTS", "USED")
        btkTools.smartSetMetadata(acq, "SUBJECTS", "USED", 0, "Hän")
Exemple #2
0
    def fill(self, acq):
        logging.info("----LowDimensionalKalmanFilter gap filling----")
        btkmarkersLoaded = btkTools.GetMarkerNames(acq)
        ff = acq.GetFirstFrame()
        lf = acq.GetLastFrame()
        pfn = acq.GetPointFrameNumber()

        btkmarkers = []
        for ml in btkmarkersLoaded:
            if btkTools.isPointExist(acq, ml):
                btkmarkers.append(ml)

        # --------

        logging.debug("Populating data matrix")
        rawDatabtk = np.zeros((pfn, len(btkmarkers) * 3))
        for i in range(0, len(btkmarkers)):
            values = acq.GetPoint(btkmarkers[i]).GetValues()
            residualValues = acq.GetPoint(btkmarkers[i]).GetResiduals()
            rawDatabtk[:, 3 * i - 3] = values[:, 0]
            rawDatabtk[:, 3 * i - 2] = values[:, 1]
            rawDatabtk[:, 3 * i - 1] = values[:, 2]
            E = residualValues[:, 0]
            rawDatabtk[np.asarray(E) == -1, 3 * i - 3] = np.nan
            rawDatabtk[np.asarray(E) == -1, 3 * i - 2] = np.nan
            rawDatabtk[np.asarray(E) == -1, 3 * i - 1] = np.nan

        Y2 = self._smooth(rawDatabtk, tol=1e-2, sigR=1e-3, keepOriginal=True)

        logging.debug("writing trajectories")

        # Create new smoothed trjectories
        filledMarkers = list()
        for i in range(0, len(btkmarkers)):
            targetMarker = btkmarkers[i]
            if btkTools.isGap(acq, targetMarker):
                logging.info("marker (%s) --> filled" % (targetMarker))
                filledMarkers.append(targetMarker)
                val_final = np.zeros((pfn, 3))
                val_final[:, 0] = Y2[:, 3 * i - 3]
                val_final[:, 1] = Y2[:, 3 * i - 2]
                val_final[:, 2] = Y2[:, 3 * i - 1]
                btkTools.smartAppendPoint(acq, targetMarker, val_final)
        logging.info(
            "----LowDimensionalKalmanFilter gap filling [complete]----")

        return acq, filledMarkers
Exemple #3
0
    def nexus_x2d(cls):

        NEXUS = ViconNexus.ViconNexus()
        NEXUS_PYTHON_CONNECTED = NEXUS.Client.IsConnected()

        DATA_PATH = "C:\\Users\\HLS501\\Documents\\VICON DATA\\pyCGM2-Data\\operations\\gapFilling\\gaitWithGaps_withX2d\\"
        filenameNoExt = "gait_GAP"
        NEXUS.OpenTrial(str(DATA_PATH + filenameNoExt), 30)

        acq_filled = btkTools.smartReader(
            str(DATA_PATH + "gait_GAP.-moGap.c3d"))

        subject = NEXUS.GetSubjectNames()[0]
        print "Gap filling for subject ", subject

        markersLoaded = NEXUS.GetMarkerNames(
            subject)  # nexus2.7 return all makers, even calibration only
        frames = NEXUS.GetFrameCount()

        markers = []
        for i in range(0, len(markersLoaded)):
            data = NEXUS.GetTrajectory(subject, markersLoaded[i])
            if data != ([], [], [], []):
                markers.append(markersLoaded[i])

        #---------
        acq = btkTools.smartReader(str(DATA_PATH + filenameNoExt + ".c3d"))
        btkmarkersLoaded = btkTools.GetMarkerNames(acq)
        ff = acq.GetFirstFrame()
        lf = acq.GetLastFrame()
        pfn = acq.GetPointFrameNumber()

        btkmarkers = []
        for ml in btkmarkersLoaded:
            if btkTools.isPointExist(acq, ml):
                btkmarkers.append(ml)
        #---------

        print "Populating data matrix"
        rawData = np.zeros((frames, len(markers) * 3))
        for i in range(0, len(markers)):
            print i
            rawData[:, 3 * i -
                    3], rawData[:, 3 * i -
                                2], rawData[:, 3 * i -
                                            1], E = NEXUS.GetTrajectory(
                                                subject, markers[i])
            rawData[np.asarray(E) == 0, 3 * i - 3] = np.nan
            rawData[np.asarray(E) == 0, 3 * i - 2] = np.nan
            rawData[np.asarray(E) == 0, 3 * i - 1] = np.nan

        Y = smooth(rawData, tol=1e-2, sigR=1e-3, keepOriginal=True)
        print "Writing new trajectories"
        # Create new smoothed trjectories
        for i in range(0, len(markers)):
            if markers[i] == "LTHAD":
                E = np.ones((len(E), 1)).tolist()
                val0 = Y[:, 3 * i - 3].tolist()
                val1 = Y[:, 3 * i - 2].tolist()
                val2 = Y[:, 3 * i - 1].tolist()
            #NEXUS.SetTrajectory(subject,markers[i],Y[:,3*i-3].tolist(),Y[:,3*i-2].tolist(),Y[:,3*i-1].tolist(),E)
        print "Done"

        # --------
        print "Populating data matrix"
        rawDatabtk = np.zeros((pfn, len(btkmarkers) * 3))
        for i in range(0, len(btkmarkers)):
            values = acq.GetPoint(btkmarkers[i]).GetValues()
            residualValues = acq.GetPoint(btkmarkers[i]).GetResiduals()
            rawDatabtk[:, 3 * i - 3] = values[:, 0]
            rawDatabtk[:, 3 * i - 2] = values[:, 1]
            rawDatabtk[:, 3 * i - 1] = values[:, 2]
            E = residualValues[:, 0]
            rawDatabtk[np.asarray(E) == -1, 3 * i - 3] = np.nan
            rawDatabtk[np.asarray(E) == -1, 3 * i - 2] = np.nan
            rawDatabtk[np.asarray(E) == -1, 3 * i - 1] = np.nan

        Y2 = smooth(rawDatabtk, tol=1e-2, sigR=1e-3, keepOriginal=True)
        print "Writing new trajectories"
        # Create new smoothed trjectories
        for i in range(0, len(btkmarkers)):
            targetMarker = btkmarkers[i]
            if btkTools.isGap(acq, targetMarker):
                val_final = np.zeros((pfn, 3))
                val_final[:, 0] = Y2[:, 3 * i - 3]
                val_final[:, 1] = Y2[:, 3 * i - 2]
                val_final[:, 2] = Y2[:, 3 * i - 1]
                btkTools.smartAppendPoint(acq, targetMarker, val_final)
                nexusTools.setTrajectoryFromAcq(NEXUS, subject, targetMarker,
                                                acq)
        print "Done"
Exemple #4
0
    def kinematicFitting_oneFile_cgmProcedure(cls):

        DATA_PATH = pyCGM2.TEST_DATA_PATH + "CGM2\\LowerLimb\\subject10_S1A1_julieDataSet_noModelOutputs\\"

        config = dict()
        config["static"] = "StaticR010S1A1.c3d"
        config["dynamicTrial"] = [
            "R010S1A1Gait001.c3d", "R010S1A1Gait003.c3d",
            "R010S1A1Gait004.c3d", "R010S1A1Gait005.c3d",
            "R010S1A1Gait007.c3d", "R010S1A1Gait010.c3d"
        ]

        configMP = {
            "Bodymass": 64.0,
            "LeftLegLength": 865.0,
            "RightLegLength": 855.0,
            "LeftKneeWidth": 100.0,
            "RightKneeWidth": 101.0,
            "LeftAnkleWidth": 69.0,
            "RightAnkleWidth": 69.0
        }

        requiredMarkers = [
            "LASI", "RASI", "LPSI", "RPSI", "RTHIAP", "RTHIAD", "RTHI", "RKNE",
            "RSHN", "RTIAP", "RTIB", "RANK", "RHEE", "RTOE", "RCUN", "RD1M",
            "RD5M"
        ]

        acqStatic = btkTools.smartReader(DATA_PATH + config["static"])

        # model
        model = cgm2Julie.CGM2ModelInf()
        model.configure()
        markerDiameter = 14

        mp = {
            'Bodymass': configMP["Bodymass"],
            'LeftLegLength': configMP["LeftLegLength"],
            'RightLegLength': configMP["RightLegLength"],
            'LeftKneeWidth': configMP["LeftKneeWidth"],
            'RightKneeWidth': configMP["RightKneeWidth"],
            'LeftAnkleWidth': configMP["LeftAnkleWidth"],
            'RightAnkleWidth': configMP["RightAnkleWidth"],
        }

        #offset 2ndToe Joint
        toe = acqStatic.GetPoint("RTOE").GetValues()[:, :].mean(axis=0)
        mp["rightToeOffset"] = (toe[2] - markerDiameter / 2.0) / 2.0

        model.addAnthropoInputParameters(mp)

        # -----------CGM STATIC CALIBRATION--------------------

        #  --- Initial calibration
        scp = modelFilters.StaticCalibrationProcedure(model)
        modelFilters.ModelCalibrationFilter(
            scp, acqStatic, model,
            rightFlatHindFoot=True).compute()  # Initial calibration

        # --- decorator
        modelDecorator.KneeCalibrationDecorator(model).midCondyles(
            acqStatic, leftMedialKneeLabel="LKNM", rightMedialKneeLabel="RKNM")
        modelDecorator.AnkleCalibrationDecorator(model).midMaleolus(
            acqStatic,
            leftMedialAnkleLabel="LMMA",
            rightMedialAnkleLabel="RMMA")

        # --- Updated calibration
        modelFilters.ModelCalibrationFilter(scp,
                                            acqStatic,
                                            model,
                                            useLeftKJCnode="LKJC_mid",
                                            useRightKJCnode="RKJC_mid",
                                            useLeftAJCnode="LAJC_mid",
                                            useRightAJCnode="RAJC_mid",
                                            rightFlatHindFoot=True).compute()

        # ---- optional -update static c3d
        #btkTools.smartWriter(acqStatic, "StaticCalibrated.c3d")

        # -----------CGM MOTION  6dof--------------------

        # --- reader and checking
        acqGait = btkTools.smartReader(DATA_PATH + config["dynamicTrial"][0])
        btkTools.checkMarkers(acqGait, requiredMarkers)
        btkTools.isGap(acqGait, requiredMarkers)
        btkTools.checkFirstAndLastFrame(acqGait, "LASI")
        logging.info("dyn Acquisition ---> OK ")

        # --- filters
        modMotion = modelFilters.ModelMotionFilter(
            scp, acqGait, model, pyCGM2Enums.motionMethod.Determinist)
        modMotion.compute()

        modelFilters.ModelJCSFilter(model,
                                    acqGait).compute(description="vectoriel",
                                                     pointLabelSuffix="2_6dof")

        # --- writer
        #btkTools.smartWriter(acqGait, "motionCalibrated.c3d")

        # ------- OPENSIM IK --------------------------------------
        cgmprocedure = opensimFilters.CgmOpensimCalibrationProcedures(model)
        markersetFile = pyCGM2.OPENSIM_PREBUILD_MODEL_PATH + "models\\draft-opensimPreProcessing\\cgm2-markerset.xml"

        osimfile = pyCGM2.OPENSIM_PREBUILD_MODEL_PATH + "models\\draft-opensimPreProcessing\\cgm2-model.osim"

        oscf = opensimFilters.opensimCalibrationFilter(osimfile, model,
                                                       cgmprocedure)
        oscf.addMarkerSet(markersetFile)
        fittingOsim = oscf.build()

        filename = config["dynamicTrial"][0]
        cgmFittingProcedure = opensimFilters.CgmOpensimFittingProcedure(model)

        cgmFittingProcedure.updateMarkerWeight("LASI", 100)

        iksetupFile = pyCGM2.OPENSIM_PREBUILD_MODEL_PATH + "models\\draft-opensimPreProcessing\\ikSetUp_template.xml"

        osrf = opensimFilters.opensimFittingFilter(iksetupFile, fittingOsim,
                                                   cgmFittingProcedure,
                                                   DATA_PATH)
        acqIK = osrf.run(acqGait, str(DATA_PATH + filename))

        # -------- NEW MOTION FILTER ON IK MARKERS ------------------

        modMotion_ik = modelFilters.ModelMotionFilter(
            scp, acqIK, model, pyCGM2Enums.motionMethod.Sodervisk)
        modMotion_ik.compute()

        finalJcs = modelFilters.ModelJCSFilter(model, acqIK)
        finalJcs.setFilterBool(False)
        finalJcs.compute(description="ik", pointLabelSuffix="2_ik")  #

        # recup du mot file
        motFilename = str(DATA_PATH + config["dynamicTrial"][0][:-4] + ".mot")

        comparisonOpensimVsCGM(motFilename, acqIK, "2_ik")