def test_in1d(self, kind): # we use two different sizes for the b array here to test the # two different paths in in1d(). for mult in (1, 10): # One check without np.array to make sure lists are handled correct a = [5, 7, 1, 2] b = [2, 4, 3, 1, 5] * mult ec = np.array([True, False, True, True]) c = in1d(a, b, assume_unique=True, kind=kind) assert_array_equal(c, ec) a[0] = 8 ec = np.array([False, False, True, True]) c = in1d(a, b, assume_unique=True, kind=kind) assert_array_equal(c, ec) a[0], a[3] = 4, 8 ec = np.array([True, False, True, False]) c = in1d(a, b, assume_unique=True, kind=kind) assert_array_equal(c, ec) a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) b = [2, 3, 4] * mult ec = [False, True, False, True, True, True, True, True, True, False, True, False, False, False] c = in1d(a, b, kind=kind) assert_array_equal(c, ec) b = b + [5, 5, 4] * mult ec = [True, True, True, True, True, True, True, True, True, True, True, False, True, True] c = in1d(a, b, kind=kind) assert_array_equal(c, ec) a = np.array([5, 7, 1, 2]) b = np.array([2, 4, 3, 1, 5] * mult) ec = np.array([True, False, True, True]) c = in1d(a, b, kind=kind) assert_array_equal(c, ec) a = np.array([5, 7, 1, 1, 2]) b = np.array([2, 4, 3, 3, 1, 5] * mult) ec = np.array([True, False, True, True, True]) c = in1d(a, b, kind=kind) assert_array_equal(c, ec) a = np.array([5, 5]) b = np.array([2, 2] * mult) ec = np.array([False, False]) c = in1d(a, b, kind=kind) assert_array_equal(c, ec) a = np.array([5]) b = np.array([2]) ec = np.array([False]) c = in1d(a, b, kind=kind) assert_array_equal(c, ec) if kind in {None, "sort"}: assert_array_equal(in1d([], [], kind=kind), [])
def test_in1d(self): # we use two different sizes for the b array here to test the # two different paths in in1d(). for mult in (1, 10): # One check without np.array, to make sure lists are handled correct a = [5, 7, 1, 2] b = [2, 4, 3, 1, 5] * mult ec = np.array([True, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) a[0] = 8 ec = np.array([False, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) a[0], a[3] = 4, 8 ec = np.array([True, False, True, False]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec) a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) b = [2, 3, 4] * mult ec = [False, True, False, True, True, True, True, True, True, False, True, False, False, False] c = in1d(a, b) assert_array_equal(c, ec) b = b + [5, 5, 4] * mult ec = [True, True, True, True, True, True, True, True, True, True, True, False, True, True] c = in1d(a, b) assert_array_equal(c, ec) a = np.array([5, 7, 1, 2]) b = np.array([2, 4, 3, 1, 5] * mult) ec = np.array([True, False, True, True]) c = in1d(a, b) assert_array_equal(c, ec) a = np.array([5, 7, 1, 1, 2]) b = np.array([2, 4, 3, 3, 1, 5] * mult) ec = np.array([True, False, True, True, True]) c = in1d(a, b) assert_array_equal(c, ec) a = np.array([5, 5]) b = np.array([2, 2] * mult) ec = np.array([False, False]) c = in1d(a, b) assert_array_equal(c, ec) a = np.array([5]) b = np.array([2]) ec = np.array([False]) c = in1d(a, b) assert_array_equal(c, ec) assert_array_equal(in1d([], []), [])
def common_timesteps(timegridA, timegridB): r""" Find the indices (wrt to A and B) of the timesteps common to both timegrids. """ IA = in1d(timegridA, timegridB) IB = in1d(timegridB, timegridA) return (IA, IB)
def test_in1d_invert(self): "Test in1d's invert parameter" # We use two different sizes for the b array here to test the # two different paths in in1d(). for mult in (1, 10): a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) b = [2, 3, 4] * mult assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
def test_in1d_boolean(self, kind): """Test that in1d works for boolean input""" a = np.array([True, False]) b = np.array([False, False, False]) expected = np.array([False, True]) assert_array_equal(expected, in1d(a, b, kind=kind)) assert_array_equal(np.invert(expected), in1d(a, b, invert=True, kind=kind))
def test_in1d_timedelta(self, kind): """Test that in1d works for timedelta input""" rstate = np.random.RandomState(0) a = rstate.randint(0, 100, size=10) b = rstate.randint(0, 100, size=10) truth = in1d(a, b) a_timedelta = a.astype("timedelta64[s]") b_timedelta = b.astype("timedelta64[s]") assert_array_equal(truth, in1d(a_timedelta, b_timedelta, kind=kind))
def test_in1d_mixed_boolean(self, kind): """Test that in1d works as expected for bool/int input.""" for dtype in np.typecodes["AllInteger"]: a = np.array([True, False, False], dtype=bool) b = np.array([1, 1, 1, 1], dtype=dtype) expected = np.array([True, False, False], dtype=bool) assert_array_equal(in1d(a, b, kind=kind), expected) a, b = b, a expected = np.array([True, True, True, True], dtype=bool) assert_array_equal(in1d(a, b, kind=kind), expected)
def test_in1d_ravel(self): # Test that in1d ravels its input arrays. This is not documented # behavior however. The test is to ensure consistentency. a = np.arange(6).reshape(2, 3) b = np.arange(3, 9).reshape(3, 2) long_b = np.arange(3, 63).reshape(30, 2) ec = np.array([False, False, False, True, True, True]) assert_array_equal(in1d(a, b, assume_unique=True), ec) assert_array_equal(in1d(a, b, assume_unique=False), ec) assert_array_equal(in1d(a, long_b, assume_unique=True), ec) assert_array_equal(in1d(a, long_b, assume_unique=False), ec)
def test_in1d_hit_alternate_algorithm(self): """Hit the standard isin code with integers""" # Need extreme range to hit standard code # This hits it without the use of kind='table' a = np.array([5, 4, 5, 3, 4, 4, 1e9], dtype=np.int64) b = np.array([2, 3, 4, 1e9], dtype=np.int64) expected = np.array([0, 1, 0, 1, 1, 1, 1], dtype=bool) assert_array_equal(expected, in1d(a, b)) assert_array_equal(np.invert(expected), in1d(a, b, invert=True)) a = np.array([5, 7, 1, 2], dtype=np.int64) b = np.array([2, 4, 3, 1, 5, 1e9], dtype=np.int64) ec = np.array([True, False, True, True]) c = in1d(a, b, assume_unique=True) assert_array_equal(c, ec)
def test_in1d_char_array(self): a = np.array(['a', 'b', 'c', 'd', 'e', 'c', 'e', 'b']) b = np.array(['a', 'c']) ec = np.array([True, False, True, False, False, True, False, False]) c = in1d(a, b) assert_array_equal(c, ec)
def test_in1d_char_array(self): a = np.array(["a", "b", "c", "d", "e", "c", "e", "b"]) b = np.array(["a", "c"]) ec = np.array([True, False, True, False, False, True, False, False]) c = in1d(a, b) assert_array_equal(c, ec)
def test_in1d_invert(self, kind): "Test in1d's invert parameter" # We use two different sizes for the b array here to test the # two different paths in in1d(). for mult in (1, 10): a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) b = [2, 3, 4] * mult assert_array_equal(np.invert(in1d(a, b, kind=kind)), in1d(a, b, invert=True, kind=kind)) # float: if kind in {None, "sort"}: for mult in (1, 10): a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5], dtype=np.float32) b = [2, 3, 4] * mult b = np.array(b, dtype=np.float32) assert_array_equal(np.invert(in1d(a, b, kind=kind)), in1d(a, b, invert=True, kind=kind))
def ghost_bnd_layer(ghosttri, tlower, tupper, mesh, p): boundary = mesh.boundary ghost_list = [] subboundary = {} new_ghost_list = ghosttri[:,0] #print new_ghost_list # 0 edge boundaries nghb0 = mesh.neighbours[new_ghost_list,0] gl0 = num.extract(num.logical_or(nghb0 < tlower, nghb0 >= tupper), new_ghost_list) nghb0 = mesh.neighbours[gl0,0] flag = numset.in1d(nghb0,new_ghost_list) gl0 = num.extract(num.logical_not(flag),gl0) edge0 = 0*num.ones_like(gl0) n0 = len(edge0) values0 = ['ghost']*n0 # 1 edge boundary nghb1 = mesh.neighbours[new_ghost_list,1] gl1 = num.extract(num.logical_or(nghb1 < tlower, nghb1 >= tupper), new_ghost_list) nghb1 = mesh.neighbours[gl1,1] flag = numset.in1d(nghb1,new_ghost_list) gl1 = num.extract(num.logical_not(flag),gl1) edge1 = 1*num.ones_like(gl1) n1 = len(edge1) values1 = ['ghost']*n1 # 2 edge boundary nghb2 = mesh.neighbours[new_ghost_list,2] gl2 = num.extract(num.logical_or(nghb2 < tlower, nghb2 >= tupper), new_ghost_list) nghb2 = mesh.neighbours[gl2,2] flag = numset.in1d(nghb2,new_ghost_list) gl2 = num.extract(num.logical_not(flag),gl2) edge2 = 2*num.ones_like(gl2) n2 = len(edge2) values2 = ['ghost']*n2 gl = num.concatenate((gl0,gl1,gl2)) edge = num.concatenate((edge0,edge1,edge2)) values = values0 + values1 + values2 # print gl # print edge # print values subboundary = dict(zip(zip(gl,edge),values)) #intersect with boundary # FIXME SR: these keys should be viewkeys but need python 2.7 subboundary.update( (k,boundary[k]) for k in set(subboundary.keys()) & set(boundary.keys()) ) #print subboundary return subboundary
def ghost_bnd_layer(ghosttri, tlower, tupper, mesh, p): boundary = mesh.boundary ghost_list = [] subboundary = {} new_ghost_list = ghosttri[:, 0] # print new_ghost_list # 0 edge boundaries nghb0 = mesh.neighbours[new_ghost_list, 0] gl0 = num.extract(num.logical_or(nghb0 < tlower, nghb0 >= tupper), new_ghost_list) nghb0 = mesh.neighbours[gl0, 0] flag = numset.in1d(nghb0, new_ghost_list) gl0 = num.extract(num.logical_not(flag), gl0) edge0 = 0 * num.ones_like(gl0) n0 = len(edge0) values0 = ['ghost'] * n0 # 1 edge boundary nghb1 = mesh.neighbours[new_ghost_list, 1] gl1 = num.extract(num.logical_or(nghb1 < tlower, nghb1 >= tupper), new_ghost_list) nghb1 = mesh.neighbours[gl1, 1] flag = numset.in1d(nghb1, new_ghost_list) gl1 = num.extract(num.logical_not(flag), gl1) edge1 = 1 * num.ones_like(gl1) n1 = len(edge1) values1 = ['ghost'] * n1 # 2 edge boundary nghb2 = mesh.neighbours[new_ghost_list, 2] gl2 = num.extract(num.logical_or(nghb2 < tlower, nghb2 >= tupper), new_ghost_list) nghb2 = mesh.neighbours[gl2, 2] flag = numset.in1d(nghb2, new_ghost_list) gl2 = num.extract(num.logical_not(flag), gl2) edge2 = 2 * num.ones_like(gl2) n2 = len(edge2) values2 = ['ghost'] * n2 gl = num.concatenate((gl0, gl1, gl2)) edge = num.concatenate((edge0, edge1, edge2)) values = values0 + values1 + values2 # print gl # print edge # print values subboundary = dict(list(zip(list(zip(gl, edge)), values))) # intersect with boundary # FIXME SR: these keys should be viewkeys but need python 2.7 subboundary.update((k, boundary[k]) for k in set(subboundary.keys()) & set(boundary.keys())) # print subboundary return subboundary
def test_in1d_table_timedelta_fails(self): a = np.array([0, 1, 2], dtype="timedelta64[s]") b = a # Make sure it raises a value error: with pytest.raises(ValueError): in1d(a, b, kind="table")