def check_int_a2f(in_type, out_type): # Check that array to / from file returns roughly the same as input big_floater = np.maximum_sctype(np.float) info = type_info(in_type) this_min, this_max = info['min'], info['max'] if not in_type in np.sctypes['complex']: data = np.array([this_min, this_max], in_type) else: # Funny behavior with complex256 data = np.zeros((2, ), in_type) data[0] = this_min + 0j data[1] = this_max + 0j str_io = BytesIO() try: scale, inter, mn, mx = calculate_scale(data, out_type, True) except ValueError: if DEBUG: print in_type, out_type, sys.exc_info()[1] return array_to_file(data, str_io, out_type, 0, inter, scale, mn, mx) data_back = array_from_file(data.shape, out_type, str_io) data_back = apply_read_scaling(data_back, scale, inter) assert_true(np.allclose(big_floater(data), big_floater(data_back))) # Try with analyze-size scale and inter scale32 = np.float32(scale) inter32 = np.float32(inter) if scale32 == np.inf or inter32 == np.inf: return data_back = array_from_file(data.shape, out_type, str_io) data_back = apply_read_scaling(data_back, scale32, inter32) # Clip at extremes to remove inf info = type_info(in_type) out_min, out_max = info['min'], info['max'] assert_true( np.allclose(big_floater(data), big_floater(np.clip(data_back, out_min, out_max))))
def check_int_a2f(in_type, out_type): # Check that array to / from file returns roughly the same as input big_floater = np.maximum_sctype(np.float) info = type_info(in_type) this_min, this_max = info['min'], info['max'] if not in_type in np.sctypes['complex']: data = np.array([this_min, this_max], in_type) else: # Funny behavior with complex256 data = np.zeros((2,), in_type) data[0] = this_min + 0j data[1] = this_max + 0j str_io = BytesIO() try: scale, inter, mn, mx = calculate_scale(data, out_type, True) except ValueError: if DEBUG: print in_type, out_type, sys.exc_info()[1] return array_to_file(data, str_io, out_type, 0, inter, scale, mn, mx) data_back = array_from_file(data.shape, out_type, str_io) data_back = apply_read_scaling(data_back, scale, inter) assert_true(np.allclose(big_floater(data), big_floater(data_back))) # Try with analyze-size scale and inter scale32 = np.float32(scale) inter32 = np.float32(inter) if scale32 == np.inf or inter32 == np.inf: return data_back = array_from_file(data.shape, out_type, str_io) data_back = apply_read_scaling(data_back, scale32, inter32) # Clip at extremes to remove inf info = type_info(in_type) out_min, out_max = info['min'], info['max'] assert_true(np.allclose(big_floater(data), big_floater(np.clip(data_back, out_min, out_max))))
def test_scale_min_max(): mx_dt = np.maximum_sctype(np.float) for tp in np.sctypes['uint'] + np.sctypes['int']: info = np.iinfo(tp) # Need to pump up to max fp type to contain python longs imin = np.array(info.min, dtype=mx_dt) imax = np.array(info.max, dtype=mx_dt) value_pairs = ( (0, imax), (imin, 0), (imin, imax), (1, 10), (-1, -1), (1, 1), (-10, -1), (-100, 10)) for mn, mx in value_pairs: # with intercept scale, inter = scale_min_max(mn, mx, tp, True) if mx-mn: assert_array_almost_equal, (mx-inter) / scale, imax assert_array_almost_equal, (mn-inter) / scale, imin else: assert_equal, (scale, inter), (1.0, mn) # without intercept if imin == 0 and mn < 0 and mx > 0: (assert_raises, ValueError, scale_min_max, mn, mx, tp, False) continue scale, inter = scale_min_max(mn, mx, tp, False) assert_equal, inter, 0.0 if mn == 0 and mx == 0: assert_equal, scale, 1.0 continue sc_mn = mn / scale sc_mx = mx / scale assert_true, sc_mn >= imin assert_true, sc_mx <= imax if imin == 0: if mx > 0: # numbers all +ve assert_array_almost_equal, mx / scale, imax else: # numbers all -ve assert_array_almost_equal, mn / scale, imax continue if abs(mx) >= abs(mn): assert_array_almost_equal, mx / scale, imax else: assert_array_almost_equal, mn / scale, imin
def test_scale_min_max(): mx_dt = np.maximum_sctype(np.float) for tp in np.sctypes['uint'] + np.sctypes['int']: info = np.iinfo(tp) # Need to pump up to max fp type to contain python longs imin = np.array(info.min, dtype=mx_dt) imax = np.array(info.max, dtype=mx_dt) value_pairs = ( (0, imax), (imin, 0), (imin, imax), (1, 10), (-1, -1), (1, 1), (-10, -1), (-100, 10)) for mn, mx in value_pairs: # with intercept scale, inter = scale_min_max(mn, mx, tp, True) if mx - mn: assert_array_almost_equal, (mx - inter) / scale, imax assert_array_almost_equal, (mn - inter) / scale, imin else: assert_equal, (scale, inter), (1.0, mn) # without intercept if imin == 0 and mn < 0 and mx > 0: (assert_raises, ValueError, scale_min_max, mn, mx, tp, False) continue scale, inter = scale_min_max(mn, mx, tp, False) assert_equal, inter, 0.0 if mn == 0 and mx == 0: assert_equal, scale, 1.0 continue sc_mn = mn / scale sc_mx = mx / scale assert_true, sc_mn >= imin assert_true, sc_mx <= imax if imin == 0: if mx > 0: # numbers all +ve assert_array_almost_equal, mx / scale, imax else: # numbers all -ve assert_array_almost_equal, mn / scale, imax continue if abs(mx) >= abs(mn): assert_array_almost_equal, mx / scale, imax else: assert_array_almost_equal, mn / scale, imin
def check_int_a2f(in_type, out_type): # Check that array to / from file returns roughly the same as input big_floater = np.maximum_sctype(np.float) info = type_info(in_type) this_min, this_max = info['min'], info['max'] if not in_type in np.sctypes['complex']: data = np.array([this_min, this_max], in_type) # Bug in numpy 1.6.2 on PPC leading to infs - abort if not np.all(np.isfinite(data)): if DEBUG: print(f'Hit PPC max -> inf bug; skip in_type {in_type}') return else: # Funny behavior with complex256 data = np.zeros((2, ), in_type) data[0] = this_min + 0j data[1] = this_max + 0j str_io = BytesIO() try: scale, inter, mn, mx = _calculate_scale(data, out_type, True) except ValueError as e: if DEBUG: warnings.warn(str((in_type, out_type, e))) return array_to_file(data, str_io, out_type, 0, inter, scale, mn, mx) data_back = array_from_file(data.shape, out_type, str_io) data_back = apply_read_scaling(data_back, scale, inter) assert np.allclose(big_floater(data), big_floater(data_back)) # Try with analyze-size scale and inter scale32 = np.float32(scale) inter32 = np.float32(inter) if scale32 == np.inf or inter32 == np.inf: return data_back = array_from_file(data.shape, out_type, str_io) data_back = apply_read_scaling(data_back, scale32, inter32) # Clip at extremes to remove inf info = type_info(in_type) out_min, out_max = info['min'], info['max'] assert np.allclose(big_floater(data), big_floater(np.clip(data_back, out_min, out_max)))
real (scalar) part, and ``x, y, z`` are the complex (vector) part. Note - rotation matrices here apply to column vectors, that is, they are applied on the left of the vector. For example: >>> import numpy as np >>> q = [0, 1, 0, 0] # 180 degree rotation around axis 0 >>> M = quat2mat(q) # from this module >>> vec = np.array([1, 2, 3]).reshape((3,1)) # column vector >>> tvec = np.dot(M, vec) ''' import math import numpy as np MAX_FLOAT = np.maximum_sctype(np.float) FLOAT_EPS = np.finfo(np.float).eps def fillpositive(xyz, w2_thresh=None): ''' Compute unit quaternion from last 3 values Parameters ---------- xyz : iterable iterable containing 3 values, corresponding to quaternion x, y, z w2_thresh : None or float, optional threshold to determine if w squared is really negative. If None (default) then w2_thresh set equal to ``-np.finfo(xyz.dtype).eps``, if possible, otherwise ``-np.finfo(np.float).eps``
def test_other(self, t): assert_equal(np.maximum_sctype(t), t)
def test_complex(self, t): assert_equal(np.maximum_sctype(t), np.sctypes['complex'][-1])
def test_float(self, t): assert_equal(np.maximum_sctype(t), np.sctypes['float'][-1])
# and string comma- (or '+') separated lists of bit flags). # 5. Added 'is_bit_flag()' function to check if an integer number has # only one bit set (i.e., that it is a power of 2). # 1.1.0 (29-January-2018) - Multiple enhancements: # 1. Added support for long type in Python 2.7 in # `interpret_bit_flags()` and `bitfield_to_boolean_mask()`. # 2. `interpret_bit_flags()` now always returns `int` (or `int` or `long` # in Python 2.7). Previously when input was of integer-like type # (i.e., `numpy.uint64`), it was not converted to Python `int`. # 3. `bitfield_to_boolean_mask()` will no longer crash when # `ignore_flags` argument contains bit flags beyond what the type of # the argument `bitfield` can hold. # 1.1.1 (30-January-2018) - Improved filtering of high bits in flags. # INT_TYPE = (int, long,) if sys.version_info < (3,) else (int,) MAX_UINT_TYPE = np.maximum_sctype(np.uint) SUPPORTED_FLAGS = int(np.bitwise_not( 0, dtype=MAX_UINT_TYPE, casting='unsafe' )) def is_bit_flag(n): """ Verifies if the input number is a bit flag (i.e., an integer number that is an integer power of 2). Parameters ---------- n : int A positive integer number. Non-positive integers are considered not to be "flags".
def test_int(self, t): assert_equal(np.maximum_sctype(t), np.sctypes["int"][-1])
def scale_min_max(mn, mx, out_type, allow_intercept): ''' Return scaling and intercept min, max of data, given output type Returns ``scalefactor`` and ``intercept`` to best fit data with given ``mn`` and ``mx`` min and max values into range of data type with ``type_min`` and ``type_max`` min and max values for type. The calculated scaling is therefore:: scaled_data = (data-intercept) / scalefactor Parameters ---------- mn : scalar data minimum value mx : scalar data maximum value out_type : numpy type numpy type of output allow_intercept : bool If true, allow calculation of non-zero intercept. Otherwise, returned intercept is always 0.0 Returns ------- scalefactor : numpy scalar, dtype=np.maximum_sctype(np.float) scalefactor by which to divide data after subtracting intercept intercept : numpy scalar, dtype=np.maximum_sctype(np.float) value to subtract from data before dividing by scalefactor Examples -------- >>> scale_min_max(0, 255, np.uint8, False) (1.0, 0.0) >>> scale_min_max(-128, 127, np.int8, False) (1.0, 0.0) >>> scale_min_max(0, 127, np.int8, False) (1.0, 0.0) >>> scaling, intercept = scale_min_max(0, 127, np.int8, True) >>> np.allclose((0 - intercept) / scaling, -128) True >>> np.allclose((127 - intercept) / scaling, 127) True >>> scaling, intercept = scale_min_max(-10, -1, np.int8, True) >>> np.allclose((-10 - intercept) / scaling, -128) True >>> np.allclose((-1 - intercept) / scaling, 127) True >>> scaling, intercept = scale_min_max(1, 10, np.int8, True) >>> np.allclose((1 - intercept) / scaling, -128) True >>> np.allclose((10 - intercept) / scaling, 127) True Notes ----- We don't use this function anywhere in nibabel now, it's here for API compatibility only. The large integers lead to python long types as max / min for type. To contain the rounding error, we need to use the maximum numpy float types when casting to float. ''' if mn > mx: raise ValueError('min value > max value') info = type_info(out_type) mn, mx, type_min, type_max = np.array( [mn, mx, info['min'], info['max']], np.maximum_sctype(np.float)) # with intercept if allow_intercept: data_range = mx-mn if data_range == 0: return 1.0, mn type_range = type_max - type_min scaling = data_range / type_range intercept = mn - type_min * scaling return scaling, intercept # without intercept if mx == 0 and mn == 0: return 1.0, 0.0 if type_min == 0: # uint if mn < 0 and mx > 0: raise ValueError('Cannot scale negative and positive ' 'numbers to uint without intercept') if mx < 0: scaling = mn / type_max else: scaling = mx / type_max else: # int if abs(mx) >= abs(mn): scaling = mx / type_max else: scaling = mn / type_min return scaling, 0.0
def scale_min_max(mn, mx, out_type, allow_intercept): ''' Return scaling and intercept min, max of data, given output type Returns ``scalefactor`` and ``intercept`` to best fit data with given ``mn`` and ``mx`` min and max values into range of data type with ``type_min`` and ``type_max`` min and max values for type. The calculated scaling is therefore:: scaled_data = (data-intercept) / scalefactor Parameters ---------- mn : scalar data minimum value mx : scalar data maximum value out_type : numpy type numpy type of output allow_intercept : bool If true, allow calculation of non-zero intercept. Otherwise, returned intercept is always 0.0 Returns ------- scalefactor : numpy scalar, dtype=np.maximum_sctype(np.float) scalefactor by which to divide data after subtracting intercept intercept : numpy scalar, dtype=np.maximum_sctype(np.float) value to subtract from data before dividing by scalefactor Examples -------- >>> scale_min_max(0, 255, np.uint8, False) (1.0, 0.0) >>> scale_min_max(-128, 127, np.int8, False) (1.0, 0.0) >>> scale_min_max(0, 127, np.int8, False) (1.0, 0.0) >>> scaling, intercept = scale_min_max(0, 127, np.int8, True) >>> np.allclose((0 - intercept) / scaling, -128) True >>> np.allclose((127 - intercept) / scaling, 127) True >>> scaling, intercept = scale_min_max(-10, -1, np.int8, True) >>> np.allclose((-10 - intercept) / scaling, -128) True >>> np.allclose((-1 - intercept) / scaling, 127) True >>> scaling, intercept = scale_min_max(1, 10, np.int8, True) >>> np.allclose((1 - intercept) / scaling, -128) True >>> np.allclose((10 - intercept) / scaling, 127) True Notes ----- We don't use this function anywhere in nibabel now, it's here for API compatibility only. The large integers lead to python long types as max / min for type. To contain the rounding error, we need to use the maximum numpy float types when casting to float. ''' if mn > mx: raise ValueError('min value > max value') info = type_info(out_type) mn, mx, type_min, type_max = np.array([mn, mx, info['min'], info['max']], np.maximum_sctype(np.float)) # with intercept if allow_intercept: data_range = mx - mn if data_range == 0: return 1.0, mn type_range = type_max - type_min scaling = data_range / type_range intercept = mn - type_min * scaling return scaling, intercept # without intercept if mx == 0 and mn == 0: return 1.0, 0.0 if type_min == 0: # uint if mn < 0 and mx > 0: raise ValueError('Cannot scale negative and positive ' 'numbers to uint without intercept') if mx < 0: scaling = mn / type_max else: scaling = mx / type_max else: # int if abs(mx) >= abs(mn): scaling = mx / type_max else: scaling = mn / type_min return scaling, 0.0
import numpy as np # Techincally this works, but probably shouldn't. See # # https://github.com/numpy/numpy/issues/16366 # np.maximum_sctype(1) # E: incompatible type "int" np.issubsctype(1, np.int64) # E: incompatible type "int" np.issubdtype(1, np.int64) # E: incompatible type "int" np.find_common_type( np.int64, np.int64) # E: incompatible type "Type[signedinteger[Any]]"
import numpy as np #todo 实现两个矩阵相加 def native_add(x, y): assert len(x.shape) == 2 #todo 判断x是否为2D张量 assert x.shape == y.shape #todo 判断两个矩阵是否相等 x = x.copy() for i in range(x.shape[0]): for j in range(x.shape[1]): x[i, j] += y[i, j] return x num1 = np.array([[1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8]]) num2 = np.array([[2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]]) num3 = np.array([[2, 3, 4, 5], [3, 4, 5, 6]]) z = num1 + num2 z = np.maximum_sctype()(z, 0) print(z) #print(native_add((num1, num3))) #todo 根据矩阵相加的法则,那么当两个矩阵大小不同时,那么会出现错误 print(native_add(num1, num2))
""" A module containing unit tests for the `bitmask` modue. Licensed under a 3-clause BSD style license - see LICENSE.rst """ import warnings import numpy as np import pytest from astropy.nddata import bitmask MAX_INT_TYPE = np.maximum_sctype(np.int_) MAX_UINT_TYPE = np.maximum_sctype(np.uint) MAX_UINT_FLAG = np.left_shift(MAX_UINT_TYPE(1), MAX_UINT_TYPE(np.iinfo(MAX_UINT_TYPE).bits - 1)) MAX_INT_FLAG = np.left_shift(MAX_INT_TYPE(1), MAX_INT_TYPE(np.iinfo(MAX_INT_TYPE).bits - 2)) SUPER_LARGE_FLAG = 1 << np.iinfo(MAX_UINT_TYPE).bits EXTREME_TEST_DATA = np.array([ 0, 1, 1 + 1 << 2, MAX_INT_FLAG, ~0, MAX_INT_TYPE(MAX_UINT_FLAG), 1 + MAX_INT_TYPE(MAX_UINT_FLAG) ], dtype=MAX_INT_TYPE) @pytest.mark.parametrize('flag', [0, -1]) def test_nonpositive_not_a_bit_flag(flag): assert not bitmask._is_bit_flag(n=flag)
import numpy as np reveal_type(np.maximum_sctype(np.float64)) # E: Type[{float64}] reveal_type(np.maximum_sctype("f8")) # E: Type[Any] reveal_type(np.issctype(np.float64)) # E: bool reveal_type(np.issctype("foo")) # E: Literal[False] reveal_type(np.obj2sctype(np.float64)) # E: Union[None, Type[{float64}]] reveal_type(np.obj2sctype( np.float64, default=False)) # E: Union[builtins.bool, Type[{float64}]] reveal_type(np.obj2sctype("S8")) # E: Union[None, Type[Any]] reveal_type(np.obj2sctype("S8", default=None)) # E: Union[None, Type[Any]] reveal_type(np.obj2sctype("foo", default=False)) # E: Union[builtins.bool, Type[Any]] reveal_type(np.obj2sctype(1)) # E: None reveal_type(np.obj2sctype(1, default=False)) # E: bool reveal_type(np.issubclass_(np.float64, float)) # E: bool reveal_type(np.issubclass_(np.float64, (int, float))) # E: bool reveal_type(np.issubclass_(1, 1)) # E: Literal[False] reveal_type(np.sctype2char("S8")) # E: str reveal_type(np.sctype2char(list)) # E: str reveal_type(np.find_common_type([np.int64], [np.int64])) # E: numpy.dtype[Any] reveal_type(np.cast[int]) # E: _CastFunc reveal_type(np.cast["i8"]) # E: _CastFunc reveal_type(np.cast[np.int64]) # E: _CastFunc
def test_uint(self, t): assert_equal(np.maximum_sctype(t), np.sctypes['uint'][-1])
import numpy as np np.maximum_sctype("S8") np.maximum_sctype(object) np.issctype(object) np.issctype("S8") np.obj2sctype(list) np.obj2sctype(list, default=None) np.obj2sctype(list, default=np.string_) np.issubclass_(np.int32, int) np.issubclass_(np.float64, float) np.issubclass_(np.float64, (int, float)) np.issubsctype("int64", int) np.issubsctype(np.array([1]), np.array([1])) np.issubdtype("S1", np.string_) np.issubdtype(np.float64, np.float32) np.sctype2char("S1") np.sctype2char(list) np.find_common_type([], [np.int64, np.float32, complex]) np.find_common_type((), (np.int64, np.float32, complex)) np.find_common_type([np.int64, np.float32], []) np.find_common_type([np.float32], [np.int64, np.float64]) np.cast[int]
>>> M = quat2mat(q) # from this module >>> vec = np.array([1, 2, 3]).reshape((3,1)) # column vector >>> tvec = np.dot(M, vec) Terms used in function names: * *mat* : array shape (3, 3) (3D non-homogenous coordinates) * *aff* : affine array shape (4, 4) (3D homogenous coordinates) * *quat* : quaternion shape (4,) * *axangle* : rotations encoded by axis vector and angle scalar ''' import math import numpy as np _MAX_FLOAT = np.maximum_sctype(np.float) _FLOAT_EPS = np.finfo(np.float).eps def fillpositive(xyz, w2_thresh=None): ''' Compute unit quaternion from last 3 values Parameters ---------- xyz : iterable iterable containing 3 values, corresponding to quaternion x, y, z w2_thresh : None or float, optional threshold to determine if w squared is really negative. If None (default) then w2_thresh set equal to ``-np.finfo(xyz.dtype).eps``, if possible, otherwise ``-np.finfo(np.float).eps``
import numpy as np # Technically this works, but probably shouldn't. See # # https://github.com/numpy/numpy/issues/16366 # np.maximum_sctype(1) # E: No overload variant np.issubsctype(1, np.int64) # E: incompatible type np.issubdtype(1, np.int64) # E: incompatible type np.find_common_type(np.int64, np.int64) # E: incompatible type