Esempio n. 1
0
def main(sessionFilename,
         createPDFReport=True,
         checkEventsInMokka=True,
         anomalyException=False):

    detectAnomaly = False
    LOGGER.set_file_handler("pyCGM2-QTM-Workflow.log")

    LOGGER.logger.info("------------QTM - pyCGM2 Workflow---------------")

    sessionXML = files.readXml(os.getcwd() + "\\", sessionFilename)
    sessionDate = files.getFileCreationDate(os.getcwd() + "\\" +
                                            sessionFilename)

    #---------------------------------------------------------------------------
    #management of the Processed folder
    DATA_PATH = os.getcwd() + "\\" + "processed\\"
    files.createDir(DATA_PATH)

    staticMeasurement = qtmTools.findStatic(sessionXML)
    calibrateFilenameLabelled = qtmTools.getFilename(staticMeasurement)
    if not os.path.isfile(DATA_PATH + calibrateFilenameLabelled):
        shutil.copyfile(os.getcwd() + "\\" + calibrateFilenameLabelled,
                        DATA_PATH + calibrateFilenameLabelled)
        LOGGER.logger.info(
            "qualisys exported c3d file [%s] copied to processed folder" %
            (calibrateFilenameLabelled))

    if qtmTools.findKneeCalibration(
            sessionXML, "Left") is not None or qtmTools.findKneeCalibration(
                sessionXML, "Right") is not None:
        LOGGER.logger.info(
            " the %s not accept functional knee calibration !!" % (MODEL))

    dynamicMeasurements = qtmTools.findDynamic(sessionXML)
    for dynamicMeasurement in dynamicMeasurements:
        reconstructFilenameLabelled = qtmTools.getFilename(dynamicMeasurement)

        # marker
        order_marker = int(
            float(dynamicMeasurement.Marker_lowpass_filter_order.text))
        fc_marker = float(
            dynamicMeasurement.Marker_lowpass_filter_frequency.text)

        if not os.path.isfile(DATA_PATH + reconstructFilenameLabelled):
            shutil.copyfile(os.getcwd() + "\\" + reconstructFilenameLabelled,
                            DATA_PATH + reconstructFilenameLabelled)
            LOGGER.logger.info(
                "qualisys exported c3d file [%s] copied to processed folder" %
                (reconstructFilenameLabelled))

            acq = btkTools.smartReader(
                str(DATA_PATH + reconstructFilenameLabelled))

            acq, zeniState = eventDetector.zeni(
                acq,
                fc_lowPass_marker=fc_marker,
                order_lowPass_marker=order_marker)

            if zeniState:
                btkTools.smartWriter(
                    acq, str(DATA_PATH + reconstructFilenameLabelled))
                if checkEventsInMokka:
                    cmd = "Mokka.exe \"%s\"" % (
                        str(DATA_PATH + reconstructFilenameLabelled))
                    os.system(cmd)

    # --------------------------GLOBAL SETTINGS ------------------------------------
    # global setting ( in user/AppData)

    if os.path.isfile(pyCGM2.PYCGM2_APPDATA_PATH + "CGM2_5-pyCGM2.settings"):
        settings = files.openFile(pyCGM2.PYCGM2_APPDATA_PATH,
                                  "CGM2_5-pyCGM2.settings")
    else:
        settings = files.openFile(pyCGM2.PYCGM2_SETTINGS_FOLDER,
                                  "CGM2_5-pyCGM2.settings")
    # --------------------------MP ------------------------------------
    required_mp, optional_mp = qtmTools.SubjectMp(sessionXML)

    #  translators management
    translators = files.getTranslators(os.getcwd() + "\\",
                                       "CGM2_5.translators")
    if not translators: translators = settings["Translators"]

    #  ikweight
    ikWeight = files.getIKweightSet(DATA_PATH, "CGM2_5.ikw")
    if not ikWeight: ikWeight = settings["Fitting"]["Weight"]

    # --------------------------MODEL CALIBRATION -----------------------
    LOGGER.logger.info(
        "--------------------------MODEL CALIBRATION -----------------------")
    staticMeasurement = qtmTools.findStatic(sessionXML)
    calibrateFilenameLabelled = qtmTools.getFilename(staticMeasurement)

    LOGGER.logger.info("----- CALIBRATION-  static file [%s]--" %
                       (calibrateFilenameLabelled))

    leftFlatFoot = utils.toBool(
        sessionXML.Left_foot_normalised_to_static_trial.text)
    rightFlatFoot = utils.toBool(
        sessionXML.Right_foot_normalised_to_static_trial.text)
    headFlat = utils.toBool(sessionXML.Head_normalised_to_static_trial.text)
    markerDiameter = float(sessionXML.Marker_diameter.text) * 1000.0
    hjcMethod = settings["Calibration"]["HJC"]
    pointSuffix = None

    # Calibration checking
    # --------------------
    acqStatic = btkTools.smartReader(DATA_PATH + calibrateFilenameLabelled)

    # Calibration operation
    # --------------------
    model, acqStatic, detectAnomaly = cgm2_5.calibrate(
        DATA_PATH,
        calibrateFilenameLabelled,
        translators,
        settings,
        required_mp,
        optional_mp,
        False,
        leftFlatFoot,
        rightFlatFoot,
        headFlat,
        markerDiameter,
        hjcMethod,
        pointSuffix,
        anomalyException=anomalyException)

    LOGGER.logger.info("----- CALIBRATION-  static file [%s]-----> DONE" %
                       (calibrateFilenameLabelled))

    # --------------------------MODEL FITTING ----------------------------------
    LOGGER.logger.info(
        "--------------------------MODEL FITTING ----------------------------------"
    )
    dynamicMeasurements = qtmTools.findDynamic(sessionXML)

    ik_flag = True

    modelledC3ds = list()
    eventInspectorStates = list()
    for dynamicMeasurement in dynamicMeasurements:

        reconstructFilenameLabelled = qtmTools.getFilename(dynamicMeasurement)

        LOGGER.logger.info("----Processing of [%s]-----" %
                           (reconstructFilenameLabelled))
        mfpa = qtmTools.getForcePlateAssigment(dynamicMeasurement)
        momentProjection_text = sessionXML.Moment_Projection.text
        if momentProjection_text == "Default":
            momentProjection_text = settings["Fitting"]["Moment Projection"]
        if momentProjection_text == "Distal":
            momentProjection = enums.MomentProjection.Distal
        elif momentProjection_text == "Proximal":
            momentProjection = enums.MomentProjection.Proximal
        elif momentProjection_text == "Global":
            momentProjection = enums.MomentProjection.Global
        elif momentProjection_text == "JCS":
            momentProjection = enums.MomentProjection.JCS

        acq = btkTools.smartReader(DATA_PATH + reconstructFilenameLabelled)

        # filtering
        # -----------------------

        # marker
        order_marker = int(
            float(dynamicMeasurement.Marker_lowpass_filter_order.text))
        fc_marker = float(
            dynamicMeasurement.Marker_lowpass_filter_frequency.text)

        # force plate
        order_fp = int(
            float(dynamicMeasurement.Forceplate_lowpass_filter_order.text))
        fc_fp = float(
            dynamicMeasurement.Forceplate_lowpass_filter_frequency.text)

        # ik accuracy
        ikAccuracy = float(dynamicMeasurement.IkAccuracy.text)

        if dynamicMeasurement.First_frame_to_process.text != "":
            vff = int(dynamicMeasurement.First_frame_to_process.text)
        else:
            vff = None

        if dynamicMeasurement.Last_frame_to_process.text != "":
            vlf = int(dynamicMeasurement.Last_frame_to_process.text)
        else:
            vlf = None

        # fitting operation
        # -----------------------
        LOGGER.logger.info("[pyCGM2] --- Fitting operation ---")
        acqGait, detectAnomaly = cgm2_5.fitting(
            model,
            DATA_PATH,
            reconstructFilenameLabelled,
            translators,
            settings,
            ik_flag,
            markerDiameter,
            pointSuffix,
            mfpa,
            momentProjection,
            fc_lowPass_marker=fc_marker,
            order_lowPass_marker=order_marker,
            fc_lowPass_forcePlate=fc_fp,
            order_lowPass_forcePlate=order_fp,
            anomalyException=anomalyException,
            ikAccuracy=ikAccuracy,
            frameInit=vff,
            frameEnd=vlf)

        outFilename = reconstructFilenameLabelled
        btkTools.smartWriter(acqGait, str(DATA_PATH + outFilename))
        modelledC3ds.append(outFilename)

        LOGGER.logger.info("----Processing of [%s]-----> DONE" %
                           (reconstructFilenameLabelled))

    LOGGER.logger.info(
        "---------------------GAIT PROCESSING -----------------------")
    if createPDFReport:
        nds = normativeDatasets.NormativeData("Schwartz2008", "Free")
        types = qtmTools.detectMeasurementType(sessionXML)
        for type in types:
            modelledTrials = list()
            for dynamicMeasurement in dynamicMeasurements:
                if qtmTools.isType(dynamicMeasurement, type):
                    filename = qtmTools.getFilename(dynamicMeasurement)
                    # event checking
                    # -----------------------
                    acq = btkTools.smartReader(DATA_PATH + filename)
                    geap = AnomalyDetectionProcedure.GaitEventAnomalyProcedure(
                    )
                    adf = AnomalyFilter.AnomalyDetectionFilter(
                        acq, filename, geap)
                    anomaly_events = adf.run()
                    if anomaly_events["ErrorState"]:
                        detectAnomaly = True
                        LOGGER.logger.warning(
                            "file [%s] not used for generating the gait report. bad gait event detected"
                            % (filename))
                    else:
                        modelledTrials.append(filename)
            try:
                report.pdfGaitReport(DATA_PATH,
                                     model,
                                     modelledTrials,
                                     nds,
                                     pointSuffix,
                                     title=type)
                LOGGER.logger.error("Generation of Gait report complete")
            except:
                LOGGER.logger.error("Generation of Gait report failed")

    LOGGER.logger.info(
        "-------------------------------------------------------")
    if detectAnomaly:
        LOGGER.logger.error(
            "Anomalies has been detected - Find Error messages, then check warning message in the log file"
        )
    else:
        LOGGER.logger.info("workflow return with no detected anomalies")
Esempio n. 2
0
def main(args):

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

    if NEXUS_PYTHON_CONNECTED:  # run Operation

        # --------------------GLOBAL SETTINGS ------------------------------

        # ( in user/AppData)
        if os.path.isfile(pyCGM2.PYCGM2_APPDATA_PATH +
                          "CGM2_5-pyCGM2.settings"):
            settings = files.openFile(pyCGM2.PYCGM2_APPDATA_PATH,
                                      "CGM2_5-pyCGM2.settings")
        else:
            settings = files.openFile(pyCGM2.PYCGM2_SETTINGS_FOLDER,
                                      "CGM2_5-pyCGM2.settings")

        argsManager = CgmArgsManager.argsManager_cgm(settings, args)
        leftFlatFoot = argsManager.getLeftFlatFoot()
        rightFlatFoot = argsManager.getRightFlatFoot()
        headFlat = argsManager.getHeadFlat()
        markerDiameter = argsManager.getMarkerDiameter()
        pointSuffix = argsManager.getPointSuffix("cgm2.5")
        ik_flag = argsManager.enableIKflag()

        hjcMethod = settings["Calibration"]["HJC"]
        lhjc = argsManager.forceHjc("left")
        rhjc = argsManager.forceHjc("right")
        if lhjc is not None:
            hjcMethod["Left"] = lhjc
        if rhjc is not None:
            hjcMethod["Right"] = rhjc

        # --------------------------LOADING------------------------------
        DATA_PATH, calibrateFilenameLabelledNoExt = NEXUS.GetTrialName()

        calibrateFilenameLabelled = calibrateFilenameLabelledNoExt + ".c3d"

        logging.info("data Path: " + DATA_PATH)
        logging.info("calibration file: " + calibrateFilenameLabelled)

        # --------------------------SUBJECT -----------------------------------
        # Notice : Work with ONE subject by session
        subjects = NEXUS.GetSubjectNames()
        subject = nexusTools.checkActivatedSubject(NEXUS, subjects)
        Parameters = NEXUS.GetSubjectParamNames(subject)

        required_mp, optional_mp = nexusUtils.getNexusSubjectMp(
            NEXUS, subject, resetFlag=args.resetMP)

        # --------------------------SESSION INFOS -----------------------------
        # --------------------------SESSIONS INFOS -----------------------------------
        mpInfo, mpFilename = files.getMpFileContent(DATA_PATH, "mp.pyCGM2",
                                                    subject)

        #  translators management
        translators = files.getTranslators(DATA_PATH, "CGM2_5.translators")
        if not translators: translators = settings["Translators"]

        # btkAcq builder
        nacf = nexusFilters.NexusConstructAcquisitionFilter(
            DATA_PATH, calibrateFilenameLabelledNoExt, subject)
        acq = nacf.build()

        # --------------------------CONFIG ------------------------------------
        model, finalAcqStatic = cgm2_5.calibrate(DATA_PATH,
                                                 calibrateFilenameLabelled,
                                                 translators,
                                                 settings,
                                                 required_mp,
                                                 optional_mp,
                                                 ik_flag,
                                                 leftFlatFoot,
                                                 rightFlatFoot,
                                                 headFlat,
                                                 markerDiameter,
                                                 hjcMethod,
                                                 pointSuffix,
                                                 forceBtkAcq=acq)

        # ----------------------SAVE-------------------------------------------
        files.saveModel(model, DATA_PATH, subject)

        # save mp
        files.saveMp(mpInfo, model, DATA_PATH, mpFilename)

        # ----------------------DISPLAY ON VICON-------------------------------
        nexusUtils.updateNexusSubjectMp(NEXUS, model, subject)
        nexusFilters.NexusModelFilter(NEXUS,
                                      model,
                                      finalAcqStatic,
                                      subject,
                                      pointSuffix,
                                      staticProcessing=True).run()

        # ========END of the nexus OPERATION if run from Nexus  =========

    else:
        raise Exception("NO Nexus connection. Turn on Nexus")
Esempio n. 3
0
def main(args):
    DATA_PATH = os.getcwd() + "\\"

    # User Settings
    if os.path.isfile(DATA_PATH + args.userFile):
        userSettings = files.openFile(DATA_PATH, args.userFile)
    else:
        raise Exception("user setting file not found")

    # internal (expert) Settings
    if args.expertFile:
        if os.path.isfile(DATA_PATH + args.expertFile):
            internalSettings = files.openFile(DATA_PATH, args.expertFile)
        else:
            raise Exception("expert setting file not found")
    else:
        internalSettings = None

    # translators
    if os.path.isfile(DATA_PATH + "CGM2_5.translators"):
        translators = files.openFile(DATA_PATH, "CGM2_5.translators")
    else:
        translators = None

    # localIkWeight
    if os.path.isfile(DATA_PATH + "CGM2_5.ikw"):
        localIkWeight = files.openFile(DATA_PATH, "CGM2_5.ikw")
    else:
        localIkWeight = None

    if args.vskFile:
        vsk = vskTools.Vsk(str(DATA_PATH + args.vskFile))
    else:
        vsk = None

    # --- Manager ----
    manager = ModelManager.CGM2_5ConfigManager(
        userSettings,
        localInternalSettings=internalSettings,
        localTranslators=translators,
        localIkWeight=localIkWeight,
        vsk=vsk)
    manager.contruct()
    finalSettings = manager.getFinalSettings()
    files.prettyDictPrint(finalSettings)

    logging.info("=============Calibration=============")
    model, finalAcqStatic = cgm2_5.calibrate(DATA_PATH,
                                             manager.staticTrial,
                                             manager.translators,
                                             finalSettings,
                                             manager.requiredMp,
                                             manager.optionalMp,
                                             manager.enableIK,
                                             manager.leftFlatFoot,
                                             manager.rightFlatFoot,
                                             manager.headFlat,
                                             manager.markerDiameter,
                                             manager.hjcMethod,
                                             manager.pointSuffix,
                                             displayCoordinateSystem=True)

    btkTools.smartWriter(
        finalAcqStatic,
        str(DATA_PATH + finalSettings["Calibration"]["StaticTrial"][:-4] +
            "-pyCGM2modelled.c3d"))
    logging.info("Static Calibration -----> Done")

    manager.updateMp(model)
    #files.prettyDictPrint(manager.finalSettings)

    logging.info("=============Fitting=============")
    for trial in manager.dynamicTrials:
        mfpa = None if trial["Mfpa"] == "Auto" else trial["Mfpa"]
        reconstructFilenameLabelled = trial["File"]

        acqGait = cgm2_5.fitting(model,
                                 DATA_PATH,
                                 reconstructFilenameLabelled,
                                 manager.translators,
                                 finalSettings,
                                 manager.enableIK,
                                 manager.markerDiameter,
                                 manager.pointSuffix,
                                 mfpa,
                                 manager.momentProjection,
                                 displayCoordinateSystem=True)

        btkTools.smartWriter(
            acqGait,
            str(DATA_PATH + reconstructFilenameLabelled[:-4] +
                "-pyCGM2modelled.c3d"))
        logging.info("---->dynamic trial (%s) processed" %
                     (reconstructFilenameLabelled))

    logging.info("=============Writing of final Settings=============")
    i = 0
    while os.path.exists("CGM2.5 [%s].completeSettings" % i):
        i += 1
    filename = "CGM2.5 [" + str(i) + "].completeSettings"
    files.saveJson(DATA_PATH, filename, finalSettings)
    logging.info("---->complete settings (%s) exported" % (filename))

    raw_input("Press return to exit..")
Esempio n. 4
0
def main():

    parser = argparse.ArgumentParser(description='CGM2.5 Calibration')
    parser.add_argument('-l',
                        '--leftFlatFoot',
                        type=int,
                        help='left flat foot option')
    parser.add_argument('-r',
                        '--rightFlatFoot',
                        type=int,
                        help='right flat foot option')
    parser.add_argument('-hf', '--headFlat', type=int, help='head flat option')
    parser.add_argument('-md',
                        '--markerDiameter',
                        type=float,
                        help='marker diameter')
    parser.add_argument('-ps',
                        '--pointSuffix',
                        type=str,
                        help='suffix of model outputs')
    parser.add_argument('--check',
                        action='store_true',
                        help='force model output suffix')
    parser.add_argument('--noIk',
                        action='store_true',
                        help='cancel inverse kinematic')
    parser.add_argument('--resetMP',
                        action='store_true',
                        help='reset optional mass parameters')
    parser.add_argument('--forceLHJC', nargs='+')
    parser.add_argument('--forceRHJC', nargs='+')
    parser.add_argument('-ae',
                        '--anomalyException',
                        action='store_true',
                        help='stop if anomaly detected ')
    args = parser.parse_args()

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

    if NEXUS_PYTHON_CONNECTED:  # run Operation

        # --------------------GLOBAL SETTINGS ------------------------------

        # ( in user/AppData)
        if os.path.isfile(pyCGM2.PYCGM2_APPDATA_PATH +
                          "CGM2_5-pyCGM2.settings"):
            settings = files.openFile(pyCGM2.PYCGM2_APPDATA_PATH,
                                      "CGM2_5-pyCGM2.settings")
        else:
            settings = files.openFile(pyCGM2.PYCGM2_SETTINGS_FOLDER,
                                      "CGM2_5-pyCGM2.settings")

        argsManager = CgmArgsManager.argsManager_cgm(settings, args)
        leftFlatFoot = argsManager.getLeftFlatFoot()
        rightFlatFoot = argsManager.getRightFlatFoot()
        headFlat = argsManager.getHeadFlat()
        markerDiameter = argsManager.getMarkerDiameter()
        pointSuffix = argsManager.getPointSuffix("cgm2.5")
        ik_flag = argsManager.enableIKflag()

        hjcMethod = settings["Calibration"]["HJC"]
        lhjc = argsManager.forceHjc("left")
        rhjc = argsManager.forceHjc("right")
        if lhjc is not None:
            hjcMethod["Left"] = lhjc
        if rhjc is not None:
            hjcMethod["Right"] = rhjc

        # --------------------------LOADING------------------------------
        DATA_PATH, calibrateFilenameLabelledNoExt = NEXUS.GetTrialName()

        calibrateFilenameLabelled = calibrateFilenameLabelledNoExt + ".c3d"

        LOGGER.logger.info("data Path: " + DATA_PATH)
        LOGGER.set_file_handler(DATA_PATH + "pyCGM2-Calibration.log")
        LOGGER.logger.info("calibration file: " + calibrateFilenameLabelled)

        # --------------------------SUBJECT -----------------------------------
        # Notice : Work with ONE subject by session
        subjects = NEXUS.GetSubjectNames()
        subject = nexusTools.getActiveSubject(NEXUS)
        Parameters = NEXUS.GetSubjectParamNames(subject)

        required_mp, optional_mp = nexusUtils.getNexusSubjectMp(
            NEXUS, subject, resetFlag=args.resetMP)

        # --------------------------SESSION INFOS -----------------------------
        # --------------------------SESSIONS INFOS -----------------------------------
        mpInfo, mpFilename = files.getMpFileContent(DATA_PATH, "mp.pyCGM2",
                                                    subject)

        #  translators management
        translators = files.getTranslators(DATA_PATH, "CGM2_5.translators")
        if not translators: translators = settings["Translators"]

        # btkAcq builder
        nacf = nexusFilters.NexusConstructAcquisitionFilter(
            DATA_PATH, calibrateFilenameLabelledNoExt, subject)
        acq = nacf.build()

        # --------------------------CONFIG ------------------------------------
        model, finalAcqStatic, detectAnomaly = cgm2_5.calibrate(
            DATA_PATH,
            calibrateFilenameLabelled,
            translators,
            settings,
            required_mp,
            optional_mp,
            ik_flag,
            leftFlatFoot,
            rightFlatFoot,
            headFlat,
            markerDiameter,
            hjcMethod,
            pointSuffix,
            forceBtkAcq=acq,
            anomalyException=args.anomalyException)

        # ----------------------SAVE-------------------------------------------
        files.saveModel(model, DATA_PATH, subject)

        # save mp
        files.saveMp(mpInfo, model, DATA_PATH, mpFilename)

        # ----------------------DISPLAY ON VICON-------------------------------
        nexusUtils.updateNexusSubjectMp(NEXUS, model, subject)
        nexusFilters.NexusModelFilter(NEXUS,
                                      model,
                                      finalAcqStatic,
                                      subject,
                                      pointSuffix,
                                      staticProcessing=True).run()

        # ========END of the nexus OPERATION if run from Nexus  =========

    else:
        raise Exception("NO Nexus connection. Turn on Nexus")