Python numpy 模块,alltrue() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.alltrue()。
def test_sgld_sparse(self):
tf.reset_default_graph()
z = tf.Variable(tf.zeros((5, 2)), dtype=tf.float32)
idx = tf.placeholder(tf.int32)
zi = tf.gather(z, idx)
zloss = tf.square(zi - [10.0, 5.0])
sgld = SGLD(learning_rate=0.4)
train_op_sgld = sgld.minimize(zloss)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
self.assertTrue(np.alltrue(sess.run(z) == 0.0))
sess.run(train_op_sgld, feed_dict={idx: 3})
zh = sess.run(z)
self.assertTrue(np.alltrue(zh[[0, 1, 2, 4], :] == 0.0))
self.assertTrue(zh[3, 0] > 0)
def test_psgld_sparse(self):
tf.reset_default_graph()
z = tf.Variable(tf.zeros((5, 2)), dtype=tf.float32)
idx = tf.placeholder(tf.int32)
zi = tf.gather(z, idx)
zloss = tf.square(zi - [10.0, 5.0])
psgld = pSGLD(learning_rate=0.4)
train_op_psgld = psgld.minimize(zloss)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
self.assertTrue(np.alltrue(sess.run(z) == 0.0))
sess.run(train_op_psgld, feed_dict={idx: 3})
zh = sess.run(z)
self.assertTrue(np.alltrue(zh[[0, 1, 2, 4], :] == 0.0))
self.assertTrue(zh[3, 0] > 0)
def _set_startprob(self, startprob):
if startprob is None:
startprob = np.ones(shape=(self.n_partial_states, self.n_hidden_states)) / self.n_hidden_states
else:
startprob = np.asarray(startprob, dtype=np.float)
# check if there exists a component whose value is exactly zero
# if so, add a small number and re-normalize
if not np.alltrue(startprob):
startprob = normalize(startprob, axis=1)
if len(startprob) != self.n_partial_states:
raise ValueError('startprob must have length n_partial_states')
if not np.allclose(np.sum(startprob, axis=1), 1.0):
raise ValueError('startprob must sum to 1.0')
self._log_startprob = np.log(np.asarray(startprob).copy())
def _set_steadyprob(self, steadyprob):
if steadyprob is None:
steadyprob = np.ones(shape=(self.n_partial_states, self.n_hidden_states)) / self.n_hidden_states
else:
steadyprob = np.asarray(steadyprob, dtype=np.float)
# check if there exists a component whose value is exactly zero
# if so, add a small number and re-normalize
if not np.alltrue(steadyprob):
steadyprob = normalize(steadyprob, axis=1)
if len(steadyprob) != self.n_partial_states:
raise ValueError('steadyprob must have length n_partial_states')
if not np.allclose(np.sum(steadyprob, axis=1), 1.0):
raise ValueError('steadyprob must sum to 1.0')
self._log_steadyprob = np.log(np.asarray(steadyprob).copy())
def _set_transmat(self, transmat):
if transmat is None:
transmat = np.ones(shape=(self.n_partial_states, self.n_partial_states, self.n_hidden_states,
self.n_hidden_states)) / self.n_hidden_states
# check if there exists a component whose value is exactly zero
# if so, add a small number and re-normalize
if not np.alltrue(transmat):
transmat = normalize(transmat, axis=3)
if (np.asarray(transmat).shape
!= (self.n_partial_states, self.n_partial_states, self.n_hidden_states, self.n_hidden_states)):
raise ValueError('transmat must have shape '
'(n_partial_states,n_partial_states,n_hidden_states,n_hidden_states)')
if not np.all(np.allclose(np.sum(transmat, axis=3), 1.0)):
raise ValueError('Rows of transmat must sum to 1.0')
self._log_transmat = np.log(np.asarray(transmat).copy())
underflow_idx = np.isnan(self._log_transmat)
self._log_transmat[underflow_idx] = NEGINF
def test_backbone_dihedrals(self):
self.feat = MDFeaturizer(topfile=self.asn_leu_pdbfile)
self.feat.add_backbone_torsions()
traj = mdtraj.load(self.asn_leu_pdbfile)
Y = self.feat.transform(traj)
assert(np.alltrue(Y >= -np.pi))
assert(np.alltrue(Y <= np.pi))
desc = self.feat.describe()
self.assertEqual(len(desc), self.feat.dimension())
# test ordering of indices
backbone_feature = self.feat.active_features[0]
angle_indices = backbone_feature.angle_indexes
np.testing.assert_equal(angle_indices[0], backbone_feature._phi_inds[0])
np.testing.assert_equal(angle_indices[1], backbone_feature._psi_inds[0])
np.testing.assert_equal(angle_indices[2], backbone_feature._phi_inds[1])
np.testing.assert_equal(angle_indices[3], backbone_feature._psi_inds[1])
def test_single_spline_penalty():
"""
check that feature functions with only 1 basis are penalized correctly
derivative penalty should be 0.
l2 should penalty be 1.
monotonic_ and convexity_ should be 0.
"""
coef = np.array(1.)
assert(np.alltrue(derivative(1, coef).A == 0.))
assert(np.alltrue(l2(1, coef).A == 1.))
assert(np.alltrue(monotonic_inc(1, coef).A == 0.))
assert(np.alltrue(monotonic_dec(1, coef).A == 0.))
assert(np.alltrue(convex(1, coef).A == 0.))
assert(np.alltrue(concave(1, coef).A == 0.))
assert(np.alltrue(circular(1, coef).A == 0.))
assert(np.alltrue(none(1, coef).A == 0.))
def arraysigtest(self,test,ref):
#Raise an error if the arrays are not the same size
if test.shape != ref.shape:
raise ValueError("Array size mismatch")
tt=test[2:-2]
rr=ref[2:-2]
#Identify the significant elements
tidx=N.where(tt>(self.sigthresh*tt.max()))[0]
ridx=N.where(rr>(self.sigthresh*rr.max()))[0]
#Set a flag if they're not the same set
if not (N.alltrue(tidx == ridx)):
self.tra['SigElemDiscrep']=True
tidx=ridx
#Now compare only the significant elements.
#We no longer need to exclude points with zero value, because
#those points were already excluded as insignificant.
self.arraytest(tt[ridx],rr[ridx])
def arraytest(self,test,ref):
#Exclude the endpoints where the gradient is very steep
self.adiscrep=self.arraydiff(test,ref)#[2:-2]
count=N.where(abs(self.adiscrep)>self.thresh)[0].size
try:
self.tra['Discrepfrac']=float(count)/self.adiscrep.size
self.tra['Discrepmin']=self.adiscrep.min()
self.tra['Discrepmax']=self.adiscrep.max()
self.tra['Discrepmean']=self.adiscrep.mean()
self.tra['Discrepstd']=self.adiscrep.std()
self.tra['Outliers']=self.count_outliers(5)
if (self.tra['Discrepfrac'] > self.superthresh):
self.tra['Extreme']=True
self.failUnless(N.alltrue(abs(self.adiscrep)<self.thresh),
msg="Worst case %f"%abs(self.adiscrep).max())
except ZeroDivisionError:
self.tra['Discrepfrac']=0.0
self.tra['Discrepmin']=0.0
self.tra['Discrepmax']=0.0
def arraysigtest(self,ref,test):
#Raise an error if the arrays are not the same size
if test.shape != ref.shape:
raise ValueError("Array size mismatch")
tt=test[2:-2]
rr=ref[2:-2]
#Identify the significant elements
tidx=N.where(tt>(self.sigthresh*tt.max()))[0]
ridx=N.where(rr>(self.sigthresh*rr.max()))[0]
#Set a flag if they're not the same set
if not (N.alltrue(tidx == ridx)):
self.tra['SigElemDiscrep']=True
tidx=ridx
#Now compare only the significant elements.
#We no longer need to exclude points with zero value, because
#those points were already excluded as insignificant.
self.arraytest(tt[ridx],rr[ridx])
def arraytest(self,ref,test):
self.adiscrep=self.arraydiff(test,ref)
count=N.where(abs(self.adiscrep)>self.thresh)[0].size
try:
self.tra['Discrepfrac']=float(count)/self.adiscrep.size
self.tra['Discrepmin']=self.adiscrep.min()
self.tra['Discrepmax']=self.adiscrep.max()
self.tra['Discrepmean']=self.adiscrep.mean()
self.tra['Discrepstd']=self.adiscrep.std()
self.tra['Outliers']=self.count_outliers(5)
self.failUnless(N.alltrue(abs(self.adiscrep)<self.thresh),
msg="Worst case %f"%abs(self.adiscrep).max())
except ZeroDivisionError:
self.tra['Discrepfrac']=0.0
self.tra['Discrepmin']=0.0
self.tra['Discrepmax']=0.0
#Helper method for scalar comparison
def _arraylike_copy(arr):
"""Duplicate content of arr into a numpy array.
Examples
--------
>>> X_npy = numpy.array([1, 2, 3])
>>> numpy.alltrue(_arraylike_copy(X_npy) == X_npy)
True
>>> _arraylike_copy(X_npy) is X_npy
False
>>> numpy.alltrue(_arraylike_copy([1, 2, 3]) == X_npy)
True
"""
if type(arr) != numpy.ndarray:
return numpy.array(arr)
else:
return arr.copy()
def test_point_correctness():
import itertools
stencil = [-1, 0, 1]
ndim = 3
n = 2000
stencil = itertools.product(*[stencil]*ndim)
stencil = np.array(list(stencil)).astype(np.int32)
points = (np.random.rand(n, ndim) * [1, 2, 3]).astype(np.float32)
scale = 0.1
spec = GridSpec(points, float(scale))
offsets = spec.stencil(stencil).astype(np.int32)
grid = PointGrid(spec, points, offsets)
pairs = grid.pairs()
from scipy.spatial import cKDTree
tree = cKDTree(points)
tree_pairs = tree.query_pairs(scale, output_type='ndarray')
print(tree_pairs)
print(pairs)
assert np.alltrue(npi.unique(tree_pairs) == npi.unique(np.sort(pairs, axis=1)))
def test_point_correctness():
import itertools
stencil = [-1, 0, 1]
ndim = 3
n = 2000
stencil = itertools.product(*[stencil]*ndim)
stencil = np.array(list(stencil)).astype(np.int32)
points = (np.random.rand(n, ndim) * [1, 2, 3]).astype(np.float32)
scale = 0.1
spec = GridSpec(points, float(scale))
offsets = spec.stencil(stencil).astype(np.int32)
grid = PointGrid(spec, points, offsets)
pairs = grid.pairs()
from scipy.spatial import cKDTree
tree = cKDTree(points)
tree_pairs = tree.query_pairs(scale, output_type='ndarray')
print(tree_pairs)
print(pairs)
assert np.alltrue(npi.unique(tree_pairs) == npi.unique(np.sort(pairs, axis=1)))
def test_grad_zmat():
path = os.path.join(STRUCTURE_PATH, 'MIL53_beta.xyz')
molecule = cc.Cartesian.read_xyz(path, start_index=1)
fragment = molecule.get_fragment([(12, 17), (55, 60)])
connection = np.array([[3, 99, 1, 12], [17, 3, 99, 12], [60, 3, 17, 12]])
connection = pd.DataFrame(connection[:, 1:], index=connection[:, 0],
columns=['b', 'a', 'd'])
c_table = molecule.get_construction_table([(fragment, connection)])
molecule = molecule.loc[c_table.index]
x = sympy.symbols('x', real=True)
dist_mol = molecule.copy()
dist_mol.loc[:, ['x', 'y', 'z']] = 0.
dist_mol.loc[13, 'x'] = x
zmat_dist = molecule.get_grad_zmat(c_table)(dist_mol)
moved_atoms = zmat_dist.index[
(zmat_dist.loc[:, ['bond', 'angle', 'dihedral']] != 0.).any(axis=1)]
assert moved_atoms[0] == 13
assert np.alltrue(
moved_atoms[1:] == c_table.index[(c_table == 13).any(axis=1)])
def allclose(a, b, align=False, rtol=1.e-5, atol=1.e-8):
"""Compare two molecules for numerical equality.
Args:
a (Cartesian):
b (Cartesian):
align (bool): a and b are
prealigned along their principal axes of inertia and moved to their
barycenters before comparing.
rtol (float): Relative tolerance for the numerical equality comparison
look into :func:`numpy.allclose` for further explanation.
atol (float): Relative tolerance for the numerical equality comparison
look into :func:`numpy.allclose` for further explanation.
Returns:
bool:
"""
return np.alltrue(isclose(a, b, align=align, rtol=rtol, atol=atol))
def testVoltageLimitsAreSetAtStartUp(self):
wanted= self._cfg.lowerVoltageLimit
actual= self._ctrl.getLowerVoltageLimit(self._tt.ALL_CHANNELS)
self.assertTrue(
np.alltrue(wanted == actual),
"%s %s" % (wanted, actual))
wanted= self._cfg.upperVoltageLimit
actual= self._ctrl.getUpperVoltageLimit(self._tt.ALL_CHANNELS)
self.assertTrue(
np.alltrue(wanted == actual),
"%s %s" % (wanted, actual))
def test3rdAxisIsSetAsPivotAtStartUp(self):
pivot= self._ctrl.getAxesIdentifiers()[2]
wanted= self._cfg.pivotValue
actual= self._ctrl.getOpenLoopAxisValue(pivot)
self.assertTrue(
np.alltrue(wanted == actual),
"%s %s" % (wanted, actual))
def _prepare_injection_directions(self):
"""provide genotypic directions for TPA and selective mirroring,
with no specific length normalization, to be used in the
coming iteration.
Details:
This method is called in the end of `tell`. The result is
assigned to ``self.pop_injection_directions`` and used in
`ask_geno`.
"""
# self.pop_injection_directions is supposed to be empty here
if self.pop_injection_directions or self.pop_injection_solutions:
raise ValueError("""Found unused injected direction/solutions.
This could be a bug in the calling order/logics or due to
a too small popsize used in `ask()` or when only using
`ask(1)` repeatedly. """)
ary = []
if self.mean_shift_samples:
ary = [self.mean - self.mean_old]
ary.append(self.mean_old - self.mean) # another copy!
if np.alltrue(ary[-1] == 0.0):
utils.print_warning('zero mean shift encountered',
'_prepare_injection_directions',
'CMAEvolutionStrategy', self.countiter)
if self.opts['pc_line_samples']: # caveat: before, two samples were used
ary.append(self.pc.copy())
if self.sp.lam_mirr and (
self.opts['CMA_mirrormethod'] == 2 or (
self.opts['CMA_mirrormethod'] == 1 and ( # replacement for direct selective mirrors
not hasattr(self, '_mirrormethod1_done') or
self._mirrormethod1_done < self.countiter - 1))):
i0 = len(ary)
ary += self.get_selective_mirrors()
self._indices_of_selective_mirrors = range(i0, len(ary))
self.pop_injection_directions = ary
return ary
def test_method_args(self, level=rlevel):
# Make sure methods and functions have same default axis
# keyword and arguments
funcs1 = ['argmax', 'argmin', 'sum', ('product', 'prod'),
('sometrue', 'any'),
('alltrue', 'all'), 'cumsum', ('cumproduct', 'cumprod'),
'ptp', 'cumprod', 'prod', 'std', 'var', 'mean',
'round', 'min', 'max', 'argsort', 'sort']
funcs2 = ['compress', 'take', 'repeat']
for func in funcs1:
arr = np.random.rand(8, 7)
arr2 = arr.copy()
if isinstance(func, tuple):
func_meth = func[1]
func = func[0]
else:
func_meth = func
res1 = getattr(arr, func_meth)()
res2 = getattr(np, func)(arr2)
if res1 is None:
res1 = arr
if res1.dtype.kind in 'uib':
assert_((res1 == res2).all(), func)
else:
assert_(abs(res1-res2).max() < 1e-8, func)
for func in funcs2:
arr1 = np.random.rand(8, 7)
arr2 = np.random.rand(8, 7)
res1 = None
if func == 'compress':
arr1 = arr1.ravel()
res1 = getattr(arr2, func)(arr1)
else:
arr2 = (15*arr2).astype(int).ravel()
if res1 is None:
res1 = getattr(arr1, func)(arr2)
res2 = getattr(np, func)(arr1, arr2)
assert_(abs(res1-res2).max() < 1e-8, func)
def test_fromiter_bytes(self):
# Ticket #1058
a = np.fromiter(list(range(10)), dtype='b')
b = np.fromiter(list(range(10)), dtype='B')
assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
def test_fromiter_comparison(self, level=rlevel):
a = np.fromiter(list(range(10)), dtype='b')
b = np.fromiter(list(range(10)), dtype='B')
assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
def test_values(self):
expected = np.array(list(self.makegen()))
a = np.fromiter(self.makegen(), int)
a20 = np.fromiter(self.makegen(), int, 20)
self.assertTrue(np.alltrue(a == expected, axis=0))
self.assertTrue(np.alltrue(a20 == expected[:20], axis=0))
def test_nd(self):
y1 = [[0, 0, 1], [0, 1, 1], [1, 1, 1]]
assert_(not np.all(y1))
assert_array_equal(np.alltrue(y1, axis=0), [0, 0, 1])
assert_array_equal(np.alltrue(y1, axis=1), [0, 0, 1])
def fill_diagonal(a, val, wrap=False):
"""Fills the main diagonal of the given array of any dimensionality.
For an array `a` with ``a.ndim > 2``, the diagonal is the list of
locations with indices ``a[i, i, ..., i]`` all identical. This function
modifies the input array in-place, it does not return a value.
Args:
a (cupy.ndarray): The array, at least 2-D.
val (scalar): The value to be written on the diagonal.
Its type must be compatible with that of the array a.
wrap (bool): If specified, the diagonal is "wrapped" after N columns.
This affects only tall matrices.
Examples
--------
>>> a = cupy.zeros((3, 3), int)
>>> cupy.fill_diagonal(a, 5)
>>> a
array([[5, 0, 0],
[0, 5, 0],
[0, 0, 5]])
.. seealso:: :func:`numpy.fill_diagonal`
"""
# The followings are imported from the original numpy
if a.ndim < 2:
raise ValueError('array must be at least 2-d')
end = None
if a.ndim == 2:
step = a.shape[1] + 1
if not wrap:
end = a.shape[1] * a.shape[1]
else:
if not numpy.alltrue(numpy.diff(a.shape) == 0):
raise ValueError('All dimensions of input must be of equal length')
step = 1 + numpy.cumprod(a.shape[:-1]).sum()
# Since the current cupy does not support a.flat,
# we use a.ravel() instead of a.flat
a.ravel()[:end:step] = val
def fluxpoints(self, field, n, uniform=False):
"""Returns points where field lines should enter/exit the surface.
The flux points are usually chosen so that they are equally separated
in electric field flux. However, if 'uniform' is True then the points
are equispaced.
This method requires that the flux be in xor out everywhere on the
circle (unless 'uniform' is True)."""
# Create a dense array of points around the circle
a = radians(linspace(0, 360, 1001)) + self.a0
assert len(a)%4 == 1
x = self.r*array([cos(a), sin(a)]).T + self.x
if uniform:
flux = ones_like(a)
else:
# Get the flux through each point. Ensure the fluxes are either
# all in or all out.
flux = field.projection(x, a)
if numpy.sum(flux) < 0:
flux *= -1
assert alltrue(flux > 0)
# Create an integrated flux curve
intflux = insert(cumsum((flux[:-1]+flux[1:])/2), 0, 0)
assert isclose(intflux[-1], numpy.sum(flux[:-1]))
# Divide the integrated flux curve into n+1 portions, and calculate
# the corresponding angles.
v = linspace(0, intflux[-1], n+1)
a = lininterp2(intflux, a, v)[:-1]
return self.r*array([cos(a), sin(a)]).T + self.x
def apply(self, data):
# so that correlation matrix calculation doesn't crash
for ch in data:
if np.alltrue(ch == 0.0):
ch[-1] += 0.00001
data1 = data
# if data1.shape[1] > self.max_hz:
# data1 = Resample(self.max_hz).apply(data1)
if self.scale_option == 'usf':
data1 = UnitScaleFeat().apply(data1)
elif self.scale_option == 'us':
data1 = UnitScale().apply(data1)
data1 = CorrelationMatrix().apply(data1)
if self.with_eigen:
w = Eigenvalues().apply(data1)
out = []
if self.with_corr:
data1 = upper_right_triangle(data1)
out.append(data1)
if self.with_eigen:
out.append(w)
for d in out:
assert d.ndim == 1
res = np.concatenate(out, axis=0)
return res
def test_method_args(self, level=rlevel):
# Make sure methods and functions have same default axis
# keyword and arguments
funcs1 = ['argmax', 'argmin', 'sum', ('product', 'prod'),
('sometrue', 'any'),
('alltrue', 'all'), 'cumsum', ('cumproduct', 'cumprod'),
'ptp', 'cumprod', 'prod', 'std', 'var', 'mean',
'round', 'min', 'max', 'argsort', 'sort']
funcs2 = ['compress', 'take', 'repeat']
for func in funcs1:
arr = np.random.rand(8, 7)
arr2 = arr.copy()
if isinstance(func, tuple):
func_meth = func[1]
func = func[0]
else:
func_meth = func
res1 = getattr(arr, func_meth)()
res2 = getattr(np, func)(arr2)
if res1 is None:
res1 = arr
if res1.dtype.kind in 'uib':
assert_((res1 == res2).all(), func)
else:
assert_(abs(res1-res2).max() < 1e-8, func)
for func in funcs2:
arr1 = np.random.rand(8, 7)
arr2 = np.random.rand(8, 7)
res1 = None
if func == 'compress':
arr1 = arr1.ravel()
res1 = getattr(arr2, func)(arr1)
else:
arr2 = (15*arr2).astype(int).ravel()
if res1 is None:
res1 = getattr(arr1, func)(arr2)
res2 = getattr(np, func)(arr1, arr2)
assert_(abs(res1-res2).max() < 1e-8, func)
def test_fromiter_bytes(self):
# Ticket #1058
a = np.fromiter(list(range(10)), dtype='b')
b = np.fromiter(list(range(10)), dtype='B')
assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
def test_fromiter_comparison(self, level=rlevel):
a = np.fromiter(list(range(10)), dtype='b')
b = np.fromiter(list(range(10)), dtype='B')
assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
def test_values(self):
expected = np.array(list(self.makegen()))
a = np.fromiter(self.makegen(), int)
a20 = np.fromiter(self.makegen(), int, 20)
self.assertTrue(np.alltrue(a == expected, axis=0))
self.assertTrue(np.alltrue(a20 == expected[:20], axis=0))
def test_nd(self):
y1 = [[0, 0, 1], [0, 1, 1], [1, 1, 1]]
assert_(not np.all(y1))
assert_array_equal(np.alltrue(y1, axis=0), [0, 0, 1])
assert_array_equal(np.alltrue(y1, axis=1), [0, 0, 1])
def trim(image, shape):
"""
Trim image to a given shape
Parameters
----------
image: 2D `numpy.ndarray`
Input image
shape: tuple of int
Desired output shape of the image
Returns
-------
new_image: 2D `numpy.ndarray`
Input image trimmed
"""
shape = np.asarray(shape, dtype=int)
imshape = np.asarray(image.shape, dtype=int)
if np.alltrue(imshape == shape):
return image
if np.any(shape <= 0):
raise ValueError("TRIM: null or negative shape given")
dshape = imshape - shape
if np.any(dshape < 0):
raise ValueError("TRIM: target size bigger than source one")
if np.any(dshape % 2 != 0):
raise ValueError("TRIM: source and target shapes "
"have different parity")
idx, idy = np.indices(shape)
offx, offy = dshape // 2
return image[idx + offx, idy + offy]
def test_viterbi_decoder():
coded_bits, un_coded_bits = _generate_data()
vit_decoded = components.viterbi_decoder(coded_bits, rate=(2,3))
assert np.alltrue(vit_decoded == un_coded_bits)
def test_outer_deinterleaver():
interleaved_bits = test_data['op_interleaved_bits']
super_frame_start = test_data['op_super_frame_start']
rate = test_data['op_rate']
exp_coded_bits = test_data['op_coded_bits']
exp_first_sync_byte_seq_num = test_data['op_first_sync_byte_seq_num']
coded_bits,first_sync_byte_seq_num = components.outer_deinterleaver(
interleaved_bits,
super_frame_start,
rate)
assert np.alltrue(coded_bits == exp_coded_bits)
assert first_sync_byte_seq_num == exp_first_sync_byte_seq_num
def test_outer_decoder():
coded_bits = test_data['op_coded_bits']
first_sync_byte_seq_num = test_data['op_first_sync_byte_seq_num']
exp_derandomized_bit_array = test_data['op_derandomized_bit_array']
derandomized_bit_array = components.outer_decoder(coded_bits,
first_sync_byte_seq_num)
assert np.alltrue(derandomized_bit_array == exp_derandomized_bit_array)
def test_inner_processing():
data_carriers = test_data['ip_data_carriers']
super_frame_start = test_data['ip_super_frame_start']
exp_demultiplex_bits = test_data['ip_demultiplex_bits']
demultiplex_bits = components.inner_processing(data_carriers,
super_frame_start)
assert np.alltrue(demultiplex_bits == exp_demultiplex_bits)
def test_demodulate_edge_cases():
data_carriers = np.array([1+2j,
9+0j,
])
exp_demodulated = np.array([34,
59,
])
demodulated = components.demodulate(data_carriers)
assert np.alltrue(demodulated == exp_demodulated)
def test_symbol_deinterleaver():
demodulated = test_data['ip_demodulated']
super_frame_start = test_data['ip_super_frame_start']
exp_symbol_deint = test_data['ip_symbol_deint']
symbol_deint = components.symbol_deinterleaver(demodulated,
super_frame_start)
assert np.alltrue(symbol_deint==exp_symbol_deint)
def test_bit_deinterleaver():
symbol_deint = test_data['ip_symbol_deint']
exp_demultiplex_bits = test_data['ip_demultiplex_bits']
demultiplex_bits = components.bit_deinterleaver(symbol_deint)
assert np.alltrue(demultiplex_bits==exp_demultiplex_bits)
def rank(X, cond=1.0e-12):
"""
Return the rank of a matrix X based on its generalized inverse,
not the SVD.
"""
X = np.asarray(X)
if len(X.shape) == 2:
import scipy.linalg as SL
D = SL.svdvals(X)
result = np.add.reduce(np.greater(D / D.max(), cond))
return int(result.astype(np.int32))
else:
return int(not np.alltrue(np.equal(X, 0.)))
def test_method_args(self, level=rlevel):
# Make sure methods and functions have same default axis
# keyword and arguments
funcs1 = ['argmax', 'argmin', 'sum', ('product', 'prod'),
('sometrue', 'any'),
('alltrue', 'all'), 'cumsum', ('cumproduct', 'cumprod'),
'ptp', 'cumprod', 'prod', 'std', 'var', 'mean',
'round', 'min', 'max', 'argsort', 'sort']
funcs2 = ['compress', 'take', 'repeat']
for func in funcs1:
arr = np.random.rand(8, 7)
arr2 = arr.copy()
if isinstance(func, tuple):
func_meth = func[1]
func = func[0]
else:
func_meth = func
res1 = getattr(arr, func_meth)()
res2 = getattr(np, func)(arr2)
if res1 is None:
res1 = arr
if res1.dtype.kind in 'uib':
assert_((res1 == res2).all(), func)
else:
assert_(abs(res1-res2).max() < 1e-8, func)
for func in funcs2:
arr1 = np.random.rand(8, 7)
arr2 = np.random.rand(8, 7)
res1 = None
if func == 'compress':
arr1 = arr1.ravel()
res1 = getattr(arr2, func)(arr1)
else:
arr2 = (15*arr2).astype(int).ravel()
if res1 is None:
res1 = getattr(arr1, func)(arr2)
res2 = getattr(np, func)(arr1, arr2)
assert_(abs(res1-res2).max() < 1e-8, func)
def test_fromiter_bytes(self):
# Ticket #1058
a = np.fromiter(list(range(10)), dtype='b')
b = np.fromiter(list(range(10)), dtype='B')
assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
def test_fromiter_comparison(self, level=rlevel):
a = np.fromiter(list(range(10)), dtype='b')
b = np.fromiter(list(range(10)), dtype='B')
assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
def test_values(self):
expected = np.array(list(self.makegen()))
a = np.fromiter(self.makegen(), int)
a20 = np.fromiter(self.makegen(), int, 20)
self.assertTrue(np.alltrue(a == expected, axis=0))
self.assertTrue(np.alltrue(a20 == expected[:20], axis=0))
def test_angles(self):
sel = np.array([[1, 2, 5],
[1, 3, 8],
[2, 9, 10]], dtype=int)
self.feat.add_angles(sel)
assert(self.feat.dimension() == sel.shape[0])
Y = self.feat.transform(self.traj)
assert(np.alltrue(Y >= -np.pi))
assert(np.alltrue(Y <= np.pi))
self.assertEqual(len(self.feat.describe()), self.feat.dimension())
def test_angles_deg(self):
sel = np.array([[1, 2, 5],
[1, 3, 8],
[2, 9, 10]], dtype=int)
self.feat.add_angles(sel, deg=True)
assert(self.feat.dimension() == sel.shape[0])
Y = self.feat.transform(self.traj)
assert(np.alltrue(Y >= -180.0))
assert(np.alltrue(Y <= 180.0))
def test_angles_cossin(self):
sel = np.array([[1, 2, 5],
[1, 3, 8],
[2, 9, 10]], dtype=int)
self.feat.add_angles(sel, cossin=True)
assert(self.feat.dimension() == 2 * sel.shape[0])
Y = self.feat.transform(self.traj)
assert(np.alltrue(Y >= -np.pi))
assert(np.alltrue(Y <= np.pi))
desc = self.feat.describe()
self.assertEqual(len(desc), self.feat.dimension())
def test_dihedrals(self):
sel = np.array([[1, 2, 5, 6],
[1, 3, 8, 9],
[2, 9, 10, 12]], dtype=int)
self.feat.add_dihedrals(sel)
assert(self.feat.dimension() == sel.shape[0])
Y = self.feat.transform(self.traj)
assert(np.alltrue(Y >= -np.pi))
assert(np.alltrue(Y <= np.pi))
self.assertEqual(len(self.feat.describe()), self.feat.dimension())
def test_dihedrals_deg(self):
sel = np.array([[1, 2, 5, 6],
[1, 3, 8, 9],
[2, 9, 10, 12]], dtype=int)
self.feat.add_dihedrals(sel, deg=True)
assert(self.feat.dimension() == sel.shape[0])
Y = self.feat.transform(self.traj)
assert(np.alltrue(Y >= -180.0))
assert(np.alltrue(Y <= 180.0))
self.assertEqual(len(self.feat.describe()), self.feat.dimension())