Exemple #1
0
    def test_at_least_N(self):

        # With a window_length of K, AtLeastN should return 1
        # if N or more 1's exist in the lookback window

        # This smoothing filter gives customizable "stickiness"

        data = array(
            [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0],
             [1, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 0, 0, 0, 0],
             [1, 0, 0, 0, 0, 0]],
            dtype=bool)

        expected_1 = array([[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1],
                            [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 0, 0]],
                           dtype=bool)

        expected_2 = array([[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0],
                            [1, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0]],
                           dtype=bool)

        expected_3 = array([[1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 0, 0],
                            [1, 1, 1, 0, 0, 0], [1, 1, 0, 0, 0, 0]],
                           dtype=bool)

        expected_4 = array([[1, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0],
                            [1, 1, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0]],
                           dtype=bool)

        class Input(Filter):
            inputs = ()
            window_length = 0

        all_but_one = AtLeastN(inputs=[Input()], window_length=4, N=3)

        all_but_two = AtLeastN(inputs=[Input()], window_length=4, N=2)

        any_equiv = AtLeastN(inputs=[Input()], window_length=4, N=1)

        all_equiv = AtLeastN(inputs=[Input()], window_length=4, N=4)

        results = self.run_graph(
            TermGraph({
                'AllButOne': all_but_one,
                'AllButTwo': all_but_two,
                'AnyEquiv': any_equiv,
                'AllEquiv': all_equiv,
                'Any': Any(inputs=[Input()], window_length=4),
                'All': All(inputs=[Input()], window_length=4)
            }),
            initial_workspace={Input(): data},
            mask=self.build_mask(ones(shape=data.shape)),
        )

        check_arrays(results['Any'], expected_1)
        check_arrays(results['AnyEquiv'], expected_1)
        check_arrays(results['AllButTwo'], expected_2)
        check_arrays(results['AllButOne'], expected_3)
        check_arrays(results['All'], expected_4)
        check_arrays(results['AllEquiv'], expected_4)
Exemple #2
0
    def test_at_least_N(self):

        # With a window_length of K, AtLeastN should return 1
        # if N or more 1's exist in the lookback window

        # This smoothing filter gives customizable "stickiness"

        data = np.array(
            [
                [1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 0, 0],
                [1, 1, 1, 0, 0, 0],
                [1, 1, 0, 0, 0, 0],
                [1, 0, 0, 0, 0, 0],
            ],
            dtype=bool,
        )

        expected_1 = np.array(
            [
                [1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 0, 0],
            ],
            dtype=bool,
        )

        expected_2 = np.array(
            [
                [1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 0, 0],
                [1, 1, 1, 0, 0, 0],
            ],
            dtype=bool,
        )

        expected_3 = np.array(
            [
                [1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 0, 0],
                [1, 1, 1, 0, 0, 0],
                [1, 1, 0, 0, 0, 0],
            ],
            dtype=bool,
        )

        expected_4 = np.array(
            [
                [1, 1, 1, 1, 0, 0],
                [1, 1, 1, 0, 0, 0],
                [1, 1, 0, 0, 0, 0],
                [1, 0, 0, 0, 0, 0],
            ],
            dtype=bool,
        )

        class Input(Filter):
            inputs = ()
            window_length = 0

        all_but_one = AtLeastN(inputs=[Input()], window_length=4, N=3)

        all_but_two = AtLeastN(inputs=[Input()], window_length=4, N=2)

        any_equiv = AtLeastN(inputs=[Input()], window_length=4, N=1)

        all_equiv = AtLeastN(inputs=[Input()], window_length=4, N=4)

        self.check_terms(
            terms={
                "AllButOne": all_but_one,
                "AllButTwo": all_but_two,
                "AnyEquiv": any_equiv,
                "AllEquiv": all_equiv,
                "Any": Any(inputs=[Input()], window_length=4),
                "All": All(inputs=[Input()], window_length=4),
            },
            expected={
                "Any": expected_1,
                "AnyEquiv": expected_1,
                "AllButTwo": expected_2,
                "AllButOne": expected_3,
                "All": expected_4,
                "AllEquiv": expected_4,
            },
            initial_workspace={Input(): data},
            mask=self.build_mask(np.ones(shape=data.shape)),
        )