Example #1
0
class SignalProcessor(object):
    def __init__(self, verbose=False):
        config = ConfigProvider().getProcessingConfig()
        self.maxNaNValues = config.get("maxNaNValues")
        self.lowerFreq = config.get("lowerFreq")
        self.upperFreq = config.get("upperFreq")
        self.samplingRate = ConfigProvider().getEmotivConfig().get("samplingRate")
        self.qualUtil = QualityUtil()
        self.sigUtil = SignalUtil()
        self.verbose = verbose

    def process(self, raw, quality):
        raw = self._replaceBadQuality(raw, quality)
        raw = self._replaceSequences(raw)
        raw = self._replaceOutliners(raw)
        raw = self._normalize(raw)
        invalid = self.qualUtil.isInvalidData(raw)
        return raw, invalid

    def _replaceBadQuality(self, raw, quality):
        if self.verbose:
            print "badQuality: %d" % self.qualUtil.countBadQuality(raw, quality)
        raw = self.qualUtil.replaceBadQuality(raw, quality, NaN)
        self._printNaNCount(raw)
        return raw

    def _replaceSequences(self, raw):
        if self.verbose:
            print "sequences: %d" % self.qualUtil.countSequences(raw)
        raw = self.qualUtil.replaceSequences(raw)
        self._printNaNCount(raw)
        return raw

    def _replaceOutliners(self, raw):
        if self.verbose:
            print "outliners: %d" % self.qualUtil.countOutliners(raw)
        raw = self.qualUtil.replaceOutliners(raw, NaN)
        self._printNaNCount(raw)
        return raw

    def _normalize(self, raw):
        if self.verbose:
            print "normalize: min %.2f max %.2f" % (self.sigUtil.minimum(raw),self.sigUtil.maximum(raw)) 
        raw = self.sigUtil.normalize(raw)
        if self.verbose:
            print "normalize: min %.2f max %.2f" % (self.sigUtil.minimum(raw),self.sigUtil.maximum(raw)) 
        self._printNaNCount(raw)
        return raw

    def _printNaNCount(self, raw):
        if self.verbose:
            print "NaN count: %s" % self.qualUtil.countNans(raw)
Example #2
0
class TestSignalUtil(unittest.TestCase):

    def setUp(self):
        self.util = SignalUtil()

    def test_normalize(self):
        testList = np.array([0, -5, 1, 10])
        normList = self.util.normalize(testList)
        self.assertEqual(len(testList), len(normList))
        self.assertTrue(max(normList) <= 1)
        self.assertTrue(min(normList) >= -1)

    def test_normalize_value(self):
        norm = ConfigProvider().getProcessingConfig().get("normalize")
        testList = np.array([0, -5, 1, 10])
        normList = self.util.normalize(testList, norm)
        self.assertEqual(len(testList), len(normList))
        self.assertItemsEqual(normList, testList / norm)

    def test_normalize_zero(self):
        testList = np.array([0, 0, 0, 0])
        normList = self.util.normalize(testList)
        self.assertEqual(len(testList), len(normList))
        self.assertTrue(max(normList) <= 1)
        self.assertTrue(min(normList) >= -1)
        self.assertTrue(sameEntries(testList, normList))

    def test_normalize_NaN(self):
        testList = np.array([np.NaN, -2, -1, 0, np.NaN, 1, 2, np.NaN])
        normList = self.util.normalize(testList)
        self.assertEqual(len(testList), len(normList))
        self.assertTrue(np.nanmax(normList) <= 1)
        self.assertTrue(np.nanmin(normList) >= -1)

    def test_energy(self):
        testList = np.array([1, 2, 3, 4])
        energy = self.util.energy(testList)
        self.assertEqual(energy, 30)

    def test_maximum(self):
        testList = np.array([-5, 1, 2, 3, 4])
        maximum = self.util.maximum(testList)
        self.assertEqual(maximum, 4)

    def test_minimum(self):
        testList = np.array([-5, 1, 2, 3, 6])
        minimum = self.util.minimum(testList)
        self.assertEqual(minimum, -5)

    def test_mean(self):
        testList = np.array([0, 1, 2, 3, 4])
        mean = self.util.mean(testList)
        self.assertEqual(mean, 2)
    
    def test_var(self):
        testList = np.array([0, 1, 2, 3, 4])
        var = self.util.var(testList)
        self.assertEqual(var, 2)

    def test_std(self):
        testList = np.array([0, 1, 2, 3, 4])
        std = self.util.std(testList)
        self.assertEqual(std, sqrt(self.util.var(testList)))

    def test_zcr(self):
        testList = np.array([1, -1, 1, -1, 1])
        zcr = self.util.zcr(testList)
        self.assertEqual(zcr, 4)

    def test_zcr_zeros(self):
        testList = np.array([0, 0, 0, 0, 0])
        zcr = self.util.zcr(testList)
        self.assertEqual(zcr, 0)

        testList = np.array([1, 0, -1, 0, 1, 0, -1])
        zcr = self.util.zcr(testList)
        self.assertEqual(zcr, 3)

    def test_zcr_zeroChanges(self):
        testList = np.array([1, 1, 1, 1, 1])
        zcr = self.util.zcr(testList)
        self.assertEqual(zcr, 0)

        testList = np.array([-1, -1, -1, -1, -1])
        zcr = self.util.zcr(testList)
        self.assertEqual(zcr, 0)

    def test_nan_onOtherFunctions(self):
        norm = self.util.normalize(TEST_DATA_NAN)
        self.assertItemsEqual(np.isnan(norm), np.isnan(TEST_DATA_NAN))
        maxi = self.util.maximum(TEST_DATA_NAN)
        self.assertTrue(np.isnan(maxi))
        mini = self.util.minimum(TEST_DATA_NAN)
        self.assertTrue(np.isnan(mini))
        mean = self.util.mean(TEST_DATA_NAN)
        self.assertTrue(np.isnan(mean))
        var = self.util.var(TEST_DATA_NAN)
        self.assertTrue(np.isnan(var))
        std = self.util.std(TEST_DATA_NAN)
        self.assertTrue(np.isnan(std))
        energy = self.util.energy(TEST_DATA_NAN)
        self.assertTrue(np.isnan(energy))
        zcr = self.util.zcr(TEST_DATA_NAN)
        self.assertTrue(np.isnan(zcr))

    def test_mixed_onOtherFunctions(self):
        norm = self.util.normalize(TEST_DATA_MIXED)
        self.assertItemsEqual(np.isnan(norm), np.isnan(TEST_DATA_MIXED))
        maxi = self.util.maximum(TEST_DATA_MIXED)
        self.assertEquals(maxi, 1.0)
        mini = self.util.minimum(TEST_DATA_MIXED)
        self.assertEquals(mini, 0.0)
        mean = self.util.mean(TEST_DATA_MIXED)
        self.assertEquals(mean, 0.5)
        var = self.util.var(TEST_DATA_MIXED)
        self.assertEquals(var, 0.25)
        std = self.util.std(TEST_DATA_MIXED)
        self.assertEquals(std, 0.5)
        energy = self.util.energy(TEST_DATA_MIXED)
        self.assertEquals(energy, 2.0)
        zcr = self.util.zcr(TEST_DATA_MIXED)
        self.assertEquals(zcr, 0)