def test_any(self): # FUN FACT: The inputs and outputs here are exactly the negation of # the inputs and outputs for test_all above. This isn't a coincidence. # # By de Morgan's Laws, we have:: # # ~(a & b) == (~a | ~b) # # negating both sides, we have:: # # (a & b) == ~(a | ~b) # # Since all(a, b) is isomorphic to (a & b), and any(a, b) is isomorphic # to (a | b), we have:: # # all(a, b) == ~(any(~a, ~b)) # data = array([[0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1]], dtype=bool) # With a window_length of N, 1's should be "sticky" for the (N - 1) # days after the 1 in the base data. # Note that, the way ``self.run_graph`` works, we compute the same # number of output rows for all inputs, so we only get the last 4 # outputs for expected_3 even though we have enought input data to # compute 5 rows. expected_3 = array([[1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0], [0, 0, 0, 1, 1, 1]], dtype=bool) expected_4 = array([[1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1]], dtype=bool) class Input(Filter): inputs = () window_length = 0 self.check_terms( terms={ '3': Any(inputs=[Input()], window_length=3), '4': Any(inputs=[Input()], window_length=4), }, expected={ '3': expected_3, '4': expected_4, }, initial_workspace={Input(): data}, mask=self.build_mask(ones(shape=data.shape)), )
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)
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)), )