本文整理汇总了Python中numpy.errstate函数的典型用法代码示例。如果您正苦于以下问题:Python errstate函数的具体用法?Python errstate怎么用?Python errstate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了errstate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: rectify_obs
def rectify_obs(obs):
"""Make sure the passed obs dictionary conforms to code expectations,
and make simple fixes when possible.
"""
k = obs.keys()
if 'maggies' not in k:
obs['maggies'] = None
obs['maggies_unc'] = None
if 'spectrum' not in k:
obs['spectrum'] = None
obs['unc'] = None
if obs['maggies'] is not None:
assert (len(obs['filters']) == len(obs['maggies']))
assert ('maggies_unc' in k)
assert ((len(obs['maggies']) == len(obs['maggies_unc'])) or
(np.size(obs['maggies_unc'] == 1)))
m = obs.get('phot_mask', np.ones(len(obs['maggies']), dtype=bool))
obs['phot_mask'] = (m * np.isfinite(obs['maggies']) *
np.isfinite(obs['maggies_unc']) *
(obs['maggies_unc'] > 0))
try:
obs['filternames'] = [f.name for f in obs['filters']]
except:
pass
if 'logify_spectrum' not in k:
obs['logify_spectrum'] = False
if obs['spectrum'] is not None:
assert (len(obs['wavelength']) == len(obs['spectrum']))
assert ('unc' in k)
np.errstate(invalid='ignore')
m = obs.get('mask', np.ones(len(obs['wavelength']), dtype=bool))
obs['mask'] = (m * np.isfinite(obs['spectrum']) *
np.isfinite(obs['unc']) * (obs['unc'] > 0))
return obs
示例2: getEff
def getEff(s, cut, comp='joint', reco=True):
eff, sig, relerr = {},{},{}
a = np.log10(s['MC_energy'])
Ebins = getEbins()
Emids = getMids(Ebins)
erangeDict = getErange()
c0 = cut
if comp != 'joint':
compcut = s['comp'] == comp
c0 = cut * compcut
# Set radii for finding effective area
rDict = {}
keys = ['low', 'mid', 'high']
for key in keys:
rDict[key] = np.array([600, 800, 1100, 1700, 2600, 2900])
rDict['low'][1] = 600
Ebreaks = np.array([4, 5, 6, 7, 8, 9])
rgrp = np.digitize(Emids, Ebreaks) - 1
for key in keys:
# Get efficiency and sigma
simcut = np.array([sim in erangeDict[key] for sim in s['sim']])
k = np.histogram(a[c0*simcut], bins=Ebins)[0]
#k = Nfinder(a, c0*simcut)
n = s['MC'][comp][key].astype('float')
eff[key], sig[key], relerr[key] = np.zeros((3, len(k)))
with np.errstate(divide='ignore', invalid='ignore'):
eff[key] = k / n
var = (k+1)*(k+2)/((n+2)*(n+3)) - (k+1)**2/((n+2)**2)
sig[key] = np.sqrt(var)
# Multiply by throw area
r = np.array([rDict[key][i] for i in rgrp])
eff[key] *= np.pi*(r**2)
sig[key] *= np.pi*(r**2)
# Deal with parts of the arrays with no information
for i in range(len(eff[key])):
if n[i] == 0:
eff[key][i] = 0
sig[key][i] = np.inf
# Combine low, mid, and high energy datasets
eff_tot = (np.sum([eff[key]/sig[key] for key in keys], axis=0) /
np.sum([1/sig[key] for key in keys], axis=0))
sig_tot = np.sqrt(1 / np.sum([1/sig[key]**2 for key in keys], axis=0))
with np.errstate(divide='ignore'):
relerr = sig_tot / eff_tot
# UGH : find better way to do this
if reco:
eff_tot = eff_tot[20:]
sig_tot = sig_tot[20:]
relerr = relerr[20:]
return eff_tot, sig_tot, relerr
示例3: test_no_scaling
def test_no_scaling(self):
# Test writing image converting types when no scaling
img_class = self.image_class
hdr_class = img_class.header_class
hdr = hdr_class()
supported_types = supported_np_types(hdr)
slope = 2
inter = 10 if hdr.has_data_intercept else 0
for in_dtype, out_dtype in itertools.product(
FLOAT_TYPES + IUINT_TYPES,
supported_types):
# Need to check complex scaling
mn_in, mx_in = _dt_min_max(in_dtype)
arr = np.array([mn_in, -1, 0, 1, 10, mx_in], dtype=in_dtype)
img = img_class(arr, np.eye(4), hdr)
img.set_data_dtype(out_dtype)
img.header.set_slope_inter(slope, inter)
with np.errstate(invalid='ignore'):
rt_img = bytesio_round_trip(img)
with suppress_warnings(): # invalid mult
back_arr = rt_img.get_data()
exp_back = arr.copy()
if in_dtype not in COMPLEX_TYPES:
exp_back = arr.astype(float)
if out_dtype in IUINT_TYPES:
with np.errstate(invalid='ignore'):
exp_back = np.round(exp_back)
exp_back = np.clip(exp_back, *shared_range(float, out_dtype))
exp_back = exp_back.astype(out_dtype).astype(float)
else:
exp_back = exp_back.astype(out_dtype)
# Allow for small differences in large numbers
with suppress_warnings(): # invalid value
assert_allclose_safely(back_arr,
exp_back * slope + inter)
示例4: test_3d
def test_3d():
x = np.array([[9.0, 3.0, nan, nan, 9.0, nan],
[1.0, 1.0, 1.0, nan, nan, nan],
[2.0, 2.0, 0.1, nan, 1.0, nan], # 0.0 kills geometric mean
[3.0, 9.0, 2.0, nan, nan, nan],
[4.0, 4.0, 3.0, 9.0, 2.0, nan],
[5.0, 5.0, 4.0, 4.0, nan, nan]])
sectors = ['a', 'b', 'a', 'b', 'a', 'c']
x = np.dstack((x,x))
for func in funcs_one:
xc = x.copy()
args = (xc,)
with np.errstate(invalid='ignore', divide='ignore'):
yield check_return_array, func, args
for func in funcs_oneint:
xc = x.copy()
args = (xc, 2)
with np.errstate(invalid='ignore', divide='ignore'):
yield check_return_array, func, args
for func in funcs_onefrac:
xc = x.copy()
args = (xc, -1, 0.5)
with np.errstate(invalid='ignore', divide='ignore'):
yield check_return_array, func, args
for func in funcs_sect:
xc = x.copy()
args = (xc, sectors)
with np.errstate(invalid='ignore', divide='ignore'):
yield check_return_array, func, args
示例5: _convertImagesToUint8
def _convertImagesToUint8(self, imageR, imageG, imageB):
"""Use the mapping to convert images imageR, imageG, and imageB to a triplet of uint8 images
"""
imageR = imageR - self.minimum[0] # n.b. makes copy
imageG = imageG - self.minimum[1]
imageB = imageB - self.minimum[2]
fac = self.mapIntensityToUint8(self.intensity(imageR, imageG, imageB))
imageRGB = [imageR, imageG, imageB]
with np.errstate(invalid="ignore"): # suppress NAN warnings
for c in imageRGB:
c *= fac
# individual bands can still be < 0, even if fac isn't
c[c < 0] = 0
pixmax = self._uint8Max
# copies -- could work row by row to minimise memory usage
r0, g0, b0 = imageRGB
# n.b. np.where can't and doesn't short-circuit
with np.errstate(invalid='ignore', divide='ignore'):
for i, c in enumerate(imageRGB):
c = np.where(r0 > g0,
np.where(r0 > b0,
np.where(r0 >= pixmax, c*pixmax/r0, c),
np.where(b0 >= pixmax, c*pixmax/b0, c)),
np.where(g0 > b0,
np.where(g0 >= pixmax, c*pixmax/g0, c),
np.where(b0 >= pixmax, c*pixmax/b0, c))).astype(np.uint8)
c[c > pixmax] = pixmax
imageRGB[i] = c
return imageRGB
示例6: _solve_quadratic
def _solve_quadratic(a__, b__, c__, min_val=0.0, max_val=1.0):
"""Solve quadratic equation and return the valid roots from interval
[*min_val*, *max_val*]
"""
def int_and_float_to_numpy(val):
if not isinstance(val, np.ndarray):
if isinstance(val, (int, float)):
val = [val]
val = np.array(val)
return val
a__ = int_and_float_to_numpy(a__)
b__ = int_and_float_to_numpy(b__)
c__ = int_and_float_to_numpy(c__)
discriminant = b__ * b__ - 4 * a__ * c__
# Solve the quadratic polynomial
with np.errstate(invalid='ignore', divide='ignore'):
x_1 = (-b__ + np.sqrt(discriminant)) / (2 * a__)
x_2 = (-b__ - np.sqrt(discriminant)) / (2 * a__)
# Find valid solutions, ie. 0 <= t <= 1
x__ = x_1.copy()
with np.errstate(invalid='ignore'):
idxs = (x_1 < min_val) | (x_1 > max_val)
x__[idxs] = x_2[idxs]
with np.errstate(invalid='ignore'):
idxs = (x__ < min_val) | (x__ > max_val)
x__[idxs] = np.nan
return x__
示例7: test_scaling
def test_scaling(self):
# Test integer scaling from float
# Analyze headers cannot do float-integer scaling
hdr = self.header_class()
assert_true(hdr.default_x_flip)
shape = (1,2,3)
hdr.set_data_shape(shape)
hdr.set_data_dtype(np.float32)
data = np.ones(shape, dtype=np.float64)
S = BytesIO()
# Writing to float datatype doesn't need scaling
hdr.data_to_fileobj(data, S)
rdata = hdr.data_from_fileobj(S)
assert_array_almost_equal(data, rdata)
# Now test writing to integers
hdr.set_data_dtype(np.int32)
# Writing to int needs scaling, and raises an error if we can't scale
if not hdr.has_data_slope:
assert_raises(HeaderTypeError, hdr.data_to_fileobj, data, BytesIO())
# But if we aren't scaling, convert the floats to integers and write
with np.errstate(invalid='ignore'):
hdr.data_to_fileobj(data, S, rescale=False)
rdata = hdr.data_from_fileobj(S)
assert_true(np.allclose(data, rdata))
# This won't work for floats that aren't close to integers
data_p5 = data + 0.5
with np.errstate(invalid='ignore'):
hdr.data_to_fileobj(data_p5, S, rescale=False)
rdata = hdr.data_from_fileobj(S)
assert_false(np.allclose(data_p5, rdata))
示例8: _findAndCategorizeContours
def _findAndCategorizeContours(binary):
# see auromat.utils.outline_opencv for why we need to pad the image
imCv = np.zeros((binary.shape[0]+2, binary.shape[1]+2), dtype=np.uint8)
imCv[1:-1,1:-1] = binary
contours,_ = cv.findContours(imCv, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
contours = np.asarray(contours)
contours -= 1
area = np.asarray([cv.contourArea(c) for c in contours])
rectAxes = np.asarray([cv.minAreaRect(c)[1] for c in contours])
# isBigContour needs to be big enough to not discard bigger stars!
# this could leave some spacecraft structures intact which may or may not confuse astrometry
# TODO the ratio below should depend on the estimated celestial pixel scale
# and the exposure time (longer exposure = longer star trails)
bigContourAreaRatio = 0.000013 # 0.0013% of the image area (~160 pixels for 12MP images)
bigContourArea = bigContourAreaRatio*(binary.shape[0]*binary.shape[1])
isBigContour = area > int(bigContourArea)
isSmallContour = ~isBigContour
longRatioThreshold = 5
with np.errstate(divide='ignore', invalid='ignore'): # division produces nans and infs
rectRatio = rectAxes[:,0]/rectAxes[:,1] if len(contours) > 0 else np.array([])
with np.errstate(invalid='ignore'):
isLongContour = np.logical_and(area > 20, # exclude very tiny long contours (could be stars) and inf ratios
np.logical_or(rectRatio > longRatioThreshold,
rectRatio < 1/longRatioThreshold)
)
isSmallLongContour = np.logical_and(isSmallContour, isLongContour)
isSmallShortContour = np.logical_and(isSmallContour, ~isLongContour)
return contours, area, isBigContour, isSmallLongContour, isSmallShortContour
示例9: test_modulo
def test_modulo(self):
# GH3590, modulo as ints
p = DataFrame({'first': [3, 4, 5, 8], 'second': [0, 0, 0, 3]})
# this is technically wrong as the integer portion is coerced to float
# ###
expected = DataFrame({'first': Series([0, 0, 0, 0], dtype='float64'),
'second': Series([np.nan, np.nan, np.nan, 0])})
result = p % p
assert_frame_equal(result, expected)
# numpy has a slightly different (wrong) treatement
with np.errstate(all='ignore'):
arr = p.values % p.values
result2 = DataFrame(arr, index=p.index,
columns=p.columns, dtype='float64')
result2.iloc[0:3, 1] = np.nan
assert_frame_equal(result2, expected)
result = p % 0
expected = DataFrame(np.nan, index=p.index, columns=p.columns)
assert_frame_equal(result, expected)
# numpy has a slightly different (wrong) treatement
with np.errstate(all='ignore'):
arr = p.values.astype('float64') % 0
result2 = DataFrame(arr, index=p.index, columns=p.columns)
assert_frame_equal(result2, expected)
# not commutative with series
p = DataFrame(np.random.randn(10, 5))
s = p[0]
res = s % p
res2 = p % s
self.assertFalse(np.array_equal(res.fillna(0), res2.fillna(0)))
示例10: test_nanvar_issue60
def test_nanvar_issue60():
"nanvar regression test (issue #60)"
with warnings.catch_warnings():
warnings.simplefilter("ignore")
f = bn.nanvar([1.0], ddof=1)
with np.errstate(invalid='ignore'):
s = bn.slow.nanvar([1.0], ddof=1)
assert_equal(f, s, err_msg="bn.nanvar([1.0], ddof=1) wrong")
f = bn.nanvar([1], ddof=1)
with np.errstate(invalid='ignore'):
s = bn.slow.nanvar([1], ddof=1)
assert_equal(f, s, err_msg="bn.nanvar([1], ddof=1) wrong")
f = bn.nanvar([1, np.nan], ddof=1)
with np.errstate(invalid='ignore'):
s = bn.slow.nanvar([1, np.nan], ddof=1)
assert_equal(f, s, err_msg="bn.nanvar([1, nan], ddof=1) wrong")
f = bn.nanvar([[1, np.nan], [np.nan, 1]], axis=0, ddof=1)
with np.errstate(invalid='ignore'):
s = bn.slow.nanvar([[1, np.nan], [np.nan, 1]], axis=0, ddof=1)
assert_equal(f, s, err_msg="issue #60 regression")
示例11: move_unit_maker
def move_unit_maker(func, arrfunc, methods):
"Test that different mov methods give the same results on 2d input."
arr1 = np.array([1, 2, 3, 4, 5, 6, nan, nan, 7, 8, 9])
arr2 = np.array([[9.0, 3.0, nan, nan, 9.0, nan],
[1.0, 1.0, 1.0, nan, nan, nan],
[2.0, 2.0, 0.1, nan, 1.0, nan],
[3.0, 9.0, 2.0, nan, nan, nan],
[4.0, 4.0, 3.0, 9.0, 2.0, nan],
[5.0, 5.0, 4.0, 4.0, nan, nan]])
arr3 = np.arange(60).reshape(3, 4, 5)
arr4 = np.array([nan, nan, nan])
arrs = [arr1, arr2, arr3, arr4]
msg = '\nfunc %s | method %s | nd %d | window %d | axis %d\n'
for arr in arrs:
for axis in range(arr.ndim):
for w in range(1, arr.shape[axis]):
actual = func(arr, window=w, axis=axis, method='loop')
for method in methods:
if method == 'func_loop':
with np.errstate(invalid='ignore'):
d = move_func(arrfunc, arr, window=w, axis=axis,
method='loop')
elif method == 'func_strides':
with np.errstate(invalid='ignore'):
d = move_func(arrfunc, arr, window=w, axis=axis,
method='strides')
else:
d = func(arr, window=w, axis=axis, method=method)
err_msg = msg % (func.__name__, method, arr.ndim, w, axis)
assert_array_almost_equal(actual, d, 10, err_msg)
示例12: test_return_array
def test_return_array():
"Check that functions return a numpy array or a scalar."
x = np.array([[9.0, 3.0, nan, nan, 9.0, nan],
[1.0, 1.0, 1.0, nan, nan, nan],
[2.0, 2.0, 9.0, nan, 1.0, nan],
[3.0, 9.0, 2.0, nan, nan, nan],
[4.0, 4.0, 3.0, 9.0, 2.0, nan],
[5.0, 5.0, 4.0, 4.0, nan, nan]])
sectors = ['a', 'b', 'a', 'b', 'a', 'c']
for func in funcs_one:
xc = x.copy()
args = (xc,)
with np.errstate(invalid='ignore', divide='ignore'):
yield check_return_array, func, args
for func in funcs_oneint:
xc = x.copy()
args = (xc, 2)
with np.errstate(invalid='ignore', divide='ignore'):
yield check_return_array, func, args
for func in funcs_onefrac:
xc = x.copy()
args = (xc, -1, 0.5)
with np.errstate(invalid='ignore', divide='ignore'):
yield check_return_array, func, args
for func in funcs_sect:
xc = x.copy()
args = (xc, sectors)
with np.errstate(invalid='ignore', divide='ignore'):
yield check_return_array, func, args
示例13: compute_mandelbrot
def compute_mandelbrot():
N_max = 50
some_threshold = 50
grid_interval = 1000
# construct the grid
x = np.linspace(-2, 1, grid_interval)
y = np.linspace(-1.5, 1.5, grid_interval)
c = x[:, np.newaxis] + 1j*y[np.newaxis, :]
# do the iteration
z = c
for v in range(N_max):
with np.errstate(all = "ignore"): # catches overflow and invalid value errors
z = z**2 + c
with np.errstate(all = "ignore"): # catches overflow and invalid value errors
# form a 2-D boolean mask
mask = (abs(z) < some_threshold)
# save the result to an image
plt.imshow(mask.T, extent = [-2, 1, -1.5, 1.5])
plt.gray()
plt.savefig('mandelbrot.png')
示例14: test_correlation_9
def test_correlation_9(self):
"farray.correlation_9"
x = self.a1
y = self.a2
x2 = np.empty((2, x.shape[0], x.shape[1]))
x2[0] = x
x2[1] = x
y2 = np.empty((2, y.shape[0], y.shape[1]))
y2[0] = y
y2[1] = y
with np.errstate(invalid='ignore'):
corr = correlation(x, y, axis=-1)
desired = np.array([nan, 1, -1, -0.5])
aae(corr, desired, err_msg="aggregate of 1d tests")
x = self.b1
y = self.b2
x2 = np.empty((2, x.shape[0], x.shape[1]))
x2[0] = x
x2[1] = x
y2 = np.empty((2, y.shape[0], y.shape[1]))
y2[0] = y
y2[1] = y
with np.errstate(invalid='ignore'):
corr = correlation(x, y, axis=-1)
desired = np.array([nan, 1, -1, -0.5])
aae(corr, desired, err_msg="aggregate of 1d tests")
示例15: test_SphericalCoordinates_bounds
def test_SphericalCoordinates_bounds(pyntcloud_with_rgb_and_normals):
scalar_field = SphericalCoordinates(
pyntcloud=pyntcloud_with_rgb_and_normals)
scalar_field.extract_info()
with np.errstate(divide='ignore', invalid='ignore'):
scalar_field.compute()
assert all(scalar_field.to_be_added["polar"] >= 0)
assert all(scalar_field.to_be_added["polar"] <= 180)
assert all(scalar_field.to_be_added["azimuthal"] >= -180)
assert all(scalar_field.to_be_added["azimuthal"] <= 180)
scalar_field = SphericalCoordinates(
pyntcloud=pyntcloud_with_rgb_and_normals,
degrees=False)
scalar_field.extract_info()
with np.errstate(divide='ignore', invalid='ignore'):
scalar_field.compute()
assert all(scalar_field.to_be_added["polar"] >= 0)
assert all(scalar_field.to_be_added["polar"] <= np.pi)
assert all(scalar_field.to_be_added["azimuthal"] >= -np.pi)
assert all(scalar_field.to_be_added["azimuthal"] <= np.pi)