def draw_flow(img, flow, step=16):
h, w = img.shape[:2]
y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1)
fx, fy = flow[y,x].T
m = np.bitwise_and(np.isfinite(fx), np.isfinite(fy))
lines = np.vstack([x[m], y[m], x[m]+fx[m], y[m]+fy[m]]).T.reshape(-1, 2, 2)
lines = np.int32(lines + 0.5)
vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.polylines(vis, lines, 0, (0, 255, 0))
for (x1, y1), (x2, y2) in lines:
cv2.circle(vis, (x1, y1), 1, (0, 255, 0), -1)
return vis
python类bitwise_and()的实例源码
def logpdf(self, samples):
'''
Calculates the log of the probability density function.
Parameters
----------
samples : array_like
n-by-2 matrix of samples where n is the number of samples.
Returns
-------
vals : ndarray
Log of the probability density function evaluated at `samples`.
'''
samples = np.copy(np.asarray(samples))
samples = self.__rotate_input(samples)
inner = np.all(np.bitwise_and(samples > 0.0, samples < 1.0), axis=1)
outer = np.invert(inner)
vals = np.zeros(samples.shape[0])
vals[inner] = self._logpdf(samples[inner, :])
# Assign zero mass to border
vals[outer] = -np.inf
return vals
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def get_rgb_mask(img, debug=False):
assert isinstance(img, numpy.ndarray), 'image must be a numpy array'
assert img.ndim == 3, 'skin detection can only work on color images'
logger.debug('getting rgb mask')
lower_thresh = numpy.array([45, 52, 108], dtype=numpy.uint8)
upper_thresh = numpy.array([255, 255, 255], dtype=numpy.uint8)
mask_a = cv2.inRange(img, lower_thresh, upper_thresh)
mask_b = 255 * ((img[:, :, 2] - img[:, :, 1]) / 20)
mask_c = 255 * ((numpy.max(img, axis=2) - numpy.min(img, axis=2)) / 20)
mask_d = numpy.bitwise_and(numpy.uint64(mask_a), numpy.uint64(mask_b))
# mask = numpy.zeros_like(mask_d, dtype=numpy.uint8)
msk_rgb = numpy.bitwise_and(numpy.uint64(mask_c), numpy.uint64(mask_d))
# msk_rgb = cv2.fromarray(mask_rgb)
msk_rgb[msk_rgb < 128] = 0
msk_rgb[msk_rgb >= 128] = 1
if debug:
scripts.display('input', img)
scripts.display('mask_rgb', msk_rgb)
return msk_rgb.astype(float)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
test_ufunc.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def agent_start(self, observation):
# Preprocess
tmp = np.bitwise_and(np.asarray(observation.intArray[128:]).reshape([210, 160]), 0b0001111) # Get Intensity from the observation
obs_array = (spm.imresize(tmp, (110, 84)))[110-84-8:110-8, :] # Scaling
# Initialize State
self.state = np.zeros((4, 84, 84), dtype=np.uint8)
self.state[0] = obs_array
state_ = cuda.to_gpu(np.asanyarray(self.state.reshape(1, 4, 84, 84), dtype=np.float32))
# Generate an Action e-greedy
returnAction = Action()
action, Q_now = self.DDQN.e_greedy(state_, self.epsilon)
returnAction.intArray = [action]
# Update for next step
self.lastAction = copy.deepcopy(returnAction)
self.last_state = self.state.copy()
self.last_observation = obs_array
return returnAction
def create_mask_from_bitmask(geoimg, filename=''):
""" Mask geoimg with a series of provided bitmasks """
# medium and high confidence clouds
nodata = int('0000000000000001', 2)
clouds = int('1000000000000000', 2)
cirrus = int('0011000000000000', 2)
# calculate mask
arr = geoimg.read().astype('int16')
# it is a good data mask
mask = (np.bitwise_and(arr, nodata) != nodata) & \
(np.bitwise_and(arr, clouds) < clouds) & \
(np.bitwise_and(arr, cirrus) < cirrus)
# create mask file
logger.info('Saving to file %s' % filename, action='Save file', actee=filename, actor=__name__)
maskimg = GeoImage.create_from(geoimg, filename=filename, dtype='uint8')
maskimg.set_nodata(0)
maskimg[0].write(mask.astype('uint8'))
return maskimg
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def test_values(self):
for dt in self.bitwise_types:
zeros = np.array([0], dtype=dt)
ones = np.array([-1], dtype=dt)
msg = "dt = '%s'" % dt.char
assert_equal(np.bitwise_not(zeros), ones, err_msg=msg)
assert_equal(np.bitwise_not(ones), zeros, err_msg=msg)
assert_equal(np.bitwise_or(zeros, zeros), zeros, err_msg=msg)
assert_equal(np.bitwise_or(zeros, ones), ones, err_msg=msg)
assert_equal(np.bitwise_or(ones, zeros), ones, err_msg=msg)
assert_equal(np.bitwise_or(ones, ones), ones, err_msg=msg)
assert_equal(np.bitwise_xor(zeros, zeros), zeros, err_msg=msg)
assert_equal(np.bitwise_xor(zeros, ones), ones, err_msg=msg)
assert_equal(np.bitwise_xor(ones, zeros), ones, err_msg=msg)
assert_equal(np.bitwise_xor(ones, ones), zeros, err_msg=msg)
assert_equal(np.bitwise_and(zeros, zeros), zeros, err_msg=msg)
assert_equal(np.bitwise_and(zeros, ones), zeros, err_msg=msg)
assert_equal(np.bitwise_and(ones, zeros), zeros, err_msg=msg)
assert_equal(np.bitwise_and(ones, ones), ones, err_msg=msg)
def set_ufunc(self, scalar_op):
# This is probably a speed up of the implementation
if isinstance(scalar_op, theano.scalar.basic.Add):
self.ufunc = numpy.add
elif isinstance(scalar_op, theano.scalar.basic.Mul):
self.ufunc = numpy.multiply
elif isinstance(scalar_op, theano.scalar.basic.Maximum):
self.ufunc = numpy.maximum
elif isinstance(scalar_op, theano.scalar.basic.Minimum):
self.ufunc = numpy.minimum
elif isinstance(scalar_op, theano.scalar.basic.AND):
self.ufunc = numpy.bitwise_and
elif isinstance(scalar_op, theano.scalar.basic.OR):
self.ufunc = numpy.bitwise_or
elif isinstance(scalar_op, theano.scalar.basic.XOR):
self.ufunc = numpy.bitwise_xor
else:
self.ufunc = numpy.frompyfunc(scalar_op.impl, 2, 1)
def parseNpf(self, buffer, imageWidth, imageHeight):
# Read the header
sectionLengths = self._readUgarHeader(buffer)
# Read the palette data (section number 1)
paletteData = np.frombuffer(buffer.read(roundToPower(sectionLengths[0])), dtype=np.uint16)
# Read the image data (section number 2)
imageData = np.frombuffer(buffer.read(sectionLengths[1]), dtype=np.uint8)
# NPF image data uses 1 byte per 2 pixels, so we need to split that byte into two
imageData = np.stack((np.bitwise_and(imageData, 0x0f), np.bitwise_and(imageData >> 4, 0x0f)), axis=-1).flatten()
# Unpack palette colors
palette = unpackColors(paletteData, useAlpha=False)
# Convert each pixel from a palette index to full color
pixels = np.fromiter((palette[i] if i > 0 else 0 for i in imageData), dtype=">u4")
# Clip the image data and create a Pillow image from it
return Image.fromarray(self._clipImageData(pixels, (imageWidth, imageHeight)), mode="RGBA")
# Write the image as an npf to buffer
def __init__(self, imageBuffer, size):
width, height = self.get_size(size)
self.size = size
self.surface = Surface((width, height), depth=8)
# Read the header
paletteLength, imageDataLength = self.read_ugar_header(imageBuffer)
# Read the image palette and unpack
palette = self.unpack_palette(np.fromstring(imageBuffer.read(self.round_to_power(paletteLength)), dtype=np.uint16))
self.surface.set_palette(palette)
# All pixels with the index of 0 are transparent
self.surface.set_colorkey(0)
# Read the pixel data bytes
pixelData = np.fromstring(imageBuffer.read(imageDataLength), dtype=np.uint8)
# Split each byte into 2 pixels
pixels = np.stack((np.bitwise_and(pixelData, 0x0f), np.bitwise_and(pixelData >> 4, 0x0f)), axis=-1).flatten()
pixels = np.swapaxes(np.reshape(pixels, (-1, width)), 0, 1)
pixelcopy.array_to_surface(self.surface, pixels)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def test_uw_rgbd_object():
from pybot.vision.image_utils import to_color
from pybot.vision.imshow_utils import imshow_cv
object_directory = '~/data/rgbd_datasets/udub/rgbd-object-crop/rgbd-dataset'
rgbd_data_uw = UWRGBDObjectDataset(directory=object_directory)
for f in rgbd_data_uw.iteritems(every_k_frames=5):
bbox = f.bbox
imshow_cv('frame',
np.hstack([f.img, np.bitwise_and(f.img, to_color(f.mask))]),
text='Image + Mask [Category: [%i] %s, Instance: %i]' %
(bbox['category'], rgbd_data_uw.get_category_name(bbox['category']), bbox['instance']))
imshow_cv('depth', (f.depth / 16).astype(np.uint8), text='Depth')
def project(self, X, check_bounds=False, check_depth=False, return_depth=False, min_depth=0.1):
"""
Project [Nx3] points onto 2-D image plane [Nx2]
"""
R, t = self.to_Rt()
rvec,_ = cv2.Rodrigues(R)
proj,_ = cv2.projectPoints(X, rvec, t, self.K, self.D)
x = proj.reshape(-1,2)
if check_depth:
# Only return positive depths
depths = self.depth_from_projection(X)
valid = depths >= min_depth
else:
valid = np.ones(len(x), dtype=np.bool)
if check_bounds:
if self.shape is None:
raise ValueError('check_bounds cannot proceed. Camera.shape is not set')
# Only return points within-image bounds
valid = np.bitwise_and(
valid, np.bitwise_and(
np.bitwise_and(x[:,0] >= 0, x[:,0] < self.shape[1]), \
np.bitwise_and(x[:,1] >= 0, x[:,1] < self.shape[0]))
)
if return_depth:
return x[valid], depths[valid]
return x[valid]
def get_bounded_projection(camera, pts, subsample=10):
""" Project points and only return points that are within image bounds """
# Project points
pts2d = camera.project(pts[::subsample].astype(np.float32))
# Only return points within-image bounds
valid = np.bitwise_and(np.bitwise_and(pts2d[:,0] >= 0, pts2d[:,0] < camera.shape[1]), \
np.bitwise_and(pts2d[:,1] >= 0, pts2d[:,1] < camera.shape[0]))
return pts2d[valid], valid
def bbox_inbounds(bb, shape):
assert(shape[1] > shape[0] and bb.shape[1] == 4)
return np.all(np.bitwise_and(np.bitwise_and(bb[:,0] >= 0, bb[:,2] < shape[1]), \
np.bitwise_and(bb[:,1] >= 0, bb[:,3] < shape[0])))
def match_targets(bboxes_truth, bboxes_test, intersection_th=0.5):
A = brute_force_match_coords(bboxes_truth, bboxes_test)
B = brute_force_match_target(bboxes_truth, bboxes_test)
pos = np.bitwise_and(A > intersection_th, B)
return pos
def finite_and_within_bounds(xys, shape):
H, W = shape[:2]
if not len(xys):
return np.array([])
return np.bitwise_and(np.isfinite(xys).all(axis=1),
reduce(lambda x,y: np.bitwise_and(x,y), [xys[:,0] >= 0, xys[:,0] < W,
xys[:,1] >= 0, xys[:,1] < H]))
def detect(self, im, mask=None):
edges = self.detect_edges_(im)
# edges1 = edges.copy()
if mask is not None:
edges = np.bitwise_and(edges, mask)
# cv2.imshow('mask', np.hstack([edges1, edges]))
kpts = self.detector_.detect(im, mask=edges)
return kpts
def getCrop(self, dpt, xstart, xend, ystart, yend, zstart, zend, thresh_z=True):
"""
Crop patch from image
:param dpt: depth image to crop from
:param xstart: start x
:param xend: end x
:param ystart: start y
:param yend: end y
:param zstart: start z
:param zend: end z
:param thresh_z: threshold z values
:return: cropped image
"""
if len(dpt.shape) == 2:
cropped = dpt[max(ystart, 0):min(yend, dpt.shape[0]), max(xstart, 0):min(xend, dpt.shape[1])].copy()
# add pixels that are out of the image in order to keep aspect ratio
cropped = numpy.pad(cropped, ((abs(ystart)-max(ystart, 0),
abs(yend)-min(yend, dpt.shape[0])),
(abs(xstart)-max(xstart, 0),
abs(xend)-min(xend, dpt.shape[1]))), mode='constant', constant_values=0)
elif len(dpt.shape) == 3:
cropped = dpt[max(ystart, 0):min(yend, dpt.shape[0]), max(xstart, 0):min(xend, dpt.shape[1]), :].copy()
# add pixels that are out of the image in order to keep aspect ratio
cropped = numpy.pad(cropped, ((abs(ystart)-max(ystart, 0),
abs(yend)-min(yend, dpt.shape[0])),
(abs(xstart)-max(xstart, 0),
abs(xend)-min(xend, dpt.shape[1])),
(0, 0)), mode='constant', constant_values=0)
else:
raise NotImplementedError()
if thresh_z is True:
msk1 = numpy.bitwise_and(cropped < zstart, cropped != 0)
msk2 = numpy.bitwise_and(cropped > zend, cropped != 0)
cropped[msk1] = zstart
cropped[msk2] = 0. # backface is at 0, it is set later
return cropped
def _ccdf(self, samples):
vals = np.zeros(samples.shape[0])
# Avoid subtraction of infinities
neqz = np.bitwise_and(np.any(samples > 0.0, axis=1),
np.any(samples < 1.0, axis=1))
nrvs = norm.ppf(samples[neqz, :])
vals[neqz] = norm.cdf((nrvs[:, 0] - self.theta * nrvs[:, 1])
/ np.sqrt(1 - self.theta**2))
vals[np.invert(neqz)] = norm.cdf(0.0)
return vals
def roi_from_bbox(
input_file,
bbox,
output_file):
""" Create a ROI image from a bounding box.
Parameters
----------
input_file: str
the reference image where the bbox is defined.
bbox: 6-uplet
the corner of the bbox in voxel coordinates: xmin, xmax, ymin, ymax,
zmin, zmax.
output_file: str
the desired ROI image file.
"""
# Load the reference image and generate a grid
im = nibabel.load(input_file)
xv, yv, zv = numpy.meshgrid(
numpy.linspace(0, im.shape[0] - 1, im.shape[0]),
numpy.linspace(0, im.shape[1] - 1, im.shape[1]),
numpy.linspace(0, im.shape[2] - 1, im.shape[2]))
xv = xv.astype(int)
yv = yv.astype(int)
zv = zv.astype(int)
# Intersect the grid with the bbox
xa = numpy.bitwise_and(xv >= bbox[0], xv <= bbox[1])
ya = numpy.bitwise_and(yv >= bbox[2], yv <= bbox[3])
za = numpy.bitwise_and(zv >= bbox[4], zv <= bbox[5])
# Generate bbox indices
indices = numpy.bitwise_and(numpy.bitwise_and(xa, ya), za)
# Generate/save ROI
roi = numpy.zeros(im.shape, dtype=int)
roi[xv[indices].tolist(), yv[indices].tolist(), zv[indices].tolist()] = 1
roi_im = nibabel.Nifti1Image(roi, affine=im.get_affine())
nibabel.save(roi_im, output_file)
def _rgb_integers_to_components(self, rgb_integers):
red_mask = 0x00FF0000
green_mask = 0x0000FF00
blue_mask = 0x000000FF
masks = np.asarray([[red_mask, green_mask, blue_mask]])
masked_rgb_components = np.bitwise_and(rgb_integers, masks)
red_shifted = np.right_shift(masked_rgb_components[:,0], 16)
green_shifted = np.right_shift(masked_rgb_components[:,1], 8)
blue_shifted = np.right_shift(masked_rgb_components[:,2], 0)
return np.array([red_shifted, green_shifted, blue_shifted]).transpose()
def load_feature_run_model(bundle_hdf_path, prediction_csv_path):
with h5py.File(bundle_hdf_path, 'r') as bundle_f:
is_valid = bundle_f['info']['is_valid'].value
is_test = bundle_f['info']['is_test'].value
passenger_id = bundle_f['id'].value
label = bundle_f['label'].value
# concated
feature = bundle_f['features'].value
train_filter = (np.bitwise_and(~is_valid, ~is_test))
valid_filter = (np.bitwise_and(is_valid, ~is_test))
test_filter = is_test
##############
# validation #
##############
clf = RandomForestClassifier()
clf.fit(feature[train_filter], label[train_filter])
print('validation score: (Accuracy)',
clf.score(feature[valid_filter], label[valid_filter]))
##############
# prediction #
##############
clf.fit(feature[~test_filter], label[~test_filter])
prediction = clf.predict(feature[test_filter])
df = pd.DataFrame(prediction, columns=['Survived'],
index=passenger_id[test_filter])
df.index.rename('PassengerId')
df.to_csv(prediction_csv_path)
def test_truth_table_bitwise(self):
arg1 = [False, False, True, True]
arg2 = [False, True, False, True]
out = [False, True, True, True]
assert_equal(np.bitwise_or(arg1, arg2), out)
out = [False, False, False, True]
assert_equal(np.bitwise_and(arg1, arg2), out)
out = [False, True, True, False]
assert_equal(np.bitwise_xor(arg1, arg2), out)
def __and__(self, other):
return bitwise_and(self, other)
def __iand__(self, other):
return bitwise_and(self, other, self)