Exemplo n.º 1
0
def generateClassifier(params, chamberID, observed_samplingFreq,
                       observed_epochTime):
    # self.recordWaves = params.writeWholeWaves
    # self.extractorType = params.extractorType
    # self.finalClassifierDir = params.finalClassifierDir
    networkName = 'UTSN-L'
    classifierID, model_samplingFreq, model_epochTime = selectClassifierID(
        params.finalClassifierDir, networkName, observed_samplingFreq,
        observed_epochTime)
    # classifier_samplingFreq, classifier_epochTime = classifierMetadata(params.finalClassifierDir, classifierID)
    # check if classifier's samplingFreq and epochTime matches with requested samplingFreq and epochTime
    client = ClassifierClient(params.writeWholeWaves,
                              params.extractorType,
                              params.classifierType,
                              classifierID,
                              chamberID=chamberID,
                              samplingFreq=model_samplingFreq,
                              epochTime=model_epochTime)
    client.predictionStateOn()
    client.hasGUI = False
    return client
Exemplo n.º 2
0
    def start_double_client(self):
        def to_f(inpt):
            try:
                return float(inpt)

            except Exception:
                return None

        eeg_std = to_f(self.eeg_std.text())
        emg_std = to_f(self.emg_std.text())

        statusbar = self.statusBar()

        try:
            self.client = ClassifierClient(host, port, 'double')

        except Exception as e:
            statusbar.showMessage(str(e))
            raise e

        try:
            self.server = ReadDAQServer(host,
                                        port,
                                        self.recordWaves,
                                        eeg_std=eeg_std,
                                        emg_std=emg_std,
                                        channelOpt='double')

        except Exception as e:
            statusbar.showMessage(str(e))
            raise e

        self.t.start()
        self.client.setGraph1chan(self.graph1chan)
        self.client.setGraph2chan(self.graph2chan)
        self.client.setPredictionLamp(self.predictionLamp)
        message = 'successfully started ! eeg_std is {}, and emg_std is {}'
        statusbar.showMessage(message.format(eeg_std, emg_std))
Exemplo n.º 3
0
    def initUI(self):

        self.startPredictionButton = QtWidgets.QPushButton('Predict', self)
        self.startPredictionButton.setCheckable(True)
        self.startPredictionButton.clicked.connect(self.startPrediction)
        self.startPredictionButton.resize(
            self.startPredictionButton.sizeHint())
        self.startPredictionButton.move(int(5 * self.scale),
                                        int(10 * self.scale))

        quitButton = QtWidgets.QPushButton('Quit', self)
        quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)
        quitButton.resize(quitButton.sizeHint())
        quitButton.move(int(85 * self.scale), int(10 * self.scale))

        checkConnectionButton = QtWidgets.QPushButton('Test connection', self)
        checkConnectionButton.clicked.connect(self.check_connection)
        checkConnectionButton.resize(checkConnectionButton.sizeHint())
        checkConnectionButton.move(int(160 * self.scale), int(10 * self.scale))

        # change standardization of ch2
        self.nameLabel_terminal_label = QLabel(self)
        self.nameLabel_terminal_label.setText('Terminal:')
        self.nameLabel_terminal_label.move(int(250 * self.scale),
                                           int(35 * self.scale))
        self.terminal_combobox = QtWidgets.QComboBox(self)
        self.terminal_combobox.addItem(self.terminal_str_diff)
        self.terminal_combobox.addItem(self.terminal_str_rse)
        self.terminal_combobox.addItem(self.terminal_str_nrse)
        # self.terminal_combobox.addItem(self.terminal_str_pseudo) # not available for NI DAQ USB-6210
        self.terminal_combobox.move(int(310 * self.scale),
                                    int(38 * self.scale))
        self.terminal_combobox.resize(self.terminal_combobox.sizeHint())
        self.terminal_combobox.activated[str].connect(self.terminal_choice)
        self.terminal_combobox.setCurrentText(self.terminal_config)

        # change model
        self.nameLabel_classifier = QLabel(self)
        self.nameLabel_classifier.setText('Model:')
        self.nameLabel_classifier.move(int(300 * self.scale),
                                       int(10 * self.scale))

        self.classifier_combobox = QtWidgets.QComboBox(self)
        for classifier_type in self.classifier_types:
            self.classifier_combobox.addItem(classifier_type)
        self.classifier_combobox.resize(self.classifier_combobox.sizeHint())
        self.classifier_combobox.move(int(340 * self.scale),
                                      int(10 * self.scale))
        self.classifier_combobox.activated[str].connect(self.classifier_choice)
        '''
        self.nameLabel_ch2_overwrite_label = QLabel(self)
        self.nameLabel_ch2_overwrite_label.setText('Overwrite by Ch2:')
        self.nameLabel_ch2_overwrite_label.move(int(450 * self.scale), int(5 * self.scale))

        self.overwriteOrNotButton = QtWidgets.QPushButton(self.label_notOverwrite, self)
        self.overwriteOrNotButton.clicked.connect(self.toggleOverwriteOrNot)
        self.overwriteOrNotButton.resize(self.overwriteOrNotButton.sizeHint())
        self.overwriteOrNotButton.update()
        self.overwriteOrNotButton.move(int(530 * self.scale), int(5 * self.scale))
        self.overwriteOrNotButton.setCheckable(True)
        '''

        # change standardization of eeg
        self.nameLabel_eeg_visualize_mode_label = QLabel(self)
        self.nameLabel_eeg_visualize_mode_label.setText('EEG mode:')
        self.nameLabel_eeg_visualize_mode_label.move(int(610 * self.scale),
                                                     int(2 * self.scale))
        self.eeg_visualize_mode_combobox = QtWidgets.QComboBox(self)
        self.eeg_visualize_mode_combobox.addItem(
            self.eeg_visualize_mode_str_none)
        self.eeg_visualize_mode_combobox.addItem(
            self.eeg_visualize_mode_str_normalize)
        self.eeg_visualize_mode_combobox.move(int(680 * self.scale),
                                              int(5 * self.scale))
        self.eeg_visualize_mode_combobox.resize(
            self.eeg_visualize_mode_combobox.sizeHint())
        self.eeg_visualize_mode_combobox.activated[str].connect(
            self.eeg_visualize_mode_choice)

        # change standardization of ch2
        self.nameLabel_ch2_visualize_mode_label = QLabel(self)
        self.nameLabel_ch2_visualize_mode_label.setText('Ch2 mode:')
        self.nameLabel_ch2_visualize_mode_label.move(int(610 * self.scale),
                                                     int(30 * self.scale))
        self.ch2_visualize_mode_combobox = QtWidgets.QComboBox(self)
        self.ch2_visualize_mode_combobox.addItem(
            self.ch2_visualize_mode_str_none)
        self.ch2_visualize_mode_combobox.addItem(
            self.ch2_visualize_mode_str_normalize)
        self.ch2_visualize_mode_combobox.move(int(680 * self.scale),
                                              int(33 * self.scale))
        self.ch2_visualize_mode_combobox.resize(
            self.ch2_visualize_mode_combobox.sizeHint())
        self.ch2_visualize_mode_combobox.activated[str].connect(
            self.ch2_visualize_mode_choice)

        # set overwrite threshold to W
        self.nameLabel_ch2_thresh = QLabel(self)
        self.nameLabel_ch2_thresh.setText('Overwrite threshold to W:')
        self.nameLabel_ch2_thresh.resize(self.nameLabel_ch2_thresh.sizeHint())
        self.nameLabel_ch2_thresh.move(int(840 * self.scale),
                                       int(15 * self.scale))
        self.ch2_thresh = QLineEdit(self)
        self.ch2_thresh.setText(str(self.params.ch2_thresh_default))
        self.ch2_thresh.move(int(1000 * self.scale), int(15 * self.scale))
        self.ch2_thresh.resize(50, 20)
        self.ch2_thresh.textChanged.connect(self.ch2_thresh_text_change)

        # change usage of ch2
        self.nameLabel_ch2_usage_label = QLabel(self)
        self.nameLabel_ch2_usage_label.setText('Ch2 usage:')
        self.nameLabel_ch2_usage_label.move(int(840 * self.scale),
                                            int(60 * self.scale))
        self.ch2_usage_combobox = QtWidgets.QComboBox(self)
        self.ch2_usage_combobox.addItem(self.ch2_usage_str_dontshowCh2)
        self.ch2_usage_combobox.addItem(self.ch2_usage_str_showCh2)
        self.ch2_usage_combobox.addItem(self.ch2_usage_str_overwrite)
        self.ch2_usage_combobox.move(int(910 * self.scale),
                                     int(60 * self.scale))
        self.ch2_usage_combobox.resize(self.ch2_usage_combobox.sizeHint())
        self.ch2_usage_combobox.activated[str].connect(self.ch2_usage_choice)
        self.ch2_usage_combobox_setup()

        self.ylim_label_eeg = QLabel(self)
        self.ylim_label_eeg.setText('eeg y-max:')
        self.ylim_label_eeg.move(int(40 * self.scale), int(40 * self.scale))
        self.ylim_label_eeg.resize(self.ylim_label_eeg.sizeHint())
        self.ylim_value_eeg_box = QLineEdit(self)
        self.ylim_value_eeg_box.move(int(45 * self.scale),
                                     int(60 * self.scale))
        self.ylim_value_eeg_box.resize(40, 20)

        self.ylim_slider_eeg = QSlider(Qt.Vertical, self)
        self.ylim_slider_eeg.move(int(10 * self.scale), int(40 * self.scale))
        self.ylim_slider_eeg.resize(20, 80)
        self.ylim_slider_eeg.setMinimum(0)
        self.ylim_slider_eeg.setMaximum(100)
        self.ylim_slider_eeg.setTickPosition(QSlider.TicksBelow)
        self.ylim_slider_eeg.setTickInterval(1)
        self.ylim_slider_eeg.valueChanged.connect(self.ylim_change_eeg)

        self.ylim_label_ch2 = QLabel(self)
        self.ylim_label_ch2.setText('ch2 y-max:')
        self.ylim_label_ch2.move(int(160 * self.scale), int(40 * self.scale))
        self.ylim_label_ch2.resize(self.ylim_label_ch2.sizeHint())
        self.ylim_value_ch2_box = QLineEdit(self)
        self.ylim_value_ch2_box.move(int(165 * self.scale),
                                     int(60 * self.scale))
        self.ylim_value_ch2_box.resize(40, 20)

        self.ylim_slider_ch2 = QSlider(Qt.Vertical, self)
        self.ylim_slider_ch2.move(int(130 * self.scale), int(40 * self.scale))
        self.ylim_slider_ch2.resize(20, 40)
        self.ylim_slider_ch2.setMinimum(0)
        self.ylim_slider_ch2.setMaximum(100)
        self.ylim_slider_ch2.setTickPosition(QSlider.TicksBelow)
        self.ylim_slider_ch2.setTickInterval(1)
        self.ylim_slider_ch2.valueChanged.connect(self.ylim_change_ch2)

        self.ch2_thresh_slider_tick_factor = 4
        self.ch2_thresh_slider = QSlider(Qt.Horizontal, self)
        self.ch2_thresh_slider.move(int(1035 * self.scale),
                                    int(20 * self.scale))
        self.ch2_thresh_slider.resize(190, 20)
        self.ch2_thresh_minimum = -2
        self.ch2_thresh_maximum = 10
        self.ch2_thresh_slider.setMinimum(self.ch2_thresh_minimum *
                                          self.ch2_thresh_slider_tick_factor)
        self.ch2_thresh_slider.setMaximum(self.ch2_thresh_maximum *
                                          self.ch2_thresh_slider_tick_factor)
        self.ch2_thresh_slider.setValue(self.ch2_thresh_slider_tick_factor)
        self.ch2_thresh_slider.setTickPosition(QSlider.TicksBelow)
        self.ch2_thresh_slider.setTickInterval(1)
        self.ch2_thresh_slider.valueChanged.connect(self.ch2_thresh_change)

        self.label_graph_ch2 = QLabel(self)
        self.label_graph_ch2.setFont(QtGui.QFont('Courier New', 24))
        self.label_graph_ch2.setText('Epoch# : Prediction')
        self.label_graph_ch2.resize(self.label_graph_ch2.sizeHint())
        self.label_graph_ch2.move(int(500 * self.scale), int(60 * self.scale))

        self.label_graph_eeg = QLabel(self)
        self.label_graph_eeg.setFont(QtGui.QFont('Courier New', 20))
        self.label_graph_eeg.setText('EEG')
        self.label_graph_eeg.move(int(5 * self.scale), int(205 * self.scale))

        self.label_graph_ch2 = QLabel(self)
        self.label_graph_ch2.setFont(QtGui.QFont('Courier New', 20))
        self.label_graph_ch2.setText('Ch2')
        self.label_graph_ch2.move(int(5 * self.scale), int(405 * self.scale))

        self.font = QtGui.QFont()
        self.font.setPointSize(18)
        self.font.setBold(True)
        self.font.setWeight(75)

        self.listOfPredictionResults = []
        self.listOfGraphs = []
        self.listOfGraphs.append([])
        self.listOfGraphs.append([])

        for graphID in range(self.graphNum):

            self.listOfPredictionResults.append(PredictionResultLabel(self))
            predXLoc = (graphID * 300) + 125
            predYLoc = 90
            self.listOfPredictionResults[graphID].move(
                int(predXLoc * self.scale), int(predYLoc * self.scale))

            xLoc = (graphID * 300) + 50
            for chanID in range(2):
                yLoc = (chanID * 200) + 120
                self.listOfGraphs[chanID].append(
                    DynamicGraphCanvas(self,
                                       width=int(3 * self.scale),
                                       height=int(2 * self.scale),
                                       dpi=100))
                self.listOfGraphs[chanID][graphID].move(
                    int(xLoc * self.scale), int(yLoc * self.scale))

        self.setWindowTitle('Sleep stage classifier')
        xSize = self.graphNum * 310
        ySize = 550
        self.resize(int(xSize * self.scale), int(ySize * self.scale))
        self.show()
        self.activateWindow()
        statusbar = self.statusBar()
        self.readFromDaq = False
        try:
            if len(self.args) > 5:
                print('Too many arquments for running app.py.')
                quit()
            self.sleepTime = float(
                self.args[4]) if len(self.args) > 4 else self.defaultSleepTime
            self.offsetWindowID = int(
                self.args[3]) if len(self.args) > 3 else 0
            if len(self.args) > 1:
                optionID = self.args[1]
                if optionID == 'm':
                    classifierID = selectClassifierID(
                        self.params.finalClassifierDir, self.classifier_type)
                    self.inputFileID = self.args[2] if len(
                        self.args) > 2 else self.randomlySelectInputFileID()
                    print('demo mode: reading inputFileID=', self.inputFileID)
                elif optionID == 'o':
                    classifierID = selectClassifierID(
                        self.params.finalClassifierDir, self.classifier_type)
                    self.inputFileID = ''
                else:
                    classifierID = optionID
                    self.inputFileID = ''
                self.client = ClassifierClient(self.recordWaves,
                                               self.extractorType,
                                               self.classifierType,
                                               classifierID, self.inputFileID,
                                               self.offsetWindowID)
            else:  # Neither classifierID nor inputFileID are specified.
                self.readFromDaq = True
                classifierID = selectClassifierID(
                    self.params.finalClassifierDir, self.classifier_type)
                print(
                    'Data is read from DAQ. classifier ID is randomly selected.'
                )
                self.client = ClassifierClient(self.recordWaves,
                                               self.extractorType,
                                               self.classifierType,
                                               classifierID)
            self.client.hasGUI = True
            self.ylim_value_eeg_box.setText(str(self.client.ylim_max_eeg))
            self.ylim_value_ch2_box.setText(str(self.client.ylim_max_ch2))
            self.ylim_slider_eeg.setValue(int(self.client.ylim_max_eeg * 10))
            self.ylim_slider_ch2.setValue(int(self.client.ylim_max_ch2 * 10))

        except Exception as e:
            print('Exception in self.client = ...')
            statusbar.showMessage(str(e))
            raise e
Exemplo n.º 4
0
    def initUI(self):
        ### below commented out on 2020.2.24
        '''
        self.clientButton1chan = QtWidgets.QPushButton('Predict (EEG only)', self)
        self.clientButton1chan.setCheckable(True)
        # self.channelNumForPrediction = 1
        self.clientButton1chan.clicked.connect(self.predictionStateOnEEGonly)
        self.clientButton1chan.resize(self.clientButton1chan.sizeHint())
        self.clientButton1chan.move(5, 10)
        '''

        self.clientButton2chan = QtWidgets.QPushButton('Predict (EEG + Ch.2)', self)
        self.clientButton2chan.setCheckable(True)
        # self.channelNumForPrediction = 2
        self.clientButton2chan.clicked.connect(self.predictionStateOnEEGandCh2)
        self.clientButton2chan.resize(self.clientButton2chan.sizeHint())
        ### self.clientButton2chan.move(145, 10)
        self.clientButton2chan.move(5, 10)

        '''
        self.methodName = self.str_method_deep
        method_combobox = QtWidgets.QComboBox(self)
        method_combobox.addItem(self.str_method_deep)
        method_combobox.addItem(self.str_method_classical)
        method_combobox.addItem(self.str_method_rf)
        method_combobox.move(335, 10)
        method_combobox.activated[str].connect(self.method_choice)
        '''

        checkConnectionButton = QtWidgets.QPushButton('Check connection', self)
        checkConnectionButton.clicked.connect(self.check_connection)
        checkConnectionButton.resize(checkConnectionButton.sizeHint())
        checkConnectionButton.move(440, 10)

        '''
        self.nameLabel_eeg = QLabel(self)
        self.nameLabel_eeg.setText('EEG std:')
        self.nameLabel_eeg.move(10, 35)
        self.eeg_std = QLineEdit(self)
        self.eeg_std.move(65, 40)
        self.eeg_std.resize(30, 20)

        self.nameLabel_ch2 = QLabel(self)
        self.nameLabel_ch2.setText('Ch.2 std:')
        self.nameLabel_ch2.move(105, 35)
        self.ch2_std = QLineEdit(self)
        self.ch2_std.move(160, 40)
        self.ch2_std.resize(30, 20)
        '''

        self.nameLabel_ch2_mode_label = QLabel(self)
        self.nameLabel_ch2_mode_label.setText('Mode:')
        self.nameLabel_ch2_mode_label.move(10, 35)

        if self.params.useCh2ForReplace:
            self.ch2_mode = self.ch2_mode_str_video
        else:
            self.ch2_mode = self.ch2_mode_str_none
        self.ch2_mode_combobox = QtWidgets.QComboBox(self)
        self.ch2_mode_combobox.addItem(self.ch2_mode_str_video)
        self.ch2_mode_combobox.addItem(self.ch2_mode_str_emg)
        self.ch2_mode_combobox.addItem(self.ch2_mode_str_none)
        self.ch2_mode_combobox.move(50, 38)
        self.ch2_mode_combobox.activated[str].connect(self.ch2_mode_choice)

        self.nameLabel_ch2_thresh = QLabel(self)
        self.nameLabel_ch2_thresh.setText('Thresh for R->W:')
        self.nameLabel_ch2_thresh.move(160, 35)
        self.nameLabel_ch2_thresh.resize(125, 20)
        self.ch2_thresh = QLineEdit(self)
        self.ch2_thresh.setText(str(self.params.ch2_thresh_default))
        self.ch2_thresh.move(270, 40)
        self.ch2_thresh.resize(30, 20)

        self.ch2_thresh_slider = QSlider(Qt.Horizontal, self)
        self.ch2_thresh_slider.move(310, 35)
        self.ch2_thresh_slider.resize(220, 20)
        self.ch2_thresh_slider.setMinimum(-8)
        self.ch2_thresh_slider.setMaximum(16)
        self.ch2_thresh_slider.setValue(4)
        self.ch2_thresh_slider.setTickPosition(QSlider.TicksBelow)
        self.ch2_thresh_slider.setTickInterval(1)
        self.ch2_thresh_slider.valueChanged.connect(self.ch2_thresh_change)

        self.font = QtGui.QFont()
        self.font.setPointSize(18)
        self.font.setBold(True)
        self.font.setWeight(75)

        self.nameLabel_chi2 = QLabel(self)
        self.nameLabel_chi2.setText('KS chi2:')
        self.nameLabel_chi2.move(540, 35)
        self.nameLabel_chi2.setFont(self.font)
        self.chi2ResLabel = QLabel(self)
        self.chi2ResLabel.move(620, 35)
        self.chi2ResLabel.resize(70, 30)
        self.chi2ResLabel.setFont(self.font)

        self.dHist = DynamicGraphCanvas4KSHist(self, width=1.5, height=0.8, dpi=100)
        self.dHist.move(690, 5)

        self.dGraph = DynamicGraphCanvas4KS(self, width=4.5, height=0.8, dpi=100)
        self.dGraph.move(830, 5)

        self.nameLabel_d = QLabel(self)
        self.nameLabel_d.setText('d:')
        self.nameLabel_d.move(520, 60)
        self.nameLabel_d.setFont(self.font)
        self.dResLabel = QLabel(self)
        self.dResLabel.move(580, 60)
        self.dResLabel.resize(90, 30)
        self.dResLabel.setFont(self.font)

        self.waveRecordButton = QtWidgets.QPushButton(self.label_recordWaves, self)
        self.waveRecordButton.clicked.connect(self.toggleWaveRecord)
        self.waveRecordButton.resize(self.waveRecordButton.sizeHint())
        self.waveRecordButton.update()
        self.waveRecordButton.move(10, 60)
        self.waveRecordButton.setCheckable(True)

        self.waveNotRecordButton = QtWidgets.QPushButton(self.label_notRecordWaves, self)
        self.waveNotRecordButton.clicked.connect(self.toggleWaveNotRecord)
        self.waveNotRecordButton.resize(self.waveNotRecordButton.sizeHint())
        self.waveNotRecordButton.update()
        self.waveNotRecordButton.move(180, 60)
        self.waveNotRecordButton.setCheckable(True)
        if self.recordWaves:
            self.toggleWaveRecord()
        else:
            self.toggleWaveNotRecord()

        quitButton = QtWidgets.QPushButton('Quit Application', self)
        ### commented out on 2020.2.24
        ### quitButton.clicked.connect(self.stop_reader)
        quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)
        quitButton.resize(quitButton.sizeHint())
        quitButton.move(350, 60)

        self.listOfPredictionResults = []
        self.listOfGraphs = []
        self.listOfGraphs.append([])
        self.listOfGraphs.append([])

        for graphID in range(self.graphNum):

            self.listOfPredictionResults.append(PredictionResultLabel(self))
            predXLoc = (graphID * 300) + 110
            predYLoc = 100
            self.listOfPredictionResults[graphID].move(predXLoc, predYLoc)

            xLoc = (graphID * 300) + 10
            for chanID in range(2):
                yLoc = (chanID * 200) + 140
                self.listOfGraphs[chanID].append(DynamicGraphCanvas(self, width=3, height=2, dpi=100))
                self.listOfGraphs[chanID][graphID].move(xLoc, yLoc)

        self.setWindowTitle('Sleep Staging')
        xSize = self.graphNum * 310
        self.resize(xSize, 600)
        self.show()
        self.activateWindow()
        statusbar = self.statusBar()
        self.readFromDaq = False
        try:
            if len(args) > 5:
                print('Too many arquments for running app.py.')
                quit()
            self.sleepTime = float(args[4]) if len(args) > 4 else self.defaultSleepTime
            self.offsetWindowID = int(args[3]) if len(args) > 3 else 0
            self.inputFileID = args[2] if len(args) > 2 else self.randomlySelectInputFileID()
            if len(args) > 1:
                classifierID = args[1]
                if classifierID == 'm':
                    classifierID = self.randomlySelectClassifierID()
                self.client = ClassifierClient(self.recordWaves, self.extractorType, self.classifierType, classifierID, self.inputFileID, self.offsetWindowID)
            else:   # Neither classifierID nor inputFileID are specified.
                self.readFromDaq = True
                classifierID = self.randomlySelectClassifierID()
                print('Data is read from DAQ. classifier ID is randomly selected.')
                self.client = ClassifierClient(self.recordWaves, self.extractorType, self.classifierType, classifierID)
            self.client.hasGUI = True
        except Exception as e:
            print('Exception in self.client = ...')
            statusbar.showMessage(str(e))
            raise e
Exemplo n.º 5
0
class RemApplication(QMainWindow):
    """
    GUIアプリ

    以下の二つのクラスを管理しています
    - ClassifierClient (in classifierClient.py)
    - ReadDAQServer (in readDaqServer.py)

    threadingモジュールを用いて上記のインスタンスの
    メソッドをバックグラウンドで実行しています
    """
    def __init__(self, host, port):

        # set parameters
        self.label_recordWaves = 'Record waves'
        self.label_notRecordWaves = 'Do not record waves'
        # self.current_label_for_record_waves = self.label_recordWaves
        # self.recordWaves = False
        self.current_label_for_record_waves = self.label_notRecordWaves
        self.recordWaves = True

        super(RemApplication, self).__init__()
        self.initUI()

        self.t = threading.Thread(target=self.run, daemon=True)

    def start_single_client(self):
        def to_f(inpt):
            try:
                return float(inpt)

            except Exception:
                return None

        eeg_std = to_f(self.eeg_std.text())
        emg_std = to_f(self.emg_std.text())

        statusbar = self.statusBar()

        try:
            self.client = ClassifierClient(host, port, 'single')

        except Exception as e:
            statusbar.showMessage(str(e))
            raise e

        try:
            self.server = ReadDAQServer(host,
                                        port,
                                        self.recordWaves,
                                        eeg_std=eeg_std,
                                        emg_std=emg_std,
                                        channelOpt='single')

        except Exception as e:
            statusbar.showMessage(str(e))
            raise e

        self.t.start()
        self.client.setGraph1chan(self.graph1chan)
        self.client.setGraph2chan(self.graph2chan)
        self.client.setPredictionLamp(self.predictionLamp)
        message = 'successfully started ! eeg_std is {}, and emg_std is {}'
        statusbar.showMessage(message.format(eeg_std, emg_std))

    def start_double_client(self):
        def to_f(inpt):
            try:
                return float(inpt)

            except Exception:
                return None

        eeg_std = to_f(self.eeg_std.text())
        emg_std = to_f(self.emg_std.text())

        statusbar = self.statusBar()

        try:
            self.client = ClassifierClient(host, port, 'double')

        except Exception as e:
            statusbar.showMessage(str(e))
            raise e

        try:
            self.server = ReadDAQServer(host,
                                        port,
                                        self.recordWaves,
                                        eeg_std=eeg_std,
                                        emg_std=emg_std,
                                        channelOpt='double')

        except Exception as e:
            statusbar.showMessage(str(e))
            raise e

        self.t.start()
        self.client.setGraph1chan(self.graph1chan)
        self.client.setGraph2chan(self.graph2chan)
        self.client.setPredictionLamp(self.predictionLamp)
        message = 'successfully started ! eeg_std is {}, and emg_std is {}'
        statusbar.showMessage(message.format(eeg_std, emg_std))

    def run(self):
        print('start client')
        self.client.run()
        print('start server')
        self.server.run()

    def toggleWaveRecord(self):
        if self.recordWaves:
            self.recordWaves = False
            self.current_label_for_record_waves = self.label_recordWaves
        else:
            self.recordWaves = True
            self.current_label_for_record_waves = self.label_notRecordWaves
        self.waveRecordToggleButton.setText(
            self.current_label_for_record_waves)
        self.waveRecordToggleButton.update()
        self.waveRecordToggleButton.resize(
            self.waveRecordToggleButton.sizeHint())
        self.show()

    def initUI(self):
        clientButton1chan = QtGui.QPushButton('Start (EEG only)', self)
        clientButton1chan.clicked.connect(self.start_single_client)
        clientButton1chan.resize(clientButton1chan.sizeHint())
        clientButton1chan.move(5, 10)

        clientButton2chan = QtGui.QPushButton('Start (EEG + EMG)', self)
        clientButton2chan.clicked.connect(self.start_double_client)
        clientButton2chan.resize(clientButton2chan.sizeHint())
        clientButton2chan.move(145, 10)

        self.nameLabel = QLabel(self)
        self.nameLabel.setText('EEG std:')
        self.nameLabel.move(10, 40)
        self.eeg_std = QLineEdit(self)
        self.eeg_std.move(80, 40)
        self.eeg_std.resize(140, 20)

        self.nameLabel = QLabel(self)
        self.nameLabel.setText('EMG std:')
        self.nameLabel.move(240, 40)
        self.emg_std = QLineEdit(self)
        self.emg_std.move(300, 40)
        self.emg_std.resize(140, 20)

        self.waveRecordToggleButton = QtGui.QPushButton(
            self.current_label_for_record_waves, self)
        self.waveRecordToggleButton.clicked.connect(self.toggleWaveRecord)
        self.waveRecordToggleButton.resize(
            self.waveRecordToggleButton.sizeHint())
        self.waveRecordToggleButton.move(10, 60)

        quitButton = QtGui.QPushButton('Quit Application', self)
        quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)
        quitButton.resize(quitButton.sizeHint())
        quitButton.move(180, 60)

        self.predictionLamp = LampContainer(self)
        self.predictionLamp.move(180, 110)

        self.graph1chan = DynamicGraphCanvas(self, width=4, height=2, dpi=100)
        self.graph1chan.move(20, 140)
        self.graph2chan = DynamicGraphCanvas(self, width=4, height=2, dpi=100)
        self.graph2chan.move(20, 350)

        self.setWindowTitle('Sleep Staging')
        self.resize(600, 800)
        self.show()
        self.activateWindow()
Exemplo n.º 6
0
    def start(self):
        channelOpt = 1
        params = ParameterSetup()
        self.recordWaves = params.writeWholeWaves
        self.extractorType = params.extractorType
        self.classifierType = params.classifierType
        self.postDir = params.postDir
        self.predDir = params.predDir
        self.finalClassifierDir = params.finalClassifierDir
        self.samplingFreq = params.samplingFreq

        # eegFilePath = args[1]
        # inputFileID = splitext(split(eegFilePath)[1])[0]
        postFiles = listdir(self.postDir)
        fileCnt = 0
        for inputFileName in postFiles:
            if not inputFileName.startswith('.'):
                print('inputFileName = ' + inputFileName)
                inputFileID = splitext(inputFileName)[0]
                print('inputFileID = ' + inputFileID)
                predFileFullPath = self.predDir + '/' + inputFileID + '_pred.txt'
                print('predFileFullPath = ' + predFileFullPath)

                if not isfile(predFileFullPath):
                    fileCnt += 1
                    print('  processing ' + inputFileID)
                    try:
                        classifierID = selectClassifierID(
                            self.finalClassifierDir, self.classifier_type)
                        if len(self.args) > 1:
                            if self.args[1] == '--output_the_same_fileID':
                                self.client = ClassifierClient(
                                    self.recordWaves,
                                    self.extractorType,
                                    self.classifierType,
                                    classifierID,
                                    inputFileID=inputFileID)
                            else:
                                self.client = ClassifierClient(
                                    self.recordWaves, self.extractorType,
                                    self.classifierType, classifierID)
                        else:
                            self.client = ClassifierClient(
                                self.recordWaves, self.extractorType,
                                self.classifierType, classifierID)
                        self.client.predictionStateOn()
                        self.client.hasGUI = False
                        # sys.stdout.write('classifierClient started by ' + str(channelOpt) + ' channel.')

                    except Exception as e:
                        print(str(e))
                        raise e

                    try:
                        eegFilePath = self.postDir + '/' + inputFileName
                        self.server = EEGFileReaderServer(
                            self.client,
                            eegFilePath,
                            samplingFreq=self.samplingFreq)

                    except Exception as e:
                        print(str(e))
                        raise e

                else:
                    print('  skipping ' + inputFileID + ' because ' +
                          predFileFullPath + ' exists.')