def test_intersect1d_array_like(self): # See gh-11772 class Test(object): def __array__(self): return np.arange(3) a = Test() res = intersect1d(a, a) assert_array_equal(res, a) res = intersect1d([1, 2, 3], [1, 2, 3]) assert_array_equal(res, [1, 2, 3])
def test_intersect1d(self): # unique inputs a = np.array([5, 7, 1, 2]) b = np.array([2, 4, 3, 1, 5]) ec = np.array([1, 2, 5]) c = intersect1d(a, b, assume_unique=True) assert_array_equal(c, ec) # non-unique inputs a = np.array([5, 5, 7, 1, 2]) b = np.array([2, 1, 4, 3, 3, 1, 5]) ed = np.array([1, 2, 5]) c = intersect1d(a, b) assert_array_equal(c, ed) assert_array_equal([], intersect1d([], []))
def test_manyways(self): a = np.array([5, 7, 1, 2, 8]) b = np.array([9, 8, 2, 4, 3, 1, 5]) c1 = setxor1d(a, b) aux1 = intersect1d(a, b) aux2 = union1d(a, b) c2 = setdiff1d(aux2, aux1) assert_array_equal(c1, c2)
def test_intersect1d_indices(self): # unique inputs a = np.array([1, 2, 3, 4]) b = np.array([2, 1, 4, 6]) c, i1, i2 = intersect1d(a, b, assume_unique=True, return_indices=True) ee = np.array([1, 2, 4]) assert_array_equal(c, ee) assert_array_equal(a[i1], ee) assert_array_equal(b[i2], ee) # non-unique inputs a = np.array([1, 2, 2, 3, 4, 3, 2]) b = np.array([1, 8, 4, 2, 2, 3, 2, 3]) c, i1, i2 = intersect1d(a, b, return_indices=True) ef = np.array([1, 2, 3, 4]) assert_array_equal(c, ef) assert_array_equal(a[i1], ef) assert_array_equal(b[i2], ef) # non1d, unique inputs a = np.array([[2, 4, 5, 6], [7, 8, 1, 15]]) b = np.array([[3, 2, 7, 6], [10, 12, 8, 9]]) c, i1, i2 = intersect1d(a, b, assume_unique=True, return_indices=True) ui1 = np.unravel_index(i1, a.shape) ui2 = np.unravel_index(i2, b.shape) ea = np.array([2, 6, 7, 8]) assert_array_equal(ea, a[ui1]) assert_array_equal(ea, b[ui2]) # non1d, not assumed to be uniqueinputs a = np.array([[2, 4, 5, 6, 6], [4, 7, 8, 7, 2]]) b = np.array([[3, 2, 7, 7], [10, 12, 8, 7]]) c, i1, i2 = intersect1d(a, b, return_indices=True) ui1 = np.unravel_index(i1, a.shape) ui2 = np.unravel_index(i2, b.shape) ea = np.array([2, 7, 8]) assert_array_equal(ea, a[ui1]) assert_array_equal(ea, b[ui2])
def intersect1d(a,b): from MDSplus import Data from numpy.lib.arraysetops import intersect1d return Data(intersect1d(a.data(),b.data()))
def __check_for_intersection(self,arr1,arr2): inset = nset.intersect1d(arr1,arr2)#hopefully based on arr1 arr2.reverse() insetr = nset.intersect1d(arr1,map(lambda x: -x, arr2))#could replace this with numpy array for speed at some point arr2.reverse() rvs = False if insetr.size > inset.size:#note may need to addapt subsiquent code inset = insetr rvs = True if inset.size == 0: return [], [], [], [], [], [], [] br = arr2 if rvs: arr2.reverse() br = map(lambda x: -x, arr2) arr2.reverse() apre = [arr1[:arr1.index(inset[0])]]; apo = [arr1[arr1.index(inset[-1])+1:]] bpre = [arr2[:br.index(inset[0])]]; bpo = [arr2[br.index(inset[-1])+1:]] ra = range(arr1.index(inset[0]),arr1.index(inset[-1])+1) rb = range(br.index(inset[0]),br.index(inset[-1])+1) con = 0 con_s = 0 flp = 0 a1l = [[]] s1l = [[]] for i in ra: #vectorize/mapline_loop_dict.keys() if arr1[i] in inset: if flp == 1: flp = 0 con_s += 1 s1l.append([]) s1l[con_s].append(arr1[i]) continue if flp == 0: flp = 1 con += 1 a1l.append([]) a1l[con] += [arr1[i]] con = 0 flp = 0 b1l = [[]] for i in rb: if br[i] in inset: if flp == 1: flp = 0 continue if flp == 0: flp = 1 con += 1 b1l.append([]) b1l[con].append(br[i]) if rvs: for i in b1l: i.reverse() i = map(lambda x: -x, i) apre = self.rmvMulti( apre, [] ) a1l = self.rmvMulti( a1l, [] ) apo = self.rmvMulti( apo, [] ) s1l = self.rmvMulti( s1l, [] ) bpre = self.rmvMulti( bpre, [] ) b1l = self.rmvMulti( b1l, [] ) bpo = self.rmvMulti( bpo, [] ) return [apre,a1l,apo,s1l,bpre,b1l,bpo]
def __check_for_intersection(self, arr1, arr2): inset = nset.intersect1d(arr1, arr2) #hopefully based on arr1 arr2.reverse() insetr = nset.intersect1d( arr1, map(lambda x: -x, arr2) ) #could replace this with numpy array for speed at some point arr2.reverse() rvs = False if insetr.size > inset.size: #note may need to addapt subsiquent code inset = insetr rvs = True if inset.size == 0: return [], [], [], [], [], [], [] br = arr2 if rvs: arr2.reverse() br = map(lambda x: -x, arr2) arr2.reverse() apre = [arr1[:arr1.index(inset[0])]] apo = [arr1[arr1.index(inset[-1]) + 1:]] bpre = [arr2[:br.index(inset[0])]] bpo = [arr2[br.index(inset[-1]) + 1:]] ra = range(arr1.index(inset[0]), arr1.index(inset[-1]) + 1) rb = range(br.index(inset[0]), br.index(inset[-1]) + 1) con = 0 con_s = 0 flp = 0 a1l = [[]] s1l = [[]] for i in ra: #vectorize/mapline_loop_dict.keys() if arr1[i] in inset: if flp == 1: flp = 0 con_s += 1 s1l.append([]) s1l[con_s].append(arr1[i]) continue if flp == 0: flp = 1 con += 1 a1l.append([]) a1l[con] += [arr1[i]] con = 0 flp = 0 b1l = [[]] for i in rb: if br[i] in inset: if flp == 1: flp = 0 continue if flp == 0: flp = 1 con += 1 b1l.append([]) b1l[con].append(br[i]) if rvs: for i in b1l: i.reverse() i = map(lambda x: -x, i) apre = self.rmvMulti(apre, []) a1l = self.rmvMulti(a1l, []) apo = self.rmvMulti(apo, []) s1l = self.rmvMulti(s1l, []) bpre = self.rmvMulti(bpre, []) b1l = self.rmvMulti(b1l, []) bpo = self.rmvMulti(bpo, []) return [apre, a1l, apo, s1l, bpre, b1l, bpo]