本文整理匯總了Python中numpy.can_cast方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.can_cast方法的具體用法?Python numpy.can_cast怎麽用?Python numpy.can_cast使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.can_cast方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_lonlat_to_healpix_shape
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def test_lonlat_to_healpix_shape():
healpix_index = lonlat_to_healpix(2 * u.deg, 3 * u.deg, 8)
assert np.can_cast(healpix_index, np.int64)
lon, lat = np.ones((2, 4)) * u.deg, np.zeros((2, 4)) * u.deg
healpix_index = lonlat_to_healpix(lon, lat, 8)
assert healpix_index.shape == (2, 4)
healpix_index, dx, dy = lonlat_to_healpix(2 * u.deg, 3 * u.deg, 8, return_offsets=True)
assert np.can_cast(healpix_index, np.int64)
assert isinstance(dx, float)
assert isinstance(dy, float)
lon, lat = np.ones((2, 4)) * u.deg, np.zeros((2, 4)) * u.deg
healpix_index, dx, dy = lonlat_to_healpix(lon, lat, 8, return_offsets=True)
assert healpix_index.shape == (2, 4)
assert dx.shape == (2, 4)
assert dy.shape == (2, 4)
示例2: test_can_cast_values
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def test_can_cast_values(self):
# gh-5917
for dt in np.sctypes['int'] + np.sctypes['uint']:
ii = np.iinfo(dt)
assert_(np.can_cast(ii.min, dt))
assert_(np.can_cast(ii.max, dt))
assert_(not np.can_cast(ii.min - 1, dt))
assert_(not np.can_cast(ii.max + 1, dt))
for dt in np.sctypes['float']:
fi = np.finfo(dt)
assert_(np.can_cast(fi.min, dt))
assert_(np.can_cast(fi.max, dt))
# Custom exception class to test exception propagation in fromiter
示例3: check_out_param
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def check_out_param(out, t, casting):
from .base import broadcast_to
if not hasattr(out, 'shape'):
raise TypeError('return arrays must be a tensor')
try:
broadcast_to(t, out.shape)
except ValueError:
raise ValueError("operands could not be broadcast together "
"with shapes ({0}) ({1})".format(','.join(str(s) for s in t.shape),
','.join(str(s) for s in out.shape)))
if not np.can_cast(t.dtype, out.dtype, casting):
raise TypeError("output (typecode '{0}') could not be coerced "
"to provided output paramter (typecode '{1}') "
"according to the casting rule ''{2}''".format(t.dtype.char, out.dtype.char, casting))
示例4: test_can_cast_values
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def test_can_cast_values(self):
# gh-5917
for dt in np.sctypes['int'] + np.sctypes['uint']:
ii = np.iinfo(dt)
assert_(np.can_cast(ii.min, dt))
assert_(np.can_cast(ii.max, dt))
assert_(not np.can_cast(ii.min - 1, dt))
assert_(not np.can_cast(ii.max + 1, dt))
for dt in np.sctypes['float']:
fi = np.finfo(dt)
assert_(np.can_cast(fi.min, dt))
assert_(np.can_cast(fi.max, dt))
# Custom exception class to test exception propagation in fromiter
示例5: set_data
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def set_data(self, A):
"""
Set the image array
ACCEPTS: numpy/PIL Image A
"""
# check if data is PIL Image without importing Image
if hasattr(A, 'getpixel'):
self._A = pil_to_array(A)
else:
self._A = cbook.safe_masked_invalid(A)
if (self._A.dtype != np.uint8 and
not np.can_cast(self._A.dtype, np.float)):
raise TypeError("Image data can not convert to float")
if (self._A.ndim not in (2, 3) or
(self._A.ndim == 3 and self._A.shape[-1] not in (3, 4))):
raise TypeError("Invalid dimensions for image data")
self._imcache = None
self._rgbacache = None
self._oldxslice = None
self._oldyslice = None
示例6: _can_cast_samekind
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def _can_cast_samekind(dtype1, dtype2):
"""Compatibility function for numpy 1.5.1; `casting` kw is numpy >=1.6.x
default for casting kw is 'safe', which gives the same result as in 1.5.x
and a strict subset of 'same_kind'. So for 1.5.x we just skip the cases
where 'safe' is False and 'same_kind' True.
"""
if np.__version__[:3] == '1.5':
return np.can_cast(dtype1, dtype2)
else:
return np.can_cast(dtype1, dtype2, casting='same_kind')
#------------------------------------------------------------------------------
# Generic tests
#------------------------------------------------------------------------------
# TODO check that spmatrix( ... , copy=X ) is respected
# TODO test prune
# TODO test has_sorted_indices
示例7: test_imul_scalar
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def test_imul_scalar(self):
def check(dtype):
dat = self.dat_dtypes[dtype]
datsp = self.datsp_dtypes[dtype]
# Avoid implicit casting.
if np.can_cast(type(2), dtype, casting='same_kind'):
a = datsp.copy()
a *= 2
b = dat.copy()
b *= 2
assert_array_equal(b, a.todense())
if np.can_cast(type(17.3), dtype, casting='same_kind'):
a = datsp.copy()
a *= 17.3
b = dat.copy()
b *= 17.3
assert_array_equal(b, a.todense())
for dtype in self.math_dtypes:
check(dtype)
示例8: test_idiv_scalar
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def test_idiv_scalar(self):
def check(dtype):
dat = self.dat_dtypes[dtype]
datsp = self.datsp_dtypes[dtype]
if np.can_cast(type(2), dtype, casting='same_kind'):
a = datsp.copy()
a /= 2
b = dat.copy()
b /= 2
assert_array_equal(b, a.todense())
if np.can_cast(type(17.3), dtype, casting='same_kind'):
a = datsp.copy()
a /= 17.3
b = dat.copy()
b /= 17.3
assert_array_equal(b, a.todense())
for dtype in self.math_dtypes:
# /= should only be used with float dtypes to avoid implicit
# casting.
if not np.can_cast(dtype, np.int_):
check(dtype)
示例9: set_data
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def set_data(self, A):
"""
Set the image array
ACCEPTS: numpy/PIL Image A
"""
self._full_res = A
self._A = A
if (self._A.dtype != np.uint8 and
not np.can_cast(self._A.dtype, np.float)):
raise TypeError("Image data can not convert to float")
if (self._A.ndim not in (2, 3) or
(self._A.ndim == 3 and self._A.shape[-1] not in (3, 4))):
raise TypeError("Invalid dimensions for image data")
self._imcache = None
self._rgbacache = None
self._oldxslice = None
self._oldyslice = None
self._sx, self._sy = None, None
示例10: _pad_and_fix_dtypes
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def _pad_and_fix_dtypes(self, cols, column_dtypes):
# Pad out Nones with empty arrays of appropriate dtypes
rtn = {}
index = cols[INDEX]
full_length = len(index)
for k, v in iteritems(cols):
if k != INDEX and k != 'SYMBOL':
col_len = len(v)
if col_len < full_length:
v = ([None, ] * (full_length - col_len)) + v
assert len(v) == full_length
for i, arr in enumerate(v):
if arr is None:
# Replace Nones with appropriate-length empty arrays
v[i] = self._empty(len(index[i]), column_dtypes.get(k))
else:
# Promote to appropriate dtype only if we can safely cast all the values
# This avoids the case with strings where None is cast as 'None'.
# Casting the object to a string is not worthwhile anyway as Pandas changes the
# dtype back to objectS
if (i == 0 or v[i].dtype != v[i - 1].dtype) and np.can_cast(v[i].dtype, column_dtypes[k],
casting='safe'):
v[i] = v[i].astype(column_dtypes[k], casting='safe')
rtn[k] = v
return rtn
示例11: test_nested_ring_shape
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def test_nested_ring_shape(function):
index = function(1, 8)
assert np.can_cast(index, np.int64)
index = function([[1, 2, 3], [2, 3, 4]], 8)
assert index.shape == (2, 3)
示例12: test_ang2pix_shape
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def test_ang2pix_shape():
ipix = hp_compat.ang2pix(8, 1., 2.)
assert np.can_cast(ipix, np.int64)
ipix = hp_compat.ang2pix(8, [[1., 2.], [3., 4.]], [[1., 2.], [3., 4.]])
assert ipix.shape == (2, 2)
示例13: test_vec2pix_shape
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def test_vec2pix_shape():
ipix = hp_compat.vec2pix(8, 1., 2., 3.)
assert np.can_cast(ipix, np.int64)
ipix = hp_compat.vec2pix(8, [[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]])
assert ipix.shape == (2, 2)
示例14: print_cancast_table
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def print_cancast_table(ntypes):
print('X', end=' ')
for char in ntypes:
print(char, end=' ')
print()
for row in ntypes:
print(row, end=' ')
for col in ntypes:
print(int(np.can_cast(row, col)), end=' ')
print()
示例15: replace
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import can_cast [as 別名]
def replace(self, to_replace, value, inplace=False, filter=None,
regex=False, convert=True):
inplace = validate_bool_kwarg(inplace, 'inplace')
to_replace_values = np.atleast_1d(to_replace)
if not np.can_cast(to_replace_values, bool):
return self
return super(BoolBlock, self).replace(to_replace, value,
inplace=inplace, filter=filter,
regex=regex, convert=convert)