Esempio n. 1
0
    def test_mav(self):
        mapping = {0: '0', 1: '1'}

        controller = control.DBVRController(mapping, ramp_length=1, boosts=1)

        out = controller.process((0.8, 0))
        assert out == {'0': 0.8, '1': 0}
Esempio n. 2
0
    def test_ramp_length_1(self):
        mapping = {0: '0', 1: '1'}

        controller = control.DBVRController(mapping, ramp_length=1, boosts=1)

        out = controller.process((1, 0))
        assert out == {'0': 1, '1': 0}
        out = controller.process((1, 1))
        assert out == {'0': 0, '1': 1}
Esempio n. 3
0
    def test_complex(self):
        mapping = {0: 'no-contraction', 1: '1', 2: '2'}

        controller = control.DBVRController(mapping, ramp_length=5, boosts=1)

        out = controller.process((1, 0))
        assert out == 'no-contraction'
        out = controller.process((1, 1))
        assert out['1'] > 0
        assert out['2'] == 0
        out = controller.process((1, 2))
        assert out['1'] == 0
        assert out['2'] > 0
Esempio n. 4
0
    def test_boosts(self):
        mapping = {0: '0', 1: '1'}
        boosts = {0: 2, 1: 1}
        labels = [0] * 7 + [1] * 7
        mav = [1] * len(labels)
        data = zip(mav, labels)

        controller = control.DBVRController(mapping,
                                            ramp_length=5,
                                            boosts=boosts)

        out = _run_controller(controller, data)
        assert out[5]['0'] == 1
        assert out[-1]['1'] == 1
Esempio n. 5
0
    def test_simple(self):
        mapping = {0: 0, 1: 1, 2: 2}
        labels = [0] * 10
        mav = [1] * len(labels)
        data = zip(mav, labels)

        controller = control.DBVRController(mapping, ramp_length=5, boosts=1)

        out = _run_controller(controller, data)

        on_ramp = out[3][0]
        saturated = out[5][0]

        assert on_ramp > 0 and on_ramp < 1
        assert saturated == 1
Esempio n. 6
0
    features.SSC(thresh=0.003)
], len(channels))

learner = pipeline.Classifier(
    Pipeline([('preproc', StandardScaler()), ('clf', LDA())]))

post_processor = processing.Processor(
    conditioner=conditioner,
    windower=windower,
    feature_extractor=feature_extractor,
    rest_bounds=None,
    gesture_bounds=(int((prompt_times[0] + 0.5) * daq_st['f_proc']),
                    int((prompt_times[1] - 0.5) * daq_st['f_proc'])))

controller = control.DBVRController(
    mapping={g.label: g.action
             for g in gestures}, ramp_length=5)

tac_sessions = {
    '3 active, 1 target':
    experiment.TACSession([g for g in gestures if g.dof in [1, 2, 3]],
                          simul=1,
                          rep=2,
                          timeout=15,
                          dist=60,
                          tol=10,
                          dwell=2),
    '3 active, 2 target':
    experiment.TACSession([g for g in gestures if g.dof in [1, 2, 3]],
                          simul=(1, 2),
                          rep=1,
Esempio n. 7
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)