class ProcessedSignalPlotter(RawSignalPlotter):
    def __init__(self, person, eegData, signals, filePath, save=True, plot=True, logScale=False):
        RawSignalPlotter.__init__(self, person, eegData, signals, filePath, save, plot, logScale, name="processed")
        self.pre = SignalPreProcessor()
        self.chain = SignalProcessor()

    def _getData(self, signal):
        raw = self.eegData.getColumn(signal)
        qual = self.eegData.getQuality(signal)
        proc = self.pre.process(raw)
        proc, _ = self.chain.process(proc, qual)
        return proc
 def __init__(self, person, filePath, signals=None, save=True, plot=True, logScale=False):
     self.person = person
     self.filePath = filePath
     self._initStatsDict()
     self._readData()
     self._initSignals(signals)
     self.su = SignalUtil()
     self.qu = QualityUtil()
     self._initFields()
     self.save = save
     self._initPlotter(person, plot, logScale)
     self.ssPrint = SignalStatisticPrinter(person)
     self.preProcessor = SignalPreProcessor()
예제 #3
0
    def __init__(self, inputQueue, outputQueue):
        config = ConfigProvider()
        self.eegFields = config.getEmotivConfig()["eegFields"]
        self.gyroFields = config.getEmotivConfig()["gyroFields"]
        self.samplingRate = config.getEmotivConfig()["samplingRate"]
        
        self.processingConfig = config.getProcessingConfig()
        self.preProcessor = SignalPreProcessor()
        self.signalProcessor = SignalProcessor()
        self.fftProcessor = FFTProcessor()

        self.inputQueue = inputQueue
        self.outputQueue = outputQueue
        self.runProcess = True
        self.totalInvalid = 0
        self.totalCount = 0
class DistributionSignalPlotter(AbstractSignalPlotter):

    def __init__(self, person, eegData, signals, filePath, save=True, plot=True, logScale=False):
        AbstractSignalPlotter.__init__(self, "distribution", person, eegData, signals, filePath, save, plot, logScale)
        self.preProcessor = SignalPreProcessor()

    def doPlot(self):
        if self._shouldNotPlot():
            return

        fig, axes = self._initPlot()
        fig.canvas.set_window_title(self.title)

        for x, signal in enumerate(self.signals):
            self._plotSignal(signal, axes, x)

        self._configurePlot()

        self.savePlot()
        self.showPlot()
        print "plotting done"

    def _initPlot(self):
        signalCount = self._calcSignalCount()

        fig, axes = plt.subplots(2, signalCount, figsize=self.figsize, dpi=80, sharex=False, sharey=True)
        return fig, axes

    def _plotSignal(self, signal, axes, x):
        raw = self.eegData.getColumn(signal)
        proc = self.preProcessor.process(raw)
        quality = self.eegData.getQuality(signal)

        rawAx = axes[0, x]
        rawAx.xaxis.set_label_position("top")
        if self.logScale:
            rawAx.set_yscale("log")
        # rug=True causes serious performance issues
        sns.distplot(proc, color="b", axlabel=signal, ax=rawAx, bins=30, kde=False)

        qualAx = axes[1, x]
        sns.distplot(quality, color="g", ax=qualAx, bins=15, kde=False)
예제 #5
0
class DataProcessor(object):
    
    def __init__(self, inputQueue, outputQueue):
        config = ConfigProvider()
        self.eegFields = config.getEmotivConfig()["eegFields"]
        self.gyroFields = config.getEmotivConfig()["gyroFields"]
        self.samplingRate = config.getEmotivConfig()["samplingRate"]
        
        self.processingConfig = config.getProcessingConfig()
        self.preProcessor = SignalPreProcessor()
        self.signalProcessor = SignalProcessor()
        self.fftProcessor = FFTProcessor()

        self.inputQueue = inputQueue
        self.outputQueue = outputQueue
        self.runProcess = True
        self.totalInvalid = 0
        self.totalCount = 0

    def close(self):
        self.runProcess = False

    def processData(self):
        while self.runProcess:
            try:
                data = self.inputQueue.get(timeout=1)
                procData, procInvalid = self.process(data)
                if not procInvalid:
                    self.outputQueue.put(procData)
            except Empty:
                pass

    def process(self, data):
        #TODO make me fast and nice
        eegRaw, gyroRaw = self.splitData(data)
        eegProc, eegInvalid = self.processEEGData(eegRaw)
        gyroProc, gyroIvalid = self.processGyroData(gyroRaw)
        eegProc.update(gyroProc)
        return eegProc, (eegInvalid or gyroIvalid)

    def splitData(self, data):
        '''split eeg and gyro data
        
        :param data: all values as dict
        
        :return: 
            eegData: eeg values as dict
            gyroData: gyro values as dict
        '''
        #TODO handle data except eeg and gyro?
        eegData = {x: data[x] for x in data if x in self.eegFields}
        gyroData = {x: data[x] for x in data if x in self.gyroFields}
        return eegData, gyroData

    def processEEGData(self, eegData):

        invalidCount = 0
        for _, signal in eegData.iteritems():
            raw = array(signal["value"])
            quality = array(signal["quality"])

            proc = self.preProcessor.process(raw)
            proc, _ = self.signalProcessor.process(proc, quality)

            chan, fInvalid = self.fftProcessor.process(proc)
            signal["theta"] = chan["theta"]
            invalidCount += sum([fInvalid])
        if invalidCount > 0:
            self.totalInvalid += 1
        self.totalCount += 1
        return eegData, (invalidCount > 0)

    def processGyroData(self, gyroData):
        return gyroData, False
 def __init__(self, person, eegData, signals, filePath, save=True, plot=True, logScale=False):
     AbstractSignalPlotter.__init__(self, "distribution", person, eegData, signals, filePath, save, plot, logScale)
     self.preProcessor = SignalPreProcessor()
class SignalStatisticUtil(object):
    '''
    class to show some statistical values for a channel
    '''

    def __init__(self, person, filePath, signals=None, save=True, plot=True, logScale=False):
        self.person = person
        self.filePath = filePath
        self._initStatsDict()
        self._readData()
        self._initSignals(signals)
        self.su = SignalUtil()
        self.qu = QualityUtil()
        self._initFields()
        self.save = save
        self._initPlotter(person, plot, logScale)
        self.ssPrint = SignalStatisticPrinter(person)
        self.preProcessor = SignalPreProcessor()

    def _initStatsDict(self):
        self.stats = OrderedDict()
        self.stats[GENERAL_KEY] = OrderedDict()
        self.stats[SIGNALS_KEY] = OrderedDict()

    def _readData(self):
        self.reader = EEGTableFileUtil()
        self.eegData = self.reader.readFile(self.filePath)

    def _initSignals(self, signals):
        if not signals:
            signals = ConfigProvider().getEmotivConfig().get("eegFields")
        self.signals = signals

    def _initFields(self):
        self.statFields = STAT_FIELDS
        self.statFields["max"][METHOD] = self.su.maximum 
        self.statFields["min"][METHOD] = self.su.minimum
        self.statFields["mean"][METHOD] = self.su.mean
        self.statFields["std"][METHOD] = self.su.std
        self.statFields["var"][METHOD] = self.su.var
        self.statFields["zeros"][METHOD] = self.qu.countZeros
        self.statFields["seq"][METHOD] = self.qu.countSequences
        self.statFields["out"][METHOD] = self.qu.countOutliners

    def _initPlotter(self, person, plot, logScale):
        self.plotter = []
        for clazz in PLOTTER:
            plotter = clazz(person, self.eegData, self.signals, self.filePath, self.save, plot, logScale)
            thread = multiprocessing.Process(target=plotter.doPlot)
            self.plotter.append(thread)


    def main(self):
        self.doPlot()

        self.collect_stats()
        self.printStats()

    def doPlot(self):
        for thread in self.plotter:
            thread.start()

    def collect_stats(self):
        self.collectGeneralStats()
        for signal in self.signals:
            self.stats[SIGNALS_KEY][signal] = {}
            self.collectRawStats(signal)
            self.collectQualityStats(signal)

    def collectGeneralStats(self):
        self._addGeneralStatValue("file path", self.filePath)
        self._addGeneralStatValue("sampleRate", ("%f.2" % self.eegData.getSamplingRate()))
        self._addGeneralStatValue("dataLength", ("%d" % self.eegData.len))
        self._addGeneralStatValue("bound", ("%d - %d" % (self.qu.lowerBound, self.qu.upperBound)))

        self._addGeneralTimeStat("start time", "getStartTime", TIME_FORMAT_STRING)
        self._addGeneralTimeStat("end time", "getEndTime", TIME_FORMAT_STRING)
        self._addGeneralTimeStat("duration", "getDuration", DURATION_FORMAT_STRING)

    def _addGeneralTimeStat(self, name, method, formatString):
        time = getattr(self.eegData, method)()
        value = self._buildFormattedTime(time, formatString)
        self._addGeneralStatValue(name, value)

    def _buildFormattedTime(self, time, formatString):
        value = datetime.fromtimestamp(time).strftime(formatString)
        return value
    
    def _addGeneralStatValue(self, name, value):
        self.stats[GENERAL_KEY][name] = value

    def collectRawStats(self, signal):
        data = self.eegData.getColumn(signal)
        proc = self.preProcessor.process(data)
        self._collectSignalStat(signal, RAW_KEY, proc)

    def collectQualityStats(self, signal):
        data = self.eegData.getQuality(signal)
        self._collectSignalStat(signal, QUALITY_KEY, data)

    def _collectSignalStat(self, signal, category, data):
        self.stats[SIGNALS_KEY][signal][category] = OrderedDict()
        for field, attributes in self.statFields.iteritems():
            self._addSignalStat(signal, category, field, attributes["method"], data, 0)

    def _addSignalStat(self, signal, category, name, method, raw, decPlace=2):
        value = ("%." + str(decPlace) + "f") % method(raw)
        self._addSignalStatValue(signal, category, name, value)

    def _addSignalStatValue(self, signal, category, name, value):
        self.stats[SIGNALS_KEY][signal][category][name] = value

    def setStats(self, stats):
        self.stats = stats

    def printStats(self):
        content = self.ssPrint.getSignalStatsString(self.stats)
        print content
        if self.save:
            filePath = getNewFileName(self.filePath, "txt", "_stats")
            self.ssPrint.saveStats(filePath, content)
 def __init__(self, person, eegData, signals, filePath, save=True, plot=True, logScale=False):
     RawSignalPlotter.__init__(self, person, eegData, signals, filePath, save, plot, logScale, name="processed")
     self.pre = SignalPreProcessor()
     self.chain = SignalProcessor()