Пример #1
0
    def appendAnalogs(self):
        for nexusAnalogDevice in self.m_nexusAnalogDevices:

            channels = nexusAnalogDevice.getChannels()
            for channel in channels:

                start = self.m_firstFrame - 1
                end = self.m_lastFrame - 1

                data = channel.getValues()[
                    (start) * self.m_numberAnalogSamplePerFrame:(end + 1) *
                    self.m_numberAnalogSamplePerFrame]

                if self.m_firstFrame == 1:
                    time_init = 0.0
                else:
                    time_init = self.m_firstFrame / self.m_analogFrameRate

                ts = ma.TimeSequence(utils.str(channel.getLabel()), 1,
                                     data.shape[0], self.m_analogFrameRate,
                                     time_init, ma.TimeSequence.Type_Analog,
                                     "V", 1.0, 0.0, [-10.0, 10.0],
                                     self.m_trial.timeSequences())
                ts.setData(data.reshape((data.shape[0], 1)))
                ts.setDescription(channel.getDescription())
Пример #2
0
    def appendMarkers(self):

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

        for marker in markers:
            rawDataX, rawDataY, rawDataZ, E = NEXUS.GetTrajectory(
                self.m_subject, marker)

            E = np.asarray(E).astype("float") - 1
            values = np.array([
                np.asarray(rawDataX),
                np.asarray(rawDataY),
                np.asarray(rawDataZ)
            ]).T

            start = self.m_firstFrame - self.m_trialFirstFrame
            end = self.m_lastFrame - self.m_trialFirstFrame

            #if marker == "LTHI": import ipdb; ipdb.set_trace()
            values_cut = values[start:end + 1, :]
            E_cut = E[start:end + 1]

            data = np.zeros((values_cut.shape[0], 4))
            data[:, 0:3] = values_cut
            data[:, 3] = E_cut

            if self.m_firstFrame == 1:
                time_init = 0.0
            else:
                time_init = self.m_firstFrame / self.m_framerate

            ts = ma.TimeSequence(str(marker), 4, data.shape[0],
                                 self.m_framerate, time_init,
                                 ma.TimeSequence.Type_Marker, "mm",
                                 self.m_trial.timeSequences())
            ts.setData(data)
Пример #3
0
    def appendModelOutputs(self):

        modelOutputNames = NEXUS.GetModelOutputNames(self.m_subject)

        if modelOutputNames != []:
            for modelOutputName in modelOutputNames:
                data, E = NEXUS.GetModelOutput(self.m_subject, modelOutputName)

                type = NEXUS.GetModelOutputDetails(self.m_subject,
                                                   modelOutputName)[0]

                E = np.asarray(E).astype("float") - 1
                values = np.array([
                    np.asarray(data[0]),
                    np.asarray(data[1]),
                    np.asarray(data[2])
                ]).T

                start = self.m_firstFrame - self.m_trialFirstFrame
                end = self.m_lastFrame - self.m_trialFirstFrame

                values_cut = values[start:end + 1, :]
                E_cut = E[start:end + 1]

                data = np.zeros((values_cut.shape[0], 4))
                data[:, 0:3] = values_cut
                data[:, 3] = E_cut

                if self.m_firstFrame == 1:
                    time_init = 0.0
                else:
                    time_init = self.m_firstFrame / self.m_framerate

                if type == "Angles":
                    ts = ma.TimeSequence(utils.str(modelOutputName), 4,
                                         values_cut.shape[0], self.m_framerate,
                                         time_init, ma.TimeSequence.Type_Angle,
                                         "Deg", self.m_trial.timeSequences())
                    ts.setData(data)

                elif type == "Forces":
                    ts = ma.TimeSequence(utils.str(modelOutputName), 4,
                                         values_cut.shape[0], self.m_framerate,
                                         time_init,
                                         ma.TimeSequence.Type_Force, "N.Kg-1",
                                         self.m_trial.timeSequences())
                    ts.setData(data)

                elif type == "Moments":
                    ts = ma.TimeSequence(utils.str(modelOutputName), 4,
                                         values_cut.shape[0], self.m_framerate,
                                         time_init,
                                         ma.TimeSequence.Type_Moment,
                                         "Nmm.Kg-1",
                                         self.m_trial.timeSequences())
                    ts.setData(data)

                elif type == "Powers":
                    ts = ma.TimeSequence(utils.str(modelOutputName), 4,
                                         values_cut.shape[0], self.m_framerate,
                                         time_init, ma.TimeSequence.Type_Power,
                                         "Watt.Kg-1",
                                         self.m_trial.timeSequences())
                    ts.setData(data)

                elif type == "Modeled Markers":
                    ts = ma.TimeSequence(utils.str(modelOutputName), 4,
                                         values_cut.shape[0], self.m_framerate,
                                         time_init,
                                         ma.TimeSequence.Type_Marker, "mm",
                                         self.m_trial.timeSequences())
                    ts.setData(data)
                else:
                    logging.warning(
                        "[pyCGM2] : Model Output (%s) from Nexus not added to the openma trial"
                        % (modelOutputName))
Пример #4
0
def convertBtkAcquisition(acq, returnType="Trial"):

    root = ma.Node('root')
    trial = ma.Trial("Trial", root)

    framerate = acq.GetPointFrequency()
    firstFrame = acq.GetFirstFrame()

    numberAnalogSamplePerFrame = acq.GetNumberAnalogSamplePerFrame()
    analogFramerate = acq.GetAnalogFrequency()

    if firstFrame == 1:
        time_init = 0.0
    else:
        time_init = firstFrame / framerate

    for it in btk.Iterate(acq.GetPoints()):

        label = it.GetLabel()
        values = it.GetValues()
        residuals = it.GetResiduals()
        desc = it.GetDescription()

        data = np.zeros((values.shape[0], 4))
        data[:, 0:3] = values
        data[:, 3] = residuals[:, 0]

        if it.GetType() == btk.btkPoint.Marker:
            ts = ma.TimeSequence(str(label), 4, data.shape[0], framerate,
                                 time_init, ma.TimeSequence.Type_Marker, "mm",
                                 trial.timeSequences())

        elif it.GetType() == btk.btkPoint.Angle:
            ts = ma.TimeSequence(str(label), 4, data.shape[0], framerate,
                                 time_init, ma.TimeSequence.Type_Angle, "Deg",
                                 trial.timeSequences())

        elif it.GetType() == btk.btkPoint.Force:
            ts = ma.TimeSequence(str(label), 4, data.shape[0], framerate,
                                 time_init, ma.TimeSequence.Type_Force,
                                 "N.Kg-1", trial.timeSequences())

        elif it.GetType() == btk.btkPoint.Moment:
            ts = ma.TimeSequence(str(label), 4, data.shape[0], framerate,
                                 time_init, ma.TimeSequence.Type_Moment,
                                 "Nmm.Kg-1", trial.timeSequences())

        elif it.GetType() == btk.btkPoint.Power:
            ts = ma.TimeSequence(str(label), 4, data.shape[0], framerate,
                                 time_init, ma.TimeSequence.Type_Power,
                                 "Watt.Kg-1", trial.timeSequences())

        else:
            logging.warning(
                "[pyCGM2] point [%s] not copied into openma trial" % (label))

        ts.setData(data)
        ts.setDescription(desc)

    for it in btk.Iterate(acq.GetAnalogs()):

        label = it.GetLabel()
        values = it.GetValues()
        desc = it.GetDescription()

        data = values

        ts = ma.TimeSequence(str(label), 1, data.shape[0], analogFramerate,
                             time_init, ma.TimeSequence.Type_Analog, "V", 1.0,
                             0.0, [-10.0, 10.0], trial.timeSequences())
        ts.setData(data)
        ts.setDescription(desc)

    for it in btk.Iterate(acq.GetEvents()):

        label = it.GetLabel()
        time = it.GetTime()
        context = it.GetContext()
        subject = it.GetSubject()

        ev = ma.Event(label, time, context, str(subject), trial.events())

    sortedEvents(trial)

    if returnType == "Trial":
        return trial
    else:
        return root
Пример #5
0
    def export(self, outputName, path=None):

        root = ma.Node('root')
        trial = ma.Trial("AnalysisC3d", root)

        # metadata
        #-------------

        # subject infos
        if self.analysis.subjectInfo is not None:
            subjInfo = self.analysis.subjectInfo
            for item in subjInfo.items():
                trial.setProperty("SUBJECT_INFO:" + str(item[0]), item[1])

        # model infos
        if self.analysis.modelInfo is not None:
            modelInfo = self.analysis.modelInfo
            for item in modelInfo.items():
                trial.setProperty("MODEL_INFO:" + str(item[0]), item[1])

        # model infos
        if self.analysis.experimentalInfo is not None:
            experimentalConditionInfo = self.analysis.experimentalInfo
            for item in experimentalConditionInfo.items():
                trial.setProperty("EXPERIMENTAL_INFO:" + str(item[0]), item[1])

        #trial.setProperty('MY_GROUP:MY_PARAMETER',10.0)

        # kinematic cycles
        #------------------

        # metadata
        # for key in self.analysis.kinematicStats.data.keys():
        #     if key[1]=="Left":
        #         n_left_cycle = len(self.analysis.kinematicStats.data[key[0],key[1]]["values"])
        #         trial.setProperty('PROCESSING:LeftKinematicCycleNumber',n_left_cycle)
        #         break
        #
        # for key in self.analysis.kinematicStats.data.keys():
        #     if key[1]=="Right":
        #         n_right_cycle = len(self.analysis.kinematicStats.data[key[0],key[1]]["values"])
        #         trial.setProperty('PROCESSING:RightKinematicCycleNumber',n_right_cycle)
        #         break

        # cycles
        for key in self.analysis.kinematicStats.data.keys():
            label = key[0]
            context = key[1]
            if not np.all(
                    self.analysis.kinematicStats.data[label,
                                                      context]["mean"] == 0):
                cycle = 0
                values = np.zeros((101, 4))
                values2 = np.zeros((101, 1))
                for val in self.analysis.kinematicStats.data[
                        label, context]["values"]:
                    angle = ma.TimeSequence(
                        str(label + "." + context + "." + str(cycle)), 4, 101,
                        1.0, 0.0, ma.TimeSequence.Type_Angle, "deg",
                        trial.timeSequences())
                    values[:, 0:3] = val
                    angle.setData(values)
                    cycle += 1

        # kinetic cycles
        #------------------

        # # metadata
        # for key in self.analysis.kineticStats.data.keys():
        #     if key[1]=="Left":
        #         n_left_cycle = len(self.analysis.kineticStats.data[key[0],key[1]]["values"])
        #         trial.setProperty('PROCESSING:LeftKineticCycleNumber',n_left_cycle)
        #         break
        #
        # for key in self.analysis.kineticStats.data.keys():
        #     if key[1]=="Right":
        #         n_right_cycle = len(self.analysis.kineticStats.data[key[0],key[1]]["values"])
        #         trial.setProperty('PROCESSING:RightKineticCycleNumber',n_right_cycle)
        #         break

        # cycles
        for key in self.analysis.kineticStats.data.keys():
            label = key[0]
            context = key[1]
            if not np.all(
                    self.analysis.kineticStats.data[label,
                                                    context]["mean"] == 0):
                cycle = 0
                values = np.zeros((101, 4))
                for val in self.analysis.kineticStats.data[label,
                                                           context]["values"]:
                    moment = ma.TimeSequence(
                        str(label + "." + context + "." + str(cycle)), 4, 101,
                        1.0, 0.0, ma.TimeSequence.Type_Moment, "N.mm",
                        trial.timeSequences())
                    values[:, 0:3] = val
                    moment.setData(values)
                    cycle += 1

        # for key in self.analysis.emgStats.data.keys():
        #     label = key[0]
        #     context = key[1]
        #     if not np.all( self.analysis.emgStats.data[label,context]["mean"]==0):
        #         cycle = 0
        #         for val in self.analysis.emgStats.data[label,context]["values"]:
        #             analog = ma.TimeSequence(str(label+"."+context+"."+str(cycle)),1,101,1.0,0.0,ma.TimeSequence.Type_Analog,"V", 1.0,0.0,[-10.0,10.0], trial.timeSequences())
        #             analog.setData(val)
        #             cycle+=1

        try:
            if path == None:
                ma.io.write(root, str(outputName + ".c3d"))
            else:
                ma.io.write(root, str(path + outputName + ".c3d"))
            logging.info("Analysis c3d  [%s.c3d] Exported" %
                         (str(outputName + ".c3d")))
        except:
            raise Exception("[pyCGM2] : analysis c3d doesn t export")