def test_rescaleData():
dtypes = map(np.dtype, ('ubyte', 'uint16', 'byte', 'int16', 'int', 'float'))
for dtype1 in dtypes:
for dtype2 in dtypes:
data = (np.random.random(size=10) * 2**32 - 2**31).astype(dtype1)
for scale, offset in [(10, 0), (10., 0.), (1, -50), (0.2, 0.5), (0.001, 0)]:
if dtype2.kind in 'iu':
lim = np.iinfo(dtype2)
lim = lim.min, lim.max
else:
lim = (-np.inf, np.inf)
s1 = np.clip(float(scale) * (data-float(offset)), *lim).astype(dtype2)
s2 = pg.rescaleData(data, scale, offset, dtype2)
assert s1.dtype == s2.dtype
if dtype2.kind in 'iu':
assert np.all(s1 == s2)
else:
assert np.allclose(s1, s2)
python类all()的实例源码
def assert_arrays_almost_equal(a, b, threshold, dtype=False):
'''
Check if two arrays have the same shape and contents that differ
by abs(a - b) <= threshold for all elements.
If threshold is None, do an absolute comparison rather than a relative
comparison.
'''
if threshold is None:
return assert_arrays_equal(a, b, dtype=dtype)
assert isinstance(a, np.ndarray), "a is a %s" % type(a)
assert isinstance(b, np.ndarray), "b is a %s" % type(b)
assert a.shape == b.shape, "%s != %s" % (a, b)
#assert a.dtype == b.dtype, "%s and %b not same dtype %s %s" % (a, b,
# a.dtype,
# b.dtype)
if a.dtype.kind in ['f', 'c', 'i']:
assert (abs(a - b) < threshold).all(), \
"abs(%s - %s) max(|a - b|) = %s threshold:%s" % \
(a, b, (abs(a - b)).max(), threshold)
if dtype:
assert a.dtype == b.dtype, \
"%s and %s not same dtype %s and %s" % (a, b, a.dtype, b.dtype)
def getColors(self, mode=None):
"""Return list of all color stops converted to the specified mode.
If mode is None, then no conversion is done."""
if isinstance(mode, basestring):
mode = self.enumMap[mode.lower()]
color = self.color
if mode in [self.BYTE, self.QCOLOR] and color.dtype.kind == 'f':
color = (color * 255).astype(np.ubyte)
elif mode == self.FLOAT and color.dtype.kind != 'f':
color = color.astype(float) / 255.
if mode == self.QCOLOR:
color = [QtGui.QColor(*x) for x in color]
return color
def register(self, name):
"""
Add this ViewBox to the registered list of views.
This allows users to manually link the axes of any other ViewBox to
this one. The specified *name* will appear in the drop-down lists for
axis linking in the context menus of all other views.
The same can be accomplished by initializing the ViewBox with the *name* attribute.
"""
ViewBox.AllViews[self] = None
if self.name is not None:
del ViewBox.NamedViews[self.name]
self.name = name
if name is not None:
ViewBox.NamedViews[name] = self
ViewBox.updateAllViewLists()
sid = id(self)
self.destroyed.connect(lambda: ViewBox.forgetView(sid, name) if (ViewBox is not None and 'sid' in locals() and 'name' in locals()) else None)
#self.destroyed.connect(self.unregister)
def test_rescaleData():
dtypes = map(np.dtype, ('ubyte', 'uint16', 'byte', 'int16', 'int', 'float'))
for dtype1 in dtypes:
for dtype2 in dtypes:
data = (np.random.random(size=10) * 2**32 - 2**31).astype(dtype1)
for scale, offset in [(10, 0), (10., 0.), (1, -50), (0.2, 0.5), (0.001, 0)]:
if dtype2.kind in 'iu':
lim = np.iinfo(dtype2)
lim = lim.min, lim.max
else:
lim = (-np.inf, np.inf)
s1 = np.clip(float(scale) * (data-float(offset)), *lim).astype(dtype2)
s2 = pg.rescaleData(data, scale, offset, dtype2)
assert s1.dtype == s2.dtype
if dtype2.kind in 'iu':
assert np.all(s1 == s2)
else:
assert np.allclose(s1, s2)
def testMatrix():
"""
SRTTransform3D => Transform3D => SRTTransform3D
"""
tr = pg.SRTTransform3D()
tr.setRotate(45, (0, 0, 1))
tr.setScale(0.2, 0.4, 1)
tr.setTranslate(10, 20, 40)
assert tr.getRotation() == (45, QtGui.QVector3D(0, 0, 1))
assert tr.getScale() == QtGui.QVector3D(0.2, 0.4, 1)
assert tr.getTranslation() == QtGui.QVector3D(10, 20, 40)
tr2 = pg.Transform3D(tr)
assert np.all(tr.matrix() == tr2.matrix())
# This is the most important test:
# The transition from Transform3D to SRTTransform3D is a tricky one.
tr3 = pg.SRTTransform3D(tr2)
assert_array_almost_equal(tr.matrix(), tr3.matrix())
assert_almost_equal(tr3.getRotation()[0], tr.getRotation()[0])
assert_array_almost_equal(tr3.getRotation()[1], tr.getRotation()[1])
assert_array_almost_equal(tr3.getScale(), tr.getScale())
assert_array_almost_equal(tr3.getTranslation(), tr.getTranslation())
def assert_arrays_almost_equal(a, b, threshold, dtype=False):
'''
Check if two arrays have the same shape and contents that differ
by abs(a - b) <= threshold for all elements.
If threshold is None, do an absolute comparison rather than a relative
comparison.
'''
if threshold is None:
return assert_arrays_equal(a, b, dtype=dtype)
assert isinstance(a, np.ndarray), "a is a %s" % type(a)
assert isinstance(b, np.ndarray), "b is a %s" % type(b)
assert a.shape == b.shape, "%s != %s" % (a, b)
#assert a.dtype == b.dtype, "%s and %b not same dtype %s %s" % (a, b,
# a.dtype,
# b.dtype)
if a.dtype.kind in ['f', 'c', 'i']:
assert (abs(a - b) < threshold).all(), \
"abs(%s - %s) max(|a - b|) = %s threshold:%s" % \
(a, b, (abs(a - b)).max(), threshold)
if dtype:
assert a.dtype == b.dtype, \
"%s and %s not same dtype %s and %s" % (a, b, a.dtype, b.dtype)
def _test_array_argument(self, format, a, test_pass):
from numpy import array, all as all_
try:
snd = mixer.Sound(array=a)
except ValueError:
if not test_pass:
return
self.fail("Raised ValueError: Format %i, dtype %s" %
(format, a.dtype))
if not test_pass:
self.fail("Did not raise ValueError: Format %i, dtype %s" %
(format, a.dtype))
a2 = array(snd)
a3 = a.astype(a2.dtype)
lshift = abs(format) - 8 * a.itemsize
if lshift >= 0:
# This is asymmetric with respect to downcasting.
a3 <<= lshift
self.assert_(all_(a2 == a3),
"Format %i, dtype %s" % (format, a.dtype))
def test_image_data_mask(self):
mask_cache_key = str(id(self.model.input)) + '_' + str(id(None))
mask_tensor = self.model._output_mask_cache[mask_cache_key]
mask = mask_tensor.eval(
session=K.get_session(),
feed_dict={self.model.input: self.data}
)
self.assertTrue(
np.all(
mask[:, self.x_start:self.x_end]
)
)
self.assertFalse(
np.any(
mask[:, :self.x_start]
)
)
self.assertFalse(
np.any(
mask[:, self.x_end:]
)
)
def get_calibration_metrics(model, data):
scores = (data['X'] * data['Y']).dot(model)
#distinct scores
#compute calibration error at each score
full_metrics = {
'scores': float('nan'),
'count': float('nan'),
'predicted_risk': float('nan'),
'empirical_risk': float('nan')
}
cal_error = np.sqrt(np.sum(a*(a-b)^2)) ( - full_metrics['empirical_risk'])
summary_metrics = {
'mean_calibration_error': float('nan')
}
#counts
#metrics
#mean calibration error across all scores
pass
def _import_module(module_name, warn=True, prefix='_py_', ignore='_'):
"""Try import all public attributes from module into global namespace.
Existing attributes with name clashes are renamed with prefix.
Attributes starting with underscore are ignored by default.
Return True on successful import.
"""
try:
module = __import__(module_name)
except ImportError:
if warn:
warnings.warn("Failed to import module " + module_name)
else:
for attr in dir(module):
if ignore and attr.startswith(ignore):
continue
if prefix:
if attr in globals():
globals()[prefix + attr] = globals()[attr]
elif warn:
warnings.warn("No Python implementation of " + attr)
globals()[attr] = getattr(module, attr)
return True
def get(languages, feature_set_str, header=False, random=False, minimal=False):
lang_codes = languages.split()
feature_names, feature_values = get_concatenated_sets(lang_codes, feature_set_str)
feature_names = np.array([ f.replace(" ","_") for f in feature_names ])
feats = {}
if minimal:
mask = np.all(feature_values == 0.0, axis=0)
mask |= np.all(feature_values == 1.0, axis=0)
mask |= np.all(feature_values == -1.0, axis=0)
unmasked_indices = np.where(np.logical_not(mask))
else:
unmasked_indices = np.where(np.ones(feature_values.shape[1]))
if random:
feature_values = np.random.random(feature_values.shape) >= 0.5
if header:
print("\t".join(['CODE']+list(feature_names[unmasked_indices])))
feat_names = feature_names[unmasked_indices]
for i, lang_code in enumerate(lang_codes):
values = feature_values[i,unmasked_indices].ravel()
#values = [ '--' if f == -1 else ("%0.4f"%f).rstrip("0").rstrip(".") for f in values ]
feats[lang_code] = values
#print("\t".join([lang_code]+values))
return feats, feat_names
#if __name__ == '__main__':
# argparser = argparse.ArgumentParser()
# argparser.add_argument("languages", default='', help="The languages of interest, in ISO 639-3 codes, separated by spaces (e.g., \"deu eng fra swe\")")
# argparser.add_argument("feature_set", default='', help="The feature set or sets of interest (e.g., \"syntax_knn\" or \"fam\"), joined by concatenation (+) or element-wise union (|).")
# argparser.add_argument("-f", "--fields", default=False, action="store_true", help="Print feature names as the first row of data.")
# argparser.add_argument("-r", "--random", default=False, action="store_true", help="Randomize all feature values (e.g., to make a control group).")
# argparser.add_argument("-m", "--minimal", default=False, action="store_true", help="Suppress columns that are all 0, all 1, or all nulls.")
# args = argparser.parse_args()
# get(args.languages, args.feature_set, args.fields, args.random, args.minimal)
def get_integration_weights(order,nodes=None):
"""
Returns the integration weights for Gauss-Lobatto quadrature
as a function of the order of the polynomial we want to
represent.
See: https://en.wikipedia.org/wiki/Gaussian_quadrature
See: arXive:gr-qc/0609020v1
"""
if np.all(nodes == False):
nodes=get_quadrature_points(order)
if poly == polynomial.chebyshev.Chebyshev:
weights = np.empty((order+1))
weights[1:-1] = np.pi/order
weights[0] = np.pi/(2*order)
weights[-1] = weights[0]
return weights
elif poly == polynomial.legendre.Legendre:
interior_weights = 2/((order+1)*order*poly.basis(order)(nodes[1:-1])**2)
boundary_weights = np.array([1-0.5*np.sum(interior_weights)])
weights = np.concatenate((boundary_weights,
interior_weights,
boundary_weights))
return weights
else:
raise ValueError("Not a known polynomial type.")
return False
def define_attention_model(self):
'''
Take necessary parts out of the model to get OntoLSTM attention.
'''
if not self.model:
raise RuntimeError("Model not trained yet!")
input_shape = self.model.get_input_shape_at(0)
input_layer = Input(input_shape[1:], dtype='int32') # removing batch size
embedding_layer = None
encoder_layer = None
for layer in self.model.layers:
if layer.name == "embedding":
embedding_layer = layer
elif layer.name == "onto_lstm":
# We need to redefine the OntoLSTM layer with the learned weights and set return attention to True.
# Assuming we'll want attention values for all words (return_sequences = True)
if isinstance(layer, Bidirectional):
onto_lstm = OntoAttentionLSTM(input_dim=self.embed_dim, output_dim=self.embed_dim,
num_senses=self.num_senses, num_hyps=self.num_hyps,
use_attention=True, return_attention=True, return_sequences=True,
consume_less='gpu')
encoder_layer = Bidirectional(onto_lstm, weights=layer.get_weights())
else:
encoder_layer = OntoAttentionLSTM(input_dim=self.embed_dim,
output_dim=self.embed_dim, num_senses=self.num_senses,
num_hyps=self.num_hyps, use_attention=True,
return_attention=True, return_sequences=True,
consume_less='gpu', weights=layer.get_weights())
break
if not embedding_layer or not encoder_layer:
raise RuntimeError("Required layers not found!")
attention_output = encoder_layer(embedding_layer(input_layer))
self.attention_model = Model(inputs=input_layer, outputs=attention_output)
print >>sys.stderr, "Attention model summary:"
self.attention_model.summary()
self.attention_model.compile(loss="mse", optimizer="sgd") # Loss and optimizer do not matter!
def test_FFT(FFT):
N = FFT.N
if FFT.rank == 0:
A = random(N).astype(FFT.float)
if FFT.communication == 'AlltoallN':
C = empty(FFT.global_complex_shape(), dtype=FFT.complex)
C = rfftn(A, C, axes=(0,1,2))
C[:, :, -1] = 0 # Remove Nyquist frequency
A = irfftn(C, A, axes=(0,1,2))
B2 = zeros(FFT.global_complex_shape(), dtype=FFT.complex)
B2 = rfftn(A, B2, axes=(0,1,2))
else:
A = zeros(N, dtype=FFT.float)
B2 = zeros(FFT.global_complex_shape(), dtype=FFT.complex)
atol, rtol = (1e-10, 1e-8) if FFT.float is float64 else (5e-7, 1e-4)
FFT.comm.Bcast(A, root=0)
FFT.comm.Bcast(B2, root=0)
a = zeros(FFT.real_shape(), dtype=FFT.float)
c = zeros(FFT.complex_shape(), dtype=FFT.complex)
a[:] = A[FFT.real_local_slice()]
c = FFT.fftn(a, c)
#print abs((c - B2[FFT.complex_local_slice()])/c.max()).max()
assert all(abs((c - B2[FFT.complex_local_slice()])/c.max()) < rtol)
#assert allclose(c, B2[FFT.complex_local_slice()], rtol, atol)
a = FFT.ifftn(c, a)
#print abs((a - A[FFT.real_local_slice()])/a.max()).max()
assert all(abs((a - A[FFT.real_local_slice()])/a.max()) < rtol)
#assert allclose(a, A[FFT.real_local_slice()], rtol, atol)
def is_stable(self):
"""Checks if simulation satisfies stability conditions. Does not account for instability
due to high absorption or nonlinear effects. Includes a little headroom (1%).
Returns:
True if stable, False if not.
"""
return np.all(self.material_vector('sound_velocity') <
0.99 * self.x.increment / self.t.increment)
def is_stable(self):
"""Checks if simulation satisfies stability conditions. Does not account for instability
due to high absorption and includes a little headroom (1%).
Returns:
True if stable, False if not.
"""
return np.all(self.material_vector('sound_velocity') <
0.99 * self.x.increment / self.t.increment)
def is_stable(self):
"""Checks if simulation satisfies stability conditions. Does not account for instability
due to high absorption and includes a little headroom (1%).
Returns:
True if stable, False if not.
"""
return np.all(self.material_vector('sound_velocity') <
0.99 * min(self.x.increment, self.y.increment) / self.t.increment)
def is_stable(self):
"""Checks if simulation satisfies stability conditions. Does not account for instability
due to high absorption and includes a little headroom (1%).
Returns:
True if stable, False if not.
"""
return np.all(self.material_vector('sound_velocity') <
0.99 * min(self.x.increment, self.y.increment) / self.t.increment)
def absorption_coef(self):
"""Returns a helper variable (called mu in publications by L. Claes) that sums up all
losses into a single quantity."""
if not self._absorption_coef:
return (4/3 * self.shear_viscosity + self.bulk_viscosity + self.thermal_conductivity *
(self.isobaric_heat_cap - self.isochoric_heat_cap) /
(self.isobaric_heat_cap * self.isochoric_heat_cap))
else:
return self._absorption_coef