Example #1
0
 def invalid_operands(a, b, num_axes_a, num_axes_b):
     # Types must be the same across axes.
     # At most 1 array_like selector.
     types_a, types_b = list(map(type, a)), list(map(type, b))
     num_arrays_a = 0
     num_arrays_b = 0
     for i in range(max(len(a), len(b))):
         if i >= len(a):
             if not isinstance(b[i], slice):
                 return True
         elif i >= len(b):
             if not isinstance(a[i], slice):
                 return True
         else:
             if array_utils.is_array_like(a[i]):
                 if sel_module.get_array_order(np.array(
                         a[i], dtype=np.intp)) == 0:
                     return True
                 num_arrays_a += 1
             if array_utils.is_array_like(b[i]):
                 if sel_module.get_array_order(np.array(
                         b[i], dtype=np.intp)) == 0:
                     return True
                 num_arrays_b += 1
             if types_a[i] != types_b[i]:
                 return True
     if num_axes_a > 1 or num_axes_b > 1:
         return True
     return False
Example #2
0
def test_array_intersection():
    # Test several basic tests.
    ss = BasicSelection.from_subscript
    size = 4
    sizes = list(itertools.product(np.arange(1, size), repeat=2))
    for sizes_idx, (size_a, size_b) in enumerate(sizes):
        arr: np.ndarray = np.arange(size).reshape((size,))
        test_selections = list(
            itertools.product(get_arrays(size_a), get_arrays(size_b))
        )
        pbar = tqdm.tqdm(total=len(test_selections))
        for ss_a, ss_b in test_selections:
            pbar.set_description(str((sizes_idx / len(sizes), ss_a, ss_b)))
            ss_a = (ss_a,)
            ss_b = (ss_b,)
            pbar.update(1)
            try:
                sel_a: BasicSelection = ss(arr.shape, ss_a)
            except ValueError as _:
                with pytest.raises(ValueError):
                    ss(arr.shape, ss_a)
                continue
            try:
                sel_b: BasicSelection = ss(arr.shape, ss_b)
            except ValueError as _:
                with pytest.raises(ValueError):
                    ss(arr.shape, ss_b)
                continue
            sel_c: BasicSelection = sel_a & sel_b
            true_arr_sel_a = arr[ss_a]
            true_arr_sel_b = arr[ss_b]
            test_arr_sel_a = arr[sel_a.selector()]
            test_arr_sel_b = arr[sel_b.selector()]
            assert np.allclose(true_arr_sel_a, test_arr_sel_a)
            assert np.allclose(true_arr_sel_b, test_arr_sel_b)

            a_order = sel_module.get_array_order(ss_a[0], axis=0)
            b_order = sel_module.get_array_order(ss_b[0], axis=0)
            if a_order != b_order:
                ss_c = np.array([], dtype=np.intp)
            else:
                ss_c = np.sort(
                    np.array(list(set(ss_a[0]) & set(ss_b[0])), dtype=np.intp)
                )[::a_order]
            assert np.allclose(sel_c[0].array, ss_c)
            true_arr_sel_c = arr[ss_c]
            test_arr_sel_c = arr[sel_c.selector()]
            assert np.allclose(test_arr_sel_c, true_arr_sel_c)