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_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.assertCountEqual(idx, idx1.difference(idx2)) self.assertEqual(vkey, 'x[({1&2}~{3,4})]:y')
def test_union(self): q2 = self.example_data_A_data['q2'] test_logic = (has_all([1, 2]), Index.union, 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.union(idx2)) self.assertEqual(vkey, 'x[((1&2),(3,4))]: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_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_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_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_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_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_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.assertEqual( 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.assertEqual( idx, idx_q2_b ) self.assertEqual( vkey, 'x[(({1,2}&q3={1&2&3})&{5,6})]:y' )
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 clone(self, name, b_filter=None, as_addition=False): """ Create a copy of Batch instance. Parameters ---------- name: str Name of the Batch instance that is copied. b_filter: tuple (str, dict/ complex logic) Filter logic which is applied on the new batch. (filtername, filterlogic) as_addition: bool, default False If True, the new batch is added as addition to the master batch. Returns ------- New/ copied Batch instance. """ org_name = self.name org_meta = org_copy.deepcopy(self._meta['sets']['batches'][org_name]) self._meta['sets']['batches'][name] = org_meta verbose = self._verbose_infos self.set_verbose_infomsg(False) batch_copy = self.get_batch(name) self.set_verbose_infomsg(verbose) batch_copy.set_verbose_infomsg(verbose) if b_filter: batch_copy.add_filter(b_filter[0], b_filter[1]) if batch_copy.verbatims and b_filter and not as_addition: for oe in batch_copy.verbatims: data = self._data.copy() series_data = data['@1'].copy()[pd.Index(oe['idx'])] slicer, _ = get_logic_index(series_data, b_filter[1], data) oe['idx'] = slicer.tolist() if as_addition: batch_copy.as_addition(self.name) batch_copy._update() return batch_copy
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.assertEqual( 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.assertEqual( 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.assertEqual( 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 = symmetric_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]) if pd.__version__ == '0.19.2': self.assertEqual( idx, idx1.symmetric_difference(idx2).symmetric_difference(idx3) ) else: self.assertEqual( idx, idx1.sym_diff(idx2).sym_diff(idx3) ) self.assertEqual( vkey, 'x[({1&2}^{3,4}^{3})]: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_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' )