Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_copy():
"Standard copy module integration"
import gc
x = ufloat((3, 0.1))
assert x == x
y = copy.copy(x)
assert x != y
assert not(x == y)
assert y in y.derivatives.keys() # y must not copy the dependence on x
z = copy.deepcopy(x)
assert x != z
# Copy tests on expressions:
t = x + 2*z
# t depends on x:
assert x in t.derivatives
# The relationship between the copy of an expression and the
def setUpClass(cls):
aspec = AutomatedRunSpec()
aspec.mass_spectrometer = 'jan'
aspec.labnumber = '17005'
aspec.aliquot = 95
a = AutomatedRun()
a.script_info.measurement_script_name = 'unknown_peak_hop'
s = ArgusSpectrometerManager()
ion = IonOpticsManager(spectrometer=s.spectrometer)
s.load(db_mol_weights=False)
a.spectrometer_manager = s
a.ion_optics_manager = ion
a.arar_age = ArArAge()
a.arar_age.j=ufloat(0.001, 1e-6)
a._alive = True
a.uuid = '12345-ABCDE'
a.spec = aspec
a._measured = True
a._save_enabled = True
cls.arun = a
def test_inverse():
"Tests of the matrix inverse"
m = unumpy.matrix([[ufloat((10, 1)), -3.1],
[0, ufloat((3, 0))]])
m_nominal_values = unumpy.nominal_values(m)
# "Regular" inverse matrix, when uncertainties are not taken
# into account:
m_no_uncert_inv = m_nominal_values.I
# The matrix inversion should not yield numbers with uncertainties:
assert m_no_uncert_inv.dtype == numpy.dtype(float)
# Inverse with uncertainties:
m_inv_uncert = m.I # AffineScalarFunc elements
# The inverse contains uncertainties: it must support custom
# operations on matrices with uncertainties:
assert isinstance(m_inv_uncert, unumpy.matrix)
assert type(m_inv_uncert[0, 0]) == uncertainties.AffineScalarFunc
# Checks of the numerical values: the diagonal elements of the
# some arbitrary distribution
y_old = 1. + np.sin(x_old[:-1]*np.pi) / np.ediff1d(x_old)
y_old = unp.uarray(y_old, 0.1*y_old*uniform((m,)))
# rebin
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind='piecewise_constant')
# compute answer here to check rebin
y_new_here = unp.uarray(np.zeros(2), np.zeros(2))
y_new_here[0] = 0.5 * y_old[2] + y_old[3] + y_old[4] + 0.5 * y_old[5]
y_new_here[1] = 0.5 * y_old[5] + y_old[6] + 0.5 * y_old[7]
assert_allclose(unp.nominal_values(y_new),
unp.nominal_values(y_new_here))
# mean or nominal value comparison
assert_allclose(unp.std_devs(y_new),
unp.std_devs(y_new_here))
y_new_ref = unp.uarray(a,a)
y_new_ref[1] = y_old[0] * area_subbins[2] / area_old[0]
y_new_ref[2] = y_old[0] * area_subbins[3] / area_old[0]
y_new_ref[3] = y_old[0] * area_subbins[4] / area_old[0]
y_new_ref[4] = y_old[1] * area_subbins[5] / area_old[1]
y_new_ref[5] = y_old[1] * area_subbins[6] / area_old[1]
y_new_ref[5] += y_old[2] * area_subbins[7] / area_old[2]
y_new_ref[5] += y_old[3] * area_subbins[8] / area_old[3]
# call rebin function
y_new = rebin.rebin(x_old, y_old, x_new, interp_kind=3)
# mean or nominal value comparison
assert_allclose(unp.nominal_values(y_new),
unp.nominal_values(y_new_ref))
# mean or nominal value comparison
assert_allclose(unp.std_devs(y_new),
unp.std_devs(y_new_ref))
def test_broadcast_funcs():
"""
Test of mathematical functions that work with NumPy arrays of
numbers with uncertainties.
"""
x = uncertainties.ufloat((0.2, 0.1))
arr = numpy.array([x, 2*x])
assert unumpy.cos(arr)[1] == uncertainties.umath.cos(arr[1])
# Some functions do not bear the same name in the math module and
# in NumPy (acos instead of arccos, etc.):
assert unumpy.arccos(arr)[1] == uncertainties.umath.acos(arr[1])
# The acos() function should not exist in unumpy because it does
# not exist in numpy:
assert not hasattr(numpy, 'acos')
assert not hasattr(unumpy, 'acos')
# Test of the __all__ variable:
assert 'acos' not in unumpy.__all__
def exponent_frexp(x):
return umath.frexp(x)[1]
# to help make it a drop-in replacement for math (even though
# factorial() does not work on numbers with uncertainties
# because it is restricted to integers, as for
# math.factorial()):
assert umath.factorial(4) == 24
# Boolean functions:
assert not umath.isinf(x)
# Comparison, possibly between an AffineScalarFunc object and a
# boolean, which makes things more difficult for this code:
assert umath.isinf(x) == False
# fsum is special because it does not take a fixed number of
# variables:
assert umath.fsum([x, x]).nominal_value == -3
# The same exceptions should be generated when numbers with uncertainties
# are used:
## !! The Nose testing framework seems to catch an exception when
## it is aliased: "exc = OverflowError; ... except exc:..."
## surprisingly catches OverflowError. So, tests are written in a
## version-specific manner (until the Nose issue is resolved).
if sys.version_info < (2, 6):
try:
math.log(0)
except OverflowError(err_math): # "as", for Python 2.6+
pass
else:
def test_broadcast_funcs():
"""
Test of mathematical functions that work with NumPy arrays of
numbers with uncertainties.
"""
x = uncertainties.ufloat((0.2, 0.1))
arr = numpy.array([x, 2*x])
assert unumpy.cos(arr)[1] == uncertainties.umath.cos(arr[1])
# Some functions do not bear the same name in the math module and
# in NumPy (acos instead of arccos, etc.):
assert unumpy.arccos(arr)[1] == uncertainties.umath.acos(arr[1])
# The acos() function should not exist in unumpy because it does
# not exist in numpy:
assert not hasattr(numpy, 'acos')
assert not hasattr(unumpy, 'acos')
# Test of the __all__ variable:
assert 'acos' not in unumpy.__all__
def test_matrix():
"Matrices of numbers with uncertainties"
# Matrix inversion:
# Matrix with a mix of Variable objects and regular
# Python numbers:
m = unumpy.matrix([[ufloat((10, 1)), -3.1],
[0, ufloat((3, 0))]])
m_nominal_values = unumpy.nominal_values(m)
# Test of the nominal_value attribute:
assert numpy.all(m_nominal_values == m.nominal_values)
assert type(m[0, 0]) == uncertainties.Variable
# Test of scalar multiplication, both sides:
3*m
m*3