Beispiel #1
0
def get_session_data(rootdir, pid, sid_list):
    """
    Convenience function to retrieve the data for the specified particpiant and
    session ID list in a (vector, label) tuple.
    """
    file_list = filestruct.get_feature_file_list(rootdir, pid, sid_list)
    (X, y) = read_feature_file_list(file_list)
    return (X, y)
Beispiel #2
0
    def build_pipeline(self):
        """
        Builds the processing pipeline. Most of the pipeline is specified by
        the config, but we need to gather training data, build a classifier
        with that data, and insert the classifier into the pipeline.
        """
        train_list = []
        for i in range(self.ui.trainingList.count()):
            item = self.ui.trainingList.item(i)
            if item.checkState():
                train_list.append(str(item.text()))
        self.training_sessions = train_list

        if not train_list:
            QtWidgets.QMessageBox().critical(
                self, "Error", "No sessions selected for training.",
                QtWidgets.QMessageBox.Ok)
            return

        # get only the labels for the selected TAC session
        # need to loop over available gestures to catch those with no dof
        labels = []
        mapping = {}
        for gesture in self.cfg.gestures:
            if gesture.dof is None:
                labels.append(gesture.label)
                mapping[gesture.label] = gesture.action
            else:
                if gesture in self.tac_session.gestures:
                    labels.append(gesture.label)
                    mapping[gesture.label] = gesture.action

        file_list = filestruct.get_feature_file_list(self.cfg.data_path,
                                                     self.pid, train_list)
        training_data = processing.read_feature_file_list(file_list,
                                                          labels=labels)

        # get average MAV for each gesture label to auto-set boosts
        # warning: super hacky
        j = 0
        start = 0
        for i, feature in enumerate(self.cfg.feature_extractor.features):
            if 'MAV' in str(feature):
                start = j
                break
            else:
                j += feature.dim_per_channel * len(self.cfg.channels)
        X, y = training_data
        X = X[:, start:len(self.cfg.channels)]
        boosts = dict()
        for label in labels:
            mav_avg = np.mean(X[y == label, :], axis=1)
            # -np.partition(-data, N) gets N largest elements of data
            boosts[label] = 1 / np.mean(-np.partition(-mav_avg, 10)[:10])
        self.boosts = boosts

        # re-create the controller to make sure it has the correct mapping
        self.controller = control.DBVRController(
            mapping=mapping,
            ramp_length=self.cfg.controller.ramp_length,
            boosts=1 if self.test else boosts)

        self.cfg.learner.fit(*training_data)

        self.pipeline = pipeline.Pipeline([
            self.cfg.conditioner, self.cfg.windower,
            (
                features.FeatureExtractor([features.MAV()],
                                          len(self.cfg.channels)),
                [self.cfg.feature_extractor, self.cfg.learner],
            )
        ])

        self.record_thread.set_pipeline(self.pipeline)
Beispiel #3
0
    def build_pipeline(self):
        """
        Builds the processing pipeline. Most of the pipeline is specified by
        the config, but we need to gather training data, build a classifier
        with that data, and insert the classifier into the pipeline.
        """
        train_list = []
        for i in range(self.ui.trainingList.count()):
            item = self.ui.trainingList.item(i)
            if item.checkState():
                train_list.append(str(item.text()))
        self.training_sessions = train_list

        if not train_list:
            QtWidgets.QMessageBox().critical(
                self, "Error",
                "No sessions selected for training.",
                QtWidgets.QMessageBox.Ok)
            return

        # get only the labels for the selected TAC session
        # need to loop over available gestures to catch those with no dof
        labels = []
        mapping = {}
        for gesture in self.cfg.gestures:
            if gesture.dof is None:
                labels.append(gesture.label)
                mapping[gesture.label] = gesture.action
            else:
                if gesture in self.tac_session.gestures:
                    labels.append(gesture.label)
                    mapping[gesture.label] = gesture.action

        file_list = filestruct.get_feature_file_list(
            self.cfg.data_path, self.pid, train_list)
        training_data = processing.read_feature_file_list(
            file_list, labels=labels)

        # get average MAV for each gesture label to auto-set boosts
        # warning: super hacky
        j = 0
        start = 0
        for i, feature in enumerate(self.cfg.feature_extractor.features):
            if 'MAV' in str(feature):
                start = j
                break
            else:
                j += feature.dim_per_channel*len(self.cfg.channels)
        X, y = training_data
        X = X[:, start:len(self.cfg.channels)]
        boosts = dict()
        for label in labels:
            mav_avg = np.mean(X[y == label, :], axis=1)
            # -np.partition(-data, N) gets N largest elements of data
            boosts[label] = 1 / np.mean(-np.partition(-mav_avg, 10)[:10])
        self.boosts = boosts

        self.controller = control.DBVRController(
            mapping=mapping,
            ramp_length=self.cfg.controller.ramp_length,
            boosts=1 if self.test else boosts)

        clf_type = self.ui.classifierComboBox.currentText()
        if clf_type == 'LDA':
            clf = LDA()
        elif clf_type == 'SVM':
            clf = SVC(C=50, kernel='linear')
        else:
            clf = LDA()

        preproc = StandardScaler()
        skpipeline = Pipeline([('preproc', preproc), ('clf', clf)])

        classifier = pipeline.Classifier(skpipeline)
        classifier.fit(*training_data)

        pl = pipeline.Pipeline(
            [
                self.cfg.conditioner,
                self.cfg.windower,
                (
                    features.FeatureExtractor(
                        [features.MAV()],
                        len(self.cfg.channels)),
                    [
                        self.cfg.feature_extractor,
                        classifier
                    ],
                )
            ]
        )

        self.record_thread.set_pipeline(pl)