def test_detect(self):
        eword_size = 2
        ewindow_factor = 2
        elead_window_factor = 2
        elag_window_factor = 2

        inst = AssumptionFreeAA(word_size=eword_size,
                                window_factor=ewindow_factor,
                                lead_window_factor=elead_window_factor,
                                lag_window_factor=elag_window_factor)

        data = np.array([1, 2, 2, 2, 4, 7, 8, 9, 1, 9, 5, 6, 7, 3, 2, 6])
        escore = 0.125
        ebitmp1 = np.array([[0., 0., 0., 0.], [0., 0., 1., 0.],
                            [0., 1., 0., 0.], [0., 0., 0., 0.]])
        ebitmp2 = np.array([[0., 0., 0., 1.], [0., 0., 1., 0.],
                            [0., 0., 0., 0.], [0., 0., 0., 0.]])
        actual = inst.detect(data)[0]
        self.assertEqual(escore, actual.score,
                         'exp: %s; act: %s' % (escore, actual.score))
        self.assertItemsEqual(
            ebitmp1[0], actual.bitmp1[0],
            'exp: %s; act: %s' % (ebitmp1[0], actual.bitmp1[0]))
        self.assertItemsEqual(
            ebitmp1[1], actual.bitmp1[1],
            'exp: %s; act: %s' % (ebitmp1[1], actual.bitmp1[1]))
        self.assertItemsEqual(
            ebitmp2[0], actual.bitmp2[0],
            'exp: %s; act: %s' % (ebitmp2[0], actual.bitmp2[0]))
        self.assertItemsEqual(
            ebitmp2[1], actual.bitmp2[1],
            'exp: %s; act: %s' % (ebitmp2[1], actual.bitmp2[1]))
Пример #2
0
    def test_detect(self):
        eword_size = 2
        ewindow_factor = 2
        elead_window_factor = 2
        elag_window_factor = 2

        inst = AssumptionFreeAA(word_size=eword_size,
                                window_factor=ewindow_factor,
                                lead_window_factor=elead_window_factor,
                                lag_window_factor=elag_window_factor)

        data = np.array([1, 2, 2, 2, 4, 7, 8, 9, 1, 9, 5, 6, 7, 3, 2, 6])
        escore = 0.125
        ebitmp1 = np.array([[0.,  0.,  0.,  0.],
                            [0.,  0.,  1.,  0.],
                            [0.,  1.,  0.,  0.],
                            [0.,  0.,  0.,  0.]])
        ebitmp2 = np.array([[0.,  0.,  0.,  1.],
                            [0.,  0.,  1.,  0.],
                            [0.,  0.,  0.,  0.],
                            [0.,  0.,  0.,  0.]])
        actual = inst.detect(data)[0]
        self.assertEqual(escore, actual.score,
                         'exp: %s; act: %s' % (escore, actual.score))
        self.assertItemsEqual(ebitmp1[0], actual.bitmp1[0],
                         'exp: %s; act: %s' % (ebitmp1[0], actual.bitmp1[0]))
        self.assertItemsEqual(ebitmp1[1], actual.bitmp1[1],
                         'exp: %s; act: %s' % (ebitmp1[1], actual.bitmp1[1]))
        self.assertItemsEqual(ebitmp2[0], actual.bitmp2[0],
                         'exp: %s; act: %s' % (ebitmp2[0], actual.bitmp2[0]))
        self.assertItemsEqual(ebitmp2[1], actual.bitmp2[1],
                         'exp: %s; act: %s' % (ebitmp2[1], actual.bitmp2[1]))
class AssumptionFreeDataTests():
    @classmethod
    def setUpClass(cls):
        """
        Reads a test data set into memory
        """
        dirname = os.path.dirname(__file__)
        filename = os.path.join(dirname, 'dataset.dat')
        cls._df = extract_hr_acc(read_data(filename))

    def setUp(self,
              window_size=1000, lead_window_factor=3,
              lag_window_factor=30, word_size=10,
              recursion_level=2, resampling_rate='10ms',
              resampling_method='mean'):
        """
        Copies the data set to an test's instance variable
        and instantiates an anomaly detector.
        """
        self._data = self._df.resample(rule=resampling_rate,
                                      how=resampling_method)
        # fill NAs forward
        self._data = self._data.fillna(method='ffill')
        # fill NAs backwards (fill any NAs at the start of all series)
        self._data = self._data.fillna(method='bfill')
        self._instance = AssumptionFreeAA(window_size=window_size,
                                         lead_window_factor=lead_window_factor,
                                         lag_window_factor=lag_window_factor,
                                         word_size=word_size,
                                         recursion_level=recursion_level)

    def tearDown(self):
        """
        Deletes instance's data.
        """
        del self._data
        del self._instance
        gc.collect()

    def runTest(self):
        self._scores = []
        self._bitmaps = []
        for i in xrange(len(self._data)):
            if np.isnan(self._data.ratio[i]):
                print self._data.ix[i]
                sys.exit()
            result = self._instance.detect([self._data.ratio[i]])
            if len(result) != 0:
                self._scores.append(result[0].score)
                self._bitmaps.append((result[0].bitmp1, result[0].bitmp2))
            else:
                self._scores.append(0.)
                self._bitmaps.append(([], []))
    def test_detect_not_enough_elements(self):
        eword_size = 2
        ewindow_factor = 2
        elead_window_factor = 2
        elag_window_factor = 2

        inst = AssumptionFreeAA(word_size=eword_size,
                                window_factor=ewindow_factor,
                                lead_window_factor=elead_window_factor,
                                lag_window_factor=elag_window_factor)

        data = np.array([1, 2, 2, 2, 4, 7, 8, 9, 1, 9])
        elen_score = 0
        actual = inst.detect(data)
        self.assertEqual(elen_score, len(actual),
                         'exp: %s; act: %s' % (elen_score, len(actual)))
Пример #5
0
    def test_detect_not_enough_elements(self):
        eword_size = 2
        ewindow_factor = 2
        elead_window_factor = 2
        elag_window_factor = 2

        inst = AssumptionFreeAA(word_size=eword_size,
                                window_factor=ewindow_factor,
                                lead_window_factor=elead_window_factor,
                                lag_window_factor=elag_window_factor)

        data = np.array([1, 2, 2, 2, 4, 7, 8, 9, 1, 9])
        elen_score = 0
        actual = inst.detect(data)
        self.assertEqual(elen_score, len(actual),
                         'exp: %s; act: %s' % (elen_score, len(actual)))