def __init__(self, axis, **keywords): self.axis = axis if self.axis < 0: self.slice = [Ellipsis] + (-self.axis) * [slice(None)] else: self.slice = (self.axis+1) * [slice(None)] + [Ellipsis] Operator.__init__(self, **keywords)
def __init__(self, fwhm=None, sigma=None, iter=3, lmax=None, mmax=None, use_weights=False, datapath=None, dtype=float, pol=True, **keywords): """ Keywords are passed to the Healpy function smoothing. """ if fwhm is None and sigma is None: raise ValueError('The convolution width is not specified.') if fwhm is not None and sigma is not None: raise ValueError('Ambiguous convolution width specification.') if fwhm is not None and fwhm < 0 or sigma is not None and sigma < 0: raise ValueError('The convolution width is not positive.') if fwhm in (0, None) and sigma in (0, None): self.__class__ = IdentityOperator self.__init__(**keywords) return if fwhm is None: fwhm = sigma * np.sqrt(8 * np.log(2)) if sigma is None: sigma = fwhm / np.sqrt(8 * np.log(2)) self.fwhm = fwhm self.sigma = sigma self.iter = iter self.lmax = lmax self.mmax = mmax self.use_weights = use_weights self.datapath = datapath self.pol = pol Operator.__init__(self, dtype=dtype, **keywords)
def __init__(self, mask, **keywords): mask = np.array(mask, np.bool8, copy=False) Operator.__init__(self, shapein=mask.shape, shapeout=np.sum(mask == 0), dtype=var.FLOAT_DTYPE, **keywords) self.mask = mask self.set_rule('.T', lambda s: UnpackOperator(s.mask)) self.set_rule('.{UnpackOperator}', self._rule_left_unpack, CompositionOperator) self.set_rule('{UnpackOperator}.', self._rule_right_unpack, CompositionOperator)
def __init__(self, factor, **keywords): if factor == 1: self.__class__ = IdentityOperator self.__init__(**keywords) return self.factor = int(factor) keywords['dtype'] = float Operator.__init__(self, **keywords)
def __init__(self, tau, **keywords): """ """ if hasattr(tau, 'SI'): tau = tau.SI if tau.unit != '': raise ValueError('The time constant must be dimensionless.') self.tau = np.array(tau, dtype=var.FLOAT_DTYPE, ndmin=1) Operator.__init__(self, **keywords)
def __init__(self, axis=-1, scalar=1., **keywords): if scalar == 0: self.__class__ = ZeroOperator self.__init__(flags='square', **keywords) return Operator.__init__(self, dtype=var.FLOAT_DTYPE, **keywords) self.axis = axis self.scalar = scalar self.set_rule('{HomothetyOperator}.', lambda o,s: DdTddOperator( self.axis, o.data * s.scalar), CompositionOperator)
def __init__(self, mask, operator=MPI.SUM, commin=MPI.COMM_WORLD, **keywords): commout = commin shapeout = mask.size - int(np.sum(mask)) shapein = distribute_shape(mask.shape, comm=commin) Operator.__init__(self, shapein=shapein, shapeout=shapeout, commin=commin, commout=commout, **keywords) self.mask = mask self.chunk_size = product(mask.shape[1:]) self.operator = operator
def __init__(self, n, axis=None, **keywords): Operator.__init__(self, dtype=var.FLOAT_DTYPE, **keywords) if axis is None: axis = -1 if isscalar(n) else range(-len(n), 0) self.axis = (axis,) if isscalar(axis) else tuple(axis) self.n = (n,) * len(self.axis) if isscalar(n) else \ tuple([np.array(m,int) for m in n]) if len(self.axis) != len(self.n): raise ValueError('There is a mismatch between the number of axes an' 'd offsets.')
def __init__(self, left=0, right=0., **keywords): left = int(left) right = int(right) if left < 0: raise ValueError('Left padding is not positive.') if right < 0: raise ValueError('Right padding is not positive.') self.left = left self.right = right Operator.__init__(self, **keywords)
def __init__(self): Operator.__init__(self) self.set_rule('T', '.') self.set_rule('C', '1') self.set_rule('.,T', '.', CompositionOperator) self.set_rule('T,.', '.', CompositionOperator) self.set_rule('.,C', '.I', AdditionOperator) self.set_rule('H,.', '.I', AdditionOperator) self.set_rule('.,C', '.I', MultiplicationOperator) self.set_rule('H,.', '.I', MultiplicationOperator)
def __init__(self, nside, convention, nest=False, dtype=float, **keywords): if not isinstance(convention, str): raise TypeError("The input convention '{0}' is not a string.". format(convention)) convention_ = convention.replace(' ', '').lower() if convention_ not in self.CONVENTIONS: raise ValueError( "Invalid spherical convention '{0}'. Expected values are {1}.". format(convention, strenum(self.CONVENTIONS))) self.nside = int(nside) self.convention = convention_ self.nest = bool(nest) Operator.__init__(self, dtype=dtype, **keywords)
def __init__(self, wcs, **keywords): """ wcs : FITS header or Kapteyn wcs.Projection instance Representation of the world coordinate system """ import kapteyn.wcs as kwcs if isinstance(wcs, pyfits.Header): wcs = kwcs.Projection(wcs) if 'dtype' not in keywords: keywords['dtype'] = float wcs.allow_invalid = True Operator.__init__(self, **keywords) self.wcs = wcs
def __init__(self, fft_filter, ncorrelations, nsamples, fftw_flags=['measure', 'unaligned']): filter_length = fft_filter.shape[-1] array = np.empty(filter_length, dtype=var.FLOAT_DTYPE) self.fft_filter = fft_filter self.filter_length = filter_length self.left = ncorrelations self.right = filter_length - nsamples - ncorrelations self.fftw_flags = fftw_flags self.fplan = fftw3.Plan(array, direction='forward', flags=fftw_flags, realtypes=['halfcomplex r2c'], nthreads=1) self.bplan = fftw3.Plan(array, direction='backward', flags=fftw_flags, realtypes=['halfcomplex c2r'], nthreads=1) Operator.__init__(self, shapein=(fft_filter.shape[0], nsamples))
def __init__(self, wcs, origin=0, **keywords): """ wcs : FITS header or astropy.wcs.WCS instance Representation of the world coordinate system origin : int, optional Indexing convention : 0 for 0-based indexing such as C or numpy), 1 for FITS or Fortran 1-based indexing. """ if not isinstance(wcs, astropy.wcs.WCS): wcs = astropy.wcs.WCS(wcs) if 'dtype' not in keywords: keywords['dtype'] = float Operator.__init__(self, **keywords) self.wcs = wcs self.origin = origin
def __init__(self, xin, xout, **keywords): xin = np.asarray(xin) xout = np.asarray(xout) if xin.shape[-1] != 2: raise ValueError('The shape of the object plane coordinates should' ' be (npoints,ndims), where ndims is only impleme' 'nted for 2.') if xin.shape != xout.shape: raise ValueError('The object and image coordinates do not have the' ' same shape.') keywords['dtype'] = float Operator.__init__(self, **keywords) self.interp0 = interp.CloughTocher2DInterpolator(xin, xout[..., 0]) self.interp1 = interp.CloughTocher2DInterpolator(xin, xout[..., 1]) self.set_rule('I', lambda s: DistortionOperator(xout, xin))
def __new__(cls, fft_filter=None, ncorrelations=None, nsamples=None): if fft_filter is None: return Operator.__new__(cls) filter_length = fft_filter.shape[-1] invntt = DiagonalOperator(fft_filter) fft = FftHalfComplexOperator(filter_length) padding = PadOperator(left=ncorrelations, right=filter_length - \ nsamples - ncorrelations) return padding.T * fft.T * invntt * fft * padding
def __init__(self, size, fftw_flags=['measure', 'unaligned'], **keywords): size = int(size) array1 = np.empty(size, dtype=var.FLOAT_DTYPE) array2 = np.empty(size, dtype=var.FLOAT_DTYPE) fplan = fftw3.Plan(array1, array2, direction='forward', flags= \ fftw_flags, realtypes=['halfcomplex r2c'], nthreads=1) bplan = fftw3.Plan(array1, array2, direction='backward', flags=\ fftw_flags, realtypes=['halfcomplex c2r'], nthreads=1) ifplan = fftw3.Plan(array1, direction='forward', flags=fftw_flags, realtypes=['halfcomplex r2c'], nthreads=1) ibplan = fftw3.Plan(array1, direction='backward', flags=fftw_flags, realtypes=['halfcomplex c2r'], nthreads=1) self.size = size self.fftw_flags = fftw_flags self.fplan = fplan self.bplan = bplan self.ifplan = ifplan self.ibplan = ibplan Operator.__init__(self, **keywords)
def test_zero5(): z = ZeroOperator() o = Operator(shapein=3, shapeout=6, flags='linear') zo = z*o oz = o*z assert_is_instance(zo, ZeroOperator, 'zo') assert_equal(zo.shapein, o.shapein, 'zo in') assert_is_none(zo.shapeout, 'zo out') assert_is_instance(oz, ZeroOperator, 'oz') assert_is_none(oz.shapein, 'oz, in') assert_equal(oz.shapeout, o.shapeout, 'oz, out')
def test_mul(): opnl = Operator(shapein=10, flags='square') oplin = Operator(flags='linear,square', shapein=10) clss = ((BlockRowOperator, BlockDiagonalOperator, BlockRowOperator), 3 * (BlockDiagonalOperator, ), (BlockDiagonalOperator, BlockColumnOperator, BlockColumnOperator), (BlockRowOperator, BlockColumnOperator, AdditionOperator)) def func(op, cls1, cls2, cls3): operation = CompositionOperator \ if op.flags.linear else MultiplicationOperator op1 = cls1(3 * [op], axisin=0) op2 = cls2(3 * [op], axisout=0) result = op1 * op2 assert_is_type(result, cls3) assert_is_type(result.operands[0], operation) for op in opnl, oplin: for cls1, cls2, cls3 in clss: yield func, op, cls1, cls2, cls3
def test_square(): op = Operator(shapein=3, shapeout=3) assert op.flags.square op._reset(shapeout=4) assert not op.flags.square op = Operator(shapein=3, shapeout=3) op._reset(shapein=4) assert not op.flags.square
def __new__(cls, obs=None, method='uncorrelated', **keywords): if obs is None: return Operator.__new__(cls) nsamples = obs.get_nsamples() comm_tod = obs.instrument.comm method = method.lower() if method not in ('uncorrelated', 'uncorrelated python'): raise ValueError("Invalid method '{0}'.".format(method)) filter = obs.get_filter_uncorrelated(**keywords) if filter.ndim == 2: filter = filter[np.newaxis, ...] nfilters = filter.shape[0] if nfilters != 1 and nfilters != len(nsamples): raise ValueError( "Incompatible number of filters '{0}'. Expected nu" "mber is '{1}'".format(nfilters, len(nsamples))) ncorrelations = filter.shape[-1] - 1 filter_length = np.asarray(2**np.ceil(np.log2(np.array(nsamples) + \ 2 * ncorrelations)), int) fft_filters = [] for i, n in enumerate(filter_length): i = min(i, nfilters - 1) fft_filter, status = tmf.fft_filter_uncorrelated(filter[i].T, n) if status != 0: raise RuntimeError() np.maximum(fft_filter, 0, fft_filter) fft_filters.append(fft_filter.T) norm = comm_tod.allreduce(max(np.max(f) for f in fft_filters), op=MPI.MAX) for f in fft_filters: np.divide(f, norm, f) if method == 'uncorrelated': cls = InvNttUncorrelatedOperator else: cls = InvNttUncorrelatedPythonOperator #XXX should generate BlockDiagonalOperator with no duplicates... return BlockDiagonalOperator([cls(f, ncorrelations, n) \ for f, n in zip(fft_filters, nsamples)], axisin=-1)
def __new__(cls, obs=None, method='uncorrelated', **keywords): if obs is None: return Operator.__new__(cls) nsamples = obs.get_nsamples() comm_tod = obs.instrument.comm method = method.lower() if method not in ('uncorrelated', 'uncorrelated python'): raise ValueError("Invalid method '{0}'.".format(method)) filter = obs.get_filter_uncorrelated(**keywords) if filter.ndim == 2: filter = filter[np.newaxis,...] nfilters = filter.shape[0] if nfilters != 1 and nfilters != len(nsamples): raise ValueError("Incompatible number of filters '{0}'. Expected nu" "mber is '{1}'".format(nfilters, len(nsamples))) ncorrelations = filter.shape[-1] - 1 filter_length = np.asarray(2**np.ceil(np.log2(np.array(nsamples) + \ 2 * ncorrelations)), int) fft_filters = [] for i, n in enumerate(filter_length): i = min(i, nfilters-1) fft_filter, status = tmf.fft_filter_uncorrelated(filter[i].T, n) if status != 0: raise RuntimeError() np.maximum(fft_filter, 0, fft_filter) fft_filters.append(fft_filter.T) norm = comm_tod.allreduce(max(np.max(f) for f in fft_filters), op=MPI.MAX) for f in fft_filters: np.divide(f, norm, f) if method == 'uncorrelated': cls = InvNttUncorrelatedOperator else: cls = InvNttUncorrelatedPythonOperator #XXX should generate BlockDiagonalOperator with no duplicates... return BlockDiagonalOperator([cls(f, ncorrelations, n) \ for f, n in zip(fft_filters, nsamples)], axisin=-1)
def __init__(self, nside, nest=False, dtype=float, **keywords): self.nside = int(nside) self.nest = bool(nest) Operator.__init__(self, dtype=dtype, **keywords)
def __init__(self, mask, **keywords): mask = np.array(mask, np.bool8, copy=False) Operator.__init__(self, shapein=np.sum(mask == 0), shapeout=mask.shape, dtype=var.FLOAT_DTYPE, **keywords) self.mask = mask self.set_rule('.T', lambda s: PackOperator(s.mask))
def __init__(self, x): Operator.__init__(self, dtype=float) self.x = x
from __future__ import division import numpy as np from numpy.testing import assert_equal from pyoperators import ( CompositionOperator, ConstantOperator, Operator, ZeroOperator, flags, rule_manager, O) from pyoperators.utils import ndarraywrap from pyoperators.utils.testing import ( assert_is, assert_is_instance, assert_is_none, assert_same, assert_is_type) from .common import OPS, ndarray2, attr2 op = Operator() ops = [_() for _ in OPS] + [_(flags={'linear': False}) for _ in OPS] zeros_left = ( ZeroOperator(classout=ndarray2, attrout=attr2), ZeroOperator(shapein=4, classout=ndarray2, attrout=attr2)) zeros_right = ( ZeroOperator(classout=ndarray2, attrout=attr2), ZeroOperator(classout=ndarray2, attrout=attr2, flags='square'), ZeroOperator(shapein=3, classout=ndarray2, attrout=attr2), ZeroOperator(shapein=3, shapeout=3, classout=ndarray2, attrout=attr2)) def test_zero1(): z = ZeroOperator() o = Operator(shapein=3, shapeout=6) zo = z(o) assert_is_instance(zo, ZeroOperator) assert_equal(zo.shapein, o.shapein) assert_is_none(zo.shapeout)
def __init__(self, pointing, **keywords): if any(k not in pointing.dtype.names for k in ('ra', 'dec', 'pa')): raise TypeError('The input is not an equatorial pointing.') self.pointing = pointing Operator.__init__(self, **keywords)
def __init__(self, arg1, value, arg3, mykey=None, **keywords): Operator.__init__(self, **keywords) self.arg1 = arg1 self.value = value self.arg3 = arg3 self.mykey = mykey
def __init__(self, i, **keywords): self.i = i Operator.__init__(self, dtype=np.array(mat).dtype, **keywords)
def test_str(): op = Operator() op.delete() assert str(op) == 'deleted' assert repr(op) == 'DeletedOperator()'
def test_zero4(): z = ZeroOperator() o = Operator(flags='linear') assert_is_instance(z*o, ZeroOperator) assert_is_instance(o*z, ZeroOperator)
def __init__(self, shapeout=4, **keywords): Operator.__init__(self, shapeout=shapeout, classout=ndarray1, attrout=attr1, **keywords)
def __init__(self, value=3., **keywords): self.value = np.array(value) Operator.__init__(self, **keywords) self.set_rule((HomothetyOperator, '.'), lambda o, s: AbsorbLeftOperator(s.value * o.data), CompositionOperator)
def __init__(self, **keywords): Operator.__init__(self, classout=ndarray1, attrout=attr1, **keywords)
def test_is_not_scalar(): def func(x): assert not isscalarlike(x) for x in ([], (), np.ones((0, 1)), np.ones(1), np.ones(2), object, np.int8, slice, Operator()): yield func, x
def __init__(self, shapein=3, **keywords): Operator.__init__(self, shapein=shapein, classout=ndarray1, attrout=attr1, **keywords)
def __init__(self, axis=-1, **keywords): Operator.__init__(self, dtype=var.FLOAT_DTYPE, **keywords) self.axis = axis self.set_rule('.T.', self._rule_ddtdd, CompositionOperator)
def __init__(self, value=3., **keywords): self.value = np.array(value) Operator.__init__(self, **keywords) self.set_rule(('.', HomothetyOperator), lambda s, o: AbsorbRightOperator(s.value * o.data), CompositionOperator)
def __init__(self, value, **keywords): Operator.__init__(self, **keywords) self.value = value