def reset(self):
self.cur = 0
if self.shuffle:
if self.aspect_grouping:
widths = np.array([r['width'] for r in self.roidb])
heights = np.array([r['height'] for r in self.roidb])
horz = (widths >= heights)
vert = np.logical_not(horz)
horz_inds = np.where(horz)[0]
vert_inds = np.where(vert)[0]
inds = np.hstack((np.random.permutation(horz_inds), np.random.permutation(vert_inds)))
extra = inds.shape[0] % self.batch_size
inds_ = np.reshape(inds[:-extra], (-1, self.batch_size))
row_perm = np.random.permutation(np.arange(inds_.shape[0]))
inds[:-extra] = np.reshape(inds_[row_perm, :], (-1,))
self.index = inds
else:
np.random.shuffle(self.index)
python类logical_not()的实例源码
def reset(self):
self.cur = 0
if self.shuffle:
if self.aspect_grouping:
widths = np.array([r['width'] for r in self.roidb])
heights = np.array([r['height'] for r in self.roidb])
horz = (widths >= heights)
vert = np.logical_not(horz)
horz_inds = np.where(horz)[0]
vert_inds = np.where(vert)[0]
inds = np.hstack((np.random.permutation(horz_inds), np.random.permutation(vert_inds)))
extra = inds.shape[0] % self.batch_size
inds_ = np.reshape(inds[:-extra], (-1, self.batch_size))
row_perm = np.random.permutation(np.arange(inds_.shape[0]))
inds[:-extra] = np.reshape(inds_[row_perm, :], (-1,))
self.index = inds
else:
np.random.shuffle(self.index)
def replay(self):
"""Memory Management and training of the agent
"""
if len(self.memory) < self.batch_size:
return
state, action, reward, next_state, done = self._get_batches()
reward += (self.gamma
* np.logical_not(done)
* np.amax(self.model.predict(next_state), axis=1))
q_target = self.target_model.predict(state)
_ = pd.Series(action)
one_hot = pd.get_dummies(_).as_matrix()
action_batch = np.where(one_hot == 1)
q_target[action_batch] = reward
return self.model.fit(state, q_target,
batch_size=self.batch_size,
epochs=1,
verbose=False)
def _shuffle_roidb_inds(self):
"""Randomly permute the training roidb."""
if cfg.TRAIN.ASPECT_GROUPING:
widths = np.array([r['width'] for r in self._roidb])
heights = np.array([r['height'] for r in self._roidb])
horz = (widths >= heights)
vert = np.logical_not(horz)
horz_inds = np.where(horz)[0]
vert_inds = np.where(vert)[0]
inds = np.hstack((
np.random.permutation(horz_inds),
np.random.permutation(vert_inds)))
inds = np.reshape(inds, (-1, 2))
row_perm = np.random.permutation(np.arange(inds.shape[0]))
inds = np.reshape(inds[row_perm, :], (-1,))
self._perm = inds
else:
self._perm = np.random.permutation(np.arange(len(self._roidb)))
self._cur = 0
def reset(self):
self.cur = 0
if self.shuffle:
if self.aspect_grouping:
widths = np.array([r['width'] for r in self.roidb])
heights = np.array([r['height'] for r in self.roidb])
horz = (widths >= heights)
vert = np.logical_not(horz)
horz_inds = np.where(horz)[0]
vert_inds = np.where(vert)[0]
inds = np.hstack((np.random.permutation(horz_inds), np.random.permutation(vert_inds)))
extra = inds.shape[0] % self.batch_size
inds_ = np.reshape(inds[:-extra], (-1, self.batch_size))
row_perm = np.random.permutation(np.arange(inds_.shape[0]))
inds[:-extra] = np.reshape(inds_[row_perm, :], (-1,))
self.index = inds
else:
np.random.shuffle(self.index)
def reset(self):
self.cur = 0
if self.shuffle:
if self.aspect_grouping:
widths = np.array([r['width'] for r in self.roidb])
heights = np.array([r['height'] for r in self.roidb])
horz = (widths >= heights)
vert = np.logical_not(horz)
horz_inds = np.where(horz)[0]
vert_inds = np.where(vert)[0]
inds = np.hstack((np.random.permutation(horz_inds), np.random.permutation(vert_inds)))
extra = inds.shape[0] % self.batch_size
inds_ = np.reshape(inds[:-extra], (-1, self.batch_size))
row_perm = np.random.permutation(np.arange(inds_.shape[0]))
inds[:-extra] = np.reshape(inds_[row_perm, :], (-1,))
self.index = inds
else:
np.random.shuffle(self.index)
def test_object_logical(self):
a = np.array([3, None, True, False, "test", ""], dtype=object)
assert_equal(np.logical_or(a, None),
np.array([x or None for x in a], dtype=object))
assert_equal(np.logical_or(a, True),
np.array([x or True for x in a], dtype=object))
assert_equal(np.logical_or(a, 12),
np.array([x or 12 for x in a], dtype=object))
assert_equal(np.logical_or(a, "blah"),
np.array([x or "blah" for x in a], dtype=object))
assert_equal(np.logical_and(a, None),
np.array([x and None for x in a], dtype=object))
assert_equal(np.logical_and(a, True),
np.array([x and True for x in a], dtype=object))
assert_equal(np.logical_and(a, 12),
np.array([x and 12 for x in a], dtype=object))
assert_equal(np.logical_and(a, "blah"),
np.array([x and "blah" for x in a], dtype=object))
assert_equal(np.logical_not(a),
np.array([not x for x in a], dtype=object))
assert_equal(np.logical_or.reduce(a), 3)
assert_equal(np.logical_and.reduce(a), None)
def test_2d_w_missing(self):
# Test cov on 2D variable w/ missing value
x = self.data
x[-1] = masked
x = x.reshape(3, 4)
valid = np.logical_not(getmaskarray(x)).astype(int)
frac = np.dot(valid, valid.T)
xf = (x - x.mean(1)[:, None]).filled(0)
assert_almost_equal(cov(x),
np.cov(xf) * (x.shape[1] - 1) / (frac - 1.))
assert_almost_equal(cov(x, bias=True),
np.cov(xf, bias=True) * x.shape[1] / frac)
frac = np.dot(valid.T, valid)
xf = (x - x.mean(0)).filled(0)
assert_almost_equal(cov(x, rowvar=False),
(np.cov(xf, rowvar=False) *
(x.shape[0] - 1) / (frac - 1.)))
assert_almost_equal(cov(x, rowvar=False, bias=True),
(np.cov(xf, rowvar=False, bias=True) *
x.shape[0] / frac))
def __ipow__(self, other):
"""
Raise self to the power other, in place.
"""
other_data = getdata(other)
other_mask = getmask(other)
with np.errstate(divide='ignore', invalid='ignore'):
self._data.__ipow__(np.where(self._mask, self.dtype.type(1),
other_data))
invalid = np.logical_not(np.isfinite(self._data))
if invalid.any():
if self._mask is not nomask:
self._mask |= invalid
else:
self._mask = invalid
np.copyto(self._data, self.fill_value, where=invalid)
new_mask = mask_or(other_mask, invalid)
self._mask = mask_or(self._mask, new_mask)
return self
def to_mask(self, x_size, y_size):
"""
This function ...
:param x_size:
:param y_size:
:return:
"""
base = self.base.to_mask(x_size, y_size)
exclude = self.exclude.to_mask(x_size, y_size)
# Return the mask
return base * np.logical_not(exclude)
# -----------------------------------------------------------------
def masked_outside(region, header, x_size, y_size, expand_factor=1.0):
"""
This function ...
:param region:
:param header:
:param x_size:
:param y_size:
:param expand_factor:
:return:
"""
# Create a new region ...
region = regions.expand(region, factor=expand_factor)
# Create a mask from the region
mask = np.logical_not(regions.create_mask(region, header, x_size, y_size))
# Return the mask
return mask
# -----------------------------------------------------------------
def to_mask(self, x_size, y_size):
"""
This function ...
:param x_size:
:param y_size:
:return:
"""
base = self.base.to_mask(x_size, y_size)
exclude = self.exclude.to_mask(x_size, y_size)
# Return the mask
return base * np.logical_not(exclude)
# -----------------------------------------------------------------
def masked_outside(region, header, x_size, y_size, expand_factor=1.0):
"""
This function ...
:param region:
:param header:
:param x_size:
:param y_size:
:param expand_factor:
:return:
"""
# Create a new region ...
region = regions.expand(region, factor=expand_factor)
# Create a mask from the region
mask = np.logical_not(regions.create_mask(region, header, x_size, y_size))
# Return the mask
return mask
# -----------------------------------------------------------------
def predictiveQQ(simulations, targets, bands):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
bands = toCustomLogSpace(np.array(bands)[::-1])
pValues = np.empty_like(targets)
for i0 in range(pValues.shape[0]):
sims, idxs = np.unique(simulations[i0,:],return_index=True)
try:
pValues[i0] = interp1d(sims, bands[idxs], kind='linear', assume_sorted=True)(targets[i0])
except np.linalg.linalg.LinAlgError as ex:
pValues[i0] = np.nan
except ValueError as ex:
# TODO: handle better extrapolations
if targets[i0]<sims[0]:
pValues[i0] = bands[0]+(bands[0]-bands[1])/(sims[0]-sims[1])*(targets[i0]-sims[0])
else:
pValues[i0] = bands[-1]+(bands[-1]-bands[-2])/(sims[-1]-sims[-2])*(targets[i0]-sims[-1])
pValues = fromCustomLogSpace(pValues)
pValues[pValues<0] = 0
pValues[pValues>1] = 1
pValues = np.sort(1-pValues[np.logical_not(np.isnan(pValues))])
return (np.linspace(0,1, pValues.shape[0]), pValues)
def old_viz():
objects = np.load(sys.argv[1])
if len(objects.shape)==3:
objects = [objects]
for voxels in objects:
print voxels.shape
if connect > 0:
voxels_keep = (voxels >= threshold)
voxels_keep = max_connected(voxels_keep, connect)
voxels[np.logical_not(voxels_keep)] = 0
if downsample_factor > 1:
print "==> Performing downsample: factor: "+str(downsample_factor)+" method: "+downsample_method,
voxels = downsample(voxels, downsample_factor, method=downsample_method)
print "Done"
visualization(voxels, threshold, title=str(ind+1), uniform_size=uniform_size, use_colormap=use_colormap)
def unstick_contour(edgepoints, unstick_coeff):
"""
Removes edgepoints near previously discarded points.
@type edgepoints: list[bool]
@param edgepoints: current edgepoint list
@type unstick_coeff: float
@param unstick_coeff
@return: filtered edgepoints
"""
(n, init, end) = loop_connected_components(np.logical_not(edgepoints))
filtered = np.copy(edgepoints)
n_edgepoint = len(edgepoints)
for size, s, e in zip(n, init, end):
for j in range(1, int(size * unstick_coeff + 0.5) + 1):
filtered[(e + j) % n_edgepoint] = 0
filtered[(s - j) % n_edgepoint] = 0
return filtered
def _get_limits(nifti_file, only_plot_noise=False):
from builtins import bytes, str # pylint: disable=W0622
if isinstance(nifti_file, (str, bytes)):
nii = nb.as_closest_canonical(nb.load(nifti_file))
data = nii.get_data()
else:
data = nifti_file
data_mask = np.logical_not(np.isnan(data))
if only_plot_noise:
data_mask = np.logical_and(data_mask, data != 0)
vmin = np.percentile(data[data_mask], 0)
vmax = np.percentile(data[data_mask], 61)
else:
vmin = np.percentile(data[data_mask], 0.5)
vmax = np.percentile(data[data_mask], 99.5)
return vmin, vmax
def setUp(self, m, m_lsp, m_lspet):
# prepare mock.
joints = np.array([[[50, 80, 0], [50, 80, 1], [150, 260, 1], [150, 260, 0]],
[[100, 200, 1], [100, 200, 0], [120, 280, 0], [120, 280, 1]],
[[40, 10, 0], [40, 10, 1], [120, 290, 1], [120, 290, 0]]])
m_lsp_instance = m_lsp.return_value
m_lsp_instance.name = 'lsp_dataset'
m_lsp_instance.__len__.return_value = 2
lsp_joints = joints.copy()
lsp_joints[:, :, 2] = np.logical_not(joints[:, :, 2]).astype(int)
m_lsp_instance.get_data = lambda i: ('train', lsp_joints[i], 'im{0:04d}.jpg'.format(i + 1), np.zeros((300, 200, 3)))
m_lspet_instance = m_lspet.return_value
m_lspet_instance.name = 'lspet_dataset'
m_lspet_instance.__len__.return_value = 2
lspet_joints = joints.copy()
m_lspet_instance.get_data = lambda i: ('train', lspet_joints[i], 'im{0:05d}.jpg'.format(i + 1), np.zeros((300, 200, 3)))
# initialize.
self.path = 'test_orig_data'
self.output = 'test_data'
self.generator = DatasetGenerator(path=self.path, output=self.output)
def _features_in_class(self, X, y_one_hot):
'''
Compute complement features counts
Parameters
----------
X: numpy array (n_samples, n_features)
Matrix of input samples
y_one_hot: numpy array (n_samples, n_classes)
Binary matrix encoding input
'''
if not self.is_fitted:
self.complement_features_ = X.T.dot(np.logical_not(y_one_hot))
self.features_ = X.T.dot(y_one_hot)
else:
self.complement_features_ += X.T.dot(np.logical_not(y_one_hot))
self.features_ += X.T.dot(y_one_hot)
def _features_in_class(self, X, y_one_hot):
'''
Compute complement features counts
Parameters
----------
X: numpy array (n_samples, n_features)
Matrix of input samples
y_one_hot: numpy array (n_samples, n_classes)
Binary matrix encoding input
'''
if not self.is_fitted:
self.complement_features = X.T.dot(np.logical_not(y_one_hot))
else:
self.complement_features += X.T.dot(np.logical_not(y_one_hot))