def __ne__(self, other):
return np.invert(self == other)
# Export and import functionality
python类invert()的实例源码
def gen_batch_function(data_folder, image_shape):
"""
Generate function to create batches of training data
:param data_folder: Path to folder that contains all the datasets
:param image_shape: Tuple - Shape of image
:return:
"""
def get_batches_fn(batch_size):
"""
Create batches of training data
:param batch_size: Batch Size
:return: Batches of training data
"""
image_paths = glob(os.path.join(data_folder, 'image_2', '*.png'))
label_paths = {
re.sub(r'_(lane|road)_', '_', os.path.basename(path)): path
for path in glob(os.path.join(data_folder, 'gt_image_2', '*_road_*.png'))}
background_color = np.array([255, 0, 0])
random.shuffle(image_paths)
for batch_i in range(0, len(image_paths), batch_size):
images = []
gt_images = []
for image_file in image_paths[batch_i:batch_i + batch_size]:
gt_image_file = label_paths[os.path.basename(image_file)]
image = scipy.misc.imresize(scipy.misc.imread(image_file), image_shape)
gt_image = scipy.misc.imresize(scipy.misc.imread(gt_image_file), image_shape)
gt_bg = np.all(gt_image == background_color, axis=2)
gt_bg = gt_bg.reshape(*gt_bg.shape, 1)
gt_image = np.concatenate((gt_bg, np.invert(gt_bg)), axis=2)
images.append(image)
gt_images.append(gt_image)
yield np.array(images), np.array(gt_images)
return get_batches_fn
test_extras.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
def test_in1d_invert(self):
# Test in1d's invert parameter
a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
a = array([5, 5, 2, 1, -1], mask=[0, 0, 0, 0, 1])
b = array([1, 5, -1], mask=[0, 0, 1])
assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
assert_array_equal([], in1d([], [], invert=True))
def relabel_half_side_one_label(in_data, label_old, label_new, side_to_modify, axis, plane_intercept):
"""
:param in_data:
:param label_old:
:param label_new:
:param side_to_copy:
:param axis:
:param plane_intercept:
:return:
"""
msg = 'Input array must be 3-dimensional.'
assert in_data.ndim == 3, msg
msg = 'side_to_copy must be one of the two {}.'.format(['below', 'above'])
assert side_to_modify in ['below', 'above'], msg
msg = 'axis variable must be one of the following: {}.'.format(['x', 'y', 'z'])
assert axis in ['x', 'y', 'z'], msg
positions = in_data == label_old
halfed_positions = np.zeros_like(positions)
if axis == 'x':
if side_to_modify == 'above':
halfed_positions[plane_intercept:, :, :] = positions[plane_intercept:, :, :]
if side_to_modify == 'below':
halfed_positions[:plane_intercept, :, :] = positions[:plane_intercept, :, :]
if axis == 'y':
if side_to_modify == 'above':
halfed_positions[: ,plane_intercept:, :] = positions[:, plane_intercept:, :]
if side_to_modify == 'below':
halfed_positions[:, plane_intercept, :, :] = positions[:, plane_intercept, :]
if axis == 'z':
if side_to_modify == 'above':
halfed_positions[ :, :, plane_intercept:] = positions[ :, :, plane_intercept:]
if side_to_modify == 'below':
halfed_positions[:, :, :plane_intercept] = positions[:, :, :plane_intercept]
new_data = in_data * np.invert(halfed_positions) + label_new * halfed_positions.astype(np.int)
return new_data
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 setdiff1d(ar1, ar2, assume_unique=False):
"""
Find the set difference of two arrays.
Return the sorted, unique values in `ar1` that are not in `ar2`.
Parameters
----------
ar1 : array_like
Input array.
ar2 : array_like
Input comparison array.
assume_unique : bool
If True, the input arrays are both assumed to be unique, which
can speed up the calculation. Default is False.
Returns
-------
setdiff1d : ndarray
Sorted 1D array of values in `ar1` that are not in `ar2`.
See Also
--------
numpy.lib.arraysetops : Module with a number of other functions for
performing set operations on arrays.
Examples
--------
>>> a = np.array([1, 2, 3, 2, 4, 1])
>>> b = np.array([3, 4, 5, 6])
>>> np.setdiff1d(a, b)
array([1, 2])
"""
if assume_unique:
ar1 = np.asarray(ar1).ravel()
else:
ar1 = unique(ar1)
ar2 = unique(ar2)
return ar1[in1d(ar1, ar2, assume_unique=True, invert=True)]
def test_in1d_invert(self):
# Test in1d's invert parameter
a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
a = array([5, 5, 2, 1, -1], mask=[0, 0, 0, 0, 1])
b = array([1, 5, -1], mask=[0, 0, 1])
assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
assert_array_equal([], in1d([], [], invert=True))
def get_bonus(self,path):
if self._fit_steps > self._yield_zeros_until:
bonus = self._coeff * self._f_predict(path['observations']).reshape(-1)
if self._filter_bonuses:
bonus = bonus * (np.invert(self._wrapped_constraint.evaluate(path)))
return bonus
else:
return np.zeros(path["rewards"].size)
def show_isomap(window, isomap):
#isomap_copy = isomap.copy()
background = np.zeros([ISOMAP_SIZE, ISOMAP_SIZE, 4], dtype='uint8')
background[:,:,3]=10
mask = np.array([[int(x/8) %2==int(y/8) %2 for x in range(isomap.shape[0])] for y in range(isomap.shape[1])])
#mask = np.array([[int(x/8) %2==0 for x in range(isomap.shape[0])] for y in range(isomap.shape[1])])
background[mask,:3]=[200,200,200]
mask = np.invert(mask)
background[mask,:3]=[150,150,150]
cv2.imshow(window, merge([background,isomap]))
def apply_mask(im, mask):
im[np.invert(mask.astype(np.bool))] = 0
return np.transpose(im, (1, 2, 0))
def mask_od_vessels(skel, od_center):
# Create optic disk mask
od_mask = np.zeros_like(skel, dtype=np.uint8)
cv2.circle(od_mask, od_center, 30, (1, 1, 1), -1)
od_mask_inv = np.invert(od_mask) / 255.
skel = skel.astype(np.float)
masked_skel = skel * od_mask_inv
return masked_skel.astype(np.uint8)
# def line_diameters(edt, lines):
#
# diameters = []
#
# for line in lines:
#
# p0, p1 = [np.asarray(pt) for pt in line]
# vec = p1 - p0 # vector between segment end points
# vec_len = np.linalg.norm(vec)
#
# pts_along_line = np.uint(np.asarray([p0 + (i * vec) for i in np.arange(0., 1., 1. / vec_len)]))
#
# for pt in pts_along_line:
#
# try:
# diameters.append(edt[pt[0], pt[1]])
# except IndexError:
# pass
#
# return diameters
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 setdiff1d(ar1, ar2, assume_unique=False):
"""
Find the set difference of two arrays.
Return the sorted, unique values in `ar1` that are not in `ar2`.
Parameters
----------
ar1 : array_like
Input array.
ar2 : array_like
Input comparison array.
assume_unique : bool
If True, the input arrays are both assumed to be unique, which
can speed up the calculation. Default is False.
Returns
-------
setdiff1d : ndarray
Sorted 1D array of values in `ar1` that are not in `ar2`.
See Also
--------
numpy.lib.arraysetops : Module with a number of other functions for
performing set operations on arrays.
Examples
--------
>>> a = np.array([1, 2, 3, 2, 4, 1])
>>> b = np.array([3, 4, 5, 6])
>>> np.setdiff1d(a, b)
array([1, 2])
"""
if assume_unique:
ar1 = np.asarray(ar1).ravel()
else:
ar1 = unique(ar1)
ar2 = unique(ar2)
return ar1[in1d(ar1, ar2, assume_unique=True, invert=True)]
def test_in1d_invert(self):
# Test in1d's invert parameter
a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
a = array([5, 5, 2, 1, -1], mask=[0, 0, 0, 0, 1])
b = array([1, 5, -1], mask=[0, 0, 1])
assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
assert_array_equal([], in1d([], [], invert=True))
def positions(X, V_s, stoc_vector,domain_enum):
''' Get the positions of the previous positions. '''
# X is the state space vector. N \times N_s
# stoc_vector is a vector $N_s$ with 1 when a variable is stochastic and zero otherwise.
# Initialising the positions
##pdb.set_trace()
N = X.shape[1] # Number of states.
N_s = np.sum(stoc_vector)
N_r_s = len(V_s) # N_r_s is the number of propensities which are purely stochastic ( N_r_s = len(V_s))
position = np.zeros((N,N_r_s),dtype=np.int64)
valid = np.zeros((N,N_r_s),dtype=np.bool)
#shift_M = np.zeros((N_r_s,N,N_s),dtype=np.int)
# Loops through the stochiometry and find the coresponding indexes.
##pdb.set_trace()
for i in range(N_r_s):
pre_states = X - np.array(V_s[i])[:,np.newaxis]
interior = domain_enum.contains(pre_states)
#print("shape In" + str(interior.shape))
#print("shape valid" + str(valid[:,i].shape))
valid[:,i] = interior
#exterior = np.invert(interior)
if np.sum(valid[:,i]) >0:
position[interior,i] = domain_enum.indices(pre_states[:,interior])
return valid, position
def derivative_G(propensities,V,X,w,deter_vector,stoc_positions, positions, valid):
# just the deterministics
X_d = X[deter_vector,:].copy()
temp_eta = np.zeros((np.sum(deter_vector),X.shape[1]))
j = 0
for i in range(len(stoc_positions)):
##pdb.set_trace()
# If x-\nu_i is non zero
if stoc_positions[i] == True:
if np.sum(valid[:,j]) != 0:
#print(" X shape: " + str(X.shape))
#print(" w shape: " + str(w.shape))
#print("test :" + str(map(propensities[i],*X[:,positions[valid[:,j]][:,j]])))
temp_eta[:,valid[:,j]] += (X_d[:,positions[valid[:,j]][:,j]]
- X_d[:,valid[:,j]] +
V[i][deter_vector][:,np.newaxis]
)*map(propensities[i],* X[:,positions[valid[:,j]][:,j]])*w[positions[valid[:,j]][:,j]]
j += 1
else:
temp_eta[:,:] += (V[i][deter_vector][:,np.newaxis])*map(propensities[i],* X)*w
return_X = np.zeros(X.shape)
return_X[deter_vector,:] = temp_eta
return_X[np.invert(deter_vector),:] = X[np.invert(deter_vector),:].copy()
return return_X
#return temp_eta
def derivative_G(propensities,V,X,w,deter_vector,stoc_positions, positions, valid,jac):
# just the deterministics
X_d = X[deter_vector,:].copy()
temp_eta = np.zeros((np.sum(deter_vector),X.shape[1]))
j = 0
for i in range(len(stoc_positions)):
# If x-\nu_i is non zero
if stoc_positions[i] == True:
if np.sum(valid[:,j]) != 0:
#print(" X shape: " + str(X.shape))
#print(" w shape: " + str(w.shape))
#print("test :" + str(map(propensities[i],*X[:,positions[valid[:,j]][:,j]])))
# original Terms
temp_eta[:,valid[:,j]] += (X_d[:,positions[valid[:,j]][:,j]]
- X_d[:,valid[:,j]] +
V[i][deter_vector][:,np.newaxis]
)*map(propensities[i],* X[:,positions[valid[:,j]][:,j]])*w[positions[valid[:,j]][:,j]]
# Correction terms
# x terms
temp_eta[:,:] -= jac(X,deter_vector,i)*w[np.newaxis,:] # these should be all the terms which are minusing out.
# x-v_j term.
temp_eta[:,valid[:,j]] += jac(X[:,positions[valid[:,j]][:,j]],deter_vector,i)*w[positions[valid[:,j]][:,j]][np.newaxis,:]
j += 1
else:
temp_eta[:,:] += (V[i][deter_vector][:,np.newaxis])*map(propensities[i],* X)*w
#return_X = np.zeros(X.shape)
#return_X[deter_vector,:] = temp_eta
#return_X[np.invert(deter_vector),:] = X[np.invert(deter_vector),:].copy()
return temp_eta
def positions(X, V_s, stoc_vector,domain_enum):
''' Get the positions of the previous positions. '''
# X is the state space vector. N \times N_s
# stoc_vector is a vector $N_s$ with 1 when a variable is stochastic and zero otherwise.
# Initialising the positions
##pdb.set_trace()
N = X.shape[1] # Number of states.
N_s = np.sum(stoc_vector)
N_r_s = len(V_s) # N_r_s is the number of propensities which are purely stochastic ( N_r_s = len(V_s))
position = np.zeros((N,N_r_s),dtype=np.int64)
valid = np.zeros((N,N_r_s),dtype=np.bool)
#shift_M = np.zeros((N_r_s,N,N_s),dtype=np.int)
# Loops through the stochiometry and find the coresponding indexes.
##pdb.set_trace()
for i in range(N_r_s):
pre_states = X - np.array(V_s[i])[:,np.newaxis]
interior = domain_enum.contains(pre_states)
#print("shape In" + str(interior.shape))
#print("shape valid" + str(valid[:,i].shape))
valid[:,i] = interior
#exterior = np.invert(interior)
if np.sum(valid[:,i]) >0:
position[interior,i] = domain_enum.indices(pre_states[:,interior])
return valid, position
def derivative_G(propensities,V,X,w,deter_vector,stoc_positions, positions, valid):
# just the deterministics
X_d = X[deter_vector,:].copy()
temp_eta = np.zeros((np.sum(deter_vector),X.shape[1]))
j = 0
for i in range(len(stoc_positions)):
##pdb.set_trace()
# If x-\nu_i is non zero
if stoc_positions[i] == True:
if np.sum(valid[:,j]) != 0:
#print(" X shape: " + str(X.shape))
#print(" w shape: " + str(w.shape))
#print("test :" + str(map(propensities[i],*X[:,positions[valid[:,j]][:,j]])))
temp_eta[:,valid[:,j]] += (X_d[:,positions[valid[:,j]][:,j]]
- X_d[:,valid[:,j]] +
V[i][deter_vector][:,np.newaxis]
)*map(propensities[i],* X[:,positions[valid[:,j]][:,j]])*w[positions[valid[:,j]][:,j]]
j += 1
else:
temp_eta[:,:] += (V[i][deter_vector][:,np.newaxis])*map(propensities[i],* X)*w
return_X = np.zeros(X.shape)
return_X[deter_vector,:] = temp_eta
return_X[np.invert(deter_vector),:] = X[np.invert(deter_vector),:].copy()
return return_X
#return temp_eta
def ff(self,x):
n_nodes = self.n_nodes
assert len(x) == n_nodes[0]
self.nas[0:n_nodes[0]] = x # input node_a's
# pl_ : of previous (left) layer
pl_nas = np.append([1.0],self.nas[0:n_nodes[0]])
for l in range(1,len(n_nodes)):
thsM = self.__get_thsM(l-1)
nzs = self.__get_nzs(l)
nas = self.__get_nas(l)
nzs[:] = np.dot(thsM,pl_nas)
# ??? ??? cross-entropy? ???? ???
# ??? ???? sigmoid? ?????
# ??? ??? quadric? ?????
# ??? ???? activate ?? ??? ?? ??? ?
if (l<len(n_nodes)-1):
nas[:] = self.activate(nzs)
else:
nas[:] = self.__sigmoid(nzs)
# ???? ????? traing ?? testing ?? ??
# ????? ???.
if (self.doDropout):
dropout = self.__get_dropout(l)
nas[:] = nas*np.invert(dropout)
else:
nas[:] = nas*(1.0-self.DORATE)
pl_nas = nas
pl_nas = np.append([1.0],pl_nas) # add bias node