def rotate(coords, angle, out=None): """ Rotate one or more points around the origin. Parameters ---------- coords : array-like, last dimension is 2 The (X, Y) coordinates of the points. angle : float Angle of counter-clockwise rotation in degrees. out : ndarray, optional If provided, the calculation is done into this array. """ from pyoperators.utils import isalias coords = _np.asarray(coords) if out is None: out = _np.empty_like(coords) if coords.dtype.char == 'd' and coords.flags.c_contiguous and \ out.flags.c_contiguous: if isalias(coords, out): _flib.geometry.rotate_2d_inplace(out.reshape((-1, 2)).T, angle) else: _flib.geometry.rotate_2d(coords.reshape((-1, 2)).T, out.reshape((-1, 2)).T, angle) return out if coords.dtype.itemsize > 8: angle = _np.asarray(angle, coords.dtype) angle = _np.radians(angle) m = _np.asarray([[_np.cos(angle), -_np.sin(angle)], [_np.sin(angle), _np.cos(angle)]], coords.dtype) if isalias(coords, out): coords = coords.copy() return _np.dot(coords, m.T, out)
def func(s, v): if s is setattr_unpacked: assert_raises(TypeError, s, layout, 'key', v) return s(layout, 'key', v) assert isalias(layout.key, val) assert isalias(layout.all.key, val) assert_same(layout.key, val) assert_same(layout.all.key, val.reshape(shape))
def func(s, v): layout = PackedTable(shape, key=np.ones(shape, int)) s(layout, 'key', v) assert_same(layout.key, val.ravel()) assert_equal(layout.key.dtype, float) assert_same(layout.all.key, val) assert_equal(layout.all.key.dtype, float) if s is setattr: assert isalias(layout.key, val) assert isalias(layout.all.key, val) else: assert not isalias(layout.key, val) assert not isalias(layout.all.key, val)
def distance(shape, center=None, scale=1, dtype=float, out=None): """ Returns an array whose values are the distances to a given center. Parameters ---------- shape : tuple of integer dimensions of the output array. For a 2d array, the first integer is for the Y-axis and the second one for the X-axis. center : array-like, optional The coordinates (x0, y0, ...) of the point from which the distance is calculated, assuming a zero-based coordinate indexing. Default value is the array center. scale : float or array-like, optional Inter-pixel distance (dx, dy, ...). If scale is a Quantity, its unit will be carried over to the returned distance array dtype : np.dtype, optional The output data type. Example ------- nx, ny = 3, 3 print(distance((ny,nx))) [[ 1.41421356 1. 1.41421356] [ 1. 0. 1. ] [ 1.41421356 1. 1.41421356]] """ shape = tointtuple(shape) dtype = np.dtype(dtype) unit = getattr(scale, '_unit', None) if out is None: out = np.empty(shape, dtype) ndim = out.ndim dtype = float_intrinsic_dtype(out.dtype) if ndim in (1, 2) and dtype != out.dtype: out_ = np.empty(shape, dtype) else: out_ = out if center is None: center = (np.array(shape[::-1]) - 1) / 2 else: center = np.ascontiguousarray(center, dtype) if isscalarlike(scale): scale = np.resize(scale, out.ndim) scale = np.ascontiguousarray(scale, dtype) if ndim in (1, 2): fname = 'distance_{0}d_r{1}'.format(ndim, dtype.itemsize) func = getattr(flib.datautils, fname) if ndim == 1: func(out_, center[0], scale[0]) else: func(out_.T, center, scale) if not isalias(out, out_): out[...] = out_ else: _distance_slow(shape, center, scale, dtype, out) return Map(out, copy=False, unit=unit)
def direct(self, input, output): input_, ishape, istride = _ravel_strided(input) if isalias(input, output): tmf.fft_plan_inplace(input_, ishape[0], ishape[1], istride, self.ifplan._get_parameter()) return output_, oshape, ostride = _ravel_strided(output) tmf.fft_plan_outplace(input_, ishape[0], ishape[1], istride, output_, oshape[1], ostride, self.fplan._get_parameter())
def transpose(self, input, output): input_, ishape, istride = _ravel_strided(input) if isalias(input, output): tmf.fft_plan_inplace(input_, ishape[0], ishape[1], istride, self.ibplan._get_parameter()) else: output_, oshape, ostride = _ravel_strided(output) tmf.fft_plan_outplace(input_, ishape[0], ishape[1], istride, output_, oshape[1], ostride, self.bplan._get_parameter()) output /= self.size
def direct(self, input, output): input_, ishape, istride = _ravel_strided(input) if isalias(input, output): tmf.operators.invntt_uncorrelated_inplace(input_, ishape[1], istride, self.fft_filter.T, self.fplan._get_parameter(), self.bplan._get_parameter(), self.left, self.right) return output_, oshape, ostride = _ravel_strided(output) tmf.operators.invntt_uncorrelated_outplace(input_, ishape[1], istride, output_, ostride, self.fft_filter.T, self.fplan._get_parameter(), self.bplan._get_parameter(), self.left, self.right)
def direct(self, input, output): input_, ishape, istride = _ravel_strided(input) if isalias(input, output): tmf.operators.invntt_uncorrelated_inplace( input_, ishape[1], istride, self.fft_filter.T, self.fplan._get_parameter(), self.bplan._get_parameter(), self.left, self.right) return output_, oshape, ostride = _ravel_strided(output) tmf.operators.invntt_uncorrelated_outplace(input_, ishape[1], istride, output_, ostride, self.fft_filter.T, self.fplan._get_parameter(), self.bplan._get_parameter(), self.left, self.right)
def direct(self, input, output): mask = self.mask.view(np.int8).ravel() if isalias(input, output): tmf.operators.unpack_inplace(output.ravel(), mask, input.size) else: tmf.operators.unpack_outplace(input, mask, output.ravel())
def unpack(self, x, out=None, missing_value=None, copy=False): """ Convert a 1-dimensional array into a multi-dimensional array which includes the non-selected components, mimicking the multi-dimensional layout. Parameters ---------- x : ndarray Array to be unpacked, whose first dimension is the number of selected components. out : ndarray, optional Placeholder for the unpacked array. missing_value : any, optional The value to be used for non-selected components. copy : boolean, optional Setting this keyword to True ensures that the output is not a view of the input x. Returns ------- output : ndarray Unpacked array, whose first dimensions are equal to those of the table attributes. See Also -------- pack : inverse method. Notes ----- Unless the 'copy' keyword is set to True, this method does not make a copy if it simply does a reshape (when all components are selected and no ordering is specified). """ if x is None: return None x = np.array(x, copy=False, ndmin=1, subok=True) if x.shape[0] not in (1, len(self)): raise ValueError( "Invalid input packed shape '{0}'. The expected first dimensio" "n is '{1}'.".format(x.shape, len(self))) index = self._indexable unpacked_shape = self._shape_actual + x.shape[1:] flat_shape = (self._size_actual,) + x.shape[1:] has_out = out is not None if has_out: if not isinstance(out, np.ndarray): raise TypeError('The output array is not an ndarray.') if out.shape != unpacked_shape: raise ValueError( "The output array shape '{0}' is invalid. The expected sha" "pe is '{1}'.".format(out.shape, unpacked_shape)) else: if index is Ellipsis and not copy and \ x.shape[0] == len(self) and self.comm.size == 1: return x.reshape(unpacked_shape) out = np.empty(unpacked_shape, dtype=x.dtype).view(type(x)) if out.__array_finalize__ is not None: out.__array_finalize__(x) if self._size_actual > len(self): if missing_value is None: missing_value = self._get_default_missing_value(x.dtype) out[...] = missing_value elif index is Ellipsis and x.shape[0] == len(self) and \ self.comm.size == 1: out[...] = x.reshape(unpacked_shape) return out out_ = out.reshape(flat_shape) if self.comm.size == 1: out_[index] = x else: ix = self.comm.allgather((index, x)) for i_, x_ in ix: out_[i_] = x_ if has_out and not isalias(out, out_): out[...] = out_.reshape(out.shape) return out
def unpack(self, x, out=None, missing_value=None, copy=False): """ Convert a 1-dimensional array into a multi-dimensional array which includes the non-selected components, mimicking the multi-dimensional layout. Parameters ---------- x : ndarray Array to be unpacked, whose first dimension is the number of selected components. out : ndarray, optional Placeholder for the unpacked array. missing_value : any, optional The value to be used for non-selected components. copy : boolean, optional Setting this keyword to True ensures that the output is not a view of the input x. Returns ------- output : ndarray Unpacked array, whose first dimensions are equal to those of the table attributes. See Also -------- pack : inverse method. Notes ----- Unless the 'copy' keyword is set to True, this method does not make a copy if it simply does a reshape (when all components are selected and no ordering is specified). """ if x is None: return None x = np.array(x, copy=False, ndmin=1, subok=True) if x.shape[0] not in (1, len(self)): raise ValueError( "Invalid input packed shape '{0}'. The expected first dimensio" "n is '{1}'.".format(x.shape, len(self))) index = self._indexable unpacked_shape = self._shape_actual + x.shape[1:] flat_shape = (self._size_actual, ) + x.shape[1:] has_out = out is not None if has_out: if not isinstance(out, np.ndarray): raise TypeError('The output array is not an ndarray.') if out.shape != unpacked_shape: raise ValueError( "The output array shape '{0}' is invalid. The expected sha" "pe is '{1}'.".format(out.shape, unpacked_shape)) else: if index is Ellipsis and not copy and \ x.shape[0] == len(self) and self.comm.size == 1: return x.reshape(unpacked_shape) out = np.empty(unpacked_shape, dtype=x.dtype).view(type(x)) if out.__array_finalize__ is not None: out.__array_finalize__(x) if self._size_actual > len(self): if missing_value is None: missing_value = self._get_default_missing_value(x.dtype) out[...] = missing_value elif index is Ellipsis and x.shape[0] == len(self) and \ self.comm.size == 1: out[...] = x.reshape(unpacked_shape) return out out_ = out.reshape(flat_shape) if self.comm.size == 1: out_[index] = x else: ix = self.comm.allgather((index, x)) for i_, x_ in ix: out_[i_] = x_ if has_out and not isalias(out, out_): out[...] = out_.reshape(out.shape) return out