def test_nested_logic(self): q2 = self.example_data_A_data['q2'] test_logic = ( ( has_all([1, 2]), Index.union, has_any([3, 4]) ), Index.intersection, has_any([5, 6]) ) idx, vkey = get_logic_index(q2, test_logic) idx1, vkey1 = get_logic_index(q2, test_logic[0]) idx2, vkey2 = get_logic_index(q2, test_logic[2]) self.assertItemsEqual( idx, idx1.intersection(idx2) ) self.assertEqual( vkey, 'x[(((1&2),(3,4))&(5,6))]:y' ) q2 = self.example_data_A_data['q2'] test_logic = ( ( has_any([1, 2]), Index.intersection, {'q3': has_all([1, 2, 3])} ), Index.intersection, has_any([5, 6]) ) idx, vkey = get_logic_index(q2, test_logic, self.example_data_A_data) idx_q2_a, vkey_q2_a = get_logic_index(q2, has_any([1, 2])) q3 = self.example_data_A_data['q3'] idx_q3, vkey_q3 = get_logic_index(q3, has_all([1, 2, 3])) idx_q3 = idx_q2_a.intersection(idx_q3) idx_q2_b, vkey_q2_a = get_logic_index(q2, has_any([5, 6])) idx_q2_b = idx_q3.intersection(idx_q2_b) self.assertItemsEqual( idx, idx_q2_b ) self.assertEqual( vkey, 'x[(((1,2)&q3=(1&2&3))&(5,6))]:y' )
def test_difference(self): q2 = self.example_data_A_data['q2'] test_logic = (has_all([1, 2]), Index.difference, has_any([3, 4])) idx1, vkey1 = get_logic_index(q2, test_logic[0]) idx2, vkey2 = get_logic_index(q2, test_logic[2]) idx, vkey = get_logic_index(q2, test_logic) self.assertItemsEqual(idx, idx1.difference(idx2)) self.assertEqual(vkey, 'x[({1&2}~{3,4})]:y')
def test_sym_diff(self): q2 = self.example_data_A_data['q2'] test_logic = (has_all([1, 2]), Index.sym_diff, has_any([3, 4])) idx1, vkey1 = get_logic_index(q2, test_logic[0]) idx2, vkey2 = get_logic_index(q2, test_logic[2]) idx, vkey = get_logic_index(q2, test_logic) self.assertItemsEqual(idx, idx1.sym_diff(idx2)) self.assertEqual(vkey, 'x[((1&2)^(3,4))]:y')
def test_intersection(self): q2 = self.example_data_A_data['q2'] test_logic = (has_all([1, 2]), Index.intersection, has_any([3, 4])) idx1, vkey1 = get_logic_index(q2, test_logic[0]) idx2, vkey2 = get_logic_index(q2, test_logic[2]) idx, vkey = get_logic_index(q2, test_logic) self.assertCountEqual(idx, idx1.intersection(idx2)) self.assertEqual(vkey, 'x[({1&2}&{3,4})]:y')
def test_logic_list(self): q2 = self.example_data_A_data['q2'] test_logic = union([has_all([1, 2]), has_any([3, 4]), has_count([3])]) idx, vkey = get_logic_index(q2, test_logic) idx1, vkey1 = get_logic_index(q2, test_logic[1][0]) idx2, vkey2 = get_logic_index(q2, test_logic[1][1]) idx3, vkey3 = get_logic_index(q2, test_logic[1][2]) self.assertItemsEqual(idx, idx1.union(idx2).union(idx3)) self.assertEqual(vkey, 'x[({1&2},{3,4},{3})]:y') q2 = self.example_data_A_data['q2'] test_logic = intersection( [has_all([1, 2]), has_any([3, 4]), has_count([3])]) idx, vkey = get_logic_index(q2, test_logic) idx1, vkey1 = get_logic_index(q2, test_logic[1][0]) idx2, vkey2 = get_logic_index(q2, test_logic[1][1]) idx3, vkey3 = get_logic_index(q2, test_logic[1][2]) self.assertItemsEqual(idx, idx1.intersection(idx2).intersection(idx3)) self.assertEqual(vkey, 'x[({1&2}&{3,4}&{3})]:y') q2 = self.example_data_A_data['q2'] test_logic = difference( [has_all([1, 2]), has_any([3, 4]), has_count([3])]) idx, vkey = get_logic_index(q2, test_logic) idx1, vkey1 = get_logic_index(q2, test_logic[1][0]) idx2, vkey2 = get_logic_index(q2, test_logic[1][1]) idx3, vkey3 = get_logic_index(q2, test_logic[1][2]) self.assertItemsEqual(idx, idx1.difference(idx2).difference(idx3)) self.assertEqual(vkey, 'x[({1&2}~{3,4}~{3})]:y') q2 = self.example_data_A_data['q2'] test_logic = sym_diff( [has_all([1, 2]), has_any([3, 4]), has_count([3])]) idx, vkey = get_logic_index(q2, test_logic) idx1, vkey1 = get_logic_index(q2, test_logic[1][0]) idx2, vkey2 = get_logic_index(q2, test_logic[1][1]) idx3, vkey3 = get_logic_index(q2, test_logic[1][2]) self.assertItemsEqual(idx, idx1.sym_diff(idx2).sym_diff(idx3)) self.assertEqual(vkey, 'x[({1&2}^{3,4}^{3})]:y')
def test_get_logic_key(self): logic = has_all([1, 2, 3], True) self.assertEqual( get_logic_key(logic), 'x[e(1&2&3)]:y' ) logic = has_count([is_ge(1), [5, 6, 7, 8, 9]]) self.assertEqual( get_logic_key(logic), 'x[(5,6,7,8,9){>=1}]:y' ) logic = not_count([is_ge(1), [5, 6, 7, 8, 9]]) self.assertEqual( get_logic_key(logic), 'x[(5,6,7,8,9)~{>=1}]:y' ) logic = union([ has_any([1, 2]), has_all([3, 4]), not_any([5, 6]) ]) self.assertEqual( get_logic_key(logic), 'x[((1,2),(3&4),~(5,6))]:y' ) logic = union([ intersection([ has_any([1, 2]), not_any([3]) ]), {'Wave': has_any([1,2])} ]) self.assertEqual( get_logic_key(logic, self.example_data_A_data), 'x[(((1,2)&~(3)),Wave=(1,2))]:y' )
def test_get_logic_key(self): logic = has_all([1, 2, 3], True) self.assertEqual( get_logic_key(logic), 'x[e{1&2&3}]:y' ) logic = has_count([is_ge(1), [5, 6, 7, 8, 9]]) self.assertEqual( get_logic_key(logic), 'x[(5,6,7,8,9){>=1}]:y' ) logic = not_count([is_ge(1), [5, 6, 7, 8, 9]]) self.assertEqual( get_logic_key(logic), 'x[(5,6,7,8,9)~{>=1}]:y' ) logic = union([ has_any([1, 2]), has_all([3, 4]), not_any([5, 6]) ]) self.assertEqual( get_logic_key(logic), 'x[({1,2},{3&4},~{5,6})]:y' ) logic = union([ intersection([ has_any([1, 2]), not_any([3]) ]), {'Wave': has_any([1,2])} ]) self.assertEqual( get_logic_key(logic, self.example_data_A_data), 'x[(({1,2}&~{3}),Wave={1,2})]:y' )
def test_nested_logic_list(self): q2 = self.example_data_A_data['q2'] test_logic = intersection([ union([ has_all([1, 2]), has_any([3, 4]) ]), has_count([3]) ]) idx, vkey = get_logic_index(q2, test_logic) idx1, vkey1 = get_logic_index(q2, has_all([1, 2])) idx2, vkey2 = get_logic_index(q2, has_any([3, 4])) idx3, vkey3 = get_logic_index(q2, has_count([3])) self.assertItemsEqual( idx, idx1.union(idx2).intersection(idx3) ) self.assertEqual( vkey, 'x[(((1&2),(3,4))&{3})]:y' )
def test_get_logic_key(self): logic = has_all([1, 2, 3], True) self.assertEqual(get_logic_key(logic), 'x[e(1&2&3)]:y') logic = has_count([is_ge(1), [5, 6, 7, 8, 9]]) self.assertEqual(get_logic_key(logic), 'x[(5,6,7,8,9){>=1}]:y') logic = not_count([is_ge(1), [5, 6, 7, 8, 9]]) self.assertEqual(get_logic_key(logic), 'x[(5,6,7,8,9)~{>=1}]:y') logic = union([has_any([1, 2]), has_all([3, 4]), not_any([5, 6])]) self.assertEqual(get_logic_key(logic), 'x[((1,2),(3&4),~(5,6))]:y') logic = union([ intersection([has_any([1, 2]), not_any([3])]), { 'Wave': has_any([1, 2]) } ]) self.assertEqual(get_logic_key(logic, self.example_data_A_data), 'x[(((1,2)&~(3)),Wave=(1,2))]:y')
def test_nested_logic_list(self): q2 = self.example_data_A_data['q2'] test_logic = intersection([ union([ has_all([1, 2]), has_any([3, 4]) ]), has_count([3]) ]) idx, vkey = get_logic_index(q2, test_logic) idx1, vkey1 = get_logic_index(q2, has_all([1, 2])) idx2, vkey2 = get_logic_index(q2, has_any([3, 4])) idx3, vkey3 = get_logic_index(q2, has_count([3])) self.assertEqual( idx, idx1.union(idx2).intersection(idx3) ) self.assertEqual( vkey, 'x[(({1&2},{3,4})&{3})]:y' )
def test_wildcards(self): q2 = self.example_data_A_data['q2'] q3 = self.example_data_A_data['q3'] test_logic = {'q3': has_all([1, 2, 3])} idx, vkey = get_logic_index(q2, test_logic, self.example_data_A_data) idx_q3, vkey_q3 = get_logic_index(q3, has_all([1, 2, 3])) idx_q3 = q2.dropna().index.intersection(idx_q3) self.assertItemsEqual(idx, idx_q3) self.assertEqual(vkey, 'x[q3={1&2&3}]:y') q2 = self.example_data_A_data['q2'] test_logic = (has_any([1, 2]), Index.intersection, { 'q3': has_all([1, 2, 3]) }) idx, vkey = get_logic_index(q2, test_logic, self.example_data_A_data) idx_q2, vkey_q2 = get_logic_index(q2, has_any([1, 2])) q3 = self.example_data_A_data['q3'] idx_q3, vkey_q3 = get_logic_index(q3, has_all([1, 2, 3])) idx_q3 = idx_q2.intersection(idx_q3) self.assertItemsEqual(idx, idx_q3) self.assertEqual(vkey, 'x[({1,2}&q3={1&2&3})]:y') q2 = self.example_data_A_data['q2'] test_logic = ({ 'q3': has_all([1, 2, 3]) }, Index.difference, has_any([1, 2])) idx, vkey = get_logic_index(q2, test_logic, self.example_data_A_data) idx_q2, vkey_q2 = get_logic_index(q2, has_any([1, 2])) q3 = self.example_data_A_data['q3'] idx_q3, vkey_q3 = get_logic_index(q3, has_all([1, 2, 3])) idx_q3 = q2.dropna().index.intersection(idx_q3.difference(idx_q2)) self.assertItemsEqual(idx, idx_q3) self.assertEqual(vkey, 'x[(q3={1&2&3}~{1,2})]:y')
def test_symmetric_difference(self): q2 = self.example_data_A_data['q2'] test_logic = (has_all([1, 2]), pd_symmetric_difference, has_any([3, 4])) idx1, vkey1 = get_logic_index(q2, test_logic[0]) idx2, vkey2 = get_logic_index(q2, test_logic[2]) idx, vkey = get_logic_index(q2, test_logic) if pd.__version__ == '0.19.2': self.assertCountEqual(idx, idx1.symmetric_difference(idx2)) else: self.assertCountEqual(idx, idx1.symmetric_difference(idx2)) self.assertEqual(vkey, 'x[({1&2}^{3,4})]:y')
def test_has_all(self): # Test has version test_values = [1, 3, 5] func, values, exclusive = has_all(test_values) self.assertEqual(func, _has_all) self.assertEqual(values, test_values) self.assertEqual(exclusive, False) # Test not version test_values = [1, 3, 5] func, values, exclusive = not_all(test_values) self.assertEqual(func, _not_all) self.assertEqual(values, test_values) self.assertEqual(exclusive, False)
def test_nested_logic(self): q2 = self.example_data_A_data['q2'] test_logic = ((has_all([1, 2]), Index.union, has_any([3, 4])), Index.intersection, has_any([5, 6])) idx, vkey = get_logic_index(q2, test_logic) idx1, vkey1 = get_logic_index(q2, test_logic[0]) idx2, vkey2 = get_logic_index(q2, test_logic[2]) self.assertItemsEqual(idx, idx1.intersection(idx2)) self.assertEqual(vkey, 'x[(({1&2},{3,4})&{5,6})]:y') q2 = self.example_data_A_data['q2'] test_logic = ((has_any([1, 2]), Index.intersection, { 'q3': has_all([1, 2, 3]) }), Index.intersection, has_any([5, 6])) idx, vkey = get_logic_index(q2, test_logic, self.example_data_A_data) idx_q2_a, vkey_q2_a = get_logic_index(q2, has_any([1, 2])) q3 = self.example_data_A_data['q3'] idx_q3, vkey_q3 = get_logic_index(q3, has_all([1, 2, 3])) idx_q3 = idx_q2_a.intersection(idx_q3) idx_q2_b, vkey_q2_a = get_logic_index(q2, has_any([5, 6])) idx_q2_b = idx_q3.intersection(idx_q2_b) self.assertItemsEqual(idx, idx_q2_b) self.assertEqual(vkey, 'x[(({1,2}&q3={1&2&3})&{5,6})]:y')
def test_sym_diff(self): q2 = self.example_data_A_data['q2'] test_logic = (has_all([1, 2]), Index.sym_diff, has_any([3, 4])) idx1, vkey1 = get_logic_index(q2, test_logic[0]) idx2, vkey2 = get_logic_index(q2, test_logic[2]) idx, vkey = get_logic_index(q2, test_logic) self.assertItemsEqual( idx, idx1.sym_diff(idx2) ) self.assertEqual( vkey, 'x[((1&2)^(3,4))]:y' )
def test_has_all_errors(self): # Test values not given as a list gender = self.example_data_A_data['gender'] for test_values in [1, 'wrong!']: # Test has version with self.assertRaises(TypeError) as error: func, values = has_all(test_values) self.assertEqual( error.exception.message[:54], "The values given to has_all() must be given as a list." ) # Test not version with self.assertRaises(TypeError) as error: func, values = not_all(test_values) self.assertEqual( error.exception.message[:54], "The values given to not_all() must be given as a list." ) # Test values inside the values are not int gender = self.example_data_A_data['gender'] for test_values in [['1'], [1.9, 2, 3]]: # Test has version with self.assertRaises(TypeError) as error: func, values = has_all(test_values) self.assertEqual( error.exception.message[:54], "The values given to has_all() are not correctly typed." ) # Test not version with self.assertRaises(TypeError) as error: func, values = not_all(test_values) self.assertEqual( error.exception.message[:54], "The values given to not_all() are not correctly typed." )
def test_wildcards(self): q2 = self.example_data_A_data['q2'] q3 = self.example_data_A_data['q3'] test_logic = {'q3': has_all([1, 2, 3])} idx, vkey = get_logic_index(q2, test_logic, self.example_data_A_data) idx_q3, vkey_q3 = get_logic_index(q3, has_all([1, 2, 3])) idx_q3 = q2.dropna().index.intersection(idx_q3) self.assertItemsEqual( idx, idx_q3 ) self.assertEqual( vkey, 'x[q3=(1&2&3)]:y' ) q2 = self.example_data_A_data['q2'] test_logic = (has_any([1, 2]), Index.intersection, {'q3': has_all([1, 2, 3])}) idx, vkey = get_logic_index(q2, test_logic, self.example_data_A_data) idx_q2, vkey_q2 = get_logic_index(q2, has_any([1, 2])) q3 = self.example_data_A_data['q3'] idx_q3, vkey_q3 = get_logic_index(q3, has_all([1, 2, 3])) idx_q3 = idx_q2.intersection(idx_q3) self.assertItemsEqual( idx, idx_q3 ) self.assertEqual( vkey, 'x[((1,2)&q3=(1&2&3))]:y' ) q2 = self.example_data_A_data['q2'] test_logic = ({'q3': has_all([1, 2, 3])}, Index.difference, has_any([1, 2])) idx, vkey = get_logic_index(q2, test_logic, self.example_data_A_data) idx_q2, vkey_q2 = get_logic_index(q2, has_any([1, 2])) q3 = self.example_data_A_data['q3'] idx_q3, vkey_q3 = get_logic_index(q3, has_all([1, 2, 3])) idx_q3 = q2.dropna().index.intersection(idx_q3.difference(idx_q2)) self.assertItemsEqual( idx, idx_q3 ) self.assertEqual( vkey, 'x[(q3=(1&2&3)~(1,2))]:y' )
def test_logic_list(self): q2 = self.example_data_A_data['q2'] test_logic = union([ has_all([1, 2]), has_any([3, 4]), has_count([3]) ]) idx, vkey = get_logic_index(q2, test_logic) idx1, vkey1 = get_logic_index(q2, test_logic[1][0]) idx2, vkey2 = get_logic_index(q2, test_logic[1][1]) idx3, vkey3 = get_logic_index(q2, test_logic[1][2]) self.assertItemsEqual( idx, idx1.union(idx2).union(idx3) ) self.assertEqual( vkey, 'x[((1&2),(3,4),{3})]:y' ) q2 = self.example_data_A_data['q2'] test_logic = intersection([ has_all([1, 2]), has_any([3, 4]), has_count([3]) ]) idx, vkey = get_logic_index(q2, test_logic) idx1, vkey1 = get_logic_index(q2, test_logic[1][0]) idx2, vkey2 = get_logic_index(q2, test_logic[1][1]) idx3, vkey3 = get_logic_index(q2, test_logic[1][2]) self.assertItemsEqual( idx, idx1.intersection(idx2).intersection(idx3) ) self.assertEqual( vkey, 'x[((1&2)&(3,4)&{3})]:y' ) q2 = self.example_data_A_data['q2'] test_logic = difference([ has_all([1, 2]), has_any([3, 4]), has_count([3]) ]) idx, vkey = get_logic_index(q2, test_logic) idx1, vkey1 = get_logic_index(q2, test_logic[1][0]) idx2, vkey2 = get_logic_index(q2, test_logic[1][1]) idx3, vkey3 = get_logic_index(q2, test_logic[1][2]) self.assertItemsEqual( idx, idx1.difference(idx2).difference(idx3) ) self.assertEqual( vkey, 'x[((1&2)~(3,4)~{3})]:y' ) q2 = self.example_data_A_data['q2'] test_logic = sym_diff([ has_all([1, 2]), has_any([3, 4]), has_count([3]) ]) idx, vkey = get_logic_index(q2, test_logic) idx1, vkey1 = get_logic_index(q2, test_logic[1][0]) idx2, vkey2 = get_logic_index(q2, test_logic[1][1]) idx3, vkey3 = get_logic_index(q2, test_logic[1][2]) self.assertItemsEqual( idx, idx1.sym_diff(idx2).sym_diff(idx3) ) self.assertEqual( vkey, 'x[((1&2)^(3,4)^{3})]:y' )