The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.
Example 1
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
Example 2
def fillna(self, value): if not np.can_cast(value, self.dtype): raise TypeError("fill value must match dtype of series") return self.map_partitions(M.fillna, value, meta=self)
Example 3
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()
Example 4
def __call__(self, a, b, *args, **kwargs): "Execute the call behavior." # Get the data (da, db) = (getdata(a), getdata(b)) # Get the result with np.errstate(divide='ignore', invalid='ignore'): result = self.f(da, db, *args, **kwargs) # Get the mask as a combination of the source masks and invalid m = ~umath.isfinite(result) m |= getmask(a) m |= getmask(b) # Apply the domain domain = ufunc_domain.get(self.f, None) if domain is not None: m |= filled(domain(da, db), True) # Take care of the scalar case first if (not m.ndim): if m: return masked else: return result # When the mask is True, put back da if possible # any errors, just abort; impossible to guarantee masked values try: np.copyto(result, 0, casting='unsafe', where=m) # avoid using "*" since this may be overlaid masked_da = umath.multiply(m, da) # only add back if it can be cast safely if np.can_cast(masked_da.dtype, result.dtype, casting='safe'): result += masked_da except: pass # Transforms to a (subclass of) MaskedArray masked_result = result.view(get_masked_subclass(a, b)) masked_result._mask = m if isinstance(a, MaskedArray): masked_result._update_from(a) elif isinstance(b, MaskedArray): masked_result._update_from(b) return masked_result
Example 5
def _get_reduce_op(ops, dtype): for i in ops._ops: if numpy.can_cast(dtype.type, i[0][0]): return i raise TypeError("Type is mismatched. %s(...), %s" % (ops.name, dtype.type))
Example 6
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()
Example 7
def __call__(self, a, b, *args, **kwargs): "Execute the call behavior." # Get the data (da, db) = (getdata(a), getdata(b)) # Get the result with np.errstate(divide='ignore', invalid='ignore'): result = self.f(da, db, *args, **kwargs) # Get the mask as a combination of the source masks and invalid m = ~umath.isfinite(result) m |= getmask(a) m |= getmask(b) # Apply the domain domain = ufunc_domain.get(self.f, None) if domain is not None: m |= filled(domain(da, db), True) # Take care of the scalar case first if (not m.ndim): if m: return masked else: return result # When the mask is True, put back da if possible # any errors, just abort; impossible to guarantee masked values try: np.copyto(result, 0, casting='unsafe', where=m) # avoid using "*" since this may be overlaid masked_da = umath.multiply(m, da) # only add back if it can be cast safely if np.can_cast(masked_da.dtype, result.dtype, casting='safe'): result += masked_da except: pass # Transforms to a (subclass of) MaskedArray masked_result = result.view(get_masked_subclass(a, b)) masked_result._mask = m if isinstance(a, MaskedArray): masked_result._update_from(a) elif isinstance(b, MaskedArray): masked_result._update_from(b) return masked_result
Example 8
def copyto(dst, src, casting='same_kind', where=None): """Copies values from one array to another with broadcasting. This function can be called for arrays on different devices. In this case, casting, ``where``, and broadcasting is not supported, and an exception is raised if these are used. Args: dst (cupy.ndarray): Target array. src (cupy.ndarray): Source array. casting (str): Casting rule. See :func:`numpy.can_cast` for detail. where (cupy.ndarray of bool): If specified, this array acts as a mask, and an element is copied only if the corresponding element of ``where`` is True. .. seealso:: :func:`numpy.copyto` """ if not numpy.can_cast(src.dtype, dst.dtype, casting): raise TypeError('Cannot cast %s to %s in %s casting mode' % (src.dtype, dst.dtype, casting)) if dst.size == 0: return if where is None: if _can_memcpy(dst, src): dst.data.copy_from(src.data, src.nbytes) else: core.elementwise_copy(src, dst) else: core.elementwise_copy_where(src, where, dst)
Example 9
def __init__(self, name, *dimensions, data=None, kind=None): super(Field, self).__init__(name) self.__name = name self.dimensions = dimensions if kind is None: if data is None: self.kind = COMPLEX elif isinstance(data, (numpy.ndarray, numpy.number)): self.kind = DTYPE_KINDS[data.dtype.kind] elif isinstance(data, SCALAR_TYPES): self.kind = SCALAR_KINDS[type(data)] else: assert kind in (REAL, COMPLEX) self.kind = kind dtype = DEFAULT_DTYPES[self.kind] shape = tuple(d.grid.size for d in dimensions) if data is None: self.data = numpy.empty(shape, dtype) elif isinstance(data, SCALAR_TYPES): self.data = numpy.ones(shape, dtype) * numpy.array(data).astype(dtype) elif isinstance(data, numpy.ndarray): assert data.shape == shape assert numpy.can_cast(data.dtype, dtype) self.data = data.astype(dtype) else: raise ValueError
Example 10
def replace(self, to_replace, value, inplace=False, filter=None, regex=False, mgr=None): 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, mgr=mgr)
Example 11
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()
Example 12
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()
Example 13
def __call__(self, a, b, *args, **kwargs): "Execute the call behavior." # Get the data (da, db) = (getdata(a), getdata(b)) # Get the result with np.errstate(divide='ignore', invalid='ignore'): result = self.f(da, db, *args, **kwargs) # Get the mask as a combination of the source masks and invalid m = ~umath.isfinite(result) m |= getmask(a) m |= getmask(b) # Apply the domain domain = ufunc_domain.get(self.f, None) if domain is not None: m |= filled(domain(da, db), True) # Take care of the scalar case first if (not m.ndim): if m: return masked else: return result # When the mask is True, put back da if possible # any errors, just abort; impossible to guarantee masked values try: np.copyto(result, 0, casting='unsafe', where=m) # avoid using "*" since this may be overlaid masked_da = umath.multiply(m, da) # only add back if it can be cast safely if np.can_cast(masked_da.dtype, result.dtype, casting='safe'): result += masked_da except: pass # Transforms to a (subclass of) MaskedArray masked_result = result.view(get_masked_subclass(a, b)) masked_result._mask = m masked_result._update_from(result) return masked_result
Example 14
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()
Example 15
def __call__(self, a, b, *args, **kwargs): "Execute the call behavior." # Get the data (da, db) = (getdata(a), getdata(b)) # Get the result with np.errstate(divide='ignore', invalid='ignore'): result = self.f(da, db, *args, **kwargs) # Get the mask as a combination of the source masks and invalid m = ~umath.isfinite(result) m |= getmask(a) m |= getmask(b) # Apply the domain domain = ufunc_domain.get(self.f, None) if domain is not None: m |= filled(domain(da, db), True) # Take care of the scalar case first if (not m.ndim): if m: return masked else: return result # When the mask is True, put back da if possible # any errors, just abort; impossible to guarantee masked values try: np.copyto(result, 0, casting='unsafe', where=m) # avoid using "*" since this may be overlaid masked_da = umath.multiply(m, da) # only add back if it can be cast safely if np.can_cast(masked_da.dtype, result.dtype, casting='safe'): result += masked_da except: pass # Transforms to a (subclass of) MaskedArray masked_result = result.view(get_masked_subclass(a, b)) masked_result._mask = m if isinstance(a, MaskedArray): masked_result._update_from(a) elif isinstance(b, MaskedArray): masked_result._update_from(b) return masked_result
Example 16
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()
Example 17
def __call__(self, a, b, *args, **kwargs): "Execute the call behavior." # Get the data (da, db) = (getdata(a), getdata(b)) # Get the result with np.errstate(divide='ignore', invalid='ignore'): result = self.f(da, db, *args, **kwargs) # Get the mask as a combination of the source masks and invalid m = ~umath.isfinite(result) m |= getmask(a) m |= getmask(b) # Apply the domain domain = ufunc_domain.get(self.f, None) if domain is not None: m |= domain(da, db) # Take care of the scalar case first if (not m.ndim): if m: return masked else: return result # When the mask is True, put back da if possible # any errors, just abort; impossible to guarantee masked values try: np.copyto(result, 0, casting='unsafe', where=m) # avoid using "*" since this may be overlaid masked_da = umath.multiply(m, da) # only add back if it can be cast safely if np.can_cast(masked_da.dtype, result.dtype, casting='safe'): result += masked_da except: pass # Transforms to a (subclass of) MaskedArray masked_result = result.view(get_masked_subclass(a, b)) masked_result._mask = m if isinstance(a, MaskedArray): masked_result._update_from(a) elif isinstance(b, MaskedArray): masked_result._update_from(b) return masked_result
Example 18
def perform(self, node, inp, out_): x, i = inp out, = out_ # Copy always implied by numpy advanced indexing semantic. if out[0] is not None and out[0].shape == (len(i),) + x.shape[1:]: o = out[0] else: o = None # If i.dtype is more precise than numpy.intp (int32 on 32-bit machines, # int64 on 64-bit machines), numpy may raise the following error: # TypeError: array cannot be safely cast to required type. # We need to check if values in i can fit in numpy.intp, because # if they don't, that should be an error (no array can have that # many elements on a 32-bit arch). if i.dtype != numpy.intp: i_ = theano._asarray(i, dtype=numpy.intp) if not numpy.can_cast(i.dtype, numpy.intp): # Check if there was actually an incorrect conversion if numpy.any(i != i_): raise IndexError( 'index contains values that are bigger ' 'than the maximum array size on this system.', i) i = i_ out[0] = x.take(i, axis=0, out=o)
Example 19
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()
Example 20
def __call__(self, a, b, *args, **kwargs): "Execute the call behavior." # Get the data (da, db) = (getdata(a), getdata(b)) # Get the result with np.errstate(divide='ignore', invalid='ignore'): result = self.f(da, db, *args, **kwargs) # Get the mask as a combination of the source masks and invalid m = ~umath.isfinite(result) m |= getmask(a) m |= getmask(b) # Apply the domain domain = ufunc_domain.get(self.f, None) if domain is not None: m |= filled(domain(da, db), True) # Take care of the scalar case first if (not m.ndim): if m: return masked else: return result # When the mask is True, put back da if possible # any errors, just abort; impossible to guarantee masked values try: np.copyto(result, 0, casting='unsafe', where=m) # avoid using "*" since this may be overlaid masked_da = umath.multiply(m, da) # only add back if it can be cast safely if np.can_cast(masked_da.dtype, result.dtype, casting='safe'): result += masked_da except: pass # Transforms to a (subclass of) MaskedArray masked_result = result.view(get_masked_subclass(a, b)) masked_result._mask = m if isinstance(a, MaskedArray): masked_result._update_from(a) elif isinstance(b, MaskedArray): masked_result._update_from(b) return masked_result
Example 21
def require_dataset(self, name, shape, dtype, exact=False, **kwds): """ Open a dataset, creating it if it doesn't exist. If keyword "exact" is False (default), an existing dataset must have the same shape and a conversion-compatible dtype to be returned. If True, the shape and dtype must match exactly. Other dataset keywords (see create_dataset) may be provided, but are only used if a new dataset is to be created. Raises TypeError if an incompatible object already exists, or if the shape or dtype don't match according to the above rules. """ with phil: if not name in self: return self.create_dataset(name, *(shape, dtype), **kwds) dset = self[name] if not isinstance(dset, Dataset): raise TypeError("Incompatible object (%s) already exists" % dset.__class__.__name__) if not shape == dset.shape: raise TypeError("Shapes do not match (existing %s vs new %s)" % (dset.shape, shape)) if exact: if not dtype == dset.dtype: raise TypeError("Datatypes do not exactly match (existing %s vs new %s)" % (dset.dtype, dtype)) elif not numpy.can_cast(dtype, dset.dtype): raise TypeError("Datatypes cannot be safely cast (existing %s vs new %s)" % (dset.dtype, dtype)) return dset
Example 22
def test_casting(self): # Check that casting a structured array to change its byte order # works a = np.array([(1,)], dtype=[('a', '<i4')]) assert_(np.can_cast(a.dtype, [('a', '>i4')], casting='unsafe')) b = a.astype([('a', '>i4')]) assert_equal(b, a.byteswap().newbyteorder()) assert_equal(a['a'][0], b['a'][0]) # Check that equality comparison works on structured arrays if # they are 'equiv'-castable a = np.array([(5, 42), (10, 1)], dtype=[('a', '>i4'), ('b', '<f8')]) b = np.array([(42, 5), (1, 10)], dtype=[('b', '>f8'), ('a', '<i4')]) assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) assert_equal(a == b, [True, True]) # Check that 'equiv' casting can reorder fields and change byte # order assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) c = a.astype(b.dtype, casting='equiv') assert_equal(a == c, [True, True]) # Check that 'safe' casting can change byte order and up-cast # fields t = [('a', '<i8'), ('b', '>f8')] assert_(np.can_cast(a.dtype, t, casting='safe')) c = a.astype(t, casting='safe') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that 'same_kind' casting can change byte order and # change field widths within a "kind" t = [('a', '<i4'), ('b', '>f4')] assert_(np.can_cast(a.dtype, t, casting='same_kind')) c = a.astype(t, casting='same_kind') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that casting fails if the casting rule should fail on # any of the fields t = [('a', '>i8'), ('b', '<f4')] assert_(not np.can_cast(a.dtype, t, casting='safe')) assert_raises(TypeError, a.astype, t, casting='safe') t = [('a', '>i2'), ('b', '<f8')] assert_(not np.can_cast(a.dtype, t, casting='equiv')) assert_raises(TypeError, a.astype, t, casting='equiv') t = [('a', '>i8'), ('b', '<i2')] assert_(not np.can_cast(a.dtype, t, casting='same_kind')) assert_raises(TypeError, a.astype, t, casting='same_kind') assert_(not np.can_cast(a.dtype, b.dtype, casting='no')) assert_raises(TypeError, a.astype, b.dtype, casting='no') # Check that non-'unsafe' casting can't change the set of field names for casting in ['no', 'safe', 'equiv', 'same_kind']: t = [('a', '>i4')] assert_(not np.can_cast(a.dtype, t, casting=casting)) t = [('a', '>i4'), ('b', '<f8'), ('c', 'i4')] assert_(not np.can_cast(a.dtype, t, casting=casting))
Example 23
def __call__(self, a, b, *args, **kwargs): """ Execute the call behavior. """ # Get the data, as ndarray (da, db) = (getdata(a), getdata(b)) # Get the result with np.errstate(): np.seterr(divide='ignore', invalid='ignore') result = self.f(da, db, *args, **kwargs) # Get the mask for the result (ma, mb) = (getmask(a), getmask(b)) if ma is nomask: if mb is nomask: m = nomask else: m = umath.logical_or(getmaskarray(a), mb) elif mb is nomask: m = umath.logical_or(ma, getmaskarray(b)) else: m = umath.logical_or(ma, mb) # Case 1. : scalar if not result.ndim: if m: return masked return result # Case 2. : array # Revert result to da where masked if m is not nomask and m.any(): # any errors, just abort; impossible to guarantee masked values try: np.copyto(result, 0, casting='unsafe', where=m) # avoid using "*" since this may be overlaid masked_da = umath.multiply(m, da) # only add back if it can be cast safely if np.can_cast(masked_da.dtype, result.dtype, casting='safe'): result += masked_da except: pass # Transforms to a (subclass of) MaskedArray masked_result = result.view(get_masked_subclass(a, b)) masked_result._mask = m if isinstance(a, MaskedArray): masked_result._update_from(a) elif isinstance(b, MaskedArray): masked_result._update_from(b) return masked_result
Example 24
def copyto(dst, src, casting='same_kind', where=None): """Copies values from one array to another with broadcasting. This function can be called for arrays on different devices. In this case, casting, ``where``, and broadcasting is not supported, and an exception is raised if these are used. Args: dst (cupy.ndarray): Target array. src (cupy.ndarray): Source array. casting (str): Casting rule. See :func:`numpy.can_cast` for detail. where (cupy.ndarray of bool): If specified, this array acts as a mask, and an element is copied only if the corresponding element of ``where`` is True. .. seealso:: :func:`numpy.copyto` """ src_type = type(src) src_is_python_scalar = (src_type in six.integer_types or src_type in (bool, float, complex)) if src_is_python_scalar: src_dtype = numpy.dtype(type(src)) can_cast = numpy.can_cast(src, dst.dtype, casting) else: src_dtype = src.dtype can_cast = numpy.can_cast(src_dtype, dst.dtype, casting) if not can_cast: raise TypeError('Cannot cast %s to %s in %s casting mode' % (src_dtype, dst.dtype, casting)) if dst.size == 0: return if src_is_python_scalar: dst.fill(src) return if where is None: if _can_memcpy(dst, src): dst.data.copy_from(src.data, src.nbytes) else: device = dst.device with device: if src.device != device: src = src.copy() core.elementwise_copy(src, dst) else: core.elementwise_copy_where(src, where, dst)
Example 25
def set_dtype(self, value, check=True): """Change data type of the bin contents. Allowed conversions: - from integral to float types - between the same category of type (float/integer) - from float types to integer if weights are trivial Parameters ---------- value: np.dtype or something convertible to it. check: bool If True (default), all values are checked against the limits """ # TODO: Refactor out? # TODO? Deal with unsigned types value = np.dtype(value) if value == self.dtype: return # No change if value.kind in "iu": type_info = np.iinfo(value) elif value.kind == "f": type_info = np.finfo(value) else: raise RuntimeError("Unsupported dtype. Only integer/floating-point types are supported.") if np.can_cast(self.dtype, value): pass # Ok elif check: if np.issubdtype(value, np.integer): if self.dtype.kind == "f": for array in (self._frequencies, self._errors2): if np.any(array % 1.0): raise RuntimeError("Data contain non-integer values.") for array in (self._frequencies, self._errors2): if np.any((array > type_info.max) | (array < type_info.min)): raise RuntimeError("Data contain values outside the specified range.") self._dtype = value self._frequencies = self._frequencies.astype(value) self._errors2 = self._errors2.astype(value) self._missed = self._missed.astype(value)
Example 26
def test_casting(self): # Check that casting a structured array to change its byte order # works a = np.array([(1,)], dtype=[('a', '<i4')]) assert_(np.can_cast(a.dtype, [('a', '>i4')], casting='unsafe')) b = a.astype([('a', '>i4')]) assert_equal(b, a.byteswap().newbyteorder()) assert_equal(a['a'][0], b['a'][0]) # Check that equality comparison works on structured arrays if # they are 'equiv'-castable a = np.array([(5, 42), (10, 1)], dtype=[('a', '>i4'), ('b', '<f8')]) b = np.array([(42, 5), (1, 10)], dtype=[('b', '>f8'), ('a', '<i4')]) assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) assert_equal(a == b, [True, True]) # Check that 'equiv' casting can reorder fields and change byte # order assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) c = a.astype(b.dtype, casting='equiv') assert_equal(a == c, [True, True]) # Check that 'safe' casting can change byte order and up-cast # fields t = [('a', '<i8'), ('b', '>f8')] assert_(np.can_cast(a.dtype, t, casting='safe')) c = a.astype(t, casting='safe') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that 'same_kind' casting can change byte order and # change field widths within a "kind" t = [('a', '<i4'), ('b', '>f4')] assert_(np.can_cast(a.dtype, t, casting='same_kind')) c = a.astype(t, casting='same_kind') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that casting fails if the casting rule should fail on # any of the fields t = [('a', '>i8'), ('b', '<f4')] assert_(not np.can_cast(a.dtype, t, casting='safe')) assert_raises(TypeError, a.astype, t, casting='safe') t = [('a', '>i2'), ('b', '<f8')] assert_(not np.can_cast(a.dtype, t, casting='equiv')) assert_raises(TypeError, a.astype, t, casting='equiv') t = [('a', '>i8'), ('b', '<i2')] assert_(not np.can_cast(a.dtype, t, casting='same_kind')) assert_raises(TypeError, a.astype, t, casting='same_kind') assert_(not np.can_cast(a.dtype, b.dtype, casting='no')) assert_raises(TypeError, a.astype, b.dtype, casting='no') # Check that non-'unsafe' casting can't change the set of field names for casting in ['no', 'safe', 'equiv', 'same_kind']: t = [('a', '>i4')] assert_(not np.can_cast(a.dtype, t, casting=casting)) t = [('a', '>i4'), ('b', '<f8'), ('c', 'i4')] assert_(not np.can_cast(a.dtype, t, casting=casting))
Example 27
def __call__(self, a, b, *args, **kwargs): """ Execute the call behavior. """ # Get the data, as ndarray (da, db) = (getdata(a), getdata(b)) # Get the result with np.errstate(): np.seterr(divide='ignore', invalid='ignore') result = self.f(da, db, *args, **kwargs) # Get the mask for the result (ma, mb) = (getmask(a), getmask(b)) if ma is nomask: if mb is nomask: m = nomask else: m = umath.logical_or(getmaskarray(a), mb) elif mb is nomask: m = umath.logical_or(ma, getmaskarray(b)) else: m = umath.logical_or(ma, mb) # Case 1. : scalar if not result.ndim: if m: return masked return result # Case 2. : array # Revert result to da where masked if m is not nomask and m.any(): # any errors, just abort; impossible to guarantee masked values try: np.copyto(result, 0, casting='unsafe', where=m) # avoid using "*" since this may be overlaid masked_da = umath.multiply(m, da) # only add back if it can be cast safely if np.can_cast(masked_da.dtype, result.dtype, casting='safe'): result += masked_da except: pass # Transforms to a (subclass of) MaskedArray masked_result = result.view(get_masked_subclass(a, b)) masked_result._mask = m if isinstance(a, MaskedArray): masked_result._update_from(a) elif isinstance(b, MaskedArray): masked_result._update_from(b) return masked_result
Example 28
def __init__(self, value, shape=None, dtype=None): """ Create a new lazy array. `value` : may be an int, long, float, bool, NumPy array, iterator, generator or a function, `f(i)` or `f(i,j)`, depending on the dimensions of the array. `f(i,j)` should return a single number when `i` and `j` are integers, and a 1D array when either `i` or `j` or both is a NumPy array (in the latter case the two arrays must have equal lengths). """ self.dtype = dtype self.operations = [] if isinstance(value, basestring): raise TypeError("An larray cannot be created from a string") elif isinstance(value, larray): if shape is not None and value.shape is not None: assert shape == value.shape self._shape = shape or value.shape self.base_value = value.base_value self.dtype = dtype or value.dtype self.operations = value.operations # should deepcopy? elif isinstance(value, collections.Sized): # False for numbers, generators, functions, iterators if have_scipy and sparse.issparse(value): # For sparse matrices self.dtype = dtype or value.dtype elif not isinstance(value, numpy.ndarray): value = numpy.array(value, dtype=dtype) elif dtype is not None: assert numpy.can_cast(value.dtype, dtype, casting='safe') # or could convert value to the provided dtype if shape and value.shape != shape: raise ValueError("Array has shape %s, value has shape %s" % (shape, value.shape)) self._shape = value.shape self.base_value = value else: assert numpy.isreal(value) # also True for callables, generators, iterators self._shape = shape if dtype is None or isinstance(value, dtype): self.base_value = value else: try: self.base_value = dtype(value) except TypeError: self.base_value = value
Example 29
def test_casting(self): # Check that casting a structured array to change its byte order # works a = np.array([(1,)], dtype=[('a', '<i4')]) assert_(np.can_cast(a.dtype, [('a', '>i4')], casting='unsafe')) b = a.astype([('a', '>i4')]) assert_equal(b, a.byteswap().newbyteorder()) assert_equal(a['a'][0], b['a'][0]) # Check that equality comparison works on structured arrays if # they are 'equiv'-castable a = np.array([(5, 42), (10, 1)], dtype=[('a', '>i4'), ('b', '<f8')]) b = np.array([(42, 5), (1, 10)], dtype=[('b', '>f8'), ('a', '<i4')]) assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) assert_equal(a == b, [True, True]) # Check that 'equiv' casting can reorder fields and change byte # order assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) c = a.astype(b.dtype, casting='equiv') assert_equal(a == c, [True, True]) # Check that 'safe' casting can change byte order and up-cast # fields t = [('a', '<i8'), ('b', '>f8')] assert_(np.can_cast(a.dtype, t, casting='safe')) c = a.astype(t, casting='safe') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that 'same_kind' casting can change byte order and # change field widths within a "kind" t = [('a', '<i4'), ('b', '>f4')] assert_(np.can_cast(a.dtype, t, casting='same_kind')) c = a.astype(t, casting='same_kind') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that casting fails if the casting rule should fail on # any of the fields t = [('a', '>i8'), ('b', '<f4')] assert_(not np.can_cast(a.dtype, t, casting='safe')) assert_raises(TypeError, a.astype, t, casting='safe') t = [('a', '>i2'), ('b', '<f8')] assert_(not np.can_cast(a.dtype, t, casting='equiv')) assert_raises(TypeError, a.astype, t, casting='equiv') t = [('a', '>i8'), ('b', '<i2')] assert_(not np.can_cast(a.dtype, t, casting='same_kind')) assert_raises(TypeError, a.astype, t, casting='same_kind') assert_(not np.can_cast(a.dtype, b.dtype, casting='no')) assert_raises(TypeError, a.astype, b.dtype, casting='no') # Check that non-'unsafe' casting can't change the set of field names for casting in ['no', 'safe', 'equiv', 'same_kind']: t = [('a', '>i4')] assert_(not np.can_cast(a.dtype, t, casting=casting)) t = [('a', '>i4'), ('b', '<f8'), ('c', 'i4')] assert_(not np.can_cast(a.dtype, t, casting=casting))
Example 30
def test_datetime_casting_rules(self): # Cannot cast safely/same_kind between timedelta and datetime assert_(not np.can_cast('m8', 'M8', casting='same_kind')) assert_(not np.can_cast('M8', 'm8', casting='same_kind')) assert_(not np.can_cast('m8', 'M8', casting='safe')) assert_(not np.can_cast('M8', 'm8', casting='safe')) # Can cast safely/same_kind from integer to timedelta assert_(np.can_cast('i8', 'm8', casting='same_kind')) assert_(np.can_cast('i8', 'm8', casting='safe')) # Cannot cast safely/same_kind from float to timedelta assert_(not np.can_cast('f4', 'm8', casting='same_kind')) assert_(not np.can_cast('f4', 'm8', casting='safe')) # Cannot cast safely/same_kind from integer to datetime assert_(not np.can_cast('i8', 'M8', casting='same_kind')) assert_(not np.can_cast('i8', 'M8', casting='safe')) # Cannot cast safely/same_kind from bool to datetime assert_(not np.can_cast('b1', 'M8', casting='same_kind')) assert_(not np.can_cast('b1', 'M8', casting='safe')) # Can cast safely/same_kind from bool to timedelta assert_(np.can_cast('b1', 'm8', casting='same_kind')) assert_(np.can_cast('b1', 'm8', casting='safe')) # Can cast datetime safely from months/years to days assert_(np.can_cast('M8[M]', 'M8[D]', casting='safe')) assert_(np.can_cast('M8[Y]', 'M8[D]', casting='safe')) # Cannot cast timedelta safely from months/years to days assert_(not np.can_cast('m8[M]', 'm8[D]', casting='safe')) assert_(not np.can_cast('m8[Y]', 'm8[D]', casting='safe')) # Can cast datetime same_kind from months/years to days assert_(np.can_cast('M8[M]', 'M8[D]', casting='same_kind')) assert_(np.can_cast('M8[Y]', 'M8[D]', casting='same_kind')) # Can't cast timedelta same_kind from months/years to days assert_(not np.can_cast('m8[M]', 'm8[D]', casting='same_kind')) assert_(not np.can_cast('m8[Y]', 'm8[D]', casting='same_kind')) # Can't cast datetime same_kind across the date/time boundary assert_(not np.can_cast('M8[D]', 'M8[h]', casting='same_kind')) assert_(not np.can_cast('M8[h]', 'M8[D]', casting='same_kind')) # Can cast timedelta same_kind across the date/time boundary assert_(np.can_cast('m8[D]', 'm8[h]', casting='same_kind')) assert_(np.can_cast('m8[h]', 'm8[D]', casting='same_kind')) # Cannot cast safely if the integer multiplier doesn't divide assert_(not np.can_cast('M8[7h]', 'M8[3h]', casting='safe')) assert_(not np.can_cast('M8[3h]', 'M8[6h]', casting='safe')) # But can cast same_kind assert_(np.can_cast('M8[7h]', 'M8[3h]', casting='same_kind')) # Can cast safely if the integer multiplier does divide assert_(np.can_cast('M8[6h]', 'M8[3h]', casting='safe'))
Example 31
def __call__(self, a, b, *args, **kwargs): """ Execute the call behavior. """ # Get the data, as ndarray (da, db) = (getdata(a), getdata(b)) # Get the result with np.errstate(): np.seterr(divide='ignore', invalid='ignore') result = self.f(da, db, *args, **kwargs) # Get the mask for the result (ma, mb) = (getmask(a), getmask(b)) if ma is nomask: if mb is nomask: m = nomask else: m = umath.logical_or(getmaskarray(a), mb) elif mb is nomask: m = umath.logical_or(ma, getmaskarray(b)) else: m = umath.logical_or(ma, mb) # Case 1. : scalar if not result.ndim: if m: return masked return result # Case 2. : array # Revert result to da where masked if m is not nomask and m.any(): # any errors, just abort; impossible to guarantee masked values try: np.copyto(result, 0, casting='unsafe', where=m) # avoid using "*" since this may be overlaid masked_da = umath.multiply(m, da) # only add back if it can be cast safely if np.can_cast(masked_da.dtype, result.dtype, casting='safe'): result += masked_da except: pass # Transforms to a (subclass of) MaskedArray masked_result = result.view(get_masked_subclass(a, b)) masked_result._mask = m masked_result._update_from(result) return masked_result
Example 32
def test_casting(self): # Check that casting a structured array to change its byte order # works a = np.array([(1,)], dtype=[('a', '<i4')]) assert_(np.can_cast(a.dtype, [('a', '>i4')], casting='unsafe')) b = a.astype([('a', '>i4')]) assert_equal(b, a.byteswap().newbyteorder()) assert_equal(a['a'][0], b['a'][0]) # Check that equality comparison works on structured arrays if # they are 'equiv'-castable a = np.array([(5, 42), (10, 1)], dtype=[('a', '>i4'), ('b', '<f8')]) b = np.array([(42, 5), (1, 10)], dtype=[('b', '>f8'), ('a', '<i4')]) assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) assert_equal(a == b, [True, True]) # Check that 'equiv' casting can reorder fields and change byte # order assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) c = a.astype(b.dtype, casting='equiv') assert_equal(a == c, [True, True]) # Check that 'safe' casting can change byte order and up-cast # fields t = [('a', '<i8'), ('b', '>f8')] assert_(np.can_cast(a.dtype, t, casting='safe')) c = a.astype(t, casting='safe') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that 'same_kind' casting can change byte order and # change field widths within a "kind" t = [('a', '<i4'), ('b', '>f4')] assert_(np.can_cast(a.dtype, t, casting='same_kind')) c = a.astype(t, casting='same_kind') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that casting fails if the casting rule should fail on # any of the fields t = [('a', '>i8'), ('b', '<f4')] assert_(not np.can_cast(a.dtype, t, casting='safe')) assert_raises(TypeError, a.astype, t, casting='safe') t = [('a', '>i2'), ('b', '<f8')] assert_(not np.can_cast(a.dtype, t, casting='equiv')) assert_raises(TypeError, a.astype, t, casting='equiv') t = [('a', '>i8'), ('b', '<i2')] assert_(not np.can_cast(a.dtype, t, casting='same_kind')) assert_raises(TypeError, a.astype, t, casting='same_kind') assert_(not np.can_cast(a.dtype, b.dtype, casting='no')) assert_raises(TypeError, a.astype, b.dtype, casting='no') # Check that non-'unsafe' casting can't change the set of field names for casting in ['no', 'safe', 'equiv', 'same_kind']: t = [('a', '>i4')] assert_(not np.can_cast(a.dtype, t, casting=casting)) t = [('a', '>i4'), ('b', '<f8'), ('c', 'i4')] assert_(not np.can_cast(a.dtype, t, casting=casting))
Example 33
def test_datetime_casting_rules(self): # Cannot cast safely/same_kind between timedelta and datetime assert_(not np.can_cast('m8', 'M8', casting='same_kind')) assert_(not np.can_cast('M8', 'm8', casting='same_kind')) assert_(not np.can_cast('m8', 'M8', casting='safe')) assert_(not np.can_cast('M8', 'm8', casting='safe')) # Can cast safely/same_kind from integer to timedelta assert_(np.can_cast('i8', 'm8', casting='same_kind')) assert_(np.can_cast('i8', 'm8', casting='safe')) # Cannot cast safely/same_kind from float to timedelta assert_(not np.can_cast('f4', 'm8', casting='same_kind')) assert_(not np.can_cast('f4', 'm8', casting='safe')) # Cannot cast safely/same_kind from integer to datetime assert_(not np.can_cast('i8', 'M8', casting='same_kind')) assert_(not np.can_cast('i8', 'M8', casting='safe')) # Cannot cast safely/same_kind from bool to datetime assert_(not np.can_cast('b1', 'M8', casting='same_kind')) assert_(not np.can_cast('b1', 'M8', casting='safe')) # Can cast safely/same_kind from bool to timedelta assert_(np.can_cast('b1', 'm8', casting='same_kind')) assert_(np.can_cast('b1', 'm8', casting='safe')) # Can cast datetime safely from months/years to days assert_(np.can_cast('M8[M]', 'M8[D]', casting='safe')) assert_(np.can_cast('M8[Y]', 'M8[D]', casting='safe')) # Cannot cast timedelta safely from months/years to days assert_(not np.can_cast('m8[M]', 'm8[D]', casting='safe')) assert_(not np.can_cast('m8[Y]', 'm8[D]', casting='safe')) # Can cast datetime same_kind from months/years to days assert_(np.can_cast('M8[M]', 'M8[D]', casting='same_kind')) assert_(np.can_cast('M8[Y]', 'M8[D]', casting='same_kind')) # Can't cast timedelta same_kind from months/years to days assert_(not np.can_cast('m8[M]', 'm8[D]', casting='same_kind')) assert_(not np.can_cast('m8[Y]', 'm8[D]', casting='same_kind')) # Can't cast datetime same_kind across the date/time boundary assert_(not np.can_cast('M8[D]', 'M8[h]', casting='same_kind')) assert_(not np.can_cast('M8[h]', 'M8[D]', casting='same_kind')) # Can cast timedelta same_kind across the date/time boundary assert_(np.can_cast('m8[D]', 'm8[h]', casting='same_kind')) assert_(np.can_cast('m8[h]', 'm8[D]', casting='same_kind')) # Cannot cast safely if the integer multiplier doesn't divide assert_(not np.can_cast('M8[7h]', 'M8[3h]', casting='safe')) assert_(not np.can_cast('M8[3h]', 'M8[6h]', casting='safe')) # But can cast same_kind assert_(np.can_cast('M8[7h]', 'M8[3h]', casting='same_kind')) # Can cast safely if the integer multiplier does divide assert_(np.can_cast('M8[6h]', 'M8[3h]', casting='safe'))
Example 34
def __call__(self, a, b, *args, **kwargs): """ Execute the call behavior. """ # Get the data, as ndarray (da, db) = (getdata(a), getdata(b)) # Get the result with np.errstate(): np.seterr(divide='ignore', invalid='ignore') result = self.f(da, db, *args, **kwargs) # Get the mask for the result (ma, mb) = (getmask(a), getmask(b)) if ma is nomask: if mb is nomask: m = nomask else: m = umath.logical_or(getmaskarray(a), mb) elif mb is nomask: m = umath.logical_or(ma, getmaskarray(b)) else: m = umath.logical_or(ma, mb) # Case 1. : scalar if not result.ndim: if m: return masked return result # Case 2. : array # Revert result to da where masked if m is not nomask and m.any(): # any errors, just abort; impossible to guarantee masked values try: np.copyto(result, 0, casting='unsafe', where=m) # avoid using "*" since this may be overlaid masked_da = umath.multiply(m, da) # only add back if it can be cast safely if np.can_cast(masked_da.dtype, result.dtype, casting='safe'): result += masked_da except: pass # Transforms to a (subclass of) MaskedArray masked_result = result.view(get_masked_subclass(a, b)) masked_result._mask = m masked_result._update_from(result) return masked_result
Example 35
def test_casting(self): # Check that casting a structured array to change its byte order # works a = np.array([(1,)], dtype=[('a', '<i4')]) assert_(np.can_cast(a.dtype, [('a', '>i4')], casting='unsafe')) b = a.astype([('a', '>i4')]) assert_equal(b, a.byteswap().newbyteorder()) assert_equal(a['a'][0], b['a'][0]) # Check that equality comparison works on structured arrays if # they are 'equiv'-castable a = np.array([(5, 42), (10, 1)], dtype=[('a', '>i4'), ('b', '<f8')]) b = np.array([(42, 5), (1, 10)], dtype=[('b', '>f8'), ('a', '<i4')]) assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) assert_equal(a == b, [True, True]) # Check that 'equiv' casting can reorder fields and change byte # order assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) c = a.astype(b.dtype, casting='equiv') assert_equal(a == c, [True, True]) # Check that 'safe' casting can change byte order and up-cast # fields t = [('a', '<i8'), ('b', '>f8')] assert_(np.can_cast(a.dtype, t, casting='safe')) c = a.astype(t, casting='safe') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that 'same_kind' casting can change byte order and # change field widths within a "kind" t = [('a', '<i4'), ('b', '>f4')] assert_(np.can_cast(a.dtype, t, casting='same_kind')) c = a.astype(t, casting='same_kind') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that casting fails if the casting rule should fail on # any of the fields t = [('a', '>i8'), ('b', '<f4')] assert_(not np.can_cast(a.dtype, t, casting='safe')) assert_raises(TypeError, a.astype, t, casting='safe') t = [('a', '>i2'), ('b', '<f8')] assert_(not np.can_cast(a.dtype, t, casting='equiv')) assert_raises(TypeError, a.astype, t, casting='equiv') t = [('a', '>i8'), ('b', '<i2')] assert_(not np.can_cast(a.dtype, t, casting='same_kind')) assert_raises(TypeError, a.astype, t, casting='same_kind') assert_(not np.can_cast(a.dtype, b.dtype, casting='no')) assert_raises(TypeError, a.astype, b.dtype, casting='no') # Check that non-'unsafe' casting can't change the set of field names for casting in ['no', 'safe', 'equiv', 'same_kind']: t = [('a', '>i4')] assert_(not np.can_cast(a.dtype, t, casting=casting)) t = [('a', '>i4'), ('b', '<f8'), ('c', 'i4')] assert_(not np.can_cast(a.dtype, t, casting=casting))
Example 36
def __call__(self, a, b, *args, **kwargs): """ Execute the call behavior. """ # Get the data, as ndarray (da, db) = (getdata(a), getdata(b)) # Get the result with np.errstate(): np.seterr(divide='ignore', invalid='ignore') result = self.f(da, db, *args, **kwargs) # Get the mask for the result (ma, mb) = (getmask(a), getmask(b)) if ma is nomask: if mb is nomask: m = nomask else: m = umath.logical_or(getmaskarray(a), mb) elif mb is nomask: m = umath.logical_or(ma, getmaskarray(b)) else: m = umath.logical_or(ma, mb) # Case 1. : scalar if not result.ndim: if m: return masked return result # Case 2. : array # Revert result to da where masked if m is not nomask and m.any(): # any errors, just abort; impossible to guarantee masked values try: np.copyto(result, 0, casting='unsafe', where=m) # avoid using "*" since this may be overlaid masked_da = umath.multiply(m, da) # only add back if it can be cast safely if np.can_cast(masked_da.dtype, result.dtype, casting='safe'): result += masked_da except: pass # Transforms to a (subclass of) MaskedArray masked_result = result.view(get_masked_subclass(a, b)) masked_result._mask = m if isinstance(a, MaskedArray): masked_result._update_from(a) elif isinstance(b, MaskedArray): masked_result._update_from(b) return masked_result
Example 37
def test_casting(self): # Check that casting a structured array to change its byte order # works a = np.array([(1,)], dtype=[('a', '<i4')]) assert_(np.can_cast(a.dtype, [('a', '>i4')], casting='unsafe')) b = a.astype([('a', '>i4')]) assert_equal(b, a.byteswap().newbyteorder()) assert_equal(a['a'][0], b['a'][0]) # Check that equality comparison works on structured arrays if # they are 'equiv'-castable a = np.array([(5, 42), (10, 1)], dtype=[('a', '>i4'), ('b', '<f8')]) b = np.array([(42, 5), (1, 10)], dtype=[('b', '>f8'), ('a', '<i4')]) assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) assert_equal(a == b, [True, True]) # Check that 'equiv' casting can reorder fields and change byte # order # New in 1.12: This behavior changes in 1.13, test for dep warning assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) with assert_warns(FutureWarning): c = a.astype(b.dtype, casting='equiv') assert_equal(a == c, [True, True]) # Check that 'safe' casting can change byte order and up-cast # fields t = [('a', '<i8'), ('b', '>f8')] assert_(np.can_cast(a.dtype, t, casting='safe')) c = a.astype(t, casting='safe') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that 'same_kind' casting can change byte order and # change field widths within a "kind" t = [('a', '<i4'), ('b', '>f4')] assert_(np.can_cast(a.dtype, t, casting='same_kind')) c = a.astype(t, casting='same_kind') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that casting fails if the casting rule should fail on # any of the fields t = [('a', '>i8'), ('b', '<f4')] assert_(not np.can_cast(a.dtype, t, casting='safe')) assert_raises(TypeError, a.astype, t, casting='safe') t = [('a', '>i2'), ('b', '<f8')] assert_(not np.can_cast(a.dtype, t, casting='equiv')) assert_raises(TypeError, a.astype, t, casting='equiv') t = [('a', '>i8'), ('b', '<i2')] assert_(not np.can_cast(a.dtype, t, casting='same_kind')) assert_raises(TypeError, a.astype, t, casting='same_kind') assert_(not np.can_cast(a.dtype, b.dtype, casting='no')) assert_raises(TypeError, a.astype, b.dtype, casting='no') # Check that non-'unsafe' casting can't change the set of field names for casting in ['no', 'safe', 'equiv', 'same_kind']: t = [('a', '>i4')] assert_(not np.can_cast(a.dtype, t, casting=casting)) t = [('a', '>i4'), ('b', '<f8'), ('c', 'i4')] assert_(not np.can_cast(a.dtype, t, casting=casting))
Example 38
def require_dataset(self, name, shape=None, dtype=None, exact=False, data=None, fillvalue=None): if name not in self: return self.create_dataset( name, shape=shape, dtype=dtype, data=data, fillvalue=fillvalue ) current_object = self[name] if not isinstance(current_object, ds.Dataset): raise TypeError( "Incompatible object already exists: {}".format( current_object.__class__.__name__ ) ) data, attrs, meta = ds._prepare_write(data, self.plugin_manager.dataset_plugins.write_order) # TODO verify proper attributes _assert_data_shape_dtype_match(data, shape, dtype) shape, dtype = _data_to_shape_and_dtype(data, shape, dtype) if not np.array_equal(shape, current_object.shape): raise TypeError( "Shapes do not match (existing {} vs " "new {})".format(current_object.shape, shape) ) if dtype != current_object.dtype: if exact: raise TypeError( "Datatypes do not exactly match " "existing {} vs new {})".format(current_object.dtype, dtype) ) if not np.can_cast(dtype, current_object.dtype): raise TypeError( "Cannot safely cast from {} to {}".format( dtype, current_object.dtype ) ) return current_object
Example 39
def test_casting(self): # Check that casting a structured array to change its byte order # works a = np.array([(1,)], dtype=[('a', '<i4')]) assert_(np.can_cast(a.dtype, [('a', '>i4')], casting='unsafe')) b = a.astype([('a', '>i4')]) assert_equal(b, a.byteswap().newbyteorder()) assert_equal(a['a'][0], b['a'][0]) # Check that equality comparison works on structured arrays if # they are 'equiv'-castable a = np.array([(5, 42), (10, 1)], dtype=[('a', '>i4'), ('b', '<f8')]) b = np.array([(42, 5), (1, 10)], dtype=[('b', '>f8'), ('a', '<i4')]) assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) assert_equal(a == b, [True, True]) # Check that 'equiv' casting can reorder fields and change byte # order assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) c = a.astype(b.dtype, casting='equiv') assert_equal(a == c, [True, True]) # Check that 'safe' casting can change byte order and up-cast # fields t = [('a', '<i8'), ('b', '>f8')] assert_(np.can_cast(a.dtype, t, casting='safe')) c = a.astype(t, casting='safe') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that 'same_kind' casting can change byte order and # change field widths within a "kind" t = [('a', '<i4'), ('b', '>f4')] assert_(np.can_cast(a.dtype, t, casting='same_kind')) c = a.astype(t, casting='same_kind') assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)), [True, True]) # Check that casting fails if the casting rule should fail on # any of the fields t = [('a', '>i8'), ('b', '<f4')] assert_(not np.can_cast(a.dtype, t, casting='safe')) assert_raises(TypeError, a.astype, t, casting='safe') t = [('a', '>i2'), ('b', '<f8')] assert_(not np.can_cast(a.dtype, t, casting='equiv')) assert_raises(TypeError, a.astype, t, casting='equiv') t = [('a', '>i8'), ('b', '<i2')] assert_(not np.can_cast(a.dtype, t, casting='same_kind')) assert_raises(TypeError, a.astype, t, casting='same_kind') assert_(not np.can_cast(a.dtype, b.dtype, casting='no')) assert_raises(TypeError, a.astype, b.dtype, casting='no') # Check that non-'unsafe' casting can't change the set of field names for casting in ['no', 'safe', 'equiv', 'same_kind']: t = [('a', '>i4')] assert_(not np.can_cast(a.dtype, t, casting=casting)) t = [('a', '>i4'), ('b', '<f8'), ('c', 'i4')] assert_(not np.can_cast(a.dtype, t, casting=casting))
Example 40
def __call__(self, a, b, *args, **kwargs): """ Execute the call behavior. """ # Get the data, as ndarray (da, db) = (getdata(a), getdata(b)) # Get the result with np.errstate(): np.seterr(divide='ignore', invalid='ignore') result = self.f(da, db, *args, **kwargs) # Get the mask for the result (ma, mb) = (getmask(a), getmask(b)) if ma is nomask: if mb is nomask: m = nomask else: m = umath.logical_or(getmaskarray(a), mb) elif mb is nomask: m = umath.logical_or(ma, getmaskarray(b)) else: m = umath.logical_or(ma, mb) # Case 1. : scalar if not result.ndim: if m: return masked return result # Case 2. : array # Revert result to da where masked if m is not nomask and m.any(): # any errors, just abort; impossible to guarantee masked values try: np.copyto(result, 0, casting='unsafe', where=m) # avoid using "*" since this may be overlaid masked_da = umath.multiply(m, da) # only add back if it can be cast safely if np.can_cast(masked_da.dtype, result.dtype, casting='safe'): result += masked_da except: pass # Transforms to a (subclass of) MaskedArray masked_result = result.view(get_masked_subclass(a, b)) masked_result._mask = m if isinstance(a, MaskedArray): masked_result._update_from(a) elif isinstance(b, MaskedArray): masked_result._update_from(b) return masked_result