Exemple #1
0
def main():
    qtm_folder = os.getcwd()
    sessionXML = files.readXml(qtm_folder + "\\", "session.xml")

    # --------------------------MP ------------------------------------
    required_mp, _ = qtmTools.SubjectMp(sessionXML)

    # --------------------------Check MP ----------------------------------
    logging.info("--MP Checking--")
    inspectprocedure1 = inspectProcedures.AnthropometricDataQualityProcedure(
        required_mp)
    inspector = inspectFilters.QualityFilter(inspectprocedure1)
    inspector.run()

    # --------------------------FILES -----------------------
    staticMeasurement = utils.find_static(sessionXML)
    staticFilename = qtmTools.getFilename(staticMeasurement)

    dynamicFilenames = []
    dynamicMeasurements = qtmTools.findDynamic(sessionXML)
    for dynamicMeasurement in dynamicMeasurements:
        reconstructFilenameLabelled = qtmTools.getFilename(dynamicMeasurement)
        dynamicFilenames.append(reconstructFilenameLabelled)

    # --------------------------CHECKING -----------------------
    # static is only in qtm folder and does not get processed
    runAllQualityProcedures(qtm_folder, [staticFilename])
    runAllQualityProcedures(os.path.join(qtm_folder, "processed"),
                            dynamicFilenames)
Exemple #2
0
def prepare_folder_and_run_event_detection(sessionXML, work_folder,
                                           processed_folder):
    utils.create_directory_if_needed(processed_folder)

    staticMeasurement = utils.find_static(sessionXML)
    filename = qtmTools.getFilename(staticMeasurement)
    calibrated_file_path = os.path.join(processed_folder, filename)
    if not os.path.isfile(calibrated_file_path):
        shutil.copyfile(os.path.join(work_folder, filename),
                        calibrated_file_path)
        logging.info(
            "qualisys exported c3d file [%s] copied to processed folder" %
            (filename))

    dynamicMeasurements = qtmTools.findDynamic(sessionXML)
    for dynamicMeasurement in dynamicMeasurements:
        filename = qtmTools.getFilename(dynamicMeasurement)
        no_events_file_path = os.path.join(work_folder, filename)
        processed_file_path = os.path.join(processed_folder, filename)
        if not os.path.isfile(processed_file_path):
            shutil.copyfile(no_events_file_path, processed_file_path)
            logging.info(
                "qualisys exported c3d file [%s] copied to processed folder" %
                (filename))

            acq = btkTools.smartReader(processed_file_path)

            if "5" in btkTools.smartGetMetadata(acq, "FORCE_PLATFORM", "TYPE"):
                forceplates.correctForcePlateType5(acq)

            acq, state = eventDetector.zeni(acq)
            btkTools.smartWriter(acq, processed_file_path)
def main():

    logging.info("------------------------------------------------")
    logging.info("------------QTM - pyCGM2 Workflow---------------")
    logging.info("------------------------------------------------")
    file = "session.xml"
    sessionXML = files.readXml(os.getcwd() + "\\", file)
    sessionDate = files.getFileCreationDate(os.getcwd() + "\\" + file)

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

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

    dynamicMeasurements = qtmTools.findDynamic(sessionXML)
    for dynamicMeasurement in dynamicMeasurements:
        reconstructFilenameLabelled = qtmTools.getFilename(dynamicMeasurement)
        if not os.path.isfile(DATA_PATH + reconstructFilenameLabelled):
            shutil.copyfile(os.getcwd() + "\\" + reconstructFilenameLabelled,
                            DATA_PATH + reconstructFilenameLabelled)
            logging.info(
                "qualisys exported c3d file [%s] copied to processed folder" %
                (reconstructFilenameLabelled))

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

            if btkTools.checkForcePlateExist(acq):
                if "5" in btkTools.smartGetMetadata(acq, "FORCE_PLATFORM",
                                                    "TYPE"):
                    forceplates.correctForcePlateType5(acq)

            acq, zeniState = eventDetector.zeni(acq)

            if zeniState:
                btkTools.smartWriter(
                    acq, str(DATA_PATH + reconstructFilenameLabelled))

                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_2-pyCGM2.settings"):
        settings = files.openFile(pyCGM2.PYCGM2_APPDATA_PATH,
                                  "CGM2_2-pyCGM2.settings")
    else:
        settings = files.openFile(pyCGM2.PYCGM2_SETTINGS_FOLDER,
                                  "CGM2_2-pyCGM2.settings")
    # --------------------------MP ------------------------------------
    required_mp, optional_mp = qtmTools.SubjectMp(sessionXML)

    # --Check MP
    inspectprocedure = inspectProcedures.AnthropometricDataQualityProcedure(
        required_mp)
    inspector = inspectFilters.QualityFilter(inspectprocedure)
    inspector.run()

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

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

    # --------------------------MODEL CALIBRATION -----------------------
    logging.info(
        "--------------------------MODEL CALIBRATION -----------------------")
    staticMeasurement = utils.find_static(sessionXML)
    calibrateFilenameLabelled = qtmTools.getFilename(staticMeasurement)

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

    leftFlatFoot = toBool(
        staticMeasurement.Left_foot_normalised_to_static_trial.text)
    rightFlatFoot = toBool(
        staticMeasurement.Right_foot_normalised_to_static_trial.text)
    headFlat = toBool(staticMeasurement.Head_normalised_to_static_trial.text)

    markerDiameter = float(staticMeasurement.Marker_diameter.text) * 1000.0
    hjcMethod = settings["Calibration"]["HJC"]
    pointSuffix = None

    # Calibration checking
    # --------------------
    acqStatic = btkTools.smartReader(DATA_PATH + calibrateFilenameLabelled)
    for key in MARKERSETS.keys():
        logging.info("[pyCGM2] Checking of the %s" % (key))

        # presence
        ip_presence = inspectProcedures.MarkerPresenceQualityProcedure(
            acqStatic, markers=MARKERSETS[key])
        inspector = inspectFilters.QualityFilter(ip_presence)
        inspector.run()

        if ip_presence.markersIn != []:

            ip_gap = inspectProcedures.GapQualityProcedure(
                acqStatic, markers=ip_presence.markersIn)
            inspector = inspectFilters.QualityFilter(ip_gap)
            inspector.run()

            ip_swap = inspectProcedures.SwappingMarkerQualityProcedure(
                acqStatic, markers=ip_presence.markersIn)
            inspector = inspectFilters.QualityFilter(ip_swap)
            inspector.run()

            ip_pos = inspectProcedures.MarkerPositionQualityProcedure(
                acqStatic, markers=ip_presence.markersIn)
            inspector = inspectFilters.QualityFilter(ip_pos)

    # Calibration operation
    # --------------------
    logging.info("[pyCGM2] --- calibration operation ---")
    model, acqStatic = cgm2_2.calibrate(DATA_PATH, calibrateFilenameLabelled,
                                        translators, settings, required_mp,
                                        optional_mp, False, leftFlatFoot,
                                        rightFlatFoot, headFlat,
                                        markerDiameter, hjcMethod, pointSuffix)

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

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

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

        reconstructFilenameLabelled = qtmTools.getFilename(dynamicMeasurement)

        logging.info("----Processing of [%s]-----" %
                     (reconstructFilenameLabelled))
        mfpa = qtmTools.getForcePlateAssigment(dynamicMeasurement)
        momentProjection_text = dynamicMeasurement.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)

        # Fitting checking
        # --------------------
        for key in MARKERSETS.keys():
            if key != "Calibration markers":

                logging.info("[pyCGM2] Checking of the %s" % (key))
                # presence
                ip_presence = inspectProcedures.MarkerPresenceQualityProcedure(
                    acq, markers=MARKERSETS[key])
                inspector = inspectFilters.QualityFilter(ip_presence)
                inspector.run()

                if ip_presence.markersIn != []:

                    ip_gap = inspectProcedures.GapQualityProcedure(
                        acq, markers=ip_presence.markersIn)
                    inspector = inspectFilters.QualityFilter(ip_gap)
                    inspector.run()

                    ip_swap = inspectProcedures.SwappingMarkerQualityProcedure(
                        acq, markers=ip_presence.markersIn)
                    inspector = inspectFilters.QualityFilter(ip_swap)
                    inspector.run()

                    ip_pos = inspectProcedures.MarkerPositionQualityProcedure(
                        acq, markers=ip_presence.markersIn)
                    inspector = inspectFilters.QualityFilter(ip_pos)

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

        # marker
        order = int(float(dynamicMeasurement.Marker_lowpass_filter_order.text))
        fc = float(dynamicMeasurement.Marker_lowpass_filter_frequency.text)

        signal_processing.markerFiltering(acq, order=order, fc=fc)

        # management of force plate type 5 and force plate filtering
        order = int(
            float(dynamicMeasurement.Forceplate_lowpass_filter_order.text))
        fc = float(dynamicMeasurement.Forceplate_lowpass_filter_frequency.text)

        if order != 0 and fc != 0:
            acq = btkTools.smartReader(DATA_PATH + reconstructFilenameLabelled)
            if btkTools.checkForcePlateExist(acq):
                if "5" in btkTools.smartGetMetadata(acq, "FORCE_PLATFORM",
                                                    "TYPE"):
                    forceplates.correctForcePlateType5(acq)
            signal_processing.markerFiltering(acq, order=order, fc=fc)
        else:
            if btkTools.checkForcePlateExist(acq):
                if "5" in btkTools.smartGetMetadata(acq, "FORCE_PLATFORM",
                                                    "TYPE"):
                    forceplates.correctForcePlateType5(acq)

        btkTools.smartWriter(acq, DATA_PATH + reconstructFilenameLabelled)

        # event checking
        # -----------------------
        inspectprocedureEvents = inspectProcedures.GaitEventQualityProcedure(
            acq)
        inspector = inspectFilters.QualityFilter(inspectprocedureEvents)
        inspector.run()
        eventInspectorStates.append(inspectprocedureEvents.state)

        # fitting operation
        # -----------------------
        logging.info("[pyCGM2] --- Fitting operation ---")
        acqGait = cgm2_2.fitting(model, DATA_PATH, reconstructFilenameLabelled,
                                 translators, settings, markerDiameter,
                                 pointSuffix, mfpa, momentProjection)

        outFilename = reconstructFilenameLabelled  # [:-4] + "_CGM1.c3d"
        btkTools.smartWriter(acqGait, str(DATA_PATH + outFilename))
        modelledC3ds.append(outFilename)

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

    # --------------------------GAIT PROCESSING -----------------------
    if not all(eventInspectorStates):
        raise Exception(
            "[pyCGM2] Impossible to run Gait processing. Badly gait event detection. check the log file"
        )
    logging.info(
        "---------------------GAIT PROCESSING -----------------------")
    return model
Exemple #4
0
def get_calibration_arguments_and_model_manager(model_type, data_path,
                                                session_xml, point_suffix):
    settings = get_settings(model_type)
    translators = settings["Translators"]
    required_mp, optional_mp = qtmTools.SubjectMp(session_xml)

    static_session_xml_soup = utils.find_static(session_xml)
    calibration_filename = qtmTools.getFilename(static_session_xml_soup)

    leftFlatFoot = toBool(static_session_xml_soup.Left_foot_flat)
    rightFlatFoot = toBool(static_session_xml_soup.Right_foot_flat)
    headFlat = toBool(static_session_xml_soup.Head_flat)
    markerDiameter = float(
        static_session_xml_soup.Marker_diameter.text) * 1000.0

    dynamic_measurements = qtmTools.findDynamic(session_xml)
    user_settings = {
        "Calibration": {
            "Left flat foot": leftFlatFoot,
            "Right flat foot": rightFlatFoot,
            "Head flat": headFlat,
            "StaticTrial": calibration_filename
        },
        "Global": {
            "Marker diameter": markerDiameter,
            "Point suffix": point_suffix
        },
        "MP": {
            "Required": required_mp,
            "Optional": optional_mp
        },
        "Fitting": {
            "Trials": dynamic_measurements
        }
    }
    if model_type == "CGM1":
        model_manager = ModelManager.CGM1ConfigManager(
            user_settings,
            localInternalSettings=settings,
            localTranslators={"Translators": translators})
        model_manager.contruct()
        calibration_arguments = (
            data_path + "\\",
            model_manager.staticTrial,
            model_manager.translators,
            model_manager.requiredMp,
            model_manager.optionalMp,
            model_manager.leftFlatFoot,
            model_manager.rightFlatFoot,
            model_manager.headFlat,
            model_manager.markerDiameter,
            model_manager.pointSuffix,
        )
    elif model_type == "CGM2_3":
        model_manager = ModelManager.CGM2_3ConfigManager(
            user_settings,
            localInternalSettings=settings,
            localTranslators={"Translators": translators})
        model_manager.contruct()
        finalSettings = model_manager.getFinalSettings()
        calibration_arguments = (
            data_path + "\\",
            model_manager.staticTrial,
            model_manager.translators,
            finalSettings,
            model_manager.requiredMp,
            model_manager.optionalMp,
            model_manager.enableIK,
            model_manager.leftFlatFoot,
            model_manager.rightFlatFoot,
            model_manager.headFlat,
            model_manager.markerDiameter,
            model_manager.hjcMethod,
            model_manager.pointSuffix,
        )
    else:
        raise Exception(
            "Processing for model_type={} is not implemented".format(
                model_type))
    return calibration_arguments, model_manager